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