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