1//===- SyntheticSections.h -------------------------------------*- C++ -*-===//
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#ifndef LLD_MACHO_SYNTHETIC_SECTIONS_H
10#define LLD_MACHO_SYNTHETIC_SECTIONS_H
11
12#include "Config.h"
13#include "ExportTrie.h"
14#include "InputSection.h"
15#include "OutputSection.h"
16#include "OutputSegment.h"
17#include "Target.h"
18#include "Writer.h"
19
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/SetVector.h"
23#include "llvm/BinaryFormat/MachO.h"
24#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/raw_ostream.h"
26
27#include <optional>
28
29namespace llvm {
30class DWARFUnit;
31} // namespace llvm
32
33namespace lld::macho {
34
35class Defined;
36class DylibSymbol;
37class LoadCommand;
38class ObjFile;
39class UnwindInfoSection;
40
41class SyntheticSection : public OutputSection {
42public:
43 SyntheticSection(const char *segname, const char *name);
44 virtual ~SyntheticSection() = default;
45
46 static bool classof(const OutputSection *sec) {
47 return sec->kind() == SyntheticKind;
48 }
49
50 StringRef segname;
51 // This fake InputSection makes it easier for us to write code that applies
52 // generically to both user inputs and synthetics.
53 InputSection *isec;
54};
55
56// All sections in __LINKEDIT should inherit from this.
57class LinkEditSection : public SyntheticSection {
58public:
59 LinkEditSection(const char *segname, const char *name)
60 : SyntheticSection(segname, name) {
61 align = target->wordSize;
62 }
63
64 // Implementations of this method can assume that the regular (non-__LINKEDIT)
65 // sections already have their addresses assigned.
66 virtual void finalizeContents() {}
67
68 // Sections in __LINKEDIT are special: their offsets are recorded in the
69 // load commands like LC_DYLD_INFO_ONLY and LC_SYMTAB, instead of in section
70 // headers.
71 bool isHidden() const final { return true; }
72
73 virtual uint64_t getRawSize() const = 0;
74
75 // codesign (or more specifically libstuff) checks that each section in
76 // __LINKEDIT ends where the next one starts -- no gaps are permitted. We
77 // therefore align every section's start and end points to WordSize.
78 //
79 // NOTE: This assumes that the extra bytes required for alignment can be
80 // zero-valued bytes.
81 uint64_t getSize() const final { return llvm::alignTo(Value: getRawSize(), Align: align); }
82};
83
84// The header of the Mach-O file, which must have a file offset of zero.
85class MachHeaderSection final : public SyntheticSection {
86public:
87 MachHeaderSection();
88 bool isHidden() const override { return true; }
89 uint64_t getSize() const override;
90 void writeTo(uint8_t *buf) const override;
91
92 void addLoadCommand(LoadCommand *);
93
94protected:
95 std::vector<LoadCommand *> loadCommands;
96 uint32_t sizeOfCmds = 0;
97};
98
99// A hidden section that exists solely for the purpose of creating the
100// __PAGEZERO segment, which is used to catch null pointer dereferences.
101class PageZeroSection final : public SyntheticSection {
102public:
103 PageZeroSection();
104 bool isHidden() const override { return true; }
105 bool isNeeded() const override { return target->pageZeroSize != 0; }
106 uint64_t getSize() const override { return target->pageZeroSize; }
107 uint64_t getFileSize() const override { return 0; }
108 void writeTo(uint8_t *buf) const override {}
109};
110
111// The __got section, populated by dyld with addresses to non-lazily-loaded
112// dylib symbols, including TLV descriptors.
113class GotSection final : public SyntheticSection {
114public:
115 GotSection();
116 const llvm::SetVector<const Symbol *> &getEntries() const { return entries; }
117 bool isNeeded() const override { return !entries.empty(); }
118 uint64_t getSize() const override {
119 return entries.size() * target->wordSize;
120 }
121 void writeTo(uint8_t *buf) const override;
122 void addEntry(Symbol *sym);
123 uint64_t getVA(uint32_t gotIndex) const {
124 return addr + gotIndex * target->wordSize;
125 }
126
127private:
128 llvm::SetVector<const Symbol *> entries;
129};
130
131// The arm64e __auth_got section, whose pointers dyld signs with the IA key
132// and address diversity.
133class AuthGotSection final : public SyntheticSection {
134public:
135 AuthGotSection();
136 ArrayRef<const Symbol *> getEntries() const { return entries; }
137 bool isNeeded() const override { return !entries.empty(); }
138 uint64_t getSize() const override {
139 return entries.size() * target->wordSize;
140 }
141 void writeTo(uint8_t *buf) const override;
142 void addEntry(Symbol *sym);
143 bool contains(const Symbol *sym) const { return indices.contains(Val: sym); }
144 uint64_t getVA(const Symbol *sym) const {
145 return addr + indices.at(Val: sym) * target->wordSize;
146 }
147
148private:
149 // Kept here rather than in Symbol, so that other targets don't pay for it.
150 llvm::DenseMap<const Symbol *, uint32_t> indices;
151 std::vector<const Symbol *> entries;
152};
153
154struct Location {
155 const InputSection *isec;
156 uint64_t offset;
157
158 Location(const InputSection *isec, uint64_t offset)
159 : isec(isec), offset(offset) {}
160 uint64_t getVA() const { return isec->getVA(off: offset); }
161};
162
163// Stores rebase opcodes, which tell dyld where absolute addresses have been
164// encoded in the binary. If the binary is not loaded at its preferred address,
165// dyld has to rebase these addresses by adding an offset to them.
166class RebaseSection final : public LinkEditSection {
167public:
168 RebaseSection();
169 void finalizeContents() override;
170 uint64_t getRawSize() const override { return contents.size(); }
171 bool isNeeded() const override { return !locations.empty(); }
172 void writeTo(uint8_t *buf) const override;
173
174 void addEntry(const InputSection *isec, uint64_t offset) {
175 if (config->isPic)
176 locations.emplace_back(args&: isec, args&: offset);
177 }
178
179private:
180 std::vector<Location> locations;
181 SmallVector<char, 128> contents;
182};
183
184struct BindingEntry {
185 int64_t addend;
186 Location target;
187 BindingEntry(int64_t addend, Location target)
188 : addend(addend), target(target) {}
189};
190
191template <class Sym>
192using BindingsMap = llvm::DenseMap<Sym, std::vector<BindingEntry>>;
193
194// Stores bind opcodes for telling dyld which symbols to load non-lazily.
195class BindingSection final : public LinkEditSection {
196public:
197 BindingSection();
198 void finalizeContents() override;
199 uint64_t getRawSize() const override { return contents.size(); }
200 bool isNeeded() const override { return !bindingsMap.empty(); }
201 void writeTo(uint8_t *buf) const override;
202
203 void addEntry(const Symbol *dysym, const InputSection *isec, uint64_t offset,
204 int64_t addend = 0) {
205 bindingsMap[dysym].emplace_back(args&: addend, args: Location(isec, offset));
206 }
207
208private:
209 BindingsMap<const Symbol *> bindingsMap;
210 SmallVector<char, 128> contents;
211};
212
213// Stores bind opcodes for telling dyld which weak symbols need coalescing.
214// There are two types of entries in this section:
215//
216// 1) Non-weak definitions: This is a symbol definition that weak symbols in
217// other dylibs should coalesce to.
218//
219// 2) Weak bindings: These tell dyld that a given symbol reference should
220// coalesce to a non-weak definition if one is found. Note that unlike the
221// entries in the BindingSection, the bindings here only refer to these
222// symbols by name, but do not specify which dylib to load them from.
223class WeakBindingSection final : public LinkEditSection {
224public:
225 WeakBindingSection();
226 void finalizeContents() override;
227 uint64_t getRawSize() const override { return contents.size(); }
228 bool isNeeded() const override {
229 return !bindingsMap.empty() || !definitions.empty();
230 }
231
232 void writeTo(uint8_t *buf) const override;
233
234 void addEntry(const Symbol *symbol, const InputSection *isec, uint64_t offset,
235 int64_t addend = 0) {
236 bindingsMap[symbol].emplace_back(args&: addend, args: Location(isec, offset));
237 }
238
239 bool hasEntry() const { return !bindingsMap.empty(); }
240
241 void addNonWeakDefinition(const Defined *defined) {
242 definitions.emplace_back(args&: defined);
243 }
244
245 bool hasNonWeakDefinition() const { return !definitions.empty(); }
246
247private:
248 BindingsMap<const Symbol *> bindingsMap;
249 std::vector<const Defined *> definitions;
250 SmallVector<char, 128> contents;
251};
252
253// The following sections implement lazy symbol binding -- very similar to the
254// PLT mechanism in ELF.
255//
256// ELF's .plt section is broken up into two sections in Mach-O: StubsSection
257// and StubHelperSection. Calls to functions in dylibs will end up calling into
258// StubsSection, which contains indirect jumps to addresses stored in the
259// LazyPointerSection (the counterpart to ELF's .plt.got).
260//
261// We will first describe how non-weak symbols are handled.
262//
263// At program start, the LazyPointerSection contains addresses that point into
264// one of the entry points in the middle of the StubHelperSection. The code in
265// StubHelperSection will push on the stack an offset into the
266// LazyBindingSection. The push is followed by a jump to the beginning of the
267// StubHelperSection (similar to PLT0), which then calls into dyld_stub_binder.
268// dyld_stub_binder is a non-lazily-bound symbol, so this call looks it up in
269// the GOT.
270//
271// The stub binder will look up the bind opcodes in the LazyBindingSection at
272// the given offset. The bind opcodes will tell the binder to update the
273// address in the LazyPointerSection to point to the symbol, so that subsequent
274// calls don't have to redo the symbol resolution. The binder will then jump to
275// the resolved symbol.
276//
277// With weak symbols, the situation is slightly different. Since there is no
278// "weak lazy" lookup, function calls to weak symbols are always non-lazily
279// bound. We emit both regular non-lazy bindings as well as weak bindings, in
280// order that the weak bindings may overwrite the non-lazy bindings if an
281// appropriate symbol is found at runtime. However, the bound addresses will
282// still be written (non-lazily) into the LazyPointerSection.
283//
284// Symbols are always bound eagerly when chained fixups are used. In that case,
285// StubsSection contains indirect jumps to addresses stored in the GotSection.
286// The GOT directly contains the fixup entries, which will be replaced by the
287// address of the target symbols on load. LazyPointerSection and
288// StubHelperSection are not used.
289
290class StubsSection final : public SyntheticSection {
291public:
292 StubsSection();
293 uint64_t getSize() const override;
294 bool isNeeded() const override { return !entries.empty(); }
295 void finalize() override;
296 void writeTo(uint8_t *buf) const override;
297 const llvm::SetVector<Symbol *> &getEntries() const { return entries; }
298 // Creates a stub for the symbol and the corresponding entry in the
299 // LazyPointerSection.
300 void addEntry(Symbol *);
301 uint64_t getVA(uint32_t stubsIndex) const {
302 assert(isFinal || target->usesThunks());
303 // ConcatOutputSection::finalize() can seek the address of a
304 // stub before its address is assigned. Before __stubs is
305 // finalized, return a contrived out-of-range address.
306 return isFinal ? addr + stubsIndex * target->stubSize
307 : TargetInfo::outOfRangeVA;
308 }
309
310 bool isFinal = false; // is address assigned?
311
312private:
313 llvm::SetVector<Symbol *> entries;
314};
315
316class StubHelperSection final : public SyntheticSection {
317public:
318 StubHelperSection();
319 uint64_t getSize() const override;
320 bool isNeeded() const override;
321 void writeTo(uint8_t *buf) const override;
322
323 void setUp();
324
325 DylibSymbol *stubBinder = nullptr;
326 Defined *dyldPrivate = nullptr;
327};
328
329class ObjCSelRefsHelper {
330public:
331 static void initialize();
332 static void cleanup();
333
334 static ConcatInputSection *getSelRef(StringRef methname);
335 static ConcatInputSection *makeSelRef(StringRef methname);
336
337private:
338 static llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
339 methnameToSelref;
340};
341
342// Objective-C stubs are hoisted objc_msgSend calls per selector called in the
343// program. Apple Clang produces undefined symbols to each stub, such as
344// '_objc_msgSend$foo', which are then synthesized by the linker. The stubs
345// load the particular selector 'foo' from __objc_selrefs, setting it to the
346// first argument of the objc_msgSend call, and then jumps to objc_msgSend. The
347// actual stub contents are mirrored from ld64.
348class ObjCStubsSection final : public SyntheticSection {
349public:
350 ObjCStubsSection();
351 void addEntry(Symbol *sym);
352 uint64_t getSize() const override;
353 bool isNeeded() const override { return !symbols.empty(); }
354 void finalize() override { isec->isFinal = true; }
355 void writeTo(uint8_t *buf) const override;
356 void setUp();
357
358 static constexpr llvm::StringLiteral objcMsgSendStubPrefix = "_objc_msgSend$";
359 static constexpr llvm::StringLiteral objcMsgSendClassStubPrefix =
360 "_objc_msgSendClass$";
361 static constexpr llvm::StringLiteral objcClassSymbolPrefix = "_OBJC_CLASS_$_";
362 // Plain selector stub: _objc_msgSend$<selector>.
363 static bool isObjCMsgSendStubSymbol(Symbol *sym);
364 // Class-message stub: _objc_msgSendClass$<selector>$_OBJC_CLASS_$_<class>.
365 static bool isObjCClassStubSymbol(Symbol *sym);
366 // Any symbol handled by ObjCStubsSection.
367 static bool isObjCStubSymbol(Symbol *sym);
368 static StringRef getMethname(Symbol *sym);
369 ArrayRef<Defined *> getSymbols() const { return symbols; }
370 struct ObjCClassStubNames {
371 StringRef selectorName;
372 StringRef classSymbolName;
373 };
374 static std::optional<ObjCClassStubNames>
375 parseObjCClassStubSymbol(Symbol *sym);
376 void recordClassSymbol(Symbol *stubSym, Symbol *classSym);
377 Symbol *lookupClassSymbol(Symbol *stubSym) const;
378
379 /// Stably sort the stubs by \p priorities and reassign their offsets. Must
380 /// run before addresses are assigned.
381 void sortSymbols(const llvm::DenseMap<const Symbol *, int> &priorities);
382
383private:
384 size_t getStubSize(Symbol *sym) const;
385
386 std::vector<Defined *> symbols;
387 llvm::DenseMap<Symbol *, Symbol *> classSymbols;
388 // Total byte size of all stubs added so far.
389 size_t stubsSectionSize = 0;
390 Symbol *objcMsgSend = nullptr;
391};
392
393// Note that this section may also be targeted by non-lazy bindings. In
394// particular, this happens when branch relocations target weak symbols.
395class LazyPointerSection final : public SyntheticSection {
396public:
397 LazyPointerSection();
398 uint64_t getSize() const override;
399 bool isNeeded() const override;
400 void writeTo(uint8_t *buf) const override;
401 uint64_t getVA(uint32_t index) const {
402 return addr + (index << target->p2WordSize);
403 }
404};
405
406class LazyBindingSection final : public LinkEditSection {
407public:
408 LazyBindingSection();
409 void finalizeContents() override;
410 uint64_t getRawSize() const override { return contents.size(); }
411 bool isNeeded() const override { return !entries.empty(); }
412 void writeTo(uint8_t *buf) const override;
413 // Note that every entry here will by referenced by a corresponding entry in
414 // the StubHelperSection.
415 void addEntry(Symbol *dysym);
416 const llvm::SetVector<Symbol *> &getEntries() const { return entries; }
417
418private:
419 uint32_t encode(const Symbol &);
420
421 llvm::SetVector<Symbol *> entries;
422 SmallVector<char, 128> contents;
423 llvm::raw_svector_ostream os{contents};
424};
425
426// Stores a trie that describes the set of exported symbols.
427class ExportSection final : public LinkEditSection {
428public:
429 ExportSection();
430 void finalizeContents() override;
431 uint64_t getRawSize() const override { return size; }
432 bool isNeeded() const override { return size; }
433 void writeTo(uint8_t *buf) const override;
434
435 bool hasWeakSymbol = false;
436
437private:
438 TrieBuilder trieBuilder;
439 size_t size = 0;
440};
441
442// Stores 'data in code' entries that describe the locations of data regions
443// inside code sections. This is used by llvm-objdump to distinguish jump tables
444// and stop them from being disassembled as instructions.
445class DataInCodeSection final : public LinkEditSection {
446public:
447 DataInCodeSection();
448 void finalizeContents() override;
449 uint64_t getRawSize() const override {
450 return sizeof(llvm::MachO::data_in_code_entry) * entries.size();
451 }
452 void writeTo(uint8_t *buf) const override;
453
454private:
455 std::vector<llvm::MachO::data_in_code_entry> entries;
456};
457
458// Stores ULEB128 delta encoded addresses of functions.
459class FunctionStartsSection final : public LinkEditSection {
460public:
461 FunctionStartsSection();
462 void finalizeContents() override;
463 uint64_t getRawSize() const override { return contents.size(); }
464 void writeTo(uint8_t *buf) const override;
465
466private:
467 SmallVector<char, 128> contents;
468};
469
470// Stores the strings referenced by the symbol table.
471class StringTableSection final : public LinkEditSection {
472public:
473 StringTableSection();
474 // Returns the start offset of the added string.
475 uint32_t addString(StringRef);
476 uint64_t getRawSize() const override { return size; }
477 void writeTo(uint8_t *buf) const override;
478
479 static constexpr size_t emptyStringIndex = 1;
480
481private:
482 // ld64 emits string tables which start with a space and a zero byte. We
483 // match its behavior here since some tools depend on it.
484 // Consequently, the empty string will be at index 1, not zero.
485 std::vector<StringRef> strings{" "};
486 llvm::DenseMap<llvm::CachedHashStringRef, uint32_t> stringMap;
487 size_t size = 2;
488};
489
490struct SymtabEntry {
491 Symbol *sym;
492 size_t strx;
493};
494
495struct StabsEntry {
496 uint8_t type = 0;
497 uint32_t strx = StringTableSection::emptyStringIndex;
498 uint8_t sect = 0;
499 uint16_t desc = 0;
500 uint64_t value = 0;
501
502 StabsEntry() = default;
503 explicit StabsEntry(uint8_t type) : type(type) {}
504};
505
506// Symbols of the same type must be laid out contiguously: we choose to emit
507// all local symbols first, then external symbols, and finally undefined
508// symbols. For each symbol type, the LC_DYSYMTAB load command will record the
509// range (start index and total number) of those symbols in the symbol table.
510class SymtabSection : public LinkEditSection {
511public:
512 void finalizeContents() override;
513 uint32_t getNumSymbols() const;
514 uint32_t getNumLocalSymbols() const {
515 return stabs.size() + localSymbols.size();
516 }
517 uint32_t getNumExternalSymbols() const { return externalSymbols.size(); }
518 uint32_t getNumUndefinedSymbols() const { return undefinedSymbols.size(); }
519
520private:
521 void emitBeginSourceStab(StringRef);
522 void emitEndSourceStab();
523 void emitObjectFileStab(ObjFile *);
524 void emitEndFunStab(Defined *);
525 Defined *getFuncBodySym(Defined *);
526 void emitStabs();
527
528protected:
529 SymtabSection(StringTableSection &);
530
531 StringTableSection &stringTableSection;
532 // STABS symbols are always local symbols, but we represent them with special
533 // entries because they may use fields like n_sect and n_desc differently.
534 std::vector<StabsEntry> stabs;
535 std::vector<SymtabEntry> localSymbols;
536 std::vector<SymtabEntry> externalSymbols;
537 std::vector<SymtabEntry> undefinedSymbols;
538};
539
540template <class LP> SymtabSection *makeSymtabSection(StringTableSection &);
541
542// The indirect symbol table is a list of 32-bit integers that serve as indices
543// into the (actual) symbol table. The indirect symbol table is a
544// concatenation of several sub-arrays of indices, each sub-array belonging to
545// a separate section. The starting offset of each sub-array is stored in the
546// reserved1 header field of the respective section.
547//
548// These sub-arrays provide symbol information for sections that store
549// contiguous sequences of symbol references. These references can be pointers
550// (e.g. those in the GOT and TLVP sections) or assembly sequences (e.g.
551// function stubs).
552class IndirectSymtabSection final : public LinkEditSection {
553public:
554 IndirectSymtabSection();
555 void finalizeContents() override;
556 uint32_t getNumSymbols() const;
557 uint64_t getRawSize() const override {
558 return getNumSymbols() * sizeof(uint32_t);
559 }
560 bool isNeeded() const override;
561 void writeTo(uint8_t *buf) const override;
562};
563
564// The code signature comes at the very end of the linked output file.
565class CodeSignatureSection final : public LinkEditSection {
566public:
567 // NOTE: These values are duplicated in llvm-objcopy's MachO/Object.h file
568 // and any changes here, should be repeated there.
569 static constexpr uint8_t blockSizeShift = 12;
570 static constexpr size_t blockSize = (1 << blockSizeShift); // 4 KiB
571 static constexpr size_t hashSize = 256 / 8;
572 static constexpr size_t blobHeadersSize = llvm::alignTo<8>(
573 Value: sizeof(llvm::MachO::CS_SuperBlob) + sizeof(llvm::MachO::CS_BlobIndex));
574 static constexpr uint32_t fixedHeadersSize =
575 blobHeadersSize + sizeof(llvm::MachO::CS_CodeDirectory);
576
577 uint32_t fileNamePad = 0;
578 uint32_t allHeadersSize = 0;
579 StringRef fileName;
580
581 CodeSignatureSection();
582 uint64_t getRawSize() const override;
583 bool isNeeded() const override { return true; }
584 void writeTo(uint8_t *buf) const override;
585 uint32_t getBlockCount() const;
586 void writeHashes(uint8_t *buf) const;
587};
588
589class CStringSection : public SyntheticSection {
590public:
591 CStringSection(const char *name);
592 void addInput(CStringInputSection *);
593 uint64_t getSize() const override { return size; }
594 virtual void finalizeContents();
595 bool isNeeded() const override { return !inputs.empty(); }
596 void writeTo(uint8_t *buf) const override;
597
598 std::vector<CStringInputSection *> inputs;
599
600private:
601 uint64_t size;
602};
603
604class DeduplicatedCStringSection final : public CStringSection {
605public:
606 DeduplicatedCStringSection(const char *name) : CStringSection(name){};
607 uint64_t getSize() const override { return size; }
608 void finalizeContents() override;
609 void writeTo(uint8_t *buf) const override;
610 uint64_t getStringOffset(StringRef str) const;
611
612private:
613 llvm::DenseMap<llvm::CachedHashStringRef, uint64_t> stringOffsetMap;
614 size_t size = 0;
615};
616
617/*
618 * This section contains deduplicated literal values. The 16-byte values are
619 * laid out first, followed by the 8- and then the 4-byte ones.
620 */
621class WordLiteralSection final : public SyntheticSection {
622public:
623 using UInt128 = std::pair<uint64_t, uint64_t>;
624 // I don't think the standard guarantees the size of a pair, so let's make
625 // sure it's exact -- that way we can construct it via `mmap`.
626 static_assert(sizeof(UInt128) == 16);
627
628 WordLiteralSection();
629 void addInput(WordLiteralInputSection *);
630 void finalizeContents();
631 void writeTo(uint8_t *buf) const override;
632
633 uint64_t getSize() const override {
634 return literal16Map.size() * 16 + literal8Map.size() * 8 +
635 literal4Map.size() * 4;
636 }
637
638 bool isNeeded() const override {
639 return !literal16Map.empty() || !literal4Map.empty() ||
640 !literal8Map.empty();
641 }
642
643 uint64_t getLiteral16Offset(uintptr_t buf) const {
644 return literal16Map.at(Val: *reinterpret_cast<const UInt128 *>(buf)) * 16;
645 }
646
647 uint64_t getLiteral8Offset(uintptr_t buf) const {
648 return literal16Map.size() * 16 +
649 literal8Map.at(Val: *reinterpret_cast<const uint64_t *>(buf)) * 8;
650 }
651
652 uint64_t getLiteral4Offset(uintptr_t buf) const {
653 return literal16Map.size() * 16 + literal8Map.size() * 8 +
654 literal4Map.at(Val: *reinterpret_cast<const uint32_t *>(buf)) * 4;
655 }
656
657private:
658 std::vector<WordLiteralInputSection *> inputs;
659
660 // Literal values can be any bit pattern.
661 llvm::DenseMap<UInt128, uint64_t> literal16Map;
662 llvm::DenseMap<uint64_t, uint64_t> literal8Map;
663 llvm::DenseMap<uint32_t, uint64_t> literal4Map;
664};
665
666class ObjCImageInfoSection final : public SyntheticSection {
667public:
668 ObjCImageInfoSection();
669 bool isNeeded() const override { return !files.empty(); }
670 uint64_t getSize() const override { return 8; }
671 void addFile(const InputFile *file) {
672 assert(!file->objCImageInfo.empty());
673 files.push_back(x: file);
674 }
675 void finalizeContents();
676 void writeTo(uint8_t *buf) const override;
677
678private:
679 struct ImageInfo {
680 uint8_t swiftVersion = 0;
681 bool hasCategoryClassProperties = false;
682 bool hasSignedClassROs = false;
683 } info;
684 static ImageInfo parseImageInfo(const InputFile *);
685 std::vector<const InputFile *> files; // files with image info
686};
687
688// This section stores 32-bit __TEXT segment offsets of initializer functions.
689//
690// The compiler stores pointers to initializers in __mod_init_func. These need
691// to be fixed up at load time, which takes time and dirties memory. By
692// synthesizing InitOffsetsSection from them, this data can live in the
693// read-only __TEXT segment instead. This section is used by default when
694// chained fixups are enabled.
695//
696// There is no similar counterpart to __mod_term_func, as that section is
697// deprecated, and static destructors are instead handled by registering them
698// via __cxa_atexit from an autogenerated initializer function (see D121736).
699class InitOffsetsSection final : public SyntheticSection {
700public:
701 InitOffsetsSection();
702 bool isNeeded() const override { return !sections.empty(); }
703 uint64_t getSize() const override;
704 void writeTo(uint8_t *buf) const override;
705 void setUp();
706
707 void addInput(ConcatInputSection *isec) { sections.push_back(x: isec); }
708 const std::vector<ConcatInputSection *> &inputs() const { return sections; }
709
710private:
711 std::vector<ConcatInputSection *> sections;
712};
713
714// This SyntheticSection is for the __objc_methlist section, which contains
715// relative method lists if the -objc_relative_method_lists option is enabled.
716class ObjCMethListSection final : public SyntheticSection {
717public:
718 ObjCMethListSection();
719
720 static bool isMethodList(const ConcatInputSection *isec);
721 void addInput(ConcatInputSection *isec) { inputs.push_back(x: isec); }
722 std::vector<ConcatInputSection *> getInputs() { return inputs; }
723
724 void setUp();
725 void finalize() override;
726 bool isNeeded() const override { return !inputs.empty(); }
727 uint64_t getSize() const override { return sectionSize; }
728 void writeTo(uint8_t *bufStart) const override;
729
730private:
731 void readMethodListHeader(const uint8_t *buf, uint32_t &structSizeAndFlags,
732 uint32_t &structCount) const;
733 void writeMethodListHeader(uint8_t *buf, uint32_t structSizeAndFlags,
734 uint32_t structCount) const;
735 uint32_t computeRelativeMethodListSize(uint32_t absoluteMethodListSize) const;
736 void writeRelativeOffsetForIsec(const ConcatInputSection *isec, uint8_t *buf,
737 uint32_t &inSecOff, uint32_t &outSecOff,
738 bool useSelRef) const;
739 uint32_t writeRelativeMethodList(const ConcatInputSection *isec,
740 uint8_t *buf) const;
741
742 static constexpr uint32_t methodListHeaderSize =
743 /*structSizeAndFlags*/ sizeof(uint32_t) +
744 /*structCount*/ sizeof(uint32_t);
745 // Relative method lists are supported only for 3-pointer method lists
746 static constexpr uint32_t pointersPerStruct = 3;
747 // The runtime identifies relative method lists via this magic value
748 static constexpr uint32_t relMethodHeaderFlag = 0x80000000;
749 // In the method list header, the first 2 bytes are the size of struct
750 static constexpr uint32_t structSizeMask = 0x0000FFFF;
751 // In the method list header, the last 2 bytes are the flags for the struct
752 static constexpr uint32_t structFlagsMask = 0xFFFF0000;
753 // Relative method lists have 4 byte alignment as all data in the InputSection
754 // is 4 byte
755 static constexpr uint32_t relativeOffsetSize = sizeof(uint32_t);
756
757 // The output size of the __objc_methlist section, computed during finalize()
758 uint32_t sectionSize = 0;
759 std::vector<ConcatInputSection *> inputs;
760};
761
762// Chained fixups are a replacement for classic dyld opcodes. In this format,
763// most of the metadata necessary for binding symbols and rebasing addresses is
764// stored directly in the memory location that will have the fixup applied.
765//
766// The fixups form singly linked lists; each one covering a single page in
767// memory. The __LINKEDIT,__chainfixups section stores the page offset of the
768// first fixup of each page; the rest can be found by walking the chain using
769// the offset that is embedded in each entry.
770//
771// This setup allows pages to be relocated lazily at page-in time and without
772// being dirtied. The kernel can discard and load them again as needed. This
773// technique, called page-in linking, was introduced in macOS 13.
774//
775// The benefits of this format are:
776// - smaller __LINKEDIT segment, as most of the fixup information is stored in
777// the data segment
778// - faster startup, since not all relocations need to be done upfront
779// - slightly lower memory usage, as fewer pages are dirtied
780//
781// Userspace x86_64 and arm64 binaries have two types of fixup entries:
782// - Rebase entries contain an absolute address, to which the object's load
783// address will be added to get the final value. This is used for loading
784// the address of a symbol defined in the same binary.
785// - Binding entries are mostly used for symbols imported from other dylibs,
786// but for weakly bound and interposable symbols as well. They are looked up
787// by a (symbol name, library) pair stored in __chainfixups. This import
788// entry also encodes whether the import is weak (i.e. if the symbol is
789// missing, it should be set to null instead of producing a load error).
790// The fixup encodes an ordinal associated with the import, and an optional
791// addend.
792//
793// The entries are tightly packed 64-bit bitfields. One of the bits specifies
794// which kind of fixup to interpret them as.
795//
796// LLD generates the fixup data in 5 stages:
797// 1. While scanning relocations, we make a note of each location that needs
798// a fixup by calling addRebase() or addBinding(). During this, we assign
799// a unique ordinal for each (symbol name, library, addend) import tuple.
800// 2. After addresses have been assigned to all sections, and thus the memory
801// layout of the linked image is final; finalizeContents() is called. Here,
802// the page offsets of the chain start entries are calculated.
803// 3. ChainedFixupsSection::writeTo() writes the page start offsets and the
804// imports table to the output file.
805// 4. Each section's fixup entries are encoded and written to disk in
806// ConcatInputSection::writeTo(), but without writing the offsets that form
807// the chain.
808// 5. Finally, each page's (which might correspond to multiple sections)
809// fixups are linked together in Writer::buildFixupChains().
810class ChainedFixupsSection final : public LinkEditSection {
811public:
812 ChainedFixupsSection();
813 void finalizeContents() override;
814 uint64_t getRawSize() const override { return size; }
815 bool isNeeded() const override;
816 void writeTo(uint8_t *buf) const override;
817
818 void addRebase(const InputSection *isec, uint64_t offset) {
819 locations.emplace_back(args&: isec, args&: offset);
820 }
821 void addBinding(const Symbol *dysym, const InputSection *isec,
822 uint64_t offset, int64_t addend = 0,
823 bool forceOutline = false);
824
825 void setHasNonWeakDefinition() { hasNonWeakDef = true; }
826
827 // Returns the import ordinal and the addend for the bind's own addend field,
828 // which is 0 when the import holds the addend.
829 std::pair<uint32_t, int32_t> getBinding(const Symbol *sym, int64_t addend,
830 bool forceOutline = false) const;
831
832 const std::vector<Location> &getLocations() const { return locations; }
833
834 // Each fixup's "next" field holds the distance to the next fixup on its page
835 // in units of this stride, so every fixup must be aligned to it.
836 static uint32_t chainStride();
837
838 bool hasWeakBinding() const { return hasWeakBind; }
839 bool hasNonWeakDefinition() const { return hasNonWeakDef; }
840
841private:
842 // Location::offset initially stores the offset within an InputSection, but
843 // contains output segment offsets after finalizeContents().
844 std::vector<Location> locations;
845 // (target symbol, addend) => import ordinal
846 llvm::MapVector<std::pair<const Symbol *, int64_t>, uint32_t> bindings;
847
848 struct SegmentInfo {
849 SegmentInfo(const OutputSegment *oseg) : oseg(oseg) {}
850
851 const OutputSegment *oseg;
852 // (page index, fixup starts offset)
853 llvm::SmallVector<std::pair<uint16_t, uint16_t>> pageStarts;
854
855 size_t getSize() const;
856 size_t writeTo(uint8_t *buf) const;
857 };
858 llvm::SmallVector<SegmentInfo, 4> fixupSegments;
859
860 size_t symtabSize = 0;
861 size_t size = 0;
862
863 bool needsAddend = false;
864 bool needsLargeAddend = false;
865 bool hasWeakBind = false;
866 bool hasNonWeakDef = false;
867 llvm::MachO::ChainedImportFormat importFormat;
868};
869
870void writeChainedRebase(uint8_t *buf, uint64_t targetVA,
871 const AuthInfo *auth = nullptr);
872void writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend,
873 const AuthInfo *auth = nullptr);
874
875// Whether -no_fixup_chains can avoid a fixup that chained fixups can't encode.
876// It can't on arm64e, which always uses chained fixups.
877bool canDisableChainedFixups();
878
879struct InStruct {
880 const uint8_t *bufferStart = nullptr;
881 MachHeaderSection *header = nullptr;
882 /// The list of cstring sections. Note that this includes \p cStringSection
883 /// and \p objcMethnameSection already.
884 llvm::SmallVector<CStringSection *> cStringSections;
885 CStringSection *cStringSection = nullptr;
886 DeduplicatedCStringSection *objcMethnameSection = nullptr;
887 WordLiteralSection *wordLiteralSection = nullptr;
888 RebaseSection *rebase = nullptr;
889 BindingSection *binding = nullptr;
890 WeakBindingSection *weakBinding = nullptr;
891 LazyBindingSection *lazyBinding = nullptr;
892 ExportSection *exports = nullptr;
893 GotSection *got = nullptr;
894 AuthGotSection *authGot = nullptr;
895 LazyPointerSection *lazyPointers = nullptr;
896 StubsSection *stubs = nullptr;
897 StubHelperSection *stubHelper = nullptr;
898 ObjCStubsSection *objcStubs = nullptr;
899 UnwindInfoSection *unwindInfo = nullptr;
900 ObjCImageInfoSection *objCImageInfo = nullptr;
901 ConcatInputSection *imageLoaderCache = nullptr;
902 InitOffsetsSection *initOffsets = nullptr;
903 ObjCMethListSection *objcMethList = nullptr;
904 ChainedFixupsSection *chainedFixups = nullptr;
905
906 CStringSection *getOrCreateCStringSection(StringRef name,
907 bool forceDedupStrings = false) {
908 auto [it, didEmplace] =
909 cStringSectionMap.try_emplace(Key: name, Args: cStringSections.size());
910 if (!didEmplace)
911 return cStringSections[it->getValue()];
912
913 std::string &nameData = *make<std::string>(args&: name);
914 CStringSection *sec;
915 if (config->dedupStrings || forceDedupStrings)
916 sec = make<DeduplicatedCStringSection>(args: nameData.c_str());
917 else
918 sec = make<CStringSection>(args: nameData.c_str());
919 cStringSections.push_back(Elt: sec);
920 return sec;
921 }
922
923private:
924 llvm::StringMap<unsigned> cStringSectionMap;
925};
926
927extern InStruct in;
928extern std::vector<SyntheticSection *> syntheticSections;
929
930void createSyntheticSymbols();
931
932} // namespace lld::macho
933
934#endif
935