MachOWriter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //===- MachOWriter.cpp ------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "MachOWriter.h"
  9. #include "MachOLayoutBuilder.h"
  10. #include "Object.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/BinaryFormat/MachO.h"
  13. #include "llvm/Object/MachO.h"
  14. #include "llvm/Support/Errc.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include <memory>
  17. namespace llvm {
  18. namespace objcopy {
  19. namespace macho {
  20. size_t MachOWriter::headerSize() const {
  21. return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
  22. }
  23. size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; }
  24. size_t MachOWriter::symTableSize() const {
  25. return O.SymTable.Symbols.size() *
  26. (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist));
  27. }
  28. size_t MachOWriter::totalSize() const {
  29. // Going from tail to head and looking for an appropriate "anchor" to
  30. // calculate the total size assuming that all the offsets are either valid
  31. // ("true") or 0 (0 indicates that the corresponding part is missing).
  32. SmallVector<size_t, 7> Ends;
  33. if (O.SymTabCommandIndex) {
  34. const MachO::symtab_command &SymTabCommand =
  35. O.LoadCommands[*O.SymTabCommandIndex]
  36. .MachOLoadCommand.symtab_command_data;
  37. if (SymTabCommand.symoff)
  38. Ends.push_back(SymTabCommand.symoff + symTableSize());
  39. if (SymTabCommand.stroff)
  40. Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize);
  41. }
  42. if (O.DyLdInfoCommandIndex) {
  43. const MachO::dyld_info_command &DyLdInfoCommand =
  44. O.LoadCommands[*O.DyLdInfoCommandIndex]
  45. .MachOLoadCommand.dyld_info_command_data;
  46. if (DyLdInfoCommand.rebase_off) {
  47. assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
  48. "Incorrect rebase opcodes size");
  49. Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size);
  50. }
  51. if (DyLdInfoCommand.bind_off) {
  52. assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
  53. "Incorrect bind opcodes size");
  54. Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size);
  55. }
  56. if (DyLdInfoCommand.weak_bind_off) {
  57. assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
  58. "Incorrect weak bind opcodes size");
  59. Ends.push_back(DyLdInfoCommand.weak_bind_off +
  60. DyLdInfoCommand.weak_bind_size);
  61. }
  62. if (DyLdInfoCommand.lazy_bind_off) {
  63. assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
  64. "Incorrect lazy bind opcodes size");
  65. Ends.push_back(DyLdInfoCommand.lazy_bind_off +
  66. DyLdInfoCommand.lazy_bind_size);
  67. }
  68. if (DyLdInfoCommand.export_off) {
  69. assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
  70. "Incorrect trie size");
  71. Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size);
  72. }
  73. }
  74. if (O.DySymTabCommandIndex) {
  75. const MachO::dysymtab_command &DySymTabCommand =
  76. O.LoadCommands[*O.DySymTabCommandIndex]
  77. .MachOLoadCommand.dysymtab_command_data;
  78. if (DySymTabCommand.indirectsymoff)
  79. Ends.push_back(DySymTabCommand.indirectsymoff +
  80. sizeof(uint32_t) * O.IndirectSymTable.Symbols.size());
  81. }
  82. if (O.CodeSignatureCommandIndex) {
  83. const MachO::linkedit_data_command &LinkEditDataCommand =
  84. O.LoadCommands[*O.CodeSignatureCommandIndex]
  85. .MachOLoadCommand.linkedit_data_command_data;
  86. if (LinkEditDataCommand.dataoff)
  87. Ends.push_back(LinkEditDataCommand.dataoff +
  88. LinkEditDataCommand.datasize);
  89. }
  90. if (O.DataInCodeCommandIndex) {
  91. const MachO::linkedit_data_command &LinkEditDataCommand =
  92. O.LoadCommands[*O.DataInCodeCommandIndex]
  93. .MachOLoadCommand.linkedit_data_command_data;
  94. if (LinkEditDataCommand.dataoff)
  95. Ends.push_back(LinkEditDataCommand.dataoff +
  96. LinkEditDataCommand.datasize);
  97. }
  98. if (O.FunctionStartsCommandIndex) {
  99. const MachO::linkedit_data_command &LinkEditDataCommand =
  100. O.LoadCommands[*O.FunctionStartsCommandIndex]
  101. .MachOLoadCommand.linkedit_data_command_data;
  102. if (LinkEditDataCommand.dataoff)
  103. Ends.push_back(LinkEditDataCommand.dataoff +
  104. LinkEditDataCommand.datasize);
  105. }
  106. // Otherwise, use the last section / reloction.
  107. for (const LoadCommand &LC : O.LoadCommands)
  108. for (const std::unique_ptr<Section> &S : LC.Sections) {
  109. if (!S->hasValidOffset()) {
  110. assert((S->Offset == 0) && "Skipped section's offset must be zero");
  111. assert((S->isVirtualSection() || S->Size == 0) &&
  112. "Non-zero-fill sections with zero offset must have zero size");
  113. continue;
  114. }
  115. assert((S->Offset != 0) &&
  116. "Non-zero-fill section's offset cannot be zero");
  117. Ends.push_back(S->Offset + S->Size);
  118. if (S->RelOff)
  119. Ends.push_back(S->RelOff +
  120. S->NReloc * sizeof(MachO::any_relocation_info));
  121. }
  122. if (!Ends.empty())
  123. return *std::max_element(Ends.begin(), Ends.end());
  124. // Otherwise, we have only Mach header and load commands.
  125. return headerSize() + loadCommandsSize();
  126. }
  127. void MachOWriter::writeHeader() {
  128. MachO::mach_header_64 Header;
  129. Header.magic = O.Header.Magic;
  130. Header.cputype = O.Header.CPUType;
  131. Header.cpusubtype = O.Header.CPUSubType;
  132. Header.filetype = O.Header.FileType;
  133. Header.ncmds = O.Header.NCmds;
  134. Header.sizeofcmds = O.Header.SizeOfCmds;
  135. Header.flags = O.Header.Flags;
  136. Header.reserved = O.Header.Reserved;
  137. if (IsLittleEndian != sys::IsLittleEndianHost)
  138. MachO::swapStruct(Header);
  139. auto HeaderSize =
  140. Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
  141. memcpy(B.getBufferStart(), &Header, HeaderSize);
  142. }
  143. void MachOWriter::writeLoadCommands() {
  144. uint8_t *Begin = B.getBufferStart() + headerSize();
  145. for (const LoadCommand &LC : O.LoadCommands) {
  146. // Construct a load command.
  147. MachO::macho_load_command MLC = LC.MachOLoadCommand;
  148. switch (MLC.load_command_data.cmd) {
  149. case MachO::LC_SEGMENT:
  150. if (IsLittleEndian != sys::IsLittleEndianHost)
  151. MachO::swapStruct(MLC.segment_command_data);
  152. memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command));
  153. Begin += sizeof(MachO::segment_command);
  154. for (const std::unique_ptr<Section> &Sec : LC.Sections)
  155. writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
  156. continue;
  157. case MachO::LC_SEGMENT_64:
  158. if (IsLittleEndian != sys::IsLittleEndianHost)
  159. MachO::swapStruct(MLC.segment_command_64_data);
  160. memcpy(Begin, &MLC.segment_command_64_data,
  161. sizeof(MachO::segment_command_64));
  162. Begin += sizeof(MachO::segment_command_64);
  163. for (const std::unique_ptr<Section> &Sec : LC.Sections)
  164. writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
  165. continue;
  166. }
  167. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
  168. case MachO::LCName: \
  169. assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
  170. MLC.load_command_data.cmdsize); \
  171. if (IsLittleEndian != sys::IsLittleEndianHost) \
  172. MachO::swapStruct(MLC.LCStruct##_data); \
  173. memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
  174. Begin += sizeof(MachO::LCStruct); \
  175. if (!LC.Payload.empty()) \
  176. memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
  177. Begin += LC.Payload.size(); \
  178. break;
  179. // Copy the load command as it is.
  180. switch (MLC.load_command_data.cmd) {
  181. default:
  182. assert(sizeof(MachO::load_command) + LC.Payload.size() ==
  183. MLC.load_command_data.cmdsize);
  184. if (IsLittleEndian != sys::IsLittleEndianHost)
  185. MachO::swapStruct(MLC.load_command_data);
  186. memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));
  187. Begin += sizeof(MachO::load_command);
  188. if (!LC.Payload.empty())
  189. memcpy(Begin, LC.Payload.data(), LC.Payload.size());
  190. Begin += LC.Payload.size();
  191. break;
  192. #include "llvm/BinaryFormat/MachO.def"
  193. }
  194. }
  195. }
  196. template <typename StructType>
  197. void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) {
  198. StructType Temp;
  199. assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name");
  200. assert(Sec.Sectname.size() <= sizeof(Temp.sectname) &&
  201. "too long section name");
  202. memset(&Temp, 0, sizeof(StructType));
  203. memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size());
  204. memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size());
  205. Temp.addr = Sec.Addr;
  206. Temp.size = Sec.Size;
  207. Temp.offset = Sec.Offset;
  208. Temp.align = Sec.Align;
  209. Temp.reloff = Sec.RelOff;
  210. Temp.nreloc = Sec.NReloc;
  211. Temp.flags = Sec.Flags;
  212. Temp.reserved1 = Sec.Reserved1;
  213. Temp.reserved2 = Sec.Reserved2;
  214. if (IsLittleEndian != sys::IsLittleEndianHost)
  215. MachO::swapStruct(Temp);
  216. memcpy(Out, &Temp, sizeof(StructType));
  217. Out += sizeof(StructType);
  218. }
  219. void MachOWriter::writeSections() {
  220. for (const LoadCommand &LC : O.LoadCommands)
  221. for (const std::unique_ptr<Section> &Sec : LC.Sections) {
  222. if (!Sec->hasValidOffset()) {
  223. assert((Sec->Offset == 0) && "Skipped section's offset must be zero");
  224. assert((Sec->isVirtualSection() || Sec->Size == 0) &&
  225. "Non-zero-fill sections with zero offset must have zero size");
  226. continue;
  227. }
  228. assert(Sec->Offset && "Section offset can not be zero");
  229. assert((Sec->Size == Sec->Content.size()) && "Incorrect section size");
  230. memcpy(B.getBufferStart() + Sec->Offset, Sec->Content.data(),
  231. Sec->Content.size());
  232. for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
  233. RelocationInfo RelocInfo = Sec->Relocations[Index];
  234. if (!RelocInfo.Scattered) {
  235. const uint32_t SymbolNum = RelocInfo.Extern
  236. ? (*RelocInfo.Symbol)->Index
  237. : (*RelocInfo.Sec)->Index;
  238. RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
  239. }
  240. if (IsLittleEndian != sys::IsLittleEndianHost)
  241. MachO::swapStruct(
  242. reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));
  243. memcpy(B.getBufferStart() + Sec->RelOff +
  244. Index * sizeof(MachO::any_relocation_info),
  245. &RelocInfo.Info, sizeof(RelocInfo.Info));
  246. }
  247. }
  248. }
  249. template <typename NListType>
  250. void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out,
  251. uint32_t Nstrx) {
  252. NListType ListEntry;
  253. ListEntry.n_strx = Nstrx;
  254. ListEntry.n_type = SE.n_type;
  255. ListEntry.n_sect = SE.n_sect;
  256. ListEntry.n_desc = SE.n_desc;
  257. ListEntry.n_value = SE.n_value;
  258. if (IsLittleEndian != sys::IsLittleEndianHost)
  259. MachO::swapStruct(ListEntry);
  260. memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
  261. Out += sizeof(NListType);
  262. }
  263. void MachOWriter::writeStringTable() {
  264. if (!O.SymTabCommandIndex)
  265. return;
  266. const MachO::symtab_command &SymTabCommand =
  267. O.LoadCommands[*O.SymTabCommandIndex]
  268. .MachOLoadCommand.symtab_command_data;
  269. uint8_t *StrTable = (uint8_t *)B.getBufferStart() + SymTabCommand.stroff;
  270. LayoutBuilder.getStringTableBuilder().write(StrTable);
  271. }
  272. void MachOWriter::writeSymbolTable() {
  273. if (!O.SymTabCommandIndex)
  274. return;
  275. const MachO::symtab_command &SymTabCommand =
  276. O.LoadCommands[*O.SymTabCommandIndex]
  277. .MachOLoadCommand.symtab_command_data;
  278. char *SymTable = (char *)B.getBufferStart() + SymTabCommand.symoff;
  279. for (auto Iter = O.SymTable.Symbols.begin(), End = O.SymTable.Symbols.end();
  280. Iter != End; Iter++) {
  281. SymbolEntry *Sym = Iter->get();
  282. uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name);
  283. if (Is64Bit)
  284. writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx);
  285. else
  286. writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx);
  287. }
  288. }
  289. void MachOWriter::writeRebaseInfo() {
  290. if (!O.DyLdInfoCommandIndex)
  291. return;
  292. const MachO::dyld_info_command &DyLdInfoCommand =
  293. O.LoadCommands[*O.DyLdInfoCommandIndex]
  294. .MachOLoadCommand.dyld_info_command_data;
  295. char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.rebase_off;
  296. assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
  297. "Incorrect rebase opcodes size");
  298. memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
  299. }
  300. void MachOWriter::writeBindInfo() {
  301. if (!O.DyLdInfoCommandIndex)
  302. return;
  303. const MachO::dyld_info_command &DyLdInfoCommand =
  304. O.LoadCommands[*O.DyLdInfoCommandIndex]
  305. .MachOLoadCommand.dyld_info_command_data;
  306. char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.bind_off;
  307. assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
  308. "Incorrect bind opcodes size");
  309. memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
  310. }
  311. void MachOWriter::writeWeakBindInfo() {
  312. if (!O.DyLdInfoCommandIndex)
  313. return;
  314. const MachO::dyld_info_command &DyLdInfoCommand =
  315. O.LoadCommands[*O.DyLdInfoCommandIndex]
  316. .MachOLoadCommand.dyld_info_command_data;
  317. char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.weak_bind_off;
  318. assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
  319. "Incorrect weak bind opcodes size");
  320. memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
  321. }
  322. void MachOWriter::writeLazyBindInfo() {
  323. if (!O.DyLdInfoCommandIndex)
  324. return;
  325. const MachO::dyld_info_command &DyLdInfoCommand =
  326. O.LoadCommands[*O.DyLdInfoCommandIndex]
  327. .MachOLoadCommand.dyld_info_command_data;
  328. char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.lazy_bind_off;
  329. assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
  330. "Incorrect lazy bind opcodes size");
  331. memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
  332. }
  333. void MachOWriter::writeExportInfo() {
  334. if (!O.DyLdInfoCommandIndex)
  335. return;
  336. const MachO::dyld_info_command &DyLdInfoCommand =
  337. O.LoadCommands[*O.DyLdInfoCommandIndex]
  338. .MachOLoadCommand.dyld_info_command_data;
  339. char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.export_off;
  340. assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
  341. "Incorrect export trie size");
  342. memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
  343. }
  344. void MachOWriter::writeIndirectSymbolTable() {
  345. if (!O.DySymTabCommandIndex)
  346. return;
  347. const MachO::dysymtab_command &DySymTabCommand =
  348. O.LoadCommands[*O.DySymTabCommandIndex]
  349. .MachOLoadCommand.dysymtab_command_data;
  350. uint32_t *Out =
  351. (uint32_t *)(B.getBufferStart() + DySymTabCommand.indirectsymoff);
  352. for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
  353. uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;
  354. if (IsLittleEndian != sys::IsLittleEndianHost)
  355. sys::swapByteOrder(Entry);
  356. *Out++ = Entry;
  357. }
  358. }
  359. void MachOWriter::writeLinkData(Optional<size_t> LCIndex, const LinkData &LD) {
  360. if (!LCIndex)
  361. return;
  362. const MachO::linkedit_data_command &LinkEditDataCommand =
  363. O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
  364. char *Out = (char *)B.getBufferStart() + LinkEditDataCommand.dataoff;
  365. assert((LinkEditDataCommand.datasize == LD.Data.size()) &&
  366. "Incorrect data size");
  367. memcpy(Out, LD.Data.data(), LD.Data.size());
  368. }
  369. void MachOWriter::writeCodeSignatureData() {
  370. return writeLinkData(O.CodeSignatureCommandIndex, O.CodeSignature);
  371. }
  372. void MachOWriter::writeDataInCodeData() {
  373. return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);
  374. }
  375. void MachOWriter::writeFunctionStartsData() {
  376. return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);
  377. }
  378. void MachOWriter::writeTail() {
  379. typedef void (MachOWriter::*WriteHandlerType)(void);
  380. typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
  381. SmallVector<WriteOperation, 7> Queue;
  382. if (O.SymTabCommandIndex) {
  383. const MachO::symtab_command &SymTabCommand =
  384. O.LoadCommands[*O.SymTabCommandIndex]
  385. .MachOLoadCommand.symtab_command_data;
  386. if (SymTabCommand.symoff)
  387. Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});
  388. if (SymTabCommand.stroff)
  389. Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});
  390. }
  391. if (O.DyLdInfoCommandIndex) {
  392. const MachO::dyld_info_command &DyLdInfoCommand =
  393. O.LoadCommands[*O.DyLdInfoCommandIndex]
  394. .MachOLoadCommand.dyld_info_command_data;
  395. if (DyLdInfoCommand.rebase_off)
  396. Queue.push_back(
  397. {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});
  398. if (DyLdInfoCommand.bind_off)
  399. Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});
  400. if (DyLdInfoCommand.weak_bind_off)
  401. Queue.push_back(
  402. {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});
  403. if (DyLdInfoCommand.lazy_bind_off)
  404. Queue.push_back(
  405. {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});
  406. if (DyLdInfoCommand.export_off)
  407. Queue.push_back(
  408. {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});
  409. }
  410. if (O.DySymTabCommandIndex) {
  411. const MachO::dysymtab_command &DySymTabCommand =
  412. O.LoadCommands[*O.DySymTabCommandIndex]
  413. .MachOLoadCommand.dysymtab_command_data;
  414. if (DySymTabCommand.indirectsymoff)
  415. Queue.emplace_back(DySymTabCommand.indirectsymoff,
  416. &MachOWriter::writeIndirectSymbolTable);
  417. }
  418. if (O.CodeSignatureCommandIndex) {
  419. const MachO::linkedit_data_command &LinkEditDataCommand =
  420. O.LoadCommands[*O.CodeSignatureCommandIndex]
  421. .MachOLoadCommand.linkedit_data_command_data;
  422. if (LinkEditDataCommand.dataoff)
  423. Queue.emplace_back(LinkEditDataCommand.dataoff,
  424. &MachOWriter::writeCodeSignatureData);
  425. }
  426. if (O.DataInCodeCommandIndex) {
  427. const MachO::linkedit_data_command &LinkEditDataCommand =
  428. O.LoadCommands[*O.DataInCodeCommandIndex]
  429. .MachOLoadCommand.linkedit_data_command_data;
  430. if (LinkEditDataCommand.dataoff)
  431. Queue.emplace_back(LinkEditDataCommand.dataoff,
  432. &MachOWriter::writeDataInCodeData);
  433. }
  434. if (O.FunctionStartsCommandIndex) {
  435. const MachO::linkedit_data_command &LinkEditDataCommand =
  436. O.LoadCommands[*O.FunctionStartsCommandIndex]
  437. .MachOLoadCommand.linkedit_data_command_data;
  438. if (LinkEditDataCommand.dataoff)
  439. Queue.emplace_back(LinkEditDataCommand.dataoff,
  440. &MachOWriter::writeFunctionStartsData);
  441. }
  442. llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) {
  443. return LHS.first < RHS.first;
  444. });
  445. for (auto WriteOp : Queue)
  446. (this->*WriteOp.second)();
  447. }
  448. Error MachOWriter::finalize() { return LayoutBuilder.layout(); }
  449. Error MachOWriter::write() {
  450. if (Error E = B.allocate(totalSize()))
  451. return E;
  452. memset(B.getBufferStart(), 0, totalSize());
  453. writeHeader();
  454. writeLoadCommands();
  455. writeSections();
  456. writeTail();
  457. return B.commit();
  458. }
  459. } // end namespace macho
  460. } // end namespace objcopy
  461. } // end namespace llvm