]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SyntheticSections.h
Merge lld trunk r300422 and resolve conflicts.
[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 template <class ELFT> class GotSection final : public SyntheticSection {
108 public:
109   GotSection();
110   void writeTo(uint8_t *Buf) override;
111   size_t getSize() const override { return Size; }
112   void finalizeContents() override;
113   bool empty() const 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 private:
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 template <class ELFT> class SymbolTableSection final : public SyntheticSection {
405 public:
406   typedef typename ELFT::Sym Elf_Sym;
407
408   SymbolTableSection(StringTableSection &StrTabSec);
409
410   void finalizeContents() override;
411   void postThunkContents() override;
412   void writeTo(uint8_t *Buf) override;
413   size_t getSize() const override { return getNumSymbols() * sizeof(Elf_Sym); }
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 private:
420   // A vector of symbols and their string table offsets.
421   std::vector<SymbolTableEntry> Symbols;
422
423   StringTableSection &StrTabSec;
424 };
425
426 // Outputs GNU Hash section. For detailed explanation see:
427 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
428 template <class ELFT>
429 class GnuHashTableSection final : public SyntheticSection {
430 public:
431   GnuHashTableSection();
432   void finalizeContents() override;
433   void writeTo(uint8_t *Buf) override;
434   size_t getSize() const override { return Size; }
435
436   // Adds symbols to the hash table.
437   // Sorts the input to satisfy GNU hash section requirements.
438   void addSymbols(std::vector<SymbolTableEntry> &Symbols);
439
440 private:
441   size_t getShift2() const { return Config->Is64 ? 6 : 5; }
442
443   void writeBloomFilter(uint8_t *Buf);
444   void writeHashTable(uint8_t *Buf);
445
446   struct Entry {
447     SymbolBody *Body;
448     size_t StrTabOffset;
449     uint32_t Hash;
450   };
451
452   std::vector<Entry> Symbols;
453   size_t MaskWords;
454   size_t NBuckets = 0;
455   size_t Size = 0;
456 };
457
458 template <class ELFT> class HashTableSection final : public SyntheticSection {
459 public:
460   HashTableSection();
461   void finalizeContents() override;
462   void writeTo(uint8_t *Buf) override;
463   size_t getSize() const override { return Size; }
464
465 private:
466   size_t Size = 0;
467 };
468
469 // The PltSection is used for both the Plt and Iplt. The former always has a
470 // header as its first entry that is used at run-time to resolve lazy binding.
471 // The latter is used for GNU Ifunc symbols, that will be subject to a
472 // Target->IRelativeRel.
473 class PltSection : public SyntheticSection {
474 public:
475   PltSection(size_t HeaderSize);
476   void writeTo(uint8_t *Buf) override;
477   size_t getSize() const override;
478   bool empty() const override { return Entries.empty(); }
479   void addSymbols();
480
481   template <class ELFT> void addEntry(SymbolBody &Sym);
482
483 private:
484   void writeHeader(uint8_t *Buf){};
485   void addHeaderSymbols(){};
486   unsigned getPltRelocOff() const;
487   std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
488   // Iplt always has HeaderSize of 0, the Plt HeaderSize is always non-zero
489   size_t HeaderSize;
490 };
491
492 class GdbIndexSection final : public SyntheticSection {
493   const unsigned OffsetTypeSize = 4;
494   const unsigned CuListOffset = 6 * OffsetTypeSize;
495   const unsigned CompilationUnitSize = 16;
496   const unsigned AddressEntrySize = 16 + OffsetTypeSize;
497   const unsigned SymTabEntrySize = 2 * OffsetTypeSize;
498
499 public:
500   GdbIndexSection();
501   void finalizeContents() override;
502   void writeTo(uint8_t *Buf) override;
503   size_t getSize() const override;
504   bool empty() const override;
505
506   // Pairs of [CU Offset, CU length].
507   std::vector<std::pair<uint64_t, uint64_t>> CompilationUnits;
508
509   llvm::StringTableBuilder StringPool;
510
511   GdbHashTab SymbolTable;
512
513   // The CU vector portion of the constant pool.
514   std::vector<std::vector<std::pair<uint32_t, uint8_t>>> CuVectors;
515
516   std::vector<AddressEntry> AddressArea;
517
518 private:
519   void readDwarf(InputSection *Sec);
520
521   uint32_t CuTypesOffset;
522   uint32_t SymTabOffset;
523   uint32_t ConstantPoolOffset;
524   uint32_t StringPoolOffset;
525
526   size_t CuVectorsSize = 0;
527   std::vector<size_t> CuVectorsOffset;
528
529   bool Finalized = false;
530 };
531
532 // --eh-frame-hdr option tells linker to construct a header for all the
533 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
534 // and also to a PT_GNU_EH_FRAME segment.
535 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
536 // calling dl_iterate_phdr.
537 // This section contains a lookup table for quick binary search of FDEs.
538 // Detailed info about internals can be found in Ian Lance Taylor's blog:
539 // http://www.airs.com/blog/archives/460 (".eh_frame")
540 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
541 template <class ELFT> class EhFrameHeader final : public SyntheticSection {
542 public:
543   EhFrameHeader();
544   void writeTo(uint8_t *Buf) override;
545   size_t getSize() const override;
546   void addFde(uint32_t Pc, uint32_t FdeVA);
547   bool empty() const override;
548
549 private:
550   struct FdeData {
551     uint32_t Pc;
552     uint32_t FdeVA;
553   };
554
555   std::vector<FdeData> Fdes;
556 };
557
558 // For more information about .gnu.version and .gnu.version_r see:
559 // https://www.akkadia.org/drepper/symbol-versioning
560
561 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
562 // contain symbol version definitions. The number of entries in this section
563 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
564 // The section shall contain an array of Elf_Verdef structures, optionally
565 // followed by an array of Elf_Verdaux structures.
566 template <class ELFT>
567 class VersionDefinitionSection final : public SyntheticSection {
568   typedef typename ELFT::Verdef Elf_Verdef;
569   typedef typename ELFT::Verdaux Elf_Verdaux;
570
571 public:
572   VersionDefinitionSection();
573   void finalizeContents() override;
574   size_t getSize() const override;
575   void writeTo(uint8_t *Buf) override;
576
577 private:
578   void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
579
580   unsigned FileDefNameOff;
581 };
582
583 // The .gnu.version section specifies the required version of each symbol in the
584 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
585 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
586 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
587 // The values 0 and 1 are reserved. All other values are used for versions in
588 // the own object or in any of the dependencies.
589 template <class ELFT>
590 class VersionTableSection final : public SyntheticSection {
591   typedef typename ELFT::Versym Elf_Versym;
592
593 public:
594   VersionTableSection();
595   void finalizeContents() override;
596   size_t getSize() const override;
597   void writeTo(uint8_t *Buf) override;
598   bool empty() const override;
599 };
600
601 // The .gnu.version_r section defines the version identifiers used by
602 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
603 // Elf_Verneed specifies the version requirements for a single DSO, and contains
604 // a reference to a linked list of Elf_Vernaux data structures which define the
605 // mapping from version identifiers to version names.
606 template <class ELFT> class VersionNeedSection final : public SyntheticSection {
607   typedef typename ELFT::Verneed Elf_Verneed;
608   typedef typename ELFT::Vernaux Elf_Vernaux;
609
610   // A vector of shared files that need Elf_Verneed data structures and the
611   // string table offsets of their sonames.
612   std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
613
614   // The next available version identifier.
615   unsigned NextIndex;
616
617 public:
618   VersionNeedSection();
619   void addSymbol(SharedSymbol *SS);
620   void finalizeContents() override;
621   void writeTo(uint8_t *Buf) override;
622   size_t getSize() const override;
623   size_t getNeedNum() const { return Needed.size(); }
624   bool empty() const override;
625 };
626
627 // MergeSyntheticSection is a class that allows us to put mergeable sections
628 // with different attributes in a single output sections. To do that
629 // we put them into MergeSyntheticSection synthetic input sections which are
630 // attached to regular output sections.
631 class MergeSyntheticSection final : public SyntheticSection {
632 public:
633   MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
634                         uint32_t Alignment);
635   void addSection(MergeInputSection *MS);
636   void writeTo(uint8_t *Buf) override;
637   void finalizeContents() override;
638   bool shouldTailMerge() const;
639   size_t getSize() const override;
640
641 private:
642   void finalizeTailMerge();
643   void finalizeNoTailMerge();
644
645   bool Finalized = false;
646   llvm::StringTableBuilder Builder;
647   std::vector<MergeInputSection *> Sections;
648 };
649
650 // .MIPS.abiflags section.
651 template <class ELFT>
652 class MipsAbiFlagsSection final : public SyntheticSection {
653   typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
654
655 public:
656   static MipsAbiFlagsSection *create();
657
658   MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
659   size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
660   void writeTo(uint8_t *Buf) override;
661
662 private:
663   Elf_Mips_ABIFlags Flags;
664 };
665
666 // .MIPS.options section.
667 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
668   typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
669   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
670
671 public:
672   static MipsOptionsSection *create();
673
674   MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
675   void writeTo(uint8_t *Buf) override;
676
677   size_t getSize() const override {
678     return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
679   }
680
681 private:
682   Elf_Mips_RegInfo Reginfo;
683 };
684
685 // MIPS .reginfo section.
686 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
687   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
688
689 public:
690   static MipsReginfoSection *create();
691
692   MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
693   size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
694   void writeTo(uint8_t *Buf) override;
695
696 private:
697   Elf_Mips_RegInfo Reginfo;
698 };
699
700 // This is a MIPS specific section to hold a space within the data segment
701 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
702 // See "Dynamic section" in Chapter 5 in the following document:
703 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
704 class MipsRldMapSection : public SyntheticSection {
705 public:
706   MipsRldMapSection();
707   size_t getSize() const override { return Config->Wordsize; }
708   void writeTo(uint8_t *Buf) override;
709 };
710
711 class ARMExidxSentinelSection : public SyntheticSection {
712 public:
713   ARMExidxSentinelSection();
714   size_t getSize() const override { return 8; }
715   void writeTo(uint8_t *Buf) override;
716 };
717
718 // A container for one or more linker generated thunks. Instances of these
719 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
720 class ThunkSection : public SyntheticSection {
721 public:
722   // ThunkSection in OS, with desired OutSecOff of Off
723   ThunkSection(OutputSection *OS, uint64_t Off);
724
725   // Add a newly created Thunk to this container:
726   // Thunk is given offset from start of this InputSection
727   // Thunk defines a symbol in this InputSection that can be used as target
728   // of a relocation
729   void addThunk(Thunk *T);
730   size_t getSize() const override { return Size; }
731   void writeTo(uint8_t *Buf) override;
732   InputSection *getTargetInputSection() const;
733
734 private:
735   std::vector<const Thunk *> Thunks;
736   size_t Size = 0;
737 };
738
739 template <class ELFT> InputSection *createCommonSection();
740 InputSection *createInterpSection();
741 template <class ELFT> MergeInputSection *createCommentSection();
742 template <class ELFT>
743 SymbolBody *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
744                               uint64_t Size, InputSectionBase *Section);
745
746 // Linker generated sections which can be used as inputs.
747 struct InX {
748   static InputSection *ARMAttributes;
749   static BssSection *Bss;
750   static BssSection *BssRelRo;
751   static BuildIdSection *BuildId;
752   static InputSection *Common;
753   static StringTableSection *DynStrTab;
754   static InputSection *Interp;
755   static GdbIndexSection *GdbIndex;
756   static GotPltSection *GotPlt;
757   static IgotPltSection *IgotPlt;
758   static MipsGotSection *MipsGot;
759   static MipsRldMapSection *MipsRldMap;
760   static PltSection *Plt;
761   static PltSection *Iplt;
762   static StringTableSection *ShStrTab;
763   static StringTableSection *StrTab;
764 };
765
766 template <class ELFT> struct In : public InX {
767   static DynamicSection<ELFT> *Dynamic;
768   static SymbolTableSection<ELFT> *DynSymTab;
769   static EhFrameHeader<ELFT> *EhFrameHdr;
770   static GnuHashTableSection<ELFT> *GnuHashTab;
771   static GotSection<ELFT> *Got;
772   static EhFrameSection<ELFT> *EhFrame;
773   static HashTableSection<ELFT> *HashTab;
774   static RelocationSection<ELFT> *RelaDyn;
775   static RelocationSection<ELFT> *RelaPlt;
776   static RelocationSection<ELFT> *RelaIplt;
777   static SymbolTableSection<ELFT> *SymTab;
778   static VersionDefinitionSection<ELFT> *VerDef;
779   static VersionTableSection<ELFT> *VerSym;
780   static VersionNeedSection<ELFT> *VerNeed;
781 };
782
783 template <class ELFT> DynamicSection<ELFT> *In<ELFT>::Dynamic;
784 template <class ELFT> SymbolTableSection<ELFT> *In<ELFT>::DynSymTab;
785 template <class ELFT> EhFrameHeader<ELFT> *In<ELFT>::EhFrameHdr;
786 template <class ELFT> GnuHashTableSection<ELFT> *In<ELFT>::GnuHashTab;
787 template <class ELFT> GotSection<ELFT> *In<ELFT>::Got;
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> SymbolTableSection<ELFT> *In<ELFT>::SymTab;
794 template <class ELFT> VersionDefinitionSection<ELFT> *In<ELFT>::VerDef;
795 template <class ELFT> VersionTableSection<ELFT> *In<ELFT>::VerSym;
796 template <class ELFT> VersionNeedSection<ELFT> *In<ELFT>::VerNeed;
797 } // namespace elf
798 } // namespace lld
799
800 #endif