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