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