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