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