xref: /netbsd-src/external/apache2/llvm/dist/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
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(Buf->getBufferStart(), &Header, HeaderSize);
163 }
164 
writeLoadCommands()165 void MachOWriter::writeLoadCommands() {
166   uint8_t *Begin =
167       reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + headerSize();
168   for (const LoadCommand &LC : O.LoadCommands) {
169     // Construct a load command.
170     MachO::macho_load_command MLC = LC.MachOLoadCommand;
171     switch (MLC.load_command_data.cmd) {
172     case MachO::LC_SEGMENT:
173       if (IsLittleEndian != sys::IsLittleEndianHost)
174         MachO::swapStruct(MLC.segment_command_data);
175       memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command));
176       Begin += sizeof(MachO::segment_command);
177 
178       for (const std::unique_ptr<Section> &Sec : LC.Sections)
179         writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
180       continue;
181     case MachO::LC_SEGMENT_64:
182       if (IsLittleEndian != sys::IsLittleEndianHost)
183         MachO::swapStruct(MLC.segment_command_64_data);
184       memcpy(Begin, &MLC.segment_command_64_data,
185              sizeof(MachO::segment_command_64));
186       Begin += sizeof(MachO::segment_command_64);
187 
188       for (const std::unique_ptr<Section> &Sec : LC.Sections)
189         writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
190       continue;
191     }
192 
193 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \
194   case MachO::LCName:                                                          \
195     assert(sizeof(MachO::LCStruct) + LC.Payload.size() ==                      \
196            MLC.load_command_data.cmdsize);                                     \
197     if (IsLittleEndian != sys::IsLittleEndianHost)                             \
198       MachO::swapStruct(MLC.LCStruct##_data);                                  \
199     memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct));              \
200     Begin += sizeof(MachO::LCStruct);                                          \
201     if (!LC.Payload.empty())                                                   \
202       memcpy(Begin, LC.Payload.data(), LC.Payload.size());                     \
203     Begin += LC.Payload.size();                                                \
204     break;
205 
206     // Copy the load command as it is.
207     switch (MLC.load_command_data.cmd) {
208     default:
209       assert(sizeof(MachO::load_command) + LC.Payload.size() ==
210              MLC.load_command_data.cmdsize);
211       if (IsLittleEndian != sys::IsLittleEndianHost)
212         MachO::swapStruct(MLC.load_command_data);
213       memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));
214       Begin += sizeof(MachO::load_command);
215       if (!LC.Payload.empty())
216         memcpy(Begin, LC.Payload.data(), LC.Payload.size());
217       Begin += LC.Payload.size();
218       break;
219 #include "llvm/BinaryFormat/MachO.def"
220     }
221   }
222 }
223 
224 template <typename StructType>
writeSectionInLoadCommand(const Section & Sec,uint8_t * & Out)225 void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) {
226   StructType Temp;
227   assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name");
228   assert(Sec.Sectname.size() <= sizeof(Temp.sectname) &&
229          "too long section name");
230   memset(&Temp, 0, sizeof(StructType));
231   memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size());
232   memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size());
233   Temp.addr = Sec.Addr;
234   Temp.size = Sec.Size;
235   Temp.offset = Sec.Offset;
236   Temp.align = Sec.Align;
237   Temp.reloff = Sec.RelOff;
238   Temp.nreloc = Sec.NReloc;
239   Temp.flags = Sec.Flags;
240   Temp.reserved1 = Sec.Reserved1;
241   Temp.reserved2 = Sec.Reserved2;
242 
243   if (IsLittleEndian != sys::IsLittleEndianHost)
244     MachO::swapStruct(Temp);
245   memcpy(Out, &Temp, sizeof(StructType));
246   Out += sizeof(StructType);
247 }
248 
writeSections()249 void MachOWriter::writeSections() {
250   for (const LoadCommand &LC : O.LoadCommands)
251     for (const std::unique_ptr<Section> &Sec : LC.Sections) {
252       if (!Sec->hasValidOffset()) {
253         assert((Sec->Offset == 0) && "Skipped section's offset must be zero");
254         assert((Sec->isVirtualSection() || Sec->Size == 0) &&
255                "Non-zero-fill sections with zero offset must have zero size");
256         continue;
257       }
258 
259       assert(Sec->Offset && "Section offset can not be zero");
260       assert((Sec->Size == Sec->Content.size()) && "Incorrect section size");
261       memcpy(Buf->getBufferStart() + Sec->Offset, Sec->Content.data(),
262              Sec->Content.size());
263       for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
264         RelocationInfo RelocInfo = Sec->Relocations[Index];
265         if (!RelocInfo.Scattered) {
266           const uint32_t SymbolNum = RelocInfo.Extern
267                                          ? (*RelocInfo.Symbol)->Index
268                                          : (*RelocInfo.Sec)->Index;
269           RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
270         }
271         if (IsLittleEndian != sys::IsLittleEndianHost)
272           MachO::swapStruct(
273               reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));
274         memcpy(Buf->getBufferStart() + Sec->RelOff +
275                    Index * sizeof(MachO::any_relocation_info),
276                &RelocInfo.Info, sizeof(RelocInfo.Info));
277       }
278     }
279 }
280 
281 template <typename NListType>
writeNListEntry(const SymbolEntry & SE,bool IsLittleEndian,char * & Out,uint32_t Nstrx)282 void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out,
283                      uint32_t Nstrx) {
284   NListType ListEntry;
285   ListEntry.n_strx = Nstrx;
286   ListEntry.n_type = SE.n_type;
287   ListEntry.n_sect = SE.n_sect;
288   ListEntry.n_desc = SE.n_desc;
289   ListEntry.n_value = SE.n_value;
290 
291   if (IsLittleEndian != sys::IsLittleEndianHost)
292     MachO::swapStruct(ListEntry);
293   memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
294   Out += sizeof(NListType);
295 }
296 
writeStringTable()297 void MachOWriter::writeStringTable() {
298   if (!O.SymTabCommandIndex)
299     return;
300   const MachO::symtab_command &SymTabCommand =
301       O.LoadCommands[*O.SymTabCommandIndex]
302           .MachOLoadCommand.symtab_command_data;
303 
304   uint8_t *StrTable = (uint8_t *)Buf->getBufferStart() + SymTabCommand.stroff;
305   LayoutBuilder.getStringTableBuilder().write(StrTable);
306 }
307 
writeSymbolTable()308 void MachOWriter::writeSymbolTable() {
309   if (!O.SymTabCommandIndex)
310     return;
311   const MachO::symtab_command &SymTabCommand =
312       O.LoadCommands[*O.SymTabCommandIndex]
313           .MachOLoadCommand.symtab_command_data;
314 
315   char *SymTable = (char *)Buf->getBufferStart() + SymTabCommand.symoff;
316   for (auto Iter = O.SymTable.Symbols.begin(), End = O.SymTable.Symbols.end();
317        Iter != End; Iter++) {
318     SymbolEntry *Sym = Iter->get();
319     uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name);
320 
321     if (Is64Bit)
322       writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx);
323     else
324       writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx);
325   }
326 }
327 
writeRebaseInfo()328 void MachOWriter::writeRebaseInfo() {
329   if (!O.DyLdInfoCommandIndex)
330     return;
331   const MachO::dyld_info_command &DyLdInfoCommand =
332       O.LoadCommands[*O.DyLdInfoCommandIndex]
333           .MachOLoadCommand.dyld_info_command_data;
334   char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.rebase_off;
335   assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
336          "Incorrect rebase opcodes size");
337   memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
338 }
339 
writeBindInfo()340 void MachOWriter::writeBindInfo() {
341   if (!O.DyLdInfoCommandIndex)
342     return;
343   const MachO::dyld_info_command &DyLdInfoCommand =
344       O.LoadCommands[*O.DyLdInfoCommandIndex]
345           .MachOLoadCommand.dyld_info_command_data;
346   char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.bind_off;
347   assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
348          "Incorrect bind opcodes size");
349   memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
350 }
351 
writeWeakBindInfo()352 void MachOWriter::writeWeakBindInfo() {
353   if (!O.DyLdInfoCommandIndex)
354     return;
355   const MachO::dyld_info_command &DyLdInfoCommand =
356       O.LoadCommands[*O.DyLdInfoCommandIndex]
357           .MachOLoadCommand.dyld_info_command_data;
358   char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.weak_bind_off;
359   assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
360          "Incorrect weak bind opcodes size");
361   memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
362 }
363 
writeLazyBindInfo()364 void MachOWriter::writeLazyBindInfo() {
365   if (!O.DyLdInfoCommandIndex)
366     return;
367   const MachO::dyld_info_command &DyLdInfoCommand =
368       O.LoadCommands[*O.DyLdInfoCommandIndex]
369           .MachOLoadCommand.dyld_info_command_data;
370   char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.lazy_bind_off;
371   assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
372          "Incorrect lazy bind opcodes size");
373   memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
374 }
375 
writeExportInfo()376 void MachOWriter::writeExportInfo() {
377   if (!O.DyLdInfoCommandIndex)
378     return;
379   const MachO::dyld_info_command &DyLdInfoCommand =
380       O.LoadCommands[*O.DyLdInfoCommandIndex]
381           .MachOLoadCommand.dyld_info_command_data;
382   char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.export_off;
383   assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
384          "Incorrect export trie size");
385   memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
386 }
387 
writeIndirectSymbolTable()388 void MachOWriter::writeIndirectSymbolTable() {
389   if (!O.DySymTabCommandIndex)
390     return;
391 
392   const MachO::dysymtab_command &DySymTabCommand =
393       O.LoadCommands[*O.DySymTabCommandIndex]
394           .MachOLoadCommand.dysymtab_command_data;
395 
396   uint32_t *Out =
397       (uint32_t *)(Buf->getBufferStart() + DySymTabCommand.indirectsymoff);
398   for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
399     uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;
400     if (IsLittleEndian != sys::IsLittleEndianHost)
401       sys::swapByteOrder(Entry);
402     *Out++ = Entry;
403   }
404 }
405 
writeLinkData(Optional<size_t> LCIndex,const LinkData & LD)406 void MachOWriter::writeLinkData(Optional<size_t> LCIndex, const LinkData &LD) {
407   if (!LCIndex)
408     return;
409   const MachO::linkedit_data_command &LinkEditDataCommand =
410       O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
411   char *Out = (char *)Buf->getBufferStart() + LinkEditDataCommand.dataoff;
412   assert((LinkEditDataCommand.datasize == LD.Data.size()) &&
413          "Incorrect data size");
414   memcpy(Out, LD.Data.data(), LD.Data.size());
415 }
416 
writeCodeSignatureData()417 void MachOWriter::writeCodeSignatureData() {
418   return writeLinkData(O.CodeSignatureCommandIndex, O.CodeSignature);
419 }
420 
writeDataInCodeData()421 void MachOWriter::writeDataInCodeData() {
422   return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);
423 }
424 
writeFunctionStartsData()425 void MachOWriter::writeFunctionStartsData() {
426   return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);
427 }
428 
writeTail()429 void MachOWriter::writeTail() {
430   typedef void (MachOWriter::*WriteHandlerType)(void);
431   typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
432   SmallVector<WriteOperation, 7> Queue;
433 
434   if (O.SymTabCommandIndex) {
435     const MachO::symtab_command &SymTabCommand =
436         O.LoadCommands[*O.SymTabCommandIndex]
437             .MachOLoadCommand.symtab_command_data;
438     if (SymTabCommand.symoff)
439       Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});
440     if (SymTabCommand.stroff)
441       Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});
442   }
443 
444   if (O.DyLdInfoCommandIndex) {
445     const MachO::dyld_info_command &DyLdInfoCommand =
446         O.LoadCommands[*O.DyLdInfoCommandIndex]
447             .MachOLoadCommand.dyld_info_command_data;
448     if (DyLdInfoCommand.rebase_off)
449       Queue.push_back(
450           {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});
451     if (DyLdInfoCommand.bind_off)
452       Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});
453     if (DyLdInfoCommand.weak_bind_off)
454       Queue.push_back(
455           {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});
456     if (DyLdInfoCommand.lazy_bind_off)
457       Queue.push_back(
458           {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});
459     if (DyLdInfoCommand.export_off)
460       Queue.push_back(
461           {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});
462   }
463 
464   if (O.DySymTabCommandIndex) {
465     const MachO::dysymtab_command &DySymTabCommand =
466         O.LoadCommands[*O.DySymTabCommandIndex]
467             .MachOLoadCommand.dysymtab_command_data;
468 
469     if (DySymTabCommand.indirectsymoff)
470       Queue.emplace_back(DySymTabCommand.indirectsymoff,
471                          &MachOWriter::writeIndirectSymbolTable);
472   }
473 
474   if (O.CodeSignatureCommandIndex) {
475     const MachO::linkedit_data_command &LinkEditDataCommand =
476         O.LoadCommands[*O.CodeSignatureCommandIndex]
477             .MachOLoadCommand.linkedit_data_command_data;
478 
479     if (LinkEditDataCommand.dataoff)
480       Queue.emplace_back(LinkEditDataCommand.dataoff,
481                          &MachOWriter::writeCodeSignatureData);
482   }
483 
484   if (O.DataInCodeCommandIndex) {
485     const MachO::linkedit_data_command &LinkEditDataCommand =
486         O.LoadCommands[*O.DataInCodeCommandIndex]
487             .MachOLoadCommand.linkedit_data_command_data;
488 
489     if (LinkEditDataCommand.dataoff)
490       Queue.emplace_back(LinkEditDataCommand.dataoff,
491                          &MachOWriter::writeDataInCodeData);
492   }
493 
494   if (O.FunctionStartsCommandIndex) {
495     const MachO::linkedit_data_command &LinkEditDataCommand =
496         O.LoadCommands[*O.FunctionStartsCommandIndex]
497             .MachOLoadCommand.linkedit_data_command_data;
498 
499     if (LinkEditDataCommand.dataoff)
500       Queue.emplace_back(LinkEditDataCommand.dataoff,
501                          &MachOWriter::writeFunctionStartsData);
502   }
503 
504   llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) {
505     return LHS.first < RHS.first;
506   });
507 
508   for (auto WriteOp : Queue)
509     (this->*WriteOp.second)();
510 }
511 
finalize()512 Error MachOWriter::finalize() { return LayoutBuilder.layout(); }
513 
write()514 Error MachOWriter::write() {
515   size_t TotalSize = totalSize();
516   Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
517   if (!Buf)
518     return createStringError(errc::not_enough_memory,
519                              "failed to allocate memory buffer of " +
520                                  Twine::utohexstr(TotalSize) + " bytes");
521   memset(Buf->getBufferStart(), 0, totalSize());
522   writeHeader();
523   writeLoadCommands();
524   writeSections();
525   writeTail();
526 
527   // TODO: Implement direct writing to the output stream (without intermediate
528   // memory buffer Buf).
529   Out.write(Buf->getBufferStart(), Buf->getBufferSize());
530   return Error::success();
531 }
532 
533 } // end namespace macho
534 } // end namespace objcopy
535 } // end namespace llvm
536