]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/OutputSections.h
Import mandoc 1.4.1rc2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / OutputSections.h
1 //===- OutputSections.h -----------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_OUTPUT_SECTIONS_H
11 #define LLD_ELF_OUTPUT_SECTIONS_H
12
13 #include "Config.h"
14 #include "Relocations.h"
15
16 #include "lld/Core/LLVM.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/SHA1.h"
22
23 namespace lld {
24 namespace elf {
25
26 class SymbolBody;
27 struct SectionPiece;
28 template <class ELFT> class SymbolTable;
29 template <class ELFT> class SymbolTableSection;
30 template <class ELFT> class StringTableSection;
31 template <class ELFT> class EhInputSection;
32 template <class ELFT> class InputSection;
33 template <class ELFT> class InputSectionBase;
34 template <class ELFT> class MergeInputSection;
35 template <class ELFT> class MipsReginfoInputSection;
36 template <class ELFT> class OutputSection;
37 template <class ELFT> class ObjectFile;
38 template <class ELFT> class SharedFile;
39 template <class ELFT> class SharedSymbol;
40 template <class ELFT> class DefinedRegular;
41
42 // This represents a section in an output file.
43 // Different sub classes represent different types of sections. Some contain
44 // input sections, others are created by the linker.
45 // The writer creates multiple OutputSections and assign them unique,
46 // non-overlapping file offsets and VAs.
47 template <class ELFT> class OutputSectionBase {
48 public:
49   typedef typename ELFT::uint uintX_t;
50   typedef typename ELFT::Shdr Elf_Shdr;
51
52   OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
53   void setVA(uintX_t VA) { Header.sh_addr = VA; }
54   uintX_t getVA() const { return Header.sh_addr; }
55   void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
56   void setSHName(unsigned Val) { Header.sh_name = Val; }
57   void writeHeaderTo(Elf_Shdr *SHdr);
58   StringRef getName() { return Name; }
59
60   virtual void addSection(InputSectionBase<ELFT> *C) {}
61
62   unsigned SectionIndex;
63
64   // Returns the size of the section in the output file.
65   uintX_t getSize() const { return Header.sh_size; }
66   void setSize(uintX_t Val) { Header.sh_size = Val; }
67   uintX_t getFlags() const { return Header.sh_flags; }
68   uintX_t getFileOff() const { return Header.sh_offset; }
69   uintX_t getAlignment() const { return Header.sh_addralign; }
70   uint32_t getType() const { return Header.sh_type; }
71
72   void updateAlignment(uintX_t Alignment) {
73     if (Alignment > Header.sh_addralign)
74       Header.sh_addralign = Alignment;
75   }
76
77   // If true, this section will be page aligned on disk.
78   // Typically the first section of each PT_LOAD segment has this flag.
79   bool PageAlign = false;
80
81   virtual void finalize() {}
82   virtual void finalizePieces() {}
83   virtual void assignOffsets() {}
84   virtual void writeTo(uint8_t *Buf) {}
85   virtual ~OutputSectionBase() = default;
86
87 protected:
88   StringRef Name;
89   Elf_Shdr Header;
90 };
91
92 template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
93   typedef OutputSectionBase<ELFT> Base;
94   typedef typename ELFT::uint uintX_t;
95
96 public:
97   GotSection();
98   void finalize() override;
99   void writeTo(uint8_t *Buf) override;
100   void addEntry(SymbolBody &Sym);
101   void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
102   bool addDynTlsEntry(SymbolBody &Sym);
103   bool addTlsIndex();
104   bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
105   uintX_t getMipsLocalPageOffset(uintX_t Addr);
106   uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
107   uintX_t getGlobalDynAddr(const SymbolBody &B) const;
108   uintX_t getGlobalDynOffset(const SymbolBody &B) const;
109   uintX_t getNumEntries() const { return Entries.size(); }
110
111   // Returns the symbol which corresponds to the first entry of the global part
112   // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
113   // table properties.
114   // Returns nullptr if the global part is empty.
115   const SymbolBody *getMipsFirstGlobalEntry() const;
116
117   // Returns the number of entries in the local part of GOT including
118   // the number of reserved entries. This method is MIPS-specific.
119   unsigned getMipsLocalEntriesNum() const;
120
121   // Returns offset of TLS part of the MIPS GOT table. This part goes
122   // after 'local' and 'global' entries.
123   uintX_t getMipsTlsOffset();
124
125   uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
126   uint32_t getTlsIndexOff() { return TlsIndexOff; }
127
128   // Flag to force GOT to be in output if we have relocations
129   // that relies on its address.
130   bool HasGotOffRel = false;
131
132 private:
133   std::vector<const SymbolBody *> Entries;
134   uint32_t TlsIndexOff = -1;
135   uint32_t MipsPageEntries = 0;
136   // Output sections referenced by MIPS GOT relocations.
137   llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
138   llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
139
140   // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
141   // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
142   // or `MipsGlobal` vectors. In general it does not have a sence to take in
143   // account addend for preemptible symbols because the corresponding
144   // GOT entries should have one-to-one mapping with dynamic symbols table.
145   // But we use the same container's types for both kind of GOT entries
146   // to handle them uniformly.
147   typedef std::pair<const SymbolBody*, uintX_t> MipsGotEntry;
148   typedef std::vector<MipsGotEntry> MipsGotEntries;
149   llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
150   MipsGotEntries MipsLocal;
151   MipsGotEntries MipsGlobal;
152
153   // Write MIPS-specific parts of the GOT.
154   void writeMipsGot(uint8_t *&Buf);
155 };
156
157 template <class ELFT>
158 class GotPltSection final : public OutputSectionBase<ELFT> {
159   typedef typename ELFT::uint uintX_t;
160
161 public:
162   GotPltSection();
163   void finalize() override;
164   void writeTo(uint8_t *Buf) override;
165   void addEntry(SymbolBody &Sym);
166   bool empty() const;
167
168 private:
169   std::vector<const SymbolBody *> Entries;
170 };
171
172 template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
173   typedef OutputSectionBase<ELFT> Base;
174   typedef typename ELFT::uint uintX_t;
175
176 public:
177   PltSection();
178   void finalize() override;
179   void writeTo(uint8_t *Buf) override;
180   void addEntry(SymbolBody &Sym);
181   bool empty() const { return Entries.empty(); }
182
183 private:
184   std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
185 };
186
187 template <class ELFT> class DynamicReloc {
188   typedef typename ELFT::uint uintX_t;
189
190 public:
191   DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
192                uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
193                uintX_t Addend)
194       : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
195         UseSymVA(UseSymVA), Addend(Addend) {}
196
197   DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
198                uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
199                uintX_t Addend)
200       : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
201         UseSymVA(UseSymVA), Addend(Addend) {}
202
203   uintX_t getOffset() const;
204   uintX_t getAddend() const;
205   uint32_t getSymIndex() const;
206   const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
207
208   uint32_t Type;
209
210 private:
211   SymbolBody *Sym;
212   const InputSectionBase<ELFT> *InputSec = nullptr;
213   const OutputSectionBase<ELFT> *OutputSec = nullptr;
214   uintX_t OffsetInSec;
215   bool UseSymVA;
216   uintX_t Addend;
217 };
218
219 template <class ELFT>
220 class SymbolTableSection final : public OutputSectionBase<ELFT> {
221 public:
222   typedef typename ELFT::Shdr Elf_Shdr;
223   typedef typename ELFT::Sym Elf_Sym;
224   typedef typename ELFT::SymRange Elf_Sym_Range;
225   typedef typename ELFT::uint uintX_t;
226   SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
227
228   void finalize() override;
229   void writeTo(uint8_t *Buf) override;
230   void addSymbol(SymbolBody *Body);
231   StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
232   unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
233
234   ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
235     return Symbols;
236   }
237
238   unsigned NumLocals = 0;
239   StringTableSection<ELFT> &StrTabSec;
240
241 private:
242   void writeLocalSymbols(uint8_t *&Buf);
243   void writeGlobalSymbols(uint8_t *Buf);
244
245   const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
246
247   // A vector of symbols and their string table offsets.
248   std::vector<std::pair<SymbolBody *, size_t>> Symbols;
249 };
250
251 // For more information about .gnu.version and .gnu.version_r see:
252 // https://www.akkadia.org/drepper/symbol-versioning
253
254 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
255 // contain symbol version definitions. The number of entries in this section
256 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
257 // The section shall contain an array of Elf_Verdef structures, optionally
258 // followed by an array of Elf_Verdaux structures.
259 template <class ELFT>
260 class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
261   typedef typename ELFT::Verdef Elf_Verdef;
262   typedef typename ELFT::Verdaux Elf_Verdaux;
263
264 public:
265   VersionDefinitionSection();
266   void finalize() override;
267   void writeTo(uint8_t *Buf) override;
268
269 private:
270   void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
271
272   unsigned FileDefNameOff;
273 };
274
275 // The .gnu.version section specifies the required version of each symbol in the
276 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
277 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version
278 // identifier defined in the either .gnu.version_r or .gnu.version_d section.
279 // The values 0 and 1 are reserved. All other values are used for versions in
280 // the own object or in any of the dependencies.
281 template <class ELFT>
282 class VersionTableSection final : public OutputSectionBase<ELFT> {
283   typedef typename ELFT::Versym Elf_Versym;
284
285 public:
286   VersionTableSection();
287   void finalize() override;
288   void writeTo(uint8_t *Buf) override;
289 };
290
291 // The .gnu.version_r section defines the version identifiers used by
292 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
293 // Elf_Verneed specifies the version requirements for a single DSO, and contains
294 // a reference to a linked list of Elf_Vernaux data structures which define the
295 // mapping from version identifiers to version names.
296 template <class ELFT>
297 class VersionNeedSection final : public OutputSectionBase<ELFT> {
298   typedef typename ELFT::Verneed Elf_Verneed;
299   typedef typename ELFT::Vernaux Elf_Vernaux;
300
301   // A vector of shared files that need Elf_Verneed data structures and the
302   // string table offsets of their sonames.
303   std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
304
305   // The next available version identifier.
306   unsigned NextIndex;
307
308 public:
309   VersionNeedSection();
310   void addSymbol(SharedSymbol<ELFT> *SS);
311   void finalize() override;
312   void writeTo(uint8_t *Buf) override;
313   size_t getNeedNum() const { return Needed.size(); }
314 };
315
316 template <class ELFT>
317 class RelocationSection final : public OutputSectionBase<ELFT> {
318   typedef typename ELFT::Rel Elf_Rel;
319   typedef typename ELFT::Rela Elf_Rela;
320   typedef typename ELFT::uint uintX_t;
321
322 public:
323   RelocationSection(StringRef Name, bool Sort);
324   void addReloc(const DynamicReloc<ELFT> &Reloc);
325   unsigned getRelocOffset();
326   void finalize() override;
327   void writeTo(uint8_t *Buf) override;
328   bool hasRelocs() const { return !Relocs.empty(); }
329
330   bool Static = false;
331
332 private:
333   bool Sort;
334   std::vector<DynamicReloc<ELFT>> Relocs;
335 };
336
337 template <class ELFT>
338 class OutputSection final : public OutputSectionBase<ELFT> {
339 public:
340   typedef typename ELFT::Shdr Elf_Shdr;
341   typedef typename ELFT::Sym Elf_Sym;
342   typedef typename ELFT::Rel Elf_Rel;
343   typedef typename ELFT::Rela Elf_Rela;
344   typedef typename ELFT::uint uintX_t;
345   OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
346   void addSection(InputSectionBase<ELFT> *C) override;
347   void sortInitFini();
348   void sortCtorsDtors();
349   void writeTo(uint8_t *Buf) override;
350   void finalize() override;
351   void assignOffsets() override;
352   std::vector<InputSection<ELFT> *> Sections;
353 };
354
355 template <class ELFT>
356 class MergeOutputSection final : public OutputSectionBase<ELFT> {
357   typedef typename ELFT::uint uintX_t;
358
359 public:
360   MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
361                      uintX_t Alignment);
362   void addSection(InputSectionBase<ELFT> *S) override;
363   void writeTo(uint8_t *Buf) override;
364   unsigned getOffset(StringRef Val);
365   void finalize() override;
366   void finalizePieces() override;
367   bool shouldTailMerge() const;
368
369 private:
370   llvm::StringTableBuilder Builder;
371   std::vector<MergeInputSection<ELFT> *> Sections;
372 };
373
374 struct CieRecord {
375   SectionPiece *Piece = nullptr;
376   std::vector<SectionPiece *> FdePieces;
377 };
378
379 // Output section for .eh_frame.
380 template <class ELFT>
381 class EhOutputSection final : public OutputSectionBase<ELFT> {
382   typedef typename ELFT::uint uintX_t;
383   typedef typename ELFT::Shdr Elf_Shdr;
384   typedef typename ELFT::Rel Elf_Rel;
385   typedef typename ELFT::Rela Elf_Rela;
386
387 public:
388   EhOutputSection();
389   void writeTo(uint8_t *Buf) override;
390   void finalize() override;
391   bool empty() const { return Sections.empty(); }
392
393   void addSection(InputSectionBase<ELFT> *S) override;
394
395   size_t NumFdes = 0;
396
397 private:
398   template <class RelTy>
399   void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
400
401   template <class RelTy>
402   CieRecord *addCie(SectionPiece &Piece, EhInputSection<ELFT> *Sec,
403                     ArrayRef<RelTy> &Rels);
404
405   template <class RelTy>
406   bool isFdeLive(SectionPiece &Piece, EhInputSection<ELFT> *Sec,
407                  ArrayRef<RelTy> &Rels);
408
409   uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
410
411   std::vector<EhInputSection<ELFT> *> Sections;
412   std::vector<CieRecord *> Cies;
413
414   // CIE records are uniquified by their contents and personality functions.
415   llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
416 };
417
418 template <class ELFT>
419 class InterpSection final : public OutputSectionBase<ELFT> {
420 public:
421   InterpSection();
422   void writeTo(uint8_t *Buf) override;
423 };
424
425 template <class ELFT>
426 class StringTableSection final : public OutputSectionBase<ELFT> {
427 public:
428   typedef typename ELFT::uint uintX_t;
429   StringTableSection(StringRef Name, bool Dynamic);
430   unsigned addString(StringRef S, bool HashIt = true);
431   void writeTo(uint8_t *Buf) override;
432   unsigned getSize() const { return Size; }
433   void finalize() override { this->Header.sh_size = getSize(); }
434   bool isDynamic() const { return Dynamic; }
435
436 private:
437   const bool Dynamic;
438   llvm::DenseMap<StringRef, unsigned> StringMap;
439   std::vector<StringRef> Strings;
440   unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
441 };
442
443 template <class ELFT>
444 class HashTableSection final : public OutputSectionBase<ELFT> {
445   typedef typename ELFT::Word Elf_Word;
446
447 public:
448   HashTableSection();
449   void finalize() override;
450   void writeTo(uint8_t *Buf) override;
451 };
452
453 // Outputs GNU Hash section. For detailed explanation see:
454 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
455 template <class ELFT>
456 class GnuHashTableSection final : public OutputSectionBase<ELFT> {
457   typedef typename ELFT::Off Elf_Off;
458   typedef typename ELFT::Word Elf_Word;
459   typedef typename ELFT::uint uintX_t;
460
461 public:
462   GnuHashTableSection();
463   void finalize() override;
464   void writeTo(uint8_t *Buf) override;
465
466   // Adds symbols to the hash table.
467   // Sorts the input to satisfy GNU hash section requirements.
468   void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
469
470 private:
471   static unsigned calcNBuckets(unsigned NumHashed);
472   static unsigned calcMaskWords(unsigned NumHashed);
473
474   void writeHeader(uint8_t *&Buf);
475   void writeBloomFilter(uint8_t *&Buf);
476   void writeHashTable(uint8_t *Buf);
477
478   struct SymbolData {
479     SymbolBody *Body;
480     size_t STName;
481     uint32_t Hash;
482   };
483
484   std::vector<SymbolData> Symbols;
485
486   unsigned MaskWords;
487   unsigned NBuckets;
488   unsigned Shift2;
489 };
490
491 template <class ELFT>
492 class DynamicSection final : public OutputSectionBase<ELFT> {
493   typedef OutputSectionBase<ELFT> Base;
494   typedef typename ELFT::Dyn Elf_Dyn;
495   typedef typename ELFT::Rel Elf_Rel;
496   typedef typename ELFT::Rela Elf_Rela;
497   typedef typename ELFT::Shdr Elf_Shdr;
498   typedef typename ELFT::Sym Elf_Sym;
499   typedef typename ELFT::uint uintX_t;
500
501   // The .dynamic section contains information for the dynamic linker.
502   // The section consists of fixed size entries, which consist of
503   // type and value fields. Value are one of plain integers, symbol
504   // addresses, or section addresses. This struct represents the entry.
505   struct Entry {
506     int32_t Tag;
507     union {
508       OutputSectionBase<ELFT> *OutSec;
509       uint64_t Val;
510       const SymbolBody *Sym;
511     };
512     enum KindT { SecAddr, SymAddr, PlainInt } Kind;
513     Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
514         : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
515     Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
516     Entry(int32_t Tag, const SymbolBody *Sym)
517         : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
518   };
519
520   // finalize() fills this vector with the section contents. finalize()
521   // cannot directly create final section contents because when the
522   // function is called, symbol or section addresses are not fixed yet.
523   std::vector<Entry> Entries;
524
525 public:
526   explicit DynamicSection();
527   void finalize() override;
528   void writeTo(uint8_t *Buf) override;
529
530   OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
531   OutputSectionBase<ELFT> *InitArraySec = nullptr;
532   OutputSectionBase<ELFT> *FiniArraySec = nullptr;
533 };
534
535 template <class ELFT>
536 class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
537   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
538
539 public:
540   MipsReginfoOutputSection();
541   void writeTo(uint8_t *Buf) override;
542   void addSection(InputSectionBase<ELFT> *S) override;
543
544 private:
545   uint32_t GprMask = 0;
546 };
547
548 template <class ELFT>
549 class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
550   typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
551   typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
552
553 public:
554   MipsOptionsOutputSection();
555   void writeTo(uint8_t *Buf) override;
556   void addSection(InputSectionBase<ELFT> *S) override;
557
558 private:
559   uint32_t GprMask = 0;
560 };
561
562 // --eh-frame-hdr option tells linker to construct a header for all the
563 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
564 // and also to a PT_GNU_EH_FRAME segment.
565 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
566 // calling dl_iterate_phdr.
567 // This section contains a lookup table for quick binary search of FDEs.
568 // Detailed info about internals can be found in Ian Lance Taylor's blog:
569 // http://www.airs.com/blog/archives/460 (".eh_frame")
570 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
571 template <class ELFT>
572 class EhFrameHeader final : public OutputSectionBase<ELFT> {
573   typedef typename ELFT::uint uintX_t;
574
575 public:
576   EhFrameHeader();
577   void finalize() override;
578   void writeTo(uint8_t *Buf) override;
579   void addFde(uint32_t Pc, uint32_t FdeVA);
580
581 private:
582   struct FdeData {
583     uint32_t Pc;
584     uint32_t FdeVA;
585   };
586
587   std::vector<FdeData> Fdes;
588 };
589
590 template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
591 public:
592   void writeTo(uint8_t *Buf) override;
593   virtual void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) = 0;
594
595 protected:
596   BuildIdSection(size_t HashSize);
597   size_t HashSize;
598   uint8_t *HashBuf = nullptr;
599 };
600
601 template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
602 public:
603   BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
604   void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
605 };
606
607 template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
608 public:
609   BuildIdMd5() : BuildIdSection<ELFT>(16) {}
610   void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
611 };
612
613 template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
614 public:
615   BuildIdSha1() : BuildIdSection<ELFT>(20) {}
616   void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
617 };
618
619 template <class ELFT>
620 class BuildIdHexstring final : public BuildIdSection<ELFT> {
621 public:
622   BuildIdHexstring();
623   void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
624 };
625
626 // All output sections that are hadnled by the linker specially are
627 // globally accessible. Writer initializes them, so don't use them
628 // until Writer is initialized.
629 template <class ELFT> struct Out {
630   typedef typename ELFT::uint uintX_t;
631   typedef typename ELFT::Phdr Elf_Phdr;
632   static BuildIdSection<ELFT> *BuildId;
633   static DynamicSection<ELFT> *Dynamic;
634   static EhFrameHeader<ELFT> *EhFrameHdr;
635   static EhOutputSection<ELFT> *EhFrame;
636   static GnuHashTableSection<ELFT> *GnuHashTab;
637   static GotPltSection<ELFT> *GotPlt;
638   static GotSection<ELFT> *Got;
639   static HashTableSection<ELFT> *HashTab;
640   static InterpSection<ELFT> *Interp;
641   static OutputSection<ELFT> *Bss;
642   static OutputSection<ELFT> *MipsRldMap;
643   static OutputSectionBase<ELFT> *Opd;
644   static uint8_t *OpdBuf;
645   static PltSection<ELFT> *Plt;
646   static RelocationSection<ELFT> *RelaDyn;
647   static RelocationSection<ELFT> *RelaPlt;
648   static StringTableSection<ELFT> *DynStrTab;
649   static StringTableSection<ELFT> *ShStrTab;
650   static StringTableSection<ELFT> *StrTab;
651   static SymbolTableSection<ELFT> *DynSymTab;
652   static SymbolTableSection<ELFT> *SymTab;
653   static VersionDefinitionSection<ELFT> *VerDef;
654   static VersionTableSection<ELFT> *VerSym;
655   static VersionNeedSection<ELFT> *VerNeed;
656   static Elf_Phdr *TlsPhdr;
657   static OutputSectionBase<ELFT> *ElfHeader;
658   static OutputSectionBase<ELFT> *ProgramHeaders;
659 };
660
661 template <bool Is64Bits> struct SectionKey {
662   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
663   StringRef Name;
664   uint32_t Type;
665   uintX_t Flags;
666   uintX_t Alignment;
667 };
668
669 // This class knows how to create an output section for a given
670 // input section. Output section type is determined by various
671 // factors, including input section's sh_flags, sh_type and
672 // linker scripts.
673 template <class ELFT> class OutputSectionFactory {
674   typedef typename ELFT::Shdr Elf_Shdr;
675   typedef typename ELFT::uint uintX_t;
676   typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
677
678 public:
679   std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
680                                                     StringRef OutsecName);
681
682   OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type, uintX_t Flags);
683
684 private:
685   Key createKey(InputSectionBase<ELFT> *C, StringRef OutsecName);
686
687   llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
688 };
689
690 template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
691 template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
692 template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
693 template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
694 template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
695 template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
696 template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
697 template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
698 template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
699 template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
700 template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
701 template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
702 template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
703 template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
704 template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
705 template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
706 template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
707 template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
708 template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
709 template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
710 template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
711 template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
712 template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
713 template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
714 template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
715 template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
716 template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
717
718 } // namespace elf
719 } // namespace lld
720
721 namespace llvm {
722 template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
723   typedef typename lld::elf::SectionKey<Is64Bits> Key;
724
725   static Key getEmptyKey();
726   static Key getTombstoneKey();
727   static unsigned getHashValue(const Key &Val);
728   static bool isEqual(const Key &LHS, const Key &RHS);
729 };
730 }
731
732 #endif