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