lipo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**************************************************************************/
  2. /* lipo.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "lipo.h"
  31. #include "macho.h"
  32. bool LipO::is_lipo(const String &p_path) {
  33. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ);
  34. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  35. uint32_t magic = fb->get_32();
  36. return (magic == 0xbebafeca || magic == 0xcafebabe || magic == 0xbfbafeca || magic == 0xcafebabf);
  37. }
  38. bool LipO::create_file(const String &p_output_path, const Vector<String> &p_files) {
  39. close();
  40. fa = FileAccess::open(p_output_path, FileAccess::WRITE);
  41. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_output_path));
  42. uint64_t max_size = 0;
  43. for (int i = 0; i < p_files.size(); i++) {
  44. {
  45. MachO mh;
  46. if (!mh.open_file(p_files[i])) {
  47. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid MachO file: \"%s\".", p_files[i]));
  48. }
  49. FatArch arch;
  50. arch.cputype = mh.get_cputype();
  51. arch.cpusubtype = mh.get_cpusubtype();
  52. arch.offset = 0;
  53. arch.size = mh.get_size();
  54. arch.align = mh.get_align();
  55. max_size += arch.size;
  56. archs.push_back(arch);
  57. }
  58. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  59. if (fb.is_null()) {
  60. close();
  61. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  62. }
  63. }
  64. // Write header.
  65. bool is_64 = (max_size >= std::numeric_limits<uint32_t>::max());
  66. if (is_64) {
  67. fa->store_32(0xbfbafeca);
  68. } else {
  69. fa->store_32(0xbebafeca);
  70. }
  71. fa->store_32(BSWAP32(archs.size()));
  72. uint64_t offset = archs.size() * (is_64 ? 32 : 20) + 8;
  73. for (int i = 0; i < archs.size(); i++) {
  74. archs.write[i].offset = offset + PAD(offset, uint64_t(1) << archs[i].align);
  75. if (is_64) {
  76. fa->store_32(BSWAP32(archs[i].cputype));
  77. fa->store_32(BSWAP32(archs[i].cpusubtype));
  78. fa->store_64(BSWAP64(archs[i].offset));
  79. fa->store_64(BSWAP64(archs[i].size));
  80. fa->store_32(BSWAP32(archs[i].align));
  81. fa->store_32(0);
  82. } else {
  83. fa->store_32(BSWAP32(archs[i].cputype));
  84. fa->store_32(BSWAP32(archs[i].cpusubtype));
  85. fa->store_32(BSWAP32(archs[i].offset));
  86. fa->store_32(BSWAP32(archs[i].size));
  87. fa->store_32(BSWAP32(archs[i].align));
  88. }
  89. offset = archs[i].offset + archs[i].size;
  90. }
  91. // Write files and padding.
  92. for (int i = 0; i < archs.size(); i++) {
  93. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  94. if (fb.is_null()) {
  95. close();
  96. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  97. }
  98. uint64_t cur = fa->get_position();
  99. for (uint64_t j = cur; j < archs[i].offset; j++) {
  100. fa->store_8(0);
  101. }
  102. int pages = archs[i].size / 4096;
  103. int remain = archs[i].size % 4096;
  104. unsigned char step[4096];
  105. for (int j = 0; j < pages; j++) {
  106. uint64_t br = fb->get_buffer(step, 4096);
  107. if (br > 0) {
  108. fa->store_buffer(step, br);
  109. }
  110. }
  111. uint64_t br = fb->get_buffer(step, remain);
  112. if (br > 0) {
  113. fa->store_buffer(step, br);
  114. }
  115. }
  116. return true;
  117. }
  118. bool LipO::create_file(const String &p_output_path, const Vector<String> &p_files, const Vector<Vector2i> &p_cputypes) {
  119. close();
  120. fa = FileAccess::open(p_output_path, FileAccess::WRITE);
  121. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_output_path));
  122. ERR_FAIL_COND_V(p_files.size() != p_cputypes.size(), false);
  123. uint64_t max_size = 0;
  124. for (int i = 0; i < p_files.size(); i++) {
  125. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  126. if (fb.is_null()) {
  127. close();
  128. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  129. }
  130. {
  131. FatArch arch;
  132. MachO mh;
  133. if (MachO::is_macho(p_files[i]) && mh.open_file(p_files[i])) {
  134. arch.cputype = mh.get_cputype();
  135. arch.cpusubtype = mh.get_cpusubtype();
  136. arch.offset = 0;
  137. arch.size = mh.get_size();
  138. arch.align = mh.get_align();
  139. ERR_FAIL_V_MSG(arch.cputype != (uint32_t)p_cputypes[i].x || arch.cpusubtype != (uint32_t)p_cputypes[i].y, vformat("Mismatching MachO architecture: \"%s\".", p_files[i]));
  140. } else {
  141. arch.cputype = (uint32_t)p_cputypes[i].x;
  142. arch.cpusubtype = (uint32_t)p_cputypes[i].y;
  143. arch.offset = 0;
  144. arch.size = fb->get_length();
  145. arch.align = 0x03;
  146. }
  147. max_size += arch.size;
  148. archs.push_back(arch);
  149. }
  150. }
  151. // Write header.
  152. bool is_64 = (max_size >= std::numeric_limits<uint32_t>::max());
  153. if (is_64) {
  154. fa->store_32(0xbfbafeca);
  155. } else {
  156. fa->store_32(0xbebafeca);
  157. }
  158. fa->store_32(BSWAP32(archs.size()));
  159. uint64_t offset = archs.size() * (is_64 ? 32 : 20) + 8;
  160. for (int i = 0; i < archs.size(); i++) {
  161. archs.write[i].offset = offset + PAD(offset, uint64_t(1) << archs[i].align);
  162. if (is_64) {
  163. fa->store_32(BSWAP32(archs[i].cputype));
  164. fa->store_32(BSWAP32(archs[i].cpusubtype));
  165. fa->store_64(BSWAP64(archs[i].offset));
  166. fa->store_64(BSWAP64(archs[i].size));
  167. fa->store_32(BSWAP32(archs[i].align));
  168. fa->store_32(0);
  169. } else {
  170. fa->store_32(BSWAP32(archs[i].cputype));
  171. fa->store_32(BSWAP32(archs[i].cpusubtype));
  172. fa->store_32(BSWAP32(archs[i].offset));
  173. fa->store_32(BSWAP32(archs[i].size));
  174. fa->store_32(BSWAP32(archs[i].align));
  175. }
  176. offset = archs[i].offset + archs[i].size;
  177. }
  178. // Write files and padding.
  179. for (int i = 0; i < archs.size(); i++) {
  180. Ref<FileAccess> fb = FileAccess::open(p_files[i], FileAccess::READ);
  181. if (fb.is_null()) {
  182. close();
  183. ERR_FAIL_V_MSG(false, vformat("LipO: Can't open file: \"%s\".", p_files[i]));
  184. }
  185. uint64_t cur = fa->get_position();
  186. for (uint64_t j = cur; j < archs[i].offset; j++) {
  187. fa->store_8(0);
  188. }
  189. int pages = archs[i].size / 4096;
  190. int remain = archs[i].size % 4096;
  191. unsigned char step[4096];
  192. for (int j = 0; j < pages; j++) {
  193. uint64_t br = fb->get_buffer(step, 4096);
  194. if (br > 0) {
  195. fa->store_buffer(step, br);
  196. }
  197. }
  198. uint64_t br = fb->get_buffer(step, remain);
  199. if (br > 0) {
  200. fa->store_buffer(step, br);
  201. }
  202. }
  203. return true;
  204. }
  205. bool LipO::open_file(const String &p_path) {
  206. close();
  207. fa = FileAccess::open(p_path, FileAccess::READ);
  208. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  209. uint32_t magic = fa->get_32();
  210. if (magic == 0xbebafeca) {
  211. // 32-bit fat binary, bswap.
  212. uint32_t nfat_arch = BSWAP32(fa->get_32());
  213. for (uint32_t i = 0; i < nfat_arch; i++) {
  214. FatArch arch;
  215. arch.cputype = BSWAP32(fa->get_32());
  216. arch.cpusubtype = BSWAP32(fa->get_32());
  217. arch.offset = BSWAP32(fa->get_32());
  218. arch.size = BSWAP32(fa->get_32());
  219. arch.align = BSWAP32(fa->get_32());
  220. archs.push_back(arch);
  221. }
  222. } else if (magic == 0xcafebabe) {
  223. // 32-bit fat binary.
  224. uint32_t nfat_arch = fa->get_32();
  225. for (uint32_t i = 0; i < nfat_arch; i++) {
  226. FatArch arch;
  227. arch.cputype = fa->get_32();
  228. arch.cpusubtype = fa->get_32();
  229. arch.offset = fa->get_32();
  230. arch.size = fa->get_32();
  231. arch.align = fa->get_32();
  232. archs.push_back(arch);
  233. }
  234. } else if (magic == 0xbfbafeca) {
  235. // 64-bit fat binary, bswap.
  236. uint32_t nfat_arch = BSWAP32(fa->get_32());
  237. for (uint32_t i = 0; i < nfat_arch; i++) {
  238. FatArch arch;
  239. arch.cputype = BSWAP32(fa->get_32());
  240. arch.cpusubtype = BSWAP32(fa->get_32());
  241. arch.offset = BSWAP64(fa->get_64());
  242. arch.size = BSWAP64(fa->get_64());
  243. arch.align = BSWAP32(fa->get_32());
  244. fa->get_32(); // Skip, reserved.
  245. archs.push_back(arch);
  246. }
  247. } else if (magic == 0xcafebabf) {
  248. // 64-bit fat binary.
  249. uint32_t nfat_arch = fa->get_32();
  250. for (uint32_t i = 0; i < nfat_arch; i++) {
  251. FatArch arch;
  252. arch.cputype = fa->get_32();
  253. arch.cpusubtype = fa->get_32();
  254. arch.offset = fa->get_64();
  255. arch.size = fa->get_64();
  256. arch.align = fa->get_32();
  257. fa->get_32(); // Skip, reserved.
  258. archs.push_back(arch);
  259. }
  260. } else {
  261. close();
  262. ERR_FAIL_V_MSG(false, vformat("LipO: Invalid fat binary: \"%s\".", p_path));
  263. }
  264. return true;
  265. }
  266. int LipO::get_arch_count() const {
  267. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  268. return archs.size();
  269. }
  270. uint32_t LipO::get_arch_cputype(int p_index) const {
  271. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  272. ERR_FAIL_INDEX_V(p_index, archs.size(), 0);
  273. return archs[p_index].cputype;
  274. }
  275. uint32_t LipO::get_arch_cpusubtype(int p_index) const {
  276. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "LipO: File not opened.");
  277. ERR_FAIL_INDEX_V(p_index, archs.size(), 0);
  278. return archs[p_index].cpusubtype;
  279. }
  280. bool LipO::extract_arch(int p_index, const String &p_path) {
  281. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "LipO: File not opened.");
  282. ERR_FAIL_INDEX_V(p_index, archs.size(), false);
  283. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::WRITE);
  284. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("LipO: Can't open file: \"%s\".", p_path));
  285. fa->seek(archs[p_index].offset);
  286. int pages = archs[p_index].size / 4096;
  287. int remain = archs[p_index].size % 4096;
  288. unsigned char step[4096];
  289. for (int i = 0; i < pages; i++) {
  290. uint64_t br = fa->get_buffer(step, 4096);
  291. if (br > 0) {
  292. fb->store_buffer(step, br);
  293. }
  294. }
  295. uint64_t br = fa->get_buffer(step, remain);
  296. if (br > 0) {
  297. fb->store_buffer(step, br);
  298. }
  299. return true;
  300. }
  301. void LipO::close() {
  302. archs.clear();
  303. }
  304. LipO::~LipO() {
  305. close();
  306. }