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