1 //===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Synthetic sections represent chunks of linker-created data. If you
11 // need to create a chunk of data that to be included in some section
12 // in the result, you probably want to create that as a synthetic section.
14 // Synthetic sections are designed as input sections as opposed to
15 // output sections because we want to allow them to be manipulated
16 // using linker scripts just like other input sections from regular
19 //===----------------------------------------------------------------------===//
21 #ifndef LLD_ELF_SYNTHETIC_SECTION_H
22 #define LLD_ELF_SYNTHETIC_SECTION_H
26 #include "InputSection.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/MC/StringTableBuilder.h"
29 #include "llvm/Support/Endian.h"
37 class SyntheticSection : public InputSection {
39 SyntheticSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
41 : InputSection(nullptr, Flags, Type, Alignment, {}, Name,
42 InputSectionBase::Synthetic) {
46 virtual ~SyntheticSection() = default;
47 virtual void writeTo(uint8_t *Buf) = 0;
48 virtual size_t getSize() const = 0;
49 virtual void finalizeContents() {}
50 // If the section has the SHF_ALLOC flag and the size may be changed if
51 // thunks are added, update the section size.
52 virtual bool updateAllocSize() { return false; }
53 virtual bool empty() const { return false; }
55 static bool classof(const SectionBase *D) {
56 return D->kind() == InputSectionBase::Synthetic;
61 EhSectionPiece *Cie = nullptr;
62 std::vector<EhSectionPiece *> Fdes;
65 // Section for .eh_frame.
66 class EhFrameSection final : public SyntheticSection {
69 void writeTo(uint8_t *Buf) override;
70 void finalizeContents() override;
71 bool empty() const override { return Sections.empty(); }
72 size_t getSize() const override { return Size; }
74 template <class ELFT> void addSection(InputSectionBase *S);
76 std::vector<EhInputSection *> Sections;
84 std::vector<FdeData> getFdeData() const;
85 ArrayRef<CieRecord *> getCieRecords() const { return CieRecords; }
88 // This is used only when parsing EhInputSection. We keep it here to avoid
89 // allocating one for each EhInputSection.
90 llvm::DenseMap<size_t, CieRecord *> OffsetToCie;
94 template <class ELFT, class RelTy>
95 void addSectionAux(EhInputSection *S, llvm::ArrayRef<RelTy> Rels);
97 template <class ELFT, class RelTy>
98 CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
100 template <class ELFT, class RelTy>
101 bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
103 uint64_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc) const;
105 std::vector<CieRecord *> CieRecords;
107 // CIE records are uniquified by their contents and personality functions.
108 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> CieMap;
111 class GotSection : public SyntheticSection {
114 size_t getSize() const override { return Size; }
115 void finalizeContents() override;
116 bool empty() const override;
117 void writeTo(uint8_t *Buf) override;
119 void addEntry(Symbol &Sym);
120 bool addDynTlsEntry(Symbol &Sym);
122 uint64_t getGlobalDynAddr(const Symbol &B) const;
123 uint64_t getGlobalDynOffset(const Symbol &B) const;
125 uint64_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
126 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
128 // Flag to force GOT to be in output if we have relocations
129 // that relies on its address.
130 bool HasGotOffRel = false;
133 size_t NumEntries = 0;
134 uint32_t TlsIndexOff = -1;
138 // .note.GNU-stack section.
139 class GnuStackSection : public SyntheticSection {
142 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
143 void writeTo(uint8_t *Buf) override {}
144 size_t getSize() const override { return 0; }
147 // .note.gnu.build-id section.
148 class BuildIdSection : public SyntheticSection {
149 // First 16 bytes are a header.
150 static const unsigned HeaderSize = 16;
154 void writeTo(uint8_t *Buf) override;
155 size_t getSize() const override { return HeaderSize + HashSize; }
156 void writeBuildId(llvm::ArrayRef<uint8_t> Buf);
159 void computeHash(llvm::ArrayRef<uint8_t> Buf,
160 std::function<void(uint8_t *, ArrayRef<uint8_t>)> Hash);
166 // BssSection is used to reserve space for copy relocations and common symbols.
167 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
168 // that are used for writable symbols, read-only symbols and common symbols,
170 class BssSection final : public SyntheticSection {
172 BssSection(StringRef Name, uint64_t Size, uint32_t Alignment);
173 void writeTo(uint8_t *) override {
174 llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
176 bool empty() const override { return getSize() == 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 empty() const override;
192 // Join separate GOTs built for each input file to generate
193 // primary and optional multiple secondary GOTs.
194 template <class ELFT> void build();
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 typedef std::pair<Symbol *, int64_t> GotEntry;
316 InputFile *File = nullptr;
317 size_t StartIndex = 0;
320 size_t FirstIndex = 0;
324 // Map output sections referenced by MIPS GOT relocations
325 // to the description (index/count) "page" entries allocated
327 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> PagesMap;
328 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
329 llvm::MapVector<GotEntry, size_t> Local16;
330 llvm::MapVector<GotEntry, size_t> Local32;
331 llvm::MapVector<Symbol *, size_t> Global;
332 llvm::MapVector<Symbol *, size_t> Relocs;
333 llvm::MapVector<Symbol *, size_t> Tls;
334 // Set of symbols referenced by dynamic TLS relocations.
335 llvm::MapVector<Symbol *, size_t> DynTlsSymbols;
337 // Total number of all entries.
338 size_t getEntriesNum() const;
339 // Number of "page" entries.
340 size_t getPageEntriesNum() const;
341 // Number of entries require 16-bit index to access.
342 size_t getIndexedEntriesNum() const;
345 // Container of GOT created for each input file.
346 // After building a final series of GOTs this container
347 // holds primary and secondary GOT's.
348 std::vector<FileGot> Gots;
350 // Return (and create if necessary) `FileGot`.
351 FileGot &getGot(InputFile &F);
353 // Try to merge two GOTs. In case of success the `Dst` contains
354 // result of merging and the function returns true. In case of
355 // ovwerflow the `Dst` is unchanged and the function returns false.
356 bool tryMergeGots(FileGot & Dst, FileGot & Src, bool IsPrimary);
359 class GotPltSection final : public SyntheticSection {
362 void addEntry(Symbol &Sym);
363 size_t getSize() const override;
364 void writeTo(uint8_t *Buf) override;
365 bool empty() const override;
368 std::vector<const Symbol *> Entries;
371 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
372 // Symbols that will be relocated by Target->IRelativeRel.
373 // On most Targets the IgotPltSection will immediately follow the GotPltSection
374 // on ARM the IgotPltSection will immediately follow the GotSection.
375 class IgotPltSection final : public SyntheticSection {
378 void addEntry(Symbol &Sym);
379 size_t getSize() const override;
380 void writeTo(uint8_t *Buf) override;
381 bool empty() const override { return Entries.empty(); }
384 std::vector<const Symbol *> Entries;
387 class StringTableSection final : public SyntheticSection {
389 StringTableSection(StringRef Name, bool Dynamic);
390 unsigned addString(StringRef S, bool HashIt = true);
391 void writeTo(uint8_t *Buf) override;
392 size_t getSize() const override { return Size; }
393 bool isDynamic() const { return Dynamic; }
400 llvm::DenseMap<StringRef, unsigned> StringMap;
401 std::vector<StringRef> Strings;
406 DynamicReloc(RelType Type, const InputSectionBase *InputSec,
407 uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
408 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
409 UseSymVA(UseSymVA), Addend(Addend), OutputSec(nullptr) {}
410 // This constructor records dynamic relocation settings used by MIPS
411 // multi-GOT implementation. It's to relocate addresses of 64kb pages
412 // lie inside the output section.
413 DynamicReloc(RelType Type, const InputSectionBase *InputSec,
414 uint64_t OffsetInSec, const OutputSection *OutputSec,
416 : Type(Type), Sym(nullptr), InputSec(InputSec), OffsetInSec(OffsetInSec),
417 UseSymVA(false), Addend(Addend), OutputSec(OutputSec) {}
419 uint64_t getOffset() const;
420 uint32_t getSymIndex() const;
421 const InputSectionBase *getInputSec() const { return InputSec; }
423 // Computes the addend of the dynamic relocation. Note that this is not the
424 // same as the Addend member variable as it also includes the symbol address
425 // if UseSymVA is true.
426 int64_t computeAddend() const;
432 const InputSectionBase *InputSec = nullptr;
433 uint64_t OffsetInSec;
434 // If this member is true, the dynamic relocation will not be against the
435 // symbol but will instead be a relative relocation that simply adds the
436 // load address. This means we need to write the symbol virtual address
437 // plus the original addend as the final relocation addend.
440 const OutputSection *OutputSec;
443 template <class ELFT> class DynamicSection final : public SyntheticSection {
444 typedef typename ELFT::Dyn Elf_Dyn;
445 typedef typename ELFT::Rel Elf_Rel;
446 typedef typename ELFT::Rela Elf_Rela;
447 typedef typename ELFT::Relr Elf_Relr;
448 typedef typename ELFT::Shdr Elf_Shdr;
449 typedef typename ELFT::Sym Elf_Sym;
451 // finalizeContents() fills this vector with the section contents.
452 std::vector<std::pair<int32_t, std::function<uint64_t()>>> Entries;
456 void finalizeContents() override;
457 void writeTo(uint8_t *Buf) override;
458 size_t getSize() const override { return Size; }
461 void add(int32_t Tag, std::function<uint64_t()> Fn);
462 void addInt(int32_t Tag, uint64_t Val);
463 void addInSec(int32_t Tag, InputSection *Sec);
464 void addInSecRelative(int32_t Tag, InputSection *Sec);
465 void addOutSec(int32_t Tag, OutputSection *Sec);
466 void addSize(int32_t Tag, OutputSection *Sec);
467 void addSym(int32_t Tag, Symbol *Sym);
472 class RelocationBaseSection : public SyntheticSection {
474 RelocationBaseSection(StringRef Name, uint32_t Type, int32_t DynamicTag,
475 int32_t SizeDynamicTag);
476 void addReloc(RelType DynType, InputSectionBase *IS, uint64_t OffsetInSec,
478 // Add a dynamic relocation that might need an addend. This takes care of
479 // writing the addend to the output section if needed.
480 void addReloc(RelType DynType, InputSectionBase *InputSec,
481 uint64_t OffsetInSec, Symbol *Sym, int64_t Addend, RelExpr Expr,
483 void addReloc(const DynamicReloc &Reloc);
484 bool empty() const override { return Relocs.empty(); }
485 size_t getSize() const override { return Relocs.size() * this->Entsize; }
486 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
487 void finalizeContents() override;
488 int32_t DynamicTag, SizeDynamicTag;
491 std::vector<DynamicReloc> Relocs;
492 size_t NumRelativeRelocs = 0;
495 template <class ELFT>
496 class RelocationSection final : public RelocationBaseSection {
497 typedef typename ELFT::Rel Elf_Rel;
498 typedef typename ELFT::Rela Elf_Rela;
501 RelocationSection(StringRef Name, bool Sort);
502 unsigned getRelocOffset();
503 void writeTo(uint8_t *Buf) override;
509 template <class ELFT>
510 class AndroidPackedRelocationSection final : public RelocationBaseSection {
511 typedef typename ELFT::Rel Elf_Rel;
512 typedef typename ELFT::Rela Elf_Rela;
515 AndroidPackedRelocationSection(StringRef Name);
517 bool updateAllocSize() override;
518 size_t getSize() const override { return RelocData.size(); }
519 void writeTo(uint8_t *Buf) override {
520 memcpy(Buf, RelocData.data(), RelocData.size());
524 SmallVector<char, 0> RelocData;
527 struct RelativeReloc {
528 uint64_t getOffset() const { return InputSec->getVA(OffsetInSec); }
530 const InputSectionBase *InputSec;
531 uint64_t OffsetInSec;
534 class RelrBaseSection : public SyntheticSection {
537 bool empty() const override { return Relocs.empty(); }
538 std::vector<RelativeReloc> Relocs;
541 // RelrSection is used to encode offsets for relative relocations.
542 // Proposal for adding SHT_RELR sections to generic-abi is here:
543 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
544 // For more details, see the comment in RelrSection::updateAllocSize().
545 template <class ELFT> class RelrSection final : public RelrBaseSection {
546 typedef typename ELFT::Relr Elf_Relr;
551 bool updateAllocSize() override;
552 size_t getSize() const override { return RelrRelocs.size() * this->Entsize; }
553 void writeTo(uint8_t *Buf) override {
554 memcpy(Buf, RelrRelocs.data(), getSize());
558 std::vector<Elf_Relr> RelrRelocs;
561 struct SymbolTableEntry {
566 class SymbolTableBaseSection : public SyntheticSection {
568 SymbolTableBaseSection(StringTableSection &StrTabSec);
569 void finalizeContents() override;
570 size_t getSize() const override { return getNumSymbols() * Entsize; }
571 void addSymbol(Symbol *Sym);
572 unsigned getNumSymbols() const { return Symbols.size() + 1; }
573 size_t getSymbolIndex(Symbol *Sym);
574 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
577 void sortSymTabSymbols();
579 // A vector of symbols and their string table offsets.
580 std::vector<SymbolTableEntry> Symbols;
582 StringTableSection &StrTabSec;
584 llvm::once_flag OnceFlag;
585 llvm::DenseMap<Symbol *, size_t> SymbolIndexMap;
586 llvm::DenseMap<OutputSection *, size_t> SectionIndexMap;
589 template <class ELFT>
590 class SymbolTableSection final : public SymbolTableBaseSection {
591 typedef typename ELFT::Sym Elf_Sym;
594 SymbolTableSection(StringTableSection &StrTabSec);
595 void writeTo(uint8_t *Buf) override;
598 class SymtabShndxSection final : public SyntheticSection {
600 SymtabShndxSection();
602 void writeTo(uint8_t *Buf) override;
603 size_t getSize() const override;
604 bool empty() const override;
605 void finalizeContents() override;
608 // Outputs GNU Hash section. For detailed explanation see:
609 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
610 class GnuHashTableSection final : public SyntheticSection {
612 GnuHashTableSection();
613 void finalizeContents() override;
614 void writeTo(uint8_t *Buf) override;
615 size_t getSize() const override { return Size; }
617 // Adds symbols to the hash table.
618 // Sorts the input to satisfy GNU hash section requirements.
619 void addSymbols(std::vector<SymbolTableEntry> &Symbols);
622 // See the comment in writeBloomFilter.
623 enum { Shift2 = 26 };
625 void writeBloomFilter(uint8_t *Buf);
626 void writeHashTable(uint8_t *Buf);
635 std::vector<Entry> Symbols;
641 class HashTableSection final : public SyntheticSection {
644 void finalizeContents() override;
645 void writeTo(uint8_t *Buf) override;
646 size_t getSize() const override { return Size; }
652 // The PltSection is used for both the Plt and Iplt. The former usually has a
653 // header as its first entry that is used at run-time to resolve lazy binding.
654 // The latter is used for GNU Ifunc symbols, that will be subject to a
655 // Target->IRelativeRel.
656 class PltSection : public SyntheticSection {
658 PltSection(bool IsIplt);
659 void writeTo(uint8_t *Buf) override;
660 size_t getSize() const override;
661 bool empty() const override { return Entries.empty(); }
663 template <class ELFT> void addEntry(Symbol &Sym);
668 unsigned getPltRelocOff() const;
669 std::vector<std::pair<const Symbol *, unsigned>> Entries;
673 class GdbIndexSection final : public SyntheticSection {
675 struct AddressEntry {
676 InputSection *Section;
678 uint64_t HighAddress;
687 struct NameAttrEntry {
688 llvm::CachedHashStringRef Name;
689 uint32_t CuIndexAndAttrs;
694 std::vector<AddressEntry> AddressAreas;
695 std::vector<CuEntry> CompilationUnits;
699 llvm::CachedHashStringRef Name;
700 std::vector<uint32_t> CuVector;
702 uint32_t CuVectorOff;
706 template <typename ELFT> static GdbIndexSection *create();
707 void writeTo(uint8_t *Buf) override;
708 size_t getSize() const override { return Size; }
709 bool empty() const override;
712 struct GdbIndexHeader {
713 llvm::support::ulittle32_t Version;
714 llvm::support::ulittle32_t CuListOff;
715 llvm::support::ulittle32_t CuTypesOff;
716 llvm::support::ulittle32_t AddressAreaOff;
717 llvm::support::ulittle32_t SymtabOff;
718 llvm::support::ulittle32_t ConstantPoolOff;
721 void initOutputSize();
722 size_t computeSymtabSize() const;
724 // Each chunk contains information gathered from debug sections of a
725 // single object file.
726 std::vector<GdbChunk> Chunks;
728 // A symbol table for this .gdb_index section.
729 std::vector<GdbSymbol> Symbols;
734 // --eh-frame-hdr option tells linker to construct a header for all the
735 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
736 // and also to a PT_GNU_EH_FRAME segment.
737 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
738 // calling dl_iterate_phdr.
739 // This section contains a lookup table for quick binary search of FDEs.
740 // Detailed info about internals can be found in Ian Lance Taylor's blog:
741 // http://www.airs.com/blog/archives/460 (".eh_frame")
742 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
743 class EhFrameHeader final : public SyntheticSection {
746 void writeTo(uint8_t *Buf) override;
747 size_t getSize() const override;
748 bool empty() const override;
751 // For more information about .gnu.version and .gnu.version_r see:
752 // https://www.akkadia.org/drepper/symbol-versioning
754 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
755 // contain symbol version definitions. The number of entries in this section
756 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
757 // The section shall contain an array of Elf_Verdef structures, optionally
758 // followed by an array of Elf_Verdaux structures.
759 class VersionDefinitionSection final : public SyntheticSection {
761 VersionDefinitionSection();
762 void finalizeContents() override;
763 size_t getSize() const override;
764 void writeTo(uint8_t *Buf) override;
767 enum { EntrySize = 28 };
768 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
770 unsigned FileDefNameOff;
773 // The .gnu.version section specifies the required version of each symbol in the
774 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
775 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
776 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
777 // The values 0 and 1 are reserved. All other values are used for versions in
778 // the own object or in any of the dependencies.
779 template <class ELFT>
780 class VersionTableSection final : public SyntheticSection {
782 VersionTableSection();
783 void finalizeContents() override;
784 size_t getSize() const override;
785 void writeTo(uint8_t *Buf) override;
786 bool empty() const override;
789 // The .gnu.version_r section defines the version identifiers used by
790 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
791 // Elf_Verneed specifies the version requirements for a single DSO, and contains
792 // a reference to a linked list of Elf_Vernaux data structures which define the
793 // mapping from version identifiers to version names.
794 template <class ELFT> class VersionNeedSection final : public SyntheticSection {
795 typedef typename ELFT::Verneed Elf_Verneed;
796 typedef typename ELFT::Vernaux Elf_Vernaux;
798 // A vector of shared files that need Elf_Verneed data structures and the
799 // string table offsets of their sonames.
800 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
802 // The next available version identifier.
806 VersionNeedSection();
807 void addSymbol(Symbol *Sym);
808 void finalizeContents() override;
809 void writeTo(uint8_t *Buf) override;
810 size_t getSize() const override;
811 size_t getNeedNum() const { return Needed.size(); }
812 bool empty() const override;
815 // MergeSyntheticSection is a class that allows us to put mergeable sections
816 // with different attributes in a single output sections. To do that
817 // we put them into MergeSyntheticSection synthetic input sections which are
818 // attached to regular output sections.
819 class MergeSyntheticSection : public SyntheticSection {
821 void addSection(MergeInputSection *MS);
822 std::vector<MergeInputSection *> Sections;
825 MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
827 : SyntheticSection(Flags, Type, Alignment, Name) {}
830 class MergeTailSection final : public MergeSyntheticSection {
832 MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
835 size_t getSize() const override;
836 void writeTo(uint8_t *Buf) override;
837 void finalizeContents() override;
840 llvm::StringTableBuilder Builder;
843 class MergeNoTailSection final : public MergeSyntheticSection {
845 MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
847 : MergeSyntheticSection(Name, Type, Flags, Alignment) {}
849 size_t getSize() const override { return Size; }
850 void writeTo(uint8_t *Buf) override;
851 void finalizeContents() override;
854 // We use the most significant bits of a hash as a shard ID.
855 // The reason why we don't want to use the least significant bits is
856 // because DenseMap also uses lower bits to determine a bucket ID.
857 // If we use lower bits, it significantly increases the probability of
859 size_t getShardId(uint32_t Hash) {
860 return Hash >> (32 - llvm::countTrailingZeros(NumShards));
866 // String table contents
867 constexpr static size_t NumShards = 32;
868 std::vector<llvm::StringTableBuilder> Shards;
869 size_t ShardOffsets[NumShards];
872 // .MIPS.abiflags section.
873 template <class ELFT>
874 class MipsAbiFlagsSection final : public SyntheticSection {
875 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
878 static MipsAbiFlagsSection *create();
880 MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
881 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
882 void writeTo(uint8_t *Buf) override;
885 Elf_Mips_ABIFlags Flags;
888 // .MIPS.options section.
889 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
890 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
891 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
894 static MipsOptionsSection *create();
896 MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
897 void writeTo(uint8_t *Buf) override;
899 size_t getSize() const override {
900 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
904 Elf_Mips_RegInfo Reginfo;
907 // MIPS .reginfo section.
908 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
909 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
912 static MipsReginfoSection *create();
914 MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
915 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
916 void writeTo(uint8_t *Buf) override;
919 Elf_Mips_RegInfo Reginfo;
922 // This is a MIPS specific section to hold a space within the data segment
923 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
924 // See "Dynamic section" in Chapter 5 in the following document:
925 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
926 class MipsRldMapSection : public SyntheticSection {
929 size_t getSize() const override { return Config->Wordsize; }
930 void writeTo(uint8_t *Buf) override {}
933 class ARMExidxSentinelSection : public SyntheticSection {
935 ARMExidxSentinelSection();
936 size_t getSize() const override { return 8; }
937 void writeTo(uint8_t *Buf) override;
938 bool empty() const override;
940 static bool classof(const SectionBase *D);
942 // The last section referenced by a regular .ARM.exidx section.
943 // It is found and filled in Writer<ELFT>::resolveShfLinkOrder().
944 // The sentinel points at the end of that section.
945 InputSection *Highest = nullptr;
948 // A container for one or more linker generated thunks. Instances of these
949 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
950 class ThunkSection : public SyntheticSection {
952 // ThunkSection in OS, with desired OutSecOff of Off
953 ThunkSection(OutputSection *OS, uint64_t Off);
955 // Add a newly created Thunk to this container:
956 // Thunk is given offset from start of this InputSection
957 // Thunk defines a symbol in this InputSection that can be used as target
959 void addThunk(Thunk *T);
960 size_t getSize() const override { return Size; }
961 void writeTo(uint8_t *Buf) override;
962 InputSection *getTargetInputSection() const;
963 bool assignOffsets();
966 std::vector<Thunk *> Thunks;
970 // This section is used to store the addresses of functions that are called
971 // in range-extending thunks on PowerPC64. When producing position dependant
972 // code the addresses are link-time constants and the table is written out to
973 // the binary. When producing position-dependant code the table is allocated and
974 // filled in by the dynamic linker.
975 class PPC64LongBranchTargetSection final : public SyntheticSection {
977 PPC64LongBranchTargetSection();
978 void addEntry(Symbol &Sym);
979 size_t getSize() const override;
980 void writeTo(uint8_t *Buf) override;
981 bool empty() const override;
982 void finalizeContents() override { Finalized = true; }
985 std::vector<const Symbol *> Entries;
986 bool Finalized = false;
989 InputSection *createInterpSection();
990 MergeInputSection *createCommentSection();
991 template <class ELFT> void splitSections();
992 void mergeSections();
994 Defined *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
995 uint64_t Size, InputSectionBase &Section);
997 // Linker generated sections which can be used as inputs.
999 InputSection *ARMAttributes;
1001 BssSection *BssRelRo;
1002 BuildIdSection *BuildId;
1003 EhFrameHeader *EhFrameHdr;
1004 EhFrameSection *EhFrame;
1005 SyntheticSection *Dynamic;
1006 StringTableSection *DynStrTab;
1007 SymbolTableBaseSection *DynSymTab;
1008 GnuHashTableSection *GnuHashTab;
1009 HashTableSection *HashTab;
1010 InputSection *Interp;
1011 GdbIndexSection *GdbIndex;
1013 GotPltSection *GotPlt;
1014 IgotPltSection *IgotPlt;
1015 PPC64LongBranchTargetSection *PPC64LongBranchTarget;
1016 MipsGotSection *MipsGot;
1017 MipsRldMapSection *MipsRldMap;
1020 RelocationBaseSection *RelaDyn;
1021 RelrBaseSection *RelrDyn;
1022 RelocationBaseSection *RelaPlt;
1023 RelocationBaseSection *RelaIplt;
1024 StringTableSection *ShStrTab;
1025 StringTableSection *StrTab;
1026 SymbolTableBaseSection *SymTab;
1027 SymtabShndxSection *SymTabShndx;
1028 VersionDefinitionSection *VerDef;
1033 template <class ELFT> struct InX {
1034 static VersionTableSection<ELFT> *VerSym;
1035 static VersionNeedSection<ELFT> *VerNeed;
1038 template <class ELFT> VersionTableSection<ELFT> *InX<ELFT>::VerSym;
1039 template <class ELFT> VersionNeedSection<ELFT> *InX<ELFT>::VerNeed;