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