macho.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /**************************************************************************/
  2. /* macho.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 "macho.h"
  31. #include "core/crypto/crypto_core.h"
  32. uint32_t MachO::seg_align(uint64_t p_vmaddr, uint32_t p_min, uint32_t p_max) {
  33. uint32_t salign = p_max;
  34. if (p_vmaddr != 0) {
  35. uint64_t seg_align = 1;
  36. salign = 0;
  37. while ((seg_align & p_vmaddr) == 0) {
  38. seg_align = seg_align << 1;
  39. salign++;
  40. }
  41. salign = CLAMP(salign, p_min, p_max);
  42. }
  43. return salign;
  44. }
  45. bool MachO::alloc_signature(uint64_t p_size) {
  46. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  47. if (signature_offset != 0) {
  48. // Nothing to do, already have signature load command.
  49. return true;
  50. }
  51. if (lc_limit == 0 || lc_limit + 16 > exe_base) {
  52. ERR_FAIL_V_MSG(false, "MachO: Can't allocate signature load command, please use \"codesign_allocate\" utility first.");
  53. } else {
  54. // Add signature load command.
  55. signature_offset = lc_limit;
  56. fa->seek(lc_limit);
  57. LoadCommandHeader lc;
  58. lc.cmd = LC_CODE_SIGNATURE;
  59. lc.cmdsize = 16;
  60. if (swap) {
  61. lc.cmdsize = BSWAP32(lc.cmdsize);
  62. }
  63. fa->store_buffer((const uint8_t *)&lc, sizeof(LoadCommandHeader));
  64. uint32_t lc_offset = fa->get_length() + PAD(fa->get_length(), 16);
  65. uint32_t lc_size = 0;
  66. if (swap) {
  67. lc_offset = BSWAP32(lc_offset);
  68. lc_size = BSWAP32(lc_size);
  69. }
  70. fa->store_32(lc_offset);
  71. fa->store_32(lc_size);
  72. // Write new command number.
  73. fa->seek(0x10);
  74. uint32_t ncmds = fa->get_32();
  75. uint32_t cmdssize = fa->get_32();
  76. if (swap) {
  77. ncmds = BSWAP32(ncmds);
  78. cmdssize = BSWAP32(cmdssize);
  79. }
  80. ncmds += 1;
  81. cmdssize += 16;
  82. if (swap) {
  83. ncmds = BSWAP32(ncmds);
  84. cmdssize = BSWAP32(cmdssize);
  85. }
  86. fa->seek(0x10);
  87. fa->store_32(ncmds);
  88. fa->store_32(cmdssize);
  89. lc_limit = lc_limit + sizeof(LoadCommandHeader) + 8;
  90. return true;
  91. }
  92. }
  93. bool MachO::is_macho(const String &p_path) {
  94. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ);
  95. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("MachO: Can't open file: \"%s\".", p_path));
  96. uint32_t magic = fb->get_32();
  97. return (magic == 0xcefaedfe || magic == 0xfeedface || magic == 0xcffaedfe || magic == 0xfeedfacf);
  98. }
  99. uint32_t MachO::get_filetype(const String &p_path) {
  100. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::READ);
  101. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, vformat("MachO: Can't open file: \"%s\".", p_path));
  102. uint32_t magic = fa->get_32();
  103. MachHeader mach_header;
  104. // Read MachO header.
  105. if (magic == 0xcefaedfe || magic == 0xfeedface) {
  106. // Thin 32-bit binary.
  107. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  108. } else if (magic == 0xcffaedfe || magic == 0xfeedfacf) {
  109. // Thin 64-bit binary.
  110. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  111. fa->get_32(); // Skip extra reserved field.
  112. } else {
  113. ERR_FAIL_V_MSG(0, vformat("MachO: File is not a valid MachO binary: \"%s\".", p_path));
  114. }
  115. return mach_header.filetype;
  116. }
  117. bool MachO::open_file(const String &p_path) {
  118. fa = FileAccess::open(p_path, FileAccess::READ_WRITE);
  119. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("MachO: Can't open file: \"%s\".", p_path));
  120. uint32_t magic = fa->get_32();
  121. MachHeader mach_header;
  122. // Read MachO header.
  123. swap = (magic == 0xcffaedfe || magic == 0xcefaedfe);
  124. if (magic == 0xcefaedfe || magic == 0xfeedface) {
  125. // Thin 32-bit binary.
  126. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  127. } else if (magic == 0xcffaedfe || magic == 0xfeedfacf) {
  128. // Thin 64-bit binary.
  129. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  130. fa->get_32(); // Skip extra reserved field.
  131. } else {
  132. ERR_FAIL_V_MSG(false, vformat("MachO: File is not a valid MachO binary: \"%s\".", p_path));
  133. }
  134. if (swap) {
  135. mach_header.ncmds = BSWAP32(mach_header.ncmds);
  136. mach_header.cpusubtype = BSWAP32(mach_header.cpusubtype);
  137. mach_header.cputype = BSWAP32(mach_header.cputype);
  138. }
  139. cpusubtype = mach_header.cpusubtype;
  140. cputype = mach_header.cputype;
  141. align = 0;
  142. exe_base = std::numeric_limits<uint64_t>::max();
  143. exe_limit = 0;
  144. lc_limit = 0;
  145. link_edit_offset = 0;
  146. signature_offset = 0;
  147. // Read load commands.
  148. for (uint32_t i = 0; i < mach_header.ncmds; i++) {
  149. LoadCommandHeader lc;
  150. fa->get_buffer((uint8_t *)&lc, sizeof(LoadCommandHeader));
  151. if (swap) {
  152. lc.cmd = BSWAP32(lc.cmd);
  153. lc.cmdsize = BSWAP32(lc.cmdsize);
  154. }
  155. uint64_t ps = fa->get_position();
  156. switch (lc.cmd) {
  157. case LC_SEGMENT: {
  158. LoadCommandSegment lc_seg;
  159. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  160. if (swap) {
  161. lc_seg.nsects = BSWAP32(lc_seg.nsects);
  162. lc_seg.vmaddr = BSWAP32(lc_seg.vmaddr);
  163. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  164. }
  165. align = MAX(align, seg_align(lc_seg.vmaddr, 2, 15));
  166. if (String(lc_seg.segname) == "__TEXT") {
  167. exe_limit = MAX(exe_limit, lc_seg.vmsize);
  168. for (uint32_t j = 0; j < lc_seg.nsects; j++) {
  169. Section lc_sect;
  170. fa->get_buffer((uint8_t *)&lc_sect, sizeof(Section));
  171. if (String(lc_sect.sectname) == "__text") {
  172. if (swap) {
  173. exe_base = MIN(exe_base, BSWAP32(lc_sect.offset));
  174. } else {
  175. exe_base = MIN(exe_base, lc_sect.offset);
  176. }
  177. }
  178. if (swap) {
  179. align = MAX(align, BSWAP32(lc_sect.align));
  180. } else {
  181. align = MAX(align, lc_sect.align);
  182. }
  183. }
  184. } else if (String(lc_seg.segname) == "__LINKEDIT") {
  185. link_edit_offset = ps - 8;
  186. }
  187. } break;
  188. case LC_SEGMENT_64: {
  189. LoadCommandSegment64 lc_seg;
  190. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  191. if (swap) {
  192. lc_seg.nsects = BSWAP32(lc_seg.nsects);
  193. lc_seg.vmaddr = BSWAP64(lc_seg.vmaddr);
  194. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  195. }
  196. align = MAX(align, seg_align(lc_seg.vmaddr, 3, 15));
  197. if (String(lc_seg.segname) == "__TEXT") {
  198. exe_limit = MAX(exe_limit, lc_seg.vmsize);
  199. for (uint32_t j = 0; j < lc_seg.nsects; j++) {
  200. Section64 lc_sect;
  201. fa->get_buffer((uint8_t *)&lc_sect, sizeof(Section64));
  202. if (String(lc_sect.sectname) == "__text") {
  203. if (swap) {
  204. exe_base = MIN(exe_base, BSWAP32(lc_sect.offset));
  205. } else {
  206. exe_base = MIN(exe_base, lc_sect.offset);
  207. }
  208. if (swap) {
  209. align = MAX(align, BSWAP32(lc_sect.align));
  210. } else {
  211. align = MAX(align, lc_sect.align);
  212. }
  213. }
  214. }
  215. } else if (String(lc_seg.segname) == "__LINKEDIT") {
  216. link_edit_offset = ps - 8;
  217. }
  218. } break;
  219. case LC_CODE_SIGNATURE: {
  220. signature_offset = ps - 8;
  221. } break;
  222. default: {
  223. } break;
  224. }
  225. fa->seek(ps + lc.cmdsize - 8);
  226. lc_limit = ps + lc.cmdsize - 8;
  227. }
  228. if (exe_limit == 0 || lc_limit == 0) {
  229. ERR_FAIL_V_MSG(false, vformat("MachO: No load commands or executable code found: \"%s\".", p_path));
  230. }
  231. return true;
  232. }
  233. uint64_t MachO::get_exe_base() {
  234. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  235. return exe_base;
  236. }
  237. uint64_t MachO::get_exe_limit() {
  238. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  239. return exe_limit;
  240. }
  241. int32_t MachO::get_align() {
  242. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  243. return align;
  244. }
  245. uint32_t MachO::get_cputype() {
  246. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  247. return cputype;
  248. }
  249. uint32_t MachO::get_cpusubtype() {
  250. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  251. return cpusubtype;
  252. }
  253. uint64_t MachO::get_size() {
  254. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  255. return fa->get_length();
  256. }
  257. uint64_t MachO::get_signature_offset() {
  258. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  259. ERR_FAIL_COND_V_MSG(signature_offset == 0, 0, "MachO: No signature load command.");
  260. fa->seek(signature_offset + 8);
  261. if (swap) {
  262. return BSWAP32(fa->get_32());
  263. } else {
  264. return fa->get_32();
  265. }
  266. }
  267. uint64_t MachO::get_code_limit() {
  268. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  269. if (signature_offset == 0) {
  270. return fa->get_length() + PAD(fa->get_length(), 16);
  271. } else {
  272. return get_signature_offset();
  273. }
  274. }
  275. uint64_t MachO::get_signature_size() {
  276. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  277. ERR_FAIL_COND_V_MSG(signature_offset == 0, 0, "MachO: No signature load command.");
  278. fa->seek(signature_offset + 12);
  279. if (swap) {
  280. return BSWAP32(fa->get_32());
  281. } else {
  282. return fa->get_32();
  283. }
  284. }
  285. bool MachO::is_signed() {
  286. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  287. if (signature_offset == 0) {
  288. return false;
  289. }
  290. fa->seek(get_signature_offset());
  291. uint32_t magic = BSWAP32(fa->get_32());
  292. if (magic != 0xfade0cc0) {
  293. return false; // No SuperBlob found.
  294. }
  295. fa->get_32(); // Skip size field, unused.
  296. uint32_t count = BSWAP32(fa->get_32());
  297. for (uint32_t i = 0; i < count; i++) {
  298. uint32_t index_type = BSWAP32(fa->get_32());
  299. uint32_t offset = BSWAP32(fa->get_32());
  300. if (index_type == 0x00000000) { // CodeDirectory index type.
  301. fa->seek(get_signature_offset() + offset + 12);
  302. uint32_t flags = BSWAP32(fa->get_32());
  303. if (flags & 0x20000) {
  304. return false; // Found CD, linker-signed.
  305. } else {
  306. return true; // Found CD, not linker-signed.
  307. }
  308. }
  309. }
  310. return false; // No CD found.
  311. }
  312. PackedByteArray MachO::get_cdhash_sha1() {
  313. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  314. if (signature_offset == 0) {
  315. return PackedByteArray();
  316. }
  317. fa->seek(get_signature_offset());
  318. uint32_t magic = BSWAP32(fa->get_32());
  319. if (magic != 0xfade0cc0) {
  320. return PackedByteArray(); // No SuperBlob found.
  321. }
  322. fa->get_32(); // Skip size field, unused.
  323. uint32_t count = BSWAP32(fa->get_32());
  324. for (uint32_t i = 0; i < count; i++) {
  325. fa->get_32(); // Index type, skip.
  326. uint32_t offset = BSWAP32(fa->get_32());
  327. uint64_t pos = fa->get_position();
  328. fa->seek(get_signature_offset() + offset);
  329. uint32_t cdmagic = BSWAP32(fa->get_32());
  330. uint32_t cdsize = BSWAP32(fa->get_32());
  331. if (cdmagic == 0xfade0c02) { // CodeDirectory.
  332. fa->seek(get_signature_offset() + offset + 36);
  333. uint8_t hash_size = fa->get_8();
  334. uint8_t hash_type = fa->get_8();
  335. if (hash_size == 0x14 && hash_type == 0x01) { /* SHA-1 */
  336. PackedByteArray hash;
  337. hash.resize(0x14);
  338. fa->seek(get_signature_offset() + offset);
  339. PackedByteArray blob;
  340. blob.resize(cdsize);
  341. fa->get_buffer(blob.ptrw(), cdsize);
  342. CryptoCore::SHA1Context ctx;
  343. ctx.start();
  344. ctx.update(blob.ptr(), blob.size());
  345. ctx.finish(hash.ptrw());
  346. return hash;
  347. }
  348. }
  349. fa->seek(pos);
  350. }
  351. return PackedByteArray();
  352. }
  353. PackedByteArray MachO::get_cdhash_sha256() {
  354. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  355. if (signature_offset == 0) {
  356. return PackedByteArray();
  357. }
  358. fa->seek(get_signature_offset());
  359. uint32_t magic = BSWAP32(fa->get_32());
  360. if (magic != 0xfade0cc0) {
  361. return PackedByteArray(); // No SuperBlob found.
  362. }
  363. fa->get_32(); // Skip size field, unused.
  364. uint32_t count = BSWAP32(fa->get_32());
  365. for (uint32_t i = 0; i < count; i++) {
  366. fa->get_32(); // Index type, skip.
  367. uint32_t offset = BSWAP32(fa->get_32());
  368. uint64_t pos = fa->get_position();
  369. fa->seek(get_signature_offset() + offset);
  370. uint32_t cdmagic = BSWAP32(fa->get_32());
  371. uint32_t cdsize = BSWAP32(fa->get_32());
  372. if (cdmagic == 0xfade0c02) { // CodeDirectory.
  373. fa->seek(get_signature_offset() + offset + 36);
  374. uint8_t hash_size = fa->get_8();
  375. uint8_t hash_type = fa->get_8();
  376. if (hash_size == 0x20 && hash_type == 0x02) { /* SHA-256 */
  377. PackedByteArray hash;
  378. hash.resize(0x20);
  379. fa->seek(get_signature_offset() + offset);
  380. PackedByteArray blob;
  381. blob.resize(cdsize);
  382. fa->get_buffer(blob.ptrw(), cdsize);
  383. CryptoCore::SHA256Context ctx;
  384. ctx.start();
  385. ctx.update(blob.ptr(), blob.size());
  386. ctx.finish(hash.ptrw());
  387. return hash;
  388. }
  389. }
  390. fa->seek(pos);
  391. }
  392. return PackedByteArray();
  393. }
  394. PackedByteArray MachO::get_requirements() {
  395. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  396. if (signature_offset == 0) {
  397. return PackedByteArray();
  398. }
  399. fa->seek(get_signature_offset());
  400. uint32_t magic = BSWAP32(fa->get_32());
  401. if (magic != 0xfade0cc0) {
  402. return PackedByteArray(); // No SuperBlob found.
  403. }
  404. fa->get_32(); // Skip size field, unused.
  405. uint32_t count = BSWAP32(fa->get_32());
  406. for (uint32_t i = 0; i < count; i++) {
  407. fa->get_32(); // Index type, skip.
  408. uint32_t offset = BSWAP32(fa->get_32());
  409. uint64_t pos = fa->get_position();
  410. fa->seek(get_signature_offset() + offset);
  411. uint32_t rqmagic = BSWAP32(fa->get_32());
  412. uint32_t rqsize = BSWAP32(fa->get_32());
  413. if (rqmagic == 0xfade0c01) { // Requirements.
  414. PackedByteArray blob;
  415. fa->seek(get_signature_offset() + offset);
  416. blob.resize(rqsize);
  417. fa->get_buffer(blob.ptrw(), rqsize);
  418. return blob;
  419. }
  420. fa->seek(pos);
  421. }
  422. return PackedByteArray();
  423. }
  424. const Ref<FileAccess> MachO::get_file() const {
  425. return fa;
  426. }
  427. Ref<FileAccess> MachO::get_file() {
  428. return fa;
  429. }
  430. bool MachO::set_signature_size(uint64_t p_size) {
  431. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  432. // Ensure signature load command exists.
  433. ERR_FAIL_COND_V_MSG(link_edit_offset == 0, false, "MachO: No __LINKEDIT segment found.");
  434. ERR_FAIL_COND_V_MSG(!alloc_signature(p_size), false, "MachO: Can't allocate signature load command.");
  435. // Update signature load command.
  436. uint64_t old_size = get_signature_size();
  437. uint64_t new_size = p_size + PAD(p_size, 16384);
  438. if (new_size <= old_size) {
  439. fa->seek(get_signature_offset());
  440. for (uint64_t i = 0; i < old_size; i++) {
  441. fa->store_8(0x00);
  442. }
  443. return true;
  444. }
  445. fa->seek(signature_offset + 12);
  446. if (swap) {
  447. fa->store_32(BSWAP32(new_size));
  448. } else {
  449. fa->store_32(new_size);
  450. }
  451. uint64_t end = get_signature_offset() + new_size;
  452. // Update "__LINKEDIT" segment.
  453. LoadCommandHeader lc;
  454. fa->seek(link_edit_offset);
  455. fa->get_buffer((uint8_t *)&lc, sizeof(LoadCommandHeader));
  456. if (swap) {
  457. lc.cmd = BSWAP32(lc.cmd);
  458. lc.cmdsize = BSWAP32(lc.cmdsize);
  459. }
  460. switch (lc.cmd) {
  461. case LC_SEGMENT: {
  462. LoadCommandSegment lc_seg;
  463. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  464. if (swap) {
  465. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  466. lc_seg.filesize = BSWAP32(lc_seg.filesize);
  467. lc_seg.fileoff = BSWAP32(lc_seg.fileoff);
  468. }
  469. lc_seg.vmsize = end - lc_seg.fileoff;
  470. lc_seg.vmsize += PAD(lc_seg.vmsize, 4096);
  471. lc_seg.filesize = end - lc_seg.fileoff;
  472. if (swap) {
  473. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  474. lc_seg.filesize = BSWAP32(lc_seg.filesize);
  475. }
  476. fa->seek(link_edit_offset + 8);
  477. fa->store_buffer((const uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  478. } break;
  479. case LC_SEGMENT_64: {
  480. LoadCommandSegment64 lc_seg;
  481. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  482. if (swap) {
  483. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  484. lc_seg.filesize = BSWAP64(lc_seg.filesize);
  485. lc_seg.fileoff = BSWAP64(lc_seg.fileoff);
  486. }
  487. lc_seg.vmsize = end - lc_seg.fileoff;
  488. lc_seg.vmsize += PAD(lc_seg.vmsize, 4096);
  489. lc_seg.filesize = end - lc_seg.fileoff;
  490. if (swap) {
  491. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  492. lc_seg.filesize = BSWAP64(lc_seg.filesize);
  493. }
  494. fa->seek(link_edit_offset + 8);
  495. fa->store_buffer((const uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  496. } break;
  497. default: {
  498. ERR_FAIL_V_MSG(false, "MachO: Invalid __LINKEDIT segment type.");
  499. } break;
  500. }
  501. fa->seek(get_signature_offset());
  502. for (uint64_t i = 0; i < new_size; i++) {
  503. fa->store_8(0x00);
  504. }
  505. return true;
  506. }