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"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/MC/StringTableBuilder.h"
28 #include "llvm/Support/Endian.h"
35 class SymbolTableBaseSection;
36 class VersionNeedBaseSection;
38 class SyntheticSection : public InputSection {
40 SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
42 : InputSection(nullptr, flags, type, alignment, {}, name,
43 InputSectionBase::Synthetic) {
47 virtual ~SyntheticSection() = default;
48 virtual void writeTo(uint8_t *buf) = 0;
49 virtual size_t getSize() const = 0;
50 virtual void finalizeContents() {}
51 // If the section has the SHF_ALLOC flag and the size may be changed if
52 // thunks are added, update the section size.
53 virtual bool updateAllocSize() { return false; }
54 virtual bool isNeeded() const { return true; }
56 static bool classof(const SectionBase *d) {
57 return d->kind() == InputSectionBase::Synthetic;
62 EhSectionPiece *cie = nullptr;
63 std::vector<EhSectionPiece *> fdes;
66 // Section for .eh_frame.
67 class EhFrameSection final : public SyntheticSection {
70 void writeTo(uint8_t *buf) override;
71 void finalizeContents() override;
72 bool isNeeded() const override { return !sections.empty(); }
73 size_t getSize() const override { return size; }
75 static bool classof(const SectionBase *d) {
76 return SyntheticSection::classof(d) && d->name == ".eh_frame";
79 void addSection(EhInputSection *sec);
81 std::vector<EhInputSection *> sections;
89 std::vector<FdeData> getFdeData() const;
90 ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; }
93 // This is used only when parsing EhInputSection. We keep it here to avoid
94 // allocating one for each EhInputSection.
95 llvm::DenseMap<size_t, CieRecord *> offsetToCie;
99 template <class ELFT, class RelTy>
100 void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels);
101 template <class ELFT>
102 void addSectionAux(EhInputSection *s);
104 template <class ELFT, class RelTy>
105 CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels);
107 template <class ELFT, class RelTy>
108 bool isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels);
110 uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
112 std::vector<CieRecord *> cieRecords;
114 // CIE records are uniquified by their contents and personality functions.
115 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
118 class GotSection : public SyntheticSection {
121 size_t getSize() const override { return size; }
122 void finalizeContents() override;
123 bool isNeeded() const override;
124 void writeTo(uint8_t *buf) override;
126 void addEntry(Symbol &sym);
127 bool addDynTlsEntry(Symbol &sym);
129 uint64_t getGlobalDynAddr(const Symbol &b) const;
130 uint64_t getGlobalDynOffset(const Symbol &b) const;
132 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
133 uint32_t getTlsIndexOff() const { return tlsIndexOff; }
135 // Flag to force GOT to be in output if we have relocations
136 // that relies on its address.
137 bool hasGotOffRel = false;
140 size_t numEntries = 0;
141 uint32_t tlsIndexOff = -1;
145 // .note.GNU-stack section.
146 class GnuStackSection : public SyntheticSection {
149 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
150 void writeTo(uint8_t *buf) override {}
151 size_t getSize() const override { return 0; }
154 class GnuPropertySection : public SyntheticSection {
156 GnuPropertySection();
157 void writeTo(uint8_t *buf) override;
158 size_t getSize() const override;
161 // .note.gnu.build-id section.
162 class BuildIdSection : public SyntheticSection {
163 // First 16 bytes are a header.
164 static const unsigned headerSize = 16;
167 const size_t hashSize;
169 void writeTo(uint8_t *buf) override;
170 size_t getSize() const override { return headerSize + hashSize; }
171 void writeBuildId(llvm::ArrayRef<uint8_t> buf);
177 // BssSection is used to reserve space for copy relocations and common symbols.
178 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
179 // that are used for writable symbols, read-only symbols and common symbols,
181 class BssSection final : public SyntheticSection {
183 BssSection(StringRef name, uint64_t size, uint32_t alignment);
184 void writeTo(uint8_t *) override {
185 llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
187 bool isNeeded() const override { return size != 0; }
188 size_t getSize() const override { return size; }
190 static bool classof(const SectionBase *s) { return s->bss; }
194 class MipsGotSection final : public SyntheticSection {
197 void writeTo(uint8_t *buf) override;
198 size_t getSize() const override { return size; }
199 bool updateAllocSize() override;
200 void finalizeContents() override;
201 bool isNeeded() const override;
203 // Join separate GOTs built for each input file to generate
204 // primary and optional multiple secondary GOTs.
207 void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
208 void addDynTlsEntry(InputFile &file, Symbol &sym);
209 void addTlsIndex(InputFile &file);
211 uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
212 int64_t addend) const;
213 uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
214 int64_t addend) const;
215 uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
216 uint64_t getTlsIndexOffset(const InputFile *f) const;
218 // Returns the symbol which corresponds to the first entry of the global part
219 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
221 // Returns nullptr if the global part is empty.
222 const Symbol *getFirstGlobalEntry() const;
224 // Returns the number of entries in the local part of GOT including
225 // the number of reserved entries.
226 unsigned getLocalEntriesNum() const;
228 // Return _gp value for primary GOT (nullptr) or particular input file.
229 uint64_t getGp(const InputFile *f = nullptr) const;
232 // MIPS GOT consists of three parts: local, global and tls. Each part
233 // contains different types of entries. Here is a layout of GOT:
234 // - Header entries |
235 // - Page entries | Local part
236 // - Local entries (16-bit access) |
237 // - Local entries (32-bit access) |
238 // - Normal global entries || Global part
239 // - Reloc-only global entries ||
240 // - TLS entries ||| TLS part
243 // Two entries hold predefined value 0x0 and 0x80000000.
245 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
246 // relocation against local symbols. They are initialized by higher 16-bit
247 // of the corresponding symbol's value. So each 64kb of address space
248 // requires a single GOT entry.
249 // Local entries (16-bit access):
250 // These entries created by GOT relocations against global non-preemptible
251 // symbols so dynamic linker is not necessary to resolve the symbol's
252 // values. "16-bit access" means that corresponding relocations address
253 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own
255 // Local entries (32-bit access):
256 // These entries are the same as above but created by relocations which
257 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
258 // Normal global entries:
259 // These entries created by GOT relocations against preemptible global
260 // symbols. They need to be initialized by dynamic linker and they ordered
261 // exactly as the corresponding entries in the dynamic symbols table.
262 // Reloc-only global entries:
263 // These entries created for symbols that are referenced by dynamic
264 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative
265 // addressing, but MIPS ABI requires that these entries be present in GOT.
267 // Entries created by TLS relocations.
269 // If the sum of local, global and tls entries is less than 64K only single
270 // got is enough. Otherwise, multi-got is created. Series of primary and
271 // multiple secondary GOTs have the following layout:
276 // Relocation only entries
285 // All GOT entries required by relocations from a single input file entirely
286 // belong to either primary or one of secondary GOTs. To reference GOT entries
287 // each GOT has its own _gp value points to the "middle" of the GOT.
288 // In the code this value loaded to the register which is used for GOT access.
290 // MIPS 32 function's prologue:
292 // 0: R_MIPS_HI16 _gp_disp
294 // 4: R_MIPS_LO16 _gp_disp
298 // 14: R_MIPS_GPREL16 main
300 // Dynamic linker does not know anything about secondary GOTs and cannot
301 // use a regular MIPS mechanism for GOT entries initialization. So we have
302 // to use an approach accepted by other architectures and create dynamic
303 // relocations R_MIPS_REL32 to initialize global entries (and local in case
304 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
305 // requires GOT entries and correspondingly ordered dynamic symbol table
306 // entries to deal with dynamic relocations. To handle this problem
307 // relocation-only section in the primary GOT contains entries for all
308 // symbols referenced in global parts of secondary GOTs. Although the sum
309 // of local and normal global entries of the primary got should be less
310 // than 64K, the size of the primary got (including relocation-only entries
311 // can be greater than 64K, because parts of the primary got that overflow
312 // the 64K limit are used only by the dynamic linker at dynamic link-time
313 // and not by 16-bit gp-relative addressing at run-time.
315 // For complete multi-GOT description see the following link
316 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
318 // Number of "Header" entries.
319 static const unsigned headerEntriesNum = 2;
323 // Symbol and addend.
324 using GotEntry = std::pair<Symbol *, int64_t>;
327 InputFile *file = nullptr;
328 size_t startIndex = 0;
333 PageBlock() : firstIndex(0), count(0) {}
336 // Map output sections referenced by MIPS GOT relocations
337 // to the description (index/count) "page" entries allocated
339 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
340 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
341 llvm::MapVector<GotEntry, size_t> local16;
342 llvm::MapVector<GotEntry, size_t> local32;
343 llvm::MapVector<Symbol *, size_t> global;
344 llvm::MapVector<Symbol *, size_t> relocs;
345 llvm::MapVector<Symbol *, size_t> tls;
346 // Set of symbols referenced by dynamic TLS relocations.
347 llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
349 // Total number of all entries.
350 size_t getEntriesNum() const;
351 // Number of "page" entries.
352 size_t getPageEntriesNum() const;
353 // Number of entries require 16-bit index to access.
354 size_t getIndexedEntriesNum() const;
357 // Container of GOT created for each input file.
358 // After building a final series of GOTs this container
359 // holds primary and secondary GOT's.
360 std::vector<FileGot> gots;
362 // Return (and create if necessary) `FileGot`.
363 FileGot &getGot(InputFile &f);
365 // Try to merge two GOTs. In case of success the `Dst` contains
366 // result of merging and the function returns true. In case of
367 // ovwerflow the `Dst` is unchanged and the function returns false.
368 bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
371 class GotPltSection final : public SyntheticSection {
374 void addEntry(Symbol &sym);
375 size_t getSize() const override;
376 void writeTo(uint8_t *buf) override;
377 bool isNeeded() const override;
379 // Flag to force GotPlt to be in output if we have relocations
380 // that relies on its address.
381 bool hasGotPltOffRel = false;
384 std::vector<const Symbol *> entries;
387 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
388 // Symbols that will be relocated by Target->IRelativeRel.
389 // On most Targets the IgotPltSection will immediately follow the GotPltSection
390 // on ARM the IgotPltSection will immediately follow the GotSection.
391 class IgotPltSection final : public SyntheticSection {
394 void addEntry(Symbol &sym);
395 size_t getSize() const override;
396 void writeTo(uint8_t *buf) override;
397 bool isNeeded() const override { return !entries.empty(); }
400 std::vector<const Symbol *> entries;
403 class StringTableSection final : public SyntheticSection {
405 StringTableSection(StringRef name, bool dynamic);
406 unsigned addString(StringRef s, bool hashIt = true);
407 void writeTo(uint8_t *buf) override;
408 size_t getSize() const override { return size; }
409 bool isDynamic() const { return dynamic; }
416 llvm::DenseMap<StringRef, unsigned> stringMap;
417 std::vector<StringRef> strings;
422 DynamicReloc(RelType type, const InputSectionBase *inputSec,
423 uint64_t offsetInSec, bool useSymVA, Symbol *sym, int64_t addend)
424 : type(type), sym(sym), inputSec(inputSec), offsetInSec(offsetInSec),
425 useSymVA(useSymVA), addend(addend), outputSec(nullptr) {}
426 // This constructor records dynamic relocation settings used by MIPS
427 // multi-GOT implementation. It's to relocate addresses of 64kb pages
428 // lie inside the output section.
429 DynamicReloc(RelType type, const InputSectionBase *inputSec,
430 uint64_t offsetInSec, const OutputSection *outputSec,
432 : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec),
433 useSymVA(false), addend(addend), outputSec(outputSec) {}
435 uint64_t getOffset() const;
436 uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
438 // Computes the addend of the dynamic relocation. Note that this is not the
439 // same as the addend member variable as it also includes the symbol address
440 // if useSymVA is true.
441 int64_t computeAddend() const;
446 const InputSectionBase *inputSec = nullptr;
447 uint64_t offsetInSec;
448 // If this member is true, the dynamic relocation will not be against the
449 // symbol but will instead be a relative relocation that simply adds the
450 // load address. This means we need to write the symbol virtual address
451 // plus the original addend as the final relocation addend.
454 const OutputSection *outputSec;
457 template <class ELFT> class DynamicSection final : public SyntheticSection {
458 using Elf_Dyn = typename ELFT::Dyn;
459 using Elf_Rel = typename ELFT::Rel;
460 using Elf_Rela = typename ELFT::Rela;
461 using Elf_Relr = typename ELFT::Relr;
462 using Elf_Shdr = typename ELFT::Shdr;
463 using Elf_Sym = typename ELFT::Sym;
465 // finalizeContents() fills this vector with the section contents.
466 std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries;
470 void finalizeContents() override;
471 void writeTo(uint8_t *buf) override;
472 size_t getSize() const override { return size; }
475 void add(int32_t tag, std::function<uint64_t()> fn);
476 void addInt(int32_t tag, uint64_t val);
477 void addInSec(int32_t tag, InputSection *sec);
478 void addInSecRelative(int32_t tag, InputSection *sec);
479 void addOutSec(int32_t tag, OutputSection *sec);
480 void addSize(int32_t tag, OutputSection *sec);
481 void addSym(int32_t tag, Symbol *sym);
486 class RelocationBaseSection : public SyntheticSection {
488 RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag,
489 int32_t sizeDynamicTag);
490 void addReloc(RelType dynType, InputSectionBase *isec, uint64_t offsetInSec,
492 // Add a dynamic relocation that might need an addend. This takes care of
493 // writing the addend to the output section if needed.
494 void addReloc(RelType dynType, InputSectionBase *inputSec,
495 uint64_t offsetInSec, Symbol *sym, int64_t addend, RelExpr expr,
497 void addReloc(const DynamicReloc &reloc);
498 bool isNeeded() const override { return !relocs.empty(); }
499 size_t getSize() const override { return relocs.size() * this->entsize; }
500 size_t getRelativeRelocCount() const { return numRelativeRelocs; }
501 void finalizeContents() override;
502 int32_t dynamicTag, sizeDynamicTag;
503 std::vector<DynamicReloc> relocs;
506 size_t numRelativeRelocs = 0;
509 template <class ELFT>
510 class RelocationSection final : public RelocationBaseSection {
511 using Elf_Rel = typename ELFT::Rel;
512 using Elf_Rela = typename ELFT::Rela;
515 RelocationSection(StringRef name, bool sort);
516 void writeTo(uint8_t *buf) override;
522 template <class ELFT>
523 class AndroidPackedRelocationSection final : public RelocationBaseSection {
524 using Elf_Rel = typename ELFT::Rel;
525 using Elf_Rela = typename ELFT::Rela;
528 AndroidPackedRelocationSection(StringRef name);
530 bool updateAllocSize() override;
531 size_t getSize() const override { return relocData.size(); }
532 void writeTo(uint8_t *buf) override {
533 memcpy(buf, relocData.data(), relocData.size());
537 SmallVector<char, 0> relocData;
540 struct RelativeReloc {
541 uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
543 const InputSectionBase *inputSec;
544 uint64_t offsetInSec;
547 class RelrBaseSection : public SyntheticSection {
550 bool isNeeded() const override { return !relocs.empty(); }
551 std::vector<RelativeReloc> relocs;
554 // RelrSection is used to encode offsets for relative relocations.
555 // Proposal for adding SHT_RELR sections to generic-abi is here:
556 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
557 // For more details, see the comment in RelrSection::updateAllocSize().
558 template <class ELFT> class RelrSection final : public RelrBaseSection {
559 using Elf_Relr = typename ELFT::Relr;
564 bool updateAllocSize() override;
565 size_t getSize() const override { return relrRelocs.size() * this->entsize; }
566 void writeTo(uint8_t *buf) override {
567 memcpy(buf, relrRelocs.data(), getSize());
571 std::vector<Elf_Relr> relrRelocs;
574 struct SymbolTableEntry {
579 class SymbolTableBaseSection : public SyntheticSection {
581 SymbolTableBaseSection(StringTableSection &strTabSec);
582 void finalizeContents() override;
583 size_t getSize() const override { return getNumSymbols() * entsize; }
584 void addSymbol(Symbol *sym);
585 unsigned getNumSymbols() const { return symbols.size() + 1; }
586 size_t getSymbolIndex(Symbol *sym);
587 ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
590 void sortSymTabSymbols();
592 // A vector of symbols and their string table offsets.
593 std::vector<SymbolTableEntry> symbols;
595 StringTableSection &strTabSec;
597 llvm::once_flag onceFlag;
598 llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
599 llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
602 template <class ELFT>
603 class SymbolTableSection final : public SymbolTableBaseSection {
604 using Elf_Sym = typename ELFT::Sym;
607 SymbolTableSection(StringTableSection &strTabSec);
608 void writeTo(uint8_t *buf) override;
611 class SymtabShndxSection final : public SyntheticSection {
613 SymtabShndxSection();
615 void writeTo(uint8_t *buf) override;
616 size_t getSize() const override;
617 bool isNeeded() const override;
618 void finalizeContents() override;
621 // Outputs GNU Hash section. For detailed explanation see:
622 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
623 class GnuHashTableSection final : public SyntheticSection {
625 GnuHashTableSection();
626 void finalizeContents() override;
627 void writeTo(uint8_t *buf) override;
628 size_t getSize() const override { return size; }
630 // Adds symbols to the hash table.
631 // Sorts the input to satisfy GNU hash section requirements.
632 void addSymbols(std::vector<SymbolTableEntry> &symbols);
635 // See the comment in writeBloomFilter.
636 enum { Shift2 = 26 };
638 void writeBloomFilter(uint8_t *buf);
639 void writeHashTable(uint8_t *buf);
648 std::vector<Entry> symbols;
654 class HashTableSection final : public SyntheticSection {
657 void finalizeContents() override;
658 void writeTo(uint8_t *buf) override;
659 size_t getSize() const override { return size; }
665 // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT
666 // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily
669 // On PowerPC, this section contains lazy symbol resolvers. A branch instruction
670 // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a
671 // lazy symbol resolver.
673 // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs.
674 // A call instruction jumps to a .plt.sec entry, which will then jump to the
675 // target (BIND_NOW) or a .plt entry.
676 class PltSection : public SyntheticSection {
679 void writeTo(uint8_t *buf) override;
680 size_t getSize() const override;
681 bool isNeeded() const override;
683 void addEntry(Symbol &sym);
684 size_t getNumEntries() const { return entries.size(); }
688 std::vector<const Symbol *> entries;
691 // Used for non-preemptible ifuncs. It does not have a header. Each entry is
692 // associated with an IRELATIVE relocation, which will be resolved eagerly at
693 // runtime. PltSection can only contain entries associated with JUMP_SLOT
694 // relocations, so IPLT entries are in a separate section.
695 class IpltSection final : public SyntheticSection {
696 std::vector<const Symbol *> entries;
700 void writeTo(uint8_t *buf) override;
701 size_t getSize() const override;
702 bool isNeeded() const override { return !entries.empty(); }
704 void addEntry(Symbol &sym);
707 class PPC32GlinkSection : public PltSection {
710 void writeTo(uint8_t *buf) override;
711 size_t getSize() const override;
713 std::vector<const Symbol *> canonical_plts;
714 static constexpr size_t footerSize = 64;
718 class IBTPltSection : public SyntheticSection {
721 void writeTo(uint8_t *Buf) override;
722 size_t getSize() const override;
725 class GdbIndexSection final : public SyntheticSection {
727 struct AddressEntry {
728 InputSection *section;
730 uint64_t highAddress;
739 struct NameAttrEntry {
740 llvm::CachedHashStringRef name;
741 uint32_t cuIndexAndAttrs;
746 std::vector<AddressEntry> addressAreas;
747 std::vector<CuEntry> compilationUnits;
751 llvm::CachedHashStringRef name;
752 std::vector<uint32_t> cuVector;
754 uint32_t cuVectorOff;
758 template <typename ELFT> static GdbIndexSection *create();
759 void writeTo(uint8_t *buf) override;
760 size_t getSize() const override { return size; }
761 bool isNeeded() const override;
764 struct GdbIndexHeader {
765 llvm::support::ulittle32_t version;
766 llvm::support::ulittle32_t cuListOff;
767 llvm::support::ulittle32_t cuTypesOff;
768 llvm::support::ulittle32_t addressAreaOff;
769 llvm::support::ulittle32_t symtabOff;
770 llvm::support::ulittle32_t constantPoolOff;
773 void initOutputSize();
774 size_t computeSymtabSize() const;
776 // Each chunk contains information gathered from debug sections of a
777 // single object file.
778 std::vector<GdbChunk> chunks;
780 // A symbol table for this .gdb_index section.
781 std::vector<GdbSymbol> symbols;
786 // --eh-frame-hdr option tells linker to construct a header for all the
787 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
788 // and also to a PT_GNU_EH_FRAME segment.
789 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
790 // calling dl_iterate_phdr.
791 // This section contains a lookup table for quick binary search of FDEs.
792 // Detailed info about internals can be found in Ian Lance Taylor's blog:
793 // http://www.airs.com/blog/archives/460 (".eh_frame")
794 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
795 class EhFrameHeader final : public SyntheticSection {
799 void writeTo(uint8_t *buf) override;
800 size_t getSize() const override;
801 bool isNeeded() const override;
804 // For more information about .gnu.version and .gnu.version_r see:
805 // https://www.akkadia.org/drepper/symbol-versioning
807 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
808 // contain symbol version definitions. The number of entries in this section
809 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
810 // The section shall contain an array of Elf_Verdef structures, optionally
811 // followed by an array of Elf_Verdaux structures.
812 class VersionDefinitionSection final : public SyntheticSection {
814 VersionDefinitionSection();
815 void finalizeContents() override;
816 size_t getSize() const override;
817 void writeTo(uint8_t *buf) override;
820 enum { EntrySize = 28 };
821 void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
822 StringRef getFileDefName();
824 unsigned fileDefNameOff;
825 std::vector<unsigned> verDefNameOffs;
828 // The .gnu.version section specifies the required version of each symbol in the
829 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
830 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
831 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
832 // The values 0 and 1 are reserved. All other values are used for versions in
833 // the own object or in any of the dependencies.
834 class VersionTableSection final : public SyntheticSection {
836 VersionTableSection();
837 void finalizeContents() override;
838 size_t getSize() const override;
839 void writeTo(uint8_t *buf) override;
840 bool isNeeded() const override;
843 // The .gnu.version_r section defines the version identifiers used by
844 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
845 // Elf_Verneed specifies the version requirements for a single DSO, and contains
846 // a reference to a linked list of Elf_Vernaux data structures which define the
847 // mapping from version identifiers to version names.
848 template <class ELFT>
849 class VersionNeedSection final : public SyntheticSection {
850 using Elf_Verneed = typename ELFT::Verneed;
851 using Elf_Vernaux = typename ELFT::Vernaux;
855 uint32_t verneedIndex;
861 std::vector<Vernaux> vernauxs;
864 std::vector<Verneed> verneeds;
867 VersionNeedSection();
868 void finalizeContents() override;
869 void writeTo(uint8_t *buf) override;
870 size_t getSize() const override;
871 bool isNeeded() const override;
874 // MergeSyntheticSection is a class that allows us to put mergeable sections
875 // with different attributes in a single output sections. To do that
876 // we put them into MergeSyntheticSection synthetic input sections which are
877 // attached to regular output sections.
878 class MergeSyntheticSection : public SyntheticSection {
880 void addSection(MergeInputSection *ms);
881 std::vector<MergeInputSection *> sections;
884 MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
886 : SyntheticSection(flags, type, alignment, name) {}
889 class MergeTailSection final : public MergeSyntheticSection {
891 MergeTailSection(StringRef name, uint32_t type, uint64_t flags,
894 size_t getSize() const override;
895 void writeTo(uint8_t *buf) override;
896 void finalizeContents() override;
899 llvm::StringTableBuilder builder;
902 class MergeNoTailSection final : public MergeSyntheticSection {
904 MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags,
906 : MergeSyntheticSection(name, type, flags, alignment) {}
908 size_t getSize() const override { return size; }
909 void writeTo(uint8_t *buf) override;
910 void finalizeContents() override;
913 // We use the most significant bits of a hash as a shard ID.
914 // The reason why we don't want to use the least significant bits is
915 // because DenseMap also uses lower bits to determine a bucket ID.
916 // If we use lower bits, it significantly increases the probability of
918 size_t getShardId(uint32_t hash) {
919 assert((hash >> 31) == 0);
920 return hash >> (31 - llvm::countTrailingZeros(numShards));
926 // String table contents
927 constexpr static size_t numShards = 32;
928 std::vector<llvm::StringTableBuilder> shards;
929 size_t shardOffsets[numShards];
932 // .MIPS.abiflags section.
933 template <class ELFT>
934 class MipsAbiFlagsSection final : public SyntheticSection {
935 using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
938 static MipsAbiFlagsSection *create();
940 MipsAbiFlagsSection(Elf_Mips_ABIFlags flags);
941 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
942 void writeTo(uint8_t *buf) override;
945 Elf_Mips_ABIFlags flags;
948 // .MIPS.options section.
949 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
950 using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
951 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
954 static MipsOptionsSection *create();
956 MipsOptionsSection(Elf_Mips_RegInfo reginfo);
957 void writeTo(uint8_t *buf) override;
959 size_t getSize() const override {
960 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
964 Elf_Mips_RegInfo reginfo;
967 // MIPS .reginfo section.
968 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
969 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
972 static MipsReginfoSection *create();
974 MipsReginfoSection(Elf_Mips_RegInfo reginfo);
975 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
976 void writeTo(uint8_t *buf) override;
979 Elf_Mips_RegInfo reginfo;
982 // This is a MIPS specific section to hold a space within the data segment
983 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
984 // See "Dynamic section" in Chapter 5 in the following document:
985 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
986 class MipsRldMapSection : public SyntheticSection {
989 size_t getSize() const override { return config->wordsize; }
990 void writeTo(uint8_t *buf) override {}
993 // Representation of the combined .ARM.Exidx input sections. We process these
994 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries
995 // and add terminating sentinel entries.
997 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
998 // a table that the unwinder can derive (Addresses are encoded as offsets from
1000 // | Address of function | Unwind instructions for function |
1001 // where the unwind instructions are either a small number of unwind or the
1002 // special EXIDX_CANTUNWIND entry representing no unwinding information.
1003 // When an exception is thrown from an address A, the unwinder searches the
1004 // table for the closest table entry with Address of function <= A. This means
1005 // that for two consecutive table entries:
1008 // The range of addresses described by U1 is [A1, A2)
1010 // There are two cases where we need a linker generated table entry to fixup
1011 // the address ranges in the table
1013 // - A sentinel entry added with an address higher than all
1014 // executable sections. This was needed to work around libunwind bug pr31091.
1015 // - After address assignment we need to find the highest addressed executable
1016 // section and use the limit of that section so that the unwinder never
1019 // - InputSections without a .ARM.exidx section (usually from Assembly)
1020 // need a table entry so that they terminate the range of the previously
1021 // function. This is pr40277.
1023 // Instead of storing pointers to the .ARM.exidx InputSections from
1024 // InputObjects, we store pointers to the executable sections that need
1025 // .ARM.exidx sections. We can then use the dependentSections of these to
1026 // either find the .ARM.exidx section or know that we need to generate one.
1027 class ARMExidxSyntheticSection : public SyntheticSection {
1029 ARMExidxSyntheticSection();
1031 // Add an input section to the ARMExidxSyntheticSection. Returns whether the
1032 // section needs to be removed from the main input section list.
1033 bool addSection(InputSection *isec);
1035 size_t getSize() const override { return size; }
1036 void writeTo(uint8_t *buf) override;
1037 bool isNeeded() const override;
1038 // Sort and remove duplicate entries.
1039 void finalizeContents() override;
1040 InputSection *getLinkOrderDep() const;
1042 static bool classof(const SectionBase *d);
1044 // Links to the ARMExidxSections so we can transfer the relocations once the
1046 std::vector<InputSection *> exidxSections;
1051 // Instead of storing pointers to the .ARM.exidx InputSections from
1052 // InputObjects, we store pointers to the executable sections that need
1053 // .ARM.exidx sections. We can then use the dependentSections of these to
1054 // either find the .ARM.exidx section or know that we need to generate one.
1055 std::vector<InputSection *> executableSections;
1057 // The executable InputSection with the highest address to use for the
1058 // sentinel. We store separately from ExecutableSections as merging of
1059 // duplicate entries may mean this InputSection is removed from
1060 // ExecutableSections.
1061 InputSection *sentinel = nullptr;
1064 // A container for one or more linker generated thunks. Instances of these
1065 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1066 class ThunkSection : public SyntheticSection {
1068 // ThunkSection in OS, with desired outSecOff of Off
1069 ThunkSection(OutputSection *os, uint64_t off);
1071 // Add a newly created Thunk to this container:
1072 // Thunk is given offset from start of this InputSection
1073 // Thunk defines a symbol in this InputSection that can be used as target
1075 void addThunk(Thunk *t);
1076 size_t getSize() const override;
1077 void writeTo(uint8_t *buf) override;
1078 InputSection *getTargetInputSection() const;
1079 bool assignOffsets();
1081 // When true, round up reported size of section to 4 KiB. See comment
1082 // in addThunkSection() for more details.
1083 bool roundUpSizeForErrata = false;
1086 std::vector<Thunk *> thunks;
1090 // Used to compute outSecOff of .got2 in each object file. This is needed to
1091 // synthesize PLT entries for PPC32 Secure PLT ABI.
1092 class PPC32Got2Section final : public SyntheticSection {
1095 size_t getSize() const override { return 0; }
1096 bool isNeeded() const override;
1097 void finalizeContents() override;
1098 void writeTo(uint8_t *buf) override {}
1101 // This section is used to store the addresses of functions that are called
1102 // in range-extending thunks on PowerPC64. When producing position dependent
1103 // code the addresses are link-time constants and the table is written out to
1104 // the binary. When producing position-dependent code the table is allocated and
1105 // filled in by the dynamic linker.
1106 class PPC64LongBranchTargetSection final : public SyntheticSection {
1108 PPC64LongBranchTargetSection();
1109 uint64_t getEntryVA(const Symbol *sym, int64_t addend);
1110 llvm::Optional<uint32_t> addEntry(const Symbol *sym, int64_t addend);
1111 size_t getSize() const override;
1112 void writeTo(uint8_t *buf) override;
1113 bool isNeeded() const override;
1114 void finalizeContents() override { finalized = true; }
1117 std::vector<std::pair<const Symbol *, int64_t>> entries;
1118 llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index;
1119 bool finalized = false;
1122 template <typename ELFT>
1123 class PartitionElfHeaderSection : public SyntheticSection {
1125 PartitionElfHeaderSection();
1126 size_t getSize() const override;
1127 void writeTo(uint8_t *buf) override;
1130 template <typename ELFT>
1131 class PartitionProgramHeadersSection : public SyntheticSection {
1133 PartitionProgramHeadersSection();
1134 size_t getSize() const override;
1135 void writeTo(uint8_t *buf) override;
1138 class PartitionIndexSection : public SyntheticSection {
1140 PartitionIndexSection();
1141 size_t getSize() const override;
1142 void finalizeContents() override;
1143 void writeTo(uint8_t *buf) override;
1146 InputSection *createInterpSection();
1147 MergeInputSection *createCommentSection();
1148 MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type,
1149 uint64_t flags, uint32_t alignment);
1150 template <class ELFT> void splitSections();
1152 template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
1153 template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
1155 Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
1156 uint64_t size, InputSectionBase §ion);
1158 void addVerneed(Symbol *ss);
1160 // Linker generated per-partition sections.
1163 uint64_t nameStrTab;
1165 SyntheticSection *elfHeader;
1166 SyntheticSection *programHeaders;
1167 std::vector<PhdrEntry *> phdrs;
1169 ARMExidxSyntheticSection *armExidx;
1170 BuildIdSection *buildId;
1171 SyntheticSection *dynamic;
1172 StringTableSection *dynStrTab;
1173 SymbolTableBaseSection *dynSymTab;
1174 EhFrameHeader *ehFrameHdr;
1175 EhFrameSection *ehFrame;
1176 GnuHashTableSection *gnuHashTab;
1177 HashTableSection *hashTab;
1178 RelocationBaseSection *relaDyn;
1179 RelrBaseSection *relrDyn;
1180 VersionDefinitionSection *verDef;
1181 SyntheticSection *verNeed;
1182 VersionTableSection *verSym;
1184 unsigned getNumber() const { return this - &partitions[0] + 1; }
1187 extern Partition *mainPart;
1189 inline Partition &SectionBase::getPartition() const {
1191 return partitions[partition - 1];
1194 // Linker generated sections which can be used as inputs and are not specific to
1197 InputSection *armAttributes;
1199 BssSection *bssRelRo;
1201 GotPltSection *gotPlt;
1202 IgotPltSection *igotPlt;
1203 PPC64LongBranchTargetSection *ppc64LongBranchTarget;
1204 MipsGotSection *mipsGot;
1205 MipsRldMapSection *mipsRldMap;
1206 SyntheticSection *partEnd;
1207 SyntheticSection *partIndex;
1210 PPC32Got2Section *ppc32Got2;
1211 IBTPltSection *ibtPlt;
1212 RelocationBaseSection *relaPlt;
1213 RelocationBaseSection *relaIplt;
1214 StringTableSection *shStrTab;
1215 StringTableSection *strTab;
1216 SymbolTableBaseSection *symTab;
1217 SymtabShndxSection *symTabShndx;