xref: /llvm-project-15.0.7/lld/MachO/Writer.cpp (revision bbae0665)
1 //===- Writer.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 "Writer.h"
10 #include "Config.h"
11 #include "InputFiles.h"
12 #include "InputSection.h"
13 #include "MapFile.h"
14 #include "MergedOutputSection.h"
15 #include "OutputSection.h"
16 #include "OutputSegment.h"
17 #include "SymbolTable.h"
18 #include "Symbols.h"
19 #include "SyntheticSections.h"
20 #include "Target.h"
21 #include "UnwindInfoSection.h"
22 
23 #include "lld/Common/ErrorHandler.h"
24 #include "lld/Common/Memory.h"
25 #include "llvm/BinaryFormat/MachO.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/Support/LEB128.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/TimeProfiler.h"
31 #include "llvm/Support/xxhash.h"
32 
33 #include <algorithm>
34 
35 using namespace llvm;
36 using namespace llvm::MachO;
37 using namespace llvm::sys;
38 using namespace lld;
39 using namespace lld::macho;
40 
41 namespace {
42 class LCUuid;
43 
44 class Writer {
45 public:
46   Writer() : buffer(errorHandler().outputBuffer) {}
47 
48   void scanRelocations();
49   void scanSymbols();
50   void createOutputSections();
51   void createLoadCommands();
52   void finalizeAddressses();
53   void finalizeLinkEditSegment();
54   void assignAddresses(OutputSegment *);
55 
56   void openFile();
57   void writeSections();
58   void writeUuid();
59   void writeCodeSignature();
60   void writeOutputFile();
61 
62   void run();
63 
64   std::unique_ptr<FileOutputBuffer> &buffer;
65   uint64_t addr = 0;
66   uint64_t fileOff = 0;
67   MachHeaderSection *header = nullptr;
68   StringTableSection *stringTableSection = nullptr;
69   SymtabSection *symtabSection = nullptr;
70   IndirectSymtabSection *indirectSymtabSection = nullptr;
71   CodeSignatureSection *codeSignatureSection = nullptr;
72   UnwindInfoSection *unwindInfoSection = nullptr;
73   FunctionStartsSection *functionStartsSection = nullptr;
74 
75   LCUuid *uuidCommand = nullptr;
76   OutputSegment *linkEditSegment = nullptr;
77 };
78 
79 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information.
80 class LCDyldInfo : public LoadCommand {
81 public:
82   LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection,
83              WeakBindingSection *weakBindingSection,
84              LazyBindingSection *lazyBindingSection,
85              ExportSection *exportSection)
86       : rebaseSection(rebaseSection), bindingSection(bindingSection),
87         weakBindingSection(weakBindingSection),
88         lazyBindingSection(lazyBindingSection), exportSection(exportSection) {}
89 
90   uint32_t getSize() const override { return sizeof(dyld_info_command); }
91 
92   void writeTo(uint8_t *buf) const override {
93     auto *c = reinterpret_cast<dyld_info_command *>(buf);
94     c->cmd = LC_DYLD_INFO_ONLY;
95     c->cmdsize = getSize();
96     if (rebaseSection->isNeeded()) {
97       c->rebase_off = rebaseSection->fileOff;
98       c->rebase_size = rebaseSection->getFileSize();
99     }
100     if (bindingSection->isNeeded()) {
101       c->bind_off = bindingSection->fileOff;
102       c->bind_size = bindingSection->getFileSize();
103     }
104     if (weakBindingSection->isNeeded()) {
105       c->weak_bind_off = weakBindingSection->fileOff;
106       c->weak_bind_size = weakBindingSection->getFileSize();
107     }
108     if (lazyBindingSection->isNeeded()) {
109       c->lazy_bind_off = lazyBindingSection->fileOff;
110       c->lazy_bind_size = lazyBindingSection->getFileSize();
111     }
112     if (exportSection->isNeeded()) {
113       c->export_off = exportSection->fileOff;
114       c->export_size = exportSection->getFileSize();
115     }
116   }
117 
118   RebaseSection *rebaseSection;
119   BindingSection *bindingSection;
120   WeakBindingSection *weakBindingSection;
121   LazyBindingSection *lazyBindingSection;
122   ExportSection *exportSection;
123 };
124 
125 class LCFunctionStarts : public LoadCommand {
126 public:
127   explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection)
128       : functionStartsSection(functionStartsSection) {}
129 
130   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
131 
132   void writeTo(uint8_t *buf) const override {
133     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
134     c->cmd = LC_FUNCTION_STARTS;
135     c->cmdsize = getSize();
136     c->dataoff = functionStartsSection->fileOff;
137     c->datasize = functionStartsSection->getFileSize();
138   }
139 
140 private:
141   FunctionStartsSection *functionStartsSection;
142 };
143 
144 class LCDysymtab : public LoadCommand {
145 public:
146   LCDysymtab(SymtabSection *symtabSection,
147              IndirectSymtabSection *indirectSymtabSection)
148       : symtabSection(symtabSection),
149         indirectSymtabSection(indirectSymtabSection) {}
150 
151   uint32_t getSize() const override { return sizeof(dysymtab_command); }
152 
153   void writeTo(uint8_t *buf) const override {
154     auto *c = reinterpret_cast<dysymtab_command *>(buf);
155     c->cmd = LC_DYSYMTAB;
156     c->cmdsize = getSize();
157 
158     c->ilocalsym = 0;
159     c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols();
160     c->nextdefsym = symtabSection->getNumExternalSymbols();
161     c->iundefsym = c->iextdefsym + c->nextdefsym;
162     c->nundefsym = symtabSection->getNumUndefinedSymbols();
163 
164     c->indirectsymoff = indirectSymtabSection->fileOff;
165     c->nindirectsyms = indirectSymtabSection->getNumSymbols();
166   }
167 
168   SymtabSection *symtabSection;
169   IndirectSymtabSection *indirectSymtabSection;
170 };
171 
172 class LCSegment : public LoadCommand {
173 public:
174   LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {}
175 
176   uint32_t getSize() const override {
177     return sizeof(segment_command_64) +
178            seg->numNonHiddenSections() * sizeof(section_64);
179   }
180 
181   void writeTo(uint8_t *buf) const override {
182     auto *c = reinterpret_cast<segment_command_64 *>(buf);
183     buf += sizeof(segment_command_64);
184 
185     c->cmd = LC_SEGMENT_64;
186     c->cmdsize = getSize();
187     memcpy(c->segname, name.data(), name.size());
188     c->fileoff = seg->fileOff;
189     c->maxprot = seg->maxProt;
190     c->initprot = seg->initProt;
191 
192     if (seg->getSections().empty())
193       return;
194 
195     c->vmaddr = seg->firstSection()->addr;
196     c->vmsize =
197         seg->lastSection()->addr + seg->lastSection()->getSize() - c->vmaddr;
198     c->nsects = seg->numNonHiddenSections();
199 
200     for (const OutputSection *osec : seg->getSections()) {
201       if (!isZeroFill(osec->flags)) {
202         assert(osec->fileOff >= seg->fileOff);
203         c->filesize = std::max(
204             c->filesize, osec->fileOff + osec->getFileSize() - seg->fileOff);
205       }
206 
207       if (osec->isHidden())
208         continue;
209 
210       auto *sectHdr = reinterpret_cast<section_64 *>(buf);
211       buf += sizeof(section_64);
212 
213       memcpy(sectHdr->sectname, osec->name.data(), osec->name.size());
214       memcpy(sectHdr->segname, name.data(), name.size());
215 
216       sectHdr->addr = osec->addr;
217       sectHdr->offset = osec->fileOff;
218       sectHdr->align = Log2_32(osec->align);
219       sectHdr->flags = osec->flags;
220       sectHdr->size = osec->getSize();
221       sectHdr->reserved1 = osec->reserved1;
222       sectHdr->reserved2 = osec->reserved2;
223     }
224   }
225 
226 private:
227   StringRef name;
228   OutputSegment *seg;
229 };
230 
231 class LCMain : public LoadCommand {
232   uint32_t getSize() const override { return sizeof(entry_point_command); }
233 
234   void writeTo(uint8_t *buf) const override {
235     auto *c = reinterpret_cast<entry_point_command *>(buf);
236     c->cmd = LC_MAIN;
237     c->cmdsize = getSize();
238 
239     if (config->entry->isInStubs())
240       c->entryoff =
241           in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
242     else
243       c->entryoff = config->entry->getFileOffset();
244 
245     c->stacksize = 0;
246   }
247 };
248 
249 class LCSymtab : public LoadCommand {
250 public:
251   LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection)
252       : symtabSection(symtabSection), stringTableSection(stringTableSection) {}
253 
254   uint32_t getSize() const override { return sizeof(symtab_command); }
255 
256   void writeTo(uint8_t *buf) const override {
257     auto *c = reinterpret_cast<symtab_command *>(buf);
258     c->cmd = LC_SYMTAB;
259     c->cmdsize = getSize();
260     c->symoff = symtabSection->fileOff;
261     c->nsyms = symtabSection->getNumSymbols();
262     c->stroff = stringTableSection->fileOff;
263     c->strsize = stringTableSection->getFileSize();
264   }
265 
266   SymtabSection *symtabSection = nullptr;
267   StringTableSection *stringTableSection = nullptr;
268 };
269 
270 // There are several dylib load commands that share the same structure:
271 //   * LC_LOAD_DYLIB
272 //   * LC_ID_DYLIB
273 //   * LC_REEXPORT_DYLIB
274 class LCDylib : public LoadCommand {
275 public:
276   LCDylib(LoadCommandType type, StringRef path,
277           uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0)
278       : type(type), path(path), compatibilityVersion(compatibilityVersion),
279         currentVersion(currentVersion) {
280     instanceCount++;
281   }
282 
283   uint32_t getSize() const override {
284     return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
285   }
286 
287   void writeTo(uint8_t *buf) const override {
288     auto *c = reinterpret_cast<dylib_command *>(buf);
289     buf += sizeof(dylib_command);
290 
291     c->cmd = type;
292     c->cmdsize = getSize();
293     c->dylib.name = sizeof(dylib_command);
294     c->dylib.timestamp = 0;
295     c->dylib.compatibility_version = compatibilityVersion;
296     c->dylib.current_version = currentVersion;
297 
298     memcpy(buf, path.data(), path.size());
299     buf[path.size()] = '\0';
300   }
301 
302   static uint32_t getInstanceCount() { return instanceCount; }
303 
304 private:
305   LoadCommandType type;
306   StringRef path;
307   uint32_t compatibilityVersion;
308   uint32_t currentVersion;
309   static uint32_t instanceCount;
310 };
311 
312 uint32_t LCDylib::instanceCount = 0;
313 
314 class LCLoadDylinker : public LoadCommand {
315 public:
316   uint32_t getSize() const override {
317     return alignTo(sizeof(dylinker_command) + path.size() + 1, 8);
318   }
319 
320   void writeTo(uint8_t *buf) const override {
321     auto *c = reinterpret_cast<dylinker_command *>(buf);
322     buf += sizeof(dylinker_command);
323 
324     c->cmd = LC_LOAD_DYLINKER;
325     c->cmdsize = getSize();
326     c->name = sizeof(dylinker_command);
327 
328     memcpy(buf, path.data(), path.size());
329     buf[path.size()] = '\0';
330   }
331 
332 private:
333   // Recent versions of Darwin won't run any binary that has dyld at a
334   // different location.
335   const StringRef path = "/usr/lib/dyld";
336 };
337 
338 class LCRPath : public LoadCommand {
339 public:
340   LCRPath(StringRef path) : path(path) {}
341 
342   uint32_t getSize() const override {
343     return alignTo(sizeof(rpath_command) + path.size() + 1, WordSize);
344   }
345 
346   void writeTo(uint8_t *buf) const override {
347     auto *c = reinterpret_cast<rpath_command *>(buf);
348     buf += sizeof(rpath_command);
349 
350     c->cmd = LC_RPATH;
351     c->cmdsize = getSize();
352     c->path = sizeof(rpath_command);
353 
354     memcpy(buf, path.data(), path.size());
355     buf[path.size()] = '\0';
356   }
357 
358 private:
359   StringRef path;
360 };
361 
362 class LCBuildVersion : public LoadCommand {
363 public:
364   LCBuildVersion(PlatformKind platform, const PlatformInfo &platformInfo)
365       : platform(platform), platformInfo(platformInfo) {}
366 
367   const int ntools = 1;
368 
369   uint32_t getSize() const override {
370     return sizeof(build_version_command) + ntools * sizeof(build_tool_version);
371   }
372 
373   void writeTo(uint8_t *buf) const override {
374     auto *c = reinterpret_cast<build_version_command *>(buf);
375     c->cmd = LC_BUILD_VERSION;
376     c->cmdsize = getSize();
377     c->platform = static_cast<uint32_t>(platform);
378     c->minos = ((platformInfo.minimum.getMajor() << 020) |
379                 (platformInfo.minimum.getMinor().getValueOr(0) << 010) |
380                 platformInfo.minimum.getSubminor().getValueOr(0));
381     c->sdk = ((platformInfo.sdk.getMajor() << 020) |
382               (platformInfo.sdk.getMinor().getValueOr(0) << 010) |
383               platformInfo.sdk.getSubminor().getValueOr(0));
384     c->ntools = ntools;
385     auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
386     t->tool = TOOL_LD;
387     t->version = (LLVM_VERSION_MAJOR << 020) | (LLVM_VERSION_MINOR << 010) |
388                  LLVM_VERSION_PATCH;
389   }
390 
391   PlatformKind platform;
392   const PlatformInfo &platformInfo;
393 };
394 
395 // Stores a unique identifier for the output file based on an MD5 hash of its
396 // contents. In order to hash the contents, we must first write them, but
397 // LC_UUID itself must be part of the written contents in order for all the
398 // offsets to be calculated correctly. We resolve this circular paradox by
399 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with
400 // its real value later.
401 class LCUuid : public LoadCommand {
402 public:
403   uint32_t getSize() const override { return sizeof(uuid_command); }
404 
405   void writeTo(uint8_t *buf) const override {
406     auto *c = reinterpret_cast<uuid_command *>(buf);
407     c->cmd = LC_UUID;
408     c->cmdsize = getSize();
409     uuidBuf = c->uuid;
410   }
411 
412   void writeUuid(uint64_t digest) const {
413     // xxhash only gives us 8 bytes, so put some fixed data in the other half.
414     static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size");
415     memcpy(uuidBuf, "LLD\xa1UU1D", 8);
416     memcpy(uuidBuf + 8, &digest, 8);
417 
418     // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in
419     // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't
420     // want to lose bits of the digest in byte 8, so swap that with a byte of
421     // fixed data that happens to have the right bits set.
422     std::swap(uuidBuf[3], uuidBuf[8]);
423 
424     // Claim that this is an MD5-based hash. It isn't, but this signals that
425     // this is not a time-based and not a random hash. MD5 seems like the least
426     // bad lie we can put here.
427     assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3");
428     assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2");
429   }
430 
431   mutable uint8_t *uuidBuf;
432 };
433 
434 class LCCodeSignature : public LoadCommand {
435 public:
436   LCCodeSignature(CodeSignatureSection *section) : section(section) {}
437 
438   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
439 
440   void writeTo(uint8_t *buf) const override {
441     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
442     c->cmd = LC_CODE_SIGNATURE;
443     c->cmdsize = getSize();
444     c->dataoff = static_cast<uint32_t>(section->fileOff);
445     c->datasize = section->getSize();
446   }
447 
448   CodeSignatureSection *section;
449 };
450 
451 } // namespace
452 
453 // Adds stubs and bindings where necessary (e.g. if the symbol is a
454 // DylibSymbol.)
455 static void prepareBranchTarget(Symbol *sym) {
456   if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
457     if (in.stubs->addEntry(dysym)) {
458       if (sym->isWeakDef()) {
459         in.binding->addEntry(dysym, in.lazyPointers->isec,
460                              sym->stubsIndex * WordSize);
461         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
462                                  sym->stubsIndex * WordSize);
463       } else {
464         in.lazyBinding->addEntry(dysym);
465       }
466     }
467   } else if (auto *defined = dyn_cast<Defined>(sym)) {
468     if (defined->isExternalWeakDef()) {
469       if (in.stubs->addEntry(sym)) {
470         in.rebase->addEntry(in.lazyPointers->isec, sym->stubsIndex * WordSize);
471         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
472                                  sym->stubsIndex * WordSize);
473       }
474     }
475   }
476 }
477 
478 // Can a symbol's address can only be resolved at runtime?
479 static bool needsBinding(const Symbol *sym) {
480   if (isa<DylibSymbol>(sym))
481     return true;
482   if (const auto *defined = dyn_cast<Defined>(sym))
483     return defined->isExternalWeakDef();
484   return false;
485 }
486 
487 static void prepareSymbolRelocation(lld::macho::Symbol *sym,
488                                     const InputSection *isec, const Reloc &r) {
489   const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
490 
491   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
492     prepareBranchTarget(sym);
493   } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
494     if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym))
495       in.got->addEntry(sym);
496   } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
497     if (needsBinding(sym))
498       in.tlvPointers->addEntry(sym);
499   } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) {
500     // References from thread-local variable sections are treated as offsets
501     // relative to the start of the referent section, and therefore have no
502     // need of rebase opcodes.
503     if (!(isThreadLocalVariables(isec->flags) && isa<Defined>(sym)))
504       addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
505   }
506 }
507 
508 void Writer::scanRelocations() {
509   TimeTraceScope timeScope("Scan relocations");
510   for (InputSection *isec : inputSections) {
511     if (isec->segname == segment_names::ld) {
512       prepareCompactUnwind(isec);
513       continue;
514     }
515 
516     for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
517       Reloc &r = *it;
518       if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
519         // Skip over the following UNSIGNED relocation -- it's just there as the
520         // minuend, and doesn't have the usual UNSIGNED semantics. We don't want
521         // to emit rebase opcodes for it.
522         it = std::next(it);
523         assert(isa<Defined>(it->referent.dyn_cast<lld::macho::Symbol *>()));
524         continue;
525       }
526       if (auto *sym = r.referent.dyn_cast<lld::macho::Symbol *>()) {
527         if (auto *undefined = dyn_cast<Undefined>(sym))
528           treatUndefinedSymbol(*undefined);
529         // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
530         if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r))
531           prepareSymbolRelocation(sym, isec, r);
532       } else {
533         assert(r.referent.is<InputSection *>());
534         if (!r.pcrel)
535           in.rebase->addEntry(isec, r.offset);
536       }
537     }
538   }
539 }
540 
541 void Writer::scanSymbols() {
542   TimeTraceScope timeScope("Scan symbols");
543   for (const macho::Symbol *sym : symtab->getSymbols()) {
544     if (const auto *defined = dyn_cast<Defined>(sym)) {
545       if (defined->overridesWeakDef)
546         in.weakBinding->addNonWeakDefinition(defined);
547     } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
548       if (dysym->isDynamicLookup())
549         continue;
550       dysym->getFile()->refState =
551           std::max(dysym->getFile()->refState, dysym->refState);
552     }
553   }
554 }
555 
556 void Writer::createLoadCommands() {
557   uint8_t segIndex = 0;
558   for (OutputSegment *seg : outputSegments) {
559     in.header->addLoadCommand(make<LCSegment>(seg->name, seg));
560     seg->index = segIndex++;
561   }
562 
563   in.header->addLoadCommand(make<LCDyldInfo>(
564       in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports));
565   in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection));
566   in.header->addLoadCommand(
567       make<LCDysymtab>(symtabSection, indirectSymtabSection));
568   if (functionStartsSection)
569     in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection));
570   for (StringRef path : config->runtimePaths)
571     in.header->addLoadCommand(make<LCRPath>(path));
572 
573   switch (config->outputType) {
574   case MH_EXECUTE:
575     in.header->addLoadCommand(make<LCLoadDylinker>());
576     in.header->addLoadCommand(make<LCMain>());
577     break;
578   case MH_DYLIB:
579     in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
580                                             config->dylibCompatibilityVersion,
581                                             config->dylibCurrentVersion));
582     break;
583   case MH_BUNDLE:
584     break;
585   default:
586     llvm_unreachable("unhandled output file type");
587   }
588 
589   uuidCommand = make<LCUuid>();
590   in.header->addLoadCommand(uuidCommand);
591 
592   in.header->addLoadCommand(
593       make<LCBuildVersion>(config->target.Platform, config->platformInfo));
594 
595   int64_t dylibOrdinal = 1;
596   for (InputFile *file : inputFiles) {
597     if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
598       if (dylibFile->isBundleLoader) {
599         dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE;
600         // Shortcut since bundle-loader does not re-export the symbols.
601 
602         dylibFile->reexport = false;
603         continue;
604       }
605 
606       dylibFile->ordinal = dylibOrdinal++;
607       LoadCommandType lcType =
608           dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
609               ? LC_LOAD_WEAK_DYLIB
610               : LC_LOAD_DYLIB;
611       in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->dylibName,
612                                               dylibFile->compatibilityVersion,
613                                               dylibFile->currentVersion));
614 
615       if (dylibFile->reexport)
616         in.header->addLoadCommand(
617             make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->dylibName));
618     }
619   }
620 
621   if (codeSignatureSection)
622     in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection));
623 
624   const uint32_t MACOS_MAXPATHLEN = 1024;
625   config->headerPad = std::max(
626       config->headerPad, (config->headerPadMaxInstallNames
627                               ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN
628                               : 0));
629 }
630 
631 static size_t getSymbolPriority(const SymbolPriorityEntry &entry,
632                                 const InputFile *f) {
633   // We don't use toString(InputFile *) here because it returns the full path
634   // for object files, and we only want the basename.
635   StringRef filename;
636   if (f->archiveName.empty())
637     filename = path::filename(f->getName());
638   else
639     filename = saver.save(path::filename(f->archiveName) + "(" +
640                           path::filename(f->getName()) + ")");
641   return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile);
642 }
643 
644 // Each section gets assigned the priority of the highest-priority symbol it
645 // contains.
646 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() {
647   DenseMap<const InputSection *, size_t> sectionPriorities;
648 
649   if (config->priorities.empty())
650     return sectionPriorities;
651 
652   auto addSym = [&](Defined &sym) {
653     auto it = config->priorities.find(sym.getName());
654     if (it == config->priorities.end())
655       return;
656 
657     SymbolPriorityEntry &entry = it->second;
658     size_t &priority = sectionPriorities[sym.isec];
659     priority = std::max(priority, getSymbolPriority(entry, sym.isec->file));
660   };
661 
662   // TODO: Make sure this handles weak symbols correctly.
663   for (const InputFile *file : inputFiles)
664     if (isa<ObjFile>(file))
665       for (lld::macho::Symbol *sym : file->symbols)
666         if (auto *d = dyn_cast<Defined>(sym))
667           addSym(*d);
668 
669   return sectionPriorities;
670 }
671 
672 static int segmentOrder(OutputSegment *seg) {
673   return StringSwitch<int>(seg->name)
674       .Case(segment_names::pageZero, -4)
675       .Case(segment_names::text, -3)
676       .Case(segment_names::dataConst, -2)
677       .Case(segment_names::data, -1)
678       // Make sure __LINKEDIT is the last segment (i.e. all its hidden
679       // sections must be ordered after other sections).
680       .Case(segment_names::linkEdit, std::numeric_limits<int>::max())
681       .Default(0);
682 }
683 
684 static int sectionOrder(OutputSection *osec) {
685   StringRef segname = osec->parent->name;
686   // Sections are uniquely identified by their segment + section name.
687   if (segname == segment_names::text) {
688     return StringSwitch<int>(osec->name)
689         .Case(section_names::header, -4)
690         .Case(section_names::text, -3)
691         .Case(section_names::stubs, -2)
692         .Case(section_names::stubHelper, -1)
693         .Case(section_names::unwindInfo, std::numeric_limits<int>::max() - 1)
694         .Case(section_names::ehFrame, std::numeric_limits<int>::max())
695         .Default(0);
696   } else if (segname == segment_names::data) {
697     // For each thread spawned, dyld will initialize its TLVs by copying the
698     // address range from the start of the first thread-local data section to
699     // the end of the last one. We therefore arrange these sections contiguously
700     // to minimize the amount of memory used. Additionally, since zerofill
701     // sections must be at the end of their segments, and since TLV data
702     // sections can be zerofills, we end up putting all TLV data sections at the
703     // end of the segment.
704     switch (sectionType(osec->flags)) {
705     case S_THREAD_LOCAL_REGULAR:
706       return std::numeric_limits<int>::max() - 2;
707     case S_THREAD_LOCAL_ZEROFILL:
708       return std::numeric_limits<int>::max() - 1;
709     case S_ZEROFILL:
710       return std::numeric_limits<int>::max();
711     default:
712       return StringSwitch<int>(osec->name)
713           .Case(section_names::laSymbolPtr, -2)
714           .Case(section_names::data, -1)
715           .Default(0);
716     }
717   } else if (segname == segment_names::linkEdit) {
718     return StringSwitch<int>(osec->name)
719         .Case(section_names::rebase, -9)
720         .Case(section_names::binding, -8)
721         .Case(section_names::weakBinding, -7)
722         .Case(section_names::lazyBinding, -6)
723         .Case(section_names::export_, -5)
724         .Case(section_names::functionStarts, -4)
725         .Case(section_names::symbolTable, -3)
726         .Case(section_names::indirectSymbolTable, -2)
727         .Case(section_names::stringTable, -1)
728         .Case(section_names::codeSignature, std::numeric_limits<int>::max())
729         .Default(0);
730   }
731   // ZeroFill sections must always be the at the end of their segments,
732   // otherwise subsequent sections may get overwritten with zeroes at runtime.
733   if (sectionType(osec->flags) == S_ZEROFILL)
734     return std::numeric_limits<int>::max();
735   return 0;
736 }
737 
738 template <typename T, typename F>
739 static std::function<bool(T, T)> compareByOrder(F ord) {
740   return [=](T a, T b) { return ord(a) < ord(b); };
741 }
742 
743 // Sorting only can happen once all outputs have been collected. Here we sort
744 // segments, output sections within each segment, and input sections within each
745 // output segment.
746 static void sortSegmentsAndSections() {
747   TimeTraceScope timeScope("Sort segments and sections");
748 
749   llvm::stable_sort(outputSegments,
750                     compareByOrder<OutputSegment *>(segmentOrder));
751 
752   DenseMap<const InputSection *, size_t> isecPriorities =
753       buildInputSectionPriorities();
754 
755   uint32_t sectionIndex = 0;
756   for (OutputSegment *seg : outputSegments) {
757     seg->sortOutputSections(compareByOrder<OutputSection *>(sectionOrder));
758     for (OutputSection *osec : seg->getSections()) {
759       // Now that the output sections are sorted, assign the final
760       // output section indices.
761       if (!osec->isHidden())
762         osec->index = ++sectionIndex;
763       if (!firstTLVDataSection && isThreadLocalData(osec->flags))
764         firstTLVDataSection = osec;
765 
766       if (!isecPriorities.empty()) {
767         if (auto *merged = dyn_cast<MergedOutputSection>(osec)) {
768           llvm::stable_sort(merged->inputs,
769                             [&](InputSection *a, InputSection *b) {
770                               return isecPriorities[a] > isecPriorities[b];
771                             });
772         }
773       }
774     }
775   }
776 }
777 
778 static NamePair maybeRenameSection(NamePair key) {
779   auto newNames = config->sectionRenameMap.find(key);
780   if (newNames != config->sectionRenameMap.end())
781     return newNames->second;
782   auto newName = config->segmentRenameMap.find(key.first);
783   if (newName != config->segmentRenameMap.end())
784     return std::make_pair(newName->second, key.second);
785   return key;
786 }
787 
788 void Writer::createOutputSections() {
789   TimeTraceScope timeScope("Create output sections");
790   // First, create hidden sections
791   stringTableSection = make<StringTableSection>();
792   unwindInfoSection = make<UnwindInfoSection>(); // TODO(gkm): only when no -r
793   symtabSection = make<SymtabSection>(*stringTableSection);
794   indirectSymtabSection = make<IndirectSymtabSection>();
795   if (config->adhocCodesign)
796     codeSignatureSection = make<CodeSignatureSection>();
797   if (config->emitFunctionStarts)
798     functionStartsSection = make<FunctionStartsSection>();
799 
800   switch (config->outputType) {
801   case MH_EXECUTE:
802     make<PageZeroSection>();
803     break;
804   case MH_DYLIB:
805   case MH_BUNDLE:
806     break;
807   default:
808     llvm_unreachable("unhandled output file type");
809   }
810 
811   // Then merge input sections into output sections.
812   MapVector<NamePair, MergedOutputSection *> mergedOutputSections;
813   for (InputSection *isec : inputSections) {
814     NamePair names = maybeRenameSection({isec->segname, isec->name});
815     MergedOutputSection *&osec = mergedOutputSections[names];
816     if (osec == nullptr)
817       osec = make<MergedOutputSection>(names.second);
818     osec->mergeInput(isec);
819   }
820 
821   for (const auto &it : mergedOutputSections) {
822     StringRef segname = it.first.first;
823     MergedOutputSection *osec = it.second;
824     if (unwindInfoSection && segname == segment_names::ld) {
825       assert(osec->name == section_names::compactUnwind);
826       unwindInfoSection->setCompactUnwindSection(osec);
827     } else {
828       getOrCreateOutputSegment(segname)->addOutputSection(osec);
829     }
830   }
831 
832   for (SyntheticSection *ssec : syntheticSections) {
833     auto it = mergedOutputSections.find({ssec->segname, ssec->name});
834     if (it == mergedOutputSections.end()) {
835       if (ssec->isNeeded())
836         getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
837     } else {
838       error("section from " + toString(it->second->firstSection()->file) +
839             " conflicts with synthetic section " + ssec->segname + "," +
840             ssec->name);
841     }
842   }
843 
844   // dyld requires __LINKEDIT segment to always exist (even if empty).
845   linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit);
846 }
847 
848 void Writer::finalizeAddressses() {
849   TimeTraceScope timeScope("Finalize addresses");
850   // Ensure that segments (and the sections they contain) are allocated
851   // addresses in ascending order, which dyld requires.
852   //
853   // Note that at this point, __LINKEDIT sections are empty, but we need to
854   // determine addresses of other segments/sections before generating its
855   // contents.
856   for (OutputSegment *seg : outputSegments)
857     if (seg != linkEditSegment)
858       assignAddresses(seg);
859 
860   // FIXME(gkm): create branch-extension thunks here, then adjust addresses
861 }
862 
863 void Writer::finalizeLinkEditSegment() {
864   TimeTraceScope timeScope("Finalize __LINKEDIT segment");
865   // Fill __LINKEDIT contents.
866   in.rebase->finalizeContents();
867   in.binding->finalizeContents();
868   in.weakBinding->finalizeContents();
869   in.lazyBinding->finalizeContents();
870   in.exports->finalizeContents();
871   symtabSection->finalizeContents();
872   indirectSymtabSection->finalizeContents();
873 
874   if (functionStartsSection)
875     functionStartsSection->finalizeContents();
876 
877   // Now that __LINKEDIT is filled out, do a proper calculation of its
878   // addresses and offsets.
879   assignAddresses(linkEditSegment);
880 }
881 
882 void Writer::assignAddresses(OutputSegment *seg) {
883   uint64_t pageSize = target->getPageSize();
884   addr = alignTo(addr, pageSize);
885   fileOff = alignTo(fileOff, pageSize);
886   seg->fileOff = fileOff;
887 
888   for (OutputSection *osec : seg->getSections()) {
889     if (!osec->isNeeded())
890       continue;
891     addr = alignTo(addr, osec->align);
892     fileOff = alignTo(fileOff, osec->align);
893     osec->addr = addr;
894     osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff;
895     osec->finalize();
896 
897     addr += osec->getSize();
898     fileOff += osec->getFileSize();
899   }
900   seg->fileSize = fileOff - seg->fileOff;
901 }
902 
903 void Writer::openFile() {
904   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
905       FileOutputBuffer::create(config->outputFile, fileOff,
906                                FileOutputBuffer::F_executable);
907 
908   if (!bufferOrErr)
909     error("failed to open " + config->outputFile + ": " +
910           llvm::toString(bufferOrErr.takeError()));
911   else
912     buffer = std::move(*bufferOrErr);
913 }
914 
915 void Writer::writeSections() {
916   uint8_t *buf = buffer->getBufferStart();
917   for (const OutputSegment *seg : outputSegments)
918     for (const OutputSection *osec : seg->getSections())
919       osec->writeTo(buf + osec->fileOff);
920 }
921 
922 void Writer::writeUuid() {
923   TimeTraceScope timeScope("Computing UUID");
924   uint64_t digest =
925       xxHash64({buffer->getBufferStart(), buffer->getBufferEnd()});
926   uuidCommand->writeUuid(digest);
927 }
928 
929 void Writer::writeCodeSignature() {
930   if (codeSignatureSection)
931     codeSignatureSection->writeHashes(buffer->getBufferStart());
932 }
933 
934 void Writer::writeOutputFile() {
935   TimeTraceScope timeScope("Write output file");
936   openFile();
937   if (errorCount())
938     return;
939   writeSections();
940   writeUuid();
941   writeCodeSignature();
942 
943   if (auto e = buffer->commit())
944     error("failed to write to the output file: " + toString(std::move(e)));
945 }
946 
947 void Writer::run() {
948   prepareBranchTarget(config->entry);
949   scanRelocations();
950   if (in.stubHelper->isNeeded())
951     in.stubHelper->setup();
952   scanSymbols();
953   createOutputSections();
954   // No more sections nor segments are created beyond this point.
955   sortSegmentsAndSections();
956   createLoadCommands();
957   finalizeAddressses();
958   finalizeLinkEditSegment();
959   writeMapFile();
960   writeOutputFile();
961 }
962 
963 void macho::writeResult() { Writer().run(); }
964 
965 void macho::createSyntheticSections() {
966   in.header = make<MachHeaderSection>();
967   in.rebase = make<RebaseSection>();
968   in.binding = make<BindingSection>();
969   in.weakBinding = make<WeakBindingSection>();
970   in.lazyBinding = make<LazyBindingSection>();
971   in.exports = make<ExportSection>();
972   in.got = make<GotSection>();
973   in.tlvPointers = make<TlvPointerSection>();
974   in.lazyPointers = make<LazyPointerSection>();
975   in.stubs = make<StubsSection>();
976   in.stubHelper = make<StubHelperSection>();
977   in.imageLoaderCache = make<ImageLoaderCacheSection>();
978 }
979 
980 OutputSection *macho::firstTLVDataSection = nullptr;
981