xref: /llvm-project-15.0.7/lld/MachO/Writer.cpp (revision 566bfbb7)
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 "ConcatOutputSection.h"
11 #include "Config.h"
12 #include "InputFiles.h"
13 #include "InputSection.h"
14 #include "MapFile.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/Arrays.h"
24 #include "lld/Common/ErrorHandler.h"
25 #include "lld/Common/Memory.h"
26 #include "llvm/BinaryFormat/MachO.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/Parallel.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/TimeProfiler.h"
33 #include "llvm/Support/xxhash.h"
34 
35 #include <algorithm>
36 
37 using namespace llvm;
38 using namespace llvm::MachO;
39 using namespace llvm::sys;
40 using namespace lld;
41 using namespace lld::macho;
42 
43 namespace {
44 class LCUuid;
45 
46 class Writer {
47 public:
48   Writer() : buffer(errorHandler().outputBuffer) {}
49 
50   void treatSpecialUndefineds();
51   void scanRelocations();
52   void scanSymbols();
53   template <class LP> void createOutputSections();
54   template <class LP> void createLoadCommands();
55   void finalizeAddresses();
56   void finalizeLinkEditSegment();
57   void assignAddresses(OutputSegment *);
58 
59   void openFile();
60   void writeSections();
61   void writeUuid();
62   void writeCodeSignature();
63   void writeOutputFile();
64 
65   template <class LP> void run();
66 
67   std::unique_ptr<FileOutputBuffer> &buffer;
68   uint64_t addr = 0;
69   uint64_t fileOff = 0;
70   MachHeaderSection *header = nullptr;
71   StringTableSection *stringTableSection = nullptr;
72   SymtabSection *symtabSection = nullptr;
73   IndirectSymtabSection *indirectSymtabSection = nullptr;
74   CodeSignatureSection *codeSignatureSection = nullptr;
75   DataInCodeSection *dataInCodeSection = nullptr;
76   FunctionStartsSection *functionStartsSection = nullptr;
77 
78   LCUuid *uuidCommand = nullptr;
79   OutputSegment *linkEditSegment = nullptr;
80 };
81 
82 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information.
83 class LCDyldInfo final : public LoadCommand {
84 public:
85   LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection,
86              WeakBindingSection *weakBindingSection,
87              LazyBindingSection *lazyBindingSection,
88              ExportSection *exportSection)
89       : rebaseSection(rebaseSection), bindingSection(bindingSection),
90         weakBindingSection(weakBindingSection),
91         lazyBindingSection(lazyBindingSection), exportSection(exportSection) {}
92 
93   uint32_t getSize() const override { return sizeof(dyld_info_command); }
94 
95   void writeTo(uint8_t *buf) const override {
96     auto *c = reinterpret_cast<dyld_info_command *>(buf);
97     c->cmd = LC_DYLD_INFO_ONLY;
98     c->cmdsize = getSize();
99     if (rebaseSection->isNeeded()) {
100       c->rebase_off = rebaseSection->fileOff;
101       c->rebase_size = rebaseSection->getFileSize();
102     }
103     if (bindingSection->isNeeded()) {
104       c->bind_off = bindingSection->fileOff;
105       c->bind_size = bindingSection->getFileSize();
106     }
107     if (weakBindingSection->isNeeded()) {
108       c->weak_bind_off = weakBindingSection->fileOff;
109       c->weak_bind_size = weakBindingSection->getFileSize();
110     }
111     if (lazyBindingSection->isNeeded()) {
112       c->lazy_bind_off = lazyBindingSection->fileOff;
113       c->lazy_bind_size = lazyBindingSection->getFileSize();
114     }
115     if (exportSection->isNeeded()) {
116       c->export_off = exportSection->fileOff;
117       c->export_size = exportSection->getFileSize();
118     }
119   }
120 
121   RebaseSection *rebaseSection;
122   BindingSection *bindingSection;
123   WeakBindingSection *weakBindingSection;
124   LazyBindingSection *lazyBindingSection;
125   ExportSection *exportSection;
126 };
127 
128 class LCSubFramework final : public LoadCommand {
129 public:
130   LCSubFramework(StringRef umbrella) : umbrella(umbrella) {}
131 
132   uint32_t getSize() const override {
133     return alignTo(sizeof(sub_framework_command) + umbrella.size() + 1,
134                    target->wordSize);
135   }
136 
137   void writeTo(uint8_t *buf) const override {
138     auto *c = reinterpret_cast<sub_framework_command *>(buf);
139     buf += sizeof(sub_framework_command);
140 
141     c->cmd = LC_SUB_FRAMEWORK;
142     c->cmdsize = getSize();
143     c->umbrella = sizeof(sub_framework_command);
144 
145     memcpy(buf, umbrella.data(), umbrella.size());
146     buf[umbrella.size()] = '\0';
147   }
148 
149 private:
150   const StringRef umbrella;
151 };
152 
153 class LCFunctionStarts final : public LoadCommand {
154 public:
155   explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection)
156       : functionStartsSection(functionStartsSection) {}
157 
158   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
159 
160   void writeTo(uint8_t *buf) const override {
161     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
162     c->cmd = LC_FUNCTION_STARTS;
163     c->cmdsize = getSize();
164     c->dataoff = functionStartsSection->fileOff;
165     c->datasize = functionStartsSection->getFileSize();
166   }
167 
168 private:
169   FunctionStartsSection *functionStartsSection;
170 };
171 
172 class LCDataInCode final : public LoadCommand {
173 public:
174   explicit LCDataInCode(DataInCodeSection *dataInCodeSection)
175       : dataInCodeSection(dataInCodeSection) {}
176 
177   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
178 
179   void writeTo(uint8_t *buf) const override {
180     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
181     c->cmd = LC_DATA_IN_CODE;
182     c->cmdsize = getSize();
183     c->dataoff = dataInCodeSection->fileOff;
184     c->datasize = dataInCodeSection->getFileSize();
185   }
186 
187 private:
188   DataInCodeSection *dataInCodeSection;
189 };
190 
191 class LCDysymtab final : public LoadCommand {
192 public:
193   LCDysymtab(SymtabSection *symtabSection,
194              IndirectSymtabSection *indirectSymtabSection)
195       : symtabSection(symtabSection),
196         indirectSymtabSection(indirectSymtabSection) {}
197 
198   uint32_t getSize() const override { return sizeof(dysymtab_command); }
199 
200   void writeTo(uint8_t *buf) const override {
201     auto *c = reinterpret_cast<dysymtab_command *>(buf);
202     c->cmd = LC_DYSYMTAB;
203     c->cmdsize = getSize();
204 
205     c->ilocalsym = 0;
206     c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols();
207     c->nextdefsym = symtabSection->getNumExternalSymbols();
208     c->iundefsym = c->iextdefsym + c->nextdefsym;
209     c->nundefsym = symtabSection->getNumUndefinedSymbols();
210 
211     c->indirectsymoff = indirectSymtabSection->fileOff;
212     c->nindirectsyms = indirectSymtabSection->getNumSymbols();
213   }
214 
215   SymtabSection *symtabSection;
216   IndirectSymtabSection *indirectSymtabSection;
217 };
218 
219 template <class LP> class LCSegment final : public LoadCommand {
220 public:
221   LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {}
222 
223   uint32_t getSize() const override {
224     return sizeof(typename LP::segment_command) +
225            seg->numNonHiddenSections() * sizeof(typename LP::section);
226   }
227 
228   void writeTo(uint8_t *buf) const override {
229     using SegmentCommand = typename LP::segment_command;
230     using Section = typename LP::section;
231 
232     auto *c = reinterpret_cast<SegmentCommand *>(buf);
233     buf += sizeof(SegmentCommand);
234 
235     c->cmd = LP::segmentLCType;
236     c->cmdsize = getSize();
237     memcpy(c->segname, name.data(), name.size());
238     c->fileoff = seg->fileOff;
239     c->maxprot = seg->maxProt;
240     c->initprot = seg->initProt;
241 
242     c->vmaddr = seg->addr;
243     c->vmsize = seg->vmSize;
244     c->filesize = seg->fileSize;
245     c->nsects = seg->numNonHiddenSections();
246 
247     for (const OutputSection *osec : seg->getSections()) {
248       if (osec->isHidden())
249         continue;
250 
251       auto *sectHdr = reinterpret_cast<Section *>(buf);
252       buf += sizeof(Section);
253 
254       memcpy(sectHdr->sectname, osec->name.data(), osec->name.size());
255       memcpy(sectHdr->segname, name.data(), name.size());
256 
257       sectHdr->addr = osec->addr;
258       sectHdr->offset = osec->fileOff;
259       sectHdr->align = Log2_32(osec->align);
260       sectHdr->flags = osec->flags;
261       sectHdr->size = osec->getSize();
262       sectHdr->reserved1 = osec->reserved1;
263       sectHdr->reserved2 = osec->reserved2;
264     }
265   }
266 
267 private:
268   StringRef name;
269   OutputSegment *seg;
270 };
271 
272 class LCMain final : public LoadCommand {
273   uint32_t getSize() const override {
274     return sizeof(structs::entry_point_command);
275   }
276 
277   void writeTo(uint8_t *buf) const override {
278     auto *c = reinterpret_cast<structs::entry_point_command *>(buf);
279     c->cmd = LC_MAIN;
280     c->cmdsize = getSize();
281 
282     if (config->entry->isInStubs())
283       c->entryoff =
284           in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
285     else
286       c->entryoff = config->entry->getVA() - in.header->addr;
287 
288     c->stacksize = 0;
289   }
290 };
291 
292 class LCSymtab final : public LoadCommand {
293 public:
294   LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection)
295       : symtabSection(symtabSection), stringTableSection(stringTableSection) {}
296 
297   uint32_t getSize() const override { return sizeof(symtab_command); }
298 
299   void writeTo(uint8_t *buf) const override {
300     auto *c = reinterpret_cast<symtab_command *>(buf);
301     c->cmd = LC_SYMTAB;
302     c->cmdsize = getSize();
303     c->symoff = symtabSection->fileOff;
304     c->nsyms = symtabSection->getNumSymbols();
305     c->stroff = stringTableSection->fileOff;
306     c->strsize = stringTableSection->getFileSize();
307   }
308 
309   SymtabSection *symtabSection = nullptr;
310   StringTableSection *stringTableSection = nullptr;
311 };
312 
313 // There are several dylib load commands that share the same structure:
314 //   * LC_LOAD_DYLIB
315 //   * LC_ID_DYLIB
316 //   * LC_REEXPORT_DYLIB
317 class LCDylib final : public LoadCommand {
318 public:
319   LCDylib(LoadCommandType type, StringRef path,
320           uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0)
321       : type(type), path(path), compatibilityVersion(compatibilityVersion),
322         currentVersion(currentVersion) {
323     instanceCount++;
324   }
325 
326   uint32_t getSize() const override {
327     return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
328   }
329 
330   void writeTo(uint8_t *buf) const override {
331     auto *c = reinterpret_cast<dylib_command *>(buf);
332     buf += sizeof(dylib_command);
333 
334     c->cmd = type;
335     c->cmdsize = getSize();
336     c->dylib.name = sizeof(dylib_command);
337     c->dylib.timestamp = 0;
338     c->dylib.compatibility_version = compatibilityVersion;
339     c->dylib.current_version = currentVersion;
340 
341     memcpy(buf, path.data(), path.size());
342     buf[path.size()] = '\0';
343   }
344 
345   static uint32_t getInstanceCount() { return instanceCount; }
346 
347 private:
348   LoadCommandType type;
349   StringRef path;
350   uint32_t compatibilityVersion;
351   uint32_t currentVersion;
352   static uint32_t instanceCount;
353 };
354 
355 uint32_t LCDylib::instanceCount = 0;
356 
357 class LCLoadDylinker final : public LoadCommand {
358 public:
359   uint32_t getSize() const override {
360     return alignTo(sizeof(dylinker_command) + path.size() + 1, 8);
361   }
362 
363   void writeTo(uint8_t *buf) const override {
364     auto *c = reinterpret_cast<dylinker_command *>(buf);
365     buf += sizeof(dylinker_command);
366 
367     c->cmd = LC_LOAD_DYLINKER;
368     c->cmdsize = getSize();
369     c->name = sizeof(dylinker_command);
370 
371     memcpy(buf, path.data(), path.size());
372     buf[path.size()] = '\0';
373   }
374 
375 private:
376   // Recent versions of Darwin won't run any binary that has dyld at a
377   // different location.
378   const StringRef path = "/usr/lib/dyld";
379 };
380 
381 class LCRPath final : public LoadCommand {
382 public:
383   explicit LCRPath(StringRef path) : path(path) {}
384 
385   uint32_t getSize() const override {
386     return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize);
387   }
388 
389   void writeTo(uint8_t *buf) const override {
390     auto *c = reinterpret_cast<rpath_command *>(buf);
391     buf += sizeof(rpath_command);
392 
393     c->cmd = LC_RPATH;
394     c->cmdsize = getSize();
395     c->path = sizeof(rpath_command);
396 
397     memcpy(buf, path.data(), path.size());
398     buf[path.size()] = '\0';
399   }
400 
401 private:
402   StringRef path;
403 };
404 
405 class LCMinVersion final : public LoadCommand {
406 public:
407   explicit LCMinVersion(const PlatformInfo &platformInfo)
408       : platformInfo(platformInfo) {}
409 
410   uint32_t getSize() const override { return sizeof(version_min_command); }
411 
412   void writeTo(uint8_t *buf) const override {
413     auto *c = reinterpret_cast<version_min_command *>(buf);
414     switch (platformInfo.target.Platform) {
415     case PlatformKind::macOS:
416       c->cmd = LC_VERSION_MIN_MACOSX;
417       break;
418     case PlatformKind::iOS:
419     case PlatformKind::iOSSimulator:
420       c->cmd = LC_VERSION_MIN_IPHONEOS;
421       break;
422     case PlatformKind::tvOS:
423     case PlatformKind::tvOSSimulator:
424       c->cmd = LC_VERSION_MIN_TVOS;
425       break;
426     case PlatformKind::watchOS:
427     case PlatformKind::watchOSSimulator:
428       c->cmd = LC_VERSION_MIN_WATCHOS;
429       break;
430     default:
431       llvm_unreachable("invalid platform");
432       break;
433     }
434     c->cmdsize = getSize();
435     c->version = encodeVersion(platformInfo.minimum);
436     c->sdk = encodeVersion(platformInfo.sdk);
437   }
438 
439 private:
440   const PlatformInfo &platformInfo;
441 };
442 
443 class LCBuildVersion final : public LoadCommand {
444 public:
445   explicit LCBuildVersion(const PlatformInfo &platformInfo)
446       : platformInfo(platformInfo) {}
447 
448   const int ntools = 1;
449 
450   uint32_t getSize() const override {
451     return sizeof(build_version_command) + ntools * sizeof(build_tool_version);
452   }
453 
454   void writeTo(uint8_t *buf) const override {
455     auto *c = reinterpret_cast<build_version_command *>(buf);
456     c->cmd = LC_BUILD_VERSION;
457     c->cmdsize = getSize();
458     c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
459     c->minos = encodeVersion(platformInfo.minimum);
460     c->sdk = encodeVersion(platformInfo.sdk);
461     c->ntools = ntools;
462     auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
463     t->tool = TOOL_LD;
464     t->version = encodeVersion(VersionTuple(
465         LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH));
466   }
467 
468 private:
469   const PlatformInfo &platformInfo;
470 };
471 
472 // Stores a unique identifier for the output file based on an MD5 hash of its
473 // contents. In order to hash the contents, we must first write them, but
474 // LC_UUID itself must be part of the written contents in order for all the
475 // offsets to be calculated correctly. We resolve this circular paradox by
476 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with
477 // its real value later.
478 class LCUuid final : public LoadCommand {
479 public:
480   uint32_t getSize() const override { return sizeof(uuid_command); }
481 
482   void writeTo(uint8_t *buf) const override {
483     auto *c = reinterpret_cast<uuid_command *>(buf);
484     c->cmd = LC_UUID;
485     c->cmdsize = getSize();
486     uuidBuf = c->uuid;
487   }
488 
489   void writeUuid(uint64_t digest) const {
490     // xxhash only gives us 8 bytes, so put some fixed data in the other half.
491     static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size");
492     memcpy(uuidBuf, "LLD\xa1UU1D", 8);
493     memcpy(uuidBuf + 8, &digest, 8);
494 
495     // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in
496     // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't
497     // want to lose bits of the digest in byte 8, so swap that with a byte of
498     // fixed data that happens to have the right bits set.
499     std::swap(uuidBuf[3], uuidBuf[8]);
500 
501     // Claim that this is an MD5-based hash. It isn't, but this signals that
502     // this is not a time-based and not a random hash. MD5 seems like the least
503     // bad lie we can put here.
504     assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3");
505     assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2");
506   }
507 
508   mutable uint8_t *uuidBuf;
509 };
510 
511 template <class LP> class LCEncryptionInfo final : public LoadCommand {
512 public:
513   uint32_t getSize() const override {
514     return sizeof(typename LP::encryption_info_command);
515   }
516 
517   void writeTo(uint8_t *buf) const override {
518     using EncryptionInfo = typename LP::encryption_info_command;
519     auto *c = reinterpret_cast<EncryptionInfo *>(buf);
520     buf += sizeof(EncryptionInfo);
521     c->cmd = LP::encryptionInfoLCType;
522     c->cmdsize = getSize();
523     c->cryptoff = in.header->getSize();
524     auto it = find_if(outputSegments, [](const OutputSegment *seg) {
525       return seg->name == segment_names::text;
526     });
527     assert(it != outputSegments.end());
528     c->cryptsize = (*it)->fileSize - c->cryptoff;
529   }
530 };
531 
532 class LCCodeSignature final : public LoadCommand {
533 public:
534   LCCodeSignature(CodeSignatureSection *section) : section(section) {}
535 
536   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
537 
538   void writeTo(uint8_t *buf) const override {
539     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
540     c->cmd = LC_CODE_SIGNATURE;
541     c->cmdsize = getSize();
542     c->dataoff = static_cast<uint32_t>(section->fileOff);
543     c->datasize = section->getSize();
544   }
545 
546   CodeSignatureSection *section;
547 };
548 
549 } // namespace
550 
551 void Writer::treatSpecialUndefineds() {
552   if (config->entry)
553     if (auto *undefined = dyn_cast<Undefined>(config->entry))
554       treatUndefinedSymbol(*undefined, "the entry point");
555 
556   // FIXME: This prints symbols that are undefined both in input files and
557   // via -u flag twice.
558   for (const Symbol *sym : config->explicitUndefineds) {
559     if (const auto *undefined = dyn_cast<Undefined>(sym))
560       treatUndefinedSymbol(*undefined, "-u");
561   }
562   // Literal exported-symbol names must be defined, but glob
563   // patterns need not match.
564   for (const CachedHashStringRef &cachedName :
565        config->exportedSymbols.literals) {
566     if (const Symbol *sym = symtab->find(cachedName))
567       if (const auto *undefined = dyn_cast<Undefined>(sym))
568         treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
569   }
570 }
571 
572 // Add stubs and bindings where necessary (e.g. if the symbol is a
573 // DylibSymbol.)
574 static void prepareBranchTarget(Symbol *sym) {
575   if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
576     if (in.stubs->addEntry(dysym)) {
577       if (sym->isWeakDef()) {
578         in.binding->addEntry(dysym, in.lazyPointers->isec,
579                              sym->stubsIndex * target->wordSize);
580         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
581                                  sym->stubsIndex * target->wordSize);
582       } else {
583         in.lazyBinding->addEntry(dysym);
584       }
585     }
586   } else if (auto *defined = dyn_cast<Defined>(sym)) {
587     if (defined->isExternalWeakDef()) {
588       if (in.stubs->addEntry(sym)) {
589         in.rebase->addEntry(in.lazyPointers->isec,
590                             sym->stubsIndex * target->wordSize);
591         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
592                                  sym->stubsIndex * target->wordSize);
593       }
594     }
595   } else {
596     llvm_unreachable("invalid branch target symbol type");
597   }
598 }
599 
600 // Can a symbol's address can only be resolved at runtime?
601 static bool needsBinding(const Symbol *sym) {
602   if (isa<DylibSymbol>(sym))
603     return true;
604   if (const auto *defined = dyn_cast<Defined>(sym))
605     return defined->isExternalWeakDef();
606   return false;
607 }
608 
609 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec,
610                                     const Reloc &r) {
611   assert(sym->isLive());
612   const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
613 
614   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
615     prepareBranchTarget(sym);
616   } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
617     if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym))
618       in.got->addEntry(sym);
619   } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
620     if (needsBinding(sym))
621       in.tlvPointers->addEntry(sym);
622   } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) {
623     // References from thread-local variable sections are treated as offsets
624     // relative to the start of the referent section, and therefore have no
625     // need of rebase opcodes.
626     if (!(isThreadLocalVariables(isec->getFlags()) && isa<Defined>(sym)))
627       addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
628   }
629 }
630 
631 void Writer::scanRelocations() {
632   TimeTraceScope timeScope("Scan relocations");
633 
634   // This can't use a for-each loop: It calls treatUndefinedSymbol(), which can
635   // add to inputSections, which invalidates inputSections's iterators.
636   for (size_t i = 0; i < inputSections.size(); ++i) {
637     ConcatInputSection *isec = inputSections[i];
638 
639     if (isec->shouldOmitFromOutput())
640       continue;
641 
642     for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
643       Reloc &r = *it;
644       if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
645         // Skip over the following UNSIGNED relocation -- it's just there as the
646         // minuend, and doesn't have the usual UNSIGNED semantics. We don't want
647         // to emit rebase opcodes for it.
648         it++;
649         continue;
650       }
651       if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
652         if (auto *undefined = dyn_cast<Undefined>(sym))
653           treatUndefinedSymbol(*undefined);
654         // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
655         if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r))
656           prepareSymbolRelocation(sym, isec, r);
657       } else {
658         // Canonicalize the referent so that later accesses in Writer won't
659         // have to worry about it. Perhaps we should do this for Defined::isec
660         // too...
661         auto *referentIsec = r.referent.get<InputSection *>();
662         r.referent = referentIsec->canonical();
663         if (!r.pcrel)
664           in.rebase->addEntry(isec, r.offset);
665       }
666     }
667   }
668 
669   in.unwindInfo->prepareRelocations();
670 }
671 
672 void Writer::scanSymbols() {
673   TimeTraceScope timeScope("Scan symbols");
674   for (const Symbol *sym : symtab->getSymbols()) {
675     if (const auto *defined = dyn_cast<Defined>(sym)) {
676       if (!defined->isLive())
677         continue;
678       if (defined->overridesWeakDef)
679         in.weakBinding->addNonWeakDefinition(defined);
680       if (!defined->isAbsolute() && isCodeSection(defined->isec))
681         in.unwindInfo->addSymbol(defined);
682     } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
683       // This branch intentionally doesn't check isLive().
684       if (dysym->isDynamicLookup())
685         continue;
686       dysym->getFile()->refState =
687           std::max(dysym->getFile()->refState, dysym->getRefState());
688     }
689   }
690 
691   for (const InputFile *file : inputFiles) {
692     if (auto *objFile = dyn_cast<ObjFile>(file))
693       for (Symbol *sym : objFile->symbols) {
694         if (auto *defined = dyn_cast_or_null<Defined>(sym))
695           if (!defined->isExternal() && defined->isLive() &&
696               !defined->isAbsolute() && isCodeSection(defined->isec))
697             in.unwindInfo->addSymbol(defined);
698       }
699   }
700 }
701 
702 // TODO: ld64 enforces the old load commands in a few other cases.
703 static bool useLCBuildVersion(const PlatformInfo &platformInfo) {
704   static const std::vector<std::pair<PlatformKind, VersionTuple>> minVersion = {
705       {PlatformKind::macOS, VersionTuple(10, 14)},
706       {PlatformKind::iOS, VersionTuple(12, 0)},
707       {PlatformKind::iOSSimulator, VersionTuple(13, 0)},
708       {PlatformKind::tvOS, VersionTuple(12, 0)},
709       {PlatformKind::tvOSSimulator, VersionTuple(13, 0)},
710       {PlatformKind::watchOS, VersionTuple(5, 0)},
711       {PlatformKind::watchOSSimulator, VersionTuple(6, 0)}};
712   auto it = llvm::find_if(minVersion, [&](const auto &p) {
713     return p.first == platformInfo.target.Platform;
714   });
715   return it == minVersion.end() ? true : platformInfo.minimum >= it->second;
716 }
717 
718 template <class LP> void Writer::createLoadCommands() {
719   uint8_t segIndex = 0;
720   for (OutputSegment *seg : outputSegments) {
721     in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg));
722     seg->index = segIndex++;
723   }
724 
725   in.header->addLoadCommand(make<LCDyldInfo>(
726       in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports));
727   in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection));
728   in.header->addLoadCommand(
729       make<LCDysymtab>(symtabSection, indirectSymtabSection));
730   if (!config->umbrella.empty())
731     in.header->addLoadCommand(make<LCSubFramework>(config->umbrella));
732   if (config->emitEncryptionInfo)
733     in.header->addLoadCommand(make<LCEncryptionInfo<LP>>());
734   for (StringRef path : config->runtimePaths)
735     in.header->addLoadCommand(make<LCRPath>(path));
736 
737   switch (config->outputType) {
738   case MH_EXECUTE:
739     in.header->addLoadCommand(make<LCLoadDylinker>());
740     break;
741   case MH_DYLIB:
742     in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
743                                             config->dylibCompatibilityVersion,
744                                             config->dylibCurrentVersion));
745     break;
746   case MH_BUNDLE:
747     break;
748   default:
749     llvm_unreachable("unhandled output file type");
750   }
751 
752   uuidCommand = make<LCUuid>();
753   in.header->addLoadCommand(uuidCommand);
754 
755   if (useLCBuildVersion(config->platformInfo))
756     in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo));
757   else
758     in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo));
759 
760   // This is down here to match ld64's load command order.
761   if (config->outputType == MH_EXECUTE)
762     in.header->addLoadCommand(make<LCMain>());
763 
764   int64_t dylibOrdinal = 1;
765   DenseMap<StringRef, int64_t> ordinalForInstallName;
766   for (InputFile *file : inputFiles) {
767     if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
768       if (dylibFile->isBundleLoader) {
769         dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE;
770         // Shortcut since bundle-loader does not re-export the symbols.
771 
772         dylibFile->reexport = false;
773         continue;
774       }
775 
776       // Don't emit load commands for a dylib that is not referenced if:
777       // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER --
778       //   if it's on the linker command line, it's explicit)
779       // - or it's marked MH_DEAD_STRIPPABLE_DYLIB
780       // - or the flag -dead_strip_dylibs is used
781       // FIXME: `isReferenced()` is currently computed before dead code
782       // stripping, so references from dead code keep a dylib alive. This
783       // matches ld64, but it's something we should do better.
784       if (!dylibFile->isReferenced() && !dylibFile->forceNeeded &&
785           (!dylibFile->explicitlyLinked || dylibFile->deadStrippable ||
786            config->deadStripDylibs))
787         continue;
788 
789       // Several DylibFiles can have the same installName. Only emit a single
790       // load command for that installName and give all these DylibFiles the
791       // same ordinal.
792       // This can happen in several cases:
793       // - a new framework could change its installName to an older
794       //   framework name via an $ld$ symbol depending on platform_version
795       // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd;
796       //   Foo.framework/Foo.tbd is usually a symlink to
797       //   Foo.framework/Versions/Current/Foo.tbd, where
798       //   Foo.framework/Versions/Current is usually a symlink to
799       //   Foo.framework/Versions/A)
800       // - a framework can be linked both explicitly on the linker
801       //   command line and implicitly as a reexport from a different
802       //   framework. The re-export will usually point to the tbd file
803       //   in Foo.framework/Versions/A/Foo.tbd, while the explicit link will
804       //   usually find Foo.framework/Foo.tbd. These are usually symlinks,
805       //   but in a --reproduce archive they will be identical but distinct
806       //   files.
807       // In the first case, *semantically distinct* DylibFiles will have the
808       // same installName.
809       int64_t &ordinal = ordinalForInstallName[dylibFile->installName];
810       if (ordinal) {
811         dylibFile->ordinal = ordinal;
812         continue;
813       }
814 
815       ordinal = dylibFile->ordinal = dylibOrdinal++;
816       LoadCommandType lcType =
817           dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
818               ? LC_LOAD_WEAK_DYLIB
819               : LC_LOAD_DYLIB;
820       in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName,
821                                               dylibFile->compatibilityVersion,
822                                               dylibFile->currentVersion));
823 
824       if (dylibFile->reexport)
825         in.header->addLoadCommand(
826             make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName));
827     }
828   }
829 
830   if (functionStartsSection)
831     in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection));
832   if (dataInCodeSection)
833     in.header->addLoadCommand(make<LCDataInCode>(dataInCodeSection));
834   if (codeSignatureSection)
835     in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection));
836 
837   const uint32_t MACOS_MAXPATHLEN = 1024;
838   config->headerPad = std::max(
839       config->headerPad, (config->headerPadMaxInstallNames
840                               ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN
841                               : 0));
842 }
843 
844 static size_t getSymbolPriority(const SymbolPriorityEntry &entry,
845                                 const InputFile *f) {
846   // We don't use toString(InputFile *) here because it returns the full path
847   // for object files, and we only want the basename.
848   StringRef filename;
849   if (f->archiveName.empty())
850     filename = path::filename(f->getName());
851   else
852     filename = saver.save(path::filename(f->archiveName) + "(" +
853                           path::filename(f->getName()) + ")");
854   return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile);
855 }
856 
857 // Each section gets assigned the priority of the highest-priority symbol it
858 // contains.
859 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() {
860   DenseMap<const InputSection *, size_t> sectionPriorities;
861 
862   if (config->priorities.empty())
863     return sectionPriorities;
864 
865   auto addSym = [&](Defined &sym) {
866     if (sym.isAbsolute())
867       return;
868 
869     auto it = config->priorities.find(sym.getName());
870     if (it == config->priorities.end())
871       return;
872 
873     SymbolPriorityEntry &entry = it->second;
874     size_t &priority = sectionPriorities[sym.isec];
875     priority =
876         std::max(priority, getSymbolPriority(entry, sym.isec->getFile()));
877   };
878 
879   // TODO: Make sure this handles weak symbols correctly.
880   for (const InputFile *file : inputFiles) {
881     if (isa<ObjFile>(file))
882       for (Symbol *sym : file->symbols)
883         if (auto *d = dyn_cast_or_null<Defined>(sym))
884           addSym(*d);
885   }
886 
887   return sectionPriorities;
888 }
889 
890 // Sorting only can happen once all outputs have been collected. Here we sort
891 // segments, output sections within each segment, and input sections within each
892 // output segment.
893 static void sortSegmentsAndSections() {
894   TimeTraceScope timeScope("Sort segments and sections");
895   sortOutputSegments();
896 
897   DenseMap<const InputSection *, size_t> isecPriorities =
898       buildInputSectionPriorities();
899 
900   uint32_t sectionIndex = 0;
901   for (OutputSegment *seg : outputSegments) {
902     seg->sortOutputSections();
903     for (OutputSection *osec : seg->getSections()) {
904       // Now that the output sections are sorted, assign the final
905       // output section indices.
906       if (!osec->isHidden())
907         osec->index = ++sectionIndex;
908       if (!firstTLVDataSection && isThreadLocalData(osec->flags))
909         firstTLVDataSection = osec;
910 
911       if (!isecPriorities.empty()) {
912         if (auto *merged = dyn_cast<ConcatOutputSection>(osec)) {
913           llvm::stable_sort(merged->inputs,
914                             [&](InputSection *a, InputSection *b) {
915                               return isecPriorities[a] > isecPriorities[b];
916                             });
917         }
918       }
919     }
920   }
921 }
922 
923 template <class LP> void Writer::createOutputSections() {
924   TimeTraceScope timeScope("Create output sections");
925   // First, create hidden sections
926   stringTableSection = make<StringTableSection>();
927   symtabSection = makeSymtabSection<LP>(*stringTableSection);
928   indirectSymtabSection = make<IndirectSymtabSection>();
929   if (config->adhocCodesign)
930     codeSignatureSection = make<CodeSignatureSection>();
931   if (config->emitDataInCodeInfo)
932     dataInCodeSection = make<DataInCodeSection>();
933   if (config->emitFunctionStarts)
934     functionStartsSection = make<FunctionStartsSection>();
935   if (config->emitBitcodeBundle)
936     make<BitcodeBundleSection>();
937 
938   switch (config->outputType) {
939   case MH_EXECUTE:
940     make<PageZeroSection>();
941     break;
942   case MH_DYLIB:
943   case MH_BUNDLE:
944     break;
945   default:
946     llvm_unreachable("unhandled output file type");
947   }
948 
949   // Then add input sections to output sections.
950   for (ConcatInputSection *isec : inputSections) {
951     if (isec->shouldOmitFromOutput())
952       continue;
953     ConcatOutputSection *osec = cast<ConcatOutputSection>(isec->parent);
954     osec->addInput(isec);
955     osec->inputOrder =
956         std::min(osec->inputOrder, static_cast<int>(isec->outSecOff));
957   }
958 
959   // Once all the inputs are added, we can finalize the output section
960   // properties and create the corresponding output segments.
961   for (const auto &it : concatOutputSections) {
962     StringRef segname = it.first.first;
963     ConcatOutputSection *osec = it.second;
964     assert(segname != segment_names::ld);
965     if (osec->isNeeded())
966       getOrCreateOutputSegment(segname)->addOutputSection(osec);
967   }
968 
969   for (SyntheticSection *ssec : syntheticSections) {
970     auto it = concatOutputSections.find({ssec->segname, ssec->name});
971     if (ssec->isNeeded()) {
972       if (it == concatOutputSections.end()) {
973         getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
974       } else {
975         fatal("section from " +
976               toString(it->second->firstSection()->getFile()) +
977               " conflicts with synthetic section " + ssec->segname + "," +
978               ssec->name);
979       }
980     }
981   }
982 
983   // dyld requires __LINKEDIT segment to always exist (even if empty).
984   linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit);
985 }
986 
987 void Writer::finalizeAddresses() {
988   TimeTraceScope timeScope("Finalize addresses");
989   uint64_t pageSize = target->getPageSize();
990   // Ensure that segments (and the sections they contain) are allocated
991   // addresses in ascending order, which dyld requires.
992   //
993   // Note that at this point, __LINKEDIT sections are empty, but we need to
994   // determine addresses of other segments/sections before generating its
995   // contents.
996   for (OutputSegment *seg : outputSegments) {
997     if (seg == linkEditSegment)
998       continue;
999     seg->addr = addr;
1000     assignAddresses(seg);
1001     // codesign / libstuff checks for segment ordering by verifying that
1002     // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before
1003     // (instead of after) computing fileSize to ensure that the segments are
1004     // contiguous. We handle addr / vmSize similarly for the same reason.
1005     fileOff = alignTo(fileOff, pageSize);
1006     addr = alignTo(addr, pageSize);
1007     seg->vmSize = addr - seg->addr;
1008     seg->fileSize = fileOff - seg->fileOff;
1009     seg->assignAddressesToStartEndSymbols();
1010   }
1011 }
1012 
1013 void Writer::finalizeLinkEditSegment() {
1014   TimeTraceScope timeScope("Finalize __LINKEDIT segment");
1015   // Fill __LINKEDIT contents.
1016   std::vector<LinkEditSection *> linkEditSections{
1017       in.rebase,
1018       in.binding,
1019       in.weakBinding,
1020       in.lazyBinding,
1021       in.exports,
1022       symtabSection,
1023       indirectSymtabSection,
1024       dataInCodeSection,
1025       functionStartsSection,
1026   };
1027   parallelForEach(linkEditSections, [](LinkEditSection *osec) {
1028     if (osec)
1029       osec->finalizeContents();
1030   });
1031 
1032   // Now that __LINKEDIT is filled out, do a proper calculation of its
1033   // addresses and offsets.
1034   linkEditSegment->addr = addr;
1035   assignAddresses(linkEditSegment);
1036   // No need to page-align fileOff / addr here since this is the last segment.
1037   linkEditSegment->vmSize = addr - linkEditSegment->addr;
1038   linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff;
1039 }
1040 
1041 void Writer::assignAddresses(OutputSegment *seg) {
1042   seg->fileOff = fileOff;
1043 
1044   for (OutputSection *osec : seg->getSections()) {
1045     if (!osec->isNeeded())
1046       continue;
1047     addr = alignTo(addr, osec->align);
1048     fileOff = alignTo(fileOff, osec->align);
1049     osec->addr = addr;
1050     osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff;
1051     osec->finalize();
1052     osec->assignAddressesToStartEndSymbols();
1053 
1054     addr += osec->getSize();
1055     fileOff += osec->getFileSize();
1056   }
1057 }
1058 
1059 void Writer::openFile() {
1060   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
1061       FileOutputBuffer::create(config->outputFile, fileOff,
1062                                FileOutputBuffer::F_executable);
1063 
1064   if (!bufferOrErr)
1065     error("failed to open " + config->outputFile + ": " +
1066           llvm::toString(bufferOrErr.takeError()));
1067   else
1068     buffer = std::move(*bufferOrErr);
1069 }
1070 
1071 void Writer::writeSections() {
1072   uint8_t *buf = buffer->getBufferStart();
1073   for (const OutputSegment *seg : outputSegments)
1074     for (const OutputSection *osec : seg->getSections())
1075       osec->writeTo(buf + osec->fileOff);
1076 }
1077 
1078 // In order to utilize multiple cores, we first split the buffer into chunks,
1079 // compute a hash for each chunk, and then compute a hash value of the hash
1080 // values.
1081 void Writer::writeUuid() {
1082   TimeTraceScope timeScope("Computing UUID");
1083   ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()};
1084   unsigned chunkCount = parallel::strategy.compute_thread_count() * 10;
1085   // Round-up integer division
1086   size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount;
1087   std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize);
1088   std::vector<uint64_t> hashes(chunks.size());
1089   parallelForEachN(0, chunks.size(),
1090                    [&](size_t i) { hashes[i] = xxHash64(chunks[i]); });
1091   uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()),
1092                               hashes.size() * sizeof(uint64_t)});
1093   uuidCommand->writeUuid(digest);
1094 }
1095 
1096 void Writer::writeCodeSignature() {
1097   if (codeSignatureSection)
1098     codeSignatureSection->writeHashes(buffer->getBufferStart());
1099 }
1100 
1101 void Writer::writeOutputFile() {
1102   TimeTraceScope timeScope("Write output file");
1103   openFile();
1104   if (errorCount())
1105     return;
1106   writeSections();
1107   writeUuid();
1108   writeCodeSignature();
1109 
1110   if (auto e = buffer->commit())
1111     error("failed to write to the output file: " + toString(std::move(e)));
1112 }
1113 
1114 template <class LP> void Writer::run() {
1115   treatSpecialUndefineds();
1116   if (config->entry && !isa<Undefined>(config->entry))
1117     prepareBranchTarget(config->entry);
1118   scanSymbols();
1119   scanRelocations();
1120 
1121   // Do not proceed if there was an undefined symbol.
1122   if (errorCount())
1123     return;
1124 
1125   if (in.stubHelper->isNeeded())
1126     in.stubHelper->setup();
1127   createOutputSections<LP>();
1128 
1129   // After this point, we create no new segments; HOWEVER, we might
1130   // yet create branch-range extension thunks for architectures whose
1131   // hardware call instructions have limited range, e.g., ARM(64).
1132   // The thunks are created as InputSections interspersed among
1133   // the ordinary __TEXT,_text InputSections.
1134   sortSegmentsAndSections();
1135   createLoadCommands<LP>();
1136   finalizeAddresses();
1137   finalizeLinkEditSegment();
1138   writeMapFile();
1139   writeOutputFile();
1140 }
1141 
1142 template <class LP> void macho::writeResult() { Writer().run<LP>(); }
1143 
1144 void macho::createSyntheticSections() {
1145   in.header = make<MachHeaderSection>();
1146   if (config->dedupLiterals) {
1147     in.cStringSection = make<DeduplicatedCStringSection>();
1148   } else {
1149     in.cStringSection = make<CStringSection>();
1150   }
1151   in.wordLiteralSection =
1152       config->dedupLiterals ? make<WordLiteralSection>() : nullptr;
1153   in.rebase = make<RebaseSection>();
1154   in.binding = make<BindingSection>();
1155   in.weakBinding = make<WeakBindingSection>();
1156   in.lazyBinding = make<LazyBindingSection>();
1157   in.exports = make<ExportSection>();
1158   in.got = make<GotSection>();
1159   in.tlvPointers = make<TlvPointerSection>();
1160   in.lazyPointers = make<LazyPointerSection>();
1161   in.stubs = make<StubsSection>();
1162   in.stubHelper = make<StubHelperSection>();
1163   in.unwindInfo = makeUnwindInfoSection();
1164 
1165   // This section contains space for just a single word, and will be used by
1166   // dyld to cache an address to the image loader it uses.
1167   uint8_t *arr = bAlloc.Allocate<uint8_t>(target->wordSize);
1168   memset(arr, 0, target->wordSize);
1169   in.imageLoaderCache = make<ConcatInputSection>(
1170       segment_names::data, section_names::data, /*file=*/nullptr,
1171       ArrayRef<uint8_t>{arr, target->wordSize},
1172       /*align=*/target->wordSize, /*flags=*/S_REGULAR);
1173   // References from dyld are not visible to us, so ensure this section is
1174   // always treated as live.
1175   in.imageLoaderCache->live = true;
1176 }
1177 
1178 OutputSection *macho::firstTLVDataSection = nullptr;
1179 
1180 template void macho::writeResult<LP64>();
1181 template void macho::writeResult<ILP32>();
1182