1 //===- SyntheticSections.h -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_MACHO_SYNTHETIC_SECTIONS_H
10 #define LLD_MACHO_SYNTHETIC_SECTIONS_H
13 #include "ExportTrie.h"
14 #include "InputSection.h"
15 #include "OutputSection.h"
16 #include "OutputSegment.h"
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"
27 #include <unordered_map>
33 namespace lld::macho
{
39 class UnwindInfoSection
;
41 class SyntheticSection
: public OutputSection
{
43 SyntheticSection(const char *segname
, const char *name
);
44 virtual ~SyntheticSection() = default;
46 static bool classof(const OutputSection
*sec
) {
47 return sec
->kind() == SyntheticKind
;
51 // This fake InputSection makes it easier for us to write code that applies
52 // generically to both user inputs and synthetics.
56 // All sections in __LINKEDIT should inherit from this.
57 class LinkEditSection
: public SyntheticSection
{
59 LinkEditSection(const char *segname
, const char *name
)
60 : SyntheticSection(segname
, name
) {
61 align
= target
->wordSize
;
64 // Implementations of this method can assume that the regular (non-__LINKEDIT)
65 // sections already have their addresses assigned.
66 virtual void finalizeContents() {}
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
71 bool isHidden() const final
{ return true; }
73 virtual uint64_t getRawSize() const = 0;
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.
79 // NOTE: This assumes that the extra bytes required for alignment can be
81 uint64_t getSize() const final
{ return llvm::alignTo(getRawSize(), align
); }
84 // The header of the Mach-O file, which must have a file offset of zero.
85 class MachHeaderSection final
: public SyntheticSection
{
88 bool isHidden() const override
{ return true; }
89 uint64_t getSize() const override
;
90 void writeTo(uint8_t *buf
) const override
;
92 void addLoadCommand(LoadCommand
*);
95 std::vector
<LoadCommand
*> loadCommands
;
96 uint32_t sizeOfCmds
= 0;
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
{
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
{}
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
{
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
;
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
;
130 llvm::SetVector
<const Symbol
*> entries
;
133 class GotSection final
: public NonLazyPointerSectionBase
{
138 class TlvPointerSection final
: public NonLazyPointerSectionBase
{
144 const InputSection
*isec
;
147 Location(const InputSection
*isec
, uint64_t offset
)
148 : isec(isec
), offset(offset
) {}
149 uint64_t getVA() const { return isec
->getVA(offset
); }
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
{
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
;
163 void addEntry(const InputSection
*isec
, uint64_t offset
) {
165 locations
.emplace_back(isec
, offset
);
169 std::vector
<Location
> locations
;
170 SmallVector
<char, 128> contents
;
173 struct BindingEntry
{
176 BindingEntry(int64_t addend
, Location target
)
177 : addend(addend
), target(target
) {}
181 using BindingsMap
= llvm::DenseMap
<Sym
, std::vector
<BindingEntry
>>;
183 // Stores bind opcodes for telling dyld which symbols to load non-lazily.
184 class BindingSection final
: public LinkEditSection
{
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
;
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
));
198 BindingsMap
<const Symbol
*> bindingsMap
;
199 SmallVector
<char, 128> contents
;
202 // Stores bind opcodes for telling dyld which weak symbols need coalescing.
203 // There are two types of entries in this section:
205 // 1) Non-weak definitions: This is a symbol definition that weak symbols in
206 // other dylibs should coalesce to.
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
{
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();
221 void writeTo(uint8_t *buf
) const override
;
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
));
228 bool hasEntry() const { return !bindingsMap
.empty(); }
230 void addNonWeakDefinition(const Defined
*defined
) {
231 definitions
.emplace_back(defined
);
234 bool hasNonWeakDefinition() const { return !definitions
.empty(); }
237 BindingsMap
<const Symbol
*> bindingsMap
;
238 std::vector
<const Defined
*> definitions
;
239 SmallVector
<char, 128> contents
;
242 // The following sections implement lazy symbol binding -- very similar to the
243 // PLT mechanism in ELF.
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).
250 // We will first describe how non-weak symbols are handled.
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
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.
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.
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.
279 class StubsSection final
: public SyntheticSection
{
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
;
299 bool isFinal
= false; // is address assigned?
302 llvm::SetVector
<Symbol
*> entries
;
305 class StubHelperSection final
: public SyntheticSection
{
308 uint64_t getSize() const override
;
309 bool isNeeded() const override
;
310 void writeTo(uint8_t *buf
) const override
;
314 DylibSymbol
*stubBinder
= nullptr;
315 Defined
*dyldPrivate
= nullptr;
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
{
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
;
334 static constexpr llvm::StringLiteral symbolPrefix
= "_objc_msgSend$";
337 std::vector
<Defined
*> symbols
;
338 std::vector
<uint32_t> offsets
;
339 int objcMsgSendGotIndex
= 0;
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
{
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
);
355 class LazyBindingSection final
: public LinkEditSection
{
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
; }
368 uint32_t encode(const Symbol
&);
370 llvm::SetVector
<Symbol
*> entries
;
371 SmallVector
<char, 128> contents
;
372 llvm::raw_svector_ostream os
{contents
};
375 // Stores a trie that describes the set of exported symbols.
376 class ExportSection final
: public LinkEditSection
{
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
;
384 bool hasWeakSymbol
= false;
387 TrieBuilder trieBuilder
;
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
{
397 void finalizeContents() override
;
398 uint64_t getRawSize() const override
{
399 return sizeof(llvm::MachO::data_in_code_entry
) * entries
.size();
401 void writeTo(uint8_t *buf
) const override
;
404 std::vector
<llvm::MachO::data_in_code_entry
> entries
;
407 // Stores ULEB128 delta encoded addresses of functions.
408 class FunctionStartsSection final
: public LinkEditSection
{
410 FunctionStartsSection();
411 void finalizeContents() override
;
412 uint64_t getRawSize() const override
{ return contents
.size(); }
413 void writeTo(uint8_t *buf
) const override
;
416 SmallVector
<char, 128> contents
;
419 // Stores the strings referenced by the symbol table.
420 class StringTableSection final
: public LinkEditSection
{
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
;
428 static constexpr size_t emptyStringIndex
= 1;
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
{" "};
445 uint32_t strx
= StringTableSection::emptyStringIndex
;
450 StabsEntry() = default;
451 explicit StabsEntry(uint8_t type
) : type(type
) {}
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
{
460 void finalizeContents() override
;
461 uint32_t getNumSymbols() const;
462 uint32_t getNumLocalSymbols() const {
463 return stabs
.size() + localSymbols
.size();
465 uint32_t getNumExternalSymbols() const { return externalSymbols
.size(); }
466 uint32_t getNumUndefinedSymbols() const { return undefinedSymbols
.size(); }
469 void emitBeginSourceStab(StringRef
);
470 void emitEndSourceStab();
471 void emitObjectFileStab(ObjFile
*);
472 void emitEndFunStab(Defined
*);
476 SymtabSection(StringTableSection
&);
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
;
487 template <class LP
> SymtabSection
*makeSymtabSection(StringTableSection
&);
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.
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.
499 class IndirectSymtabSection final
: public LinkEditSection
{
501 IndirectSymtabSection();
502 void finalizeContents() override
;
503 uint32_t getNumSymbols() const;
504 uint64_t getRawSize() const override
{
505 return getNumSymbols() * sizeof(uint32_t);
507 bool isNeeded() const override
;
508 void writeTo(uint8_t *buf
) const override
;
511 // The code signature comes at the very end of the linked output file.
512 class CodeSignatureSection final
: public LinkEditSection
{
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
);
524 uint32_t fileNamePad
= 0;
525 uint32_t allHeadersSize
= 0;
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;
536 class CStringSection
: public SyntheticSection
{
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
;
545 std::vector
<CStringInputSection
*> inputs
;
551 class DeduplicatedCStringSection final
: public CStringSection
{
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
;
558 struct StringOffset
{
559 uint8_t trailingZeros
;
560 uint64_t outSecOff
= UINT64_MAX
;
562 explicit StringOffset(uint8_t zeros
) : trailingZeros(zeros
) {}
565 StringOffset
getStringOffset(StringRef str
) const;
568 llvm::DenseMap
<llvm::CachedHashStringRef
, StringOffset
> stringOffsetMap
;
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.
576 class WordLiteralSection final
: public SyntheticSection
{
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);
583 WordLiteralSection();
584 void addInput(WordLiteralInputSection
*);
585 void finalizeContents();
586 void writeTo(uint8_t *buf
) const override
;
588 uint64_t getSize() const override
{
589 return literal16Map
.size() * 16 + literal8Map
.size() * 8 +
590 literal4Map
.size() * 4;
593 bool isNeeded() const override
{
594 return !literal16Map
.empty() || !literal4Map
.empty() ||
595 !literal8Map
.empty();
598 uint64_t getLiteral16Offset(uintptr_t buf
) const {
599 return literal16Map
.at(*reinterpret_cast<const UInt128
*>(buf
)) * 16;
602 uint64_t getLiteral8Offset(uintptr_t buf
) const {
603 return literal16Map
.size() * 16 +
604 literal8Map
.at(*reinterpret_cast<const uint64_t *>(buf
)) * 8;
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;
613 std::vector
<WordLiteralInputSection
*> inputs
;
615 template <class T
> struct Hasher
{
616 llvm::hash_code
operator()(T v
) const { return llvm::hash_value(v
); }
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
;
626 class ObjCImageInfoSection final
: public SyntheticSection
{
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
);
635 void finalizeContents();
636 void writeTo(uint8_t *buf
) const override
;
640 uint8_t swiftVersion
= 0;
641 bool hasCategoryClassProperties
= false;
643 static ImageInfo
parseImageInfo(const InputFile
*);
644 std::vector
<const InputFile
*> files
; // files with image info
647 // This section stores 32-bit __TEXT segment offsets of initializer functions.
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.
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
{
660 InitOffsetsSection();
661 bool isNeeded() const override
{ return !sections
.empty(); }
662 uint64_t getSize() const override
;
663 void writeTo(uint8_t *buf
) const override
;
666 void addInput(ConcatInputSection
*isec
) { sections
.push_back(isec
); }
667 const std::vector
<ConcatInputSection
*> &inputs() const { return sections
; }
670 std::vector
<ConcatInputSection
*> sections
;
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.
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.
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.
686 // The benefits of this format are:
687 // - smaller __LINKEDIT segment, as most of the fixup information is stored in
689 // - faster startup, since not all relocations need to be done upfront
690 // - slightly lower memory usage, as fewer pages are dirtied
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
704 // The entries are tightly packed 64-bit bitfields. One of the bits specifies
705 // which kind of fixup to interpret them as.
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
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
{
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
;
729 void addRebase(const InputSection
*isec
, uint64_t offset
) {
730 locations
.emplace_back(isec
, offset
);
732 void addBinding(const Symbol
*dysym
, const InputSection
*isec
,
733 uint64_t offset
, int64_t addend
= 0);
735 void setHasNonWeakDefinition() { hasNonWeakDef
= true; }
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;
741 const std::vector
<Location
> &getLocations() const { return locations
; }
743 bool hasWeakBinding() const { return hasWeakBind
; }
744 bool hasNonWeakDefinition() const { return hasNonWeakDef
; }
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
;
754 SegmentInfo(const OutputSegment
*oseg
) : oseg(oseg
) {}
756 const OutputSegment
*oseg
;
757 // (page index, fixup starts offset)
758 llvm::SmallVector
<std::pair
<uint16_t, uint16_t>> pageStarts
;
760 size_t getSize() const;
761 size_t writeTo(uint8_t *buf
) const;
763 llvm::SmallVector
<SegmentInfo
, 4> fixupSegments
;
765 size_t symtabSize
= 0;
768 bool needsAddend
= false;
769 bool needsLargeAddend
= false;
770 bool hasWeakBind
= false;
771 bool hasNonWeakDef
= false;
772 llvm::MachO::ChainedImportFormat importFormat
;
775 void writeChainedRebase(uint8_t *buf
, uint64_t targetVA
);
776 void writeChainedFixup(uint8_t *buf
, const Symbol
*sym
, int64_t addend
);
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;
804 extern std::vector
<SyntheticSection
*> syntheticSections
;
806 void createSyntheticSymbols();
808 } // namespace lld::macho