xref: /freebsd-src/contrib/llvm-project/lld/MachO/Writer.cpp (revision f3fd488f1e19a3d09c4bdcece893901de4f49cdd)
15ffd83dbSDimitry Andric //===- Writer.cpp ---------------------------------------------------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #include "Writer.h"
10fe6060f1SDimitry Andric #include "ConcatOutputSection.h"
115ffd83dbSDimitry Andric #include "Config.h"
125ffd83dbSDimitry Andric #include "InputFiles.h"
135ffd83dbSDimitry Andric #include "InputSection.h"
14fe6060f1SDimitry Andric #include "MapFile.h"
155ffd83dbSDimitry Andric #include "OutputSection.h"
165ffd83dbSDimitry Andric #include "OutputSegment.h"
1704eeddc0SDimitry Andric #include "SectionPriorities.h"
185ffd83dbSDimitry Andric #include "SymbolTable.h"
195ffd83dbSDimitry Andric #include "Symbols.h"
205ffd83dbSDimitry Andric #include "SyntheticSections.h"
215ffd83dbSDimitry Andric #include "Target.h"
22e8d8bef9SDimitry Andric #include "UnwindInfoSection.h"
2381ad6265SDimitry Andric #include "llvm/Support/Parallel.h"
245ffd83dbSDimitry Andric 
25fe6060f1SDimitry Andric #include "lld/Common/Arrays.h"
2604eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
275ffd83dbSDimitry Andric #include "llvm/BinaryFormat/MachO.h"
28e8d8bef9SDimitry Andric #include "llvm/Config/llvm-config.h"
295ffd83dbSDimitry Andric #include "llvm/Support/LEB128.h"
305ffd83dbSDimitry Andric #include "llvm/Support/MathExtras.h"
31fe6060f1SDimitry Andric #include "llvm/Support/Parallel.h"
325ffd83dbSDimitry Andric #include "llvm/Support/Path.h"
330eae32dcSDimitry Andric #include "llvm/Support/ThreadPool.h"
34fe6060f1SDimitry Andric #include "llvm/Support/TimeProfiler.h"
35e8d8bef9SDimitry Andric #include "llvm/Support/xxhash.h"
36e8d8bef9SDimitry Andric 
37e8d8bef9SDimitry Andric #include <algorithm>
385ffd83dbSDimitry Andric 
395ffd83dbSDimitry Andric using namespace llvm;
405ffd83dbSDimitry Andric using namespace llvm::MachO;
41e8d8bef9SDimitry Andric using namespace llvm::sys;
425ffd83dbSDimitry Andric using namespace lld;
435ffd83dbSDimitry Andric using namespace lld::macho;
445ffd83dbSDimitry Andric 
455ffd83dbSDimitry Andric namespace {
46e8d8bef9SDimitry Andric class LCUuid;
475ffd83dbSDimitry Andric 
485ffd83dbSDimitry Andric class Writer {
495ffd83dbSDimitry Andric public:
505ffd83dbSDimitry Andric   Writer() : buffer(errorHandler().outputBuffer) {}
515ffd83dbSDimitry Andric 
52fe6060f1SDimitry Andric   void treatSpecialUndefineds();
535ffd83dbSDimitry Andric   void scanRelocations();
54e8d8bef9SDimitry Andric   void scanSymbols();
55fe6060f1SDimitry Andric   template <class LP> void createOutputSections();
56fe6060f1SDimitry Andric   template <class LP> void createLoadCommands();
57fe6060f1SDimitry Andric   void finalizeAddresses();
58fe6060f1SDimitry Andric   void finalizeLinkEditSegment();
595ffd83dbSDimitry Andric   void assignAddresses(OutputSegment *);
605ffd83dbSDimitry Andric 
615ffd83dbSDimitry Andric   void openFile();
625ffd83dbSDimitry Andric   void writeSections();
63e8d8bef9SDimitry Andric   void writeUuid();
64fe6060f1SDimitry Andric   void writeCodeSignature();
65fe6060f1SDimitry Andric   void writeOutputFile();
665ffd83dbSDimitry Andric 
67fe6060f1SDimitry Andric   template <class LP> void run();
685ffd83dbSDimitry Andric 
690eae32dcSDimitry Andric   ThreadPool threadPool;
705ffd83dbSDimitry Andric   std::unique_ptr<FileOutputBuffer> &buffer;
715ffd83dbSDimitry Andric   uint64_t addr = 0;
725ffd83dbSDimitry Andric   uint64_t fileOff = 0;
73e8d8bef9SDimitry Andric   MachHeaderSection *header = nullptr;
745ffd83dbSDimitry Andric   StringTableSection *stringTableSection = nullptr;
755ffd83dbSDimitry Andric   SymtabSection *symtabSection = nullptr;
76e8d8bef9SDimitry Andric   IndirectSymtabSection *indirectSymtabSection = nullptr;
77fe6060f1SDimitry Andric   CodeSignatureSection *codeSignatureSection = nullptr;
78fe6060f1SDimitry Andric   DataInCodeSection *dataInCodeSection = nullptr;
79fe6060f1SDimitry Andric   FunctionStartsSection *functionStartsSection = nullptr;
80fe6060f1SDimitry Andric 
81e8d8bef9SDimitry Andric   LCUuid *uuidCommand = nullptr;
82fe6060f1SDimitry Andric   OutputSegment *linkEditSegment = nullptr;
835ffd83dbSDimitry Andric };
845ffd83dbSDimitry Andric 
855ffd83dbSDimitry Andric // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information.
86fe6060f1SDimitry Andric class LCDyldInfo final : public LoadCommand {
875ffd83dbSDimitry Andric public:
88e8d8bef9SDimitry Andric   LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection,
89e8d8bef9SDimitry Andric              WeakBindingSection *weakBindingSection,
905ffd83dbSDimitry Andric              LazyBindingSection *lazyBindingSection,
915ffd83dbSDimitry Andric              ExportSection *exportSection)
92e8d8bef9SDimitry Andric       : rebaseSection(rebaseSection), bindingSection(bindingSection),
93e8d8bef9SDimitry Andric         weakBindingSection(weakBindingSection),
94e8d8bef9SDimitry Andric         lazyBindingSection(lazyBindingSection), exportSection(exportSection) {}
955ffd83dbSDimitry Andric 
965ffd83dbSDimitry Andric   uint32_t getSize() const override { return sizeof(dyld_info_command); }
975ffd83dbSDimitry Andric 
985ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
995ffd83dbSDimitry Andric     auto *c = reinterpret_cast<dyld_info_command *>(buf);
1005ffd83dbSDimitry Andric     c->cmd = LC_DYLD_INFO_ONLY;
1015ffd83dbSDimitry Andric     c->cmdsize = getSize();
102e8d8bef9SDimitry Andric     if (rebaseSection->isNeeded()) {
103e8d8bef9SDimitry Andric       c->rebase_off = rebaseSection->fileOff;
104e8d8bef9SDimitry Andric       c->rebase_size = rebaseSection->getFileSize();
105e8d8bef9SDimitry Andric     }
1065ffd83dbSDimitry Andric     if (bindingSection->isNeeded()) {
1075ffd83dbSDimitry Andric       c->bind_off = bindingSection->fileOff;
1085ffd83dbSDimitry Andric       c->bind_size = bindingSection->getFileSize();
1095ffd83dbSDimitry Andric     }
110e8d8bef9SDimitry Andric     if (weakBindingSection->isNeeded()) {
111e8d8bef9SDimitry Andric       c->weak_bind_off = weakBindingSection->fileOff;
112e8d8bef9SDimitry Andric       c->weak_bind_size = weakBindingSection->getFileSize();
113e8d8bef9SDimitry Andric     }
1145ffd83dbSDimitry Andric     if (lazyBindingSection->isNeeded()) {
1155ffd83dbSDimitry Andric       c->lazy_bind_off = lazyBindingSection->fileOff;
1165ffd83dbSDimitry Andric       c->lazy_bind_size = lazyBindingSection->getFileSize();
1175ffd83dbSDimitry Andric     }
1185ffd83dbSDimitry Andric     if (exportSection->isNeeded()) {
1195ffd83dbSDimitry Andric       c->export_off = exportSection->fileOff;
1205ffd83dbSDimitry Andric       c->export_size = exportSection->getFileSize();
1215ffd83dbSDimitry Andric     }
1225ffd83dbSDimitry Andric   }
1235ffd83dbSDimitry Andric 
124e8d8bef9SDimitry Andric   RebaseSection *rebaseSection;
1255ffd83dbSDimitry Andric   BindingSection *bindingSection;
126e8d8bef9SDimitry Andric   WeakBindingSection *weakBindingSection;
1275ffd83dbSDimitry Andric   LazyBindingSection *lazyBindingSection;
1285ffd83dbSDimitry Andric   ExportSection *exportSection;
1295ffd83dbSDimitry Andric };
1305ffd83dbSDimitry Andric 
131fe6060f1SDimitry Andric class LCSubFramework final : public LoadCommand {
132fe6060f1SDimitry Andric public:
133fe6060f1SDimitry Andric   LCSubFramework(StringRef umbrella) : umbrella(umbrella) {}
134fe6060f1SDimitry Andric 
135fe6060f1SDimitry Andric   uint32_t getSize() const override {
136fe6060f1SDimitry Andric     return alignTo(sizeof(sub_framework_command) + umbrella.size() + 1,
137fe6060f1SDimitry Andric                    target->wordSize);
138fe6060f1SDimitry Andric   }
139fe6060f1SDimitry Andric 
140fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
141fe6060f1SDimitry Andric     auto *c = reinterpret_cast<sub_framework_command *>(buf);
142fe6060f1SDimitry Andric     buf += sizeof(sub_framework_command);
143fe6060f1SDimitry Andric 
144fe6060f1SDimitry Andric     c->cmd = LC_SUB_FRAMEWORK;
145fe6060f1SDimitry Andric     c->cmdsize = getSize();
146fe6060f1SDimitry Andric     c->umbrella = sizeof(sub_framework_command);
147fe6060f1SDimitry Andric 
148fe6060f1SDimitry Andric     memcpy(buf, umbrella.data(), umbrella.size());
149fe6060f1SDimitry Andric     buf[umbrella.size()] = '\0';
150fe6060f1SDimitry Andric   }
151fe6060f1SDimitry Andric 
152fe6060f1SDimitry Andric private:
153fe6060f1SDimitry Andric   const StringRef umbrella;
154fe6060f1SDimitry Andric };
155fe6060f1SDimitry Andric 
156fe6060f1SDimitry Andric class LCFunctionStarts final : public LoadCommand {
157fe6060f1SDimitry Andric public:
158fe6060f1SDimitry Andric   explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection)
159fe6060f1SDimitry Andric       : functionStartsSection(functionStartsSection) {}
160fe6060f1SDimitry Andric 
161fe6060f1SDimitry Andric   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
162fe6060f1SDimitry Andric 
163fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
164fe6060f1SDimitry Andric     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
165fe6060f1SDimitry Andric     c->cmd = LC_FUNCTION_STARTS;
166fe6060f1SDimitry Andric     c->cmdsize = getSize();
167fe6060f1SDimitry Andric     c->dataoff = functionStartsSection->fileOff;
168fe6060f1SDimitry Andric     c->datasize = functionStartsSection->getFileSize();
169fe6060f1SDimitry Andric   }
170fe6060f1SDimitry Andric 
171fe6060f1SDimitry Andric private:
172fe6060f1SDimitry Andric   FunctionStartsSection *functionStartsSection;
173fe6060f1SDimitry Andric };
174fe6060f1SDimitry Andric 
175fe6060f1SDimitry Andric class LCDataInCode final : public LoadCommand {
176fe6060f1SDimitry Andric public:
177fe6060f1SDimitry Andric   explicit LCDataInCode(DataInCodeSection *dataInCodeSection)
178fe6060f1SDimitry Andric       : dataInCodeSection(dataInCodeSection) {}
179fe6060f1SDimitry Andric 
180fe6060f1SDimitry Andric   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
181fe6060f1SDimitry Andric 
182fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
183fe6060f1SDimitry Andric     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
184fe6060f1SDimitry Andric     c->cmd = LC_DATA_IN_CODE;
185fe6060f1SDimitry Andric     c->cmdsize = getSize();
186fe6060f1SDimitry Andric     c->dataoff = dataInCodeSection->fileOff;
187fe6060f1SDimitry Andric     c->datasize = dataInCodeSection->getFileSize();
188fe6060f1SDimitry Andric   }
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric private:
191fe6060f1SDimitry Andric   DataInCodeSection *dataInCodeSection;
192fe6060f1SDimitry Andric };
193fe6060f1SDimitry Andric 
194fe6060f1SDimitry Andric class LCDysymtab final : public LoadCommand {
1955ffd83dbSDimitry Andric public:
196e8d8bef9SDimitry Andric   LCDysymtab(SymtabSection *symtabSection,
197e8d8bef9SDimitry Andric              IndirectSymtabSection *indirectSymtabSection)
198e8d8bef9SDimitry Andric       : symtabSection(symtabSection),
199e8d8bef9SDimitry Andric         indirectSymtabSection(indirectSymtabSection) {}
200e8d8bef9SDimitry Andric 
2015ffd83dbSDimitry Andric   uint32_t getSize() const override { return sizeof(dysymtab_command); }
2025ffd83dbSDimitry Andric 
2035ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
2045ffd83dbSDimitry Andric     auto *c = reinterpret_cast<dysymtab_command *>(buf);
2055ffd83dbSDimitry Andric     c->cmd = LC_DYSYMTAB;
2065ffd83dbSDimitry Andric     c->cmdsize = getSize();
207e8d8bef9SDimitry Andric 
208e8d8bef9SDimitry Andric     c->ilocalsym = 0;
209e8d8bef9SDimitry Andric     c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols();
210e8d8bef9SDimitry Andric     c->nextdefsym = symtabSection->getNumExternalSymbols();
211e8d8bef9SDimitry Andric     c->iundefsym = c->iextdefsym + c->nextdefsym;
212e8d8bef9SDimitry Andric     c->nundefsym = symtabSection->getNumUndefinedSymbols();
213e8d8bef9SDimitry Andric 
214e8d8bef9SDimitry Andric     c->indirectsymoff = indirectSymtabSection->fileOff;
215e8d8bef9SDimitry Andric     c->nindirectsyms = indirectSymtabSection->getNumSymbols();
2165ffd83dbSDimitry Andric   }
217e8d8bef9SDimitry Andric 
218e8d8bef9SDimitry Andric   SymtabSection *symtabSection;
219e8d8bef9SDimitry Andric   IndirectSymtabSection *indirectSymtabSection;
2205ffd83dbSDimitry Andric };
2215ffd83dbSDimitry Andric 
222fe6060f1SDimitry Andric template <class LP> class LCSegment final : public LoadCommand {
2235ffd83dbSDimitry Andric public:
2245ffd83dbSDimitry Andric   LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {}
2255ffd83dbSDimitry Andric 
2265ffd83dbSDimitry Andric   uint32_t getSize() const override {
227fe6060f1SDimitry Andric     return sizeof(typename LP::segment_command) +
228fe6060f1SDimitry Andric            seg->numNonHiddenSections() * sizeof(typename LP::section);
2295ffd83dbSDimitry Andric   }
2305ffd83dbSDimitry Andric 
2315ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
232fe6060f1SDimitry Andric     using SegmentCommand = typename LP::segment_command;
233349cc55cSDimitry Andric     using SectionHeader = typename LP::section;
2345ffd83dbSDimitry Andric 
235fe6060f1SDimitry Andric     auto *c = reinterpret_cast<SegmentCommand *>(buf);
236fe6060f1SDimitry Andric     buf += sizeof(SegmentCommand);
237fe6060f1SDimitry Andric 
238fe6060f1SDimitry Andric     c->cmd = LP::segmentLCType;
2395ffd83dbSDimitry Andric     c->cmdsize = getSize();
2405ffd83dbSDimitry Andric     memcpy(c->segname, name.data(), name.size());
2415ffd83dbSDimitry Andric     c->fileoff = seg->fileOff;
2425ffd83dbSDimitry Andric     c->maxprot = seg->maxProt;
2435ffd83dbSDimitry Andric     c->initprot = seg->initProt;
2445ffd83dbSDimitry Andric 
245fe6060f1SDimitry Andric     c->vmaddr = seg->addr;
246fe6060f1SDimitry Andric     c->vmsize = seg->vmSize;
247fe6060f1SDimitry Andric     c->filesize = seg->fileSize;
2485ffd83dbSDimitry Andric     c->nsects = seg->numNonHiddenSections();
2495ffd83dbSDimitry Andric 
250fe6060f1SDimitry Andric     for (const OutputSection *osec : seg->getSections()) {
2515ffd83dbSDimitry Andric       if (osec->isHidden())
2525ffd83dbSDimitry Andric         continue;
2535ffd83dbSDimitry Andric 
254349cc55cSDimitry Andric       auto *sectHdr = reinterpret_cast<SectionHeader *>(buf);
255349cc55cSDimitry Andric       buf += sizeof(SectionHeader);
2565ffd83dbSDimitry Andric 
2575ffd83dbSDimitry Andric       memcpy(sectHdr->sectname, osec->name.data(), osec->name.size());
2585ffd83dbSDimitry Andric       memcpy(sectHdr->segname, name.data(), name.size());
2595ffd83dbSDimitry Andric 
2605ffd83dbSDimitry Andric       sectHdr->addr = osec->addr;
2615ffd83dbSDimitry Andric       sectHdr->offset = osec->fileOff;
2625ffd83dbSDimitry Andric       sectHdr->align = Log2_32(osec->align);
2635ffd83dbSDimitry Andric       sectHdr->flags = osec->flags;
2645ffd83dbSDimitry Andric       sectHdr->size = osec->getSize();
265e8d8bef9SDimitry Andric       sectHdr->reserved1 = osec->reserved1;
266e8d8bef9SDimitry Andric       sectHdr->reserved2 = osec->reserved2;
2675ffd83dbSDimitry Andric     }
2685ffd83dbSDimitry Andric   }
2695ffd83dbSDimitry Andric 
2705ffd83dbSDimitry Andric private:
2715ffd83dbSDimitry Andric   StringRef name;
2725ffd83dbSDimitry Andric   OutputSegment *seg;
2735ffd83dbSDimitry Andric };
2745ffd83dbSDimitry Andric 
275fe6060f1SDimitry Andric class LCMain final : public LoadCommand {
276fe6060f1SDimitry Andric   uint32_t getSize() const override {
277fe6060f1SDimitry Andric     return sizeof(structs::entry_point_command);
278fe6060f1SDimitry Andric   }
2795ffd83dbSDimitry Andric 
2805ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
281fe6060f1SDimitry Andric     auto *c = reinterpret_cast<structs::entry_point_command *>(buf);
2825ffd83dbSDimitry Andric     c->cmd = LC_MAIN;
2835ffd83dbSDimitry Andric     c->cmdsize = getSize();
284e8d8bef9SDimitry Andric 
285e8d8bef9SDimitry Andric     if (config->entry->isInStubs())
286e8d8bef9SDimitry Andric       c->entryoff =
287e8d8bef9SDimitry Andric           in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
288e8d8bef9SDimitry Andric     else
289fe6060f1SDimitry Andric       c->entryoff = config->entry->getVA() - in.header->addr;
290e8d8bef9SDimitry Andric 
2915ffd83dbSDimitry Andric     c->stacksize = 0;
2925ffd83dbSDimitry Andric   }
2935ffd83dbSDimitry Andric };
2945ffd83dbSDimitry Andric 
295fe6060f1SDimitry Andric class LCSymtab final : public LoadCommand {
2965ffd83dbSDimitry Andric public:
2975ffd83dbSDimitry Andric   LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection)
2985ffd83dbSDimitry Andric       : symtabSection(symtabSection), stringTableSection(stringTableSection) {}
2995ffd83dbSDimitry Andric 
3005ffd83dbSDimitry Andric   uint32_t getSize() const override { return sizeof(symtab_command); }
3015ffd83dbSDimitry Andric 
3025ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
3035ffd83dbSDimitry Andric     auto *c = reinterpret_cast<symtab_command *>(buf);
3045ffd83dbSDimitry Andric     c->cmd = LC_SYMTAB;
3055ffd83dbSDimitry Andric     c->cmdsize = getSize();
3065ffd83dbSDimitry Andric     c->symoff = symtabSection->fileOff;
3075ffd83dbSDimitry Andric     c->nsyms = symtabSection->getNumSymbols();
3085ffd83dbSDimitry Andric     c->stroff = stringTableSection->fileOff;
3095ffd83dbSDimitry Andric     c->strsize = stringTableSection->getFileSize();
3105ffd83dbSDimitry Andric   }
3115ffd83dbSDimitry Andric 
3125ffd83dbSDimitry Andric   SymtabSection *symtabSection = nullptr;
3135ffd83dbSDimitry Andric   StringTableSection *stringTableSection = nullptr;
3145ffd83dbSDimitry Andric };
3155ffd83dbSDimitry Andric 
3165ffd83dbSDimitry Andric // There are several dylib load commands that share the same structure:
3175ffd83dbSDimitry Andric //   * LC_LOAD_DYLIB
3185ffd83dbSDimitry Andric //   * LC_ID_DYLIB
3195ffd83dbSDimitry Andric //   * LC_REEXPORT_DYLIB
320fe6060f1SDimitry Andric class LCDylib final : public LoadCommand {
3215ffd83dbSDimitry Andric public:
322e8d8bef9SDimitry Andric   LCDylib(LoadCommandType type, StringRef path,
323e8d8bef9SDimitry Andric           uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0)
324e8d8bef9SDimitry Andric       : type(type), path(path), compatibilityVersion(compatibilityVersion),
325e8d8bef9SDimitry Andric         currentVersion(currentVersion) {
326e8d8bef9SDimitry Andric     instanceCount++;
327e8d8bef9SDimitry Andric   }
3285ffd83dbSDimitry Andric 
3295ffd83dbSDimitry Andric   uint32_t getSize() const override {
3305ffd83dbSDimitry Andric     return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
3315ffd83dbSDimitry Andric   }
3325ffd83dbSDimitry Andric 
3335ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
3345ffd83dbSDimitry Andric     auto *c = reinterpret_cast<dylib_command *>(buf);
3355ffd83dbSDimitry Andric     buf += sizeof(dylib_command);
3365ffd83dbSDimitry Andric 
3375ffd83dbSDimitry Andric     c->cmd = type;
3385ffd83dbSDimitry Andric     c->cmdsize = getSize();
3395ffd83dbSDimitry Andric     c->dylib.name = sizeof(dylib_command);
340e8d8bef9SDimitry Andric     c->dylib.timestamp = 0;
341e8d8bef9SDimitry Andric     c->dylib.compatibility_version = compatibilityVersion;
342e8d8bef9SDimitry Andric     c->dylib.current_version = currentVersion;
3435ffd83dbSDimitry Andric 
3445ffd83dbSDimitry Andric     memcpy(buf, path.data(), path.size());
3455ffd83dbSDimitry Andric     buf[path.size()] = '\0';
3465ffd83dbSDimitry Andric   }
3475ffd83dbSDimitry Andric 
348e8d8bef9SDimitry Andric   static uint32_t getInstanceCount() { return instanceCount; }
349349cc55cSDimitry Andric   static void resetInstanceCount() { instanceCount = 0; }
350e8d8bef9SDimitry Andric 
3515ffd83dbSDimitry Andric private:
3525ffd83dbSDimitry Andric   LoadCommandType type;
3535ffd83dbSDimitry Andric   StringRef path;
354e8d8bef9SDimitry Andric   uint32_t compatibilityVersion;
355e8d8bef9SDimitry Andric   uint32_t currentVersion;
356e8d8bef9SDimitry Andric   static uint32_t instanceCount;
3575ffd83dbSDimitry Andric };
3585ffd83dbSDimitry Andric 
359e8d8bef9SDimitry Andric uint32_t LCDylib::instanceCount = 0;
360e8d8bef9SDimitry Andric 
361fe6060f1SDimitry Andric class LCLoadDylinker final : public LoadCommand {
3625ffd83dbSDimitry Andric public:
3635ffd83dbSDimitry Andric   uint32_t getSize() const override {
3645ffd83dbSDimitry Andric     return alignTo(sizeof(dylinker_command) + path.size() + 1, 8);
3655ffd83dbSDimitry Andric   }
3665ffd83dbSDimitry Andric 
3675ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override {
3685ffd83dbSDimitry Andric     auto *c = reinterpret_cast<dylinker_command *>(buf);
3695ffd83dbSDimitry Andric     buf += sizeof(dylinker_command);
3705ffd83dbSDimitry Andric 
3715ffd83dbSDimitry Andric     c->cmd = LC_LOAD_DYLINKER;
3725ffd83dbSDimitry Andric     c->cmdsize = getSize();
3735ffd83dbSDimitry Andric     c->name = sizeof(dylinker_command);
3745ffd83dbSDimitry Andric 
3755ffd83dbSDimitry Andric     memcpy(buf, path.data(), path.size());
3765ffd83dbSDimitry Andric     buf[path.size()] = '\0';
3775ffd83dbSDimitry Andric   }
3785ffd83dbSDimitry Andric 
3795ffd83dbSDimitry Andric private:
3805ffd83dbSDimitry Andric   // Recent versions of Darwin won't run any binary that has dyld at a
3815ffd83dbSDimitry Andric   // different location.
3825ffd83dbSDimitry Andric   const StringRef path = "/usr/lib/dyld";
3835ffd83dbSDimitry Andric };
384e8d8bef9SDimitry Andric 
385fe6060f1SDimitry Andric class LCRPath final : public LoadCommand {
386e8d8bef9SDimitry Andric public:
387fe6060f1SDimitry Andric   explicit LCRPath(StringRef path) : path(path) {}
388e8d8bef9SDimitry Andric 
389e8d8bef9SDimitry Andric   uint32_t getSize() const override {
390fe6060f1SDimitry Andric     return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize);
391e8d8bef9SDimitry Andric   }
392e8d8bef9SDimitry Andric 
393e8d8bef9SDimitry Andric   void writeTo(uint8_t *buf) const override {
394e8d8bef9SDimitry Andric     auto *c = reinterpret_cast<rpath_command *>(buf);
395e8d8bef9SDimitry Andric     buf += sizeof(rpath_command);
396e8d8bef9SDimitry Andric 
397e8d8bef9SDimitry Andric     c->cmd = LC_RPATH;
398e8d8bef9SDimitry Andric     c->cmdsize = getSize();
399e8d8bef9SDimitry Andric     c->path = sizeof(rpath_command);
400e8d8bef9SDimitry Andric 
401e8d8bef9SDimitry Andric     memcpy(buf, path.data(), path.size());
402e8d8bef9SDimitry Andric     buf[path.size()] = '\0';
403e8d8bef9SDimitry Andric   }
404e8d8bef9SDimitry Andric 
405e8d8bef9SDimitry Andric private:
406e8d8bef9SDimitry Andric   StringRef path;
407e8d8bef9SDimitry Andric };
408e8d8bef9SDimitry Andric 
409fe6060f1SDimitry Andric class LCMinVersion final : public LoadCommand {
410e8d8bef9SDimitry Andric public:
411fe6060f1SDimitry Andric   explicit LCMinVersion(const PlatformInfo &platformInfo)
412fe6060f1SDimitry Andric       : platformInfo(platformInfo) {}
413fe6060f1SDimitry Andric 
414fe6060f1SDimitry Andric   uint32_t getSize() const override { return sizeof(version_min_command); }
415fe6060f1SDimitry Andric 
416fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
417fe6060f1SDimitry Andric     auto *c = reinterpret_cast<version_min_command *>(buf);
418fe6060f1SDimitry Andric     switch (platformInfo.target.Platform) {
41904eeddc0SDimitry Andric     case PLATFORM_MACOS:
420fe6060f1SDimitry Andric       c->cmd = LC_VERSION_MIN_MACOSX;
421fe6060f1SDimitry Andric       break;
42204eeddc0SDimitry Andric     case PLATFORM_IOS:
42304eeddc0SDimitry Andric     case PLATFORM_IOSSIMULATOR:
424fe6060f1SDimitry Andric       c->cmd = LC_VERSION_MIN_IPHONEOS;
425fe6060f1SDimitry Andric       break;
42604eeddc0SDimitry Andric     case PLATFORM_TVOS:
42704eeddc0SDimitry Andric     case PLATFORM_TVOSSIMULATOR:
428fe6060f1SDimitry Andric       c->cmd = LC_VERSION_MIN_TVOS;
429fe6060f1SDimitry Andric       break;
43004eeddc0SDimitry Andric     case PLATFORM_WATCHOS:
43104eeddc0SDimitry Andric     case PLATFORM_WATCHOSSIMULATOR:
432fe6060f1SDimitry Andric       c->cmd = LC_VERSION_MIN_WATCHOS;
433fe6060f1SDimitry Andric       break;
434fe6060f1SDimitry Andric     default:
435fe6060f1SDimitry Andric       llvm_unreachable("invalid platform");
436fe6060f1SDimitry Andric       break;
437fe6060f1SDimitry Andric     }
438fe6060f1SDimitry Andric     c->cmdsize = getSize();
439fe6060f1SDimitry Andric     c->version = encodeVersion(platformInfo.minimum);
440fe6060f1SDimitry Andric     c->sdk = encodeVersion(platformInfo.sdk);
441fe6060f1SDimitry Andric   }
442fe6060f1SDimitry Andric 
443fe6060f1SDimitry Andric private:
444fe6060f1SDimitry Andric   const PlatformInfo &platformInfo;
445fe6060f1SDimitry Andric };
446fe6060f1SDimitry Andric 
447fe6060f1SDimitry Andric class LCBuildVersion final : public LoadCommand {
448fe6060f1SDimitry Andric public:
449fe6060f1SDimitry Andric   explicit LCBuildVersion(const PlatformInfo &platformInfo)
450fe6060f1SDimitry Andric       : platformInfo(platformInfo) {}
451e8d8bef9SDimitry Andric 
452e8d8bef9SDimitry Andric   const int ntools = 1;
453e8d8bef9SDimitry Andric 
454e8d8bef9SDimitry Andric   uint32_t getSize() const override {
455e8d8bef9SDimitry Andric     return sizeof(build_version_command) + ntools * sizeof(build_tool_version);
456e8d8bef9SDimitry Andric   }
457e8d8bef9SDimitry Andric 
458e8d8bef9SDimitry Andric   void writeTo(uint8_t *buf) const override {
459e8d8bef9SDimitry Andric     auto *c = reinterpret_cast<build_version_command *>(buf);
460e8d8bef9SDimitry Andric     c->cmd = LC_BUILD_VERSION;
461e8d8bef9SDimitry Andric     c->cmdsize = getSize();
46281ad6265SDimitry Andric 
463fe6060f1SDimitry Andric     c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
464fe6060f1SDimitry Andric     c->minos = encodeVersion(platformInfo.minimum);
465fe6060f1SDimitry Andric     c->sdk = encodeVersion(platformInfo.sdk);
46681ad6265SDimitry Andric 
467e8d8bef9SDimitry Andric     c->ntools = ntools;
468e8d8bef9SDimitry Andric     auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
469e8d8bef9SDimitry Andric     t->tool = TOOL_LD;
470fe6060f1SDimitry Andric     t->version = encodeVersion(VersionTuple(
471fe6060f1SDimitry Andric         LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH));
472e8d8bef9SDimitry Andric   }
473e8d8bef9SDimitry Andric 
474fe6060f1SDimitry Andric private:
475fe6060f1SDimitry Andric   const PlatformInfo &platformInfo;
476e8d8bef9SDimitry Andric };
477e8d8bef9SDimitry Andric 
478e8d8bef9SDimitry Andric // Stores a unique identifier for the output file based on an MD5 hash of its
479e8d8bef9SDimitry Andric // contents. In order to hash the contents, we must first write them, but
480e8d8bef9SDimitry Andric // LC_UUID itself must be part of the written contents in order for all the
481e8d8bef9SDimitry Andric // offsets to be calculated correctly. We resolve this circular paradox by
482e8d8bef9SDimitry Andric // first writing an LC_UUID with an all-zero UUID, then updating the UUID with
483e8d8bef9SDimitry Andric // its real value later.
484fe6060f1SDimitry Andric class LCUuid final : public LoadCommand {
485e8d8bef9SDimitry Andric public:
486e8d8bef9SDimitry Andric   uint32_t getSize() const override { return sizeof(uuid_command); }
487e8d8bef9SDimitry Andric 
488e8d8bef9SDimitry Andric   void writeTo(uint8_t *buf) const override {
489e8d8bef9SDimitry Andric     auto *c = reinterpret_cast<uuid_command *>(buf);
490e8d8bef9SDimitry Andric     c->cmd = LC_UUID;
491e8d8bef9SDimitry Andric     c->cmdsize = getSize();
492e8d8bef9SDimitry Andric     uuidBuf = c->uuid;
493e8d8bef9SDimitry Andric   }
494e8d8bef9SDimitry Andric 
495e8d8bef9SDimitry Andric   void writeUuid(uint64_t digest) const {
496e8d8bef9SDimitry Andric     // xxhash only gives us 8 bytes, so put some fixed data in the other half.
497e8d8bef9SDimitry Andric     static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size");
498e8d8bef9SDimitry Andric     memcpy(uuidBuf, "LLD\xa1UU1D", 8);
499e8d8bef9SDimitry Andric     memcpy(uuidBuf + 8, &digest, 8);
500e8d8bef9SDimitry Andric 
501e8d8bef9SDimitry Andric     // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in
502e8d8bef9SDimitry Andric     // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't
503e8d8bef9SDimitry Andric     // want to lose bits of the digest in byte 8, so swap that with a byte of
504e8d8bef9SDimitry Andric     // fixed data that happens to have the right bits set.
505e8d8bef9SDimitry Andric     std::swap(uuidBuf[3], uuidBuf[8]);
506e8d8bef9SDimitry Andric 
507e8d8bef9SDimitry Andric     // Claim that this is an MD5-based hash. It isn't, but this signals that
508e8d8bef9SDimitry Andric     // this is not a time-based and not a random hash. MD5 seems like the least
509e8d8bef9SDimitry Andric     // bad lie we can put here.
510e8d8bef9SDimitry Andric     assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3");
511e8d8bef9SDimitry Andric     assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2");
512e8d8bef9SDimitry Andric   }
513e8d8bef9SDimitry Andric 
514e8d8bef9SDimitry Andric   mutable uint8_t *uuidBuf;
515e8d8bef9SDimitry Andric };
516e8d8bef9SDimitry Andric 
517fe6060f1SDimitry Andric template <class LP> class LCEncryptionInfo final : public LoadCommand {
518fe6060f1SDimitry Andric public:
519fe6060f1SDimitry Andric   uint32_t getSize() const override {
520fe6060f1SDimitry Andric     return sizeof(typename LP::encryption_info_command);
521fe6060f1SDimitry Andric   }
522fe6060f1SDimitry Andric 
523fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
524fe6060f1SDimitry Andric     using EncryptionInfo = typename LP::encryption_info_command;
525fe6060f1SDimitry Andric     auto *c = reinterpret_cast<EncryptionInfo *>(buf);
526fe6060f1SDimitry Andric     buf += sizeof(EncryptionInfo);
527fe6060f1SDimitry Andric     c->cmd = LP::encryptionInfoLCType;
528fe6060f1SDimitry Andric     c->cmdsize = getSize();
529fe6060f1SDimitry Andric     c->cryptoff = in.header->getSize();
530fe6060f1SDimitry Andric     auto it = find_if(outputSegments, [](const OutputSegment *seg) {
531fe6060f1SDimitry Andric       return seg->name == segment_names::text;
532fe6060f1SDimitry Andric     });
533fe6060f1SDimitry Andric     assert(it != outputSegments.end());
534fe6060f1SDimitry Andric     c->cryptsize = (*it)->fileSize - c->cryptoff;
535fe6060f1SDimitry Andric   }
536fe6060f1SDimitry Andric };
537fe6060f1SDimitry Andric 
538fe6060f1SDimitry Andric class LCCodeSignature final : public LoadCommand {
539fe6060f1SDimitry Andric public:
540fe6060f1SDimitry Andric   LCCodeSignature(CodeSignatureSection *section) : section(section) {}
541fe6060f1SDimitry Andric 
542fe6060f1SDimitry Andric   uint32_t getSize() const override { return sizeof(linkedit_data_command); }
543fe6060f1SDimitry Andric 
544fe6060f1SDimitry Andric   void writeTo(uint8_t *buf) const override {
545fe6060f1SDimitry Andric     auto *c = reinterpret_cast<linkedit_data_command *>(buf);
546fe6060f1SDimitry Andric     c->cmd = LC_CODE_SIGNATURE;
547fe6060f1SDimitry Andric     c->cmdsize = getSize();
548fe6060f1SDimitry Andric     c->dataoff = static_cast<uint32_t>(section->fileOff);
549fe6060f1SDimitry Andric     c->datasize = section->getSize();
550fe6060f1SDimitry Andric   }
551fe6060f1SDimitry Andric 
552fe6060f1SDimitry Andric   CodeSignatureSection *section;
553fe6060f1SDimitry Andric };
554fe6060f1SDimitry Andric 
5555ffd83dbSDimitry Andric } // namespace
5565ffd83dbSDimitry Andric 
557fe6060f1SDimitry Andric void Writer::treatSpecialUndefineds() {
558fe6060f1SDimitry Andric   if (config->entry)
559fe6060f1SDimitry Andric     if (auto *undefined = dyn_cast<Undefined>(config->entry))
560fe6060f1SDimitry Andric       treatUndefinedSymbol(*undefined, "the entry point");
561fe6060f1SDimitry Andric 
562fe6060f1SDimitry Andric   // FIXME: This prints symbols that are undefined both in input files and
563fe6060f1SDimitry Andric   // via -u flag twice.
564fe6060f1SDimitry Andric   for (const Symbol *sym : config->explicitUndefineds) {
565fe6060f1SDimitry Andric     if (const auto *undefined = dyn_cast<Undefined>(sym))
566fe6060f1SDimitry Andric       treatUndefinedSymbol(*undefined, "-u");
567fe6060f1SDimitry Andric   }
568fe6060f1SDimitry Andric   // Literal exported-symbol names must be defined, but glob
569fe6060f1SDimitry Andric   // patterns need not match.
570fe6060f1SDimitry Andric   for (const CachedHashStringRef &cachedName :
571fe6060f1SDimitry Andric        config->exportedSymbols.literals) {
572fe6060f1SDimitry Andric     if (const Symbol *sym = symtab->find(cachedName))
573fe6060f1SDimitry Andric       if (const auto *undefined = dyn_cast<Undefined>(sym))
574fe6060f1SDimitry Andric         treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
575fe6060f1SDimitry Andric   }
576fe6060f1SDimitry Andric }
577fe6060f1SDimitry Andric 
578fe6060f1SDimitry Andric // Add stubs and bindings where necessary (e.g. if the symbol is a
579fe6060f1SDimitry Andric // DylibSymbol.)
580fe6060f1SDimitry Andric static void prepareBranchTarget(Symbol *sym) {
581fe6060f1SDimitry Andric   if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
582fe6060f1SDimitry Andric     if (in.stubs->addEntry(dysym)) {
583fe6060f1SDimitry Andric       if (sym->isWeakDef()) {
584fe6060f1SDimitry Andric         in.binding->addEntry(dysym, in.lazyPointers->isec,
585fe6060f1SDimitry Andric                              sym->stubsIndex * target->wordSize);
586fe6060f1SDimitry Andric         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
587fe6060f1SDimitry Andric                                  sym->stubsIndex * target->wordSize);
588fe6060f1SDimitry Andric       } else {
589fe6060f1SDimitry Andric         in.lazyBinding->addEntry(dysym);
590fe6060f1SDimitry Andric       }
591fe6060f1SDimitry Andric     }
592fe6060f1SDimitry Andric   } else if (auto *defined = dyn_cast<Defined>(sym)) {
593fe6060f1SDimitry Andric     if (defined->isExternalWeakDef()) {
594fe6060f1SDimitry Andric       if (in.stubs->addEntry(sym)) {
595fe6060f1SDimitry Andric         in.rebase->addEntry(in.lazyPointers->isec,
596fe6060f1SDimitry Andric                             sym->stubsIndex * target->wordSize);
597fe6060f1SDimitry Andric         in.weakBinding->addEntry(sym, in.lazyPointers->isec,
598fe6060f1SDimitry Andric                                  sym->stubsIndex * target->wordSize);
599fe6060f1SDimitry Andric       }
60081ad6265SDimitry Andric     } else if (defined->interposable) {
60181ad6265SDimitry Andric       if (in.stubs->addEntry(sym))
60281ad6265SDimitry Andric         in.lazyBinding->addEntry(sym);
603fe6060f1SDimitry Andric     }
604fe6060f1SDimitry Andric   } else {
605fe6060f1SDimitry Andric     llvm_unreachable("invalid branch target symbol type");
606fe6060f1SDimitry Andric   }
607fe6060f1SDimitry Andric }
608fe6060f1SDimitry Andric 
609fe6060f1SDimitry Andric // Can a symbol's address can only be resolved at runtime?
610fe6060f1SDimitry Andric static bool needsBinding(const Symbol *sym) {
611fe6060f1SDimitry Andric   if (isa<DylibSymbol>(sym))
612fe6060f1SDimitry Andric     return true;
613fe6060f1SDimitry Andric   if (const auto *defined = dyn_cast<Defined>(sym))
61481ad6265SDimitry Andric     return defined->isExternalWeakDef() || defined->interposable;
615fe6060f1SDimitry Andric   return false;
616fe6060f1SDimitry Andric }
617fe6060f1SDimitry Andric 
618fe6060f1SDimitry Andric static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec,
61904eeddc0SDimitry Andric                                     const lld::macho::Reloc &r) {
620fe6060f1SDimitry Andric   assert(sym->isLive());
621fe6060f1SDimitry Andric   const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
622fe6060f1SDimitry Andric 
623fe6060f1SDimitry Andric   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
624fe6060f1SDimitry Andric     prepareBranchTarget(sym);
625fe6060f1SDimitry Andric   } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
626fe6060f1SDimitry Andric     if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym))
627fe6060f1SDimitry Andric       in.got->addEntry(sym);
628fe6060f1SDimitry Andric   } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
629fe6060f1SDimitry Andric     if (needsBinding(sym))
630fe6060f1SDimitry Andric       in.tlvPointers->addEntry(sym);
631fe6060f1SDimitry Andric   } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) {
632fe6060f1SDimitry Andric     // References from thread-local variable sections are treated as offsets
633fe6060f1SDimitry Andric     // relative to the start of the referent section, and therefore have no
634fe6060f1SDimitry Andric     // need of rebase opcodes.
635fe6060f1SDimitry Andric     if (!(isThreadLocalVariables(isec->getFlags()) && isa<Defined>(sym)))
636fe6060f1SDimitry Andric       addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
637fe6060f1SDimitry Andric   }
638fe6060f1SDimitry Andric }
639fe6060f1SDimitry Andric 
6405ffd83dbSDimitry Andric void Writer::scanRelocations() {
641fe6060f1SDimitry Andric   TimeTraceScope timeScope("Scan relocations");
642fe6060f1SDimitry Andric 
643fe6060f1SDimitry Andric   // This can't use a for-each loop: It calls treatUndefinedSymbol(), which can
644fe6060f1SDimitry Andric   // add to inputSections, which invalidates inputSections's iterators.
645fe6060f1SDimitry Andric   for (size_t i = 0; i < inputSections.size(); ++i) {
646fe6060f1SDimitry Andric     ConcatInputSection *isec = inputSections[i];
647fe6060f1SDimitry Andric 
648fe6060f1SDimitry Andric     if (isec->shouldOmitFromOutput())
649e8d8bef9SDimitry Andric       continue;
650e8d8bef9SDimitry Andric 
651fe6060f1SDimitry Andric     for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
65204eeddc0SDimitry Andric       lld::macho::Reloc &r = *it;
653fe6060f1SDimitry Andric       if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
654fe6060f1SDimitry Andric         // Skip over the following UNSIGNED relocation -- it's just there as the
655fe6060f1SDimitry Andric         // minuend, and doesn't have the usual UNSIGNED semantics. We don't want
656fe6060f1SDimitry Andric         // to emit rebase opcodes for it.
657fe6060f1SDimitry Andric         it++;
658fe6060f1SDimitry Andric         continue;
659fe6060f1SDimitry Andric       }
660fe6060f1SDimitry Andric       if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
661fe6060f1SDimitry Andric         if (auto *undefined = dyn_cast<Undefined>(sym))
66281ad6265SDimitry Andric           treatUndefinedSymbol(*undefined, isec, r.offset);
663fe6060f1SDimitry Andric         // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
664fe6060f1SDimitry Andric         if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r))
665fe6060f1SDimitry Andric           prepareSymbolRelocation(sym, isec, r);
666e8d8bef9SDimitry Andric       } else {
667fe6060f1SDimitry Andric         // Canonicalize the referent so that later accesses in Writer won't
668fe6060f1SDimitry Andric         // have to worry about it. Perhaps we should do this for Defined::isec
669fe6060f1SDimitry Andric         // too...
670fe6060f1SDimitry Andric         auto *referentIsec = r.referent.get<InputSection *>();
671fe6060f1SDimitry Andric         r.referent = referentIsec->canonical();
672e8d8bef9SDimitry Andric         if (!r.pcrel)
673e8d8bef9SDimitry Andric           in.rebase->addEntry(isec, r.offset);
6745ffd83dbSDimitry Andric       }
6755ffd83dbSDimitry Andric     }
6765ffd83dbSDimitry Andric   }
677fe6060f1SDimitry Andric 
678*f3fd488fSDimitry Andric   in.unwindInfo->prepare();
6795ffd83dbSDimitry Andric }
6805ffd83dbSDimitry Andric 
681e8d8bef9SDimitry Andric void Writer::scanSymbols() {
682fe6060f1SDimitry Andric   TimeTraceScope timeScope("Scan symbols");
683349cc55cSDimitry Andric   for (Symbol *sym : symtab->getSymbols()) {
684349cc55cSDimitry Andric     if (auto *defined = dyn_cast<Defined>(sym)) {
685349cc55cSDimitry Andric       if (!defined->isLive())
686349cc55cSDimitry Andric         continue;
687349cc55cSDimitry Andric       defined->canonicalize();
688349cc55cSDimitry Andric       if (defined->overridesWeakDef)
689e8d8bef9SDimitry Andric         in.weakBinding->addNonWeakDefinition(defined);
690349cc55cSDimitry Andric       if (!defined->isAbsolute() && isCodeSection(defined->isec))
691349cc55cSDimitry Andric         in.unwindInfo->addSymbol(defined);
692e8d8bef9SDimitry Andric     } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
693fe6060f1SDimitry Andric       // This branch intentionally doesn't check isLive().
694fe6060f1SDimitry Andric       if (dysym->isDynamicLookup())
695fe6060f1SDimitry Andric         continue;
696fe6060f1SDimitry Andric       dysym->getFile()->refState =
697fe6060f1SDimitry Andric           std::max(dysym->getFile()->refState, dysym->getRefState());
698e8d8bef9SDimitry Andric     }
699e8d8bef9SDimitry Andric   }
700349cc55cSDimitry Andric 
701349cc55cSDimitry Andric   for (const InputFile *file : inputFiles) {
702349cc55cSDimitry Andric     if (auto *objFile = dyn_cast<ObjFile>(file))
703349cc55cSDimitry Andric       for (Symbol *sym : objFile->symbols) {
704349cc55cSDimitry Andric         if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
705349cc55cSDimitry Andric           if (!defined->isLive())
706349cc55cSDimitry Andric             continue;
707349cc55cSDimitry Andric           defined->canonicalize();
708349cc55cSDimitry Andric           if (!defined->isExternal() && !defined->isAbsolute() &&
709349cc55cSDimitry Andric               isCodeSection(defined->isec))
710349cc55cSDimitry Andric             in.unwindInfo->addSymbol(defined);
711349cc55cSDimitry Andric         }
712349cc55cSDimitry Andric       }
713349cc55cSDimitry Andric   }
714e8d8bef9SDimitry Andric }
715e8d8bef9SDimitry Andric 
716fe6060f1SDimitry Andric // TODO: ld64 enforces the old load commands in a few other cases.
717fe6060f1SDimitry Andric static bool useLCBuildVersion(const PlatformInfo &platformInfo) {
71804eeddc0SDimitry Andric   static const std::vector<std::pair<PlatformType, VersionTuple>> minVersion = {
71904eeddc0SDimitry Andric       {PLATFORM_MACOS, VersionTuple(10, 14)},
72004eeddc0SDimitry Andric       {PLATFORM_IOS, VersionTuple(12, 0)},
72104eeddc0SDimitry Andric       {PLATFORM_IOSSIMULATOR, VersionTuple(13, 0)},
72204eeddc0SDimitry Andric       {PLATFORM_TVOS, VersionTuple(12, 0)},
72304eeddc0SDimitry Andric       {PLATFORM_TVOSSIMULATOR, VersionTuple(13, 0)},
72404eeddc0SDimitry Andric       {PLATFORM_WATCHOS, VersionTuple(5, 0)},
72504eeddc0SDimitry Andric       {PLATFORM_WATCHOSSIMULATOR, VersionTuple(6, 0)}};
726fe6060f1SDimitry Andric   auto it = llvm::find_if(minVersion, [&](const auto &p) {
727fe6060f1SDimitry Andric     return p.first == platformInfo.target.Platform;
728fe6060f1SDimitry Andric   });
729fe6060f1SDimitry Andric   return it == minVersion.end() ? true : platformInfo.minimum >= it->second;
730fe6060f1SDimitry Andric }
731fe6060f1SDimitry Andric 
732fe6060f1SDimitry Andric template <class LP> void Writer::createLoadCommands() {
733fe6060f1SDimitry Andric   uint8_t segIndex = 0;
734fe6060f1SDimitry Andric   for (OutputSegment *seg : outputSegments) {
735fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg));
736fe6060f1SDimitry Andric     seg->index = segIndex++;
737fe6060f1SDimitry Andric   }
738fe6060f1SDimitry Andric 
739e8d8bef9SDimitry Andric   in.header->addLoadCommand(make<LCDyldInfo>(
740e8d8bef9SDimitry Andric       in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports));
741e8d8bef9SDimitry Andric   in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection));
742e8d8bef9SDimitry Andric   in.header->addLoadCommand(
743e8d8bef9SDimitry Andric       make<LCDysymtab>(symtabSection, indirectSymtabSection));
744fe6060f1SDimitry Andric   if (!config->umbrella.empty())
745fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCSubFramework>(config->umbrella));
746fe6060f1SDimitry Andric   if (config->emitEncryptionInfo)
747fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCEncryptionInfo<LP>>());
748e8d8bef9SDimitry Andric   for (StringRef path : config->runtimePaths)
749e8d8bef9SDimitry Andric     in.header->addLoadCommand(make<LCRPath>(path));
7505ffd83dbSDimitry Andric 
7515ffd83dbSDimitry Andric   switch (config->outputType) {
7525ffd83dbSDimitry Andric   case MH_EXECUTE:
753e8d8bef9SDimitry Andric     in.header->addLoadCommand(make<LCLoadDylinker>());
7545ffd83dbSDimitry Andric     break;
7555ffd83dbSDimitry Andric   case MH_DYLIB:
756e8d8bef9SDimitry Andric     in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
757e8d8bef9SDimitry Andric                                             config->dylibCompatibilityVersion,
758e8d8bef9SDimitry Andric                                             config->dylibCurrentVersion));
759e8d8bef9SDimitry Andric     break;
760e8d8bef9SDimitry Andric   case MH_BUNDLE:
7615ffd83dbSDimitry Andric     break;
7625ffd83dbSDimitry Andric   default:
7635ffd83dbSDimitry Andric     llvm_unreachable("unhandled output file type");
7645ffd83dbSDimitry Andric   }
7655ffd83dbSDimitry Andric 
766e8d8bef9SDimitry Andric   uuidCommand = make<LCUuid>();
767e8d8bef9SDimitry Andric   in.header->addLoadCommand(uuidCommand);
768e8d8bef9SDimitry Andric 
769fe6060f1SDimitry Andric   if (useLCBuildVersion(config->platformInfo))
770fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo));
771fe6060f1SDimitry Andric   else
772fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo));
7735ffd83dbSDimitry Andric 
77481ad6265SDimitry Andric   if (config->secondaryPlatformInfo) {
77581ad6265SDimitry Andric     in.header->addLoadCommand(
77681ad6265SDimitry Andric         make<LCBuildVersion>(*config->secondaryPlatformInfo));
77781ad6265SDimitry Andric   }
77881ad6265SDimitry Andric 
779fe6060f1SDimitry Andric   // This is down here to match ld64's load command order.
780fe6060f1SDimitry Andric   if (config->outputType == MH_EXECUTE)
781fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCMain>());
782fe6060f1SDimitry Andric 
78361cfbce3SDimitry Andric   // See ld64's OutputFile::buildDylibOrdinalMapping for the corresponding
78461cfbce3SDimitry Andric   // library ordinal computation code in ld64.
785fe6060f1SDimitry Andric   int64_t dylibOrdinal = 1;
786fe6060f1SDimitry Andric   DenseMap<StringRef, int64_t> ordinalForInstallName;
78761cfbce3SDimitry Andric 
78861cfbce3SDimitry Andric   std::vector<DylibFile *> dylibFiles;
7895ffd83dbSDimitry Andric   for (InputFile *file : inputFiles) {
79061cfbce3SDimitry Andric     if (auto *dylibFile = dyn_cast<DylibFile>(file))
79161cfbce3SDimitry Andric       dylibFiles.push_back(dylibFile);
79261cfbce3SDimitry Andric   }
79361cfbce3SDimitry Andric   for (size_t i = 0; i < dylibFiles.size(); ++i)
79461cfbce3SDimitry Andric     dylibFiles.insert(dylibFiles.end(), dylibFiles[i]->extraDylibs.begin(),
79561cfbce3SDimitry Andric                       dylibFiles[i]->extraDylibs.end());
79661cfbce3SDimitry Andric 
79761cfbce3SDimitry Andric   for (DylibFile *dylibFile : dylibFiles) {
798fe6060f1SDimitry Andric     if (dylibFile->isBundleLoader) {
799fe6060f1SDimitry Andric       dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE;
800fe6060f1SDimitry Andric       // Shortcut since bundle-loader does not re-export the symbols.
801fe6060f1SDimitry Andric 
802fe6060f1SDimitry Andric       dylibFile->reexport = false;
803fe6060f1SDimitry Andric       continue;
804fe6060f1SDimitry Andric     }
805fe6060f1SDimitry Andric 
806fe6060f1SDimitry Andric     // Don't emit load commands for a dylib that is not referenced if:
807fe6060f1SDimitry Andric     // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER --
808fe6060f1SDimitry Andric     //   if it's on the linker command line, it's explicit)
809fe6060f1SDimitry Andric     // - or it's marked MH_DEAD_STRIPPABLE_DYLIB
810fe6060f1SDimitry Andric     // - or the flag -dead_strip_dylibs is used
811fe6060f1SDimitry Andric     // FIXME: `isReferenced()` is currently computed before dead code
812fe6060f1SDimitry Andric     // stripping, so references from dead code keep a dylib alive. This
813fe6060f1SDimitry Andric     // matches ld64, but it's something we should do better.
814fe6060f1SDimitry Andric     if (!dylibFile->isReferenced() && !dylibFile->forceNeeded &&
81561cfbce3SDimitry Andric         (!dylibFile->isExplicitlyLinked() || dylibFile->deadStrippable ||
816fe6060f1SDimitry Andric          config->deadStripDylibs))
817fe6060f1SDimitry Andric       continue;
818fe6060f1SDimitry Andric 
819fe6060f1SDimitry Andric     // Several DylibFiles can have the same installName. Only emit a single
820fe6060f1SDimitry Andric     // load command for that installName and give all these DylibFiles the
821fe6060f1SDimitry Andric     // same ordinal.
822fe6060f1SDimitry Andric     // This can happen in several cases:
823fe6060f1SDimitry Andric     // - a new framework could change its installName to an older
824fe6060f1SDimitry Andric     //   framework name via an $ld$ symbol depending on platform_version
825fe6060f1SDimitry Andric     // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd;
826fe6060f1SDimitry Andric     //   Foo.framework/Foo.tbd is usually a symlink to
827fe6060f1SDimitry Andric     //   Foo.framework/Versions/Current/Foo.tbd, where
828fe6060f1SDimitry Andric     //   Foo.framework/Versions/Current is usually a symlink to
829fe6060f1SDimitry Andric     //   Foo.framework/Versions/A)
830fe6060f1SDimitry Andric     // - a framework can be linked both explicitly on the linker
831fe6060f1SDimitry Andric     //   command line and implicitly as a reexport from a different
832fe6060f1SDimitry Andric     //   framework. The re-export will usually point to the tbd file
833fe6060f1SDimitry Andric     //   in Foo.framework/Versions/A/Foo.tbd, while the explicit link will
834fe6060f1SDimitry Andric     //   usually find Foo.framework/Foo.tbd. These are usually symlinks,
835fe6060f1SDimitry Andric     //   but in a --reproduce archive they will be identical but distinct
836fe6060f1SDimitry Andric     //   files.
837fe6060f1SDimitry Andric     // In the first case, *semantically distinct* DylibFiles will have the
838fe6060f1SDimitry Andric     // same installName.
839fe6060f1SDimitry Andric     int64_t &ordinal = ordinalForInstallName[dylibFile->installName];
840fe6060f1SDimitry Andric     if (ordinal) {
841fe6060f1SDimitry Andric       dylibFile->ordinal = ordinal;
842fe6060f1SDimitry Andric       continue;
843fe6060f1SDimitry Andric     }
844fe6060f1SDimitry Andric 
845fe6060f1SDimitry Andric     ordinal = dylibFile->ordinal = dylibOrdinal++;
846e8d8bef9SDimitry Andric     LoadCommandType lcType =
847e8d8bef9SDimitry Andric         dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
848e8d8bef9SDimitry Andric             ? LC_LOAD_WEAK_DYLIB
849e8d8bef9SDimitry Andric             : LC_LOAD_DYLIB;
850fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName,
851e8d8bef9SDimitry Andric                                             dylibFile->compatibilityVersion,
852e8d8bef9SDimitry Andric                                             dylibFile->currentVersion));
8535ffd83dbSDimitry Andric 
8545ffd83dbSDimitry Andric     if (dylibFile->reexport)
855e8d8bef9SDimitry Andric       in.header->addLoadCommand(
856fe6060f1SDimitry Andric           make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName));
8575ffd83dbSDimitry Andric   }
858e8d8bef9SDimitry Andric 
859fe6060f1SDimitry Andric   if (functionStartsSection)
860fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection));
861fe6060f1SDimitry Andric   if (dataInCodeSection)
862fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCDataInCode>(dataInCodeSection));
863fe6060f1SDimitry Andric   if (codeSignatureSection)
864fe6060f1SDimitry Andric     in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection));
865fe6060f1SDimitry Andric 
866e8d8bef9SDimitry Andric   const uint32_t MACOS_MAXPATHLEN = 1024;
867e8d8bef9SDimitry Andric   config->headerPad = std::max(
868e8d8bef9SDimitry Andric       config->headerPad, (config->headerPadMaxInstallNames
869e8d8bef9SDimitry Andric                               ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN
870e8d8bef9SDimitry Andric                               : 0));
8715ffd83dbSDimitry Andric }
8725ffd83dbSDimitry Andric 
8735ffd83dbSDimitry Andric // Sorting only can happen once all outputs have been collected. Here we sort
8745ffd83dbSDimitry Andric // segments, output sections within each segment, and input sections within each
8755ffd83dbSDimitry Andric // output segment.
8765ffd83dbSDimitry Andric static void sortSegmentsAndSections() {
877fe6060f1SDimitry Andric   TimeTraceScope timeScope("Sort segments and sections");
878fe6060f1SDimitry Andric   sortOutputSegments();
8795ffd83dbSDimitry Andric 
8805ffd83dbSDimitry Andric   DenseMap<const InputSection *, size_t> isecPriorities =
88181ad6265SDimitry Andric       priorityBuilder.buildInputSectionPriorities();
8825ffd83dbSDimitry Andric 
8835ffd83dbSDimitry Andric   uint32_t sectionIndex = 0;
8845ffd83dbSDimitry Andric   for (OutputSegment *seg : outputSegments) {
885fe6060f1SDimitry Andric     seg->sortOutputSections();
88604eeddc0SDimitry Andric     // References from thread-local variable sections are treated as offsets
88704eeddc0SDimitry Andric     // relative to the start of the thread-local data memory area, which
88804eeddc0SDimitry Andric     // is initialized via copying all the TLV data sections (which are all
88904eeddc0SDimitry Andric     // contiguous). If later data sections require a greater alignment than
89004eeddc0SDimitry Andric     // earlier ones, the offsets of data within those sections won't be
89104eeddc0SDimitry Andric     // guaranteed to aligned unless we normalize alignments. We therefore use
89204eeddc0SDimitry Andric     // the largest alignment for all TLV data sections.
89304eeddc0SDimitry Andric     uint32_t tlvAlign = 0;
89404eeddc0SDimitry Andric     for (const OutputSection *osec : seg->getSections())
89504eeddc0SDimitry Andric       if (isThreadLocalData(osec->flags) && osec->align > tlvAlign)
89604eeddc0SDimitry Andric         tlvAlign = osec->align;
89704eeddc0SDimitry Andric 
898fe6060f1SDimitry Andric     for (OutputSection *osec : seg->getSections()) {
8995ffd83dbSDimitry Andric       // Now that the output sections are sorted, assign the final
9005ffd83dbSDimitry Andric       // output section indices.
9015ffd83dbSDimitry Andric       if (!osec->isHidden())
9025ffd83dbSDimitry Andric         osec->index = ++sectionIndex;
90304eeddc0SDimitry Andric       if (isThreadLocalData(osec->flags)) {
90404eeddc0SDimitry Andric         if (!firstTLVDataSection)
905e8d8bef9SDimitry Andric           firstTLVDataSection = osec;
90604eeddc0SDimitry Andric         osec->align = tlvAlign;
90704eeddc0SDimitry Andric       }
908e8d8bef9SDimitry Andric 
9095ffd83dbSDimitry Andric       if (!isecPriorities.empty()) {
910fe6060f1SDimitry Andric         if (auto *merged = dyn_cast<ConcatOutputSection>(osec)) {
9115ffd83dbSDimitry Andric           llvm::stable_sort(merged->inputs,
9125ffd83dbSDimitry Andric                             [&](InputSection *a, InputSection *b) {
9135ffd83dbSDimitry Andric                               return isecPriorities[a] > isecPriorities[b];
9145ffd83dbSDimitry Andric                             });
9155ffd83dbSDimitry Andric         }
9165ffd83dbSDimitry Andric       }
9175ffd83dbSDimitry Andric     }
9185ffd83dbSDimitry Andric   }
9195ffd83dbSDimitry Andric }
9205ffd83dbSDimitry Andric 
921fe6060f1SDimitry Andric template <class LP> void Writer::createOutputSections() {
922fe6060f1SDimitry Andric   TimeTraceScope timeScope("Create output sections");
9235ffd83dbSDimitry Andric   // First, create hidden sections
9245ffd83dbSDimitry Andric   stringTableSection = make<StringTableSection>();
925fe6060f1SDimitry Andric   symtabSection = makeSymtabSection<LP>(*stringTableSection);
926e8d8bef9SDimitry Andric   indirectSymtabSection = make<IndirectSymtabSection>();
927fe6060f1SDimitry Andric   if (config->adhocCodesign)
928fe6060f1SDimitry Andric     codeSignatureSection = make<CodeSignatureSection>();
929fe6060f1SDimitry Andric   if (config->emitDataInCodeInfo)
930fe6060f1SDimitry Andric     dataInCodeSection = make<DataInCodeSection>();
931fe6060f1SDimitry Andric   if (config->emitFunctionStarts)
932fe6060f1SDimitry Andric     functionStartsSection = make<FunctionStartsSection>();
933fe6060f1SDimitry Andric   if (config->emitBitcodeBundle)
934fe6060f1SDimitry Andric     make<BitcodeBundleSection>();
9355ffd83dbSDimitry Andric 
9365ffd83dbSDimitry Andric   switch (config->outputType) {
9375ffd83dbSDimitry Andric   case MH_EXECUTE:
9385ffd83dbSDimitry Andric     make<PageZeroSection>();
9395ffd83dbSDimitry Andric     break;
9405ffd83dbSDimitry Andric   case MH_DYLIB:
941e8d8bef9SDimitry Andric   case MH_BUNDLE:
9425ffd83dbSDimitry Andric     break;
9435ffd83dbSDimitry Andric   default:
9445ffd83dbSDimitry Andric     llvm_unreachable("unhandled output file type");
9455ffd83dbSDimitry Andric   }
9465ffd83dbSDimitry Andric 
947fe6060f1SDimitry Andric   // Then add input sections to output sections.
948fe6060f1SDimitry Andric   for (ConcatInputSection *isec : inputSections) {
949fe6060f1SDimitry Andric     if (isec->shouldOmitFromOutput())
950fe6060f1SDimitry Andric       continue;
951fe6060f1SDimitry Andric     ConcatOutputSection *osec = cast<ConcatOutputSection>(isec->parent);
952fe6060f1SDimitry Andric     osec->addInput(isec);
953fe6060f1SDimitry Andric     osec->inputOrder =
954fe6060f1SDimitry Andric         std::min(osec->inputOrder, static_cast<int>(isec->outSecOff));
9555ffd83dbSDimitry Andric   }
9565ffd83dbSDimitry Andric 
957fe6060f1SDimitry Andric   // Once all the inputs are added, we can finalize the output section
958fe6060f1SDimitry Andric   // properties and create the corresponding output segments.
959fe6060f1SDimitry Andric   for (const auto &it : concatOutputSections) {
9605ffd83dbSDimitry Andric     StringRef segname = it.first.first;
961fe6060f1SDimitry Andric     ConcatOutputSection *osec = it.second;
962fe6060f1SDimitry Andric     assert(segname != segment_names::ld);
96381ad6265SDimitry Andric     if (osec->isNeeded()) {
96481ad6265SDimitry Andric       // See comment in ObjFile::splitEhFrames()
96581ad6265SDimitry Andric       if (osec->name == section_names::ehFrame &&
96681ad6265SDimitry Andric           segname == segment_names::text)
96781ad6265SDimitry Andric         osec->align = target->wordSize;
96881ad6265SDimitry Andric 
9695ffd83dbSDimitry Andric       getOrCreateOutputSegment(segname)->addOutputSection(osec);
9705ffd83dbSDimitry Andric     }
97181ad6265SDimitry Andric   }
9725ffd83dbSDimitry Andric 
9735ffd83dbSDimitry Andric   for (SyntheticSection *ssec : syntheticSections) {
974fe6060f1SDimitry Andric     auto it = concatOutputSections.find({ssec->segname, ssec->name});
975349cc55cSDimitry Andric     // We add all LinkEdit sections here because we don't know if they are
976349cc55cSDimitry Andric     // needed until their finalizeContents() methods get called later. While
977349cc55cSDimitry Andric     // this means that we add some redundant sections to __LINKEDIT, there is
978349cc55cSDimitry Andric     // is no redundancy in the output, as we do not emit section headers for
979349cc55cSDimitry Andric     // any LinkEdit sections.
980349cc55cSDimitry Andric     if (ssec->isNeeded() || ssec->segname == segment_names::linkEdit) {
981fe6060f1SDimitry Andric       if (it == concatOutputSections.end()) {
9825ffd83dbSDimitry Andric         getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
9835ffd83dbSDimitry Andric       } else {
984fe6060f1SDimitry Andric         fatal("section from " +
985fe6060f1SDimitry Andric               toString(it->second->firstSection()->getFile()) +
9865ffd83dbSDimitry Andric               " conflicts with synthetic section " + ssec->segname + "," +
9875ffd83dbSDimitry Andric               ssec->name);
9885ffd83dbSDimitry Andric       }
9895ffd83dbSDimitry Andric     }
9905ffd83dbSDimitry Andric   }
9915ffd83dbSDimitry Andric 
992fe6060f1SDimitry Andric   // dyld requires __LINKEDIT segment to always exist (even if empty).
993fe6060f1SDimitry Andric   linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit);
994fe6060f1SDimitry Andric }
995fe6060f1SDimitry Andric 
996fe6060f1SDimitry Andric void Writer::finalizeAddresses() {
997fe6060f1SDimitry Andric   TimeTraceScope timeScope("Finalize addresses");
998fe6060f1SDimitry Andric   uint64_t pageSize = target->getPageSize();
99981ad6265SDimitry Andric 
100081ad6265SDimitry Andric   // We could parallelize this loop, but local benchmarking indicates it is
100181ad6265SDimitry Andric   // faster to do it all in the main thread.
100281ad6265SDimitry Andric   for (OutputSegment *seg : outputSegments) {
100381ad6265SDimitry Andric     if (seg == linkEditSegment)
100481ad6265SDimitry Andric       continue;
100581ad6265SDimitry Andric     for (OutputSection *osec : seg->getSections()) {
100681ad6265SDimitry Andric       if (!osec->isNeeded())
100781ad6265SDimitry Andric         continue;
100881ad6265SDimitry Andric       // Other kinds of OutputSections have already been finalized.
100981ad6265SDimitry Andric       if (auto concatOsec = dyn_cast<ConcatOutputSection>(osec))
101081ad6265SDimitry Andric         concatOsec->finalizeContents();
101181ad6265SDimitry Andric     }
101281ad6265SDimitry Andric   }
101381ad6265SDimitry Andric 
1014fe6060f1SDimitry Andric   // Ensure that segments (and the sections they contain) are allocated
1015fe6060f1SDimitry Andric   // addresses in ascending order, which dyld requires.
1016fe6060f1SDimitry Andric   //
1017fe6060f1SDimitry Andric   // Note that at this point, __LINKEDIT sections are empty, but we need to
1018fe6060f1SDimitry Andric   // determine addresses of other segments/sections before generating its
1019fe6060f1SDimitry Andric   // contents.
1020fe6060f1SDimitry Andric   for (OutputSegment *seg : outputSegments) {
1021fe6060f1SDimitry Andric     if (seg == linkEditSegment)
1022fe6060f1SDimitry Andric       continue;
1023fe6060f1SDimitry Andric     seg->addr = addr;
1024fe6060f1SDimitry Andric     assignAddresses(seg);
1025fe6060f1SDimitry Andric     // codesign / libstuff checks for segment ordering by verifying that
1026fe6060f1SDimitry Andric     // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before
1027fe6060f1SDimitry Andric     // (instead of after) computing fileSize to ensure that the segments are
1028fe6060f1SDimitry Andric     // contiguous. We handle addr / vmSize similarly for the same reason.
1029fe6060f1SDimitry Andric     fileOff = alignTo(fileOff, pageSize);
1030fe6060f1SDimitry Andric     addr = alignTo(addr, pageSize);
1031fe6060f1SDimitry Andric     seg->vmSize = addr - seg->addr;
1032fe6060f1SDimitry Andric     seg->fileSize = fileOff - seg->fileOff;
1033fe6060f1SDimitry Andric     seg->assignAddressesToStartEndSymbols();
1034fe6060f1SDimitry Andric   }
1035fe6060f1SDimitry Andric }
1036fe6060f1SDimitry Andric 
1037fe6060f1SDimitry Andric void Writer::finalizeLinkEditSegment() {
1038fe6060f1SDimitry Andric   TimeTraceScope timeScope("Finalize __LINKEDIT segment");
1039fe6060f1SDimitry Andric   // Fill __LINKEDIT contents.
1040fe6060f1SDimitry Andric   std::vector<LinkEditSection *> linkEditSections{
1041fe6060f1SDimitry Andric       in.rebase,
1042fe6060f1SDimitry Andric       in.binding,
1043fe6060f1SDimitry Andric       in.weakBinding,
1044fe6060f1SDimitry Andric       in.lazyBinding,
1045fe6060f1SDimitry Andric       in.exports,
1046fe6060f1SDimitry Andric       symtabSection,
1047fe6060f1SDimitry Andric       indirectSymtabSection,
1048fe6060f1SDimitry Andric       dataInCodeSection,
1049fe6060f1SDimitry Andric       functionStartsSection,
1050fe6060f1SDimitry Andric   };
10510eae32dcSDimitry Andric   SmallVector<std::shared_future<void>> threadFutures;
10520eae32dcSDimitry Andric   threadFutures.reserve(linkEditSections.size());
10530eae32dcSDimitry Andric   for (LinkEditSection *osec : linkEditSections)
1054fe6060f1SDimitry Andric     if (osec)
10550eae32dcSDimitry Andric       threadFutures.emplace_back(threadPool.async(
10560eae32dcSDimitry Andric           [](LinkEditSection *osec) { osec->finalizeContents(); }, osec));
10570eae32dcSDimitry Andric   for (std::shared_future<void> &future : threadFutures)
10580eae32dcSDimitry Andric     future.wait();
1059fe6060f1SDimitry Andric 
1060fe6060f1SDimitry Andric   // Now that __LINKEDIT is filled out, do a proper calculation of its
1061fe6060f1SDimitry Andric   // addresses and offsets.
1062fe6060f1SDimitry Andric   linkEditSegment->addr = addr;
1063fe6060f1SDimitry Andric   assignAddresses(linkEditSegment);
1064fe6060f1SDimitry Andric   // No need to page-align fileOff / addr here since this is the last segment.
1065fe6060f1SDimitry Andric   linkEditSegment->vmSize = addr - linkEditSegment->addr;
1066fe6060f1SDimitry Andric   linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff;
1067fe6060f1SDimitry Andric }
1068fe6060f1SDimitry Andric 
10695ffd83dbSDimitry Andric void Writer::assignAddresses(OutputSegment *seg) {
10705ffd83dbSDimitry Andric   seg->fileOff = fileOff;
10715ffd83dbSDimitry Andric 
1072fe6060f1SDimitry Andric   for (OutputSection *osec : seg->getSections()) {
1073e8d8bef9SDimitry Andric     if (!osec->isNeeded())
1074e8d8bef9SDimitry Andric       continue;
10755ffd83dbSDimitry Andric     addr = alignTo(addr, osec->align);
10765ffd83dbSDimitry Andric     fileOff = alignTo(fileOff, osec->align);
10775ffd83dbSDimitry Andric     osec->addr = addr;
10785ffd83dbSDimitry Andric     osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff;
10795ffd83dbSDimitry Andric     osec->finalize();
1080fe6060f1SDimitry Andric     osec->assignAddressesToStartEndSymbols();
10815ffd83dbSDimitry Andric 
10825ffd83dbSDimitry Andric     addr += osec->getSize();
10835ffd83dbSDimitry Andric     fileOff += osec->getFileSize();
10845ffd83dbSDimitry Andric   }
10855ffd83dbSDimitry Andric }
10865ffd83dbSDimitry Andric 
10875ffd83dbSDimitry Andric void Writer::openFile() {
10885ffd83dbSDimitry Andric   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
10895ffd83dbSDimitry Andric       FileOutputBuffer::create(config->outputFile, fileOff,
10905ffd83dbSDimitry Andric                                FileOutputBuffer::F_executable);
10915ffd83dbSDimitry Andric 
10925ffd83dbSDimitry Andric   if (!bufferOrErr)
109381ad6265SDimitry Andric     fatal("failed to open " + config->outputFile + ": " +
10945ffd83dbSDimitry Andric           llvm::toString(bufferOrErr.takeError()));
10955ffd83dbSDimitry Andric   buffer = std::move(*bufferOrErr);
109681ad6265SDimitry Andric   in.bufferStart = buffer->getBufferStart();
10975ffd83dbSDimitry Andric }
10985ffd83dbSDimitry Andric 
10995ffd83dbSDimitry Andric void Writer::writeSections() {
11005ffd83dbSDimitry Andric   uint8_t *buf = buffer->getBufferStart();
110181ad6265SDimitry Andric   std::vector<const OutputSection *> osecs;
1102fe6060f1SDimitry Andric   for (const OutputSegment *seg : outputSegments)
110381ad6265SDimitry Andric     append_range(osecs, seg->getSections());
110481ad6265SDimitry Andric 
110581ad6265SDimitry Andric   parallelForEach(osecs.begin(), osecs.end(), [&](const OutputSection *osec) {
11065ffd83dbSDimitry Andric     osec->writeTo(buf + osec->fileOff);
110781ad6265SDimitry Andric   });
11085ffd83dbSDimitry Andric }
11095ffd83dbSDimitry Andric 
1110fe6060f1SDimitry Andric // In order to utilize multiple cores, we first split the buffer into chunks,
1111fe6060f1SDimitry Andric // compute a hash for each chunk, and then compute a hash value of the hash
1112fe6060f1SDimitry Andric // values.
1113e8d8bef9SDimitry Andric void Writer::writeUuid() {
1114fe6060f1SDimitry Andric   TimeTraceScope timeScope("Computing UUID");
11150eae32dcSDimitry Andric 
1116fe6060f1SDimitry Andric   ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()};
1117fe6060f1SDimitry Andric   unsigned chunkCount = parallel::strategy.compute_thread_count() * 10;
1118fe6060f1SDimitry Andric   // Round-up integer division
1119fe6060f1SDimitry Andric   size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount;
1120fe6060f1SDimitry Andric   std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize);
112181ad6265SDimitry Andric   // Leave one slot for filename
112281ad6265SDimitry Andric   std::vector<uint64_t> hashes(chunks.size() + 1);
11230eae32dcSDimitry Andric   SmallVector<std::shared_future<void>> threadFutures;
11240eae32dcSDimitry Andric   threadFutures.reserve(chunks.size());
11250eae32dcSDimitry Andric   for (size_t i = 0; i < chunks.size(); ++i)
11260eae32dcSDimitry Andric     threadFutures.emplace_back(threadPool.async(
112704eeddc0SDimitry Andric         [&](size_t j) { hashes[j] = xxHash64(chunks[j]); }, i));
11280eae32dcSDimitry Andric   for (std::shared_future<void> &future : threadFutures)
11290eae32dcSDimitry Andric     future.wait();
113081ad6265SDimitry Andric   // Append the output filename so that identical binaries with different names
113181ad6265SDimitry Andric   // don't get the same UUID.
113281ad6265SDimitry Andric   hashes[chunks.size()] = xxHash64(sys::path::filename(config->finalOutput));
1133fe6060f1SDimitry Andric   uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()),
1134fe6060f1SDimitry Andric                               hashes.size() * sizeof(uint64_t)});
1135e8d8bef9SDimitry Andric   uuidCommand->writeUuid(digest);
1136e8d8bef9SDimitry Andric }
1137e8d8bef9SDimitry Andric 
1138fe6060f1SDimitry Andric void Writer::writeCodeSignature() {
113981ad6265SDimitry Andric   if (codeSignatureSection) {
114081ad6265SDimitry Andric     TimeTraceScope timeScope("Write code signature");
1141fe6060f1SDimitry Andric     codeSignatureSection->writeHashes(buffer->getBufferStart());
1142fe6060f1SDimitry Andric   }
114381ad6265SDimitry Andric }
11445ffd83dbSDimitry Andric 
1145fe6060f1SDimitry Andric void Writer::writeOutputFile() {
1146fe6060f1SDimitry Andric   TimeTraceScope timeScope("Write output file");
11475ffd83dbSDimitry Andric   openFile();
114881ad6265SDimitry Andric   reportPendingUndefinedSymbols();
11495ffd83dbSDimitry Andric   if (errorCount())
11505ffd83dbSDimitry Andric     return;
11515ffd83dbSDimitry Andric   writeSections();
1152e8d8bef9SDimitry Andric   writeUuid();
1153fe6060f1SDimitry Andric   writeCodeSignature();
11545ffd83dbSDimitry Andric 
11555ffd83dbSDimitry Andric   if (auto e = buffer->commit())
11565ffd83dbSDimitry Andric     error("failed to write to the output file: " + toString(std::move(e)));
11575ffd83dbSDimitry Andric }
11585ffd83dbSDimitry Andric 
1159fe6060f1SDimitry Andric template <class LP> void Writer::run() {
1160fe6060f1SDimitry Andric   treatSpecialUndefineds();
1161fe6060f1SDimitry Andric   if (config->entry && !isa<Undefined>(config->entry))
1162fe6060f1SDimitry Andric     prepareBranchTarget(config->entry);
11631fd87a68SDimitry Andric 
1164349cc55cSDimitry Andric   // Canonicalization of all pointers to InputSections should be handled by
11651fd87a68SDimitry Andric   // these two scan* methods. I.e. from this point onward, for all live
11661fd87a68SDimitry Andric   // InputSections, we should have `isec->canonical() == isec`.
1167349cc55cSDimitry Andric   scanSymbols();
1168fe6060f1SDimitry Andric   scanRelocations();
1169349cc55cSDimitry Andric 
1170349cc55cSDimitry Andric   // Do not proceed if there was an undefined symbol.
117181ad6265SDimitry Andric   reportPendingUndefinedSymbols();
1172349cc55cSDimitry Andric   if (errorCount())
1173349cc55cSDimitry Andric     return;
1174349cc55cSDimitry Andric 
1175fe6060f1SDimitry Andric   if (in.stubHelper->isNeeded())
1176fe6060f1SDimitry Andric     in.stubHelper->setup();
1177fcaf7f86SDimitry Andric 
1178fcaf7f86SDimitry Andric   if (in.objCImageInfo->isNeeded())
1179fcaf7f86SDimitry Andric     in.objCImageInfo->finalizeContents();
1180fcaf7f86SDimitry Andric 
11811fd87a68SDimitry Andric   // At this point, we should know exactly which output sections are needed,
11821fd87a68SDimitry Andric   // courtesy of scanSymbols() and scanRelocations().
1183fe6060f1SDimitry Andric   createOutputSections<LP>();
1184349cc55cSDimitry Andric 
1185fe6060f1SDimitry Andric   // After this point, we create no new segments; HOWEVER, we might
1186fe6060f1SDimitry Andric   // yet create branch-range extension thunks for architectures whose
1187fe6060f1SDimitry Andric   // hardware call instructions have limited range, e.g., ARM(64).
1188fe6060f1SDimitry Andric   // The thunks are created as InputSections interspersed among
1189fe6060f1SDimitry Andric   // the ordinary __TEXT,_text InputSections.
1190fe6060f1SDimitry Andric   sortSegmentsAndSections();
1191fe6060f1SDimitry Andric   createLoadCommands<LP>();
1192fe6060f1SDimitry Andric   finalizeAddresses();
119304eeddc0SDimitry Andric   threadPool.async([&] {
119404eeddc0SDimitry Andric     if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
119504eeddc0SDimitry Andric       timeTraceProfilerInitialize(config->timeTraceGranularity, "writeMapFile");
119604eeddc0SDimitry Andric     writeMapFile();
119704eeddc0SDimitry Andric     if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
119804eeddc0SDimitry Andric       timeTraceProfilerFinishThread();
119904eeddc0SDimitry Andric   });
1200fe6060f1SDimitry Andric   finalizeLinkEditSegment();
1201fe6060f1SDimitry Andric   writeOutputFile();
1202fe6060f1SDimitry Andric }
1203fe6060f1SDimitry Andric 
1204fe6060f1SDimitry Andric template <class LP> void macho::writeResult() { Writer().run<LP>(); }
12055ffd83dbSDimitry Andric 
1206349cc55cSDimitry Andric void macho::resetWriter() { LCDylib::resetInstanceCount(); }
1207349cc55cSDimitry Andric 
12085ffd83dbSDimitry Andric void macho::createSyntheticSections() {
1209e8d8bef9SDimitry Andric   in.header = make<MachHeaderSection>();
12101fd87a68SDimitry Andric   if (config->dedupLiterals)
1211fe6060f1SDimitry Andric     in.cStringSection = make<DeduplicatedCStringSection>();
12121fd87a68SDimitry Andric   else
1213fe6060f1SDimitry Andric     in.cStringSection = make<CStringSection>();
1214fe6060f1SDimitry Andric   in.wordLiteralSection =
1215fe6060f1SDimitry Andric       config->dedupLiterals ? make<WordLiteralSection>() : nullptr;
1216e8d8bef9SDimitry Andric   in.rebase = make<RebaseSection>();
12175ffd83dbSDimitry Andric   in.binding = make<BindingSection>();
1218e8d8bef9SDimitry Andric   in.weakBinding = make<WeakBindingSection>();
1219e8d8bef9SDimitry Andric   in.lazyBinding = make<LazyBindingSection>();
1220e8d8bef9SDimitry Andric   in.exports = make<ExportSection>();
12215ffd83dbSDimitry Andric   in.got = make<GotSection>();
1222e8d8bef9SDimitry Andric   in.tlvPointers = make<TlvPointerSection>();
12235ffd83dbSDimitry Andric   in.lazyPointers = make<LazyPointerSection>();
12245ffd83dbSDimitry Andric   in.stubs = make<StubsSection>();
12255ffd83dbSDimitry Andric   in.stubHelper = make<StubHelperSection>();
1226fe6060f1SDimitry Andric   in.unwindInfo = makeUnwindInfoSection();
1227fcaf7f86SDimitry Andric   in.objCImageInfo = make<ObjCImageInfoSection>();
1228fe6060f1SDimitry Andric 
1229fe6060f1SDimitry Andric   // This section contains space for just a single word, and will be used by
1230fe6060f1SDimitry Andric   // dyld to cache an address to the image loader it uses.
123104eeddc0SDimitry Andric   uint8_t *arr = bAlloc().Allocate<uint8_t>(target->wordSize);
1232fe6060f1SDimitry Andric   memset(arr, 0, target->wordSize);
123381ad6265SDimitry Andric   in.imageLoaderCache = makeSyntheticInputSection(
123481ad6265SDimitry Andric       segment_names::data, section_names::data, S_REGULAR,
1235fe6060f1SDimitry Andric       ArrayRef<uint8_t>{arr, target->wordSize},
123681ad6265SDimitry Andric       /*align=*/target->wordSize);
1237fe6060f1SDimitry Andric   // References from dyld are not visible to us, so ensure this section is
1238fe6060f1SDimitry Andric   // always treated as live.
1239fe6060f1SDimitry Andric   in.imageLoaderCache->live = true;
12405ffd83dbSDimitry Andric }
1241e8d8bef9SDimitry Andric 
1242e8d8bef9SDimitry Andric OutputSection *macho::firstTLVDataSection = nullptr;
1243fe6060f1SDimitry Andric 
1244fe6060f1SDimitry Andric template void macho::writeResult<LP64>();
1245fe6060f1SDimitry Andric template void macho::writeResult<ILP32>();
1246