]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SyntheticSections.h
Merge lld trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[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 "DWARF.h"
25 #include "EhFrame.h"
26 #include "InputSection.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/MC/StringTableBuilder.h"
29 #include "llvm/Support/Endian.h"
30 #include <functional>
31
32 namespace lld {
33 namespace elf {
34 class Defined;
35 class SharedSymbol;
36
37 class SyntheticSection : public InputSection {
38 public:
39   SyntheticSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
40                    StringRef Name)
41       : InputSection(nullptr, Flags, Type, Alignment, {}, Name,
42                      InputSectionBase::Synthetic) {
43     this->Live = true;
44   }
45
46   virtual ~SyntheticSection() = default;
47   virtual void writeTo(uint8_t *Buf) = 0;
48   virtual size_t getSize() const = 0;
49   virtual void finalizeContents() {}
50   // If the section has the SHF_ALLOC flag and the size may be changed if
51   // thunks are added, update the section size.
52   virtual bool updateAllocSize() { return false; }
53   virtual bool empty() const { return false; }
54
55   static bool classof(const SectionBase *D) {
56     return D->kind() == InputSectionBase::Synthetic;
57   }
58 };
59
60 struct CieRecord {
61   EhSectionPiece *Cie = nullptr;
62   std::vector<EhSectionPiece *> Fdes;
63 };
64
65 // Section for .eh_frame.
66 class EhFrameSection final : public SyntheticSection {
67 public:
68   EhFrameSection();
69   void writeTo(uint8_t *Buf) override;
70   void finalizeContents() override;
71   bool empty() const override { return Sections.empty(); }
72   size_t getSize() const override { return Size; }
73
74   template <class ELFT> void addSection(InputSectionBase *S);
75
76   std::vector<EhInputSection *> Sections;
77   size_t NumFdes = 0;
78
79   struct FdeData {
80     uint32_t PcRel;
81     uint32_t FdeVARel;
82   };
83
84   std::vector<FdeData> getFdeData() const;
85   ArrayRef<CieRecord *> getCieRecords() const { return CieRecords; }
86
87 private:
88   // This is used only when parsing EhInputSection. We keep it here to avoid
89   // allocating one for each EhInputSection.
90   llvm::DenseMap<size_t, CieRecord *> OffsetToCie;
91
92   uint64_t Size = 0;
93
94   template <class ELFT, class RelTy>
95   void addSectionAux(EhInputSection *S, llvm::ArrayRef<RelTy> Rels);
96
97   template <class ELFT, class RelTy>
98   CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
99
100   template <class ELFT, class RelTy>
101   bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
102
103   uint64_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc) const;
104
105   std::vector<CieRecord *> CieRecords;
106
107   // CIE records are uniquified by their contents and personality functions.
108   llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> CieMap;
109 };
110
111 class GotSection : public SyntheticSection {
112 public:
113   GotSection();
114   size_t getSize() const override { return Size; }
115   void finalizeContents() override;
116   bool empty() const override;
117   void writeTo(uint8_t *Buf) override;
118
119   void addEntry(Symbol &Sym);
120   bool addDynTlsEntry(Symbol &Sym);
121   bool addTlsIndex();
122   uint64_t getGlobalDynAddr(const Symbol &B) const;
123   uint64_t getGlobalDynOffset(const Symbol &B) const;
124
125   uint64_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
126   uint32_t getTlsIndexOff() const { return TlsIndexOff; }
127
128   // Flag to force GOT to be in output if we have relocations
129   // that relies on its address.
130   bool HasGotOffRel = false;
131
132 protected:
133   size_t NumEntries = 0;
134   uint32_t TlsIndexOff = -1;
135   uint64_t Size = 0;
136 };
137
138 // .note.GNU-stack section.
139 class GnuStackSection : public SyntheticSection {
140 public:
141   GnuStackSection()
142       : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
143   void writeTo(uint8_t *Buf) override {}
144   size_t getSize() const override { return 0; }
145 };
146
147 // .note.gnu.build-id section.
148 class BuildIdSection : public SyntheticSection {
149   // First 16 bytes are a header.
150   static const unsigned HeaderSize = 16;
151
152 public:
153   BuildIdSection();
154   void writeTo(uint8_t *Buf) override;
155   size_t getSize() const override { return HeaderSize + HashSize; }
156   void writeBuildId(llvm::ArrayRef<uint8_t> Buf);
157
158 private:
159   void computeHash(llvm::ArrayRef<uint8_t> Buf,
160                    std::function<void(uint8_t *, ArrayRef<uint8_t>)> Hash);
161
162   size_t HashSize;
163   uint8_t *HashBuf;
164 };
165
166 // BssSection is used to reserve space for copy relocations and common symbols.
167 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
168 // that are used for writable symbols, read-only symbols and common symbols,
169 // respectively.
170 class BssSection final : public SyntheticSection {
171 public:
172   BssSection(StringRef Name, uint64_t Size, uint32_t Alignment);
173   void writeTo(uint8_t *) override {
174     llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
175   }
176   bool empty() const override { return getSize() == 0; }
177   size_t getSize() const override { return Size; }
178
179   static bool classof(const SectionBase *S) { return S->Bss; }
180   uint64_t Size;
181 };
182
183 class MipsGotSection final : public SyntheticSection {
184 public:
185   MipsGotSection();
186   void writeTo(uint8_t *Buf) override;
187   size_t getSize() const override { return Size; }
188   bool updateAllocSize() override;
189   void finalizeContents() override;
190   bool empty() const override;
191
192   // Join separate GOTs built for each input file to generate
193   // primary and optional multiple secondary GOTs.
194   template <class ELFT> void build();
195
196   void addEntry(InputFile &File, Symbol &Sym, int64_t Addend, RelExpr Expr);
197   void addDynTlsEntry(InputFile &File, Symbol &Sym);
198   void addTlsIndex(InputFile &File);
199
200   uint64_t getPageEntryOffset(const InputFile *F, const Symbol &S,
201                               int64_t Addend) const;
202   uint64_t getSymEntryOffset(const InputFile *F, const Symbol &S,
203                              int64_t Addend) const;
204   uint64_t getGlobalDynOffset(const InputFile *F, const Symbol &S) const;
205   uint64_t getTlsIndexOffset(const InputFile *F) const;
206
207   // Returns the symbol which corresponds to the first entry of the global part
208   // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
209   // table properties.
210   // Returns nullptr if the global part is empty.
211   const Symbol *getFirstGlobalEntry() const;
212
213   // Returns the number of entries in the local part of GOT including
214   // the number of reserved entries.
215   unsigned getLocalEntriesNum() const;
216
217   // Return _gp value for primary GOT (nullptr) or particular input file.
218   uint64_t getGp(const InputFile *F = nullptr) const;
219
220 private:
221   // MIPS GOT consists of three parts: local, global and tls. Each part
222   // contains different types of entries. Here is a layout of GOT:
223   // - Header entries                |
224   // - Page entries                  |   Local part
225   // - Local entries (16-bit access) |
226   // - Local entries (32-bit access) |
227   // - Normal global entries         ||  Global part
228   // - Reloc-only global entries     ||
229   // - TLS entries                   ||| TLS part
230   //
231   // Header:
232   //   Two entries hold predefined value 0x0 and 0x80000000.
233   // Page entries:
234   //   These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
235   //   relocation against local symbols. They are initialized by higher 16-bit
236   //   of the corresponding symbol's value. So each 64kb of address space
237   //   requires a single GOT entry.
238   // Local entries (16-bit access):
239   //   These entries created by GOT relocations against global non-preemptible
240   //   symbols so dynamic linker is not necessary to resolve the symbol's
241   //   values. "16-bit access" means that corresponding relocations address
242   //   GOT using 16-bit index. Each unique Symbol-Addend pair has its own
243   //   GOT entry.
244   // Local entries (32-bit access):
245   //   These entries are the same as above but created by relocations which
246   //   address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
247   // Normal global entries:
248   //   These entries created by GOT relocations against preemptible global
249   //   symbols. They need to be initialized by dynamic linker and they ordered
250   //   exactly as the corresponding entries in the dynamic symbols table.
251   // Reloc-only global entries:
252   //   These entries created for symbols that are referenced by dynamic
253   //   relocations R_MIPS_REL32. These entries are not accessed with gp-relative
254   //   addressing, but MIPS ABI requires that these entries be present in GOT.
255   // TLS entries:
256   //   Entries created by TLS relocations.
257   //
258   // If the sum of local, global and tls entries is less than 64K only single
259   // got is enough. Otherwise, multi-got is created. Series of primary and
260   // multiple secondary GOTs have the following layout:
261   // - Primary GOT
262   //     Header
263   //     Local entries
264   //     Global entries
265   //     Relocation only entries
266   //     TLS entries
267   //
268   // - Secondary GOT
269   //     Local entries
270   //     Global entries
271   //     TLS entries
272   // ...
273   //
274   // All GOT entries required by relocations from a single input file entirely
275   // belong to either primary or one of secondary GOTs. To reference GOT entries
276   // each GOT has its own _gp value points to the "middle" of the GOT.
277   // In the code this value loaded to the register which is used for GOT access.
278   //
279   // MIPS 32 function's prologue:
280   //   lui     v0,0x0
281   //   0: R_MIPS_HI16  _gp_disp
282   //   addiu   v0,v0,0
283   //   4: R_MIPS_LO16  _gp_disp
284   //
285   // MIPS 64:
286   //   lui     at,0x0
287   //   14: R_MIPS_GPREL16  main
288   //
289   // Dynamic linker does not know anything about secondary GOTs and cannot
290   // use a regular MIPS mechanism for GOT entries initialization. So we have
291   // to use an approach accepted by other architectures and create dynamic
292   // relocations R_MIPS_REL32 to initialize global entries (and local in case
293   // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
294   // requires GOT entries and correspondingly ordered dynamic symbol table
295   // entries to deal with dynamic relocations. To handle this problem
296   // relocation-only section in the primary GOT contains entries for all
297   // symbols referenced in global parts of secondary GOTs. Although the sum
298   // of local and normal global entries of the primary got should be less
299   // than 64K, the size of the primary got (including relocation-only entries
300   // can be greater than 64K, because parts of the primary got that overflow
301   // the 64K limit are used only by the dynamic linker at dynamic link-time
302   // and not by 16-bit gp-relative addressing at run-time.
303   //
304   // For complete multi-GOT description see the following link
305   // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
306
307   // Number of "Header" entries.
308   static const unsigned HeaderEntriesNum = 2;
309
310   uint64_t Size = 0;
311
312   // Symbol and addend.
313   typedef std::pair<Symbol *, int64_t> GotEntry;
314
315   struct FileGot {
316     InputFile *File = nullptr;
317     size_t StartIndex = 0;
318
319     struct PageBlock {
320       size_t FirstIndex = 0;
321       size_t Count = 0;
322     };
323
324     // Map output sections referenced by MIPS GOT relocations
325     // to the description (index/count) "page" entries allocated
326     // for this section.
327     llvm::SmallMapVector<const OutputSection *, PageBlock, 16> PagesMap;
328     // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
329     llvm::MapVector<GotEntry, size_t> Local16;
330     llvm::MapVector<GotEntry, size_t> Local32;
331     llvm::MapVector<Symbol *, size_t> Global;
332     llvm::MapVector<Symbol *, size_t> Relocs;
333     llvm::MapVector<Symbol *, size_t> Tls;
334     // Set of symbols referenced by dynamic TLS relocations.
335     llvm::MapVector<Symbol *, size_t> DynTlsSymbols;
336
337     // Total number of all entries.
338     size_t getEntriesNum() const;
339     // Number of "page" entries.
340     size_t getPageEntriesNum() const;
341     // Number of entries require 16-bit index to access.
342     size_t getIndexedEntriesNum() const;
343   };
344
345   // Container of GOT created for each input file.
346   // After building a final series of GOTs this container
347   // holds primary and secondary GOT's.
348   std::vector<FileGot> Gots;
349
350   // Return (and create if necessary) `FileGot`.
351   FileGot &getGot(InputFile &F);
352
353   // Try to merge two GOTs. In case of success the `Dst` contains
354   // result of merging and the function returns true. In case of
355   // ovwerflow the `Dst` is unchanged and the function returns false.
356   bool tryMergeGots(FileGot & Dst, FileGot & Src, bool IsPrimary);
357 };
358
359 class GotPltSection final : public SyntheticSection {
360 public:
361   GotPltSection();
362   void addEntry(Symbol &Sym);
363   size_t getSize() const override;
364   void writeTo(uint8_t *Buf) override;
365   bool empty() const override;
366
367 private:
368   std::vector<const Symbol *> Entries;
369 };
370
371 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
372 // Symbols that will be relocated by Target->IRelativeRel.
373 // On most Targets the IgotPltSection will immediately follow the GotPltSection
374 // on ARM the IgotPltSection will immediately follow the GotSection.
375 class IgotPltSection final : public SyntheticSection {
376 public:
377   IgotPltSection();
378   void addEntry(Symbol &Sym);
379   size_t getSize() const override;
380   void writeTo(uint8_t *Buf) override;
381   bool empty() const override { return Entries.empty(); }
382
383 private:
384   std::vector<const Symbol *> Entries;
385 };
386
387 class StringTableSection final : public SyntheticSection {
388 public:
389   StringTableSection(StringRef Name, bool Dynamic);
390   unsigned addString(StringRef S, bool HashIt = true);
391   void writeTo(uint8_t *Buf) override;
392   size_t getSize() const override { return Size; }
393   bool isDynamic() const { return Dynamic; }
394
395 private:
396   const bool Dynamic;
397
398   uint64_t Size = 0;
399
400   llvm::DenseMap<StringRef, unsigned> StringMap;
401   std::vector<StringRef> Strings;
402 };
403
404 class DynamicReloc {
405 public:
406   DynamicReloc(RelType Type, const InputSectionBase *InputSec,
407                uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
408       : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
409         UseSymVA(UseSymVA), Addend(Addend), OutputSec(nullptr) {}
410   // This constructor records dynamic relocation settings used by MIPS
411   // multi-GOT implementation. It's to relocate addresses of 64kb pages
412   // lie inside the output section.
413   DynamicReloc(RelType Type, const InputSectionBase *InputSec,
414                uint64_t OffsetInSec, const OutputSection *OutputSec,
415                int64_t Addend)
416       : Type(Type), Sym(nullptr), InputSec(InputSec), OffsetInSec(OffsetInSec),
417         UseSymVA(false), Addend(Addend), OutputSec(OutputSec) {}
418
419   uint64_t getOffset() const;
420   uint32_t getSymIndex() const;
421   const InputSectionBase *getInputSec() const { return InputSec; }
422
423   // Computes the addend of the dynamic relocation. Note that this is not the
424   // same as the Addend member variable as it also includes the symbol address
425   // if UseSymVA is true.
426   int64_t computeAddend() const;
427
428   RelType Type;
429
430 private:
431   Symbol *Sym;
432   const InputSectionBase *InputSec = nullptr;
433   uint64_t OffsetInSec;
434   // If this member is true, the dynamic relocation will not be against the
435   // symbol but will instead be a relative relocation that simply adds the
436   // load address. This means we need to write the symbol virtual address
437   // plus the original addend as the final relocation addend.
438   bool UseSymVA;
439   int64_t Addend;
440   const OutputSection *OutputSec;
441 };
442
443 template <class ELFT> class DynamicSection final : public SyntheticSection {
444   typedef typename ELFT::Dyn Elf_Dyn;
445   typedef typename ELFT::Rel Elf_Rel;
446   typedef typename ELFT::Rela Elf_Rela;
447   typedef typename ELFT::Relr Elf_Relr;
448   typedef typename ELFT::Shdr Elf_Shdr;
449   typedef typename ELFT::Sym Elf_Sym;
450
451   // finalizeContents() fills this vector with the section contents.
452   std::vector<std::pair<int32_t, std::function<uint64_t()>>> Entries;
453
454 public:
455   DynamicSection();
456   void finalizeContents() override;
457   void writeTo(uint8_t *Buf) override;
458   size_t getSize() const override { return Size; }
459
460 private:
461   void add(int32_t Tag, std::function<uint64_t()> Fn);
462   void addInt(int32_t Tag, uint64_t Val);
463   void addInSec(int32_t Tag, InputSection *Sec);
464   void addInSecRelative(int32_t Tag, InputSection *Sec);
465   void addOutSec(int32_t Tag, OutputSection *Sec);
466   void addSize(int32_t Tag, OutputSection *Sec);
467   void addSym(int32_t Tag, Symbol *Sym);
468
469   uint64_t Size = 0;
470 };
471
472 class RelocationBaseSection : public SyntheticSection {
473 public:
474   RelocationBaseSection(StringRef Name, uint32_t Type, int32_t DynamicTag,
475                         int32_t SizeDynamicTag);
476   void addReloc(RelType DynType, InputSectionBase *IS, uint64_t OffsetInSec,
477                 Symbol *Sym);
478   // Add a dynamic relocation that might need an addend. This takes care of
479   // writing the addend to the output section if needed.
480   void addReloc(RelType DynType, InputSectionBase *InputSec,
481                 uint64_t OffsetInSec, Symbol *Sym, int64_t Addend, RelExpr Expr,
482                 RelType Type);
483   void addReloc(const DynamicReloc &Reloc);
484   bool empty() const override { return Relocs.empty(); }
485   size_t getSize() const override { return Relocs.size() * this->Entsize; }
486   size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
487   void finalizeContents() override;
488   int32_t DynamicTag, SizeDynamicTag;
489
490 protected:
491   std::vector<DynamicReloc> Relocs;
492   size_t NumRelativeRelocs = 0;
493 };
494
495 template <class ELFT>
496 class RelocationSection final : public RelocationBaseSection {
497   typedef typename ELFT::Rel Elf_Rel;
498   typedef typename ELFT::Rela Elf_Rela;
499
500 public:
501   RelocationSection(StringRef Name, bool Sort);
502   unsigned getRelocOffset();
503   void writeTo(uint8_t *Buf) override;
504
505 private:
506   bool Sort;
507 };
508
509 template <class ELFT>
510 class AndroidPackedRelocationSection final : public RelocationBaseSection {
511   typedef typename ELFT::Rel Elf_Rel;
512   typedef typename ELFT::Rela Elf_Rela;
513
514 public:
515   AndroidPackedRelocationSection(StringRef Name);
516
517   bool updateAllocSize() override;
518   size_t getSize() const override { return RelocData.size(); }
519   void writeTo(uint8_t *Buf) override {
520     memcpy(Buf, RelocData.data(), RelocData.size());
521   }
522
523 private:
524   SmallVector<char, 0> RelocData;
525 };
526
527 struct RelativeReloc {
528   uint64_t getOffset() const { return InputSec->getVA(OffsetInSec); }
529
530   const InputSectionBase *InputSec;
531   uint64_t OffsetInSec;
532 };
533
534 class RelrBaseSection : public SyntheticSection {
535 public:
536   RelrBaseSection();
537   bool empty() const override { return Relocs.empty(); }
538   std::vector<RelativeReloc> Relocs;
539 };
540
541 // RelrSection is used to encode offsets for relative relocations.
542 // Proposal for adding SHT_RELR sections to generic-abi is here:
543 //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
544 // For more details, see the comment in RelrSection::updateAllocSize().
545 template <class ELFT> class RelrSection final : public RelrBaseSection {
546   typedef typename ELFT::Relr Elf_Relr;
547
548 public:
549   RelrSection();
550
551   bool updateAllocSize() override;
552   size_t getSize() const override { return RelrRelocs.size() * this->Entsize; }
553   void writeTo(uint8_t *Buf) override {
554     memcpy(Buf, RelrRelocs.data(), getSize());
555   }
556
557 private:
558   std::vector<Elf_Relr> RelrRelocs;
559 };
560
561 struct SymbolTableEntry {
562   Symbol *Sym;
563   size_t StrTabOffset;
564 };
565
566 class SymbolTableBaseSection : public SyntheticSection {
567 public:
568   SymbolTableBaseSection(StringTableSection &StrTabSec);
569   void finalizeContents() override;
570   size_t getSize() const override { return getNumSymbols() * Entsize; }
571   void addSymbol(Symbol *Sym);
572   unsigned getNumSymbols() const { return Symbols.size() + 1; }
573   size_t getSymbolIndex(Symbol *Sym);
574   ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
575
576 protected:
577   void sortSymTabSymbols();
578
579   // A vector of symbols and their string table offsets.
580   std::vector<SymbolTableEntry> Symbols;
581
582   StringTableSection &StrTabSec;
583
584   llvm::once_flag OnceFlag;
585   llvm::DenseMap<Symbol *, size_t> SymbolIndexMap;
586   llvm::DenseMap<OutputSection *, size_t> SectionIndexMap;
587 };
588
589 template <class ELFT>
590 class SymbolTableSection final : public SymbolTableBaseSection {
591   typedef typename ELFT::Sym Elf_Sym;
592
593 public:
594   SymbolTableSection(StringTableSection &StrTabSec);
595   void writeTo(uint8_t *Buf) override;
596 };
597
598 class SymtabShndxSection final : public SyntheticSection {
599 public:
600   SymtabShndxSection();
601
602   void writeTo(uint8_t *Buf) override;
603   size_t getSize() const override;
604   bool empty() const override;
605   void finalizeContents() override;
606 };
607
608 // Outputs GNU Hash section. For detailed explanation see:
609 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
610 class GnuHashTableSection final : public SyntheticSection {
611 public:
612   GnuHashTableSection();
613   void finalizeContents() override;
614   void writeTo(uint8_t *Buf) override;
615   size_t getSize() const override { return Size; }
616
617   // Adds symbols to the hash table.
618   // Sorts the input to satisfy GNU hash section requirements.
619   void addSymbols(std::vector<SymbolTableEntry> &Symbols);
620
621 private:
622   // See the comment in writeBloomFilter.
623   enum { Shift2 = 26 };
624
625   void writeBloomFilter(uint8_t *Buf);
626   void writeHashTable(uint8_t *Buf);
627
628   struct Entry {
629     Symbol *Sym;
630     size_t StrTabOffset;
631     uint32_t Hash;
632     uint32_t BucketIdx;
633   };
634
635   std::vector<Entry> Symbols;
636   size_t MaskWords;
637   size_t NBuckets = 0;
638   size_t Size = 0;
639 };
640
641 class HashTableSection final : public SyntheticSection {
642 public:
643   HashTableSection();
644   void finalizeContents() override;
645   void writeTo(uint8_t *Buf) override;
646   size_t getSize() const override { return Size; }
647
648 private:
649   size_t Size = 0;
650 };
651
652 // The PltSection is used for both the Plt and Iplt. The former usually has a
653 // header as its first entry that is used at run-time to resolve lazy binding.
654 // The latter is used for GNU Ifunc symbols, that will be subject to a
655 // Target->IRelativeRel.
656 class PltSection : public SyntheticSection {
657 public:
658   PltSection(bool IsIplt);
659   void writeTo(uint8_t *Buf) override;
660   size_t getSize() const override;
661   bool empty() const override { return Entries.empty(); }
662   void addSymbols();
663   template <class ELFT> void addEntry(Symbol &Sym);
664
665   size_t HeaderSize;
666
667 private:
668   unsigned getPltRelocOff() const;
669   std::vector<std::pair<const Symbol *, unsigned>> Entries;
670   bool IsIplt;
671 };
672
673 class GdbIndexSection final : public SyntheticSection {
674 public:
675   struct AddressEntry {
676     InputSection *Section;
677     uint64_t LowAddress;
678     uint64_t HighAddress;
679     uint32_t CuIndex;
680   };
681
682   struct CuEntry {
683     uint64_t CuOffset;
684     uint64_t CuLength;
685   };
686
687   struct NameAttrEntry {
688     llvm::CachedHashStringRef Name;
689     uint32_t CuIndexAndAttrs;
690   };
691
692   struct GdbChunk {
693     InputSection *Sec;
694     std::vector<AddressEntry> AddressAreas;
695     std::vector<CuEntry> CompilationUnits;
696   };
697
698   struct GdbSymbol {
699     llvm::CachedHashStringRef Name;
700     std::vector<uint32_t> CuVector;
701     uint32_t NameOff;
702     uint32_t CuVectorOff;
703   };
704
705   GdbIndexSection();
706   template <typename ELFT> static GdbIndexSection *create();
707   void writeTo(uint8_t *Buf) override;
708   size_t getSize() const override { return Size; }
709   bool empty() const override;
710
711 private:
712   struct GdbIndexHeader {
713     llvm::support::ulittle32_t Version;
714     llvm::support::ulittle32_t CuListOff;
715     llvm::support::ulittle32_t CuTypesOff;
716     llvm::support::ulittle32_t AddressAreaOff;
717     llvm::support::ulittle32_t SymtabOff;
718     llvm::support::ulittle32_t ConstantPoolOff;
719   };
720
721   void initOutputSize();
722   size_t computeSymtabSize() const;
723
724   // Each chunk contains information gathered from debug sections of a
725   // single object file.
726   std::vector<GdbChunk> Chunks;
727
728   // A symbol table for this .gdb_index section.
729   std::vector<GdbSymbol> Symbols;
730
731   size_t Size;
732 };
733
734 // --eh-frame-hdr option tells linker to construct a header for all the
735 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
736 // and also to a PT_GNU_EH_FRAME segment.
737 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
738 // calling dl_iterate_phdr.
739 // This section contains a lookup table for quick binary search of FDEs.
740 // Detailed info about internals can be found in Ian Lance Taylor's blog:
741 // http://www.airs.com/blog/archives/460 (".eh_frame")
742 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
743 class EhFrameHeader final : public SyntheticSection {
744 public:
745   EhFrameHeader();
746   void writeTo(uint8_t *Buf) override;
747   size_t getSize() const override;
748   bool empty() const override;
749 };
750
751 // For more information about .gnu.version and .gnu.version_r see:
752 // https://www.akkadia.org/drepper/symbol-versioning
753
754 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
755 // contain symbol version definitions. The number of entries in this section
756 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
757 // The section shall contain an array of Elf_Verdef structures, optionally
758 // followed by an array of Elf_Verdaux structures.
759 class VersionDefinitionSection final : public SyntheticSection {
760 public:
761   VersionDefinitionSection();
762   void finalizeContents() override;
763   size_t getSize() const override;
764   void writeTo(uint8_t *Buf) override;
765
766 private:
767   enum { EntrySize = 28 };
768   void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
769
770   unsigned FileDefNameOff;
771 };
772
773 // The .gnu.version section specifies the required version of each symbol in the
774 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
775 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
776 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
777 // The values 0 and 1 are reserved. All other values are used for versions in
778 // the own object or in any of the dependencies.
779 template <class ELFT>
780 class VersionTableSection final : public SyntheticSection {
781 public:
782   VersionTableSection();
783   void finalizeContents() override;
784   size_t getSize() const override;
785   void writeTo(uint8_t *Buf) override;
786   bool empty() const override;
787 };
788
789 // The .gnu.version_r section defines the version identifiers used by
790 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
791 // Elf_Verneed specifies the version requirements for a single DSO, and contains
792 // a reference to a linked list of Elf_Vernaux data structures which define the
793 // mapping from version identifiers to version names.
794 template <class ELFT> class VersionNeedSection final : public SyntheticSection {
795   typedef typename ELFT::Verneed Elf_Verneed;
796   typedef typename ELFT::Vernaux Elf_Vernaux;
797
798   // A vector of shared files that need Elf_Verneed data structures and the
799   // string table offsets of their sonames.
800   std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
801
802   // The next available version identifier.
803   unsigned NextIndex;
804
805 public:
806   VersionNeedSection();
807   void addSymbol(Symbol *Sym);
808   void finalizeContents() override;
809   void writeTo(uint8_t *Buf) override;
810   size_t getSize() const override;
811   size_t getNeedNum() const { return Needed.size(); }
812   bool empty() const override;
813 };
814
815 // MergeSyntheticSection is a class that allows us to put mergeable sections
816 // with different attributes in a single output sections. To do that
817 // we put them into MergeSyntheticSection synthetic input sections which are
818 // attached to regular output sections.
819 class MergeSyntheticSection : public SyntheticSection {
820 public:
821   void addSection(MergeInputSection *MS);
822   std::vector<MergeInputSection *> Sections;
823
824 protected:
825   MergeSyntheticSection(StringRef Name, uint32_t Type, uint64_t Flags,
826                         uint32_t Alignment)
827       : SyntheticSection(Flags, Type, Alignment, Name) {}
828 };
829
830 class MergeTailSection final : public MergeSyntheticSection {
831 public:
832   MergeTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
833                    uint32_t Alignment);
834
835   size_t getSize() const override;
836   void writeTo(uint8_t *Buf) override;
837   void finalizeContents() override;
838
839 private:
840   llvm::StringTableBuilder Builder;
841 };
842
843 class MergeNoTailSection final : public MergeSyntheticSection {
844 public:
845   MergeNoTailSection(StringRef Name, uint32_t Type, uint64_t Flags,
846                      uint32_t Alignment)
847       : MergeSyntheticSection(Name, Type, Flags, Alignment) {}
848
849   size_t getSize() const override { return Size; }
850   void writeTo(uint8_t *Buf) override;
851   void finalizeContents() override;
852
853 private:
854   // We use the most significant bits of a hash as a shard ID.
855   // The reason why we don't want to use the least significant bits is
856   // because DenseMap also uses lower bits to determine a bucket ID.
857   // If we use lower bits, it significantly increases the probability of
858   // hash collisons.
859   size_t getShardId(uint32_t Hash) {
860     return Hash >> (32 - llvm::countTrailingZeros(NumShards));
861   }
862
863   // Section size
864   size_t Size;
865
866   // String table contents
867   constexpr static size_t NumShards = 32;
868   std::vector<llvm::StringTableBuilder> Shards;
869   size_t ShardOffsets[NumShards];
870 };
871
872 // .MIPS.abiflags section.
873 template <class ELFT>
874 class MipsAbiFlagsSection final : public SyntheticSection {
875   typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
876
877 public:
878   static MipsAbiFlagsSection *create();
879
880   MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags);
881   size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
882   void writeTo(uint8_t *Buf) override;
883
884 private:
885   Elf_Mips_ABIFlags Flags;
886 };
887
888 // .MIPS.options section.
889 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
890   typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
891   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
892
893 public:
894   static MipsOptionsSection *create();
895
896   MipsOptionsSection(Elf_Mips_RegInfo Reginfo);
897   void writeTo(uint8_t *Buf) override;
898
899   size_t getSize() const override {
900     return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
901   }
902
903 private:
904   Elf_Mips_RegInfo Reginfo;
905 };
906
907 // MIPS .reginfo section.
908 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
909   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
910
911 public:
912   static MipsReginfoSection *create();
913
914   MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
915   size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
916   void writeTo(uint8_t *Buf) override;
917
918 private:
919   Elf_Mips_RegInfo Reginfo;
920 };
921
922 // This is a MIPS specific section to hold a space within the data segment
923 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
924 // See "Dynamic section" in Chapter 5 in the following document:
925 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
926 class MipsRldMapSection : public SyntheticSection {
927 public:
928   MipsRldMapSection();
929   size_t getSize() const override { return Config->Wordsize; }
930   void writeTo(uint8_t *Buf) override {}
931 };
932
933 class ARMExidxSentinelSection : public SyntheticSection {
934 public:
935   ARMExidxSentinelSection();
936   size_t getSize() const override { return 8; }
937   void writeTo(uint8_t *Buf) override;
938   bool empty() const override;
939
940   static bool classof(const SectionBase *D);
941
942   // The last section referenced by a regular .ARM.exidx section.
943   // It is found and filled in Writer<ELFT>::resolveShfLinkOrder().
944   // The sentinel points at the end of that section.
945   InputSection *Highest = nullptr;
946 };
947
948 // A container for one or more linker generated thunks. Instances of these
949 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
950 class ThunkSection : public SyntheticSection {
951 public:
952   // ThunkSection in OS, with desired OutSecOff of Off
953   ThunkSection(OutputSection *OS, uint64_t Off);
954
955   // Add a newly created Thunk to this container:
956   // Thunk is given offset from start of this InputSection
957   // Thunk defines a symbol in this InputSection that can be used as target
958   // of a relocation
959   void addThunk(Thunk *T);
960   size_t getSize() const override { return Size; }
961   void writeTo(uint8_t *Buf) override;
962   InputSection *getTargetInputSection() const;
963   bool assignOffsets();
964
965 private:
966   std::vector<Thunk *> Thunks;
967   size_t Size = 0;
968 };
969
970 // This section is used to store the addresses of functions that are called
971 // in range-extending thunks on PowerPC64. When producing position dependant
972 // code the addresses are link-time constants and the table is written out to
973 // the binary. When producing position-dependant code the table is allocated and
974 // filled in by the dynamic linker.
975 class PPC64LongBranchTargetSection final : public SyntheticSection {
976 public:
977   PPC64LongBranchTargetSection();
978   void addEntry(Symbol &Sym);
979   size_t getSize() const override;
980   void writeTo(uint8_t *Buf) override;
981   bool empty() const override;
982   void finalizeContents() override { Finalized = true; }
983
984 private:
985   std::vector<const Symbol *> Entries;
986   bool Finalized = false;
987 };
988
989 InputSection *createInterpSection();
990 MergeInputSection *createCommentSection();
991 template <class ELFT> void splitSections();
992 void mergeSections();
993
994 Defined *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
995                            uint64_t Size, InputSectionBase &Section);
996
997 // Linker generated sections which can be used as inputs.
998 struct InStruct {
999   InputSection *ARMAttributes;
1000   BssSection *Bss;
1001   BssSection *BssRelRo;
1002   BuildIdSection *BuildId;
1003   EhFrameHeader *EhFrameHdr;
1004   EhFrameSection *EhFrame;
1005   SyntheticSection *Dynamic;
1006   StringTableSection *DynStrTab;
1007   SymbolTableBaseSection *DynSymTab;
1008   GnuHashTableSection *GnuHashTab;
1009   HashTableSection *HashTab;
1010   InputSection *Interp;
1011   GdbIndexSection *GdbIndex;
1012   GotSection *Got;
1013   GotPltSection *GotPlt;
1014   IgotPltSection *IgotPlt;
1015   PPC64LongBranchTargetSection *PPC64LongBranchTarget;
1016   MipsGotSection *MipsGot;
1017   MipsRldMapSection *MipsRldMap;
1018   PltSection *Plt;
1019   PltSection *Iplt;
1020   RelocationBaseSection *RelaDyn;
1021   RelrBaseSection *RelrDyn;
1022   RelocationBaseSection *RelaPlt;
1023   RelocationBaseSection *RelaIplt;
1024   StringTableSection *ShStrTab;
1025   StringTableSection *StrTab;
1026   SymbolTableBaseSection *SymTab;
1027   SymtabShndxSection *SymTabShndx;
1028   VersionDefinitionSection *VerDef;
1029 };
1030
1031 extern InStruct In;
1032
1033 template <class ELFT> struct InX {
1034   static VersionTableSection<ELFT> *VerSym;
1035   static VersionNeedSection<ELFT> *VerNeed;
1036 };
1037
1038 template <class ELFT> VersionTableSection<ELFT> *InX<ELFT>::VerSym;
1039 template <class ELFT> VersionNeedSection<ELFT> *InX<ELFT>::VerNeed;
1040 } // namespace elf
1041 } // namespace lld
1042
1043 #endif