macho.cpp 17 KB

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