]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SyntheticSections.h
MFV r337818:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / SyntheticSections.h
1 //===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
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
17 // files.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLD_ELF_SYNTHETIC_SECTION_H
22 #define LLD_ELF_SYNTHETIC_SECTION_H
23
24 #include "EhFrame.h"
25 #include "GdbIndex.h"
26 #include "InputSection.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/MC/StringTableBuilder.h"
29 #include <functional>
30
31 namespace lld {
32 namespace elf {
33 class SharedSymbol;
34
35 class SyntheticSection : public InputSection {
36 public:
37   SyntheticSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
38                    StringRef Name)
39       : InputSection(nullptr, Flags, Type, Alignment, {}, Name,
40                      InputSectionBase::Synthetic) {
41     this->Live = true;
42   }
43
44   virtual ~SyntheticSection() = default;
45   virtual void writeTo(uint8_t *Buf) = 0;
46   virtual size_t getSize() const = 0;
47   virtual void finalizeContents() {}
48   // If the section has the SHF_ALLOC flag and the size may be changed if
49   // thunks are added, update the section size.
50   virtual bool updateAllocSize() { return false; }
51   // If any additional finalization of contents are needed post thunk creation.
52   virtual void postThunkContents() {}
53   virtual bool empty() const { return false; }
54   uint64_t getVA() const;
55
56   static bool classof(const SectionBase *D) {
57     return D->kind() == InputSectionBase::Synthetic;
58   }
59 };
60
61 struct CieRecord {
62   EhSectionPiece *Cie = nullptr;
63   std::vector<EhSectionPiece *> Fdes;
64 };
65
66 // Section for .eh_frame.
67 class EhFrameSection final : public SyntheticSection {
68 public:
69   EhFrameSection();
70   void writeTo(uint8_t *Buf) override;
71   void finalizeContents() override;
72   bool empty() const override { return Sections.empty(); }
73   size_t getSize() const override { return Size; }
74
75   template <class ELFT> void addSection(InputSectionBase *S);
76
77   std::vector<EhInputSection *> Sections;
78   size_t NumFdes = 0;
79
80   struct FdeData {
81     uint32_t Pc;
82     uint32_t FdeVA;
83   };
84
85   std::vector<FdeData> getFdeData() const;
86
87 private:
88   uint64_t Size = 0;
89
90   template <class ELFT, class RelTy>
91   void addSectionAux(EhInputSection *S, llvm::ArrayRef<RelTy> Rels);
92
93   template <class ELFT, class RelTy>
94   CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
95
96   template <class ELFT, class RelTy>
97   bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
98
99   uint64_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc) const;
100
101   std::vector<CieRecord *> CieRecords;
102
103   // CIE records are uniquified by their contents and personality functions.
104   llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> CieMap;
105 };
106
107 class GotSection : public SyntheticSection {
108 public:
109   GotSection();
110   size_t getSize() const override { return Size; }
111   void finalizeContents() override;
112   bool empty() const override;
113   void writeTo(uint8_t *Buf) override;
114
115   void addEntry(Symbol &Sym);
116   bool addDynTlsEntry(Symbol &Sym);
117   bool addTlsIndex();
118   uint64_t getGlobalDynAddr(const Symbol &B) const;
119   uint64_t getGlobalDynOffset(const Symbol &B) const;
120
121   uint64_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
122   uint32_t getTlsIndexOff() const { return TlsIndexOff; }
123
124   // Flag to force GOT to be in output if we have relocations
125   // that relies on its address.
126   bool HasGotOffRel = false;
127
128 protected:
129   size_t NumEntries = 0;
130   uint32_t TlsIndexOff = -1;
131   uint64_t Size = 0;
132 };
133
134 // .note.gnu.build-id section.
135 class BuildIdSection : public SyntheticSection {
136   // First 16 bytes are a header.
137   static const unsigned HeaderSize = 16;
138
139 public:
140   BuildIdSection();
141   void writeTo(uint8_t *Buf) override;
142   size_t getSize() const override { return HeaderSize + HashSize; }
143   void writeBuildId(llvm::ArrayRef<uint8_t> Buf);
144
145 private:
146   void computeHash(llvm::ArrayRef<uint8_t> Buf,
147                    std::function<void(uint8_t *, ArrayRef<uint8_t>)> Hash);
148
149   size_t HashSize;
150   uint8_t *HashBuf;
151 };
152
153 // BssSection is used to reserve space for copy relocations and common symbols.
154 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
155 // that are used for writable symbols, read-only symbols and common symbols,
156 // respectively.
157 class BssSection final : public SyntheticSection {
158 public:
159   BssSection(StringRef Name, uint64_t Size, uint32_t Alignment);
160   void writeTo(uint8_t *) override {}
161   bool empty() const override { return getSize() == 0; }
162   size_t getSize() const override { return Size; }
163
164   static bool classof(const SectionBase *S) { return S->Bss; }
165   uint64_t Size;
166 };
167
168 class MipsGotSection final : public SyntheticSection {
169 public:
170   MipsGotSection();
171   void writeTo(uint8_t *Buf) override;
172   size_t getSize() const override { return Size; }
173   bool updateAllocSize() override;
174   void finalizeContents() override;
175   bool empty() const override;
176   void addEntry(Symbol &Sym, int64_t Addend, RelExpr Expr);
177   bool addDynTlsEntry(Symbol &Sym);
178   bool addTlsIndex();
179   uint64_t getPageEntryOffset(const Symbol &B, int64_t Addend) const;
180   uint64_t getSymEntryOffset(const Symbol &B, int64_t Addend) const;
181   uint64_t getGlobalDynOffset(const Symbol &B) const;
182
183   // Returns the symbol which corresponds to the first entry of the global part
184   // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
185   // table properties.
186   // Returns nullptr if the global part is empty.
187   const Symbol *getFirstGlobalEntry() const;
188
189   // Returns the number of entries in the local part of GOT including
190   // the number of reserved entries.
191   unsigned getLocalEntriesNum() const;
192
193   // Returns offset of TLS part of the MIPS GOT table. This part goes
194   // after 'local' and 'global' entries.
195   uint64_t getTlsOffset() const;
196
197   uint32_t getTlsIndexOff() const { return TlsIndexOff; }
198
199   uint64_t getGp() const;
200
201 private:
202   // MIPS GOT consists of three parts: local, global and tls. Each part
203   // contains different types of entries. Here is a layout of GOT:
204   // - Header entries                |
205   // - Page entries                  |   Local part
206   // - Local entries (16-bit access) |
207   // - Local entries (32-bit access) |
208   // - Normal global entries         ||  Global part
209   // - Reloc-only global entries     ||
210   // - TLS entries                   ||| TLS part
211   //
212   // Header:
213   //   Two entries hold predefined value 0x0 and 0x80000000.
214   // Page entries:
215   //   These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
216   //   relocation against local symbols. They are initialized by higher 16-bit
217   //   of the corresponding symbol's value. So each 64kb of address space
218   //   requires a single GOT entry.
219   // Local entries (16-bit access):
220   //   These entries created by GOT relocations against global non-preemptible
221   //   symbols so dynamic linker is not necessary to resolve the symbol's
222   //   values. "16-bit access" means that corresponding relocations address
223   //   GOT using 16-bit index. Each unique Symbol-Addend pair has its own
224   //   GOT entry.
225   // Local entries (32-bit access):
226   //   These entries are the same as above but created by relocations which
227   //   address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
228   // Normal global entries:
229   //   These entries created by GOT relocations against preemptible global
230   //   symbols. They need to be initialized by dynamic linker and they ordered
231   //   exactly as the corresponding entries in the dynamic symbols table.
232   // Reloc-only global entries:
233   //   These entries created for symbols that are referenced by dynamic
234   //   relocations R_MIPS_REL32. These entries are not accessed with gp-relative
235   //   addressing, but MIPS ABI requires that these entries be present in GOT.
236   // TLS entries:
237   //   Entries created by TLS relocations.
238
239   // Number of "Header" entries.
240   static const unsigned HeaderEntriesNum = 2;
241   // Number of allocated "Page" entries.
242   uint32_t PageEntriesNum = 0;
243   // Map output sections referenced by MIPS GOT relocations
244   // to the first index of "Page" entries allocated for this section.
245   llvm::SmallMapVector<const OutputSection *, size_t, 16> PageIndexMap;
246
247   typedef std::pair<const Symbol *, uint64_t> GotEntry;
248   typedef std::vector<GotEntry> GotEntries;
249   // Map from Symbol-Addend pair to the GOT index.
250   llvm::DenseMap<GotEntry, size_t> EntryIndexMap;
251   // Local entries (16-bit access).
252   GotEntries LocalEntries;
253   // Local entries (32-bit access).
254   GotEntries LocalEntries32;
255
256   // Normal and reloc-only global entries.
257   GotEntries GlobalEntries;
258
259   // TLS entries.
260   std::vector<const Symbol *> TlsEntries;
261
262   uint32_t TlsIndexOff = -1;
263   uint64_t Size = 0;
264 };
265
266 class GotPltSection final : public SyntheticSection {
267 public:
268   GotPltSection();
269   void addEntry(Symbol &Sym);
270   size_t getSize() const override;
271   void writeTo(uint8_t *Buf) override;
272   bool empty() const override { return Entries.empty(); }
273
274 private:
275   std::vector<const Symbol *> Entries;
276 };
277
278 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
279 // Symbols that will be relocated by Target->IRelativeRel.
280 // On most Targets the IgotPltSection will immediately follow the GotPltSection
281 // on ARM the IgotPltSection will immediately follow the GotSection.
282 class IgotPltSection final : public SyntheticSection {
283 public:
284   IgotPltSection();
285   void addEntry(Symbol &Sym);
286   size_t getSize() const override;
287   void writeTo(uint8_t *Buf) override;
288   bool empty() const override { return Entries.empty(); }
289
290 private:
291   std::vector<const Symbol *> Entries;
292 };
293
294 class StringTableSection final : public SyntheticSection {
295 public:
296   StringTableSection(StringRef Name, bool Dynamic);
297   unsigned addString(StringRef S, bool HashIt = true);
298   void writeTo(uint8_t *Buf) override;
299   size_t getSize() const override { return Size; }
300   bool isDynamic() const { return Dynamic; }
301
302 private:
303   const bool Dynamic;
304
305   uint64_t Size = 0;
306
307   llvm::DenseMap<StringRef, unsigned> StringMap;
308   std::vector<StringRef> Strings;
309 };
310
311 class DynamicReloc {
312 public:
313   DynamicReloc(uint32_t Type, const InputSectionBase *InputSec,
314                uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
315       : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
316         UseSymVA(UseSymVA), Addend(Addend) {}
317
318   uint64_t getOffset() const;
319   int64_t getAddend() const;
320   uint32_t getSymIndex() const;
321   const InputSectionBase *getInputSec() const { return InputSec; }
322
323   uint32_t Type;
324
325 private:
326   Symbol *Sym;
327   const InputSectionBase *InputSec = nullptr;
328   uint64_t OffsetInSec;
329   bool UseSymVA;
330   int64_t Addend;
331 };
332
333 template <class ELFT> class DynamicSection final : public SyntheticSection {
334   typedef typename ELFT::Dyn Elf_Dyn;
335   typedef typename ELFT::Rel Elf_Rel;
336   typedef typename ELFT::Rela Elf_Rela;
337   typedef typename ELFT::Shdr Elf_Shdr;
338   typedef typename ELFT::Sym Elf_Sym;
339
340   // finalizeContents() fills this vector with the section contents.
341   std::vector<std::pair<int32_t, std::function<uint64_t()>>> Entries;
342
343 public:
344   DynamicSection();
345   void finalizeContents() override;
346   void writeTo(uint8_t *Buf) override;
347   size_t getSize() const override { return Size; }
348
349 private:
350   void add(int32_t Tag, std::function<uint64_t()> Fn);
351   void addInt(int32_t Tag, uint64_t Val);
352   void addInSec(int32_t Tag, InputSection *Sec);
353   void addOutSec(int32_t Tag, OutputSection *Sec);
354   void addSize(int32_t Tag, OutputSection *Sec);
355   void addSym(int32_t Tag, Symbol *Sym);
356
357   uint64_t Size = 0;
358 };
359
360 class RelocationBaseSection : public SyntheticSection {
361 public:
362   RelocationBaseSection(StringRef Name, uint32_t Type, int32_t DynamicTag,
363                         int32_t SizeDynamicTag);
364   void addReloc(const DynamicReloc &Reloc);
365   bool empty() const override { return Relocs.empty(); }
366   size_t getSize() const override { return Relocs.size() * this->Entsize; }
367   size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
368   void finalizeContents() override;
369   int32_t DynamicTag, SizeDynamicTag;
370
371 protected:
372   std::vector<DynamicReloc> Relocs;
373   size_t NumRelativeRelocs = 0;
374 };
375
376 template <class ELFT>
377 class RelocationSection final : public RelocationBaseSection {
378   typedef typename ELFT::Rel Elf_Rel;
379   typedef typename ELFT::Rela Elf_Rela;
380
381 public:
382   RelocationSection(StringRef Name, bool Sort);
383   unsigned getRelocOffset();
384   void writeTo(uint8_t *Buf) override;
385
386 private:
387   bool Sort;
388 };
389
390 template <class ELFT>
391 class AndroidPackedRelocationSection final : public RelocationBaseSection {
392   typedef typename ELFT::Rel Elf_Rel;
393   typedef typename ELFT::Rela Elf_Rela;
394
395 public:
396   AndroidPackedRelocationSection(StringRef Name);
397
398   bool updateAllocSize() override;
399   size_t getSize() const override { return RelocData.size(); }
400   void writeTo(uint8_t *Buf) override {
401     memcpy(Buf, RelocData.data(), RelocData.size());
402   }
403
404 private:
405   SmallVector<char, 0> RelocData;
406 };
407
408 struct SymbolTableEntry {
409   Symbol *Sym;
410   size_t StrTabOffset;
411 };
412
413 class SymbolTableBaseSection : public SyntheticSection {
414 public:
415   SymbolTableBaseSection(StringTableSection &StrTabSec);
416   void finalizeContents() override;
417   void postThunkContents() override;
418   size_t getSize() const override { return getNumSymbols() * Entsize; }
419   void addSymbol(Symbol *Sym);
420   unsigned getNumSymbols() const { return Symbols.size() + 1; }
421   size_t getSymbolIndex(Symbol *Sym);
422   ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
423
424 protected:
425   // A vector of symbols and their string table offsets.
426   std::vector<SymbolTableEntry> Symbols;
427
428   StringTableSection &StrTabSec;
429
430   llvm::once_flag OnceFlag;
431   llvm::DenseMap<Symbol *, size_t> SymbolIndexMap;
432   llvm::DenseMap<OutputSection *, size_t> SectionIndexMap;
433 };
434
435 template <class ELFT>
436 class SymbolTableSection final : public SymbolTableBaseSection {
437   typedef typename ELFT::Sym Elf_Sym;
438
439 public:
440   SymbolTableSection(StringTableSection &StrTabSec);
441   void writeTo(uint8_t *Buf) override;
442 };
443
444 // Outputs GNU Hash section. For detailed explanation see:
445 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
446 class GnuHashTableSection final : public SyntheticSection {
447 public:
448   GnuHashTableSection();
449   void finalizeContents() override;
450   void writeTo(uint8_t *Buf) override;
451   size_t getSize() const override { return Size; }
452
453   // Adds symbols to the hash table.
454   // Sorts the input to satisfy GNU hash section requirements.
455   void addSymbols(std::vector<SymbolTableEntry> &Symbols);
456
457 private:
458   size_t getShift2() const { return Config->Is64 ? 6 : 5; }
459
460   void writeBloomFilter(uint8_t *Buf);
461   void writeHashTable(uint8_t *Buf);
462
463   struct Entry {
464     Symbol *Sym;
465     size_t StrTabOffset;
466     uint32_t Hash;
467     uint32_t BucketIdx;
468   };
469
470   std::vector<Entry> Symbols;
471   size_t MaskWords;
472   size_t NBuckets = 0;
473   size_t Size = 0;
474 };
475
476 class HashTableSection final : public SyntheticSection {
477 public:
478   HashTableSection();
479   void finalizeContents() override;
480   void writeTo(uint8_t *Buf) override;
481   size_t getSize() const override { return Size; }
482
483 private:
484   size_t Size = 0;
485 };
486
487 // The PltSection is used for both the Plt and Iplt. The former always has a
488 // header as its first entry that is used at run-time to resolve lazy binding.
489 // The latter is used for GNU Ifunc symbols, that will be subject to a
490 // Target->IRelativeRel.
491 class PltSection : public SyntheticSection {
492 public:
493   PltSection(size_t HeaderSize);
494   void writeTo(uint8_t *Buf) override;
495   size_t getSize() const override;
496   bool empty() const override { return Entries.empty(); }
497   void addSymbols();
498
499   template <class ELFT> void addEntry(Symbol &Sym);
500
501 private:
502   unsigned getPltRelocOff() const;
503   std::vector<std::pair<const Symbol *, unsigned>> Entries;
504   // Iplt always has HeaderSize of 0, the Plt HeaderSize is always non-zero
505   size_t HeaderSize;
506 };
507
508 // GdbIndexChunk is created for each .debug_info section and contains
509 // information to create a part of .gdb_index for a given input section.
510 struct GdbIndexChunk {
511   struct AddressEntry {
512     InputSection *Section;
513     uint64_t LowAddress;
514     uint64_t HighAddress;
515     uint32_t CuIndex;
516   };
517
518   struct CuEntry {
519     uint64_t CuOffset;
520     uint64_t CuLength;
521   };
522
523   struct NameTypeEntry {
524     llvm::CachedHashStringRef Name;
525     uint8_t Type;
526   };
527
528   InputSection *DebugInfoSec;
529   std::vector<AddressEntry> AddressAreas;
530   std::vector<CuEntry> CompilationUnits;
531   std::vector<NameTypeEntry> NamesAndTypes;
532 };
533
534 // The symbol type for the .gdb_index section.
535 struct GdbSymbol {
536   uint32_t NameHash;
537   size_t NameOffset;
538   size_t CuVectorIndex;
539 };
540
541 class GdbIndexSection final : public SyntheticSection {
542 public:
543   GdbIndexSection(std::vector<GdbIndexChunk> &&Chunks);
544   void writeTo(uint8_t *Buf) override;
545   size_t getSize() const override;
546   bool empty() const override;
547
548 private:
549   void fixCuIndex();
550   std::vector<std::vector<uint32_t>> createCuVectors();
551   std::vector<GdbSymbol *> createGdbSymtab();
552
553   // A symbol table for this .gdb_index section.
554   std::vector<GdbSymbol *> GdbSymtab;
555
556   // CU vector is a part of constant pool area of section.
557   std::vector<std::vector<uint32_t>> CuVectors;
558
559   // Symbol table contents.
560   llvm::DenseMap<llvm::CachedHashStringRef, GdbSymbol *> Symbols;
561
562   // Each chunk contains information gathered from a debug sections of single
563   // object and used to build different areas of gdb index.
564   std::vector<GdbIndexChunk> Chunks;
565
566   static constexpr uint32_t CuListOffset = 24;
567   uint32_t CuTypesOffset;
568   uint32_t SymtabOffset;
569   uint32_t ConstantPoolOffset;
570   uint32_t StringPoolOffset;
571   uint32_t StringPoolSize;
572
573   std::vector<size_t> CuVectorOffsets;
574 };
575
576 template <class ELFT> GdbIndexSection *createGdbIndex();
577
578 // --eh-frame-hdr option tells linker to construct a header for all the
579 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
580 // and also to a PT_GNU_EH_FRAME segment.
581 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
582 // calling dl_iterate_phdr.
583 // This section contains a lookup table for quick binary search of FDEs.
584 // Detailed info about internals can be found in Ian Lance Taylor's blog:
585 // http://www.airs.com/blog/archives/460 (".eh_frame")
586 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
587 class EhFrameHeader final : public SyntheticSection {
588 public:
589   EhFrameHeader();
590   void writeTo(uint8_t *Buf) override;
591   size_t getSize() const override;
592   bool empty() const override;
593 };
594
595 // For more information about .gnu.version and .gnu.version_r see:
596 // https://www.akkadia.org/drepper/symbol-versioning
597
598 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
599 // contain symbol version definitions. The number of entries in this section
600 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
601 // The section shall contain an array of Elf_Verdef structures, optionally
602 // followed by an array of Elf_Verdaux structures.
603 template <class ELFT>
604 class VersionDefinitionSection final : public SyntheticSection {
605   typedef typename ELFT::Verdef Elf_Verdef;
606   typedef typename ELFT::Verdaux Elf_Verdaux;
607
608 public:
609   VersionDefinitionSection();
610   void finalizeContents() override;
611   size_t getSize() const override;
612   void writeTo(uint8_t *Buf) override;
613
614 private:
615   void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
616
617   unsigned FileDefNameOff;
618 };
619
620 // The .gnu.version section specifies the required version of each symbol in the
621 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
622 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
623 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
624 // The values 0 and 1 are reserved. All other values are used for versions in
625 // the own object or in any of the dependencies.
626 template <class ELFT>
627 class VersionTableSection final : public SyntheticSection {
628   typedef typename ELFT::Versym Elf_Versym;
629
630 public:
631   VersionTableSection();
632   void finalizeContents() override;
633   size_t getSize() const override;
634   void writeTo(uint8_t *Buf) override;
635   bool empty() const override;
636 };
637
638 // The .gnu.version_r section defines the version identifiers used by
639 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
640 // Elf_Verneed specifies the version requirements for a single DSO, and contains
641 // a reference to a linked list of Elf_Vernaux data structures which define the
642 // mapping from version identifiers to version names.
643 template <class ELFT> class VersionNeedSection final : public SyntheticSection {
644   typedef typename ELFT::Verneed Elf_Verneed;
645   typedef typename ELFT::Vernaux Elf_Vernaux;
646
647   // A vector of shared files that need Elf_Verneed data structures and the
648   // string table offsets of their sonames.
649   std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
650
651   // The next available version identifier.
652   unsigned NextIndex;
653
654 public:
655   VersionNeedSection();
656   void addSymbol(SharedSymbol *SS);
657   void finalizeContents() override;
658   void writeTo(uint8_t *Buf) override;
659   size_t getSize() const override;
660   size_t getNeedNum() const { return Needed.size(); }
661   bool empty() const override;
662 };
663
664 // MergeSyntheticSection is a class that allows us to put mergeable sections
665 // with different attributes in a single output sections. To do that
666 // we put them into MergeSyntheticSection synthetic input sections which are
667 // attached to regular output sections.
668 class MergeSyntheticSection : public SyntheticSection {
669 public:
670   void addSection(MergeInputSection *MS);
671
672 protected:
673   MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
674                         uint32_t Alignment)
675       : SyntheticSection(Flags, Type, Alignment, Name) {}
676
677   std::vector<MergeInputSection *> Sections;
678 };
679
680 class MergeTailSection final : public MergeSyntheticSection {
681 public:
682   MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
683                    uint32_t Alignment);
684
685   size_t getSize() const override;
686   void writeTo(uint8_t *Buf) override;
687   void finalizeContents() override;
688
689 private:
690   llvm::StringTableBuilder Builder;
691 };
692
693 class MergeNoTailSection final : public MergeSyntheticSection {
694 public:
695   MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
696                      uint32_t Alignment)
697       : MergeSyntheticSection(Name, Type, Flags, Alignment) {}
698
699   size_t getSize() const override { return Size; }
700   void writeTo(uint8_t *Buf) override;
701   void finalizeContents() override;
702
703 private:
704   // We use the most significant bits of a hash as a shard ID.
705   // The reason why we don't want to use the least significant bits is
706   // because DenseMap also uses lower bits to determine a bucket ID.
707   // If we use lower bits, it significantly increases the probability of
708   // hash collisons.
709   size_t getShardId(uint32_t Hash) {
710     return Hash >> (32 - llvm::countTrailingZeros(NumShards));
711   }
712
713   // Section size
714   size_t Size;
715
716   // String table contents
717   constexpr static size_t NumShards = 32;
718   std::vector<llvm::StringTableBuilder> Shards;
719   size_t ShardOffsets[NumShards];
720 };
721
722 // .MIPS.abiflags section.
723 template <class ELFT>
724 class MipsAbiFlagsSection final : public SyntheticSection {
725   typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
726
727 public:
728   static MipsAbiFlagsSection *create();
729
730   MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
731   size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
732   void writeTo(uint8_t *Buf) override;
733
734 private:
735   Elf_Mips_ABIFlags Flags;
736 };
737
738 // .MIPS.options section.
739 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
740   typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
741   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
742
743 public:
744   static MipsOptionsSection *create();
745
746   MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
747   void writeTo(uint8_t *Buf) override;
748
749   size_t getSize() const override {
750     return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
751   }
752
753 private:
754   Elf_Mips_RegInfo Reginfo;
755 };
756
757 // MIPS .reginfo section.
758 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
759   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
760
761 public:
762   static MipsReginfoSection *create();
763
764   MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
765   size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
766   void writeTo(uint8_t *Buf) override;
767
768 private:
769   Elf_Mips_RegInfo Reginfo;
770 };
771
772 // This is a MIPS specific section to hold a space within the data segment
773 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
774 // See "Dynamic section" in Chapter 5 in the following document:
775 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
776 class MipsRldMapSection : public SyntheticSection {
777 public:
778   MipsRldMapSection();
779   size_t getSize() const override { return Config->Wordsize; }
780   void writeTo(uint8_t *Buf) override {}
781 };
782
783 class ARMExidxSentinelSection : public SyntheticSection {
784 public:
785   ARMExidxSentinelSection();
786   size_t getSize() const override { return 8; }
787   void writeTo(uint8_t *Buf) override;
788   bool empty() const override;
789
790   InputSection *Highest = 0;
791 };
792
793 // A container for one or more linker generated thunks. Instances of these
794 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
795 class ThunkSection : public SyntheticSection {
796 public:
797   // ThunkSection in OS, with desired OutSecOff of Off
798   ThunkSection(OutputSection *OS, uint64_t Off);
799
800   // Add a newly created Thunk to this container:
801   // Thunk is given offset from start of this InputSection
802   // Thunk defines a symbol in this InputSection that can be used as target
803   // of a relocation
804   void addThunk(Thunk *T);
805   size_t getSize() const override { return Size; }
806   void writeTo(uint8_t *Buf) override;
807   InputSection *getTargetInputSection() const;
808
809 private:
810   std::vector<const Thunk *> Thunks;
811   size_t Size = 0;
812 };
813
814 InputSection *createInterpSection();
815 MergeInputSection *createCommentSection();
816 void decompressSections();
817 void mergeSections();
818
819 Symbol *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
820                           uint64_t Size, InputSectionBase &Section);
821
822 // Linker generated sections which can be used as inputs.
823 struct InX {
824   static InputSection *ARMAttributes;
825   static BssSection *Bss;
826   static BssSection *BssRelRo;
827   static BuildIdSection *BuildId;
828   static EhFrameHeader *EhFrameHdr;
829   static EhFrameSection *EhFrame;
830   static SyntheticSection *Dynamic;
831   static StringTableSection *DynStrTab;
832   static SymbolTableBaseSection *DynSymTab;
833   static GnuHashTableSection *GnuHashTab;
834   static HashTableSection *HashTab;
835   static InputSection *Interp;
836   static GdbIndexSection *GdbIndex;
837   static GotSection *Got;
838   static GotPltSection *GotPlt;
839   static IgotPltSection *IgotPlt;
840   static MipsGotSection *MipsGot;
841   static MipsRldMapSection *MipsRldMap;
842   static PltSection *Plt;
843   static PltSection *Iplt;
844   static RelocationBaseSection *RelaDyn;
845   static RelocationBaseSection *RelaPlt;
846   static RelocationBaseSection *RelaIplt;
847   static StringTableSection *ShStrTab;
848   static StringTableSection *StrTab;
849   static SymbolTableBaseSection *SymTab;
850 };
851
852 template <class ELFT> struct In {
853   static VersionDefinitionSection<ELFT> *VerDef;
854   static VersionTableSection<ELFT> *VerSym;
855   static VersionNeedSection<ELFT> *VerNeed;
856 };
857
858 template <class ELFT> VersionDefinitionSection<ELFT> *In<ELFT>::VerDef;
859 template <class ELFT> VersionTableSection<ELFT> *In<ELFT>::VerSym;
860 template <class ELFT> VersionNeedSection<ELFT> *In<ELFT>::VerNeed;
861 } // namespace elf
862 } // namespace lld
863
864 #endif