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