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 template <class ELFT> void addSection(InputSectionBase *s);
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 addSectionAux(EhInputSection *s, llvm::ArrayRef<RelTy> rels);
102 template <class ELFT, class RelTy>
103 CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels);
105 template <class ELFT, class RelTy>
106 bool isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels);
108 uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
110 std::vector<CieRecord *> cieRecords;
112 // CIE records are uniquified by their contents and personality functions.
113 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
116 class GotSection : public SyntheticSection {
119 size_t getSize() const override { return size; }
120 void finalizeContents() override;
121 bool isNeeded() const override;
122 void writeTo(uint8_t *buf) override;
124 void addEntry(Symbol &sym);
125 bool addDynTlsEntry(Symbol &sym);
127 uint64_t getGlobalDynAddr(const Symbol &b) const;
128 uint64_t getGlobalDynOffset(const Symbol &b) const;
130 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
131 uint32_t getTlsIndexOff() const { return tlsIndexOff; }
133 // Flag to force GOT to be in output if we have relocations
134 // that relies on its address.
135 bool hasGotOffRel = false;
138 size_t numEntries = 0;
139 uint32_t tlsIndexOff = -1;
143 // .note.GNU-stack section.
144 class GnuStackSection : public SyntheticSection {
147 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
148 void writeTo(uint8_t *buf) override {}
149 size_t getSize() const override { return 0; }
152 class GnuPropertySection : public SyntheticSection {
154 GnuPropertySection();
155 void writeTo(uint8_t *buf) override;
156 size_t getSize() const override;
159 // .note.gnu.build-id section.
160 class BuildIdSection : public SyntheticSection {
161 // First 16 bytes are a header.
162 static const unsigned headerSize = 16;
165 const size_t hashSize;
167 void writeTo(uint8_t *buf) override;
168 size_t getSize() const override { return headerSize + hashSize; }
169 void writeBuildId(llvm::ArrayRef<uint8_t> buf);
175 // BssSection is used to reserve space for copy relocations and common symbols.
176 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
177 // that are used for writable symbols, read-only symbols and common symbols,
179 class BssSection final : public SyntheticSection {
181 BssSection(StringRef name, uint64_t size, uint32_t alignment);
182 void writeTo(uint8_t *) override {
183 llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
185 bool isNeeded() const override { return size != 0; }
186 size_t getSize() const override { return size; }
188 static bool classof(const SectionBase *s) { return s->bss; }
192 class MipsGotSection final : public SyntheticSection {
195 void writeTo(uint8_t *buf) override;
196 size_t getSize() const override { return size; }
197 bool updateAllocSize() override;
198 void finalizeContents() override;
199 bool isNeeded() const override;
201 // Join separate GOTs built for each input file to generate
202 // primary and optional multiple secondary GOTs.
205 void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
206 void addDynTlsEntry(InputFile &file, Symbol &sym);
207 void addTlsIndex(InputFile &file);
209 uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
210 int64_t addend) const;
211 uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
212 int64_t addend) const;
213 uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
214 uint64_t getTlsIndexOffset(const InputFile *f) const;
216 // Returns the symbol which corresponds to the first entry of the global part
217 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
219 // Returns nullptr if the global part is empty.
220 const Symbol *getFirstGlobalEntry() const;
222 // Returns the number of entries in the local part of GOT including
223 // the number of reserved entries.
224 unsigned getLocalEntriesNum() const;
226 // Return _gp value for primary GOT (nullptr) or particular input file.
227 uint64_t getGp(const InputFile *f = nullptr) const;
230 // MIPS GOT consists of three parts: local, global and tls. Each part
231 // contains different types of entries. Here is a layout of GOT:
232 // - Header entries |
233 // - Page entries | Local part
234 // - Local entries (16-bit access) |
235 // - Local entries (32-bit access) |
236 // - Normal global entries || Global part
237 // - Reloc-only global entries ||
238 // - TLS entries ||| TLS part
241 // Two entries hold predefined value 0x0 and 0x80000000.
243 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
244 // relocation against local symbols. They are initialized by higher 16-bit
245 // of the corresponding symbol's value. So each 64kb of address space
246 // requires a single GOT entry.
247 // Local entries (16-bit access):
248 // These entries created by GOT relocations against global non-preemptible
249 // symbols so dynamic linker is not necessary to resolve the symbol's
250 // values. "16-bit access" means that corresponding relocations address
251 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own
253 // Local entries (32-bit access):
254 // These entries are the same as above but created by relocations which
255 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
256 // Normal global entries:
257 // These entries created by GOT relocations against preemptible global
258 // symbols. They need to be initialized by dynamic linker and they ordered
259 // exactly as the corresponding entries in the dynamic symbols table.
260 // Reloc-only global entries:
261 // These entries created for symbols that are referenced by dynamic
262 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative
263 // addressing, but MIPS ABI requires that these entries be present in GOT.
265 // Entries created by TLS relocations.
267 // If the sum of local, global and tls entries is less than 64K only single
268 // got is enough. Otherwise, multi-got is created. Series of primary and
269 // multiple secondary GOTs have the following layout:
274 // Relocation only entries
283 // All GOT entries required by relocations from a single input file entirely
284 // belong to either primary or one of secondary GOTs. To reference GOT entries
285 // each GOT has its own _gp value points to the "middle" of the GOT.
286 // In the code this value loaded to the register which is used for GOT access.
288 // MIPS 32 function's prologue:
290 // 0: R_MIPS_HI16 _gp_disp
292 // 4: R_MIPS_LO16 _gp_disp
296 // 14: R_MIPS_GPREL16 main
298 // Dynamic linker does not know anything about secondary GOTs and cannot
299 // use a regular MIPS mechanism for GOT entries initialization. So we have
300 // to use an approach accepted by other architectures and create dynamic
301 // relocations R_MIPS_REL32 to initialize global entries (and local in case
302 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
303 // requires GOT entries and correspondingly ordered dynamic symbol table
304 // entries to deal with dynamic relocations. To handle this problem
305 // relocation-only section in the primary GOT contains entries for all
306 // symbols referenced in global parts of secondary GOTs. Although the sum
307 // of local and normal global entries of the primary got should be less
308 // than 64K, the size of the primary got (including relocation-only entries
309 // can be greater than 64K, because parts of the primary got that overflow
310 // the 64K limit are used only by the dynamic linker at dynamic link-time
311 // and not by 16-bit gp-relative addressing at run-time.
313 // For complete multi-GOT description see the following link
314 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
316 // Number of "Header" entries.
317 static const unsigned headerEntriesNum = 2;
321 // Symbol and addend.
322 using GotEntry = std::pair<Symbol *, int64_t>;
325 InputFile *file = nullptr;
326 size_t startIndex = 0;
331 PageBlock() : firstIndex(0), count(0) {}
334 // Map output sections referenced by MIPS GOT relocations
335 // to the description (index/count) "page" entries allocated
337 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
338 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
339 llvm::MapVector<GotEntry, size_t> local16;
340 llvm::MapVector<GotEntry, size_t> local32;
341 llvm::MapVector<Symbol *, size_t> global;
342 llvm::MapVector<Symbol *, size_t> relocs;
343 llvm::MapVector<Symbol *, size_t> tls;
344 // Set of symbols referenced by dynamic TLS relocations.
345 llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
347 // Total number of all entries.
348 size_t getEntriesNum() const;
349 // Number of "page" entries.
350 size_t getPageEntriesNum() const;
351 // Number of entries require 16-bit index to access.
352 size_t getIndexedEntriesNum() const;
355 // Container of GOT created for each input file.
356 // After building a final series of GOTs this container
357 // holds primary and secondary GOT's.
358 std::vector<FileGot> gots;
360 // Return (and create if necessary) `FileGot`.
361 FileGot &getGot(InputFile &f);
363 // Try to merge two GOTs. In case of success the `Dst` contains
364 // result of merging and the function returns true. In case of
365 // ovwerflow the `Dst` is unchanged and the function returns false.
366 bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
369 class GotPltSection final : public SyntheticSection {
372 void addEntry(Symbol &sym);
373 size_t getSize() const override;
374 void writeTo(uint8_t *buf) override;
375 bool isNeeded() const override;
377 // Flag to force GotPlt to be in output if we have relocations
378 // that relies on its address.
379 bool hasGotPltOffRel = false;
382 std::vector<const Symbol *> entries;
385 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
386 // Symbols that will be relocated by Target->IRelativeRel.
387 // On most Targets the IgotPltSection will immediately follow the GotPltSection
388 // on ARM the IgotPltSection will immediately follow the GotSection.
389 class IgotPltSection final : public SyntheticSection {
392 void addEntry(Symbol &sym);
393 size_t getSize() const override;
394 void writeTo(uint8_t *buf) override;
395 bool isNeeded() const override { return !entries.empty(); }
398 std::vector<const Symbol *> entries;
401 class StringTableSection final : public SyntheticSection {
403 StringTableSection(StringRef name, bool dynamic);
404 unsigned addString(StringRef s, bool hashIt = true);
405 void writeTo(uint8_t *buf) override;
406 size_t getSize() const override { return size; }
407 bool isDynamic() const { return dynamic; }
414 llvm::DenseMap<StringRef, unsigned> stringMap;
415 std::vector<StringRef> strings;
420 DynamicReloc(RelType type, const InputSectionBase *inputSec,
421 uint64_t offsetInSec, bool useSymVA, Symbol *sym, int64_t addend)
422 : type(type), sym(sym), inputSec(inputSec), offsetInSec(offsetInSec),
423 useSymVA(useSymVA), addend(addend), outputSec(nullptr) {}
424 // This constructor records dynamic relocation settings used by MIPS
425 // multi-GOT implementation. It's to relocate addresses of 64kb pages
426 // lie inside the output section.
427 DynamicReloc(RelType type, const InputSectionBase *inputSec,
428 uint64_t offsetInSec, const OutputSection *outputSec,
430 : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec),
431 useSymVA(false), addend(addend), outputSec(outputSec) {}
433 uint64_t getOffset() const;
434 uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
436 // Computes the addend of the dynamic relocation. Note that this is not the
437 // same as the addend member variable as it also includes the symbol address
438 // if useSymVA is true.
439 int64_t computeAddend() const;
444 const InputSectionBase *inputSec = nullptr;
445 uint64_t offsetInSec;
446 // If this member is true, the dynamic relocation will not be against the
447 // symbol but will instead be a relative relocation that simply adds the
448 // load address. This means we need to write the symbol virtual address
449 // plus the original addend as the final relocation addend.
452 const OutputSection *outputSec;
455 template <class ELFT> class DynamicSection final : public SyntheticSection {
456 using Elf_Dyn = typename ELFT::Dyn;
457 using Elf_Rel = typename ELFT::Rel;
458 using Elf_Rela = typename ELFT::Rela;
459 using Elf_Relr = typename ELFT::Relr;
460 using Elf_Shdr = typename ELFT::Shdr;
461 using Elf_Sym = typename ELFT::Sym;
463 // finalizeContents() fills this vector with the section contents.
464 std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries;
468 void finalizeContents() override;
469 void writeTo(uint8_t *buf) override;
470 size_t getSize() const override { return size; }
473 void add(int32_t tag, std::function<uint64_t()> fn);
474 void addInt(int32_t tag, uint64_t val);
475 void addInSec(int32_t tag, InputSection *sec);
476 void addInSecRelative(int32_t tag, InputSection *sec);
477 void addOutSec(int32_t tag, OutputSection *sec);
478 void addSize(int32_t tag, OutputSection *sec);
479 void addSym(int32_t tag, Symbol *sym);
484 class RelocationBaseSection : public SyntheticSection {
486 RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag,
487 int32_t sizeDynamicTag);
488 void addReloc(RelType dynType, InputSectionBase *isec, uint64_t offsetInSec,
490 // Add a dynamic relocation that might need an addend. This takes care of
491 // writing the addend to the output section if needed.
492 void addReloc(RelType dynType, InputSectionBase *inputSec,
493 uint64_t offsetInSec, Symbol *sym, int64_t addend, RelExpr expr,
495 void addReloc(const DynamicReloc &reloc);
496 bool isNeeded() const override { return !relocs.empty(); }
497 size_t getSize() const override { return relocs.size() * this->entsize; }
498 size_t getRelativeRelocCount() const { return numRelativeRelocs; }
499 void finalizeContents() override;
500 int32_t dynamicTag, sizeDynamicTag;
501 std::vector<DynamicReloc> relocs;
504 size_t numRelativeRelocs = 0;
507 template <class ELFT>
508 class RelocationSection final : public RelocationBaseSection {
509 using Elf_Rel = typename ELFT::Rel;
510 using Elf_Rela = typename ELFT::Rela;
513 RelocationSection(StringRef name, bool sort);
514 void writeTo(uint8_t *buf) override;
520 template <class ELFT>
521 class AndroidPackedRelocationSection final : public RelocationBaseSection {
522 using Elf_Rel = typename ELFT::Rel;
523 using Elf_Rela = typename ELFT::Rela;
526 AndroidPackedRelocationSection(StringRef name);
528 bool updateAllocSize() override;
529 size_t getSize() const override { return relocData.size(); }
530 void writeTo(uint8_t *buf) override {
531 memcpy(buf, relocData.data(), relocData.size());
535 SmallVector<char, 0> relocData;
538 struct RelativeReloc {
539 uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
541 const InputSectionBase *inputSec;
542 uint64_t offsetInSec;
545 class RelrBaseSection : public SyntheticSection {
548 bool isNeeded() const override { return !relocs.empty(); }
549 std::vector<RelativeReloc> relocs;
552 // RelrSection is used to encode offsets for relative relocations.
553 // Proposal for adding SHT_RELR sections to generic-abi is here:
554 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
555 // For more details, see the comment in RelrSection::updateAllocSize().
556 template <class ELFT> class RelrSection final : public RelrBaseSection {
557 using Elf_Relr = typename ELFT::Relr;
562 bool updateAllocSize() override;
563 size_t getSize() const override { return relrRelocs.size() * this->entsize; }
564 void writeTo(uint8_t *buf) override {
565 memcpy(buf, relrRelocs.data(), getSize());
569 std::vector<Elf_Relr> relrRelocs;
572 struct SymbolTableEntry {
577 class SymbolTableBaseSection : public SyntheticSection {
579 SymbolTableBaseSection(StringTableSection &strTabSec);
580 void finalizeContents() override;
581 size_t getSize() const override { return getNumSymbols() * entsize; }
582 void addSymbol(Symbol *sym);
583 unsigned getNumSymbols() const { return symbols.size() + 1; }
584 size_t getSymbolIndex(Symbol *sym);
585 ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
588 void sortSymTabSymbols();
590 // A vector of symbols and their string table offsets.
591 std::vector<SymbolTableEntry> symbols;
593 StringTableSection &strTabSec;
595 llvm::once_flag onceFlag;
596 llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
597 llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
600 template <class ELFT>
601 class SymbolTableSection final : public SymbolTableBaseSection {
602 using Elf_Sym = typename ELFT::Sym;
605 SymbolTableSection(StringTableSection &strTabSec);
606 void writeTo(uint8_t *buf) override;
609 class SymtabShndxSection final : public SyntheticSection {
611 SymtabShndxSection();
613 void writeTo(uint8_t *buf) override;
614 size_t getSize() const override;
615 bool isNeeded() const override;
616 void finalizeContents() override;
619 // Outputs GNU Hash section. For detailed explanation see:
620 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
621 class GnuHashTableSection final : public SyntheticSection {
623 GnuHashTableSection();
624 void finalizeContents() override;
625 void writeTo(uint8_t *buf) override;
626 size_t getSize() const override { return size; }
628 // Adds symbols to the hash table.
629 // Sorts the input to satisfy GNU hash section requirements.
630 void addSymbols(std::vector<SymbolTableEntry> &symbols);
633 // See the comment in writeBloomFilter.
634 enum { Shift2 = 26 };
636 void writeBloomFilter(uint8_t *buf);
637 void writeHashTable(uint8_t *buf);
646 std::vector<Entry> symbols;
652 class HashTableSection final : public SyntheticSection {
655 void finalizeContents() override;
656 void writeTo(uint8_t *buf) override;
657 size_t getSize() const override { return size; }
663 // The PltSection is used for both the Plt and Iplt. The former usually has a
664 // header as its first entry that is used at run-time to resolve lazy binding.
665 // The latter is used for GNU Ifunc symbols, that will be subject to a
666 // Target->IRelativeRel.
667 class PltSection : public SyntheticSection {
669 PltSection(bool isIplt);
670 void writeTo(uint8_t *buf) override;
671 size_t getSize() const override;
672 bool isNeeded() const override { return !entries.empty(); }
674 template <class ELFT> void addEntry(Symbol &sym);
679 std::vector<const Symbol *> entries;
683 class GdbIndexSection final : public SyntheticSection {
685 struct AddressEntry {
686 InputSection *section;
688 uint64_t highAddress;
697 struct NameAttrEntry {
698 llvm::CachedHashStringRef name;
699 uint32_t cuIndexAndAttrs;
704 std::vector<AddressEntry> addressAreas;
705 std::vector<CuEntry> compilationUnits;
709 llvm::CachedHashStringRef name;
710 std::vector<uint32_t> cuVector;
712 uint32_t cuVectorOff;
716 template <typename ELFT> static GdbIndexSection *create();
717 void writeTo(uint8_t *buf) override;
718 size_t getSize() const override { return size; }
719 bool isNeeded() const override;
722 struct GdbIndexHeader {
723 llvm::support::ulittle32_t version;
724 llvm::support::ulittle32_t cuListOff;
725 llvm::support::ulittle32_t cuTypesOff;
726 llvm::support::ulittle32_t addressAreaOff;
727 llvm::support::ulittle32_t symtabOff;
728 llvm::support::ulittle32_t constantPoolOff;
731 void initOutputSize();
732 size_t computeSymtabSize() const;
734 // Each chunk contains information gathered from debug sections of a
735 // single object file.
736 std::vector<GdbChunk> chunks;
738 // A symbol table for this .gdb_index section.
739 std::vector<GdbSymbol> symbols;
744 // --eh-frame-hdr option tells linker to construct a header for all the
745 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
746 // and also to a PT_GNU_EH_FRAME segment.
747 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
748 // calling dl_iterate_phdr.
749 // This section contains a lookup table for quick binary search of FDEs.
750 // Detailed info about internals can be found in Ian Lance Taylor's blog:
751 // http://www.airs.com/blog/archives/460 (".eh_frame")
752 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
753 class EhFrameHeader final : public SyntheticSection {
757 void writeTo(uint8_t *buf) override;
758 size_t getSize() const override;
759 bool isNeeded() const override;
762 // For more information about .gnu.version and .gnu.version_r see:
763 // https://www.akkadia.org/drepper/symbol-versioning
765 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
766 // contain symbol version definitions. The number of entries in this section
767 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
768 // The section shall contain an array of Elf_Verdef structures, optionally
769 // followed by an array of Elf_Verdaux structures.
770 class VersionDefinitionSection final : public SyntheticSection {
772 VersionDefinitionSection();
773 void finalizeContents() override;
774 size_t getSize() const override;
775 void writeTo(uint8_t *buf) override;
778 enum { EntrySize = 28 };
779 void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
780 StringRef getFileDefName();
782 unsigned fileDefNameOff;
783 std::vector<unsigned> verDefNameOffs;
786 // The .gnu.version section specifies the required version of each symbol in the
787 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
788 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
789 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
790 // The values 0 and 1 are reserved. All other values are used for versions in
791 // the own object or in any of the dependencies.
792 class VersionTableSection final : public SyntheticSection {
794 VersionTableSection();
795 void finalizeContents() override;
796 size_t getSize() const override;
797 void writeTo(uint8_t *buf) override;
798 bool isNeeded() const override;
801 // The .gnu.version_r section defines the version identifiers used by
802 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
803 // Elf_Verneed specifies the version requirements for a single DSO, and contains
804 // a reference to a linked list of Elf_Vernaux data structures which define the
805 // mapping from version identifiers to version names.
806 template <class ELFT>
807 class VersionNeedSection final : public SyntheticSection {
808 using Elf_Verneed = typename ELFT::Verneed;
809 using Elf_Vernaux = typename ELFT::Vernaux;
813 uint32_t verneedIndex;
819 std::vector<Vernaux> vernauxs;
822 std::vector<Verneed> verneeds;
825 VersionNeedSection();
826 void finalizeContents() override;
827 void writeTo(uint8_t *buf) override;
828 size_t getSize() const override;
829 bool isNeeded() const override;
832 // MergeSyntheticSection is a class that allows us to put mergeable sections
833 // with different attributes in a single output sections. To do that
834 // we put them into MergeSyntheticSection synthetic input sections which are
835 // attached to regular output sections.
836 class MergeSyntheticSection : public SyntheticSection {
838 void addSection(MergeInputSection *ms);
839 std::vector<MergeInputSection *> sections;
842 MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
844 : SyntheticSection(flags, type, alignment, name) {}
847 class MergeTailSection final : public MergeSyntheticSection {
849 MergeTailSection(StringRef name, uint32_t type, uint64_t flags,
852 size_t getSize() const override;
853 void writeTo(uint8_t *buf) override;
854 void finalizeContents() override;
857 llvm::StringTableBuilder builder;
860 class MergeNoTailSection final : public MergeSyntheticSection {
862 MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags,
864 : MergeSyntheticSection(name, type, flags, alignment) {}
866 size_t getSize() const override { return size; }
867 void writeTo(uint8_t *buf) override;
868 void finalizeContents() override;
871 // We use the most significant bits of a hash as a shard ID.
872 // The reason why we don't want to use the least significant bits is
873 // because DenseMap also uses lower bits to determine a bucket ID.
874 // If we use lower bits, it significantly increases the probability of
876 size_t getShardId(uint32_t hash) {
877 assert((hash >> 31) == 0);
878 return hash >> (31 - llvm::countTrailingZeros(numShards));
884 // String table contents
885 constexpr static size_t numShards = 32;
886 std::vector<llvm::StringTableBuilder> shards;
887 size_t shardOffsets[numShards];
890 // .MIPS.abiflags section.
891 template <class ELFT>
892 class MipsAbiFlagsSection final : public SyntheticSection {
893 using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
896 static MipsAbiFlagsSection *create();
898 MipsAbiFlagsSection(Elf_Mips_ABIFlags flags);
899 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
900 void writeTo(uint8_t *buf) override;
903 Elf_Mips_ABIFlags flags;
906 // .MIPS.options section.
907 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
908 using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
909 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
912 static MipsOptionsSection *create();
914 MipsOptionsSection(Elf_Mips_RegInfo reginfo);
915 void writeTo(uint8_t *buf) override;
917 size_t getSize() const override {
918 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
922 Elf_Mips_RegInfo reginfo;
925 // MIPS .reginfo section.
926 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
927 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
930 static MipsReginfoSection *create();
932 MipsReginfoSection(Elf_Mips_RegInfo reginfo);
933 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
934 void writeTo(uint8_t *buf) override;
937 Elf_Mips_RegInfo reginfo;
940 // This is a MIPS specific section to hold a space within the data segment
941 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
942 // See "Dynamic section" in Chapter 5 in the following document:
943 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
944 class MipsRldMapSection : public SyntheticSection {
947 size_t getSize() const override { return config->wordsize; }
948 void writeTo(uint8_t *buf) override {}
951 // Representation of the combined .ARM.Exidx input sections. We process these
952 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries
953 // and add terminating sentinel entries.
955 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
956 // a table that the unwinder can derive (Addresses are encoded as offsets from
958 // | Address of function | Unwind instructions for function |
959 // where the unwind instructions are either a small number of unwind or the
960 // special EXIDX_CANTUNWIND entry representing no unwinding information.
961 // When an exception is thrown from an address A, the unwinder searches the
962 // table for the closest table entry with Address of function <= A. This means
963 // that for two consecutive table entries:
966 // The range of addresses described by U1 is [A1, A2)
968 // There are two cases where we need a linker generated table entry to fixup
969 // the address ranges in the table
971 // - A sentinel entry added with an address higher than all
972 // executable sections. This was needed to work around libunwind bug pr31091.
973 // - After address assignment we need to find the highest addressed executable
974 // section and use the limit of that section so that the unwinder never
977 // - InputSections without a .ARM.exidx section (usually from Assembly)
978 // need a table entry so that they terminate the range of the previously
979 // function. This is pr40277.
981 // Instead of storing pointers to the .ARM.exidx InputSections from
982 // InputObjects, we store pointers to the executable sections that need
983 // .ARM.exidx sections. We can then use the dependentSections of these to
984 // either find the .ARM.exidx section or know that we need to generate one.
985 class ARMExidxSyntheticSection : public SyntheticSection {
987 ARMExidxSyntheticSection();
989 // Add an input section to the ARMExidxSyntheticSection. Returns whether the
990 // section needs to be removed from the main input section list.
991 bool addSection(InputSection *isec);
993 size_t getSize() const override { return size; }
994 void writeTo(uint8_t *buf) override;
995 bool isNeeded() const override { return !empty; }
996 // Sort and remove duplicate entries.
997 void finalizeContents() override;
998 InputSection *getLinkOrderDep() const;
1000 static bool classof(const SectionBase *d);
1002 // Links to the ARMExidxSections so we can transfer the relocations once the
1004 std::vector<InputSection *> exidxSections;
1009 // Empty if ExecutableSections contains no dependent .ARM.exidx sections.
1012 // Instead of storing pointers to the .ARM.exidx InputSections from
1013 // InputObjects, we store pointers to the executable sections that need
1014 // .ARM.exidx sections. We can then use the dependentSections of these to
1015 // either find the .ARM.exidx section or know that we need to generate one.
1016 std::vector<InputSection *> executableSections;
1018 // The executable InputSection with the highest address to use for the
1019 // sentinel. We store separately from ExecutableSections as merging of
1020 // duplicate entries may mean this InputSection is removed from
1021 // ExecutableSections.
1022 InputSection *sentinel = nullptr;
1025 // A container for one or more linker generated thunks. Instances of these
1026 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1027 class ThunkSection : public SyntheticSection {
1029 // ThunkSection in OS, with desired outSecOff of Off
1030 ThunkSection(OutputSection *os, uint64_t off);
1032 // Add a newly created Thunk to this container:
1033 // Thunk is given offset from start of this InputSection
1034 // Thunk defines a symbol in this InputSection that can be used as target
1036 void addThunk(Thunk *t);
1037 size_t getSize() const override { return size; }
1038 void writeTo(uint8_t *buf) override;
1039 InputSection *getTargetInputSection() const;
1040 bool assignOffsets();
1043 std::vector<Thunk *> thunks;
1047 // Used to compute outSecOff of .got2 in each object file. This is needed to
1048 // synthesize PLT entries for PPC32 Secure PLT ABI.
1049 class PPC32Got2Section final : public SyntheticSection {
1052 size_t getSize() const override { return 0; }
1053 bool isNeeded() const override;
1054 void finalizeContents() override;
1055 void writeTo(uint8_t *buf) override {}
1058 // This section is used to store the addresses of functions that are called
1059 // in range-extending thunks on PowerPC64. When producing position dependant
1060 // code the addresses are link-time constants and the table is written out to
1061 // the binary. When producing position-dependant code the table is allocated and
1062 // filled in by the dynamic linker.
1063 class PPC64LongBranchTargetSection final : public SyntheticSection {
1065 PPC64LongBranchTargetSection();
1066 void addEntry(Symbol &sym);
1067 size_t getSize() const override;
1068 void writeTo(uint8_t *buf) override;
1069 bool isNeeded() const override;
1070 void finalizeContents() override { finalized = true; }
1073 std::vector<const Symbol *> entries;
1074 bool finalized = false;
1077 template <typename ELFT>
1078 class PartitionElfHeaderSection : public SyntheticSection {
1080 PartitionElfHeaderSection();
1081 size_t getSize() const override;
1082 void writeTo(uint8_t *buf) override;
1085 template <typename ELFT>
1086 class PartitionProgramHeadersSection : public SyntheticSection {
1088 PartitionProgramHeadersSection();
1089 size_t getSize() const override;
1090 void writeTo(uint8_t *buf) override;
1093 class PartitionIndexSection : public SyntheticSection {
1095 PartitionIndexSection();
1096 size_t getSize() const override;
1097 void finalizeContents() override;
1098 void writeTo(uint8_t *buf) override;
1101 // Create a dummy .sdata for __global_pointer$ if .sdata does not exist.
1102 class RISCVSdataSection final : public SyntheticSection {
1104 RISCVSdataSection();
1105 size_t getSize() const override { return 0; }
1106 bool isNeeded() const override;
1107 void writeTo(uint8_t *buf) override {}
1110 InputSection *createInterpSection();
1111 MergeInputSection *createCommentSection();
1112 template <class ELFT> void splitSections();
1113 void mergeSections();
1115 template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
1116 template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
1118 Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
1119 uint64_t size, InputSectionBase §ion);
1121 void addVerneed(Symbol *ss);
1123 // Linker generated per-partition sections.
1126 uint64_t nameStrTab;
1128 SyntheticSection *elfHeader;
1129 SyntheticSection *programHeaders;
1130 std::vector<PhdrEntry *> phdrs;
1132 ARMExidxSyntheticSection *armExidx;
1133 BuildIdSection *buildId;
1134 SyntheticSection *dynamic;
1135 StringTableSection *dynStrTab;
1136 SymbolTableBaseSection *dynSymTab;
1137 EhFrameHeader *ehFrameHdr;
1138 EhFrameSection *ehFrame;
1139 GnuHashTableSection *gnuHashTab;
1140 HashTableSection *hashTab;
1141 RelocationBaseSection *relaDyn;
1142 RelrBaseSection *relrDyn;
1143 VersionDefinitionSection *verDef;
1144 SyntheticSection *verNeed;
1145 VersionTableSection *verSym;
1147 unsigned getNumber() const { return this - &partitions[0] + 1; }
1150 extern Partition *mainPart;
1152 inline Partition &SectionBase::getPartition() const {
1154 return partitions[partition - 1];
1157 // Linker generated sections which can be used as inputs and are not specific to
1160 InputSection *armAttributes;
1162 BssSection *bssRelRo;
1164 GotPltSection *gotPlt;
1165 IgotPltSection *igotPlt;
1166 PPC64LongBranchTargetSection *ppc64LongBranchTarget;
1167 MipsGotSection *mipsGot;
1168 MipsRldMapSection *mipsRldMap;
1169 SyntheticSection *partEnd;
1170 SyntheticSection *partIndex;
1173 PPC32Got2Section *ppc32Got2;
1174 RISCVSdataSection *riscvSdata;
1175 RelocationBaseSection *relaPlt;
1176 RelocationBaseSection *relaIplt;
1177 StringTableSection *shStrTab;
1178 StringTableSection *strTab;
1179 SymbolTableBaseSection *symTab;
1180 SymtabShndxSection *symTabShndx;