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/Optional.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/MC/StringTableBuilder.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include <unordered_map>
34 namespace lld::macho
{
40 class UnwindInfoSection
;
42 class SyntheticSection
: public OutputSection
{
44 SyntheticSection(const char *segname
, const char *name
);
45 virtual ~SyntheticSection() = default;
47 static bool classof(const OutputSection
*sec
) {
48 return sec
->kind() == SyntheticKind
;
52 // This fake InputSection makes it easier for us to write code that applies
53 // generically to both user inputs and synthetics.
57 // All sections in __LINKEDIT should inherit from this.
58 class LinkEditSection
: public SyntheticSection
{
60 LinkEditSection(const char *segname
, const char *name
)
61 : SyntheticSection(segname
, name
) {
62 align
= target
->wordSize
;
65 // Implementations of this method can assume that the regular (non-__LINKEDIT)
66 // sections already have their addresses assigned.
67 virtual void finalizeContents() {}
69 // Sections in __LINKEDIT are special: their offsets are recorded in the
70 // load commands like LC_DYLD_INFO_ONLY and LC_SYMTAB, instead of in section
72 bool isHidden() const final
{ return true; }
74 virtual uint64_t getRawSize() const = 0;
76 // codesign (or more specifically libstuff) checks that each section in
77 // __LINKEDIT ends where the next one starts -- no gaps are permitted. We
78 // therefore align every section's start and end points to WordSize.
80 // NOTE: This assumes that the extra bytes required for alignment can be
82 uint64_t getSize() const final
{ return llvm::alignTo(getRawSize(), align
); }
85 // The header of the Mach-O file, which must have a file offset of zero.
86 class MachHeaderSection final
: public SyntheticSection
{
89 bool isHidden() const override
{ return true; }
90 uint64_t getSize() const override
;
91 void writeTo(uint8_t *buf
) const override
;
93 void addLoadCommand(LoadCommand
*);
96 std::vector
<LoadCommand
*> loadCommands
;
97 uint32_t sizeOfCmds
= 0;
100 // A hidden section that exists solely for the purpose of creating the
101 // __PAGEZERO segment, which is used to catch null pointer dereferences.
102 class PageZeroSection final
: public SyntheticSection
{
105 bool isHidden() const override
{ return true; }
106 bool isNeeded() const override
{ return target
->pageZeroSize
!= 0; }
107 uint64_t getSize() const override
{ return target
->pageZeroSize
; }
108 uint64_t getFileSize() const override
{ return 0; }
109 void writeTo(uint8_t *buf
) const override
{}
112 // This is the base class for the GOT and TLVPointer sections, which are nearly
113 // functionally identical -- they will both be populated by dyld with addresses
114 // to non-lazily-loaded dylib symbols. The main difference is that the
115 // TLVPointerSection stores references to thread-local variables.
116 class NonLazyPointerSectionBase
: public SyntheticSection
{
118 NonLazyPointerSectionBase(const char *segname
, const char *name
);
119 const llvm::SetVector
<const Symbol
*> &getEntries() const { return entries
; }
120 bool isNeeded() const override
{ return !entries
.empty(); }
121 uint64_t getSize() const override
{
122 return entries
.size() * target
->wordSize
;
124 void writeTo(uint8_t *buf
) const override
;
125 void addEntry(Symbol
*sym
);
126 uint64_t getVA(uint32_t gotIndex
) const {
127 return addr
+ gotIndex
* target
->wordSize
;
131 llvm::SetVector
<const Symbol
*> entries
;
134 class GotSection final
: public NonLazyPointerSectionBase
{
139 class TlvPointerSection final
: public NonLazyPointerSectionBase
{
145 const InputSection
*isec
;
148 Location(const InputSection
*isec
, uint64_t offset
)
149 : isec(isec
), offset(offset
) {}
150 uint64_t getVA() const { return isec
->getVA(offset
); }
153 // Stores rebase opcodes, which tell dyld where absolute addresses have been
154 // encoded in the binary. If the binary is not loaded at its preferred address,
155 // dyld has to rebase these addresses by adding an offset to them.
156 class RebaseSection final
: public LinkEditSection
{
159 void finalizeContents() override
;
160 uint64_t getRawSize() const override
{ return contents
.size(); }
161 bool isNeeded() const override
{ return !locations
.empty(); }
162 void writeTo(uint8_t *buf
) const override
;
164 void addEntry(const InputSection
*isec
, uint64_t offset
) {
166 locations
.push_back({isec
, offset
});
170 std::vector
<Location
> locations
;
171 SmallVector
<char, 128> contents
;
174 struct BindingEntry
{
177 BindingEntry(int64_t addend
, Location target
)
178 : addend(addend
), target(std::move(target
)) {}
182 using BindingsMap
= llvm::DenseMap
<Sym
, std::vector
<BindingEntry
>>;
184 // Stores bind opcodes for telling dyld which symbols to load non-lazily.
185 class BindingSection final
: public LinkEditSection
{
188 void finalizeContents() override
;
189 uint64_t getRawSize() const override
{ return contents
.size(); }
190 bool isNeeded() const override
{ return !bindingsMap
.empty(); }
191 void writeTo(uint8_t *buf
) const override
;
193 void addEntry(const Symbol
*dysym
, const InputSection
*isec
, uint64_t offset
,
194 int64_t addend
= 0) {
195 bindingsMap
[dysym
].emplace_back(addend
, Location(isec
, offset
));
199 BindingsMap
<const Symbol
*> bindingsMap
;
200 SmallVector
<char, 128> contents
;
203 // Stores bind opcodes for telling dyld which weak symbols need coalescing.
204 // There are two types of entries in this section:
206 // 1) Non-weak definitions: This is a symbol definition that weak symbols in
207 // other dylibs should coalesce to.
209 // 2) Weak bindings: These tell dyld that a given symbol reference should
210 // coalesce to a non-weak definition if one is found. Note that unlike the
211 // entries in the BindingSection, the bindings here only refer to these
212 // symbols by name, but do not specify which dylib to load them from.
213 class WeakBindingSection final
: public LinkEditSection
{
215 WeakBindingSection();
216 void finalizeContents() override
;
217 uint64_t getRawSize() const override
{ return contents
.size(); }
218 bool isNeeded() const override
{
219 return !bindingsMap
.empty() || !definitions
.empty();
222 void writeTo(uint8_t *buf
) const override
;
224 void addEntry(const Symbol
*symbol
, const InputSection
*isec
, uint64_t offset
,
225 int64_t addend
= 0) {
226 bindingsMap
[symbol
].emplace_back(addend
, Location(isec
, offset
));
229 bool hasEntry() const { return !bindingsMap
.empty(); }
231 void addNonWeakDefinition(const Defined
*defined
) {
232 definitions
.emplace_back(defined
);
235 bool hasNonWeakDefinition() const { return !definitions
.empty(); }
238 BindingsMap
<const Symbol
*> bindingsMap
;
239 std::vector
<const Defined
*> definitions
;
240 SmallVector
<char, 128> contents
;
243 // The following sections implement lazy symbol binding -- very similar to the
244 // PLT mechanism in ELF.
246 // ELF's .plt section is broken up into two sections in Mach-O: StubsSection
247 // and StubHelperSection. Calls to functions in dylibs will end up calling into
248 // StubsSection, which contains indirect jumps to addresses stored in the
249 // LazyPointerSection (the counterpart to ELF's .plt.got).
251 // We will first describe how non-weak symbols are handled.
253 // At program start, the LazyPointerSection contains addresses that point into
254 // one of the entry points in the middle of the StubHelperSection. The code in
255 // StubHelperSection will push on the stack an offset into the
256 // LazyBindingSection. The push is followed by a jump to the beginning of the
257 // StubHelperSection (similar to PLT0), which then calls into dyld_stub_binder.
258 // dyld_stub_binder is a non-lazily-bound symbol, so this call looks it up in
261 // The stub binder will look up the bind opcodes in the LazyBindingSection at
262 // the given offset. The bind opcodes will tell the binder to update the
263 // address in the LazyPointerSection to point to the symbol, so that subsequent
264 // calls don't have to redo the symbol resolution. The binder will then jump to
265 // the resolved symbol.
267 // With weak symbols, the situation is slightly different. Since there is no
268 // "weak lazy" lookup, function calls to weak symbols are always non-lazily
269 // bound. We emit both regular non-lazy bindings as well as weak bindings, in
270 // order that the weak bindings may overwrite the non-lazy bindings if an
271 // appropriate symbol is found at runtime. However, the bound addresses will
272 // still be written (non-lazily) into the LazyPointerSection.
274 class StubsSection final
: public SyntheticSection
{
277 uint64_t getSize() const override
;
278 bool isNeeded() const override
{ return !entries
.empty(); }
279 void finalize() override
;
280 void writeTo(uint8_t *buf
) const override
;
281 const llvm::SetVector
<Symbol
*> &getEntries() const { return entries
; }
282 // Returns whether the symbol was added. Note that every stubs entry will
283 // have a corresponding entry in the LazyPointerSection.
284 bool addEntry(Symbol
*);
285 uint64_t getVA(uint32_t stubsIndex
) const {
286 assert(isFinal
|| target
->usesThunks());
287 // ConcatOutputSection::finalize() can seek the address of a
288 // stub before its address is assigned. Before __stubs is
289 // finalized, return a contrived out-of-range address.
290 return isFinal
? addr
+ stubsIndex
* target
->stubSize
291 : TargetInfo::outOfRangeVA
;
294 bool isFinal
= false; // is address assigned?
297 llvm::SetVector
<Symbol
*> entries
;
300 class StubHelperSection final
: public SyntheticSection
{
303 uint64_t getSize() const override
;
304 bool isNeeded() const override
;
305 void writeTo(uint8_t *buf
) const override
;
309 DylibSymbol
*stubBinder
= nullptr;
310 Defined
*dyldPrivate
= nullptr;
313 // Note that this section may also be targeted by non-lazy bindings. In
314 // particular, this happens when branch relocations target weak symbols.
315 class LazyPointerSection final
: public SyntheticSection
{
317 LazyPointerSection();
318 uint64_t getSize() const override
;
319 bool isNeeded() const override
;
320 void writeTo(uint8_t *buf
) const override
;
323 class LazyBindingSection final
: public LinkEditSection
{
325 LazyBindingSection();
326 void finalizeContents() override
;
327 uint64_t getRawSize() const override
{ return contents
.size(); }
328 bool isNeeded() const override
{ return !entries
.empty(); }
329 void writeTo(uint8_t *buf
) const override
;
330 // Note that every entry here will by referenced by a corresponding entry in
331 // the StubHelperSection.
332 void addEntry(Symbol
*dysym
);
333 const llvm::SetVector
<Symbol
*> &getEntries() const { return entries
; }
336 uint32_t encode(const Symbol
&);
338 llvm::SetVector
<Symbol
*> entries
;
339 SmallVector
<char, 128> contents
;
340 llvm::raw_svector_ostream os
{contents
};
343 // Stores a trie that describes the set of exported symbols.
344 class ExportSection final
: public LinkEditSection
{
347 void finalizeContents() override
;
348 uint64_t getRawSize() const override
{ return size
; }
349 bool isNeeded() const override
{ return size
; }
350 void writeTo(uint8_t *buf
) const override
;
352 bool hasWeakSymbol
= false;
355 TrieBuilder trieBuilder
;
359 // Stores 'data in code' entries that describe the locations of
360 // data regions inside code sections.
361 class DataInCodeSection final
: public LinkEditSection
{
364 void finalizeContents() override
;
365 uint64_t getRawSize() const override
{
366 return sizeof(llvm::MachO::data_in_code_entry
) * entries
.size();
368 void writeTo(uint8_t *buf
) const override
;
371 std::vector
<llvm::MachO::data_in_code_entry
> entries
;
374 // Stores ULEB128 delta encoded addresses of functions.
375 class FunctionStartsSection final
: public LinkEditSection
{
377 FunctionStartsSection();
378 void finalizeContents() override
;
379 uint64_t getRawSize() const override
{ return contents
.size(); }
380 void writeTo(uint8_t *buf
) const override
;
383 SmallVector
<char, 128> contents
;
386 // Stores the strings referenced by the symbol table.
387 class StringTableSection final
: public LinkEditSection
{
389 StringTableSection();
390 // Returns the start offset of the added string.
391 uint32_t addString(StringRef
);
392 uint64_t getRawSize() const override
{ return size
; }
393 void writeTo(uint8_t *buf
) const override
;
395 static constexpr size_t emptyStringIndex
= 1;
398 // ld64 emits string tables which start with a space and a zero byte. We
399 // match its behavior here since some tools depend on it.
400 // Consequently, the empty string will be at index 1, not zero.
401 std::vector
<StringRef
> strings
{" "};
412 uint32_t strx
= StringTableSection::emptyStringIndex
;
417 StabsEntry() = default;
418 explicit StabsEntry(uint8_t type
) : type(type
) {}
421 // Symbols of the same type must be laid out contiguously: we choose to emit
422 // all local symbols first, then external symbols, and finally undefined
423 // symbols. For each symbol type, the LC_DYSYMTAB load command will record the
424 // range (start index and total number) of those symbols in the symbol table.
425 class SymtabSection
: public LinkEditSection
{
427 void finalizeContents() override
;
428 uint32_t getNumSymbols() const;
429 uint32_t getNumLocalSymbols() const {
430 return stabs
.size() + localSymbols
.size();
432 uint32_t getNumExternalSymbols() const { return externalSymbols
.size(); }
433 uint32_t getNumUndefinedSymbols() const { return undefinedSymbols
.size(); }
436 void emitBeginSourceStab(StringRef
);
437 void emitEndSourceStab();
438 void emitObjectFileStab(ObjFile
*);
439 void emitEndFunStab(Defined
*);
443 SymtabSection(StringTableSection
&);
445 StringTableSection
&stringTableSection
;
446 // STABS symbols are always local symbols, but we represent them with special
447 // entries because they may use fields like n_sect and n_desc differently.
448 std::vector
<StabsEntry
> stabs
;
449 std::vector
<SymtabEntry
> localSymbols
;
450 std::vector
<SymtabEntry
> externalSymbols
;
451 std::vector
<SymtabEntry
> undefinedSymbols
;
454 template <class LP
> SymtabSection
*makeSymtabSection(StringTableSection
&);
456 // The indirect symbol table is a list of 32-bit integers that serve as indices
457 // into the (actual) symbol table. The indirect symbol table is a
458 // concatenation of several sub-arrays of indices, each sub-array belonging to
459 // a separate section. The starting offset of each sub-array is stored in the
460 // reserved1 header field of the respective section.
462 // These sub-arrays provide symbol information for sections that store
463 // contiguous sequences of symbol references. These references can be pointers
464 // (e.g. those in the GOT and TLVP sections) or assembly sequences (e.g.
466 class IndirectSymtabSection final
: public LinkEditSection
{
468 IndirectSymtabSection();
469 void finalizeContents() override
;
470 uint32_t getNumSymbols() const;
471 uint64_t getRawSize() const override
{
472 return getNumSymbols() * sizeof(uint32_t);
474 bool isNeeded() const override
;
475 void writeTo(uint8_t *buf
) const override
;
478 // The code signature comes at the very end of the linked output file.
479 class CodeSignatureSection final
: public LinkEditSection
{
481 // NOTE: These values are duplicated in llvm-objcopy's MachO/Object.h file
482 // and any changes here, should be repeated there.
483 static constexpr uint8_t blockSizeShift
= 12;
484 static constexpr size_t blockSize
= (1 << blockSizeShift
); // 4 KiB
485 static constexpr size_t hashSize
= 256 / 8;
486 static constexpr size_t blobHeadersSize
= llvm::alignTo
<8>(
487 sizeof(llvm::MachO::CS_SuperBlob
) + sizeof(llvm::MachO::CS_BlobIndex
));
488 static constexpr uint32_t fixedHeadersSize
=
489 blobHeadersSize
+ sizeof(llvm::MachO::CS_CodeDirectory
);
491 uint32_t fileNamePad
= 0;
492 uint32_t allHeadersSize
= 0;
495 CodeSignatureSection();
496 uint64_t getRawSize() const override
;
497 bool isNeeded() const override
{ return true; }
498 void writeTo(uint8_t *buf
) const override
;
499 uint32_t getBlockCount() const;
500 void writeHashes(uint8_t *buf
) const;
503 class BitcodeBundleSection final
: public SyntheticSection
{
505 BitcodeBundleSection();
506 uint64_t getSize() const override
{ return xarSize
; }
507 void finalize() override
;
508 void writeTo(uint8_t *buf
) const override
;
511 llvm::SmallString
<261> xarPath
;
515 class CStringSection
: public SyntheticSection
{
518 void addInput(CStringInputSection
*);
519 uint64_t getSize() const override
{ return size
; }
520 virtual void finalizeContents();
521 bool isNeeded() const override
{ return !inputs
.empty(); }
522 void writeTo(uint8_t *buf
) const override
;
524 std::vector
<CStringInputSection
*> inputs
;
530 class DeduplicatedCStringSection final
: public CStringSection
{
532 uint64_t getSize() const override
{ return size
; }
533 void finalizeContents() override
;
534 void writeTo(uint8_t *buf
) const override
;
537 struct StringOffset
{
538 uint8_t trailingZeros
;
539 uint64_t outSecOff
= UINT64_MAX
;
541 explicit StringOffset(uint8_t zeros
) : trailingZeros(zeros
) {}
543 llvm::DenseMap
<llvm::CachedHashStringRef
, StringOffset
> stringOffsetMap
;
548 * This section contains deduplicated literal values. The 16-byte values are
549 * laid out first, followed by the 8- and then the 4-byte ones.
551 class WordLiteralSection final
: public SyntheticSection
{
553 using UInt128
= std::pair
<uint64_t, uint64_t>;
554 // I don't think the standard guarantees the size of a pair, so let's make
555 // sure it's exact -- that way we can construct it via `mmap`.
556 static_assert(sizeof(UInt128
) == 16, "");
558 WordLiteralSection();
559 void addInput(WordLiteralInputSection
*);
560 void finalizeContents();
561 void writeTo(uint8_t *buf
) const override
;
563 uint64_t getSize() const override
{
564 return literal16Map
.size() * 16 + literal8Map
.size() * 8 +
565 literal4Map
.size() * 4;
568 bool isNeeded() const override
{
569 return !literal16Map
.empty() || !literal4Map
.empty() ||
570 !literal8Map
.empty();
573 uint64_t getLiteral16Offset(uintptr_t buf
) const {
574 return literal16Map
.at(*reinterpret_cast<const UInt128
*>(buf
)) * 16;
577 uint64_t getLiteral8Offset(uintptr_t buf
) const {
578 return literal16Map
.size() * 16 +
579 literal8Map
.at(*reinterpret_cast<const uint64_t *>(buf
)) * 8;
582 uint64_t getLiteral4Offset(uintptr_t buf
) const {
583 return literal16Map
.size() * 16 + literal8Map
.size() * 8 +
584 literal4Map
.at(*reinterpret_cast<const uint32_t *>(buf
)) * 4;
588 std::vector
<WordLiteralInputSection
*> inputs
;
590 template <class T
> struct Hasher
{
591 llvm::hash_code
operator()(T v
) const { return llvm::hash_value(v
); }
593 // We're using unordered_map instead of DenseMap here because we need to
594 // support all possible integer values -- there are no suitable tombstone
595 // values for DenseMap.
596 std::unordered_map
<UInt128
, uint64_t, Hasher
<UInt128
>> literal16Map
;
597 std::unordered_map
<uint64_t, uint64_t> literal8Map
;
598 std::unordered_map
<uint32_t, uint64_t> literal4Map
;
601 class ObjCImageInfoSection final
: public SyntheticSection
{
603 ObjCImageInfoSection();
604 bool isNeeded() const override
{ return !files
.empty(); }
605 uint64_t getSize() const override
{ return 8; }
606 void addFile(const InputFile
*file
) {
607 assert(!file
->objCImageInfo
.empty());
608 files
.push_back(file
);
610 void finalizeContents();
611 void writeTo(uint8_t *buf
) const override
;
615 uint8_t swiftVersion
= 0;
616 bool hasCategoryClassProperties
= false;
618 static ImageInfo
parseImageInfo(const InputFile
*);
619 std::vector
<const InputFile
*> files
; // files with image info
623 const uint8_t *bufferStart
= nullptr;
624 MachHeaderSection
*header
= nullptr;
625 CStringSection
*cStringSection
= nullptr;
626 WordLiteralSection
*wordLiteralSection
= nullptr;
627 RebaseSection
*rebase
= nullptr;
628 BindingSection
*binding
= nullptr;
629 WeakBindingSection
*weakBinding
= nullptr;
630 LazyBindingSection
*lazyBinding
= nullptr;
631 ExportSection
*exports
= nullptr;
632 GotSection
*got
= nullptr;
633 TlvPointerSection
*tlvPointers
= nullptr;
634 LazyPointerSection
*lazyPointers
= nullptr;
635 StubsSection
*stubs
= nullptr;
636 StubHelperSection
*stubHelper
= nullptr;
637 UnwindInfoSection
*unwindInfo
= nullptr;
638 ObjCImageInfoSection
*objCImageInfo
= nullptr;
639 ConcatInputSection
*imageLoaderCache
= nullptr;
643 extern std::vector
<SyntheticSection
*> syntheticSections
;
645 void createSyntheticSymbols();
647 } // namespace lld::macho