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