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