xref: /freebsd/contrib/llvm-project/lld/MachO/SyntheticSections.cpp (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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/CommonLinkerContext.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Config/llvm-config.h"
22 #include "llvm/Support/EndianStream.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/Parallel.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/xxhash.h"
28 
29 #if defined(__APPLE__)
30 #include <sys/mman.h>
31 
32 #define COMMON_DIGEST_FOR_OPENSSL
33 #include <CommonCrypto/CommonDigest.h>
34 #else
35 #include "llvm/Support/SHA256.h"
36 #endif
37 
38 using namespace llvm;
39 using namespace llvm::MachO;
40 using namespace llvm::support;
41 using namespace llvm::support::endian;
42 using namespace lld;
43 using namespace lld::macho;
44 
45 // Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`.
46 static void sha256(const uint8_t *data, size_t len, uint8_t *output) {
47 #if defined(__APPLE__)
48   // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121
49   // for some notes on this.
50   CC_SHA256(data, len, output);
51 #else
52   ArrayRef<uint8_t> block(data, len);
53   std::array<uint8_t, 32> hash = SHA256::hash(block);
54   static_assert(hash.size() == CodeSignatureSection::hashSize);
55   memcpy(output, hash.data(), hash.size());
56 #endif
57 }
58 
59 InStruct macho::in;
60 std::vector<SyntheticSection *> macho::syntheticSections;
61 
62 SyntheticSection::SyntheticSection(const char *segname, const char *name)
63     : OutputSection(SyntheticKind, name) {
64   std::tie(this->segname, this->name) = maybeRenameSection({segname, name});
65   isec = makeSyntheticInputSection(segname, name);
66   isec->parent = this;
67   syntheticSections.push_back(this);
68 }
69 
70 // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
71 // from the beginning of the file (i.e. the header).
72 MachHeaderSection::MachHeaderSection()
73     : SyntheticSection(segment_names::text, section_names::header) {
74   // XXX: This is a hack. (See D97007)
75   // Setting the index to 1 to pretend that this section is the text
76   // section.
77   index = 1;
78   isec->isFinal = true;
79 }
80 
81 void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
82   loadCommands.push_back(lc);
83   sizeOfCmds += lc->getSize();
84 }
85 
86 uint64_t MachHeaderSection::getSize() const {
87   uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
88   // If we are emitting an encryptable binary, our load commands must have a
89   // separate (non-encrypted) page to themselves.
90   if (config->emitEncryptionInfo)
91     size = alignToPowerOf2(size, target->getPageSize());
92   return size;
93 }
94 
95 static uint32_t cpuSubtype() {
96   uint32_t subtype = target->cpuSubtype;
97 
98   if (config->outputType == MH_EXECUTE && !config->staticLink &&
99       target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
100       config->platform() == PLATFORM_MACOS &&
101       config->platformInfo.target.MinDeployment >= VersionTuple(10, 5))
102     subtype |= CPU_SUBTYPE_LIB64;
103 
104   return subtype;
105 }
106 
107 static bool hasWeakBinding() {
108   return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding()
109                                    : in.weakBinding->hasEntry();
110 }
111 
112 static bool hasNonWeakDefinition() {
113   return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition()
114                                    : in.weakBinding->hasNonWeakDefinition();
115 }
116 
117 void MachHeaderSection::writeTo(uint8_t *buf) const {
118   auto *hdr = reinterpret_cast<mach_header *>(buf);
119   hdr->magic = target->magic;
120   hdr->cputype = target->cpuType;
121   hdr->cpusubtype = cpuSubtype();
122   hdr->filetype = config->outputType;
123   hdr->ncmds = loadCommands.size();
124   hdr->sizeofcmds = sizeOfCmds;
125   hdr->flags = MH_DYLDLINK;
126 
127   if (config->namespaceKind == NamespaceKind::twolevel)
128     hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
129 
130   if (config->outputType == MH_DYLIB && !config->hasReexports)
131     hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
132 
133   if (config->markDeadStrippableDylib)
134     hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
135 
136   if (config->outputType == MH_EXECUTE && config->isPic)
137     hdr->flags |= MH_PIE;
138 
139   if (config->outputType == MH_DYLIB && config->applicationExtension)
140     hdr->flags |= MH_APP_EXTENSION_SAFE;
141 
142   if (in.exports->hasWeakSymbol || hasNonWeakDefinition())
143     hdr->flags |= MH_WEAK_DEFINES;
144 
145   if (in.exports->hasWeakSymbol || hasWeakBinding())
146     hdr->flags |= MH_BINDS_TO_WEAK;
147 
148   for (const OutputSegment *seg : outputSegments) {
149     for (const OutputSection *osec : seg->getSections()) {
150       if (isThreadLocalVariables(osec->flags)) {
151         hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
152         break;
153       }
154     }
155   }
156 
157   uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
158   for (const LoadCommand *lc : loadCommands) {
159     lc->writeTo(p);
160     p += lc->getSize();
161   }
162 }
163 
164 PageZeroSection::PageZeroSection()
165     : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
166 
167 RebaseSection::RebaseSection()
168     : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
169 
170 namespace {
171 struct RebaseState {
172   uint64_t sequenceLength;
173   uint64_t skipLength;
174 };
175 } // namespace
176 
177 static void emitIncrement(uint64_t incr, raw_svector_ostream &os) {
178   assert(incr != 0);
179 
180   if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK &&
181       (incr % target->wordSize) == 0) {
182     os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED |
183                                (incr >> target->p2WordSize));
184   } else {
185     os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
186     encodeULEB128(incr, os);
187   }
188 }
189 
190 static void flushRebase(const RebaseState &state, raw_svector_ostream &os) {
191   assert(state.sequenceLength > 0);
192 
193   if (state.skipLength == target->wordSize) {
194     if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) {
195       os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
196                                  state.sequenceLength);
197     } else {
198       os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
199       encodeULEB128(state.sequenceLength, os);
200     }
201   } else if (state.sequenceLength == 1) {
202     os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
203     encodeULEB128(state.skipLength - target->wordSize, os);
204   } else {
205     os << static_cast<uint8_t>(
206         REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
207     encodeULEB128(state.sequenceLength, os);
208     encodeULEB128(state.skipLength - target->wordSize, os);
209   }
210 }
211 
212 // Rebases are communicated to dyld using a bytecode, whose opcodes cause the
213 // memory location at a specific address to be rebased and/or the address to be
214 // incremented.
215 //
216 // Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic
217 // one, encoding a series of evenly spaced addresses. This algorithm works by
218 // splitting up the sorted list of addresses into such chunks. If the locations
219 // are consecutive or the sequence consists of a single location, flushRebase
220 // will use a smaller, more specialized encoding.
221 static void encodeRebases(const OutputSegment *seg,
222                           MutableArrayRef<Location> locations,
223                           raw_svector_ostream &os) {
224   // dyld operates on segments. Translate section offsets into segment offsets.
225   for (Location &loc : locations)
226     loc.offset =
227         loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
228   // The algorithm assumes that locations are unique.
229   Location *end =
230       llvm::unique(locations, [](const Location &a, const Location &b) {
231         return a.offset == b.offset;
232       });
233   size_t count = end - locations.begin();
234 
235   os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
236                              seg->index);
237   assert(!locations.empty());
238   uint64_t offset = locations[0].offset;
239   encodeULEB128(offset, os);
240 
241   RebaseState state{1, target->wordSize};
242 
243   for (size_t i = 1; i < count; ++i) {
244     offset = locations[i].offset;
245 
246     uint64_t skip = offset - locations[i - 1].offset;
247     assert(skip != 0 && "duplicate locations should have been weeded out");
248 
249     if (skip == state.skipLength) {
250       ++state.sequenceLength;
251     } else if (state.sequenceLength == 1) {
252       ++state.sequenceLength;
253       state.skipLength = skip;
254     } else if (skip < state.skipLength) {
255       // The address is lower than what the rebase pointer would be if the last
256       // location would be part of a sequence. We start a new sequence from the
257       // previous location.
258       --state.sequenceLength;
259       flushRebase(state, os);
260 
261       state.sequenceLength = 2;
262       state.skipLength = skip;
263     } else {
264       // The address is at some positive offset from the rebase pointer. We
265       // start a new sequence which begins with the current location.
266       flushRebase(state, os);
267       emitIncrement(skip - state.skipLength, os);
268       state.sequenceLength = 1;
269       state.skipLength = target->wordSize;
270     }
271   }
272   flushRebase(state, os);
273 }
274 
275 void RebaseSection::finalizeContents() {
276   if (locations.empty())
277     return;
278 
279   raw_svector_ostream os{contents};
280   os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
281 
282   llvm::sort(locations, [](const Location &a, const Location &b) {
283     return a.isec->getVA(a.offset) < b.isec->getVA(b.offset);
284   });
285 
286   for (size_t i = 0, count = locations.size(); i < count;) {
287     const OutputSegment *seg = locations[i].isec->parent->parent;
288     size_t j = i + 1;
289     while (j < count && locations[j].isec->parent->parent == seg)
290       ++j;
291     encodeRebases(seg, {locations.data() + i, locations.data() + j}, os);
292     i = j;
293   }
294   os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
295 }
296 
297 void RebaseSection::writeTo(uint8_t *buf) const {
298   memcpy(buf, contents.data(), contents.size());
299 }
300 
301 NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname,
302                                                      const char *name)
303     : SyntheticSection(segname, name) {
304   align = target->wordSize;
305 }
306 
307 void macho::addNonLazyBindingEntries(const Symbol *sym,
308                                      const InputSection *isec, uint64_t offset,
309                                      int64_t addend) {
310   if (config->emitChainedFixups) {
311     if (needsBinding(sym))
312       in.chainedFixups->addBinding(sym, isec, offset, addend);
313     else if (isa<Defined>(sym))
314       in.chainedFixups->addRebase(isec, offset);
315     else
316       llvm_unreachable("cannot bind to an undefined symbol");
317     return;
318   }
319 
320   if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
321     in.binding->addEntry(dysym, isec, offset, addend);
322     if (dysym->isWeakDef())
323       in.weakBinding->addEntry(sym, isec, offset, addend);
324   } else if (const auto *defined = dyn_cast<Defined>(sym)) {
325     in.rebase->addEntry(isec, offset);
326     if (defined->isExternalWeakDef())
327       in.weakBinding->addEntry(sym, isec, offset, addend);
328     else if (defined->interposable)
329       in.binding->addEntry(sym, isec, offset, addend);
330   } else {
331     // Undefined symbols are filtered out in scanRelocations(); we should never
332     // get here
333     llvm_unreachable("cannot bind to an undefined symbol");
334   }
335 }
336 
337 void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
338   if (entries.insert(sym)) {
339     assert(!sym->isInGot());
340     sym->gotIndex = entries.size() - 1;
341 
342     addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize);
343   }
344 }
345 
346 void macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA) {
347   assert(config->emitChainedFixups);
348   assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
349   auto *rebase = reinterpret_cast<dyld_chained_ptr_64_rebase *>(buf);
350   rebase->target = targetVA & 0xf'ffff'ffff;
351   rebase->high8 = (targetVA >> 56);
352   rebase->reserved = 0;
353   rebase->next = 0;
354   rebase->bind = 0;
355 
356   // The fixup format places a 64 GiB limit on the output's size.
357   // Should we handle this gracefully?
358   uint64_t encodedVA = rebase->target | ((uint64_t)rebase->high8 << 56);
359   if (encodedVA != targetVA)
360     error("rebase target address 0x" + Twine::utohexstr(targetVA) +
361           " does not fit into chained fixup. Re-link with -no_fixup_chains");
362 }
363 
364 static void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend) {
365   assert(config->emitChainedFixups);
366   assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
367   auto *bind = reinterpret_cast<dyld_chained_ptr_64_bind *>(buf);
368   auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend);
369   bind->ordinal = ordinal;
370   bind->addend = inlineAddend;
371   bind->reserved = 0;
372   bind->next = 0;
373   bind->bind = 1;
374 }
375 
376 void macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend) {
377   if (needsBinding(sym))
378     writeChainedBind(buf, sym, addend);
379   else
380     writeChainedRebase(buf, sym->getVA() + addend);
381 }
382 
383 void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
384   if (config->emitChainedFixups) {
385     for (const auto &[i, entry] : llvm::enumerate(entries))
386       writeChainedFixup(&buf[i * target->wordSize], entry, 0);
387   } else {
388     for (const auto &[i, entry] : llvm::enumerate(entries))
389       if (auto *defined = dyn_cast<Defined>(entry))
390         write64le(&buf[i * target->wordSize], defined->getVA());
391   }
392 }
393 
394 GotSection::GotSection()
395     : NonLazyPointerSectionBase(segment_names::data, section_names::got) {
396   flags = S_NON_LAZY_SYMBOL_POINTERS;
397 }
398 
399 TlvPointerSection::TlvPointerSection()
400     : NonLazyPointerSectionBase(segment_names::data,
401                                 section_names::threadPtrs) {
402   flags = S_THREAD_LOCAL_VARIABLE_POINTERS;
403 }
404 
405 BindingSection::BindingSection()
406     : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
407 
408 namespace {
409 struct Binding {
410   OutputSegment *segment = nullptr;
411   uint64_t offset = 0;
412   int64_t addend = 0;
413 };
414 struct BindIR {
415   // Default value of 0xF0 is not valid opcode and should make the program
416   // scream instead of accidentally writing "valid" values.
417   uint8_t opcode = 0xF0;
418   uint64_t data = 0;
419   uint64_t consecutiveCount = 0;
420 };
421 } // namespace
422 
423 // Encode a sequence of opcodes that tell dyld to write the address of symbol +
424 // addend at osec->addr + outSecOff.
425 //
426 // The bind opcode "interpreter" remembers the values of each binding field, so
427 // we only need to encode the differences between bindings. Hence the use of
428 // lastBinding.
429 static void encodeBinding(const OutputSection *osec, uint64_t outSecOff,
430                           int64_t addend, Binding &lastBinding,
431                           std::vector<BindIR> &opcodes) {
432   OutputSegment *seg = osec->parent;
433   uint64_t offset = osec->getSegmentOffset() + outSecOff;
434   if (lastBinding.segment != seg) {
435     opcodes.push_back(
436         {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
437                               seg->index),
438          offset});
439     lastBinding.segment = seg;
440     lastBinding.offset = offset;
441   } else if (lastBinding.offset != offset) {
442     opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset});
443     lastBinding.offset = offset;
444   }
445 
446   if (lastBinding.addend != addend) {
447     opcodes.push_back(
448         {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)});
449     lastBinding.addend = addend;
450   }
451 
452   opcodes.push_back({BIND_OPCODE_DO_BIND, 0});
453   // DO_BIND causes dyld to both perform the binding and increment the offset
454   lastBinding.offset += target->wordSize;
455 }
456 
457 static void optimizeOpcodes(std::vector<BindIR> &opcodes) {
458   // Pass 1: Combine bind/add pairs
459   size_t i;
460   int pWrite = 0;
461   for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
462     if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) &&
463         (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) {
464       opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
465       opcodes[pWrite].data = opcodes[i].data;
466       ++i;
467     } else {
468       opcodes[pWrite] = opcodes[i - 1];
469     }
470   }
471   if (i == opcodes.size())
472     opcodes[pWrite] = opcodes[i - 1];
473   opcodes.resize(pWrite + 1);
474 
475   // Pass 2: Compress two or more bind_add opcodes
476   pWrite = 0;
477   for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
478     if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
479         (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
480         (opcodes[i].data == opcodes[i - 1].data)) {
481       opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
482       opcodes[pWrite].consecutiveCount = 2;
483       opcodes[pWrite].data = opcodes[i].data;
484       ++i;
485       while (i < opcodes.size() &&
486              (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
487              (opcodes[i].data == opcodes[i - 1].data)) {
488         opcodes[pWrite].consecutiveCount++;
489         ++i;
490       }
491     } else {
492       opcodes[pWrite] = opcodes[i - 1];
493     }
494   }
495   if (i == opcodes.size())
496     opcodes[pWrite] = opcodes[i - 1];
497   opcodes.resize(pWrite + 1);
498 
499   // Pass 3: Use immediate encodings
500   // Every binding is the size of one pointer. If the next binding is a
501   // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the
502   // opcode can be scaled by wordSize into a single byte and dyld will
503   // expand it to the correct address.
504   for (auto &p : opcodes) {
505     // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK,
506     // but ld64 currently does this. This could be a potential bug, but
507     // for now, perform the same behavior to prevent mysterious bugs.
508     if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
509         ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) &&
510         ((p.data % target->wordSize) == 0)) {
511       p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
512       p.data /= target->wordSize;
513     }
514   }
515 }
516 
517 static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) {
518   uint8_t opcode = op.opcode & BIND_OPCODE_MASK;
519   switch (opcode) {
520   case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
521   case BIND_OPCODE_ADD_ADDR_ULEB:
522   case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
523     os << op.opcode;
524     encodeULEB128(op.data, os);
525     break;
526   case BIND_OPCODE_SET_ADDEND_SLEB:
527     os << op.opcode;
528     encodeSLEB128(static_cast<int64_t>(op.data), os);
529     break;
530   case BIND_OPCODE_DO_BIND:
531     os << op.opcode;
532     break;
533   case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
534     os << op.opcode;
535     encodeULEB128(op.consecutiveCount, os);
536     encodeULEB128(op.data, os);
537     break;
538   case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
539     os << static_cast<uint8_t>(op.opcode | op.data);
540     break;
541   default:
542     llvm_unreachable("cannot bind to an unrecognized symbol");
543   }
544 }
545 
546 // Non-weak bindings need to have their dylib ordinal encoded as well.
547 static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
548   if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
549     return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
550   assert(dysym.getFile()->isReferenced());
551   return dysym.getFile()->ordinal;
552 }
553 
554 static int16_t ordinalForSymbol(const Symbol &sym) {
555   if (const auto *dysym = dyn_cast<DylibSymbol>(&sym))
556     return ordinalForDylibSymbol(*dysym);
557   assert(cast<Defined>(&sym)->interposable);
558   return BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
559 }
560 
561 static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
562   if (ordinal <= 0) {
563     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
564                                (ordinal & BIND_IMMEDIATE_MASK));
565   } else if (ordinal <= BIND_IMMEDIATE_MASK) {
566     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
567   } else {
568     os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
569     encodeULEB128(ordinal, os);
570   }
571 }
572 
573 static void encodeWeakOverride(const Defined *defined,
574                                raw_svector_ostream &os) {
575   os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
576                              BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
577      << defined->getName() << '\0';
578 }
579 
580 // Organize the bindings so we can encoded them with fewer opcodes.
581 //
582 // First, all bindings for a given symbol should be grouped together.
583 // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it
584 // has an associated symbol string), so we only want to emit it once per symbol.
585 //
586 // Within each group, we sort the bindings by address. Since bindings are
587 // delta-encoded, sorting them allows for a more compact result. Note that
588 // sorting by address alone ensures that bindings for the same segment / section
589 // are located together, minimizing the number of times we have to emit
590 // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB.
591 //
592 // Finally, we sort the symbols by the address of their first binding, again
593 // to facilitate the delta-encoding process.
594 template <class Sym>
595 std::vector<std::pair<const Sym *, std::vector<BindingEntry>>>
596 sortBindings(const BindingsMap<const Sym *> &bindingsMap) {
597   std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec(
598       bindingsMap.begin(), bindingsMap.end());
599   for (auto &p : bindingsVec) {
600     std::vector<BindingEntry> &bindings = p.second;
601     llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
602       return a.target.getVA() < b.target.getVA();
603     });
604   }
605   llvm::sort(bindingsVec, [](const auto &a, const auto &b) {
606     return a.second[0].target.getVA() < b.second[0].target.getVA();
607   });
608   return bindingsVec;
609 }
610 
611 // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
612 // interprets to update a record with the following fields:
613 //  * segment index (of the segment to write the symbol addresses to, typically
614 //    the __DATA_CONST segment which contains the GOT)
615 //  * offset within the segment, indicating the next location to write a binding
616 //  * symbol type
617 //  * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
618 //  * symbol name
619 //  * addend
620 // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
621 // a symbol in the GOT, and increments the segment offset to point to the next
622 // entry. It does *not* clear the record state after doing the bind, so
623 // subsequent opcodes only need to encode the differences between bindings.
624 void BindingSection::finalizeContents() {
625   raw_svector_ostream os{contents};
626   Binding lastBinding;
627   int16_t lastOrdinal = 0;
628 
629   for (auto &p : sortBindings(bindingsMap)) {
630     const Symbol *sym = p.first;
631     std::vector<BindingEntry> &bindings = p.second;
632     uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
633     if (sym->isWeakRef())
634       flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
635     os << flags << sym->getName() << '\0'
636        << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
637     int16_t ordinal = ordinalForSymbol(*sym);
638     if (ordinal != lastOrdinal) {
639       encodeDylibOrdinal(ordinal, os);
640       lastOrdinal = ordinal;
641     }
642     std::vector<BindIR> opcodes;
643     for (const BindingEntry &b : bindings)
644       encodeBinding(b.target.isec->parent,
645                     b.target.isec->getOffset(b.target.offset), b.addend,
646                     lastBinding, opcodes);
647     if (config->optimize > 1)
648       optimizeOpcodes(opcodes);
649     for (const auto &op : opcodes)
650       flushOpcodes(op, os);
651   }
652   if (!bindingsMap.empty())
653     os << static_cast<uint8_t>(BIND_OPCODE_DONE);
654 }
655 
656 void BindingSection::writeTo(uint8_t *buf) const {
657   memcpy(buf, contents.data(), contents.size());
658 }
659 
660 WeakBindingSection::WeakBindingSection()
661     : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
662 
663 void WeakBindingSection::finalizeContents() {
664   raw_svector_ostream os{contents};
665   Binding lastBinding;
666 
667   for (const Defined *defined : definitions)
668     encodeWeakOverride(defined, os);
669 
670   for (auto &p : sortBindings(bindingsMap)) {
671     const Symbol *sym = p.first;
672     std::vector<BindingEntry> &bindings = p.second;
673     os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
674        << sym->getName() << '\0'
675        << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
676     std::vector<BindIR> opcodes;
677     for (const BindingEntry &b : bindings)
678       encodeBinding(b.target.isec->parent,
679                     b.target.isec->getOffset(b.target.offset), b.addend,
680                     lastBinding, opcodes);
681     if (config->optimize > 1)
682       optimizeOpcodes(opcodes);
683     for (const auto &op : opcodes)
684       flushOpcodes(op, os);
685   }
686   if (!bindingsMap.empty() || !definitions.empty())
687     os << static_cast<uint8_t>(BIND_OPCODE_DONE);
688 }
689 
690 void WeakBindingSection::writeTo(uint8_t *buf) const {
691   memcpy(buf, contents.data(), contents.size());
692 }
693 
694 StubsSection::StubsSection()
695     : SyntheticSection(segment_names::text, section_names::stubs) {
696   flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
697   // The stubs section comprises machine instructions, which are aligned to
698   // 4 bytes on the archs we care about.
699   align = 4;
700   reserved2 = target->stubSize;
701 }
702 
703 uint64_t StubsSection::getSize() const {
704   return entries.size() * target->stubSize;
705 }
706 
707 void StubsSection::writeTo(uint8_t *buf) const {
708   size_t off = 0;
709   for (const Symbol *sym : entries) {
710     uint64_t pointerVA =
711         config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA();
712     target->writeStub(buf + off, *sym, pointerVA);
713     off += target->stubSize;
714   }
715 }
716 
717 void StubsSection::finalize() { isFinal = true; }
718 
719 static void addBindingsForStub(Symbol *sym) {
720   assert(!config->emitChainedFixups);
721   if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
722     if (sym->isWeakDef()) {
723       in.binding->addEntry(dysym, in.lazyPointers->isec,
724                            sym->stubsIndex * target->wordSize);
725       in.weakBinding->addEntry(sym, in.lazyPointers->isec,
726                                sym->stubsIndex * target->wordSize);
727     } else {
728       in.lazyBinding->addEntry(dysym);
729     }
730   } else if (auto *defined = dyn_cast<Defined>(sym)) {
731     if (defined->isExternalWeakDef()) {
732       in.rebase->addEntry(in.lazyPointers->isec,
733                           sym->stubsIndex * target->wordSize);
734       in.weakBinding->addEntry(sym, in.lazyPointers->isec,
735                                sym->stubsIndex * target->wordSize);
736     } else if (defined->interposable) {
737       in.lazyBinding->addEntry(sym);
738     } else {
739       llvm_unreachable("invalid stub target");
740     }
741   } else {
742     llvm_unreachable("invalid stub target symbol type");
743   }
744 }
745 
746 void StubsSection::addEntry(Symbol *sym) {
747   bool inserted = entries.insert(sym);
748   if (inserted) {
749     sym->stubsIndex = entries.size() - 1;
750 
751     if (config->emitChainedFixups)
752       in.got->addEntry(sym);
753     else
754       addBindingsForStub(sym);
755   }
756 }
757 
758 StubHelperSection::StubHelperSection()
759     : SyntheticSection(segment_names::text, section_names::stubHelper) {
760   flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
761   align = 4; // This section comprises machine instructions
762 }
763 
764 uint64_t StubHelperSection::getSize() const {
765   return target->stubHelperHeaderSize +
766          in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
767 }
768 
769 bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
770 
771 void StubHelperSection::writeTo(uint8_t *buf) const {
772   target->writeStubHelperHeader(buf);
773   size_t off = target->stubHelperHeaderSize;
774   for (const Symbol *sym : in.lazyBinding->getEntries()) {
775     target->writeStubHelperEntry(buf + off, *sym, addr + off);
776     off += target->stubHelperEntrySize;
777   }
778 }
779 
780 void StubHelperSection::setUp() {
781   Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr,
782                                         /*isWeakRef=*/false);
783   if (auto *undefined = dyn_cast<Undefined>(binder))
784     treatUndefinedSymbol(*undefined,
785                          "lazy binding (normally in libSystem.dylib)");
786 
787   // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
788   stubBinder = dyn_cast_or_null<DylibSymbol>(binder);
789   if (stubBinder == nullptr)
790     return;
791 
792   in.got->addEntry(stubBinder);
793 
794   in.imageLoaderCache->parent =
795       ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
796   inputSections.push_back(in.imageLoaderCache);
797   // Since this isn't in the symbol table or in any input file, the noDeadStrip
798   // argument doesn't matter.
799   dyldPrivate =
800       make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0,
801                     /*isWeakDef=*/false,
802                     /*isExternal=*/false, /*isPrivateExtern=*/false,
803                     /*includeInSymtab=*/true,
804                     /*isReferencedDynamically=*/false,
805                     /*noDeadStrip=*/false);
806   dyldPrivate->used = true;
807 }
808 
809 ObjCStubsSection::ObjCStubsSection()
810     : SyntheticSection(segment_names::text, section_names::objcStubs) {
811   flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
812   align = config->objcStubsMode == ObjCStubsMode::fast
813               ? target->objcStubsFastAlignment
814               : target->objcStubsSmallAlignment;
815 }
816 
817 void ObjCStubsSection::addEntry(Symbol *sym) {
818   assert(sym->getName().starts_with(symbolPrefix) && "not an objc stub");
819   StringRef methname = sym->getName().drop_front(symbolPrefix.size());
820   offsets.push_back(
821       in.objcMethnameSection->getStringOffset(methname).outSecOff);
822 
823   auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
824                       ? target->objcStubsFastSize
825                       : target->objcStubsSmallSize;
826   Defined *newSym = replaceSymbol<Defined>(
827       sym, sym->getName(), nullptr, isec,
828       /*value=*/symbols.size() * stubSize,
829       /*size=*/stubSize,
830       /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true,
831       /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
832       /*noDeadStrip=*/false);
833   symbols.push_back(newSym);
834 }
835 
836 void ObjCStubsSection::setUp() {
837   objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr,
838                                      /*isWeakRef=*/false);
839   if (auto *undefined = dyn_cast<Undefined>(objcMsgSend))
840     treatUndefinedSymbol(*undefined,
841                          "lazy binding (normally in libobjc.dylib)");
842   objcMsgSend->used = true;
843   if (config->objcStubsMode == ObjCStubsMode::fast) {
844     in.got->addEntry(objcMsgSend);
845     assert(objcMsgSend->isInGot());
846   } else {
847     assert(config->objcStubsMode == ObjCStubsMode::small);
848     // In line with ld64's behavior, when objc_msgSend is a direct symbol,
849     // we directly reference it.
850     // In other cases, typically when binding in libobjc.dylib,
851     // we generate a stub to invoke objc_msgSend.
852     if (!isa<Defined>(objcMsgSend))
853       in.stubs->addEntry(objcMsgSend);
854   }
855 
856   size_t size = offsets.size() * target->wordSize;
857   uint8_t *selrefsData = bAlloc().Allocate<uint8_t>(size);
858   for (size_t i = 0, n = offsets.size(); i < n; ++i)
859     write64le(&selrefsData[i * target->wordSize], offsets[i]);
860 
861   in.objcSelrefs =
862       makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs,
863                                 S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
864                                 ArrayRef<uint8_t>{selrefsData, size},
865                                 /*align=*/target->wordSize);
866   in.objcSelrefs->live = true;
867 
868   for (size_t i = 0, n = offsets.size(); i < n; ++i) {
869     in.objcSelrefs->relocs.push_back(
870         {/*type=*/target->unsignedRelocType,
871          /*pcrel=*/false, /*length=*/3,
872          /*offset=*/static_cast<uint32_t>(i * target->wordSize),
873          /*addend=*/offsets[i] * in.objcMethnameSection->align,
874          /*referent=*/in.objcMethnameSection->isec});
875   }
876 
877   in.objcSelrefs->parent =
878       ConcatOutputSection::getOrCreateForInput(in.objcSelrefs);
879   inputSections.push_back(in.objcSelrefs);
880   in.objcSelrefs->isFinal = true;
881 }
882 
883 uint64_t ObjCStubsSection::getSize() const {
884   auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
885                       ? target->objcStubsFastSize
886                       : target->objcStubsSmallSize;
887   return stubSize * symbols.size();
888 }
889 
890 void ObjCStubsSection::writeTo(uint8_t *buf) const {
891   assert(in.objcSelrefs->live);
892   assert(in.objcSelrefs->isFinal);
893 
894   uint64_t stubOffset = 0;
895   for (size_t i = 0, n = symbols.size(); i < n; ++i) {
896     Defined *sym = symbols[i];
897     target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,
898                                  stubOffset, in.objcSelrefs->getVA(), i,
899                                  objcMsgSend);
900   }
901 }
902 
903 LazyPointerSection::LazyPointerSection()
904     : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
905   align = target->wordSize;
906   flags = S_LAZY_SYMBOL_POINTERS;
907 }
908 
909 uint64_t LazyPointerSection::getSize() const {
910   return in.stubs->getEntries().size() * target->wordSize;
911 }
912 
913 bool LazyPointerSection::isNeeded() const {
914   return !in.stubs->getEntries().empty();
915 }
916 
917 void LazyPointerSection::writeTo(uint8_t *buf) const {
918   size_t off = 0;
919   for (const Symbol *sym : in.stubs->getEntries()) {
920     if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
921       if (dysym->hasStubsHelper()) {
922         uint64_t stubHelperOffset =
923             target->stubHelperHeaderSize +
924             dysym->stubsHelperIndex * target->stubHelperEntrySize;
925         write64le(buf + off, in.stubHelper->addr + stubHelperOffset);
926       }
927     } else {
928       write64le(buf + off, sym->getVA());
929     }
930     off += target->wordSize;
931   }
932 }
933 
934 LazyBindingSection::LazyBindingSection()
935     : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
936 
937 void LazyBindingSection::finalizeContents() {
938   // TODO: Just precompute output size here instead of writing to a temporary
939   // buffer
940   for (Symbol *sym : entries)
941     sym->lazyBindOffset = encode(*sym);
942 }
943 
944 void LazyBindingSection::writeTo(uint8_t *buf) const {
945   memcpy(buf, contents.data(), contents.size());
946 }
947 
948 void LazyBindingSection::addEntry(Symbol *sym) {
949   assert(!config->emitChainedFixups && "Chained fixups always bind eagerly");
950   if (entries.insert(sym)) {
951     sym->stubsHelperIndex = entries.size() - 1;
952     in.rebase->addEntry(in.lazyPointers->isec,
953                         sym->stubsIndex * target->wordSize);
954   }
955 }
956 
957 // Unlike the non-lazy binding section, the bind opcodes in this section aren't
958 // interpreted all at once. Rather, dyld will start interpreting opcodes at a
959 // given offset, typically only binding a single symbol before it finds a
960 // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
961 // we cannot encode just the differences between symbols; we have to emit the
962 // complete bind information for each symbol.
963 uint32_t LazyBindingSection::encode(const Symbol &sym) {
964   uint32_t opstreamOffset = contents.size();
965   OutputSegment *dataSeg = in.lazyPointers->parent;
966   os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
967                              dataSeg->index);
968   uint64_t offset =
969       in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize;
970   encodeULEB128(offset, os);
971   encodeDylibOrdinal(ordinalForSymbol(sym), os);
972 
973   uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
974   if (sym.isWeakRef())
975     flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
976 
977   os << flags << sym.getName() << '\0'
978      << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
979      << static_cast<uint8_t>(BIND_OPCODE_DONE);
980   return opstreamOffset;
981 }
982 
983 ExportSection::ExportSection()
984     : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
985 
986 void ExportSection::finalizeContents() {
987   trieBuilder.setImageBase(in.header->addr);
988   for (const Symbol *sym : symtab->getSymbols()) {
989     if (const auto *defined = dyn_cast<Defined>(sym)) {
990       if (defined->privateExtern || !defined->isLive())
991         continue;
992       trieBuilder.addSymbol(*defined);
993       hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
994     } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
995       if (dysym->shouldReexport)
996         trieBuilder.addSymbol(*dysym);
997     }
998   }
999   size = trieBuilder.build();
1000 }
1001 
1002 void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
1003 
1004 DataInCodeSection::DataInCodeSection()
1005     : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {}
1006 
1007 template <class LP>
1008 static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
1009   std::vector<MachO::data_in_code_entry> dataInCodeEntries;
1010   for (const InputFile *inputFile : inputFiles) {
1011     if (!isa<ObjFile>(inputFile))
1012       continue;
1013     const ObjFile *objFile = cast<ObjFile>(inputFile);
1014     ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode();
1015     if (entries.empty())
1016       continue;
1017 
1018     assert(is_sorted(entries, [](const data_in_code_entry &lhs,
1019                                  const data_in_code_entry &rhs) {
1020       return lhs.offset < rhs.offset;
1021     }));
1022     // For each code subsection find 'data in code' entries residing in it.
1023     // Compute the new offset values as
1024     // <offset within subsection> + <subsection address> - <__TEXT address>.
1025     for (const Section *section : objFile->sections) {
1026       for (const Subsection &subsec : section->subsections) {
1027         const InputSection *isec = subsec.isec;
1028         if (!isCodeSection(isec))
1029           continue;
1030         if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput())
1031           continue;
1032         const uint64_t beginAddr = section->addr + subsec.offset;
1033         auto it = llvm::lower_bound(
1034             entries, beginAddr,
1035             [](const MachO::data_in_code_entry &entry, uint64_t addr) {
1036               return entry.offset < addr;
1037             });
1038         const uint64_t endAddr = beginAddr + isec->getSize();
1039         for (const auto end = entries.end();
1040              it != end && it->offset + it->length <= endAddr; ++it)
1041           dataInCodeEntries.push_back(
1042               {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) -
1043                                      in.header->addr),
1044                it->length, it->kind});
1045       }
1046     }
1047   }
1048 
1049   // ld64 emits the table in sorted order too.
1050   llvm::sort(dataInCodeEntries,
1051              [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) {
1052                return lhs.offset < rhs.offset;
1053              });
1054   return dataInCodeEntries;
1055 }
1056 
1057 void DataInCodeSection::finalizeContents() {
1058   entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>()
1059                                   : collectDataInCodeEntries<ILP32>();
1060 }
1061 
1062 void DataInCodeSection::writeTo(uint8_t *buf) const {
1063   if (!entries.empty())
1064     memcpy(buf, entries.data(), getRawSize());
1065 }
1066 
1067 FunctionStartsSection::FunctionStartsSection()
1068     : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
1069 
1070 void FunctionStartsSection::finalizeContents() {
1071   raw_svector_ostream os{contents};
1072   std::vector<uint64_t> addrs;
1073   for (const InputFile *file : inputFiles) {
1074     if (auto *objFile = dyn_cast<ObjFile>(file)) {
1075       for (const Symbol *sym : objFile->symbols) {
1076         if (const auto *defined = dyn_cast_or_null<Defined>(sym)) {
1077           if (!defined->isec || !isCodeSection(defined->isec) ||
1078               !defined->isLive())
1079             continue;
1080           addrs.push_back(defined->getVA());
1081         }
1082       }
1083     }
1084   }
1085   llvm::sort(addrs);
1086   uint64_t addr = in.header->addr;
1087   for (uint64_t nextAddr : addrs) {
1088     uint64_t delta = nextAddr - addr;
1089     if (delta == 0)
1090       continue;
1091     encodeULEB128(delta, os);
1092     addr = nextAddr;
1093   }
1094   os << '\0';
1095 }
1096 
1097 void FunctionStartsSection::writeTo(uint8_t *buf) const {
1098   memcpy(buf, contents.data(), contents.size());
1099 }
1100 
1101 SymtabSection::SymtabSection(StringTableSection &stringTableSection)
1102     : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
1103       stringTableSection(stringTableSection) {}
1104 
1105 void SymtabSection::emitBeginSourceStab(StringRef sourceFile) {
1106   StabsEntry stab(N_SO);
1107   stab.strx = stringTableSection.addString(saver().save(sourceFile));
1108   stabs.emplace_back(std::move(stab));
1109 }
1110 
1111 void SymtabSection::emitEndSourceStab() {
1112   StabsEntry stab(N_SO);
1113   stab.sect = 1;
1114   stabs.emplace_back(std::move(stab));
1115 }
1116 
1117 void SymtabSection::emitObjectFileStab(ObjFile *file) {
1118   StabsEntry stab(N_OSO);
1119   stab.sect = target->cpuSubtype;
1120   SmallString<261> path(!file->archiveName.empty() ? file->archiveName
1121                                                    : file->getName());
1122   std::error_code ec = sys::fs::make_absolute(path);
1123   if (ec)
1124     fatal("failed to get absolute path for " + path);
1125 
1126   if (!file->archiveName.empty())
1127     path.append({"(", file->getName(), ")"});
1128 
1129   StringRef adjustedPath = saver().save(path.str());
1130   adjustedPath.consume_front(config->osoPrefix);
1131 
1132   stab.strx = stringTableSection.addString(adjustedPath);
1133   stab.desc = 1;
1134   stab.value = file->modTime;
1135   stabs.emplace_back(std::move(stab));
1136 }
1137 
1138 void SymtabSection::emitEndFunStab(Defined *defined) {
1139   StabsEntry stab(N_FUN);
1140   stab.value = defined->size;
1141   stabs.emplace_back(std::move(stab));
1142 }
1143 
1144 void SymtabSection::emitStabs() {
1145   if (config->omitDebugInfo)
1146     return;
1147 
1148   for (const std::string &s : config->astPaths) {
1149     StabsEntry astStab(N_AST);
1150     astStab.strx = stringTableSection.addString(s);
1151     stabs.emplace_back(std::move(astStab));
1152   }
1153 
1154   // Cache the file ID for each symbol in an std::pair for faster sorting.
1155   using SortingPair = std::pair<Defined *, int>;
1156   std::vector<SortingPair> symbolsNeedingStabs;
1157   for (const SymtabEntry &entry :
1158        concat<SymtabEntry>(localSymbols, externalSymbols)) {
1159     Symbol *sym = entry.sym;
1160     assert(sym->isLive() &&
1161            "dead symbols should not be in localSymbols, externalSymbols");
1162     if (auto *defined = dyn_cast<Defined>(sym)) {
1163       // Excluded symbols should have been filtered out in finalizeContents().
1164       assert(defined->includeInSymtab);
1165 
1166       if (defined->isAbsolute())
1167         continue;
1168 
1169       // Constant-folded symbols go in the executable's symbol table, but don't
1170       // get a stabs entry.
1171       if (defined->wasIdenticalCodeFolded)
1172         continue;
1173 
1174       ObjFile *file = defined->getObjectFile();
1175       if (!file || !file->compileUnit)
1176         continue;
1177 
1178       symbolsNeedingStabs.emplace_back(defined, defined->isec->getFile()->id);
1179     }
1180   }
1181 
1182   llvm::stable_sort(symbolsNeedingStabs,
1183                     [&](const SortingPair &a, const SortingPair &b) {
1184                       return a.second < b.second;
1185                     });
1186 
1187   // Emit STABS symbols so that dsymutil and/or the debugger can map address
1188   // regions in the final binary to the source and object files from which they
1189   // originated.
1190   InputFile *lastFile = nullptr;
1191   for (SortingPair &pair : symbolsNeedingStabs) {
1192     Defined *defined = pair.first;
1193     InputSection *isec = defined->isec;
1194     ObjFile *file = cast<ObjFile>(isec->getFile());
1195 
1196     if (lastFile == nullptr || lastFile != file) {
1197       if (lastFile != nullptr)
1198         emitEndSourceStab();
1199       lastFile = file;
1200 
1201       emitBeginSourceStab(file->sourceFile());
1202       emitObjectFileStab(file);
1203     }
1204 
1205     StabsEntry symStab;
1206     symStab.sect = defined->isec->parent->index;
1207     symStab.strx = stringTableSection.addString(defined->getName());
1208     symStab.value = defined->getVA();
1209 
1210     if (isCodeSection(isec)) {
1211       symStab.type = N_FUN;
1212       stabs.emplace_back(std::move(symStab));
1213       emitEndFunStab(defined);
1214     } else {
1215       symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
1216       stabs.emplace_back(std::move(symStab));
1217     }
1218   }
1219 
1220   if (!stabs.empty())
1221     emitEndSourceStab();
1222 }
1223 
1224 void SymtabSection::finalizeContents() {
1225   auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
1226     uint32_t strx = stringTableSection.addString(sym->getName());
1227     symbols.push_back({sym, strx});
1228   };
1229 
1230   std::function<void(Symbol *)> localSymbolsHandler;
1231   switch (config->localSymbolsPresence) {
1232   case SymtabPresence::All:
1233     localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); };
1234     break;
1235   case SymtabPresence::None:
1236     localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ };
1237     break;
1238   case SymtabPresence::SelectivelyIncluded:
1239     localSymbolsHandler = [&](Symbol *sym) {
1240       if (config->localSymbolPatterns.match(sym->getName()))
1241         addSymbol(localSymbols, sym);
1242     };
1243     break;
1244   case SymtabPresence::SelectivelyExcluded:
1245     localSymbolsHandler = [&](Symbol *sym) {
1246       if (!config->localSymbolPatterns.match(sym->getName()))
1247         addSymbol(localSymbols, sym);
1248     };
1249     break;
1250   }
1251 
1252   // Local symbols aren't in the SymbolTable, so we walk the list of object
1253   // files to gather them.
1254   // But if `-x` is set, then we don't need to. localSymbolsHandler() will do
1255   // the right thing regardless, but this check is a perf optimization because
1256   // iterating through all the input files and their symbols is expensive.
1257   if (config->localSymbolsPresence != SymtabPresence::None) {
1258     for (const InputFile *file : inputFiles) {
1259       if (auto *objFile = dyn_cast<ObjFile>(file)) {
1260         for (Symbol *sym : objFile->symbols) {
1261           if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
1262             if (defined->isExternal() || !defined->isLive() ||
1263                 !defined->includeInSymtab)
1264               continue;
1265             localSymbolsHandler(sym);
1266           }
1267         }
1268       }
1269     }
1270   }
1271 
1272   // __dyld_private is a local symbol too. It's linker-created and doesn't
1273   // exist in any object file.
1274   if (in.stubHelper && in.stubHelper->dyldPrivate)
1275     localSymbolsHandler(in.stubHelper->dyldPrivate);
1276 
1277   for (Symbol *sym : symtab->getSymbols()) {
1278     if (!sym->isLive())
1279       continue;
1280     if (auto *defined = dyn_cast<Defined>(sym)) {
1281       if (!defined->includeInSymtab)
1282         continue;
1283       assert(defined->isExternal());
1284       if (defined->privateExtern)
1285         localSymbolsHandler(defined);
1286       else
1287         addSymbol(externalSymbols, defined);
1288     } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1289       if (dysym->isReferenced())
1290         addSymbol(undefinedSymbols, sym);
1291     }
1292   }
1293 
1294   emitStabs();
1295   uint32_t symtabIndex = stabs.size();
1296   for (const SymtabEntry &entry :
1297        concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) {
1298     entry.sym->symtabIndex = symtabIndex++;
1299   }
1300 }
1301 
1302 uint32_t SymtabSection::getNumSymbols() const {
1303   return stabs.size() + localSymbols.size() + externalSymbols.size() +
1304          undefinedSymbols.size();
1305 }
1306 
1307 // This serves to hide (type-erase) the template parameter from SymtabSection.
1308 template <class LP> class SymtabSectionImpl final : public SymtabSection {
1309 public:
1310   SymtabSectionImpl(StringTableSection &stringTableSection)
1311       : SymtabSection(stringTableSection) {}
1312   uint64_t getRawSize() const override;
1313   void writeTo(uint8_t *buf) const override;
1314 };
1315 
1316 template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
1317   return getNumSymbols() * sizeof(typename LP::nlist);
1318 }
1319 
1320 template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
1321   auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
1322   // Emit the stabs entries before the "real" symbols. We cannot emit them
1323   // after as that would render Symbol::symtabIndex inaccurate.
1324   for (const StabsEntry &entry : stabs) {
1325     nList->n_strx = entry.strx;
1326     nList->n_type = entry.type;
1327     nList->n_sect = entry.sect;
1328     nList->n_desc = entry.desc;
1329     nList->n_value = entry.value;
1330     ++nList;
1331   }
1332 
1333   for (const SymtabEntry &entry : concat<const SymtabEntry>(
1334            localSymbols, externalSymbols, undefinedSymbols)) {
1335     nList->n_strx = entry.strx;
1336     // TODO populate n_desc with more flags
1337     if (auto *defined = dyn_cast<Defined>(entry.sym)) {
1338       uint8_t scope = 0;
1339       if (defined->privateExtern) {
1340         // Private external -- dylib scoped symbol.
1341         // Promote to non-external at link time.
1342         scope = N_PEXT;
1343       } else if (defined->isExternal()) {
1344         // Normal global symbol.
1345         scope = N_EXT;
1346       } else {
1347         // TU-local symbol from localSymbols.
1348         scope = 0;
1349       }
1350 
1351       if (defined->isAbsolute()) {
1352         nList->n_type = scope | N_ABS;
1353         nList->n_sect = NO_SECT;
1354         nList->n_value = defined->value;
1355       } else {
1356         nList->n_type = scope | N_SECT;
1357         nList->n_sect = defined->isec->parent->index;
1358         // For the N_SECT symbol type, n_value is the address of the symbol
1359         nList->n_value = defined->getVA();
1360       }
1361       nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
1362       nList->n_desc |=
1363           defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
1364     } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
1365       uint16_t n_desc = nList->n_desc;
1366       int16_t ordinal = ordinalForDylibSymbol(*dysym);
1367       if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
1368         SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
1369       else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
1370         SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
1371       else {
1372         assert(ordinal > 0);
1373         SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
1374       }
1375 
1376       nList->n_type = N_EXT;
1377       n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
1378       n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
1379       nList->n_desc = n_desc;
1380     }
1381     ++nList;
1382   }
1383 }
1384 
1385 template <class LP>
1386 SymtabSection *
1387 macho::makeSymtabSection(StringTableSection &stringTableSection) {
1388   return make<SymtabSectionImpl<LP>>(stringTableSection);
1389 }
1390 
1391 IndirectSymtabSection::IndirectSymtabSection()
1392     : LinkEditSection(segment_names::linkEdit,
1393                       section_names::indirectSymbolTable) {}
1394 
1395 uint32_t IndirectSymtabSection::getNumSymbols() const {
1396   uint32_t size = in.got->getEntries().size() +
1397                   in.tlvPointers->getEntries().size() +
1398                   in.stubs->getEntries().size();
1399   if (!config->emitChainedFixups)
1400     size += in.stubs->getEntries().size();
1401   return size;
1402 }
1403 
1404 bool IndirectSymtabSection::isNeeded() const {
1405   return in.got->isNeeded() || in.tlvPointers->isNeeded() ||
1406          in.stubs->isNeeded();
1407 }
1408 
1409 void IndirectSymtabSection::finalizeContents() {
1410   uint32_t off = 0;
1411   in.got->reserved1 = off;
1412   off += in.got->getEntries().size();
1413   in.tlvPointers->reserved1 = off;
1414   off += in.tlvPointers->getEntries().size();
1415   in.stubs->reserved1 = off;
1416   if (in.lazyPointers) {
1417     off += in.stubs->getEntries().size();
1418     in.lazyPointers->reserved1 = off;
1419   }
1420 }
1421 
1422 static uint32_t indirectValue(const Symbol *sym) {
1423   if (sym->symtabIndex == UINT32_MAX)
1424     return INDIRECT_SYMBOL_LOCAL;
1425   if (auto *defined = dyn_cast<Defined>(sym))
1426     if (defined->privateExtern)
1427       return INDIRECT_SYMBOL_LOCAL;
1428   return sym->symtabIndex;
1429 }
1430 
1431 void IndirectSymtabSection::writeTo(uint8_t *buf) const {
1432   uint32_t off = 0;
1433   for (const Symbol *sym : in.got->getEntries()) {
1434     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1435     ++off;
1436   }
1437   for (const Symbol *sym : in.tlvPointers->getEntries()) {
1438     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1439     ++off;
1440   }
1441   for (const Symbol *sym : in.stubs->getEntries()) {
1442     write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1443     ++off;
1444   }
1445 
1446   if (in.lazyPointers) {
1447     // There is a 1:1 correspondence between stubs and LazyPointerSection
1448     // entries. But giving __stubs and __la_symbol_ptr the same reserved1
1449     // (the offset into the indirect symbol table) so that they both refer
1450     // to the same range of offsets confuses `strip`, so write the stubs
1451     // symbol table offsets a second time.
1452     for (const Symbol *sym : in.stubs->getEntries()) {
1453       write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1454       ++off;
1455     }
1456   }
1457 }
1458 
1459 StringTableSection::StringTableSection()
1460     : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
1461 
1462 uint32_t StringTableSection::addString(StringRef str) {
1463   uint32_t strx = size;
1464   strings.push_back(str); // TODO: consider deduplicating strings
1465   size += str.size() + 1; // account for null terminator
1466   return strx;
1467 }
1468 
1469 void StringTableSection::writeTo(uint8_t *buf) const {
1470   uint32_t off = 0;
1471   for (StringRef str : strings) {
1472     memcpy(buf + off, str.data(), str.size());
1473     off += str.size() + 1; // account for null terminator
1474   }
1475 }
1476 
1477 static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0);
1478 static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0);
1479 
1480 CodeSignatureSection::CodeSignatureSection()
1481     : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
1482   align = 16; // required by libstuff
1483 
1484   // XXX: This mimics LD64, where it uses the install-name as codesign
1485   // identifier, if available.
1486   if (!config->installName.empty())
1487     fileName = config->installName;
1488   else
1489     // FIXME: Consider using finalOutput instead of outputFile.
1490     fileName = config->outputFile;
1491 
1492   size_t slashIndex = fileName.rfind("/");
1493   if (slashIndex != std::string::npos)
1494     fileName = fileName.drop_front(slashIndex + 1);
1495 
1496   // NOTE: Any changes to these calculations should be repeated
1497   // in llvm-objcopy's MachOLayoutBuilder::layoutTail.
1498   allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1);
1499   fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size();
1500 }
1501 
1502 uint32_t CodeSignatureSection::getBlockCount() const {
1503   return (fileOff + blockSize - 1) / blockSize;
1504 }
1505 
1506 uint64_t CodeSignatureSection::getRawSize() const {
1507   return allHeadersSize + getBlockCount() * hashSize;
1508 }
1509 
1510 void CodeSignatureSection::writeHashes(uint8_t *buf) const {
1511   // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1512   // MachOWriter::writeSignatureData.
1513   uint8_t *hashes = buf + fileOff + allHeadersSize;
1514   parallelFor(0, getBlockCount(), [&](size_t i) {
1515     sha256(buf + i * blockSize,
1516            std::min(static_cast<size_t>(fileOff - i * blockSize), blockSize),
1517            hashes + i * hashSize);
1518   });
1519 #if defined(__APPLE__)
1520   // This is macOS-specific work-around and makes no sense for any
1521   // other host OS. See https://openradar.appspot.com/FB8914231
1522   //
1523   // The macOS kernel maintains a signature-verification cache to
1524   // quickly validate applications at time of execve(2).  The trouble
1525   // is that for the kernel creates the cache entry at the time of the
1526   // mmap(2) call, before we have a chance to write either the code to
1527   // sign or the signature header+hashes.  The fix is to invalidate
1528   // all cached data associated with the output file, thus discarding
1529   // the bogus prematurely-cached signature.
1530   msync(buf, fileOff + getSize(), MS_INVALIDATE);
1531 #endif
1532 }
1533 
1534 void CodeSignatureSection::writeTo(uint8_t *buf) const {
1535   // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1536   // MachOWriter::writeSignatureData.
1537   uint32_t signatureSize = static_cast<uint32_t>(getSize());
1538   auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
1539   write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE);
1540   write32be(&superBlob->length, signatureSize);
1541   write32be(&superBlob->count, 1);
1542   auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]);
1543   write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY);
1544   write32be(&blobIndex->offset, blobHeadersSize);
1545   auto *codeDirectory =
1546       reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize);
1547   write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY);
1548   write32be(&codeDirectory->length, signatureSize - blobHeadersSize);
1549   write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG);
1550   write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED);
1551   write32be(&codeDirectory->hashOffset,
1552             sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad);
1553   write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory));
1554   codeDirectory->nSpecialSlots = 0;
1555   write32be(&codeDirectory->nCodeSlots, getBlockCount());
1556   write32be(&codeDirectory->codeLimit, fileOff);
1557   codeDirectory->hashSize = static_cast<uint8_t>(hashSize);
1558   codeDirectory->hashType = kSecCodeSignatureHashSHA256;
1559   codeDirectory->platform = 0;
1560   codeDirectory->pageSize = blockSizeShift;
1561   codeDirectory->spare2 = 0;
1562   codeDirectory->scatterOffset = 0;
1563   codeDirectory->teamOffset = 0;
1564   codeDirectory->spare3 = 0;
1565   codeDirectory->codeLimit64 = 0;
1566   OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text);
1567   write64be(&codeDirectory->execSegBase, textSeg->fileOff);
1568   write64be(&codeDirectory->execSegLimit, textSeg->fileSize);
1569   write64be(&codeDirectory->execSegFlags,
1570             config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0);
1571   auto *id = reinterpret_cast<char *>(&codeDirectory[1]);
1572   memcpy(id, fileName.begin(), fileName.size());
1573   memset(id + fileName.size(), 0, fileNamePad);
1574 }
1575 
1576 CStringSection::CStringSection(const char *name)
1577     : SyntheticSection(segment_names::text, name) {
1578   flags = S_CSTRING_LITERALS;
1579 }
1580 
1581 void CStringSection::addInput(CStringInputSection *isec) {
1582   isec->parent = this;
1583   inputs.push_back(isec);
1584   if (isec->align > align)
1585     align = isec->align;
1586 }
1587 
1588 void CStringSection::writeTo(uint8_t *buf) const {
1589   for (const CStringInputSection *isec : inputs) {
1590     for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1591       if (!piece.live)
1592         continue;
1593       StringRef string = isec->getStringRef(i);
1594       memcpy(buf + piece.outSecOff, string.data(), string.size());
1595     }
1596   }
1597 }
1598 
1599 void CStringSection::finalizeContents() {
1600   uint64_t offset = 0;
1601   for (CStringInputSection *isec : inputs) {
1602     for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1603       if (!piece.live)
1604         continue;
1605       // See comment above DeduplicatedCStringSection for how alignment is
1606       // handled.
1607       uint32_t pieceAlign = 1
1608                             << llvm::countr_zero(isec->align | piece.inSecOff);
1609       offset = alignToPowerOf2(offset, pieceAlign);
1610       piece.outSecOff = offset;
1611       isec->isFinal = true;
1612       StringRef string = isec->getStringRef(i);
1613       offset += string.size() + 1; // account for null terminator
1614     }
1615   }
1616   size = offset;
1617 }
1618 
1619 // Mergeable cstring literals are found under the __TEXT,__cstring section. In
1620 // contrast to ELF, which puts strings that need different alignments into
1621 // different sections, clang's Mach-O backend puts them all in one section.
1622 // Strings that need to be aligned have the .p2align directive emitted before
1623 // them, which simply translates into zero padding in the object file. In other
1624 // words, we have to infer the desired alignment of these cstrings from their
1625 // addresses.
1626 //
1627 // We differ slightly from ld64 in how we've chosen to align these cstrings.
1628 // Both LLD and ld64 preserve the number of trailing zeros in each cstring's
1629 // address in the input object files. When deduplicating identical cstrings,
1630 // both linkers pick the cstring whose address has more trailing zeros, and
1631 // preserve the alignment of that address in the final binary. However, ld64
1632 // goes a step further and also preserves the offset of the cstring from the
1633 // last section-aligned address.  I.e. if a cstring is at offset 18 in the
1634 // input, with a section alignment of 16, then both LLD and ld64 will ensure the
1635 // final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also
1636 // ensure that the final address is of the form 16 * k + 2 for some k.
1637 //
1638 // Note that ld64's heuristic means that a dedup'ed cstring's final address is
1639 // dependent on the order of the input object files. E.g. if in addition to the
1640 // cstring at offset 18 above, we have a duplicate one in another file with a
1641 // `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick
1642 // the cstring from the object file earlier on the command line (since both have
1643 // the same number of trailing zeros in their address). So the final cstring may
1644 // either be at some address `16 * k + 2` or at some address `2 * k`.
1645 //
1646 // I've opted not to follow this behavior primarily for implementation
1647 // simplicity, and secondarily to save a few more bytes. It's not clear to me
1648 // that preserving the section alignment + offset is ever necessary, and there
1649 // are many cases that are clearly redundant. In particular, if an x86_64 object
1650 // file contains some strings that are accessed via SIMD instructions, then the
1651 // .cstring section in the object file will be 16-byte-aligned (since SIMD
1652 // requires its operand addresses to be 16-byte aligned). However, there will
1653 // typically also be other cstrings in the same file that aren't used via SIMD
1654 // and don't need this alignment. They will be emitted at some arbitrary address
1655 // `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16
1656 // % A`.
1657 void DeduplicatedCStringSection::finalizeContents() {
1658   // Find the largest alignment required for each string.
1659   for (const CStringInputSection *isec : inputs) {
1660     for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1661       if (!piece.live)
1662         continue;
1663       auto s = isec->getCachedHashStringRef(i);
1664       assert(isec->align != 0);
1665       uint8_t trailingZeros = llvm::countr_zero(isec->align | piece.inSecOff);
1666       auto it = stringOffsetMap.insert(
1667           std::make_pair(s, StringOffset(trailingZeros)));
1668       if (!it.second && it.first->second.trailingZeros < trailingZeros)
1669         it.first->second.trailingZeros = trailingZeros;
1670     }
1671   }
1672 
1673   // Assign an offset for each string and save it to the corresponding
1674   // StringPieces for easy access.
1675   for (CStringInputSection *isec : inputs) {
1676     for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1677       if (!piece.live)
1678         continue;
1679       auto s = isec->getCachedHashStringRef(i);
1680       auto it = stringOffsetMap.find(s);
1681       assert(it != stringOffsetMap.end());
1682       StringOffset &offsetInfo = it->second;
1683       if (offsetInfo.outSecOff == UINT64_MAX) {
1684         offsetInfo.outSecOff =
1685             alignToPowerOf2(size, 1ULL << offsetInfo.trailingZeros);
1686         size =
1687             offsetInfo.outSecOff + s.size() + 1; // account for null terminator
1688       }
1689       piece.outSecOff = offsetInfo.outSecOff;
1690     }
1691     isec->isFinal = true;
1692   }
1693 }
1694 
1695 void DeduplicatedCStringSection::writeTo(uint8_t *buf) const {
1696   for (const auto &p : stringOffsetMap) {
1697     StringRef data = p.first.val();
1698     uint64_t off = p.second.outSecOff;
1699     if (!data.empty())
1700       memcpy(buf + off, data.data(), data.size());
1701   }
1702 }
1703 
1704 DeduplicatedCStringSection::StringOffset
1705 DeduplicatedCStringSection::getStringOffset(StringRef str) const {
1706   // StringPiece uses 31 bits to store the hashes, so we replicate that
1707   uint32_t hash = xxh3_64bits(str) & 0x7fffffff;
1708   auto offset = stringOffsetMap.find(CachedHashStringRef(str, hash));
1709   assert(offset != stringOffsetMap.end() &&
1710          "Looked-up strings should always exist in section");
1711   return offset->second;
1712 }
1713 
1714 // This section is actually emitted as __TEXT,__const by ld64, but clang may
1715 // emit input sections of that name, and LLD doesn't currently support mixing
1716 // synthetic and concat-type OutputSections. To work around this, I've given
1717 // our merged-literals section a different name.
1718 WordLiteralSection::WordLiteralSection()
1719     : SyntheticSection(segment_names::text, section_names::literals) {
1720   align = 16;
1721 }
1722 
1723 void WordLiteralSection::addInput(WordLiteralInputSection *isec) {
1724   isec->parent = this;
1725   inputs.push_back(isec);
1726 }
1727 
1728 void WordLiteralSection::finalizeContents() {
1729   for (WordLiteralInputSection *isec : inputs) {
1730     // We do all processing of the InputSection here, so it will be effectively
1731     // finalized.
1732     isec->isFinal = true;
1733     const uint8_t *buf = isec->data.data();
1734     switch (sectionType(isec->getFlags())) {
1735     case S_4BYTE_LITERALS: {
1736       for (size_t off = 0, e = isec->data.size(); off < e; off += 4) {
1737         if (!isec->isLive(off))
1738           continue;
1739         uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off);
1740         literal4Map.emplace(value, literal4Map.size());
1741       }
1742       break;
1743     }
1744     case S_8BYTE_LITERALS: {
1745       for (size_t off = 0, e = isec->data.size(); off < e; off += 8) {
1746         if (!isec->isLive(off))
1747           continue;
1748         uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off);
1749         literal8Map.emplace(value, literal8Map.size());
1750       }
1751       break;
1752     }
1753     case S_16BYTE_LITERALS: {
1754       for (size_t off = 0, e = isec->data.size(); off < e; off += 16) {
1755         if (!isec->isLive(off))
1756           continue;
1757         UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off);
1758         literal16Map.emplace(value, literal16Map.size());
1759       }
1760       break;
1761     }
1762     default:
1763       llvm_unreachable("invalid literal section type");
1764     }
1765   }
1766 }
1767 
1768 void WordLiteralSection::writeTo(uint8_t *buf) const {
1769   // Note that we don't attempt to do any endianness conversion in addInput(),
1770   // so we don't do it here either -- just write out the original value,
1771   // byte-for-byte.
1772   for (const auto &p : literal16Map)
1773     memcpy(buf + p.second * 16, &p.first, 16);
1774   buf += literal16Map.size() * 16;
1775 
1776   for (const auto &p : literal8Map)
1777     memcpy(buf + p.second * 8, &p.first, 8);
1778   buf += literal8Map.size() * 8;
1779 
1780   for (const auto &p : literal4Map)
1781     memcpy(buf + p.second * 4, &p.first, 4);
1782 }
1783 
1784 ObjCImageInfoSection::ObjCImageInfoSection()
1785     : SyntheticSection(segment_names::data, section_names::objCImageInfo) {}
1786 
1787 ObjCImageInfoSection::ImageInfo
1788 ObjCImageInfoSection::parseImageInfo(const InputFile *file) {
1789   ImageInfo info;
1790   ArrayRef<uint8_t> data = file->objCImageInfo;
1791   // The image info struct has the following layout:
1792   // struct {
1793   //   uint32_t version;
1794   //   uint32_t flags;
1795   // };
1796   if (data.size() < 8) {
1797     warn(toString(file) + ": invalid __objc_imageinfo size");
1798     return info;
1799   }
1800 
1801   auto *buf = reinterpret_cast<const uint32_t *>(data.data());
1802   if (read32le(buf) != 0) {
1803     warn(toString(file) + ": invalid __objc_imageinfo version");
1804     return info;
1805   }
1806 
1807   uint32_t flags = read32le(buf + 1);
1808   info.swiftVersion = (flags >> 8) & 0xff;
1809   info.hasCategoryClassProperties = flags & 0x40;
1810   return info;
1811 }
1812 
1813 static std::string swiftVersionString(uint8_t version) {
1814   switch (version) {
1815     case 1:
1816       return "1.0";
1817     case 2:
1818       return "1.1";
1819     case 3:
1820       return "2.0";
1821     case 4:
1822       return "3.0";
1823     case 5:
1824       return "4.0";
1825     default:
1826       return ("0x" + Twine::utohexstr(version)).str();
1827   }
1828 }
1829 
1830 // Validate each object file's __objc_imageinfo and use them to generate the
1831 // image info for the output binary. Only two pieces of info are relevant:
1832 // 1. The Swift version (should be identical across inputs)
1833 // 2. `bool hasCategoryClassProperties` (true only if true for all inputs)
1834 void ObjCImageInfoSection::finalizeContents() {
1835   assert(files.size() != 0); // should have already been checked via isNeeded()
1836 
1837   info.hasCategoryClassProperties = true;
1838   const InputFile *firstFile;
1839   for (const InputFile *file : files) {
1840     ImageInfo inputInfo = parseImageInfo(file);
1841     info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties;
1842 
1843     // swiftVersion 0 means no Swift is present, so no version checking required
1844     if (inputInfo.swiftVersion == 0)
1845       continue;
1846 
1847     if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) {
1848       error("Swift version mismatch: " + toString(firstFile) + " has version " +
1849             swiftVersionString(info.swiftVersion) + " but " + toString(file) +
1850             " has version " + swiftVersionString(inputInfo.swiftVersion));
1851     } else {
1852       info.swiftVersion = inputInfo.swiftVersion;
1853       firstFile = file;
1854     }
1855   }
1856 }
1857 
1858 void ObjCImageInfoSection::writeTo(uint8_t *buf) const {
1859   uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0;
1860   flags |= info.swiftVersion << 8;
1861   write32le(buf + 4, flags);
1862 }
1863 
1864 InitOffsetsSection::InitOffsetsSection()
1865     : SyntheticSection(segment_names::text, section_names::initOffsets) {
1866   flags = S_INIT_FUNC_OFFSETS;
1867   align = 4; // This section contains 32-bit integers.
1868 }
1869 
1870 uint64_t InitOffsetsSection::getSize() const {
1871   size_t count = 0;
1872   for (const ConcatInputSection *isec : sections)
1873     count += isec->relocs.size();
1874   return count * sizeof(uint32_t);
1875 }
1876 
1877 void InitOffsetsSection::writeTo(uint8_t *buf) const {
1878   // FIXME: Add function specified by -init when that argument is implemented.
1879   for (ConcatInputSection *isec : sections) {
1880     for (const Reloc &rel : isec->relocs) {
1881       const Symbol *referent = rel.referent.dyn_cast<Symbol *>();
1882       assert(referent && "section relocation should have been rejected");
1883       uint64_t offset = referent->getVA() - in.header->addr;
1884       // FIXME: Can we handle this gracefully?
1885       if (offset > UINT32_MAX)
1886         fatal(isec->getLocation(rel.offset) + ": offset to initializer " +
1887               referent->getName() + " (" + utohexstr(offset) +
1888               ") does not fit in 32 bits");
1889 
1890       // Entries need to be added in the order they appear in the section, but
1891       // relocations aren't guaranteed to be sorted.
1892       size_t index = rel.offset >> target->p2WordSize;
1893       write32le(&buf[index * sizeof(uint32_t)], offset);
1894     }
1895     buf += isec->relocs.size() * sizeof(uint32_t);
1896   }
1897 }
1898 
1899 // The inputs are __mod_init_func sections, which contain pointers to
1900 // initializer functions, therefore all relocations should be of the UNSIGNED
1901 // type. InitOffsetsSection stores offsets, so if the initializer's address is
1902 // not known at link time, stub-indirection has to be used.
1903 void InitOffsetsSection::setUp() {
1904   for (const ConcatInputSection *isec : sections) {
1905     for (const Reloc &rel : isec->relocs) {
1906       RelocAttrs attrs = target->getRelocAttrs(rel.type);
1907       if (!attrs.hasAttr(RelocAttrBits::UNSIGNED))
1908         error(isec->getLocation(rel.offset) +
1909               ": unsupported relocation type: " + attrs.name);
1910       if (rel.addend != 0)
1911         error(isec->getLocation(rel.offset) +
1912               ": relocation addend is not representable in __init_offsets");
1913       if (rel.referent.is<InputSection *>())
1914         error(isec->getLocation(rel.offset) +
1915               ": unexpected section relocation");
1916 
1917       Symbol *sym = rel.referent.dyn_cast<Symbol *>();
1918       if (auto *undefined = dyn_cast<Undefined>(sym))
1919         treatUndefinedSymbol(*undefined, isec, rel.offset);
1920       if (needsBinding(sym))
1921         in.stubs->addEntry(sym);
1922     }
1923   }
1924 }
1925 
1926 void macho::createSyntheticSymbols() {
1927   auto addHeaderSymbol = [](const char *name) {
1928     symtab->addSynthetic(name, in.header->isec, /*value=*/0,
1929                          /*isPrivateExtern=*/true, /*includeInSymtab=*/false,
1930                          /*referencedDynamically=*/false);
1931   };
1932 
1933   switch (config->outputType) {
1934     // FIXME: Assign the right address value for these symbols
1935     // (rather than 0). But we need to do that after assignAddresses().
1936   case MH_EXECUTE:
1937     // If linking PIE, __mh_execute_header is a defined symbol in
1938     //  __TEXT, __text)
1939     // Otherwise, it's an absolute symbol.
1940     if (config->isPic)
1941       symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0,
1942                            /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
1943                            /*referencedDynamically=*/true);
1944     else
1945       symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
1946                            /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
1947                            /*referencedDynamically=*/true);
1948     break;
1949 
1950     // The following symbols are N_SECT symbols, even though the header is not
1951     // part of any section and that they are private to the bundle/dylib/object
1952     // they are part of.
1953   case MH_BUNDLE:
1954     addHeaderSymbol("__mh_bundle_header");
1955     break;
1956   case MH_DYLIB:
1957     addHeaderSymbol("__mh_dylib_header");
1958     break;
1959   case MH_DYLINKER:
1960     addHeaderSymbol("__mh_dylinker_header");
1961     break;
1962   case MH_OBJECT:
1963     addHeaderSymbol("__mh_object_header");
1964     break;
1965   default:
1966     llvm_unreachable("unexpected outputType");
1967     break;
1968   }
1969 
1970   // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
1971   // which does e.g. cleanup of static global variables. The ABI document
1972   // says that the pointer can point to any address in one of the dylib's
1973   // segments, but in practice ld64 seems to set it to point to the header,
1974   // so that's what's implemented here.
1975   addHeaderSymbol("___dso_handle");
1976 }
1977 
1978 ChainedFixupsSection::ChainedFixupsSection()
1979     : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {}
1980 
1981 bool ChainedFixupsSection::isNeeded() const {
1982   assert(config->emitChainedFixups);
1983   // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid
1984   // dyld_chained_fixups_header, so we create this section even if there aren't
1985   // any fixups.
1986   return true;
1987 }
1988 
1989 static bool needsWeakBind(const Symbol &sym) {
1990   if (auto *dysym = dyn_cast<DylibSymbol>(&sym))
1991     return dysym->isWeakDef();
1992   if (auto *defined = dyn_cast<Defined>(&sym))
1993     return defined->isExternalWeakDef();
1994   return false;
1995 }
1996 
1997 void ChainedFixupsSection::addBinding(const Symbol *sym,
1998                                       const InputSection *isec, uint64_t offset,
1999                                       int64_t addend) {
2000   locations.emplace_back(isec, offset);
2001   int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2002   auto [it, inserted] = bindings.insert(
2003       {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())});
2004 
2005   if (inserted) {
2006     symtabSize += sym->getName().size() + 1;
2007     hasWeakBind = hasWeakBind || needsWeakBind(*sym);
2008     if (!isInt<23>(outlineAddend))
2009       needsLargeAddend = true;
2010     else if (outlineAddend != 0)
2011       needsAddend = true;
2012   }
2013 }
2014 
2015 std::pair<uint32_t, uint8_t>
2016 ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend) const {
2017   int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2018   auto it = bindings.find({sym, outlineAddend});
2019   assert(it != bindings.end() && "binding not found in the imports table");
2020   if (outlineAddend == 0)
2021     return {it->second, addend};
2022   return {it->second, 0};
2023 }
2024 
2025 static size_t writeImport(uint8_t *buf, int format, uint32_t libOrdinal,
2026                           bool weakRef, uint32_t nameOffset, int64_t addend) {
2027   switch (format) {
2028   case DYLD_CHAINED_IMPORT: {
2029     auto *import = reinterpret_cast<dyld_chained_import *>(buf);
2030     import->lib_ordinal = libOrdinal;
2031     import->weak_import = weakRef;
2032     import->name_offset = nameOffset;
2033     return sizeof(dyld_chained_import);
2034   }
2035   case DYLD_CHAINED_IMPORT_ADDEND: {
2036     auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf);
2037     import->lib_ordinal = libOrdinal;
2038     import->weak_import = weakRef;
2039     import->name_offset = nameOffset;
2040     import->addend = addend;
2041     return sizeof(dyld_chained_import_addend);
2042   }
2043   case DYLD_CHAINED_IMPORT_ADDEND64: {
2044     auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf);
2045     import->lib_ordinal = libOrdinal;
2046     import->weak_import = weakRef;
2047     import->name_offset = nameOffset;
2048     import->addend = addend;
2049     return sizeof(dyld_chained_import_addend64);
2050   }
2051   default:
2052     llvm_unreachable("Unknown import format");
2053   }
2054 }
2055 
2056 size_t ChainedFixupsSection::SegmentInfo::getSize() const {
2057   assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?");
2058   return alignTo<8>(sizeof(dyld_chained_starts_in_segment) +
2059                     pageStarts.back().first * sizeof(uint16_t));
2060 }
2061 
2062 size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const {
2063   auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf);
2064   segInfo->size = getSize();
2065   segInfo->page_size = target->getPageSize();
2066   // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions.
2067   segInfo->pointer_format = DYLD_CHAINED_PTR_64;
2068   segInfo->segment_offset = oseg->addr - in.header->addr;
2069   segInfo->max_valid_pointer = 0; // not used on 64-bit
2070   segInfo->page_count = pageStarts.back().first + 1;
2071 
2072   uint16_t *starts = segInfo->page_start;
2073   for (size_t i = 0; i < segInfo->page_count; ++i)
2074     starts[i] = DYLD_CHAINED_PTR_START_NONE;
2075 
2076   for (auto [pageIdx, startAddr] : pageStarts)
2077     starts[pageIdx] = startAddr;
2078   return segInfo->size;
2079 }
2080 
2081 static size_t importEntrySize(int format) {
2082   switch (format) {
2083   case DYLD_CHAINED_IMPORT:
2084     return sizeof(dyld_chained_import);
2085   case DYLD_CHAINED_IMPORT_ADDEND:
2086     return sizeof(dyld_chained_import_addend);
2087   case DYLD_CHAINED_IMPORT_ADDEND64:
2088     return sizeof(dyld_chained_import_addend64);
2089   default:
2090     llvm_unreachable("Unknown import format");
2091   }
2092 }
2093 
2094 // This is step 3 of the algorithm described in the class comment of
2095 // ChainedFixupsSection.
2096 //
2097 // LC_DYLD_CHAINED_FIXUPS data consists of (in this order):
2098 // * A dyld_chained_fixups_header
2099 // * A dyld_chained_starts_in_image
2100 // * One dyld_chained_starts_in_segment per segment
2101 // * List of all imports (dyld_chained_import, dyld_chained_import_addend, or
2102 //   dyld_chained_import_addend64)
2103 // * Names of imported symbols
2104 void ChainedFixupsSection::writeTo(uint8_t *buf) const {
2105   auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf);
2106   header->fixups_version = 0;
2107   header->imports_count = bindings.size();
2108   header->imports_format = importFormat;
2109   header->symbols_format = 0;
2110 
2111   buf += alignTo<8>(sizeof(*header));
2112 
2113   auto curOffset = [&buf, &header]() -> uint32_t {
2114     return buf - reinterpret_cast<uint8_t *>(header);
2115   };
2116 
2117   header->starts_offset = curOffset();
2118 
2119   auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf);
2120   imageInfo->seg_count = outputSegments.size();
2121   uint32_t *segStarts = imageInfo->seg_info_offset;
2122 
2123   // dyld_chained_starts_in_image ends in a flexible array member containing an
2124   // uint32_t for each segment. Leave room for it, and fill it via segStarts.
2125   buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2126                     outputSegments.size() * sizeof(uint32_t));
2127 
2128   // Initialize all offsets to 0, which indicates that the segment does not have
2129   // fixups. Those that do have them will be filled in below.
2130   for (size_t i = 0; i < outputSegments.size(); ++i)
2131     segStarts[i] = 0;
2132 
2133   for (const SegmentInfo &seg : fixupSegments) {
2134     segStarts[seg.oseg->index] = curOffset() - header->starts_offset;
2135     buf += seg.writeTo(buf);
2136   }
2137 
2138   // Write imports table.
2139   header->imports_offset = curOffset();
2140   uint64_t nameOffset = 0;
2141   for (auto [import, idx] : bindings) {
2142     const Symbol &sym = *import.first;
2143     int16_t libOrdinal = needsWeakBind(sym)
2144                              ? (int64_t)BIND_SPECIAL_DYLIB_WEAK_LOOKUP
2145                              : ordinalForSymbol(sym);
2146     buf += writeImport(buf, importFormat, libOrdinal, sym.isWeakRef(),
2147                        nameOffset, import.second);
2148     nameOffset += sym.getName().size() + 1;
2149   }
2150 
2151   // Write imported symbol names.
2152   header->symbols_offset = curOffset();
2153   for (auto [import, idx] : bindings) {
2154     StringRef name = import.first->getName();
2155     memcpy(buf, name.data(), name.size());
2156     buf += name.size() + 1; // account for null terminator
2157   }
2158 
2159   assert(curOffset() == getRawSize());
2160 }
2161 
2162 // This is step 2 of the algorithm described in the class comment of
2163 // ChainedFixupsSection.
2164 void ChainedFixupsSection::finalizeContents() {
2165   assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
2166   assert(config->emitChainedFixups);
2167 
2168   if (!isUInt<32>(symtabSize))
2169     error("cannot encode chained fixups: imported symbols table size " +
2170           Twine(symtabSize) + " exceeds 4 GiB");
2171 
2172   if (needsLargeAddend || !isUInt<23>(symtabSize))
2173     importFormat = DYLD_CHAINED_IMPORT_ADDEND64;
2174   else if (needsAddend)
2175     importFormat = DYLD_CHAINED_IMPORT_ADDEND;
2176   else
2177     importFormat = DYLD_CHAINED_IMPORT;
2178 
2179   for (Location &loc : locations)
2180     loc.offset =
2181         loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
2182 
2183   llvm::sort(locations, [](const Location &a, const Location &b) {
2184     const OutputSegment *segA = a.isec->parent->parent;
2185     const OutputSegment *segB = b.isec->parent->parent;
2186     if (segA == segB)
2187       return a.offset < b.offset;
2188     return segA->addr < segB->addr;
2189   });
2190 
2191   auto sameSegment = [](const Location &a, const Location &b) {
2192     return a.isec->parent->parent == b.isec->parent->parent;
2193   };
2194 
2195   const uint64_t pageSize = target->getPageSize();
2196   for (size_t i = 0, count = locations.size(); i < count;) {
2197     const Location &firstLoc = locations[i];
2198     fixupSegments.emplace_back(firstLoc.isec->parent->parent);
2199     while (i < count && sameSegment(locations[i], firstLoc)) {
2200       uint32_t pageIdx = locations[i].offset / pageSize;
2201       fixupSegments.back().pageStarts.emplace_back(
2202           pageIdx, locations[i].offset % pageSize);
2203       ++i;
2204       while (i < count && sameSegment(locations[i], firstLoc) &&
2205              locations[i].offset / pageSize == pageIdx)
2206         ++i;
2207     }
2208   }
2209 
2210   // Compute expected encoded size.
2211   size = alignTo<8>(sizeof(dyld_chained_fixups_header));
2212   size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2213                      outputSegments.size() * sizeof(uint32_t));
2214   for (const SegmentInfo &seg : fixupSegments)
2215     size += seg.getSize();
2216   size += importEntrySize(importFormat) * bindings.size();
2217   size += symtabSize;
2218 }
2219 
2220 template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
2221 template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);
2222