1 //===- SyntheticSections.cpp ---------------------------------------------===//
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 "SyntheticSections.h"
10 #include "ConcatOutputSection.h"
11 #include "Config.h"
12 #include "ExportTrie.h"
13 #include "InputFiles.h"
14 #include "MachOStructs.h"
15 #include "OutputSegment.h"
16 #include "SymbolTable.h"
17 #include "Symbols.h"
18 
19 #include "lld/Common/ErrorHandler.h"
20 #include "lld/Common/Memory.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/Support/EndianStream.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/Path.h"
27 
28 #ifdef LLVM_HAVE_LIBXAR
29 #include <fcntl.h>
30 extern "C" {
31 #include <xar/xar.h>
32 }
33 #endif
34 
35 using namespace llvm;
36 using namespace llvm::MachO;
37 using namespace llvm::support;
38 using namespace llvm::support::endian;
39 using namespace lld;
40 using namespace lld::macho;
41 
42 InStruct macho::in;
43 std::vector<SyntheticSection *> macho::syntheticSections;
44 
45 SyntheticSection::SyntheticSection(const char *segname, const char *name)
46     : OutputSection(SyntheticKind, name) {
47   std::tie(this->segname, this->name) = maybeRenameSection({segname, name});
48   isec = make<ConcatInputSection>(segname, name);
49   isec->parent = this;
50   syntheticSections.push_back(this);
51 }
52 
53 // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
54 // from the beginning of the file (i.e. the header).
55 MachHeaderSection::MachHeaderSection()
56     : SyntheticSection(segment_names::text, section_names::header) {
57   // XXX: This is a hack. (See D97007)
58   // Setting the index to 1 to pretend that this section is the text
59   // section.
60   index = 1;
61   isec->isFinal = true;
62 }
63 
64 void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
65   loadCommands.push_back(lc);
66   sizeOfCmds += lc->getSize();
67 }
68 
69 uint64_t MachHeaderSection::getSize() const {
70   uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
71   // If we are emitting an encryptable binary, our load commands must have a
72   // separate (non-encrypted) page to themselves.
73   if (config->emitEncryptionInfo)
74     size = alignTo(size, target->getPageSize());
75   return size;
76 }
77 
78 static uint32_t cpuSubtype() {
79   uint32_t subtype = target->cpuSubtype;
80 
81   if (config->outputType == MH_EXECUTE && !config->staticLink &&
82       target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
83       config->platform() == PlatformKind::macOS &&
84       config->platformInfo.minimum >= VersionTuple(10, 5))
85     subtype |= CPU_SUBTYPE_LIB64;
86 
87   return subtype;
88 }
89 
90 void MachHeaderSection::writeTo(uint8_t *buf) const {
91   auto *hdr = reinterpret_cast<mach_header *>(buf);
92   hdr->magic = target->magic;
93   hdr->cputype = target->cpuType;
94   hdr->cpusubtype = cpuSubtype();
95   hdr->filetype = config->outputType;
96   hdr->ncmds = loadCommands.size();
97   hdr->sizeofcmds = sizeOfCmds;
98   hdr->flags = MH_DYLDLINK;
99 
100   if (config->namespaceKind == NamespaceKind::twolevel)
101     hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
102 
103   if (config->outputType == MH_DYLIB && !config->hasReexports)
104     hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
105 
106   if (config->markDeadStrippableDylib)
107     hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
108 
109   if (config->outputType == MH_EXECUTE && config->isPic)
110     hdr->flags |= MH_PIE;
111 
112   if (config->outputType == MH_DYLIB && config->applicationExtension)
113     hdr->flags |= MH_APP_EXTENSION_SAFE;
114 
115   if (in.exports->hasWeakSymbol || in.weakBinding->hasNonWeakDefinition())
116     hdr->flags |= MH_WEAK_DEFINES;
117 
118   if (in.exports->hasWeakSymbol || in.weakBinding->hasEntry())
119     hdr->flags |= MH_BINDS_TO_WEAK;
120 
121   for (const OutputSegment *seg : outputSegments) {
122     for (const OutputSection *osec : seg->getSections()) {
123       if (isThreadLocalVariables(osec->flags)) {
124         hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
125         break;
126       }
127     }
128   }
129 
130   uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
131   for (const LoadCommand *lc : loadCommands) {
132     lc->writeTo(p);
133     p += lc->getSize();
134   }
135 }
136 
137 PageZeroSection::PageZeroSection()
138     : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
139 
140 RebaseSection::RebaseSection()
141     : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
142 
143 namespace {
144 struct Rebase {
145   OutputSegment *segment = nullptr;
146   uint64_t offset = 0;
147   uint64_t consecutiveCount = 0;
148 };
149 } // namespace
150 
151 // Rebase opcodes allow us to describe a contiguous sequence of rebase location
152 // using a single DO_REBASE opcode. To take advantage of it, we delay emitting
153 // `DO_REBASE` until we have reached the end of a contiguous sequence.
154 static void encodeDoRebase(Rebase &rebase, raw_svector_ostream &os) {
155   assert(rebase.consecutiveCount != 0);
156   if (rebase.consecutiveCount <= REBASE_IMMEDIATE_MASK) {
157     os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
158                                rebase.consecutiveCount);
159   } else {
160     os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
161     encodeULEB128(rebase.consecutiveCount, os);
162   }
163   rebase.consecutiveCount = 0;
164 }
165 
166 static void encodeRebase(const OutputSection *osec, uint64_t outSecOff,
167                          Rebase &lastRebase, raw_svector_ostream &os) {
168   OutputSegment *seg = osec->parent;
169   uint64_t offset = osec->getSegmentOffset() + outSecOff;
170   if (lastRebase.segment != seg || lastRebase.offset != offset) {
171     if (lastRebase.consecutiveCount != 0)
172       encodeDoRebase(lastRebase, os);
173 
174     if (lastRebase.segment != seg) {
175       os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
176                                  seg->index);
177       encodeULEB128(offset, os);
178       lastRebase.segment = seg;
179       lastRebase.offset = offset;
180     } else {
181       assert(lastRebase.offset != offset);
182       os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
183       encodeULEB128(offset - lastRebase.offset, os);
184       lastRebase.offset = offset;
185     }
186   }
187   ++lastRebase.consecutiveCount;
188   // DO_REBASE causes dyld to both perform the binding and increment the offset
189   lastRebase.offset += target->wordSize;
190 }
191 
192 void RebaseSection::finalizeContents() {
193   if (locations.empty())
194     return;
195 
196   raw_svector_ostream os{contents};
197   Rebase lastRebase;
198 
199   os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
200 
201   llvm::sort(locations, [](const Location &a, const Location &b) {
202     return a.isec->getVA(a.offset) < b.isec->getVA(b.offset);
203   });
204   for (const Location &loc : locations)
205     encodeRebase(loc.isec->parent, loc.isec->getOffset(loc.offset), lastRebase,
206                  os);
207   if (lastRebase.consecutiveCount != 0)
208     encodeDoRebase(lastRebase, os);
209 
210   os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
211 }
212 
213 void RebaseSection::writeTo(uint8_t *buf) const {
214   memcpy(buf, contents.data(), contents.size());
215 }
216 
217 NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname,
218                                                      const char *name)
219     : SyntheticSection(segname, name) {
220   align = target->wordSize;
221 }
222 
223 void macho::addNonLazyBindingEntries(const Symbol *sym,
224                                      const InputSection *isec, uint64_t offset,
225                                      int64_t addend) {
226   if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
227     in.binding->addEntry(dysym, isec, offset, addend);
228     if (dysym->isWeakDef())
229       in.weakBinding->addEntry(sym, isec, offset, addend);
230   } else if (const auto *defined = dyn_cast<Defined>(sym)) {
231     in.rebase->addEntry(isec, offset);
232     if (defined->isExternalWeakDef())
233       in.weakBinding->addEntry(sym, isec, offset, addend);
234   } else {
235     // Undefined symbols are filtered out in scanRelocations(); we should never
236     // get here
237     llvm_unreachable("cannot bind to an undefined symbol");
238   }
239 }
240 
241 void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
242   if (entries.insert(sym)) {
243     assert(!sym->isInGot());
244     sym->gotIndex = entries.size() - 1;
245 
246     addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize);
247   }
248 }
249 
250 void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
251   for (size_t i = 0, n = entries.size(); i < n; ++i)
252     if (auto *defined = dyn_cast<Defined>(entries[i]))
253       write64le(&buf[i * target->wordSize], defined->getVA());
254 }
255 
256 GotSection::GotSection()
257     : NonLazyPointerSectionBase(segment_names::dataConst, section_names::got) {
258   flags = S_NON_LAZY_SYMBOL_POINTERS;
259 }
260 
261 TlvPointerSection::TlvPointerSection()
262     : NonLazyPointerSectionBase(segment_names::data,
263                                 section_names::threadPtrs) {
264   flags = S_THREAD_LOCAL_VARIABLE_POINTERS;
265 }
266 
267 BindingSection::BindingSection()
268     : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
269 
270 namespace {
271 struct Binding {
272   OutputSegment *segment = nullptr;
273   uint64_t offset = 0;
274   int64_t addend = 0;
275 };
276 struct BindIR {
277   // Default value of 0xF0 is not valid opcode and should make the program
278   // scream instead of accidentally writing "valid" values.
279   uint8_t opcode = 0xF0;
280   uint64_t data = 0;
281   uint64_t consecutiveCount = 0;
282 };
283 } // namespace
284 
285 // Encode a sequence of opcodes that tell dyld to write the address of symbol +
286 // addend at osec->addr + outSecOff.
287 //
288 // The bind opcode "interpreter" remembers the values of each binding field, so
289 // we only need to encode the differences between bindings. Hence the use of
290 // lastBinding.
291 static void encodeBinding(const OutputSection *osec, uint64_t outSecOff,
292                           int64_t addend, Binding &lastBinding,
293                           std::vector<BindIR> &opcodes) {
294   OutputSegment *seg = osec->parent;
295   uint64_t offset = osec->getSegmentOffset() + outSecOff;
296   if (lastBinding.segment != seg) {
297     opcodes.push_back(
298         {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
299                               seg->index),
300          offset});
301     lastBinding.segment = seg;
302     lastBinding.offset = offset;
303   } else if (lastBinding.offset != offset) {
304     opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset});
305     lastBinding.offset = offset;
306   }
307 
308   if (lastBinding.addend != addend) {
309     opcodes.push_back(
310         {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)});
311     lastBinding.addend = addend;
312   }
313 
314   opcodes.push_back({BIND_OPCODE_DO_BIND, 0});
315   // DO_BIND causes dyld to both perform the binding and increment the offset
316   lastBinding.offset += target->wordSize;
317 }
318 
319 static void optimizeOpcodes(std::vector<BindIR> &opcodes) {
320   // Pass 1: Combine bind/add pairs
321   size_t i;
322   int pWrite = 0;
323   for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
324     if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) &&
325         (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) {
326       opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
327       opcodes[pWrite].data = opcodes[i].data;
328       ++i;
329     } else {
330       opcodes[pWrite] = opcodes[i - 1];
331     }
332   }
333   if (i == opcodes.size())
334     opcodes[pWrite] = opcodes[i - 1];
335   opcodes.resize(pWrite + 1);
336 
337   // Pass 2: Compress two or more bind_add opcodes
338   pWrite = 0;
339   for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
340     if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
341         (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
342         (opcodes[i].data == opcodes[i - 1].data)) {
343       opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
344       opcodes[pWrite].consecutiveCount = 2;
345       opcodes[pWrite].data = opcodes[i].data;
346       ++i;
347       while (i < opcodes.size() &&
348              (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
349              (opcodes[i].data == opcodes[i - 1].data)) {
350         opcodes[pWrite].consecutiveCount++;
351         ++i;
352       }
353     } else {
354       opcodes[pWrite] = opcodes[i - 1];
355     }
356   }
357   if (i == opcodes.size())
358     opcodes[pWrite] = opcodes[i - 1];
359   opcodes.resize(pWrite + 1);
360 
361   // Pass 3: Use immediate encodings
362   // Every binding is the size of one pointer. If the next binding is a
363   // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the
364   // opcode can be scaled by wordSize into a single byte and dyld will
365   // expand it to the correct address.
366   for (auto &p : opcodes) {
367     // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK,
368     // but ld64 currently does this. This could be a potential bug, but
369     // for now, perform the same behavior to prevent mysterious bugs.
370     if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
371         ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) &&
372         ((p.data % target->wordSize) == 0)) {
373       p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
374       p.data /= target->wordSize;
375     }
376   }
377 }
378 
379 static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) {
380   uint8_t opcode = op.opcode & BIND_OPCODE_MASK;
381   switch (opcode) {
382   case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
383   case BIND_OPCODE_ADD_ADDR_ULEB:
384   case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
385     os << op.opcode;
386     encodeULEB128(op.data, os);
387     break;
388   case BIND_OPCODE_SET_ADDEND_SLEB:
389     os << op.opcode;
390     encodeSLEB128(static_cast<int64_t>(op.data), os);
391     break;
392   case BIND_OPCODE_DO_BIND:
393     os << op.opcode;
394     break;
395   case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
396     os << op.opcode;
397     encodeULEB128(op.consecutiveCount, os);
398     encodeULEB128(op.data, os);
399     break;
400   case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
401     os << static_cast<uint8_t>(op.opcode | op.data);
402     break;
403   default:
404     llvm_unreachable("cannot bind to an unrecognized symbol");
405   }
406 }
407 
408 // Non-weak bindings need to have their dylib ordinal encoded as well.
409 static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
410   if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
411     return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
412   assert(dysym.getFile()->isReferenced());
413   return dysym.getFile()->ordinal;
414 }
415 
416 static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
417   if (ordinal <= 0) {
418     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
419                                (ordinal & BIND_IMMEDIATE_MASK));
420   } else if (ordinal <= BIND_IMMEDIATE_MASK) {
421     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
422   } else {
423     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
424     encodeULEB128(ordinal, os);
425   }
426 }
427 
428 static void encodeWeakOverride(const Defined *defined,
429                                raw_svector_ostream &os) {
430   os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
431                              BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
432      << defined->getName() << '\0';
433 }
434 
435 // Organize the bindings so we can encoded them with fewer opcodes.
436 //
437 // First, all bindings for a given symbol should be grouped together.
438 // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it
439 // has an associated symbol string), so we only want to emit it once per symbol.
440 //
441 // Within each group, we sort the bindings by address. Since bindings are
442 // delta-encoded, sorting them allows for a more compact result. Note that
443 // sorting by address alone ensures that bindings for the same segment / section
444 // are located together, minimizing the number of times we have to emit
445 // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB.
446 //
447 // Finally, we sort the symbols by the address of their first binding, again
448 // to facilitate the delta-encoding process.
449 template <class Sym>
450 std::vector<std::pair<const Sym *, std::vector<BindingEntry>>>
451 sortBindings(const BindingsMap<const Sym *> &bindingsMap) {
452   std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec(
453       bindingsMap.begin(), bindingsMap.end());
454   for (auto &p : bindingsVec) {
455     std::vector<BindingEntry> &bindings = p.second;
456     llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
457       return a.target.getVA() < b.target.getVA();
458     });
459   }
460   llvm::sort(bindingsVec, [](const auto &a, const auto &b) {
461     return a.second[0].target.getVA() < b.second[0].target.getVA();
462   });
463   return bindingsVec;
464 }
465 
466 // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
467 // interprets to update a record with the following fields:
468 //  * segment index (of the segment to write the symbol addresses to, typically
469 //    the __DATA_CONST segment which contains the GOT)
470 //  * offset within the segment, indicating the next location to write a binding
471 //  * symbol type
472 //  * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
473 //  * symbol name
474 //  * addend
475 // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
476 // a symbol in the GOT, and increments the segment offset to point to the next
477 // entry. It does *not* clear the record state after doing the bind, so
478 // subsequent opcodes only need to encode the differences between bindings.
479 void BindingSection::finalizeContents() {
480   raw_svector_ostream os{contents};
481   Binding lastBinding;
482   int16_t lastOrdinal = 0;
483 
484   for (auto &p : sortBindings(bindingsMap)) {
485     const DylibSymbol *sym = p.first;
486     std::vector<BindingEntry> &bindings = p.second;
487     uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
488     if (sym->isWeakRef())
489       flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
490     os << flags << sym->getName() << '\0'
491        << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
492     int16_t ordinal = ordinalForDylibSymbol(*sym);
493     if (ordinal != lastOrdinal) {
494       encodeDylibOrdinal(ordinal, os);
495       lastOrdinal = ordinal;
496     }
497     std::vector<BindIR> opcodes;
498     for (const BindingEntry &b : bindings)
499       encodeBinding(b.target.isec->parent,
500                     b.target.isec->getOffset(b.target.offset), b.addend,
501                     lastBinding, opcodes);
502     if (config->optimize > 1)
503       optimizeOpcodes(opcodes);
504     for (const auto &op : opcodes)
505       flushOpcodes(op, os);
506   }
507   if (!bindingsMap.empty())
508     os << static_cast<uint8_t>(BIND_OPCODE_DONE);
509 }
510 
511 void BindingSection::writeTo(uint8_t *buf) const {
512   memcpy(buf, contents.data(), contents.size());
513 }
514 
515 WeakBindingSection::WeakBindingSection()
516     : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
517 
518 void WeakBindingSection::finalizeContents() {
519   raw_svector_ostream os{contents};
520   Binding lastBinding;
521 
522   for (const Defined *defined : definitions)
523     encodeWeakOverride(defined, os);
524 
525   for (auto &p : sortBindings(bindingsMap)) {
526     const Symbol *sym = p.first;
527     std::vector<BindingEntry> &bindings = p.second;
528     os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
529        << sym->getName() << '\0'
530        << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
531     std::vector<BindIR> opcodes;
532     for (const BindingEntry &b : bindings)
533       encodeBinding(b.target.isec->parent,
534                     b.target.isec->getOffset(b.target.offset), b.addend,
535                     lastBinding, opcodes);
536     if (config->optimize > 1)
537       optimizeOpcodes(opcodes);
538     for (const auto &op : opcodes)
539       flushOpcodes(op, os);
540   }
541   if (!bindingsMap.empty() || !definitions.empty())
542     os << static_cast<uint8_t>(BIND_OPCODE_DONE);
543 }
544 
545 void WeakBindingSection::writeTo(uint8_t *buf) const {
546   memcpy(buf, contents.data(), contents.size());
547 }
548 
549 StubsSection::StubsSection()
550     : SyntheticSection(segment_names::text, section_names::stubs) {
551   flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
552   // The stubs section comprises machine instructions, which are aligned to
553   // 4 bytes on the archs we care about.
554   align = 4;
555   reserved2 = target->stubSize;
556 }
557 
558 uint64_t StubsSection::getSize() const {
559   return entries.size() * target->stubSize;
560 }
561 
562 void StubsSection::writeTo(uint8_t *buf) const {
563   size_t off = 0;
564   for (const Symbol *sym : entries) {
565     target->writeStub(buf + off, *sym);
566     off += target->stubSize;
567   }
568 }
569 
570 void StubsSection::finalize() { isFinal = true; }
571 
572 bool StubsSection::addEntry(Symbol *sym) {
573   bool inserted = entries.insert(sym);
574   if (inserted)
575     sym->stubsIndex = entries.size() - 1;
576   return inserted;
577 }
578 
579 StubHelperSection::StubHelperSection()
580     : SyntheticSection(segment_names::text, section_names::stubHelper) {
581   flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
582   align = 4; // This section comprises machine instructions
583 }
584 
585 uint64_t StubHelperSection::getSize() const {
586   return target->stubHelperHeaderSize +
587          in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
588 }
589 
590 bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
591 
592 void StubHelperSection::writeTo(uint8_t *buf) const {
593   target->writeStubHelperHeader(buf);
594   size_t off = target->stubHelperHeaderSize;
595   for (const DylibSymbol *sym : in.lazyBinding->getEntries()) {
596     target->writeStubHelperEntry(buf + off, *sym, addr + off);
597     off += target->stubHelperEntrySize;
598   }
599 }
600 
601 void StubHelperSection::setup() {
602   Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr,
603                                         /*isWeakRef=*/false);
604   if (auto *undefined = dyn_cast<Undefined>(binder))
605     treatUndefinedSymbol(*undefined,
606                          "lazy binding (normally in libSystem.dylib)");
607 
608   // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
609   stubBinder = dyn_cast_or_null<DylibSymbol>(binder);
610   if (stubBinder == nullptr)
611     return;
612 
613   in.got->addEntry(stubBinder);
614 
615   in.imageLoaderCache->parent =
616       ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
617   inputSections.push_back(in.imageLoaderCache);
618   // Since this isn't in the symbol table or in any input file, the noDeadStrip
619   // argument doesn't matter. It's kept alive by ImageLoaderCacheSection()
620   // setting `live` to true on the backing InputSection.
621   dyldPrivate =
622       make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0,
623                     /*isWeakDef=*/false,
624                     /*isExternal=*/false, /*isPrivateExtern=*/false,
625                     /*isThumb=*/false, /*isReferencedDynamically=*/false,
626                     /*noDeadStrip=*/false);
627 }
628 
629 LazyPointerSection::LazyPointerSection()
630     : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
631   align = target->wordSize;
632   flags = S_LAZY_SYMBOL_POINTERS;
633 }
634 
635 uint64_t LazyPointerSection::getSize() const {
636   return in.stubs->getEntries().size() * target->wordSize;
637 }
638 
639 bool LazyPointerSection::isNeeded() const {
640   return !in.stubs->getEntries().empty();
641 }
642 
643 void LazyPointerSection::writeTo(uint8_t *buf) const {
644   size_t off = 0;
645   for (const Symbol *sym : in.stubs->getEntries()) {
646     if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
647       if (dysym->hasStubsHelper()) {
648         uint64_t stubHelperOffset =
649             target->stubHelperHeaderSize +
650             dysym->stubsHelperIndex * target->stubHelperEntrySize;
651         write64le(buf + off, in.stubHelper->addr + stubHelperOffset);
652       }
653     } else {
654       write64le(buf + off, sym->getVA());
655     }
656     off += target->wordSize;
657   }
658 }
659 
660 LazyBindingSection::LazyBindingSection()
661     : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
662 
663 void LazyBindingSection::finalizeContents() {
664   // TODO: Just precompute output size here instead of writing to a temporary
665   // buffer
666   for (DylibSymbol *sym : entries)
667     sym->lazyBindOffset = encode(*sym);
668 }
669 
670 void LazyBindingSection::writeTo(uint8_t *buf) const {
671   memcpy(buf, contents.data(), contents.size());
672 }
673 
674 void LazyBindingSection::addEntry(DylibSymbol *dysym) {
675   if (entries.insert(dysym)) {
676     dysym->stubsHelperIndex = entries.size() - 1;
677     in.rebase->addEntry(in.lazyPointers->isec,
678                         dysym->stubsIndex * target->wordSize);
679   }
680 }
681 
682 // Unlike the non-lazy binding section, the bind opcodes in this section aren't
683 // interpreted all at once. Rather, dyld will start interpreting opcodes at a
684 // given offset, typically only binding a single symbol before it finds a
685 // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
686 // we cannot encode just the differences between symbols; we have to emit the
687 // complete bind information for each symbol.
688 uint32_t LazyBindingSection::encode(const DylibSymbol &sym) {
689   uint32_t opstreamOffset = contents.size();
690   OutputSegment *dataSeg = in.lazyPointers->parent;
691   os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
692                              dataSeg->index);
693   uint64_t offset = in.lazyPointers->addr - dataSeg->addr +
694                     sym.stubsIndex * target->wordSize;
695   encodeULEB128(offset, os);
696   encodeDylibOrdinal(ordinalForDylibSymbol(sym), os);
697 
698   uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
699   if (sym.isWeakRef())
700     flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
701 
702   os << flags << sym.getName() << '\0'
703      << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
704      << static_cast<uint8_t>(BIND_OPCODE_DONE);
705   return opstreamOffset;
706 }
707 
708 ExportSection::ExportSection()
709     : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
710 
711 void ExportSection::finalizeContents() {
712   trieBuilder.setImageBase(in.header->addr);
713   for (const Symbol *sym : symtab->getSymbols()) {
714     if (const auto *defined = dyn_cast<Defined>(sym)) {
715       if (defined->privateExtern || !defined->isLive())
716         continue;
717       trieBuilder.addSymbol(*defined);
718       hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
719     }
720   }
721   size = trieBuilder.build();
722 }
723 
724 void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
725 
726 DataInCodeSection::DataInCodeSection()
727     : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {}
728 
729 template <class LP>
730 static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
731   using SegmentCommand = typename LP::segment_command;
732   using Section = typename LP::section;
733 
734   std::vector<MachO::data_in_code_entry> dataInCodeEntries;
735   for (const InputFile *inputFile : inputFiles) {
736     if (!isa<ObjFile>(inputFile))
737       continue;
738     const ObjFile *objFile = cast<ObjFile>(inputFile);
739     const auto *c = reinterpret_cast<const SegmentCommand *>(
740         findCommand(objFile->mb.getBufferStart(), LP::segmentLCType));
741     if (!c)
742       continue;
743     ArrayRef<Section> sections{reinterpret_cast<const Section *>(c + 1),
744                                c->nsects};
745 
746     ArrayRef<MachO::data_in_code_entry> entries = objFile->dataInCodeEntries;
747     if (entries.empty())
748       continue;
749     // For each code subsection find 'data in code' entries residing in it.
750     // Compute the new offset values as
751     // <offset within subsection> + <subsection address> - <__TEXT address>.
752     for (size_t i = 0, n = sections.size(); i < n; ++i) {
753       const SubsectionMap &subsecMap = objFile->subsections[i];
754       for (const SubsectionEntry &subsecEntry : subsecMap) {
755         const InputSection *isec = subsecEntry.isec;
756         if (!isCodeSection(isec))
757           continue;
758         if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput())
759           continue;
760         const uint64_t beginAddr = sections[i].addr + subsecEntry.offset;
761         auto it = llvm::lower_bound(
762             entries, beginAddr,
763             [](const MachO::data_in_code_entry &entry, uint64_t addr) {
764               return entry.offset < addr;
765             });
766         const uint64_t endAddr = beginAddr + isec->getFileSize();
767         for (const auto end = entries.end();
768              it != end && it->offset + it->length <= endAddr; ++it)
769           dataInCodeEntries.push_back(
770               {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) -
771                                      in.header->addr),
772                it->length, it->kind});
773       }
774     }
775   }
776   return dataInCodeEntries;
777 }
778 
779 void DataInCodeSection::finalizeContents() {
780   entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>()
781                                   : collectDataInCodeEntries<ILP32>();
782 }
783 
784 void DataInCodeSection::writeTo(uint8_t *buf) const {
785   if (!entries.empty())
786     memcpy(buf, entries.data(), getRawSize());
787 }
788 
789 FunctionStartsSection::FunctionStartsSection()
790     : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
791 
792 void FunctionStartsSection::finalizeContents() {
793   raw_svector_ostream os{contents};
794   std::vector<uint64_t> addrs;
795   for (const Symbol *sym : symtab->getSymbols()) {
796     if (const auto *defined = dyn_cast<Defined>(sym)) {
797       if (!defined->isec || !isCodeSection(defined->isec) || !defined->isLive())
798         continue;
799       if (const auto *concatIsec = dyn_cast<ConcatInputSection>(defined->isec))
800         if (concatIsec->shouldOmitFromOutput())
801           continue;
802       // TODO: Add support for thumbs, in that case
803       // the lowest bit of nextAddr needs to be set to 1.
804       addrs.push_back(defined->getVA());
805     }
806   }
807   llvm::sort(addrs);
808   uint64_t addr = in.header->addr;
809   for (uint64_t nextAddr : addrs) {
810     uint64_t delta = nextAddr - addr;
811     if (delta == 0)
812       continue;
813     encodeULEB128(delta, os);
814     addr = nextAddr;
815   }
816   os << '\0';
817 }
818 
819 void FunctionStartsSection::writeTo(uint8_t *buf) const {
820   memcpy(buf, contents.data(), contents.size());
821 }
822 
823 SymtabSection::SymtabSection(StringTableSection &stringTableSection)
824     : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
825       stringTableSection(stringTableSection) {}
826 
827 void SymtabSection::emitBeginSourceStab(DWARFUnit *compileUnit) {
828   StabsEntry stab(N_SO);
829   SmallString<261> dir(compileUnit->getCompilationDir());
830   StringRef sep = sys::path::get_separator();
831   // We don't use `path::append` here because we want an empty `dir` to result
832   // in an absolute path. `append` would give us a relative path for that case.
833   if (!dir.endswith(sep))
834     dir += sep;
835   stab.strx = stringTableSection.addString(
836       saver.save(dir + compileUnit->getUnitDIE().getShortName()));
837   stabs.emplace_back(std::move(stab));
838 }
839 
840 void SymtabSection::emitEndSourceStab() {
841   StabsEntry stab(N_SO);
842   stab.sect = 1;
843   stabs.emplace_back(std::move(stab));
844 }
845 
846 void SymtabSection::emitObjectFileStab(ObjFile *file) {
847   StabsEntry stab(N_OSO);
848   stab.sect = target->cpuSubtype;
849   SmallString<261> path(!file->archiveName.empty() ? file->archiveName
850                                                    : file->getName());
851   std::error_code ec = sys::fs::make_absolute(path);
852   if (ec)
853     fatal("failed to get absolute path for " + path);
854 
855   if (!file->archiveName.empty())
856     path.append({"(", file->getName(), ")"});
857 
858   stab.strx = stringTableSection.addString(saver.save(path.str()));
859   stab.desc = 1;
860   stab.value = file->modTime;
861   stabs.emplace_back(std::move(stab));
862 }
863 
864 void SymtabSection::emitEndFunStab(Defined *defined) {
865   StabsEntry stab(N_FUN);
866   stab.value = defined->size;
867   stabs.emplace_back(std::move(stab));
868 }
869 
870 void SymtabSection::emitStabs() {
871   for (const std::string &s : config->astPaths) {
872     StabsEntry astStab(N_AST);
873     astStab.strx = stringTableSection.addString(s);
874     stabs.emplace_back(std::move(astStab));
875   }
876 
877   std::vector<Defined *> symbolsNeedingStabs;
878   for (const SymtabEntry &entry :
879        concat<SymtabEntry>(localSymbols, externalSymbols)) {
880     Symbol *sym = entry.sym;
881     assert(sym->isLive() &&
882            "dead symbols should not be in localSymbols, externalSymbols");
883     if (auto *defined = dyn_cast<Defined>(sym)) {
884       if (defined->isAbsolute())
885         continue;
886       InputSection *isec = defined->isec;
887       ObjFile *file = dyn_cast_or_null<ObjFile>(isec->getFile());
888       if (!file || !file->compileUnit)
889         continue;
890       symbolsNeedingStabs.push_back(defined);
891     }
892   }
893 
894   llvm::stable_sort(symbolsNeedingStabs, [&](Defined *a, Defined *b) {
895     return a->isec->getFile()->id < b->isec->getFile()->id;
896   });
897 
898   // Emit STABS symbols so that dsymutil and/or the debugger can map address
899   // regions in the final binary to the source and object files from which they
900   // originated.
901   InputFile *lastFile = nullptr;
902   for (Defined *defined : symbolsNeedingStabs) {
903     InputSection *isec = defined->isec;
904     ObjFile *file = cast<ObjFile>(isec->getFile());
905 
906     if (lastFile == nullptr || lastFile != file) {
907       if (lastFile != nullptr)
908         emitEndSourceStab();
909       lastFile = file;
910 
911       emitBeginSourceStab(file->compileUnit);
912       emitObjectFileStab(file);
913     }
914 
915     StabsEntry symStab;
916     symStab.sect = defined->isec->canonical()->parent->index;
917     symStab.strx = stringTableSection.addString(defined->getName());
918     symStab.value = defined->getVA();
919 
920     if (isCodeSection(isec)) {
921       symStab.type = N_FUN;
922       stabs.emplace_back(std::move(symStab));
923       emitEndFunStab(defined);
924     } else {
925       symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
926       stabs.emplace_back(std::move(symStab));
927     }
928   }
929 
930   if (!stabs.empty())
931     emitEndSourceStab();
932 }
933 
934 void SymtabSection::finalizeContents() {
935   auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
936     uint32_t strx = stringTableSection.addString(sym->getName());
937     symbols.push_back({sym, strx});
938   };
939 
940   // Local symbols aren't in the SymbolTable, so we walk the list of object
941   // files to gather them.
942   for (const InputFile *file : inputFiles) {
943     if (auto *objFile = dyn_cast<ObjFile>(file)) {
944       for (Symbol *sym : objFile->symbols) {
945         if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
946           if (!defined->isExternal() && defined->isLive()) {
947             StringRef name = defined->getName();
948             if (!name.startswith("l") && !name.startswith("L"))
949               addSymbol(localSymbols, sym);
950           }
951         }
952       }
953     }
954   }
955 
956   // __dyld_private is a local symbol too. It's linker-created and doesn't
957   // exist in any object file.
958   if (Defined *dyldPrivate = in.stubHelper->dyldPrivate)
959     addSymbol(localSymbols, dyldPrivate);
960 
961   for (Symbol *sym : symtab->getSymbols()) {
962     if (!sym->isLive())
963       continue;
964     if (auto *defined = dyn_cast<Defined>(sym)) {
965       if (!defined->includeInSymtab)
966         continue;
967       assert(defined->isExternal());
968       if (defined->privateExtern)
969         addSymbol(localSymbols, defined);
970       else
971         addSymbol(externalSymbols, defined);
972     } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
973       if (dysym->isReferenced())
974         addSymbol(undefinedSymbols, sym);
975     }
976   }
977 
978   emitStabs();
979   uint32_t symtabIndex = stabs.size();
980   for (const SymtabEntry &entry :
981        concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) {
982     entry.sym->symtabIndex = symtabIndex++;
983   }
984 }
985 
986 uint32_t SymtabSection::getNumSymbols() const {
987   return stabs.size() + localSymbols.size() + externalSymbols.size() +
988          undefinedSymbols.size();
989 }
990 
991 // This serves to hide (type-erase) the template parameter from SymtabSection.
992 template <class LP> class SymtabSectionImpl final : public SymtabSection {
993 public:
994   SymtabSectionImpl(StringTableSection &stringTableSection)
995       : SymtabSection(stringTableSection) {}
996   uint64_t getRawSize() const override;
997   void writeTo(uint8_t *buf) const override;
998 };
999 
1000 template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
1001   return getNumSymbols() * sizeof(typename LP::nlist);
1002 }
1003 
1004 template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
1005   auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
1006   // Emit the stabs entries before the "real" symbols. We cannot emit them
1007   // after as that would render Symbol::symtabIndex inaccurate.
1008   for (const StabsEntry &entry : stabs) {
1009     nList->n_strx = entry.strx;
1010     nList->n_type = entry.type;
1011     nList->n_sect = entry.sect;
1012     nList->n_desc = entry.desc;
1013     nList->n_value = entry.value;
1014     ++nList;
1015   }
1016 
1017   for (const SymtabEntry &entry : concat<const SymtabEntry>(
1018            localSymbols, externalSymbols, undefinedSymbols)) {
1019     nList->n_strx = entry.strx;
1020     // TODO populate n_desc with more flags
1021     if (auto *defined = dyn_cast<Defined>(entry.sym)) {
1022       uint8_t scope = 0;
1023       if (defined->privateExtern) {
1024         // Private external -- dylib scoped symbol.
1025         // Promote to non-external at link time.
1026         scope = N_PEXT;
1027       } else if (defined->isExternal()) {
1028         // Normal global symbol.
1029         scope = N_EXT;
1030       } else {
1031         // TU-local symbol from localSymbols.
1032         scope = 0;
1033       }
1034 
1035       if (defined->isAbsolute()) {
1036         nList->n_type = scope | N_ABS;
1037         nList->n_sect = NO_SECT;
1038         nList->n_value = defined->value;
1039       } else {
1040         nList->n_type = scope | N_SECT;
1041         nList->n_sect = defined->isec->canonical()->parent->index;
1042         // For the N_SECT symbol type, n_value is the address of the symbol
1043         nList->n_value = defined->getVA();
1044       }
1045       nList->n_desc |= defined->thumb ? N_ARM_THUMB_DEF : 0;
1046       nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
1047       nList->n_desc |=
1048           defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
1049     } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
1050       uint16_t n_desc = nList->n_desc;
1051       int16_t ordinal = ordinalForDylibSymbol(*dysym);
1052       if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
1053         SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
1054       else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
1055         SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
1056       else {
1057         assert(ordinal > 0);
1058         SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
1059       }
1060 
1061       nList->n_type = N_EXT;
1062       n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
1063       n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
1064       nList->n_desc = n_desc;
1065     }
1066     ++nList;
1067   }
1068 }
1069 
1070 template <class LP>
1071 SymtabSection *
1072 macho::makeSymtabSection(StringTableSection &stringTableSection) {
1073   return make<SymtabSectionImpl<LP>>(stringTableSection);
1074 }
1075 
1076 IndirectSymtabSection::IndirectSymtabSection()
1077     : LinkEditSection(segment_names::linkEdit,
1078                       section_names::indirectSymbolTable) {}
1079 
1080 uint32_t IndirectSymtabSection::getNumSymbols() const {
1081   return in.got->getEntries().size() + in.tlvPointers->getEntries().size() +
1082          2 * in.stubs->getEntries().size();
1083 }
1084 
1085 bool IndirectSymtabSection::isNeeded() const {
1086   return in.got->isNeeded() || in.tlvPointers->isNeeded() ||
1087          in.stubs->isNeeded();
1088 }
1089 
1090 void IndirectSymtabSection::finalizeContents() {
1091   uint32_t off = 0;
1092   in.got->reserved1 = off;
1093   off += in.got->getEntries().size();
1094   in.tlvPointers->reserved1 = off;
1095   off += in.tlvPointers->getEntries().size();
1096   in.stubs->reserved1 = off;
1097   off += in.stubs->getEntries().size();
1098   in.lazyPointers->reserved1 = off;
1099 }
1100 
1101 static uint32_t indirectValue(const Symbol *sym) {
1102   return sym->symtabIndex != UINT32_MAX ? sym->symtabIndex
1103                                         : INDIRECT_SYMBOL_LOCAL;
1104 }
1105 
1106 void IndirectSymtabSection::writeTo(uint8_t *buf) const {
1107   uint32_t off = 0;
1108   for (const Symbol *sym : in.got->getEntries()) {
1109     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1110     ++off;
1111   }
1112   for (const Symbol *sym : in.tlvPointers->getEntries()) {
1113     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1114     ++off;
1115   }
1116   for (const Symbol *sym : in.stubs->getEntries()) {
1117     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1118     ++off;
1119   }
1120   // There is a 1:1 correspondence between stubs and LazyPointerSection
1121   // entries. But giving __stubs and __la_symbol_ptr the same reserved1
1122   // (the offset into the indirect symbol table) so that they both refer
1123   // to the same range of offsets confuses `strip`, so write the stubs
1124   // symbol table offsets a second time.
1125   for (const Symbol *sym : in.stubs->getEntries()) {
1126     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1127     ++off;
1128   }
1129 }
1130 
1131 StringTableSection::StringTableSection()
1132     : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
1133 
1134 uint32_t StringTableSection::addString(StringRef str) {
1135   uint32_t strx = size;
1136   strings.push_back(str); // TODO: consider deduplicating strings
1137   size += str.size() + 1; // account for null terminator
1138   return strx;
1139 }
1140 
1141 void StringTableSection::writeTo(uint8_t *buf) const {
1142   uint32_t off = 0;
1143   for (StringRef str : strings) {
1144     memcpy(buf + off, str.data(), str.size());
1145     off += str.size() + 1; // account for null terminator
1146   }
1147 }
1148 
1149 CodeSignatureSection::CodeSignatureSection()
1150     : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
1151   align = object::CodeSignatureSection::Align; // required by libstuff
1152 }
1153 
1154 uint64_t CodeSignatureSection::getRawSize() const {
1155   return static_cast<uint64_t>(sectionBuilder->getRawSize());
1156 }
1157 
1158 void CodeSignatureSection::writeHashes(uint8_t *buf) const {
1159   sectionBuilder->write(buf);
1160 }
1161 
1162 void CodeSignatureSection::writeTo(uint8_t *buf) const {
1163   // The entire code section including header is written
1164   // in CodeSignatureSection::writeHashes above.
1165 }
1166 
1167 void CodeSignatureSection::finalize() {
1168   OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text);
1169   // NOTE: ld64 seems to also use outputFile instead of finalOutput
1170   sectionBuilder = std::make_unique<object::CodeSignatureSection>(
1171       fileOff, config->outputFile, config->outputType, textSeg->fileOff,
1172       textSeg->fileSize);
1173 }
1174 
1175 BitcodeBundleSection::BitcodeBundleSection()
1176     : SyntheticSection(segment_names::llvm, section_names::bitcodeBundle) {}
1177 
1178 class ErrorCodeWrapper {
1179 public:
1180   explicit ErrorCodeWrapper(std::error_code ec) : errorCode(ec.value()) {}
1181   explicit ErrorCodeWrapper(int ec) : errorCode(ec) {}
1182   operator int() const { return errorCode; }
1183 
1184 private:
1185   int errorCode;
1186 };
1187 
1188 #define CHECK_EC(exp)                                                          \
1189   do {                                                                         \
1190     ErrorCodeWrapper ec(exp);                                                  \
1191     if (ec)                                                                    \
1192       fatal(Twine("operation failed with error code ") + Twine(ec) + ": " +    \
1193             #exp);                                                             \
1194   } while (0);
1195 
1196 void BitcodeBundleSection::finalize() {
1197 #ifdef LLVM_HAVE_LIBXAR
1198   using namespace llvm::sys::fs;
1199   CHECK_EC(createTemporaryFile("bitcode-bundle", "xar", xarPath));
1200 
1201   xar_t xar(xar_open(xarPath.data(), O_RDWR));
1202   if (!xar)
1203     fatal("failed to open XAR temporary file at " + xarPath);
1204   CHECK_EC(xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE));
1205   // FIXME: add more data to XAR
1206   CHECK_EC(xar_close(xar));
1207 
1208   file_size(xarPath, xarSize);
1209 #endif // defined(LLVM_HAVE_LIBXAR)
1210 }
1211 
1212 void BitcodeBundleSection::writeTo(uint8_t *buf) const {
1213   using namespace llvm::sys::fs;
1214   file_t handle =
1215       CHECK(openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None),
1216             "failed to open XAR file");
1217   std::error_code ec;
1218   mapped_file_region xarMap(handle, mapped_file_region::mapmode::readonly,
1219                             xarSize, 0, ec);
1220   if (ec)
1221     fatal("failed to map XAR file");
1222   memcpy(buf, xarMap.const_data(), xarSize);
1223 
1224   closeFile(handle);
1225   remove(xarPath);
1226 }
1227 
1228 CStringSection::CStringSection()
1229     : SyntheticSection(segment_names::text, section_names::cString) {
1230   flags = S_CSTRING_LITERALS;
1231 }
1232 
1233 void CStringSection::addInput(CStringInputSection *isec) {
1234   isec->parent = this;
1235   inputs.push_back(isec);
1236   if (isec->align > align)
1237     align = isec->align;
1238 }
1239 
1240 void CStringSection::writeTo(uint8_t *buf) const {
1241   for (const CStringInputSection *isec : inputs) {
1242     for (size_t i = 0, e = isec->pieces.size(); i != e; ++i) {
1243       if (!isec->pieces[i].live)
1244         continue;
1245       StringRef string = isec->getStringRef(i);
1246       memcpy(buf + isec->pieces[i].outSecOff, string.data(), string.size());
1247     }
1248   }
1249 }
1250 
1251 void CStringSection::finalizeContents() {
1252   uint64_t offset = 0;
1253   for (CStringInputSection *isec : inputs) {
1254     for (size_t i = 0, e = isec->pieces.size(); i != e; ++i) {
1255       if (!isec->pieces[i].live)
1256         continue;
1257       uint32_t pieceAlign = MinAlign(isec->pieces[i].inSecOff, align);
1258       offset = alignTo(offset, pieceAlign);
1259       isec->pieces[i].outSecOff = offset;
1260       isec->isFinal = true;
1261       StringRef string = isec->getStringRef(i);
1262       offset += string.size();
1263     }
1264   }
1265   size = offset;
1266 }
1267 // Mergeable cstring literals are found under the __TEXT,__cstring section. In
1268 // contrast to ELF, which puts strings that need different alignments into
1269 // different sections, clang's Mach-O backend puts them all in one section.
1270 // Strings that need to be aligned have the .p2align directive emitted before
1271 // them, which simply translates into zero padding in the object file.
1272 //
1273 // I *think* ld64 extracts the desired per-string alignment from this data by
1274 // preserving each string's offset from the last section-aligned address. I'm
1275 // not entirely certain since it doesn't seem consistent about doing this, and
1276 // in fact doesn't seem to be correct in general: we can in fact can induce ld64
1277 // to produce a crashing binary just by linking in an additional object file
1278 // that only contains a duplicate cstring at a different alignment. See PR50563
1279 // for details.
1280 //
1281 // On x86_64, the cstrings we've seen so far that require special alignment are
1282 // all accessed by SIMD operations -- x86_64 requires SIMD accesses to be
1283 // 16-byte-aligned. arm64 also seems to require 16-byte-alignment in some cases
1284 // (PR50791), but I haven't tracked down the root cause. So for now, I'm just
1285 // aligning all strings to 16 bytes.  This is indeed wasteful, but
1286 // implementation-wise it's simpler than preserving per-string
1287 // alignment+offsets. It also avoids the aforementioned crash after
1288 // deduplication of differently-aligned strings.  Finally, the overhead is not
1289 // huge: using 16-byte alignment (vs no alignment) is only a 0.5% size overhead
1290 // when linking chromium_framework on x86_64.
1291 DeduplicatedCStringSection::DeduplicatedCStringSection()
1292     : builder(StringTableBuilder::RAW, /*Alignment=*/16) {}
1293 
1294 void DeduplicatedCStringSection::finalizeContents() {
1295   // Add all string pieces to the string table builder to create section
1296   // contents.
1297   for (const CStringInputSection *isec : inputs)
1298     for (size_t i = 0, e = isec->pieces.size(); i != e; ++i)
1299       if (isec->pieces[i].live)
1300         builder.add(isec->getCachedHashStringRef(i));
1301 
1302   // Fix the string table content. After this, the contents will never change.
1303   builder.finalizeInOrder();
1304 
1305   // finalize() fixed tail-optimized strings, so we can now get
1306   // offsets of strings. Get an offset for each string and save it
1307   // to a corresponding SectionPiece for easy access.
1308   for (CStringInputSection *isec : inputs) {
1309     for (size_t i = 0, e = isec->pieces.size(); i != e; ++i) {
1310       if (!isec->pieces[i].live)
1311         continue;
1312       isec->pieces[i].outSecOff =
1313           builder.getOffset(isec->getCachedHashStringRef(i));
1314       isec->isFinal = true;
1315     }
1316   }
1317 }
1318 
1319 // This section is actually emitted as __TEXT,__const by ld64, but clang may
1320 // emit input sections of that name, and LLD doesn't currently support mixing
1321 // synthetic and concat-type OutputSections. To work around this, I've given
1322 // our merged-literals section a different name.
1323 WordLiteralSection::WordLiteralSection()
1324     : SyntheticSection(segment_names::text, section_names::literals) {
1325   align = 16;
1326 }
1327 
1328 void WordLiteralSection::addInput(WordLiteralInputSection *isec) {
1329   isec->parent = this;
1330   inputs.push_back(isec);
1331 }
1332 
1333 void WordLiteralSection::finalizeContents() {
1334   for (WordLiteralInputSection *isec : inputs) {
1335     // We do all processing of the InputSection here, so it will be effectively
1336     // finalized.
1337     isec->isFinal = true;
1338     const uint8_t *buf = isec->data.data();
1339     switch (sectionType(isec->getFlags())) {
1340     case S_4BYTE_LITERALS: {
1341       for (size_t off = 0, e = isec->data.size(); off < e; off += 4) {
1342         if (!isec->isLive(off))
1343           continue;
1344         uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off);
1345         literal4Map.emplace(value, literal4Map.size());
1346       }
1347       break;
1348     }
1349     case S_8BYTE_LITERALS: {
1350       for (size_t off = 0, e = isec->data.size(); off < e; off += 8) {
1351         if (!isec->isLive(off))
1352           continue;
1353         uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off);
1354         literal8Map.emplace(value, literal8Map.size());
1355       }
1356       break;
1357     }
1358     case S_16BYTE_LITERALS: {
1359       for (size_t off = 0, e = isec->data.size(); off < e; off += 16) {
1360         if (!isec->isLive(off))
1361           continue;
1362         UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off);
1363         literal16Map.emplace(value, literal16Map.size());
1364       }
1365       break;
1366     }
1367     default:
1368       llvm_unreachable("invalid literal section type");
1369     }
1370   }
1371 }
1372 
1373 void WordLiteralSection::writeTo(uint8_t *buf) const {
1374   // Note that we don't attempt to do any endianness conversion in addInput(),
1375   // so we don't do it here either -- just write out the original value,
1376   // byte-for-byte.
1377   for (const auto &p : literal16Map)
1378     memcpy(buf + p.second * 16, &p.first, 16);
1379   buf += literal16Map.size() * 16;
1380 
1381   for (const auto &p : literal8Map)
1382     memcpy(buf + p.second * 8, &p.first, 8);
1383   buf += literal8Map.size() * 8;
1384 
1385   for (const auto &p : literal4Map)
1386     memcpy(buf + p.second * 4, &p.first, 4);
1387 }
1388 
1389 void macho::createSyntheticSymbols() {
1390   auto addHeaderSymbol = [](const char *name) {
1391     symtab->addSynthetic(name, in.header->isec, /*value=*/0,
1392                          /*privateExtern=*/true, /*includeInSymtab=*/false,
1393                          /*referencedDynamically=*/false);
1394   };
1395 
1396   switch (config->outputType) {
1397     // FIXME: Assign the right address value for these symbols
1398     // (rather than 0). But we need to do that after assignAddresses().
1399   case MH_EXECUTE:
1400     // If linking PIE, __mh_execute_header is a defined symbol in
1401     //  __TEXT, __text)
1402     // Otherwise, it's an absolute symbol.
1403     if (config->isPic)
1404       symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0,
1405                            /*privateExtern=*/false, /*includeInSymtab=*/true,
1406                            /*referencedDynamically=*/true);
1407     else
1408       symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
1409                            /*privateExtern=*/false, /*includeInSymtab=*/true,
1410                            /*referencedDynamically=*/true);
1411     break;
1412 
1413     // The following symbols are N_SECT symbols, even though the header is not
1414     // part of any section and that they are private to the bundle/dylib/object
1415     // they are part of.
1416   case MH_BUNDLE:
1417     addHeaderSymbol("__mh_bundle_header");
1418     break;
1419   case MH_DYLIB:
1420     addHeaderSymbol("__mh_dylib_header");
1421     break;
1422   case MH_DYLINKER:
1423     addHeaderSymbol("__mh_dylinker_header");
1424     break;
1425   case MH_OBJECT:
1426     addHeaderSymbol("__mh_object_header");
1427     break;
1428   default:
1429     llvm_unreachable("unexpected outputType");
1430     break;
1431   }
1432 
1433   // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
1434   // which does e.g. cleanup of static global variables. The ABI document
1435   // says that the pointer can point to any address in one of the dylib's
1436   // segments, but in practice ld64 seems to set it to point to the header,
1437   // so that's what's implemented here.
1438   addHeaderSymbol("___dso_handle");
1439 }
1440 
1441 template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
1442 template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);
1443