blob: 3c41e73b2b013da96264465016ed109ed723452f [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- MachOWriter.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth127252b2019-02-11 08:25:19 +00003// 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
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MachOWriter.h"
Seiya Nuta552bcb82019-08-19 21:05:31 +000010#include "MachOLayoutBuilder.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000011#include "Object.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/BinaryFormat/MachO.h"
14#include "llvm/Object/MachO.h"
Seiya Nutab728e532019-06-08 01:22:54 +000015#include "llvm/Support/Errc.h"
16#include "llvm/Support/ErrorHandling.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000017#include <memory>
18
19namespace llvm {
20namespace objcopy {
21namespace macho {
22
23size_t MachOWriter::headerSize() const {
24 return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
25}
26
27size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; }
28
29size_t MachOWriter::symTableSize() const {
Seiya Nutaf923d9b2019-06-21 00:21:50 +000030 return O.SymTable.Symbols.size() *
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000031 (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist));
32}
33
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000034size_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;
Seiya Nuta552bcb82019-08-19 21:05:31 +000044 if (SymTabCommand.symoff)
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000045 Ends.push_back(SymTabCommand.symoff + symTableSize());
Seiya Nuta552bcb82019-08-19 21:05:31 +000046 if (SymTabCommand.stroff)
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000047 Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000048 }
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
Seiya Nuta552bcb82019-08-19 21:05:31 +000082 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
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -070092 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
Seiya Nuta552bcb82019-08-19 21:05:31 +0000101 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
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000121 // Otherwise, use the last section / reloction.
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800122 for (const LoadCommand &LC : O.LoadCommands)
123 for (const std::unique_ptr<Section> &S : LC.Sections) {
124 Ends.push_back(S->Offset + S->Size);
125 if (S->RelOff)
126 Ends.push_back(S->RelOff +
127 S->NReloc * sizeof(MachO::any_relocation_info));
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000128 }
129
130 if (!Ends.empty())
131 return *std::max_element(Ends.begin(), Ends.end());
132
133 // Otherwise, we have only Mach header and load commands.
134 return headerSize() + loadCommandsSize();
135}
136
137void MachOWriter::writeHeader() {
138 MachO::mach_header_64 Header;
139
140 Header.magic = O.Header.Magic;
141 Header.cputype = O.Header.CPUType;
142 Header.cpusubtype = O.Header.CPUSubType;
143 Header.filetype = O.Header.FileType;
144 Header.ncmds = O.Header.NCmds;
145 Header.sizeofcmds = O.Header.SizeOfCmds;
146 Header.flags = O.Header.Flags;
147 Header.reserved = O.Header.Reserved;
148
149 if (IsLittleEndian != sys::IsLittleEndianHost)
150 MachO::swapStruct(Header);
151
152 auto HeaderSize =
153 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
154 memcpy(B.getBufferStart(), &Header, HeaderSize);
155}
156
157void MachOWriter::writeLoadCommands() {
158 uint8_t *Begin = B.getBufferStart() + headerSize();
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800159 for (const LoadCommand &LC : O.LoadCommands) {
Seiya Nutab728e532019-06-08 01:22:54 +0000160 // Construct a load command.
161 MachO::macho_load_command MLC = LC.MachOLoadCommand;
162 switch (MLC.load_command_data.cmd) {
163 case MachO::LC_SEGMENT:
164 if (IsLittleEndian != sys::IsLittleEndianHost)
165 MachO::swapStruct(MLC.segment_command_data);
166 memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command));
167 Begin += sizeof(MachO::segment_command);
168
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800169 for (const std::unique_ptr<Section> &Sec : LC.Sections)
170 writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
Seiya Nutab728e532019-06-08 01:22:54 +0000171 continue;
172 case MachO::LC_SEGMENT_64:
173 if (IsLittleEndian != sys::IsLittleEndianHost)
174 MachO::swapStruct(MLC.segment_command_64_data);
175 memcpy(Begin, &MLC.segment_command_64_data,
176 sizeof(MachO::segment_command_64));
177 Begin += sizeof(MachO::segment_command_64);
178
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800179 for (const std::unique_ptr<Section> &Sec : LC.Sections)
180 writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
Seiya Nutab728e532019-06-08 01:22:54 +0000181 continue;
182 }
183
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000184#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
185 case MachO::LCName: \
186 assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
Seiya Nutab728e532019-06-08 01:22:54 +0000187 MLC.load_command_data.cmdsize); \
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000188 if (IsLittleEndian != sys::IsLittleEndianHost) \
189 MachO::swapStruct(MLC.LCStruct##_data); \
190 memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
191 Begin += sizeof(MachO::LCStruct); \
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800192 if (!LC.Payload.empty()) \
193 memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000194 Begin += LC.Payload.size(); \
195 break;
196
Seiya Nutab728e532019-06-08 01:22:54 +0000197 // Copy the load command as it is.
198 switch (MLC.load_command_data.cmd) {
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000199 default:
200 assert(sizeof(MachO::load_command) + LC.Payload.size() ==
Seiya Nutab728e532019-06-08 01:22:54 +0000201 MLC.load_command_data.cmdsize);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000202 if (IsLittleEndian != sys::IsLittleEndianHost)
203 MachO::swapStruct(MLC.load_command_data);
204 memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));
205 Begin += sizeof(MachO::load_command);
Alexander Shaposhnikovc54959c2019-11-19 23:30:52 -0800206 if (!LC.Payload.empty())
207 memcpy(Begin, LC.Payload.data(), LC.Payload.size());
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000208 Begin += LC.Payload.size();
209 break;
210#include "llvm/BinaryFormat/MachO.def"
211 }
212 }
213}
214
Seiya Nutab728e532019-06-08 01:22:54 +0000215template <typename StructType>
216void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) {
217 StructType Temp;
218 assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name");
219 assert(Sec.Sectname.size() <= sizeof(Temp.sectname) &&
220 "too long section name");
221 memset(&Temp, 0, sizeof(StructType));
222 memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size());
223 memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size());
224 Temp.addr = Sec.Addr;
225 Temp.size = Sec.Size;
226 Temp.offset = Sec.Offset;
227 Temp.align = Sec.Align;
228 Temp.reloff = Sec.RelOff;
229 Temp.nreloc = Sec.NReloc;
230 Temp.flags = Sec.Flags;
231 Temp.reserved1 = Sec.Reserved1;
232 Temp.reserved2 = Sec.Reserved2;
233
234 if (IsLittleEndian != sys::IsLittleEndianHost)
235 MachO::swapStruct(Temp);
236 memcpy(Out, &Temp, sizeof(StructType));
237 Out += sizeof(StructType);
238}
239
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000240void MachOWriter::writeSections() {
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800241 for (const LoadCommand &LC : O.LoadCommands)
242 for (const std::unique_ptr<Section> &Sec : LC.Sections) {
243 if (Sec->isVirtualSection())
Seiya Nutab728e532019-06-08 01:22:54 +0000244 continue;
245
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800246 assert(Sec->Offset && "Section offset can not be zero");
247 assert((Sec->Size == Sec->Content.size()) && "Incorrect section size");
248 memcpy(B.getBufferStart() + Sec->Offset, Sec->Content.data(),
249 Sec->Content.size());
250 for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
Alexander Shaposhnikov0db3a5a2020-04-26 22:39:50 -0700251 RelocationInfo RelocInfo = Sec->Relocations[Index];
252 if (!RelocInfo.Scattered) {
253 const uint32_t SymbolNum = RelocInfo.Extern
254 ? (*RelocInfo.Symbol)->Index
Alexander Shaposhnikov29c6f5c2020-04-27 18:20:01 -0700255 : (*RelocInfo.Sec)->Index;
Alexander Shaposhnikov0db3a5a2020-04-26 22:39:50 -0700256 RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
257 }
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000258 if (IsLittleEndian != sys::IsLittleEndianHost)
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000259 MachO::swapStruct(
260 reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800261 memcpy(B.getBufferStart() + Sec->RelOff +
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000262 Index * sizeof(MachO::any_relocation_info),
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000263 &RelocInfo.Info, sizeof(RelocInfo.Info));
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000264 }
265 }
266}
267
268template <typename NListType>
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000269void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out,
270 uint32_t Nstrx) {
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000271 NListType ListEntry;
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000272 ListEntry.n_strx = Nstrx;
273 ListEntry.n_type = SE.n_type;
274 ListEntry.n_sect = SE.n_sect;
275 ListEntry.n_desc = SE.n_desc;
276 ListEntry.n_value = SE.n_value;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000277
278 if (IsLittleEndian != sys::IsLittleEndianHost)
279 MachO::swapStruct(ListEntry);
280 memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
281 Out += sizeof(NListType);
282}
283
Seiya Nuta36848ff2019-08-20 08:36:47 +0000284void MachOWriter::writeStringTable() {
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000285 if (!O.SymTabCommandIndex)
286 return;
287 const MachO::symtab_command &SymTabCommand =
288 O.LoadCommands[*O.SymTabCommandIndex]
289 .MachOLoadCommand.symtab_command_data;
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000290
291 uint8_t *StrTable = (uint8_t *)B.getBufferStart() + SymTabCommand.stroff;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000292 LayoutBuilder.getStringTableBuilder().write(StrTable);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000293}
294
Seiya Nuta36848ff2019-08-20 08:36:47 +0000295void MachOWriter::writeSymbolTable() {
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000296 if (!O.SymTabCommandIndex)
297 return;
298 const MachO::symtab_command &SymTabCommand =
299 O.LoadCommands[*O.SymTabCommandIndex]
300 .MachOLoadCommand.symtab_command_data;
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000301
302 char *SymTable = (char *)B.getBufferStart() + SymTabCommand.symoff;
303 for (auto Iter = O.SymTable.Symbols.begin(), End = O.SymTable.Symbols.end();
304 Iter != End; Iter++) {
305 SymbolEntry *Sym = Iter->get();
Seiya Nuta552bcb82019-08-19 21:05:31 +0000306 uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name);
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000307
308 if (Is64Bit)
309 writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx);
310 else
311 writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000312 }
313}
314
315void MachOWriter::writeRebaseInfo() {
316 if (!O.DyLdInfoCommandIndex)
317 return;
318 const MachO::dyld_info_command &DyLdInfoCommand =
319 O.LoadCommands[*O.DyLdInfoCommandIndex]
320 .MachOLoadCommand.dyld_info_command_data;
321 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.rebase_off;
322 assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
323 "Incorrect rebase opcodes size");
324 memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
325}
326
327void MachOWriter::writeBindInfo() {
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.bind_off;
334 assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
335 "Incorrect bind opcodes size");
336 memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
337}
338
339void MachOWriter::writeWeakBindInfo() {
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.weak_bind_off;
346 assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
347 "Incorrect weak bind opcodes size");
348 memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
349}
350
351void MachOWriter::writeLazyBindInfo() {
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.lazy_bind_off;
358 assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
359 "Incorrect lazy bind opcodes size");
360 memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
361}
362
363void MachOWriter::writeExportInfo() {
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.export_off;
370 assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
371 "Incorrect export trie size");
372 memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
373}
374
Seiya Nuta552bcb82019-08-19 21:05:31 +0000375void MachOWriter::writeIndirectSymbolTable() {
376 if (!O.DySymTabCommandIndex)
377 return;
378
379 const MachO::dysymtab_command &DySymTabCommand =
380 O.LoadCommands[*O.DySymTabCommandIndex]
381 .MachOLoadCommand.dysymtab_command_data;
382
Seiya Nuta1e589f62019-10-30 15:12:17 +0900383 uint32_t *Out =
384 (uint32_t *)(B.getBufferStart() + DySymTabCommand.indirectsymoff);
385 for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
386 uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;
387 if (IsLittleEndian != sys::IsLittleEndianHost)
388 sys::swapByteOrder(Entry);
389 *Out++ = Entry;
390 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000391}
392
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -0700393void MachOWriter::writeLinkData(Optional<size_t> LCIndex, const LinkData &LD) {
394 if (!LCIndex)
Seiya Nuta552bcb82019-08-19 21:05:31 +0000395 return;
396 const MachO::linkedit_data_command &LinkEditDataCommand =
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -0700397 O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000398 char *Out = (char *)B.getBufferStart() + LinkEditDataCommand.dataoff;
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -0700399 assert((LinkEditDataCommand.datasize == LD.Data.size()) &&
400 "Incorrect data size");
401 memcpy(Out, LD.Data.data(), LD.Data.size());
402}
403
404void MachOWriter::writeCodeSignatureData() {
405 return writeLinkData(O.CodeSignatureCommandIndex, O.CodeSignature);
406}
407
408void MachOWriter::writeDataInCodeData() {
409 return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);
Seiya Nuta552bcb82019-08-19 21:05:31 +0000410}
411
412void MachOWriter::writeFunctionStartsData() {
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -0700413 return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);
Seiya Nuta552bcb82019-08-19 21:05:31 +0000414}
415
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000416void MachOWriter::writeTail() {
417 typedef void (MachOWriter::*WriteHandlerType)(void);
418 typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
419 SmallVector<WriteOperation, 7> Queue;
420
421 if (O.SymTabCommandIndex) {
422 const MachO::symtab_command &SymTabCommand =
423 O.LoadCommands[*O.SymTabCommandIndex]
424 .MachOLoadCommand.symtab_command_data;
425 if (SymTabCommand.symoff)
426 Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});
427 if (SymTabCommand.stroff)
428 Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});
429 }
430
431 if (O.DyLdInfoCommandIndex) {
432 const MachO::dyld_info_command &DyLdInfoCommand =
433 O.LoadCommands[*O.DyLdInfoCommandIndex]
434 .MachOLoadCommand.dyld_info_command_data;
435 if (DyLdInfoCommand.rebase_off)
436 Queue.push_back(
437 {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});
438 if (DyLdInfoCommand.bind_off)
439 Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});
440 if (DyLdInfoCommand.weak_bind_off)
441 Queue.push_back(
442 {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});
443 if (DyLdInfoCommand.lazy_bind_off)
444 Queue.push_back(
445 {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});
446 if (DyLdInfoCommand.export_off)
447 Queue.push_back(
448 {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});
449 }
450
Seiya Nuta552bcb82019-08-19 21:05:31 +0000451 if (O.DySymTabCommandIndex) {
452 const MachO::dysymtab_command &DySymTabCommand =
453 O.LoadCommands[*O.DySymTabCommandIndex]
454 .MachOLoadCommand.dysymtab_command_data;
455
456 if (DySymTabCommand.indirectsymoff)
457 Queue.emplace_back(DySymTabCommand.indirectsymoff,
458 &MachOWriter::writeIndirectSymbolTable);
459 }
460
Alexander Shaposhnikov913bc312020-06-15 18:55:59 -0700461 if (O.CodeSignatureCommandIndex) {
462 const MachO::linkedit_data_command &LinkEditDataCommand =
463 O.LoadCommands[*O.CodeSignatureCommandIndex]
464 .MachOLoadCommand.linkedit_data_command_data;
465
466 if (LinkEditDataCommand.dataoff)
467 Queue.emplace_back(LinkEditDataCommand.dataoff,
468 &MachOWriter::writeCodeSignatureData);
469 }
470
Seiya Nuta552bcb82019-08-19 21:05:31 +0000471 if (O.DataInCodeCommandIndex) {
472 const MachO::linkedit_data_command &LinkEditDataCommand =
473 O.LoadCommands[*O.DataInCodeCommandIndex]
474 .MachOLoadCommand.linkedit_data_command_data;
475
476 if (LinkEditDataCommand.dataoff)
477 Queue.emplace_back(LinkEditDataCommand.dataoff,
478 &MachOWriter::writeDataInCodeData);
479 }
480
481 if (O.FunctionStartsCommandIndex) {
482 const MachO::linkedit_data_command &LinkEditDataCommand =
483 O.LoadCommands[*O.FunctionStartsCommandIndex]
484 .MachOLoadCommand.linkedit_data_command_data;
485
486 if (LinkEditDataCommand.dataoff)
487 Queue.emplace_back(LinkEditDataCommand.dataoff,
488 &MachOWriter::writeFunctionStartsData);
489 }
490
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000491 llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) {
492 return LHS.first < RHS.first;
493 });
494
495 for (auto WriteOp : Queue)
496 (this->*WriteOp.second)();
497}
498
Seiya Nuta552bcb82019-08-19 21:05:31 +0000499Error MachOWriter::finalize() { return LayoutBuilder.layout(); }
Seiya Nutab728e532019-06-08 01:22:54 +0000500
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000501Error MachOWriter::write() {
502 if (Error E = B.allocate(totalSize()))
503 return E;
504 memset(B.getBufferStart(), 0, totalSize());
505 writeHeader();
506 writeLoadCommands();
507 writeSections();
508 writeTail();
Jordan Rupprecht2e862c72019-02-04 19:09:20 +0000509 return B.commit();
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000510}
511
512} // end namespace macho
513} // end namespace objcopy
514} // end namespace llvm