]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/SyntheticSections.h
Merge ^/vendor/llvm-openmp/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / ELF / SyntheticSections.h
1 //===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Synthetic sections represent chunks of linker-created data. If you
10 // need to create a chunk of data that to be included in some section
11 // in the result, you probably want to create that as a synthetic section.
12 //
13 // Synthetic sections are designed as input sections as opposed to
14 // output sections because we want to allow them to be manipulated
15 // using linker scripts just like other input sections from regular
16 // files.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLD_ELF_SYNTHETIC_SECTIONS_H
21 #define LLD_ELF_SYNTHETIC_SECTIONS_H
22
23 #include "DWARF.h"
24 #include "EhFrame.h"
25 #include "InputSection.h"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/MC/StringTableBuilder.h"
28 #include "llvm/Support/Endian.h"
29 #include <functional>
30
31 namespace lld {
32 namespace elf {
33 class Defined;
34 struct PhdrEntry;
35 class SymbolTableBaseSection;
36 class VersionNeedBaseSection;
37
38 class SyntheticSection : public InputSection {
39 public:
40   SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
41                    StringRef name)
42       : InputSection(nullptr, flags, type, alignment, {}, name,
43                      InputSectionBase::Synthetic) {
44     markLive();
45   }
46
47   virtual ~SyntheticSection() = default;
48   virtual void writeTo(uint8_t *buf) = 0;
49   virtual size_t getSize() const = 0;
50   virtual void finalizeContents() {}
51   // If the section has the SHF_ALLOC flag and the size may be changed if
52   // thunks are added, update the section size.
53   virtual bool updateAllocSize() { return false; }
54   virtual bool isNeeded() const { return true; }
55
56   static bool classof(const SectionBase *d) {
57     return d->kind() == InputSectionBase::Synthetic;
58   }
59 };
60
61 struct CieRecord {
62   EhSectionPiece *cie = nullptr;
63   std::vector<EhSectionPiece *> fdes;
64 };
65
66 // Section for .eh_frame.
67 class EhFrameSection final : public SyntheticSection {
68 public:
69   EhFrameSection();
70   void writeTo(uint8_t *buf) override;
71   void finalizeContents() override;
72   bool isNeeded() const override { return !sections.empty(); }
73   size_t getSize() const override { return size; }
74
75   static bool classof(const SectionBase *d) {
76     return SyntheticSection::classof(d) && d->name == ".eh_frame";
77   }
78
79   void addSection(EhInputSection *sec);
80
81   std::vector<EhInputSection *> sections;
82   size_t numFdes = 0;
83
84   struct FdeData {
85     uint32_t pcRel;
86     uint32_t fdeVARel;
87   };
88
89   std::vector<FdeData> getFdeData() const;
90   ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; }
91
92 private:
93   // This is used only when parsing EhInputSection. We keep it here to avoid
94   // allocating one for each EhInputSection.
95   llvm::DenseMap<size_t, CieRecord *> offsetToCie;
96
97   uint64_t size = 0;
98
99   template <class ELFT, class RelTy>
100   void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels);
101   template <class ELFT>
102   void addSectionAux(EhInputSection *s);
103
104   template <class ELFT, class RelTy>
105   CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels);
106
107   template <class ELFT, class RelTy>
108   bool isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels);
109
110   uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
111
112   std::vector<CieRecord *> cieRecords;
113
114   // CIE records are uniquified by their contents and personality functions.
115   llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
116 };
117
118 class GotSection : public SyntheticSection {
119 public:
120   GotSection();
121   size_t getSize() const override { return size; }
122   void finalizeContents() override;
123   bool isNeeded() const override;
124   void writeTo(uint8_t *buf) override;
125
126   void addEntry(Symbol &sym);
127   bool addDynTlsEntry(Symbol &sym);
128   bool addTlsIndex();
129   uint64_t getGlobalDynAddr(const Symbol &b) const;
130   uint64_t getGlobalDynOffset(const Symbol &b) const;
131
132   uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
133   uint32_t getTlsIndexOff() const { return tlsIndexOff; }
134
135   // Flag to force GOT to be in output if we have relocations
136   // that relies on its address.
137   bool hasGotOffRel = false;
138
139 protected:
140   size_t numEntries = 0;
141   uint32_t tlsIndexOff = -1;
142   uint64_t size = 0;
143 };
144
145 // .note.GNU-stack section.
146 class GnuStackSection : public SyntheticSection {
147 public:
148   GnuStackSection()
149       : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
150   void writeTo(uint8_t *buf) override {}
151   size_t getSize() const override { return 0; }
152 };
153
154 class GnuPropertySection : public SyntheticSection {
155 public:
156   GnuPropertySection();
157   void writeTo(uint8_t *buf) override;
158   size_t getSize() const override;
159 };
160
161 // .note.gnu.build-id section.
162 class BuildIdSection : public SyntheticSection {
163   // First 16 bytes are a header.
164   static const unsigned headerSize = 16;
165
166 public:
167   const size_t hashSize;
168   BuildIdSection();
169   void writeTo(uint8_t *buf) override;
170   size_t getSize() const override { return headerSize + hashSize; }
171   void writeBuildId(llvm::ArrayRef<uint8_t> buf);
172
173 private:
174   uint8_t *hashBuf;
175 };
176
177 // BssSection is used to reserve space for copy relocations and common symbols.
178 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
179 // that are used for writable symbols, read-only symbols and common symbols,
180 // respectively.
181 class BssSection final : public SyntheticSection {
182 public:
183   BssSection(StringRef name, uint64_t size, uint32_t alignment);
184   void writeTo(uint8_t *) override {
185     llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
186   }
187   bool isNeeded() const override { return size != 0; }
188   size_t getSize() const override { return size; }
189
190   static bool classof(const SectionBase *s) { return s->bss; }
191   uint64_t size;
192 };
193
194 class MipsGotSection final : public SyntheticSection {
195 public:
196   MipsGotSection();
197   void writeTo(uint8_t *buf) override;
198   size_t getSize() const override { return size; }
199   bool updateAllocSize() override;
200   void finalizeContents() override;
201   bool isNeeded() const override;
202
203   // Join separate GOTs built for each input file to generate
204   // primary and optional multiple secondary GOTs.
205   void build();
206
207   void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
208   void addDynTlsEntry(InputFile &file, Symbol &sym);
209   void addTlsIndex(InputFile &file);
210
211   uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
212                               int64_t addend) const;
213   uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
214                              int64_t addend) const;
215   uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
216   uint64_t getTlsIndexOffset(const InputFile *f) const;
217
218   // Returns the symbol which corresponds to the first entry of the global part
219   // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
220   // table properties.
221   // Returns nullptr if the global part is empty.
222   const Symbol *getFirstGlobalEntry() const;
223
224   // Returns the number of entries in the local part of GOT including
225   // the number of reserved entries.
226   unsigned getLocalEntriesNum() const;
227
228   // Return _gp value for primary GOT (nullptr) or particular input file.
229   uint64_t getGp(const InputFile *f = nullptr) const;
230
231 private:
232   // MIPS GOT consists of three parts: local, global and tls. Each part
233   // contains different types of entries. Here is a layout of GOT:
234   // - Header entries                |
235   // - Page entries                  |   Local part
236   // - Local entries (16-bit access) |
237   // - Local entries (32-bit access) |
238   // - Normal global entries         ||  Global part
239   // - Reloc-only global entries     ||
240   // - TLS entries                   ||| TLS part
241   //
242   // Header:
243   //   Two entries hold predefined value 0x0 and 0x80000000.
244   // Page entries:
245   //   These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
246   //   relocation against local symbols. They are initialized by higher 16-bit
247   //   of the corresponding symbol's value. So each 64kb of address space
248   //   requires a single GOT entry.
249   // Local entries (16-bit access):
250   //   These entries created by GOT relocations against global non-preemptible
251   //   symbols so dynamic linker is not necessary to resolve the symbol's
252   //   values. "16-bit access" means that corresponding relocations address
253   //   GOT using 16-bit index. Each unique Symbol-Addend pair has its own
254   //   GOT entry.
255   // Local entries (32-bit access):
256   //   These entries are the same as above but created by relocations which
257   //   address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
258   // Normal global entries:
259   //   These entries created by GOT relocations against preemptible global
260   //   symbols. They need to be initialized by dynamic linker and they ordered
261   //   exactly as the corresponding entries in the dynamic symbols table.
262   // Reloc-only global entries:
263   //   These entries created for symbols that are referenced by dynamic
264   //   relocations R_MIPS_REL32. These entries are not accessed with gp-relative
265   //   addressing, but MIPS ABI requires that these entries be present in GOT.
266   // TLS entries:
267   //   Entries created by TLS relocations.
268   //
269   // If the sum of local, global and tls entries is less than 64K only single
270   // got is enough. Otherwise, multi-got is created. Series of primary and
271   // multiple secondary GOTs have the following layout:
272   // - Primary GOT
273   //     Header
274   //     Local entries
275   //     Global entries
276   //     Relocation only entries
277   //     TLS entries
278   //
279   // - Secondary GOT
280   //     Local entries
281   //     Global entries
282   //     TLS entries
283   // ...
284   //
285   // All GOT entries required by relocations from a single input file entirely
286   // belong to either primary or one of secondary GOTs. To reference GOT entries
287   // each GOT has its own _gp value points to the "middle" of the GOT.
288   // In the code this value loaded to the register which is used for GOT access.
289   //
290   // MIPS 32 function's prologue:
291   //   lui     v0,0x0
292   //   0: R_MIPS_HI16  _gp_disp
293   //   addiu   v0,v0,0
294   //   4: R_MIPS_LO16  _gp_disp
295   //
296   // MIPS 64:
297   //   lui     at,0x0
298   //   14: R_MIPS_GPREL16  main
299   //
300   // Dynamic linker does not know anything about secondary GOTs and cannot
301   // use a regular MIPS mechanism for GOT entries initialization. So we have
302   // to use an approach accepted by other architectures and create dynamic
303   // relocations R_MIPS_REL32 to initialize global entries (and local in case
304   // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
305   // requires GOT entries and correspondingly ordered dynamic symbol table
306   // entries to deal with dynamic relocations. To handle this problem
307   // relocation-only section in the primary GOT contains entries for all
308   // symbols referenced in global parts of secondary GOTs. Although the sum
309   // of local and normal global entries of the primary got should be less
310   // than 64K, the size of the primary got (including relocation-only entries
311   // can be greater than 64K, because parts of the primary got that overflow
312   // the 64K limit are used only by the dynamic linker at dynamic link-time
313   // and not by 16-bit gp-relative addressing at run-time.
314   //
315   // For complete multi-GOT description see the following link
316   // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
317
318   // Number of "Header" entries.
319   static const unsigned headerEntriesNum = 2;
320
321   uint64_t size = 0;
322
323   // Symbol and addend.
324   using GotEntry = std::pair<Symbol *, int64_t>;
325
326   struct FileGot {
327     InputFile *file = nullptr;
328     size_t startIndex = 0;
329
330     struct PageBlock {
331       size_t firstIndex;
332       size_t count;
333       PageBlock() : firstIndex(0), count(0) {}
334     };
335
336     // Map output sections referenced by MIPS GOT relocations
337     // to the description (index/count) "page" entries allocated
338     // for this section.
339     llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
340     // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
341     llvm::MapVector<GotEntry, size_t> local16;
342     llvm::MapVector<GotEntry, size_t> local32;
343     llvm::MapVector<Symbol *, size_t> global;
344     llvm::MapVector<Symbol *, size_t> relocs;
345     llvm::MapVector<Symbol *, size_t> tls;
346     // Set of symbols referenced by dynamic TLS relocations.
347     llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
348
349     // Total number of all entries.
350     size_t getEntriesNum() const;
351     // Number of "page" entries.
352     size_t getPageEntriesNum() const;
353     // Number of entries require 16-bit index to access.
354     size_t getIndexedEntriesNum() const;
355   };
356
357   // Container of GOT created for each input file.
358   // After building a final series of GOTs this container
359   // holds primary and secondary GOT's.
360   std::vector<FileGot> gots;
361
362   // Return (and create if necessary) `FileGot`.
363   FileGot &getGot(InputFile &f);
364
365   // Try to merge two GOTs. In case of success the `Dst` contains
366   // result of merging and the function returns true. In case of
367   // ovwerflow the `Dst` is unchanged and the function returns false.
368   bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
369 };
370
371 class GotPltSection final : public SyntheticSection {
372 public:
373   GotPltSection();
374   void addEntry(Symbol &sym);
375   size_t getSize() const override;
376   void writeTo(uint8_t *buf) override;
377   bool isNeeded() const override;
378
379   // Flag to force GotPlt to be in output if we have relocations
380   // that relies on its address.
381   bool hasGotPltOffRel = false;
382
383 private:
384   std::vector<const Symbol *> entries;
385 };
386
387 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
388 // Symbols that will be relocated by Target->IRelativeRel.
389 // On most Targets the IgotPltSection will immediately follow the GotPltSection
390 // on ARM the IgotPltSection will immediately follow the GotSection.
391 class IgotPltSection final : public SyntheticSection {
392 public:
393   IgotPltSection();
394   void addEntry(Symbol &sym);
395   size_t getSize() const override;
396   void writeTo(uint8_t *buf) override;
397   bool isNeeded() const override { return !entries.empty(); }
398
399 private:
400   std::vector<const Symbol *> entries;
401 };
402
403 class StringTableSection final : public SyntheticSection {
404 public:
405   StringTableSection(StringRef name, bool dynamic);
406   unsigned addString(StringRef s, bool hashIt = true);
407   void writeTo(uint8_t *buf) override;
408   size_t getSize() const override { return size; }
409   bool isDynamic() const { return dynamic; }
410
411 private:
412   const bool dynamic;
413
414   uint64_t size = 0;
415
416   llvm::DenseMap<StringRef, unsigned> stringMap;
417   std::vector<StringRef> strings;
418 };
419
420 class DynamicReloc {
421 public:
422   DynamicReloc(RelType type, const InputSectionBase *inputSec,
423                uint64_t offsetInSec, bool useSymVA, Symbol *sym, int64_t addend)
424       : type(type), sym(sym), inputSec(inputSec), offsetInSec(offsetInSec),
425         useSymVA(useSymVA), addend(addend), outputSec(nullptr) {}
426   // This constructor records dynamic relocation settings used by MIPS
427   // multi-GOT implementation. It's to relocate addresses of 64kb pages
428   // lie inside the output section.
429   DynamicReloc(RelType type, const InputSectionBase *inputSec,
430                uint64_t offsetInSec, const OutputSection *outputSec,
431                int64_t addend)
432       : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec),
433         useSymVA(false), addend(addend), outputSec(outputSec) {}
434
435   uint64_t getOffset() const;
436   uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
437
438   // Computes the addend of the dynamic relocation. Note that this is not the
439   // same as the addend member variable as it also includes the symbol address
440   // if useSymVA is true.
441   int64_t computeAddend() const;
442
443   RelType type;
444
445   Symbol *sym;
446   const InputSectionBase *inputSec = nullptr;
447   uint64_t offsetInSec;
448   // If this member is true, the dynamic relocation will not be against the
449   // symbol but will instead be a relative relocation that simply adds the
450   // load address. This means we need to write the symbol virtual address
451   // plus the original addend as the final relocation addend.
452   bool useSymVA;
453   int64_t addend;
454   const OutputSection *outputSec;
455 };
456
457 template <class ELFT> class DynamicSection final : public SyntheticSection {
458   using Elf_Dyn = typename ELFT::Dyn;
459   using Elf_Rel = typename ELFT::Rel;
460   using Elf_Rela = typename ELFT::Rela;
461   using Elf_Relr = typename ELFT::Relr;
462   using Elf_Shdr = typename ELFT::Shdr;
463   using Elf_Sym = typename ELFT::Sym;
464
465   // finalizeContents() fills this vector with the section contents.
466   std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries;
467
468 public:
469   DynamicSection();
470   void finalizeContents() override;
471   void writeTo(uint8_t *buf) override;
472   size_t getSize() const override { return size; }
473
474 private:
475   void add(int32_t tag, std::function<uint64_t()> fn);
476   void addInt(int32_t tag, uint64_t val);
477   void addInSec(int32_t tag, InputSection *sec);
478   void addInSecRelative(int32_t tag, InputSection *sec);
479   void addOutSec(int32_t tag, OutputSection *sec);
480   void addSize(int32_t tag, OutputSection *sec);
481   void addSym(int32_t tag, Symbol *sym);
482
483   uint64_t size = 0;
484 };
485
486 class RelocationBaseSection : public SyntheticSection {
487 public:
488   RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag,
489                         int32_t sizeDynamicTag);
490   void addReloc(RelType dynType, InputSectionBase *isec, uint64_t offsetInSec,
491                 Symbol *sym);
492   // Add a dynamic relocation that might need an addend. This takes care of
493   // writing the addend to the output section if needed.
494   void addReloc(RelType dynType, InputSectionBase *inputSec,
495                 uint64_t offsetInSec, Symbol *sym, int64_t addend, RelExpr expr,
496                 RelType type);
497   void addReloc(const DynamicReloc &reloc);
498   bool isNeeded() const override { return !relocs.empty(); }
499   size_t getSize() const override { return relocs.size() * this->entsize; }
500   size_t getRelativeRelocCount() const { return numRelativeRelocs; }
501   void finalizeContents() override;
502   int32_t dynamicTag, sizeDynamicTag;
503   std::vector<DynamicReloc> relocs;
504
505 protected:
506   size_t numRelativeRelocs = 0;
507 };
508
509 template <class ELFT>
510 class RelocationSection final : public RelocationBaseSection {
511   using Elf_Rel = typename ELFT::Rel;
512   using Elf_Rela = typename ELFT::Rela;
513
514 public:
515   RelocationSection(StringRef name, bool sort);
516   void writeTo(uint8_t *buf) override;
517
518 private:
519   bool sort;
520 };
521
522 template <class ELFT>
523 class AndroidPackedRelocationSection final : public RelocationBaseSection {
524   using Elf_Rel = typename ELFT::Rel;
525   using Elf_Rela = typename ELFT::Rela;
526
527 public:
528   AndroidPackedRelocationSection(StringRef name);
529
530   bool updateAllocSize() override;
531   size_t getSize() const override { return relocData.size(); }
532   void writeTo(uint8_t *buf) override {
533     memcpy(buf, relocData.data(), relocData.size());
534   }
535
536 private:
537   SmallVector<char, 0> relocData;
538 };
539
540 struct RelativeReloc {
541   uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
542
543   const InputSectionBase *inputSec;
544   uint64_t offsetInSec;
545 };
546
547 class RelrBaseSection : public SyntheticSection {
548 public:
549   RelrBaseSection();
550   bool isNeeded() const override { return !relocs.empty(); }
551   std::vector<RelativeReloc> relocs;
552 };
553
554 // RelrSection is used to encode offsets for relative relocations.
555 // Proposal for adding SHT_RELR sections to generic-abi is here:
556 //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
557 // For more details, see the comment in RelrSection::updateAllocSize().
558 template <class ELFT> class RelrSection final : public RelrBaseSection {
559   using Elf_Relr = typename ELFT::Relr;
560
561 public:
562   RelrSection();
563
564   bool updateAllocSize() override;
565   size_t getSize() const override { return relrRelocs.size() * this->entsize; }
566   void writeTo(uint8_t *buf) override {
567     memcpy(buf, relrRelocs.data(), getSize());
568   }
569
570 private:
571   std::vector<Elf_Relr> relrRelocs;
572 };
573
574 struct SymbolTableEntry {
575   Symbol *sym;
576   size_t strTabOffset;
577 };
578
579 class SymbolTableBaseSection : public SyntheticSection {
580 public:
581   SymbolTableBaseSection(StringTableSection &strTabSec);
582   void finalizeContents() override;
583   size_t getSize() const override { return getNumSymbols() * entsize; }
584   void addSymbol(Symbol *sym);
585   unsigned getNumSymbols() const { return symbols.size() + 1; }
586   size_t getSymbolIndex(Symbol *sym);
587   ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
588
589 protected:
590   void sortSymTabSymbols();
591
592   // A vector of symbols and their string table offsets.
593   std::vector<SymbolTableEntry> symbols;
594
595   StringTableSection &strTabSec;
596
597   llvm::once_flag onceFlag;
598   llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
599   llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
600 };
601
602 template <class ELFT>
603 class SymbolTableSection final : public SymbolTableBaseSection {
604   using Elf_Sym = typename ELFT::Sym;
605
606 public:
607   SymbolTableSection(StringTableSection &strTabSec);
608   void writeTo(uint8_t *buf) override;
609 };
610
611 class SymtabShndxSection final : public SyntheticSection {
612 public:
613   SymtabShndxSection();
614
615   void writeTo(uint8_t *buf) override;
616   size_t getSize() const override;
617   bool isNeeded() const override;
618   void finalizeContents() override;
619 };
620
621 // Outputs GNU Hash section. For detailed explanation see:
622 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
623 class GnuHashTableSection final : public SyntheticSection {
624 public:
625   GnuHashTableSection();
626   void finalizeContents() override;
627   void writeTo(uint8_t *buf) override;
628   size_t getSize() const override { return size; }
629
630   // Adds symbols to the hash table.
631   // Sorts the input to satisfy GNU hash section requirements.
632   void addSymbols(std::vector<SymbolTableEntry> &symbols);
633
634 private:
635   // See the comment in writeBloomFilter.
636   enum { Shift2 = 26 };
637
638   void writeBloomFilter(uint8_t *buf);
639   void writeHashTable(uint8_t *buf);
640
641   struct Entry {
642     Symbol *sym;
643     size_t strTabOffset;
644     uint32_t hash;
645     uint32_t bucketIdx;
646   };
647
648   std::vector<Entry> symbols;
649   size_t maskWords;
650   size_t nBuckets = 0;
651   size_t size = 0;
652 };
653
654 class HashTableSection final : public SyntheticSection {
655 public:
656   HashTableSection();
657   void finalizeContents() override;
658   void writeTo(uint8_t *buf) override;
659   size_t getSize() const override { return size; }
660
661 private:
662   size_t size = 0;
663 };
664
665 // The PltSection is used for both the Plt and Iplt. The former usually has a
666 // header as its first entry that is used at run-time to resolve lazy binding.
667 // The latter is used for GNU Ifunc symbols, that will be subject to a
668 // Target->IRelativeRel.
669 class PltSection : public SyntheticSection {
670 public:
671   PltSection(bool isIplt);
672   void writeTo(uint8_t *buf) override;
673   size_t getSize() const override;
674   bool isNeeded() const override { return !entries.empty(); }
675   void addSymbols();
676   template <class ELFT> void addEntry(Symbol &sym);
677
678   size_t headerSize;
679
680 private:
681   std::vector<const Symbol *> entries;
682   bool isIplt;
683 };
684
685 class GdbIndexSection final : public SyntheticSection {
686 public:
687   struct AddressEntry {
688     InputSection *section;
689     uint64_t lowAddress;
690     uint64_t highAddress;
691     uint32_t cuIndex;
692   };
693
694   struct CuEntry {
695     uint64_t cuOffset;
696     uint64_t cuLength;
697   };
698
699   struct NameAttrEntry {
700     llvm::CachedHashStringRef name;
701     uint32_t cuIndexAndAttrs;
702   };
703
704   struct GdbChunk {
705     InputSection *sec;
706     std::vector<AddressEntry> addressAreas;
707     std::vector<CuEntry> compilationUnits;
708   };
709
710   struct GdbSymbol {
711     llvm::CachedHashStringRef name;
712     std::vector<uint32_t> cuVector;
713     uint32_t nameOff;
714     uint32_t cuVectorOff;
715   };
716
717   GdbIndexSection();
718   template <typename ELFT> static GdbIndexSection *create();
719   void writeTo(uint8_t *buf) override;
720   size_t getSize() const override { return size; }
721   bool isNeeded() const override;
722
723 private:
724   struct GdbIndexHeader {
725     llvm::support::ulittle32_t version;
726     llvm::support::ulittle32_t cuListOff;
727     llvm::support::ulittle32_t cuTypesOff;
728     llvm::support::ulittle32_t addressAreaOff;
729     llvm::support::ulittle32_t symtabOff;
730     llvm::support::ulittle32_t constantPoolOff;
731   };
732
733   void initOutputSize();
734   size_t computeSymtabSize() const;
735
736   // Each chunk contains information gathered from debug sections of a
737   // single object file.
738   std::vector<GdbChunk> chunks;
739
740   // A symbol table for this .gdb_index section.
741   std::vector<GdbSymbol> symbols;
742
743   size_t size;
744 };
745
746 // --eh-frame-hdr option tells linker to construct a header for all the
747 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
748 // and also to a PT_GNU_EH_FRAME segment.
749 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
750 // calling dl_iterate_phdr.
751 // This section contains a lookup table for quick binary search of FDEs.
752 // Detailed info about internals can be found in Ian Lance Taylor's blog:
753 // http://www.airs.com/blog/archives/460 (".eh_frame")
754 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
755 class EhFrameHeader final : public SyntheticSection {
756 public:
757   EhFrameHeader();
758   void write();
759   void writeTo(uint8_t *buf) override;
760   size_t getSize() const override;
761   bool isNeeded() const override;
762 };
763
764 // For more information about .gnu.version and .gnu.version_r see:
765 // https://www.akkadia.org/drepper/symbol-versioning
766
767 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
768 // contain symbol version definitions. The number of entries in this section
769 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
770 // The section shall contain an array of Elf_Verdef structures, optionally
771 // followed by an array of Elf_Verdaux structures.
772 class VersionDefinitionSection final : public SyntheticSection {
773 public:
774   VersionDefinitionSection();
775   void finalizeContents() override;
776   size_t getSize() const override;
777   void writeTo(uint8_t *buf) override;
778
779 private:
780   enum { EntrySize = 28 };
781   void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
782   StringRef getFileDefName();
783
784   unsigned fileDefNameOff;
785   std::vector<unsigned> verDefNameOffs;
786 };
787
788 // The .gnu.version section specifies the required version of each symbol in the
789 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
790 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
791 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
792 // The values 0 and 1 are reserved. All other values are used for versions in
793 // the own object or in any of the dependencies.
794 class VersionTableSection final : public SyntheticSection {
795 public:
796   VersionTableSection();
797   void finalizeContents() override;
798   size_t getSize() const override;
799   void writeTo(uint8_t *buf) override;
800   bool isNeeded() const override;
801 };
802
803 // The .gnu.version_r section defines the version identifiers used by
804 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
805 // Elf_Verneed specifies the version requirements for a single DSO, and contains
806 // a reference to a linked list of Elf_Vernaux data structures which define the
807 // mapping from version identifiers to version names.
808 template <class ELFT>
809 class VersionNeedSection final : public SyntheticSection {
810   using Elf_Verneed = typename ELFT::Verneed;
811   using Elf_Vernaux = typename ELFT::Vernaux;
812
813   struct Vernaux {
814     uint64_t hash;
815     uint32_t verneedIndex;
816     uint64_t nameStrTab;
817   };
818
819   struct Verneed {
820     uint64_t nameStrTab;
821     std::vector<Vernaux> vernauxs;
822   };
823
824   std::vector<Verneed> verneeds;
825
826 public:
827   VersionNeedSection();
828   void finalizeContents() override;
829   void writeTo(uint8_t *buf) override;
830   size_t getSize() const override;
831   bool isNeeded() const override;
832 };
833
834 // MergeSyntheticSection is a class that allows us to put mergeable sections
835 // with different attributes in a single output sections. To do that
836 // we put them into MergeSyntheticSection synthetic input sections which are
837 // attached to regular output sections.
838 class MergeSyntheticSection : public SyntheticSection {
839 public:
840   void addSection(MergeInputSection *ms);
841   std::vector<MergeInputSection *> sections;
842
843 protected:
844   MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
845                         uint32_t alignment)
846       : SyntheticSection(flags, type, alignment, name) {}
847 };
848
849 class MergeTailSection final : public MergeSyntheticSection {
850 public:
851   MergeTailSection(StringRef name, uint32_t type, uint64_t flags,
852                    uint32_t alignment);
853
854   size_t getSize() const override;
855   void writeTo(uint8_t *buf) override;
856   void finalizeContents() override;
857
858 private:
859   llvm::StringTableBuilder builder;
860 };
861
862 class MergeNoTailSection final : public MergeSyntheticSection {
863 public:
864   MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags,
865                      uint32_t alignment)
866       : MergeSyntheticSection(name, type, flags, alignment) {}
867
868   size_t getSize() const override { return size; }
869   void writeTo(uint8_t *buf) override;
870   void finalizeContents() override;
871
872 private:
873   // We use the most significant bits of a hash as a shard ID.
874   // The reason why we don't want to use the least significant bits is
875   // because DenseMap also uses lower bits to determine a bucket ID.
876   // If we use lower bits, it significantly increases the probability of
877   // hash collisons.
878   size_t getShardId(uint32_t hash) {
879     assert((hash >> 31) == 0);
880     return hash >> (31 - llvm::countTrailingZeros(numShards));
881   }
882
883   // Section size
884   size_t size;
885
886   // String table contents
887   constexpr static size_t numShards = 32;
888   std::vector<llvm::StringTableBuilder> shards;
889   size_t shardOffsets[numShards];
890 };
891
892 // .MIPS.abiflags section.
893 template <class ELFT>
894 class MipsAbiFlagsSection final : public SyntheticSection {
895   using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
896
897 public:
898   static MipsAbiFlagsSection *create();
899
900   MipsAbiFlagsSection(Elf_Mips_ABIFlags flags);
901   size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
902   void writeTo(uint8_t *buf) override;
903
904 private:
905   Elf_Mips_ABIFlags flags;
906 };
907
908 // .MIPS.options section.
909 template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
910   using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
911   using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
912
913 public:
914   static MipsOptionsSection *create();
915
916   MipsOptionsSection(Elf_Mips_RegInfo reginfo);
917   void writeTo(uint8_t *buf) override;
918
919   size_t getSize() const override {
920     return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
921   }
922
923 private:
924   Elf_Mips_RegInfo reginfo;
925 };
926
927 // MIPS .reginfo section.
928 template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
929   using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
930
931 public:
932   static MipsReginfoSection *create();
933
934   MipsReginfoSection(Elf_Mips_RegInfo reginfo);
935   size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
936   void writeTo(uint8_t *buf) override;
937
938 private:
939   Elf_Mips_RegInfo reginfo;
940 };
941
942 // This is a MIPS specific section to hold a space within the data segment
943 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
944 // See "Dynamic section" in Chapter 5 in the following document:
945 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
946 class MipsRldMapSection : public SyntheticSection {
947 public:
948   MipsRldMapSection();
949   size_t getSize() const override { return config->wordsize; }
950   void writeTo(uint8_t *buf) override {}
951 };
952
953 // Representation of the combined .ARM.Exidx input sections. We process these
954 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries
955 // and add terminating sentinel entries.
956 //
957 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
958 // a table that the unwinder can derive (Addresses are encoded as offsets from
959 // table):
960 // | Address of function | Unwind instructions for function |
961 // where the unwind instructions are either a small number of unwind or the
962 // special EXIDX_CANTUNWIND entry representing no unwinding information.
963 // When an exception is thrown from an address A, the unwinder searches the
964 // table for the closest table entry with Address of function <= A. This means
965 // that for two consecutive table entries:
966 // | A1 | U1 |
967 // | A2 | U2 |
968 // The range of addresses described by U1 is [A1, A2)
969 //
970 // There are two cases where we need a linker generated table entry to fixup
971 // the address ranges in the table
972 // Case 1:
973 // - A sentinel entry added with an address higher than all
974 // executable sections. This was needed to work around libunwind bug pr31091.
975 // - After address assignment we need to find the highest addressed executable
976 // section and use the limit of that section so that the unwinder never
977 // matches it.
978 // Case 2:
979 // - InputSections without a .ARM.exidx section (usually from Assembly)
980 // need a table entry so that they terminate the range of the previously
981 // function. This is pr40277.
982 //
983 // Instead of storing pointers to the .ARM.exidx InputSections from
984 // InputObjects, we store pointers to the executable sections that need
985 // .ARM.exidx sections. We can then use the dependentSections of these to
986 // either find the .ARM.exidx section or know that we need to generate one.
987 class ARMExidxSyntheticSection : public SyntheticSection {
988 public:
989   ARMExidxSyntheticSection();
990
991   // Add an input section to the ARMExidxSyntheticSection. Returns whether the
992   // section needs to be removed from the main input section list.
993   bool addSection(InputSection *isec);
994
995   size_t getSize() const override { return size; }
996   void writeTo(uint8_t *buf) override;
997   bool isNeeded() const override;
998   // Sort and remove duplicate entries.
999   void finalizeContents() override;
1000   InputSection *getLinkOrderDep() const;
1001
1002   static bool classof(const SectionBase *d);
1003
1004   // Links to the ARMExidxSections so we can transfer the relocations once the
1005   // layout is known.
1006   std::vector<InputSection *> exidxSections;
1007
1008 private:
1009   size_t size;
1010
1011   // Instead of storing pointers to the .ARM.exidx InputSections from
1012   // InputObjects, we store pointers to the executable sections that need
1013   // .ARM.exidx sections. We can then use the dependentSections of these to
1014   // either find the .ARM.exidx section or know that we need to generate one.
1015   std::vector<InputSection *> executableSections;
1016
1017   // The executable InputSection with the highest address to use for the
1018   // sentinel. We store separately from ExecutableSections as merging of
1019   // duplicate entries may mean this InputSection is removed from
1020   // ExecutableSections.
1021   InputSection *sentinel = nullptr;
1022 };
1023
1024 // A container for one or more linker generated thunks. Instances of these
1025 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
1026 class ThunkSection : public SyntheticSection {
1027 public:
1028   // ThunkSection in OS, with desired outSecOff of Off
1029   ThunkSection(OutputSection *os, uint64_t off);
1030
1031   // Add a newly created Thunk to this container:
1032   // Thunk is given offset from start of this InputSection
1033   // Thunk defines a symbol in this InputSection that can be used as target
1034   // of a relocation
1035   void addThunk(Thunk *t);
1036   size_t getSize() const override { return size; }
1037   void writeTo(uint8_t *buf) override;
1038   InputSection *getTargetInputSection() const;
1039   bool assignOffsets();
1040
1041 private:
1042   std::vector<Thunk *> thunks;
1043   size_t size = 0;
1044 };
1045
1046 // Used to compute outSecOff of .got2 in each object file. This is needed to
1047 // synthesize PLT entries for PPC32 Secure PLT ABI.
1048 class PPC32Got2Section final : public SyntheticSection {
1049 public:
1050   PPC32Got2Section();
1051   size_t getSize() const override { return 0; }
1052   bool isNeeded() const override;
1053   void finalizeContents() override;
1054   void writeTo(uint8_t *buf) override {}
1055 };
1056
1057 // This section is used to store the addresses of functions that are called
1058 // in range-extending thunks on PowerPC64. When producing position dependant
1059 // code the addresses are link-time constants and the table is written out to
1060 // the binary. When producing position-dependant code the table is allocated and
1061 // filled in by the dynamic linker.
1062 class PPC64LongBranchTargetSection final : public SyntheticSection {
1063 public:
1064   PPC64LongBranchTargetSection();
1065   void addEntry(Symbol &sym);
1066   size_t getSize() const override;
1067   void writeTo(uint8_t *buf) override;
1068   bool isNeeded() const override;
1069   void finalizeContents() override { finalized = true; }
1070
1071 private:
1072   std::vector<const Symbol *> entries;
1073   bool finalized = false;
1074 };
1075
1076 template <typename ELFT>
1077 class PartitionElfHeaderSection : public SyntheticSection {
1078 public:
1079   PartitionElfHeaderSection();
1080   size_t getSize() const override;
1081   void writeTo(uint8_t *buf) override;
1082 };
1083
1084 template <typename ELFT>
1085 class PartitionProgramHeadersSection : public SyntheticSection {
1086 public:
1087   PartitionProgramHeadersSection();
1088   size_t getSize() const override;
1089   void writeTo(uint8_t *buf) override;
1090 };
1091
1092 class PartitionIndexSection : public SyntheticSection {
1093 public:
1094   PartitionIndexSection();
1095   size_t getSize() const override;
1096   void finalizeContents() override;
1097   void writeTo(uint8_t *buf) override;
1098 };
1099
1100 InputSection *createInterpSection();
1101 MergeInputSection *createCommentSection();
1102 MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type,
1103                                             uint64_t flags, uint32_t alignment);
1104 template <class ELFT> void splitSections();
1105
1106 template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
1107 template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
1108
1109 Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
1110                            uint64_t size, InputSectionBase &section);
1111
1112 void addVerneed(Symbol *ss);
1113
1114 // Linker generated per-partition sections.
1115 struct Partition {
1116   StringRef name;
1117   uint64_t nameStrTab;
1118
1119   SyntheticSection *elfHeader;
1120   SyntheticSection *programHeaders;
1121   std::vector<PhdrEntry *> phdrs;
1122
1123   ARMExidxSyntheticSection *armExidx;
1124   BuildIdSection *buildId;
1125   SyntheticSection *dynamic;
1126   StringTableSection *dynStrTab;
1127   SymbolTableBaseSection *dynSymTab;
1128   EhFrameHeader *ehFrameHdr;
1129   EhFrameSection *ehFrame;
1130   GnuHashTableSection *gnuHashTab;
1131   HashTableSection *hashTab;
1132   RelocationBaseSection *relaDyn;
1133   RelrBaseSection *relrDyn;
1134   VersionDefinitionSection *verDef;
1135   SyntheticSection *verNeed;
1136   VersionTableSection *verSym;
1137
1138   unsigned getNumber() const { return this - &partitions[0] + 1; }
1139 };
1140
1141 extern Partition *mainPart;
1142
1143 inline Partition &SectionBase::getPartition() const {
1144   assert(isLive());
1145   return partitions[partition - 1];
1146 }
1147
1148 // Linker generated sections which can be used as inputs and are not specific to
1149 // a partition.
1150 struct InStruct {
1151   InputSection *armAttributes;
1152   BssSection *bss;
1153   BssSection *bssRelRo;
1154   GotSection *got;
1155   GotPltSection *gotPlt;
1156   IgotPltSection *igotPlt;
1157   PPC64LongBranchTargetSection *ppc64LongBranchTarget;
1158   MipsGotSection *mipsGot;
1159   MipsRldMapSection *mipsRldMap;
1160   SyntheticSection *partEnd;
1161   SyntheticSection *partIndex;
1162   PltSection *plt;
1163   PltSection *iplt;
1164   PPC32Got2Section *ppc32Got2;
1165   RelocationBaseSection *relaPlt;
1166   RelocationBaseSection *relaIplt;
1167   StringTableSection *shStrTab;
1168   StringTableSection *strTab;
1169   SymbolTableBaseSection *symTab;
1170   SymtabShndxSection *symTabShndx;
1171 };
1172
1173 extern InStruct in;
1174
1175 } // namespace elf
1176 } // namespace lld
1177
1178 #endif