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