1 //===- SyntheticSection.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 // Synthetic sections represent chunks of linker-created data. If you
10 // need to create a chunk of data that to be included in some section
11 // in the result, you probably want to create that as a synthetic section.
13 // Synthetic sections are designed as input sections as opposed to
14 // output sections because we want to allow them to be manipulated
15 // using linker scripts just like other input sections from regular
18 //===----------------------------------------------------------------------===//
20 #ifndef LLD_ELF_SYNTHETIC_SECTIONS_H
21 #define LLD_ELF_SYNTHETIC_SECTIONS_H
25 #include "InputSection.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/FoldingSet.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/STLFunctionalExtras.h"
31 #include "llvm/BinaryFormat/ELF.h"
32 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
33 #include "llvm/MC/StringTableBuilder.h"
34 #include "llvm/Support/Allocator.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/Parallel.h"
38 #include "llvm/Support/Threading.h"
43 class SymbolTableBaseSection
;
46 EhSectionPiece
*cie
= nullptr;
47 SmallVector
<EhSectionPiece
*, 0> fdes
;
50 // Section for .eh_frame.
51 class EhFrameSection final
: public SyntheticSection
{
54 void writeTo(uint8_t *buf
) override
;
55 void finalizeContents() override
;
56 bool isNeeded() const override
{ return !sections
.empty(); }
57 size_t getSize() const override
{ return size
; }
59 static bool classof(const SectionBase
*d
) {
60 return SyntheticSection::classof(d
) && d
->name
== ".eh_frame";
63 SmallVector
<EhInputSection
*, 0> sections
;
71 SmallVector
<FdeData
, 0> getFdeData() const;
72 ArrayRef
<CieRecord
*> getCieRecords() const { return cieRecords
; }
74 void iterateFDEWithLSDA(llvm::function_ref
<void(InputSection
&)> fn
);
77 // This is used only when parsing EhInputSection. We keep it here to avoid
78 // allocating one for each EhInputSection.
79 llvm::DenseMap
<size_t, CieRecord
*> offsetToCie
;
83 template <class ELFT
, class RelTy
>
84 void addRecords(EhInputSection
*s
, llvm::ArrayRef
<RelTy
> rels
);
85 template <class ELFT
> void addSectionAux(EhInputSection
*s
);
86 template <class ELFT
, class RelTy
>
87 void iterateFDEWithLSDAAux(EhInputSection
&sec
, ArrayRef
<RelTy
> rels
,
88 llvm::DenseSet
<size_t> &ciesWithLSDA
,
89 llvm::function_ref
<void(InputSection
&)> fn
);
91 template <class ELFT
, class RelTy
>
92 CieRecord
*addCie(EhSectionPiece
&piece
, ArrayRef
<RelTy
> rels
);
94 template <class ELFT
, class RelTy
>
95 Defined
*isFdeLive(EhSectionPiece
&piece
, ArrayRef
<RelTy
> rels
);
97 uint64_t getFdePc(uint8_t *buf
, size_t off
, uint8_t enc
) const;
99 SmallVector
<CieRecord
*, 0> cieRecords
;
101 // CIE records are uniquified by their contents and personality functions.
102 llvm::DenseMap
<std::pair
<ArrayRef
<uint8_t>, Symbol
*>, CieRecord
*> cieMap
;
105 class GotSection final
: public SyntheticSection
{
108 size_t getSize() const override
{ return size
; }
109 void finalizeContents() override
;
110 bool isNeeded() const override
;
111 void writeTo(uint8_t *buf
) override
;
113 void addConstant(const Relocation
&r
);
114 void addEntry(const Symbol
&sym
);
115 bool addTlsDescEntry(const Symbol
&sym
);
116 bool addDynTlsEntry(const Symbol
&sym
);
118 uint32_t getTlsDescOffset(const Symbol
&sym
) const;
119 uint64_t getTlsDescAddr(const Symbol
&sym
) const;
120 uint64_t getGlobalDynAddr(const Symbol
&b
) const;
121 uint64_t getGlobalDynOffset(const Symbol
&b
) const;
123 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff
; }
124 uint32_t getTlsIndexOff() const { return tlsIndexOff
; }
126 // Flag to force GOT to be in output if we have relocations
127 // that relies on its address.
128 std::atomic
<bool> hasGotOffRel
= false;
131 size_t numEntries
= 0;
132 uint32_t tlsIndexOff
= -1;
136 // .note.GNU-stack section.
137 class GnuStackSection
: public SyntheticSection
{
140 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS
, 1, ".note.GNU-stack") {}
141 void writeTo(uint8_t *buf
) override
{}
142 size_t getSize() const override
{ return 0; }
145 class GnuPropertySection final
: public SyntheticSection
{
147 GnuPropertySection();
148 void writeTo(uint8_t *buf
) override
;
149 size_t getSize() const override
;
152 // .note.gnu.build-id section.
153 class BuildIdSection
: public SyntheticSection
{
154 // First 16 bytes are a header.
155 static const unsigned headerSize
= 16;
158 const size_t hashSize
;
160 void writeTo(uint8_t *buf
) override
;
161 size_t getSize() const override
{ return headerSize
+ hashSize
; }
162 void writeBuildId(llvm::ArrayRef
<uint8_t> buf
);
168 // BssSection is used to reserve space for copy relocations and common symbols.
169 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
170 // that are used for writable symbols, read-only symbols and common symbols,
172 class BssSection final
: public SyntheticSection
{
174 BssSection(StringRef name
, uint64_t size
, uint32_t addralign
);
175 void writeTo(uint8_t *) override
{}
176 bool isNeeded() const override
{ return size
!= 0; }
177 size_t getSize() const override
{ return size
; }
179 static bool classof(const SectionBase
*s
) { return s
->bss
; }
183 class MipsGotSection final
: public SyntheticSection
{
186 void writeTo(uint8_t *buf
) override
;
187 size_t getSize() const override
{ return size
; }
188 bool updateAllocSize() override
;
189 void finalizeContents() override
;
190 bool isNeeded() const override
;
192 // Join separate GOTs built for each input file to generate
193 // primary and optional multiple secondary GOTs.
196 void addEntry(InputFile
&file
, Symbol
&sym
, int64_t addend
, RelExpr expr
);
197 void addDynTlsEntry(InputFile
&file
, Symbol
&sym
);
198 void addTlsIndex(InputFile
&file
);
200 uint64_t getPageEntryOffset(const InputFile
*f
, const Symbol
&s
,
201 int64_t addend
) const;
202 uint64_t getSymEntryOffset(const InputFile
*f
, const Symbol
&s
,
203 int64_t addend
) const;
204 uint64_t getGlobalDynOffset(const InputFile
*f
, const Symbol
&s
) const;
205 uint64_t getTlsIndexOffset(const InputFile
*f
) const;
207 // Returns the symbol which corresponds to the first entry of the global part
208 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
210 // Returns nullptr if the global part is empty.
211 const Symbol
*getFirstGlobalEntry() const;
213 // Returns the number of entries in the local part of GOT including
214 // the number of reserved entries.
215 unsigned getLocalEntriesNum() const;
217 // Return _gp value for primary GOT (nullptr) or particular input file.
218 uint64_t getGp(const InputFile
*f
= nullptr) const;
221 // MIPS GOT consists of three parts: local, global and tls. Each part
222 // contains different types of entries. Here is a layout of GOT:
223 // - Header entries |
224 // - Page entries | Local part
225 // - Local entries (16-bit access) |
226 // - Local entries (32-bit access) |
227 // - Normal global entries || Global part
228 // - Reloc-only global entries ||
229 // - TLS entries ||| TLS part
232 // Two entries hold predefined value 0x0 and 0x80000000.
234 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
235 // relocation against local symbols. They are initialized by higher 16-bit
236 // of the corresponding symbol's value. So each 64kb of address space
237 // requires a single GOT entry.
238 // Local entries (16-bit access):
239 // These entries created by GOT relocations against global non-preemptible
240 // symbols so dynamic linker is not necessary to resolve the symbol's
241 // values. "16-bit access" means that corresponding relocations address
242 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own
244 // Local entries (32-bit access):
245 // These entries are the same as above but created by relocations which
246 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
247 // Normal global entries:
248 // These entries created by GOT relocations against preemptible global
249 // symbols. They need to be initialized by dynamic linker and they ordered
250 // exactly as the corresponding entries in the dynamic symbols table.
251 // Reloc-only global entries:
252 // These entries created for symbols that are referenced by dynamic
253 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative
254 // addressing, but MIPS ABI requires that these entries be present in GOT.
256 // Entries created by TLS relocations.
258 // If the sum of local, global and tls entries is less than 64K only single
259 // got is enough. Otherwise, multi-got is created. Series of primary and
260 // multiple secondary GOTs have the following layout:
265 // Relocation only entries
274 // All GOT entries required by relocations from a single input file entirely
275 // belong to either primary or one of secondary GOTs. To reference GOT entries
276 // each GOT has its own _gp value points to the "middle" of the GOT.
277 // In the code this value loaded to the register which is used for GOT access.
279 // MIPS 32 function's prologue:
281 // 0: R_MIPS_HI16 _gp_disp
283 // 4: R_MIPS_LO16 _gp_disp
287 // 14: R_MIPS_GPREL16 main
289 // Dynamic linker does not know anything about secondary GOTs and cannot
290 // use a regular MIPS mechanism for GOT entries initialization. So we have
291 // to use an approach accepted by other architectures and create dynamic
292 // relocations R_MIPS_REL32 to initialize global entries (and local in case
293 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
294 // requires GOT entries and correspondingly ordered dynamic symbol table
295 // entries to deal with dynamic relocations. To handle this problem
296 // relocation-only section in the primary GOT contains entries for all
297 // symbols referenced in global parts of secondary GOTs. Although the sum
298 // of local and normal global entries of the primary got should be less
299 // than 64K, the size of the primary got (including relocation-only entries
300 // can be greater than 64K, because parts of the primary got that overflow
301 // the 64K limit are used only by the dynamic linker at dynamic link-time
302 // and not by 16-bit gp-relative addressing at run-time.
304 // For complete multi-GOT description see the following link
305 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
307 // Number of "Header" entries.
308 static const unsigned headerEntriesNum
= 2;
312 // Symbol and addend.
313 using GotEntry
= std::pair
<Symbol
*, int64_t>;
316 InputFile
*file
= nullptr;
317 size_t startIndex
= 0;
322 PageBlock() : firstIndex(0), count(0) {}
325 // Map output sections referenced by MIPS GOT relocations
326 // to the description (index/count) "page" entries allocated
328 llvm::SmallMapVector
<const OutputSection
*, PageBlock
, 16> pagesMap
;
329 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
330 llvm::MapVector
<GotEntry
, size_t> local16
;
331 llvm::MapVector
<GotEntry
, size_t> local32
;
332 llvm::MapVector
<Symbol
*, size_t> global
;
333 llvm::MapVector
<Symbol
*, size_t> relocs
;
334 llvm::MapVector
<Symbol
*, size_t> tls
;
335 // Set of symbols referenced by dynamic TLS relocations.
336 llvm::MapVector
<Symbol
*, size_t> dynTlsSymbols
;
338 // Total number of all entries.
339 size_t getEntriesNum() const;
340 // Number of "page" entries.
341 size_t getPageEntriesNum() const;
342 // Number of entries require 16-bit index to access.
343 size_t getIndexedEntriesNum() const;
346 // Container of GOT created for each input file.
347 // After building a final series of GOTs this container
348 // holds primary and secondary GOT's.
349 std::vector
<FileGot
> gots
;
351 // Return (and create if necessary) `FileGot`.
352 FileGot
&getGot(InputFile
&f
);
354 // Try to merge two GOTs. In case of success the `Dst` contains
355 // result of merging and the function returns true. In case of
356 // overflow the `Dst` is unchanged and the function returns false.
357 bool tryMergeGots(FileGot
& dst
, FileGot
& src
, bool isPrimary
);
360 class GotPltSection final
: public SyntheticSection
{
363 void addEntry(Symbol
&sym
);
364 size_t getSize() const override
;
365 void writeTo(uint8_t *buf
) override
;
366 bool isNeeded() const override
;
368 // Flag to force GotPlt to be in output if we have relocations
369 // that relies on its address.
370 std::atomic
<bool> hasGotPltOffRel
= false;
373 SmallVector
<const Symbol
*, 0> entries
;
376 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
377 // Symbols that will be relocated by Target->IRelativeRel.
378 // On most Targets the IgotPltSection will immediately follow the GotPltSection
379 // on ARM the IgotPltSection will immediately follow the GotSection.
380 class IgotPltSection final
: public SyntheticSection
{
383 void addEntry(Symbol
&sym
);
384 size_t getSize() const override
;
385 void writeTo(uint8_t *buf
) override
;
386 bool isNeeded() const override
{ return !entries
.empty(); }
389 SmallVector
<const Symbol
*, 0> entries
;
392 class StringTableSection final
: public SyntheticSection
{
394 StringTableSection(StringRef name
, bool dynamic
);
395 unsigned addString(StringRef s
, bool hashIt
= true);
396 void writeTo(uint8_t *buf
) override
;
397 size_t getSize() const override
{ return size
; }
398 bool isDynamic() const { return dynamic
; }
405 llvm::DenseMap
<llvm::CachedHashStringRef
, unsigned> stringMap
;
406 SmallVector
<StringRef
, 0> strings
;
412 /// The resulting dynamic relocation does not reference a symbol (#sym must
413 /// be nullptr) and uses #addend as the result of computeAddend().
415 /// The resulting dynamic relocation will not reference a symbol: #sym is
416 /// only used to compute the addend with InputSection::getRelocTargetVA().
417 /// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64).
418 AddendOnlyWithTargetVA
,
419 /// The resulting dynamic relocation references symbol #sym from the dynamic
420 /// symbol table and uses #addend as the value of computeAddend().
422 /// The resulting dynamic relocation references symbol #sym from the dynamic
423 /// symbol table and uses InputSection::getRelocTargetVA() + #addend for the
424 /// final addend. It can be used for relocations that write the symbol VA as
425 // the addend (e.g. R_MIPS_TLS_TPREL64) but still reference the symbol.
426 AgainstSymbolWithTargetVA
,
427 /// This is used by the MIPS multi-GOT implementation. It relocates
428 /// addresses of 64kb pages that lie inside the output section.
431 /// This constructor records a relocation against a symbol.
432 DynamicReloc(RelType type
, const InputSectionBase
*inputSec
,
433 uint64_t offsetInSec
, Kind kind
, Symbol
&sym
, int64_t addend
,
435 : sym(&sym
), inputSec(inputSec
), offsetInSec(offsetInSec
), type(type
),
436 addend(addend
), kind(kind
), expr(expr
) {}
437 /// This constructor records a relative relocation with no symbol.
438 DynamicReloc(RelType type
, const InputSectionBase
*inputSec
,
439 uint64_t offsetInSec
, int64_t addend
= 0)
440 : sym(nullptr), inputSec(inputSec
), offsetInSec(offsetInSec
), type(type
),
441 addend(addend
), kind(AddendOnly
), expr(R_ADDEND
) {}
442 /// This constructor records dynamic relocation settings used by the MIPS
443 /// multi-GOT implementation.
444 DynamicReloc(RelType type
, const InputSectionBase
*inputSec
,
445 uint64_t offsetInSec
, const OutputSection
*outputSec
,
447 : sym(nullptr), outputSec(outputSec
), inputSec(inputSec
),
448 offsetInSec(offsetInSec
), type(type
), addend(addend
),
449 kind(MipsMultiGotPage
), expr(R_ADDEND
) {}
451 uint64_t getOffset() const;
452 uint32_t getSymIndex(SymbolTableBaseSection
*symTab
) const;
453 bool needsDynSymIndex() const {
454 return kind
== AgainstSymbol
|| kind
== AgainstSymbolWithTargetVA
;
457 /// Computes the addend of the dynamic relocation. Note that this is not the
458 /// same as the #addend member variable as it may also include the symbol
459 /// address/the address of the corresponding GOT entry/etc.
460 int64_t computeAddend() const;
462 void computeRaw(SymbolTableBaseSection
*symtab
);
465 const OutputSection
*outputSec
= nullptr;
466 const InputSectionBase
*inputSec
;
467 uint64_t offsetInSec
;
471 // Initially input addend, then the output addend after
472 // RelocationSection<ELFT>::writeTo.
477 // The kind of expression used to calculate the added (required e.g. for
478 // relative GOT relocations).
482 template <class ELFT
> class DynamicSection final
: public SyntheticSection
{
483 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
487 void finalizeContents() override
;
488 void writeTo(uint8_t *buf
) override
;
489 size_t getSize() const override
{ return size
; }
492 std::vector
<std::pair
<int32_t, uint64_t>> computeContents();
496 class RelocationBaseSection
: public SyntheticSection
{
498 RelocationBaseSection(StringRef name
, uint32_t type
, int32_t dynamicTag
,
499 int32_t sizeDynamicTag
, bool combreloc
,
500 unsigned concurrency
);
501 /// Add a dynamic relocation without writing an addend to the output section.
502 /// This overload can be used if the addends are written directly instead of
503 /// using relocations on the input section (e.g. MipsGotSection::writeTo()).
504 template <bool shard
= false> void addReloc(const DynamicReloc
&reloc
) {
505 relocs
.push_back(reloc
);
507 /// Add a dynamic relocation against \p sym with an optional addend.
508 void addSymbolReloc(RelType dynType
, InputSectionBase
&isec
,
509 uint64_t offsetInSec
, Symbol
&sym
, int64_t addend
= 0,
510 std::optional
<RelType
> addendRelType
= {});
511 /// Add a relative dynamic relocation that uses the target address of \p sym
512 /// (i.e. InputSection::getRelocTargetVA()) + \p addend as the addend.
513 /// This function should only be called for non-preemptible symbols or
514 /// RelExpr values that refer to an address inside the output file (e.g. the
515 /// address of the GOT entry for a potentially preemptible symbol).
516 template <bool shard
= false>
517 void addRelativeReloc(RelType dynType
, InputSectionBase
&isec
,
518 uint64_t offsetInSec
, Symbol
&sym
, int64_t addend
,
519 RelType addendRelType
, RelExpr expr
) {
520 assert(expr
!= R_ADDEND
&& "expected non-addend relocation expression");
521 addReloc
<shard
>(DynamicReloc::AddendOnlyWithTargetVA
, dynType
, isec
,
522 offsetInSec
, sym
, addend
, expr
, addendRelType
);
524 /// Add a dynamic relocation using the target address of \p sym as the addend
525 /// if \p sym is non-preemptible. Otherwise add a relocation against \p sym.
526 void addAddendOnlyRelocIfNonPreemptible(RelType dynType
, GotSection
&sec
,
527 uint64_t offsetInSec
, Symbol
&sym
,
528 RelType addendRelType
);
529 template <bool shard
= false>
530 void addReloc(DynamicReloc::Kind kind
, RelType dynType
, InputSectionBase
&sec
,
531 uint64_t offsetInSec
, Symbol
&sym
, int64_t addend
, RelExpr expr
,
532 RelType addendRelType
) {
533 // Write the addends to the relocated address if required. We skip
534 // it if the written value would be zero.
535 if (config
->writeAddends
&& (expr
!= R_ADDEND
|| addend
!= 0))
536 sec
.addReloc({expr
, addendRelType
, offsetInSec
, addend
, &sym
});
537 addReloc
<shard
>({dynType
, &sec
, offsetInSec
, kind
, sym
, addend
, expr
});
539 bool isNeeded() const override
{
540 return !relocs
.empty() ||
541 llvm::any_of(relocsVec
, [](auto &v
) { return !v
.empty(); });
543 size_t getSize() const override
{ return relocs
.size() * this->entsize
; }
544 size_t getRelativeRelocCount() const { return numRelativeRelocs
; }
546 void partitionRels();
547 void finalizeContents() override
;
548 static bool classof(const SectionBase
*d
) {
549 return SyntheticSection::classof(d
) &&
550 (d
->type
== llvm::ELF::SHT_RELA
|| d
->type
== llvm::ELF::SHT_REL
||
551 d
->type
== llvm::ELF::SHT_RELR
||
552 (d
->type
== llvm::ELF::SHT_AARCH64_AUTH_RELR
&&
553 config
->emachine
== llvm::ELF::EM_AARCH64
));
555 int32_t dynamicTag
, sizeDynamicTag
;
556 SmallVector
<DynamicReloc
, 0> relocs
;
560 // Used when parallel relocation scanning adds relocations. The elements
561 // will be moved into relocs by mergeRel().
562 SmallVector
<SmallVector
<DynamicReloc
, 0>, 0> relocsVec
;
563 size_t numRelativeRelocs
= 0; // used by -z combreloc
568 inline void RelocationBaseSection::addReloc
<true>(const DynamicReloc
&reloc
) {
569 relocsVec
[llvm::parallel::getThreadIndex()].push_back(reloc
);
572 template <class ELFT
>
573 class RelocationSection final
: public RelocationBaseSection
{
574 using Elf_Rel
= typename
ELFT::Rel
;
575 using Elf_Rela
= typename
ELFT::Rela
;
578 RelocationSection(StringRef name
, bool combreloc
, unsigned concurrency
);
579 void writeTo(uint8_t *buf
) override
;
582 template <class ELFT
>
583 class AndroidPackedRelocationSection final
: public RelocationBaseSection
{
584 using Elf_Rel
= typename
ELFT::Rel
;
585 using Elf_Rela
= typename
ELFT::Rela
;
588 AndroidPackedRelocationSection(StringRef name
, unsigned concurrency
);
590 bool updateAllocSize() override
;
591 size_t getSize() const override
{ return relocData
.size(); }
592 void writeTo(uint8_t *buf
) override
{
593 memcpy(buf
, relocData
.data(), relocData
.size());
597 SmallVector
<char, 0> relocData
;
600 struct RelativeReloc
{
601 uint64_t getOffset() const {
602 return inputSec
->getVA(inputSec
->relocs()[relocIdx
].offset
);
605 const InputSectionBase
*inputSec
;
609 class RelrBaseSection
: public SyntheticSection
{
611 RelrBaseSection(unsigned concurrency
, bool isAArch64Auth
= false);
613 bool isNeeded() const override
{
614 return !relocs
.empty() ||
615 llvm::any_of(relocsVec
, [](auto &v
) { return !v
.empty(); });
617 SmallVector
<RelativeReloc
, 0> relocs
;
618 SmallVector
<SmallVector
<RelativeReloc
, 0>, 0> relocsVec
;
621 // RelrSection is used to encode offsets for relative relocations.
622 // Proposal for adding SHT_RELR sections to generic-abi is here:
623 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
624 // For more details, see the comment in RelrSection::updateAllocSize().
625 template <class ELFT
> class RelrSection final
: public RelrBaseSection
{
626 using Elf_Relr
= typename
ELFT::Relr
;
629 RelrSection(unsigned concurrency
, bool isAArch64Auth
= false);
631 bool updateAllocSize() override
;
632 size_t getSize() const override
{ return relrRelocs
.size() * this->entsize
; }
633 void writeTo(uint8_t *buf
) override
{
634 memcpy(buf
, relrRelocs
.data(), getSize());
638 SmallVector
<Elf_Relr
, 0> relrRelocs
;
641 struct SymbolTableEntry
{
646 class SymbolTableBaseSection
: public SyntheticSection
{
648 SymbolTableBaseSection(StringTableSection
&strTabSec
);
649 void finalizeContents() override
;
650 size_t getSize() const override
{ return getNumSymbols() * entsize
; }
651 void addSymbol(Symbol
*sym
);
652 unsigned getNumSymbols() const { return symbols
.size() + 1; }
653 size_t getSymbolIndex(const Symbol
&sym
);
654 ArrayRef
<SymbolTableEntry
> getSymbols() const { return symbols
; }
657 void sortSymTabSymbols();
659 // A vector of symbols and their string table offsets.
660 SmallVector
<SymbolTableEntry
, 0> symbols
;
662 StringTableSection
&strTabSec
;
664 llvm::once_flag onceFlag
;
665 llvm::DenseMap
<Symbol
*, size_t> symbolIndexMap
;
666 llvm::DenseMap
<OutputSection
*, size_t> sectionIndexMap
;
669 template <class ELFT
>
670 class SymbolTableSection final
: public SymbolTableBaseSection
{
671 using Elf_Sym
= typename
ELFT::Sym
;
674 SymbolTableSection(StringTableSection
&strTabSec
);
675 void writeTo(uint8_t *buf
) override
;
678 class SymtabShndxSection final
: public SyntheticSection
{
680 SymtabShndxSection();
682 void writeTo(uint8_t *buf
) override
;
683 size_t getSize() const override
;
684 bool isNeeded() const override
;
685 void finalizeContents() override
;
688 // Outputs GNU Hash section. For detailed explanation see:
689 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
690 class GnuHashTableSection final
: public SyntheticSection
{
692 GnuHashTableSection();
693 void finalizeContents() override
;
694 void writeTo(uint8_t *buf
) override
;
695 size_t getSize() const override
{ return size
; }
697 // Adds symbols to the hash table.
698 // Sorts the input to satisfy GNU hash section requirements.
699 void addSymbols(llvm::SmallVectorImpl
<SymbolTableEntry
> &symbols
);
702 // See the comment in writeBloomFilter.
703 enum { Shift2
= 26 };
712 SmallVector
<Entry
, 0> symbols
;
718 class HashTableSection final
: public SyntheticSection
{
721 void finalizeContents() override
;
722 void writeTo(uint8_t *buf
) override
;
723 size_t getSize() const override
{ return size
; }
729 // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT
730 // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily
733 // On PowerPC, this section contains lazy symbol resolvers. A branch instruction
734 // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a
735 // lazy symbol resolver.
737 // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs.
738 // A call instruction jumps to a .plt.sec entry, which will then jump to the
739 // target (BIND_NOW) or a .plt entry.
740 class PltSection
: public SyntheticSection
{
743 void writeTo(uint8_t *buf
) override
;
744 size_t getSize() const override
;
745 bool isNeeded() const override
;
747 void addEntry(Symbol
&sym
);
748 size_t getNumEntries() const { return entries
.size(); }
752 SmallVector
<const Symbol
*, 0> entries
;
755 // Used for non-preemptible ifuncs. It does not have a header. Each entry is
756 // associated with an IRELATIVE relocation, which will be resolved eagerly at
757 // runtime. PltSection can only contain entries associated with JUMP_SLOT
758 // relocations, so IPLT entries are in a separate section.
759 class IpltSection final
: public SyntheticSection
{
760 SmallVector
<const Symbol
*, 0> entries
;
764 void writeTo(uint8_t *buf
) override
;
765 size_t getSize() const override
;
766 bool isNeeded() const override
{ return !entries
.empty(); }
768 void addEntry(Symbol
&sym
);
771 class PPC32GlinkSection
: public PltSection
{
774 void writeTo(uint8_t *buf
) override
;
775 size_t getSize() const override
;
777 SmallVector
<const Symbol
*, 0> canonical_plts
;
778 static constexpr size_t footerSize
= 64;
782 class IBTPltSection
: public SyntheticSection
{
785 void writeTo(uint8_t *Buf
) override
;
786 bool isNeeded() const override
;
787 size_t getSize() const override
;
790 // Used to align the end of the PT_GNU_RELRO segment and the associated PT_LOAD
791 // segment to a common-page-size boundary. This padding section ensures that all
792 // pages in the PT_LOAD segment is covered by at least one section.
793 class RelroPaddingSection final
: public SyntheticSection
{
795 RelroPaddingSection();
796 size_t getSize() const override
{ return 0; }
797 void writeTo(uint8_t *buf
) override
{}
800 // Used by the merged DWARF32 .debug_names (a per-module index). If we
801 // move to DWARF64, most of this data will need to be re-sized.
802 class DebugNamesBaseSection
: public SyntheticSection
{
804 struct Abbrev
: llvm::FoldingSetNode
{
807 SmallVector
<llvm::DWARFDebugNames::AttributeEncoding
, 2> attributes
;
809 void Profile(llvm::FoldingSetNodeID
&id
) const;
821 uint64_t parentOffset
= 0;
822 IndexEntry
*parentEntry
;
824 SmallVector
<AttrValue
, 3> attrValues
;
830 uint32_t stringOffset
;
831 uint32_t entryOffset
;
832 // Used to relocate `stringOffset` in the merged section.
834 SmallVector
<IndexEntry
*, 0> indexEntries
;
836 llvm::iterator_range
<
837 llvm::pointee_iterator
<typename SmallVector
<IndexEntry
*, 0>::iterator
>>
839 return llvm::make_pointee_range(indexEntries
);
843 // The contents of one input .debug_names section. An InputChunk
844 // typically contains one NameData, but might contain more, especially
847 llvm::DWARFDebugNames::Header hdr
;
848 llvm::DenseMap
<uint32_t, uint32_t> abbrevCodeMap
;
849 SmallVector
<NameEntry
, 0> nameEntries
;
852 // InputChunk and OutputChunk hold per-file contributions to the merged index.
853 // InputChunk instances will be discarded after `init` completes.
856 LLDDWARFSection section
;
857 SmallVector
<NameData
, 0> nameData
;
858 std::optional
<llvm::DWARFDebugNames
> llvmDebugNames
;
862 // Pointer to the .debug_info section that contains compile units, used to
863 // compute the relocated CU offsets.
864 InputSection
*infoSec
;
865 // This initially holds section offsets. After relocation, the section
866 // offsets are changed to CU offsets relative the the output section.
867 SmallVector
<uint32_t, 0> compUnits
;
870 DebugNamesBaseSection();
871 size_t getSize() const override
{ return size
; }
872 bool isNeeded() const override
{ return numChunks
> 0; }
875 void init(llvm::function_ref
<void(InputFile
*, InputChunk
&, OutputChunk
&)>);
877 parseDebugNames(InputChunk
&inputChunk
, OutputChunk
&chunk
,
878 llvm::DWARFDataExtractor
&namesExtractor
,
879 llvm::DataExtractor
&strExtractor
,
880 llvm::function_ref
<SmallVector
<uint32_t, 0>(
881 uint32_t numCUs
, const llvm::DWARFDebugNames::Header
&hdr
,
882 const llvm::DWARFDebugNames::DWARFDebugNamesOffsets
&)>
884 void computeHdrAndAbbrevTable(MutableArrayRef
<InputChunk
> inputChunks
);
885 std::pair
<uint32_t, uint32_t>
886 computeEntryPool(MutableArrayRef
<InputChunk
> inputChunks
);
888 // Input .debug_names sections for relocating string offsets in the name table
889 // in `finalizeContents`.
890 SmallVector
<InputSection
*, 0> inputSections
;
892 llvm::DWARFDebugNames::Header hdr
;
894 std::unique_ptr
<OutputChunk
[]> chunks
;
895 llvm::SpecificBumpPtrAllocator
<Abbrev
> abbrevAlloc
;
896 SmallVector
<Abbrev
*, 0> abbrevTable
;
897 SmallVector
<char, 0> abbrevTableBuf
;
899 ArrayRef
<OutputChunk
> getChunks() const {
900 return ArrayRef(chunks
.get(), numChunks
);
903 // Sharded name entries that will be used to compute bucket_count and the
905 static constexpr size_t numShards
= 32;
906 SmallVector
<NameEntry
, 0> nameVecs
[numShards
];
909 // Complement DebugNamesBaseSection for ELFT-aware code: reading offsets,
910 // relocating string offsets, and writeTo.
911 template <class ELFT
>
912 class DebugNamesSection final
: public DebugNamesBaseSection
{
915 void finalizeContents() override
;
916 void writeTo(uint8_t *buf
) override
;
918 template <class RelTy
>
919 void getNameRelocs(const InputFile
&file
,
920 llvm::DenseMap
<uint32_t, uint32_t> &relocs
,
924 static void readOffsets(InputChunk
&inputChunk
, OutputChunk
&chunk
,
925 llvm::DWARFDataExtractor
&namesExtractor
,
926 llvm::DataExtractor
&strExtractor
);
929 class GdbIndexSection final
: public SyntheticSection
{
931 struct AddressEntry
{
932 InputSection
*section
;
934 uint64_t highAddress
;
943 struct NameAttrEntry
{
944 llvm::CachedHashStringRef name
;
945 uint32_t cuIndexAndAttrs
;
950 SmallVector
<AddressEntry
, 0> addressAreas
;
951 SmallVector
<CuEntry
, 0> compilationUnits
;
955 llvm::CachedHashStringRef name
;
956 SmallVector
<uint32_t, 0> cuVector
;
958 uint32_t cuVectorOff
;
962 template <typename ELFT
> static std::unique_ptr
<GdbIndexSection
> create();
963 void writeTo(uint8_t *buf
) override
;
964 size_t getSize() const override
{ return size
; }
965 bool isNeeded() const override
;
968 struct GdbIndexHeader
{
969 llvm::support::ulittle32_t version
;
970 llvm::support::ulittle32_t cuListOff
;
971 llvm::support::ulittle32_t cuTypesOff
;
972 llvm::support::ulittle32_t addressAreaOff
;
973 llvm::support::ulittle32_t symtabOff
;
974 llvm::support::ulittle32_t constantPoolOff
;
977 size_t computeSymtabSize() const;
979 // Each chunk contains information gathered from debug sections of a
980 // single object file.
981 SmallVector
<GdbChunk
, 0> chunks
;
983 // A symbol table for this .gdb_index section.
984 SmallVector
<GdbSymbol
, 0> symbols
;
989 // --eh-frame-hdr option tells linker to construct a header for all the
990 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
991 // and also to a PT_GNU_EH_FRAME segment.
992 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
993 // calling dl_iterate_phdr.
994 // This section contains a lookup table for quick binary search of FDEs.
995 // Detailed info about internals can be found in Ian Lance Taylor's blog:
996 // http://www.airs.com/blog/archives/460 (".eh_frame")
997 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
998 class EhFrameHeader final
: public SyntheticSection
{
1002 void writeTo(uint8_t *buf
) override
;
1003 size_t getSize() const override
;
1004 bool isNeeded() const override
;
1007 // For more information about .gnu.version and .gnu.version_r see:
1008 // https://www.akkadia.org/drepper/symbol-versioning
1010 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
1011 // contain symbol version definitions. The number of entries in this section
1012 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
1013 // The section shall contain an array of Elf_Verdef structures, optionally
1014 // followed by an array of Elf_Verdaux structures.
1015 class VersionDefinitionSection final
: public SyntheticSection
{
1017 VersionDefinitionSection();
1018 void finalizeContents() override
;
1019 size_t getSize() const override
;
1020 void writeTo(uint8_t *buf
) override
;
1023 enum { EntrySize
= 28 };
1024 void writeOne(uint8_t *buf
, uint32_t index
, StringRef name
, size_t nameOff
);
1025 StringRef
getFileDefName();
1027 unsigned fileDefNameOff
;
1028 SmallVector
<unsigned, 0> verDefNameOffs
;
1031 // The .gnu.version section specifies the required version of each symbol in the
1032 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
1033 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
1034 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
1035 // The values 0 and 1 are reserved. All other values are used for versions in
1036 // the own object or in any of the dependencies.
1037 class VersionTableSection final
: public SyntheticSection
{
1039 VersionTableSection();
1040 void finalizeContents() override
;
1041 size_t getSize() const override
;
1042 void writeTo(uint8_t *buf
) override
;
1043 bool isNeeded() const override
;
1046 // The .gnu.version_r section defines the version identifiers used by
1047 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
1048 // Elf_Verneed specifies the version requirements for a single DSO, and contains
1049 // a reference to a linked list of Elf_Vernaux data structures which define the
1050 // mapping from version identifiers to version names.
1051 template <class ELFT
>
1052 class VersionNeedSection final
: public SyntheticSection
{
1053 using Elf_Verneed
= typename
ELFT::Verneed
;
1054 using Elf_Vernaux
= typename
ELFT::Vernaux
;
1058 uint32_t verneedIndex
;
1059 uint64_t nameStrTab
;
1063 uint64_t nameStrTab
;
1064 std::vector
<Vernaux
> vernauxs
;
1067 SmallVector
<Verneed
, 0> verneeds
;
1070 VersionNeedSection();
1071 void finalizeContents() override
;
1072 void writeTo(uint8_t *buf
) override
;
1073 size_t getSize() const override
;
1074 bool isNeeded() const override
;
1077 // MergeSyntheticSection is a class that allows us to put mergeable sections
1078 // with different attributes in a single output sections. To do that
1079 // we put them into MergeSyntheticSection synthetic input sections which are
1080 // attached to regular output sections.
1081 class MergeSyntheticSection
: public SyntheticSection
{
1083 void addSection(MergeInputSection
*ms
);
1084 SmallVector
<MergeInputSection
*, 0> sections
;
1087 MergeSyntheticSection(StringRef name
, uint32_t type
, uint64_t flags
,
1089 : SyntheticSection(flags
, type
, addralign
, name
) {}
1092 class MergeTailSection final
: public MergeSyntheticSection
{
1094 MergeTailSection(StringRef name
, uint32_t type
, uint64_t flags
,
1095 uint32_t addralign
);
1097 size_t getSize() const override
;
1098 void writeTo(uint8_t *buf
) override
;
1099 void finalizeContents() override
;
1102 llvm::StringTableBuilder builder
;
1105 class MergeNoTailSection final
: public MergeSyntheticSection
{
1107 MergeNoTailSection(StringRef name
, uint32_t type
, uint64_t flags
,
1109 : MergeSyntheticSection(name
, type
, flags
, addralign
) {}
1111 size_t getSize() const override
{ return size
; }
1112 void writeTo(uint8_t *buf
) override
;
1113 void finalizeContents() override
;
1116 // We use the most significant bits of a hash as a shard ID.
1117 // The reason why we don't want to use the least significant bits is
1118 // because DenseMap also uses lower bits to determine a bucket ID.
1119 // If we use lower bits, it significantly increases the probability of
1121 size_t getShardId(uint32_t hash
) {
1122 assert((hash
>> 31) == 0);
1123 return hash
>> (31 - llvm::countr_zero(numShards
));
1129 // String table contents
1130 constexpr static size_t numShards
= 32;
1131 SmallVector
<llvm::StringTableBuilder
, 0> shards
;
1132 size_t shardOffsets
[numShards
];
1135 // .MIPS.abiflags section.
1136 template <class ELFT
>
1137 class MipsAbiFlagsSection final
: public SyntheticSection
{
1138 using Elf_Mips_ABIFlags
= llvm::object::Elf_Mips_ABIFlags
<ELFT
>;
1141 static std::unique_ptr
<MipsAbiFlagsSection
> create();
1143 MipsAbiFlagsSection(Elf_Mips_ABIFlags flags
);
1144 size_t getSize() const override
{ return sizeof(Elf_Mips_ABIFlags
); }
1145 void writeTo(uint8_t *buf
) override
;
1148 Elf_Mips_ABIFlags flags
;
1151 // .MIPS.options section.
1152 template <class ELFT
> class MipsOptionsSection final
: public SyntheticSection
{
1153 using Elf_Mips_Options
= llvm::object::Elf_Mips_Options
<ELFT
>;
1154 using Elf_Mips_RegInfo
= llvm::object::Elf_Mips_RegInfo
<ELFT
>;
1157 static std::unique_ptr
<MipsOptionsSection
<ELFT
>> create();
1159 MipsOptionsSection(Elf_Mips_RegInfo reginfo
);
1160 void writeTo(uint8_t *buf
) override
;
1162 size_t getSize() const override
{
1163 return sizeof(Elf_Mips_Options
) + sizeof(Elf_Mips_RegInfo
);
1167 Elf_Mips_RegInfo reginfo
;
1170 // MIPS .reginfo section.
1171 template <class ELFT
> class MipsReginfoSection final
: public SyntheticSection
{
1172 using Elf_Mips_RegInfo
= llvm::object::Elf_Mips_RegInfo
<ELFT
>;
1175 static std::unique_ptr
<MipsReginfoSection
> create();
1177 MipsReginfoSection(Elf_Mips_RegInfo reginfo
);
1178 size_t getSize() const override
{ return sizeof(Elf_Mips_RegInfo
); }
1179 void writeTo(uint8_t *buf
) override
;
1182 Elf_Mips_RegInfo reginfo
;
1185 // This is a MIPS specific section to hold a space within the data segment
1186 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
1187 // See "Dynamic section" in Chapter 5 in the following document:
1188 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1189 class MipsRldMapSection final
: public SyntheticSection
{
1191 MipsRldMapSection();
1192 size_t getSize() const override
{ return config
->wordsize
; }
1193 void writeTo(uint8_t *buf
) override
{}
1196 // Representation of the combined .ARM.Exidx input sections. We process these
1197 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries
1198 // and add terminating sentinel entries.
1200 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
1201 // a table that the unwinder can derive (Addresses are encoded as offsets from
1203 // | Address of function | Unwind instructions for function |
1204 // where the unwind instructions are either a small number of unwind or the
1205 // special EXIDX_CANTUNWIND entry representing no unwinding information.
1206 // When an exception is thrown from an address A, the unwinder searches the
1207 // table for the closest table entry with Address of function <= A. This means
1208 // that for two consecutive table entries:
1211 // The range of addresses described by U1 is [A1, A2)
1213 // There are two cases where we need a linker generated table entry to fixup
1214 // the address ranges in the table
1216 // - A sentinel entry added with an address higher than all
1217 // executable sections. This was needed to work around libunwind bug pr31091.
1218 // - After address assignment we need to find the highest addressed executable
1219 // section and use the limit of that section so that the unwinder never
1222 // - InputSections without a .ARM.exidx section (usually from Assembly)
1223 // need a table entry so that they terminate the range of the previously
1224 // function. This is pr40277.
1226 // Instead of storing pointers to the .ARM.exidx InputSections from
1227 // InputObjects, we store pointers to the executable sections that need
1228 // .ARM.exidx sections. We can then use the dependentSections of these to
1229 // either find the .ARM.exidx section or know that we need to generate one.
1230 class ARMExidxSyntheticSection
: public SyntheticSection
{
1232 ARMExidxSyntheticSection();
1234 // Add an input section to the ARMExidxSyntheticSection. Returns whether the
1235 // section needs to be removed from the main input section list.
1236 bool addSection(InputSection
*isec
);
1238 size_t getSize() const override
{ return size
; }
1239 void writeTo(uint8_t *buf
) override
;
1240 bool isNeeded() const override
;
1241 // Sort and remove duplicate entries.
1242 void finalizeContents() override
;
1243 InputSection
*getLinkOrderDep() const;
1245 static bool classof(const SectionBase
*sec
) {
1246 return sec
->kind() == InputSectionBase::Synthetic
&&
1247 sec
->type
== llvm::ELF::SHT_ARM_EXIDX
;
1250 // Links to the ARMExidxSections so we can transfer the relocations once the
1252 SmallVector
<InputSection
*, 0> exidxSections
;
1257 // Instead of storing pointers to the .ARM.exidx InputSections from
1258 // InputObjects, we store pointers to the executable sections that need
1259 // .ARM.exidx sections. We can then use the dependentSections of these to
1260 // either find the .ARM.exidx section or know that we need to generate one.
1261 SmallVector
<InputSection
*, 0> executableSections
;
1263 // Value of executableSecitons before finalizeContents(), so that it can be
1264 // run repeateadly during fixed point iteration.
1265 SmallVector
<InputSection
*, 0> originalExecutableSections
;
1267 // The executable InputSection with the highest address to use for the
1268 // sentinel. We store separately from ExecutableSections as merging of
1269 // duplicate entries may mean this InputSection is removed from
1270 // ExecutableSections.
1271 InputSection
*sentinel
= nullptr;
1274 // A container for one or more linker generated thunks. Instances of these
1275 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1276 class ThunkSection final
: public SyntheticSection
{
1278 // ThunkSection in OS, with desired outSecOff of Off
1279 ThunkSection(OutputSection
*os
, uint64_t off
);
1281 // Add a newly created Thunk to this container:
1282 // Thunk is given offset from start of this InputSection
1283 // Thunk defines a symbol in this InputSection that can be used as target
1285 void addThunk(Thunk
*t
);
1286 size_t getSize() const override
;
1287 void writeTo(uint8_t *buf
) override
;
1288 InputSection
*getTargetInputSection() const;
1289 bool assignOffsets();
1291 // When true, round up reported size of section to 4 KiB. See comment
1292 // in addThunkSection() for more details.
1293 bool roundUpSizeForErrata
= false;
1296 SmallVector
<Thunk
*, 0> thunks
;
1300 // Cortex-M Security Extensions. Prefix for functions that should be exported
1301 // for the non-secure world.
1302 const char ACLESESYM_PREFIX
[] = "__acle_se_";
1303 const int ACLESESYM_SIZE
= 8;
1305 class ArmCmseSGVeneer
;
1307 class ArmCmseSGSection final
: public SyntheticSection
{
1310 bool isNeeded() const override
{ return !entries
.empty(); }
1311 size_t getSize() const override
;
1312 void writeTo(uint8_t *buf
) override
;
1313 void addSGVeneer(Symbol
*sym
, Symbol
*ext_sym
);
1314 void addMappingSymbol();
1315 void finalizeContents() override
;
1316 void exportEntries(SymbolTableBaseSection
*symTab
);
1317 uint64_t impLibMaxAddr
= 0;
1320 SmallVector
<std::pair
<Symbol
*, Symbol
*>, 0> entries
;
1321 SmallVector
<ArmCmseSGVeneer
*, 0> sgVeneers
;
1322 uint64_t newEntries
= 0;
1325 // Used to compute outSecOff of .got2 in each object file. This is needed to
1326 // synthesize PLT entries for PPC32 Secure PLT ABI.
1327 class PPC32Got2Section final
: public SyntheticSection
{
1330 size_t getSize() const override
{ return 0; }
1331 bool isNeeded() const override
;
1332 void finalizeContents() override
;
1333 void writeTo(uint8_t *buf
) override
{}
1336 // This section is used to store the addresses of functions that are called
1337 // in range-extending thunks on PowerPC64. When producing position dependent
1338 // code the addresses are link-time constants and the table is written out to
1339 // the binary. When producing position-dependent code the table is allocated and
1340 // filled in by the dynamic linker.
1341 class PPC64LongBranchTargetSection final
: public SyntheticSection
{
1343 PPC64LongBranchTargetSection();
1344 uint64_t getEntryVA(const Symbol
*sym
, int64_t addend
);
1345 std::optional
<uint32_t> addEntry(const Symbol
*sym
, int64_t addend
);
1346 size_t getSize() const override
;
1347 void writeTo(uint8_t *buf
) override
;
1348 bool isNeeded() const override
;
1349 void finalizeContents() override
{ finalized
= true; }
1352 SmallVector
<std::pair
<const Symbol
*, int64_t>, 0> entries
;
1353 llvm::DenseMap
<std::pair
<const Symbol
*, int64_t>, uint32_t> entry_index
;
1354 bool finalized
= false;
1357 template <typename ELFT
>
1358 class PartitionElfHeaderSection final
: public SyntheticSection
{
1360 PartitionElfHeaderSection();
1361 size_t getSize() const override
;
1362 void writeTo(uint8_t *buf
) override
;
1365 template <typename ELFT
>
1366 class PartitionProgramHeadersSection final
: public SyntheticSection
{
1368 PartitionProgramHeadersSection();
1369 size_t getSize() const override
;
1370 void writeTo(uint8_t *buf
) override
;
1373 class PartitionIndexSection final
: public SyntheticSection
{
1375 PartitionIndexSection();
1376 size_t getSize() const override
;
1377 void finalizeContents() override
;
1378 void writeTo(uint8_t *buf
) override
;
1381 // See the following link for the Android-specific loader code that operates on
1383 // https://cs.android.com/android/platform/superproject/+/master:bionic/libc/bionic/libc_init_static.cpp;drc=9425b16978f9c5aa8f2c50c873db470819480d1d;l=192
1384 class MemtagAndroidNote final
: public SyntheticSection
{
1387 : SyntheticSection(llvm::ELF::SHF_ALLOC
, llvm::ELF::SHT_NOTE
,
1388 /*alignment=*/4, ".note.android.memtag") {}
1389 void writeTo(uint8_t *buf
) override
;
1390 size_t getSize() const override
;
1393 class PackageMetadataNote final
: public SyntheticSection
{
1395 PackageMetadataNote()
1396 : SyntheticSection(llvm::ELF::SHF_ALLOC
, llvm::ELF::SHT_NOTE
,
1397 /*alignment=*/4, ".note.package") {}
1398 void writeTo(uint8_t *buf
) override
;
1399 size_t getSize() const override
;
1402 class MemtagGlobalDescriptors final
: public SyntheticSection
{
1404 MemtagGlobalDescriptors()
1405 : SyntheticSection(llvm::ELF::SHF_ALLOC
,
1406 llvm::ELF::SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC
,
1407 /*alignment=*/4, ".memtag.globals.dynamic") {}
1408 void writeTo(uint8_t *buf
) override
;
1409 // The size of the section is non-computable until all addresses are
1410 // synthetized, because the section's contents contain a sorted
1411 // varint-compressed list of pointers to global variables. We only know the
1412 // final size after `finalizeAddressDependentContent()`.
1413 size_t getSize() const override
;
1414 bool updateAllocSize() override
;
1416 void addSymbol(const Symbol
&sym
) {
1417 symbols
.push_back(&sym
);
1420 bool isNeeded() const override
{
1421 return !symbols
.empty();
1425 SmallVector
<const Symbol
*, 0> symbols
;
1428 template <class ELFT
> void createSyntheticSections();
1429 InputSection
*createInterpSection();
1430 MergeInputSection
*createCommentSection();
1431 template <class ELFT
> void splitSections();
1432 void combineEhSections();
1435 bool canHaveMemtagGlobals();
1437 template <typename ELFT
> void writeEhdr(uint8_t *buf
, Partition
&part
);
1438 template <typename ELFT
> void writePhdrs(uint8_t *buf
, Partition
&part
);
1440 Defined
*addSyntheticLocal(StringRef name
, uint8_t type
, uint64_t value
,
1441 uint64_t size
, InputSectionBase
§ion
);
1443 void addVerneed(Symbol
*ss
);
1445 // Linker generated per-partition sections.
1448 uint64_t nameStrTab
;
1450 std::unique_ptr
<SyntheticSection
> elfHeader
;
1451 std::unique_ptr
<SyntheticSection
> programHeaders
;
1452 SmallVector
<PhdrEntry
*, 0> phdrs
;
1454 std::unique_ptr
<ARMExidxSyntheticSection
> armExidx
;
1455 std::unique_ptr
<BuildIdSection
> buildId
;
1456 std::unique_ptr
<SyntheticSection
> dynamic
;
1457 std::unique_ptr
<StringTableSection
> dynStrTab
;
1458 std::unique_ptr
<SymbolTableBaseSection
> dynSymTab
;
1459 std::unique_ptr
<EhFrameHeader
> ehFrameHdr
;
1460 std::unique_ptr
<EhFrameSection
> ehFrame
;
1461 std::unique_ptr
<GnuHashTableSection
> gnuHashTab
;
1462 std::unique_ptr
<HashTableSection
> hashTab
;
1463 std::unique_ptr
<MemtagAndroidNote
> memtagAndroidNote
;
1464 std::unique_ptr
<MemtagGlobalDescriptors
> memtagGlobalDescriptors
;
1465 std::unique_ptr
<PackageMetadataNote
> packageMetadataNote
;
1466 std::unique_ptr
<RelocationBaseSection
> relaDyn
;
1467 std::unique_ptr
<RelrBaseSection
> relrDyn
;
1468 std::unique_ptr
<RelrBaseSection
> relrAuthDyn
;
1469 std::unique_ptr
<VersionDefinitionSection
> verDef
;
1470 std::unique_ptr
<SyntheticSection
> verNeed
;
1471 std::unique_ptr
<VersionTableSection
> verSym
;
1473 unsigned getNumber() const { return this - &partitions
[0] + 1; }
1476 LLVM_LIBRARY_VISIBILITY
extern Partition
*mainPart
;
1478 inline Partition
&SectionBase::getPartition() const {
1480 return partitions
[partition
- 1];
1483 // Linker generated sections which can be used as inputs and are not specific to
1486 std::unique_ptr
<InputSection
> attributes
;
1487 std::unique_ptr
<SyntheticSection
> riscvAttributes
;
1488 std::unique_ptr
<BssSection
> bss
;
1489 std::unique_ptr
<BssSection
> bssRelRo
;
1490 std::unique_ptr
<GotSection
> got
;
1491 std::unique_ptr
<GotPltSection
> gotPlt
;
1492 std::unique_ptr
<IgotPltSection
> igotPlt
;
1493 std::unique_ptr
<RelroPaddingSection
> relroPadding
;
1494 std::unique_ptr
<SyntheticSection
> armCmseSGSection
;
1495 std::unique_ptr
<PPC64LongBranchTargetSection
> ppc64LongBranchTarget
;
1496 std::unique_ptr
<SyntheticSection
> mipsAbiFlags
;
1497 std::unique_ptr
<MipsGotSection
> mipsGot
;
1498 std::unique_ptr
<SyntheticSection
> mipsOptions
;
1499 std::unique_ptr
<SyntheticSection
> mipsReginfo
;
1500 std::unique_ptr
<MipsRldMapSection
> mipsRldMap
;
1501 std::unique_ptr
<SyntheticSection
> partEnd
;
1502 std::unique_ptr
<SyntheticSection
> partIndex
;
1503 std::unique_ptr
<PltSection
> plt
;
1504 std::unique_ptr
<IpltSection
> iplt
;
1505 std::unique_ptr
<PPC32Got2Section
> ppc32Got2
;
1506 std::unique_ptr
<IBTPltSection
> ibtPlt
;
1507 std::unique_ptr
<RelocationBaseSection
> relaPlt
;
1508 // Non-SHF_ALLOC sections
1509 std::unique_ptr
<SyntheticSection
> debugNames
;
1510 std::unique_ptr
<GdbIndexSection
> gdbIndex
;
1511 std::unique_ptr
<StringTableSection
> shStrTab
;
1512 std::unique_ptr
<StringTableSection
> strTab
;
1513 std::unique_ptr
<SymbolTableBaseSection
> symTab
;
1514 std::unique_ptr
<SymtabShndxSection
> symTabShndx
;
1519 LLVM_LIBRARY_VISIBILITY
extern InStruct in
;
1521 } // namespace lld::elf