Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 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 | |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 13 | #include "Config.h" |
| 14 | |
Rui Ueyama | a0752a5 | 2016-03-13 20:28:29 +0000 | [diff] [blame] | 15 | #include "lld/Core/LLVM.h" |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Rui Ueyama | a0752a5 | 2016-03-13 20:28:29 +0000 | [diff] [blame] | 17 | #include "llvm/MC/StringTableBuilder.h" |
| 18 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 19 | |
| 20 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 21 | namespace elf { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 22 | |
| 23 | class SymbolBody; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 24 | template <class ELFT> class SymbolTable; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 25 | template <class ELFT> class SymbolTableSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 26 | template <class ELFT> class StringTableSection; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 27 | template <class ELFT> class EHInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 28 | template <class ELFT> class InputSection; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 29 | template <class ELFT> class InputSectionBase; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 30 | template <class ELFT> class MergeInputSection; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 31 | template <class ELFT> class MipsReginfoInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 32 | template <class ELFT> class OutputSection; |
| 33 | template <class ELFT> class ObjectFile; |
| 34 | template <class ELFT> class DefinedRegular; |
| 35 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 36 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 37 | static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) { |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 40 | |
| 41 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 42 | static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) { |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 43 | return Rel.r_addend; |
| 44 | } |
| 45 | |
George Rimar | 12737b7 | 2016-02-25 08:40:26 +0000 | [diff] [blame] | 46 | bool isValidCIdentifier(StringRef S); |
| 47 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 48 | // This represents a section in an output file. |
| 49 | // Different sub classes represent different types of sections. Some contain |
| 50 | // input sections, others are created by the linker. |
| 51 | // The writer creates multiple OutputSections and assign them unique, |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 52 | // non-overlapping file offsets and VAs. |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 53 | template <class ELFT> class OutputSectionBase { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 54 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 55 | typedef typename ELFT::uint uintX_t; |
| 56 | typedef typename ELFT::Shdr Elf_Shdr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 57 | |
George Rimar | 9bec24a | 2016-02-18 14:20:08 +0000 | [diff] [blame] | 58 | OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 59 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
| 60 | uintX_t getVA() const { return Header.sh_addr; } |
| 61 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 62 | void setSHName(unsigned Val) { Header.sh_name = Val; } |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 63 | void writeHeaderTo(Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 64 | StringRef getName() { return Name; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 65 | |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 66 | virtual void addSection(InputSectionBase<ELFT> *C) {} |
| 67 | |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 68 | unsigned SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 69 | |
| 70 | // Returns the size of the section in the output file. |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 71 | uintX_t getSize() const { return Header.sh_size; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 72 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
| 73 | uintX_t getFlags() { return Header.sh_flags; } |
| 74 | uintX_t getFileOff() { return Header.sh_offset; } |
| 75 | uintX_t getAlign() { |
| 76 | // The ELF spec states that a value of 0 means the section has no alignment |
| 77 | // constraits. |
| 78 | return std::max<uintX_t>(Header.sh_addralign, 1); |
| 79 | } |
| 80 | uint32_t getType() { return Header.sh_type; } |
Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 81 | void updateAlign(uintX_t Align) { |
| 82 | if (Align > Header.sh_addralign) |
| 83 | Header.sh_addralign = Align; |
| 84 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 86 | // If true, this section will be page aligned on disk. |
| 87 | // Typically the first section of each PT_LOAD segment has this flag. |
| 88 | bool PageAlign = false; |
| 89 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 90 | virtual void finalize() {} |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 91 | virtual void |
| 92 | forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {} |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 93 | virtual void writeTo(uint8_t *Buf) {} |
Rui Ueyama | d4ea7dd | 2015-12-26 07:01:26 +0000 | [diff] [blame] | 94 | virtual ~OutputSectionBase() = default; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | StringRef Name; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 98 | Elf_Shdr Header; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 101 | template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> { |
| 102 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 103 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 104 | |
| 105 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 106 | GotSection(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 107 | void finalize() override; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 108 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 109 | void addEntry(SymbolBody &Sym); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 110 | bool addDynTlsEntry(SymbolBody &Sym); |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 111 | bool addTlsIndex(); |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 112 | bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); } |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 113 | uintX_t getMipsLocalFullAddr(const SymbolBody &B); |
| 114 | uintX_t getMipsLocalPageAddr(uintX_t Addr); |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 115 | uintX_t getGlobalDynAddr(const SymbolBody &B) const; |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 116 | uintX_t getNumEntries() const { return Entries.size(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 117 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 118 | // Returns the symbol which corresponds to the first entry of the global part |
| 119 | // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic |
| 120 | // table properties. |
| 121 | // Returns nullptr if the global part is empty. |
| 122 | const SymbolBody *getMipsFirstGlobalEntry() const; |
| 123 | |
| 124 | // Returns the number of entries in the local part of GOT including |
| 125 | // the number of reserved entries. This method is MIPS-specific. |
| 126 | unsigned getMipsLocalEntriesNum() const; |
| 127 | |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 128 | uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; } |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 130 | private: |
| 131 | std::vector<const SymbolBody *> Entries; |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 132 | uint32_t TlsIndexOff = -1; |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 133 | uint32_t MipsLocalEntries = 0; |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 134 | // Output sections referenced by MIPS GOT relocations. |
| 135 | llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections; |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 136 | llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos; |
| 137 | |
| 138 | uintX_t getMipsLocalEntryAddr(uintX_t EntryValue); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 141 | template <class ELFT> |
| 142 | class GotPltSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 143 | typedef typename ELFT::uint uintX_t; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 144 | |
| 145 | public: |
| 146 | GotPltSection(); |
| 147 | void finalize() override; |
| 148 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 149 | void addEntry(SymbolBody &Sym); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 150 | bool empty() const; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | std::vector<const SymbolBody *> Entries; |
| 154 | }; |
| 155 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 156 | template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> { |
| 157 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 158 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 159 | |
| 160 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 161 | PltSection(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 162 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 163 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 164 | void addEntry(SymbolBody &Sym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 165 | bool empty() const { return Entries.empty(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 166 | |
| 167 | private: |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 168 | std::vector<std::pair<const SymbolBody *, unsigned>> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | template <class ELFT> struct DynamicReloc { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 172 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 173 | uint32_t Type; |
| 174 | |
| 175 | // Where the relocation is. |
| 176 | enum OffsetKind { |
| 177 | Off_Got, // The got entry of Sym. |
| 178 | Off_GotPlt, // The got.plt entry of Sym. |
| 179 | Off_Bss, // The bss entry of Sym (copy reloc). |
| 180 | Off_Sec, // The final position of the given input section and offset. |
| 181 | Off_LTlsIndex, // The local tls index. |
| 182 | Off_GTlsIndex, // The global tls index of Sym. |
| 183 | Off_GTlsOffset // The global tls offset of Sym. |
| 184 | } OKind; |
| 185 | |
| 186 | SymbolBody *Sym = nullptr; |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 187 | OutputSectionBase<ELFT> *OffsetSec = nullptr; |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 188 | uintX_t OffsetInSec = 0; |
| 189 | bool UseSymVA = false; |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 190 | uintX_t Addend = 0; |
| 191 | |
| 192 | DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym) |
| 193 | : Type(Type), OKind(OKind), Sym(Sym) {} |
| 194 | |
| 195 | DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym) |
| 196 | : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {} |
| 197 | |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 198 | DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec, |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 199 | uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym, |
| 200 | uintX_t Addend) |
| 201 | : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec), |
| 202 | OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {} |
| 203 | |
Rui Ueyama | a2b1f45 | 2016-02-17 06:08:42 +0000 | [diff] [blame] | 204 | uintX_t getOffset() const; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 208 | class SymbolTableSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 209 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 210 | typedef typename ELFT::Shdr Elf_Shdr; |
| 211 | typedef typename ELFT::Sym Elf_Sym; |
| 212 | typedef typename ELFT::SymRange Elf_Sym_Range; |
| 213 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 214 | SymbolTableSection(SymbolTable<ELFT> &Table, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 215 | StringTableSection<ELFT> &StrTabSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 216 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 217 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 218 | void writeTo(uint8_t *Buf) override; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 219 | void addSymbol(SymbolBody *Body); |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 220 | StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 0e92f24 | 2016-01-27 16:41:24 +0000 | [diff] [blame] | 221 | unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 222 | |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 223 | ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const { |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 224 | return Symbols; |
| 225 | } |
| 226 | |
| 227 | unsigned NumLocals = 0; |
| 228 | StringTableSection<ELFT> &StrTabSec; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 229 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 230 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 231 | void writeLocalSymbols(uint8_t *&Buf); |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 232 | void writeGlobalSymbols(uint8_t *Buf); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 233 | |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 234 | const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym); |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 235 | static uint8_t getSymbolBinding(SymbolBody *Body); |
| 236 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 237 | SymbolTable<ELFT> &Table; |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 238 | |
| 239 | // A vector of symbols and their string table offsets. |
| 240 | std::vector<std::pair<SymbolBody *, size_t>> Symbols; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 244 | class RelocationSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 245 | typedef typename ELFT::Rel Elf_Rel; |
| 246 | typedef typename ELFT::Rela Elf_Rela; |
| 247 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 248 | |
| 249 | public: |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 250 | RelocationSection(StringRef Name); |
Rafael Espindola | d30eb7d | 2016-02-05 15:03:10 +0000 | [diff] [blame] | 251 | void addReloc(const DynamicReloc<ELFT> &Reloc); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 252 | unsigned getRelocOffset(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 253 | void finalize() override; |
| 254 | void writeTo(uint8_t *Buf) override; |
| 255 | bool hasRelocs() const { return !Relocs.empty(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 256 | |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 257 | bool Static = false; |
| 258 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 259 | private: |
| 260 | std::vector<DynamicReloc<ELFT>> Relocs; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 264 | class OutputSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 265 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 266 | typedef typename ELFT::Shdr Elf_Shdr; |
| 267 | typedef typename ELFT::Sym Elf_Sym; |
| 268 | typedef typename ELFT::Rel Elf_Rel; |
| 269 | typedef typename ELFT::Rela Elf_Rela; |
| 270 | typedef typename ELFT::uint uintX_t; |
George Rimar | 9bec24a | 2016-02-18 14:20:08 +0000 | [diff] [blame] | 271 | OutputSection(StringRef Name, uint32_t Type, uintX_t Flags); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 272 | void addSection(InputSectionBase<ELFT> *C) override; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 273 | void sortInitFini(); |
| 274 | void sortCtorsDtors(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 275 | void writeTo(uint8_t *Buf) override; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 276 | void finalize() override; |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 277 | void |
| 278 | forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override; |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 279 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 282 | template <class ELFT> |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 283 | class MergeOutputSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 284 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 285 | |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 286 | bool shouldTailMerge() const; |
| 287 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 288 | public: |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 289 | MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags, |
| 290 | uintX_t Alignment); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 291 | void addSection(InputSectionBase<ELFT> *S) override; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 292 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 293 | unsigned getOffset(StringRef Val); |
| 294 | void finalize() override; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 295 | |
| 296 | private: |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 297 | llvm::StringTableBuilder Builder; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 300 | // FDE or CIE |
| 301 | template <class ELFT> struct EHRegion { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 302 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 303 | EHRegion(EHInputSection<ELFT> *S, unsigned Index); |
| 304 | StringRef data() const; |
| 305 | EHInputSection<ELFT> *S; |
| 306 | unsigned Index; |
| 307 | }; |
| 308 | |
| 309 | template <class ELFT> struct Cie : public EHRegion<ELFT> { |
| 310 | Cie(EHInputSection<ELFT> *S, unsigned Index); |
| 311 | std::vector<EHRegion<ELFT>> Fdes; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 312 | uint8_t FdeEncoding; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | template <class ELFT> |
| 316 | class EHOutputSection final : public OutputSectionBase<ELFT> { |
| 317 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 318 | typedef typename ELFT::uint uintX_t; |
| 319 | typedef typename ELFT::Shdr Elf_Shdr; |
| 320 | typedef typename ELFT::Rel Elf_Rel; |
| 321 | typedef typename ELFT::Rela Elf_Rela; |
George Rimar | 9bec24a | 2016-02-18 14:20:08 +0000 | [diff] [blame] | 322 | EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 323 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 324 | void finalize() override; |
| 325 | void |
| 326 | forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 327 | |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 328 | template <class RelTy> |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 329 | void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 330 | |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 331 | void addSection(InputSectionBase<ELFT> *S) override; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 332 | |
| 333 | private: |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 334 | uint8_t getFdeEncoding(ArrayRef<uint8_t> D); |
George Rimar | 003be4f | 2015-12-17 09:23:40 +0000 | [diff] [blame] | 335 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 336 | std::vector<EHInputSection<ELFT> *> Sections; |
| 337 | std::vector<Cie<ELFT>> Cies; |
| 338 | |
| 339 | // Maps CIE content + personality to a index in Cies. |
Rafael Espindola | 156ed8d | 2016-02-10 13:19:32 +0000 | [diff] [blame] | 340 | llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap; |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 341 | bool Finalized = false; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 344 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 345 | class InterpSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 346 | public: |
| 347 | InterpSection(); |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 348 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 351 | template <class ELFT> |
| 352 | class StringTableSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 353 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 354 | typedef typename ELFT::uint uintX_t; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 355 | StringTableSection(StringRef Name, bool Dynamic); |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 356 | unsigned addString(StringRef S, bool HashIt = true); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 357 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 358 | unsigned getSize() const { return Size; } |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 359 | void finalize() override { this->Header.sh_size = getSize(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 360 | bool isDynamic() const { return Dynamic; } |
| 361 | |
| 362 | private: |
| 363 | const bool Dynamic; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 364 | llvm::DenseMap<StringRef, unsigned> StringMap; |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 365 | std::vector<StringRef> Strings; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 366 | unsigned Size = 1; // ELF string tables start with a NUL byte, so 1. |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 370 | class HashTableSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 371 | typedef typename ELFT::Word Elf_Word; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 372 | |
| 373 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 374 | HashTableSection(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 375 | void finalize() override; |
| 376 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 379 | // Outputs GNU Hash section. For detailed explanation see: |
| 380 | // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections |
| 381 | template <class ELFT> |
| 382 | class GnuHashTableSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 383 | typedef typename ELFT::Off Elf_Off; |
| 384 | typedef typename ELFT::Word Elf_Word; |
| 385 | typedef typename ELFT::uint uintX_t; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 386 | |
| 387 | public: |
| 388 | GnuHashTableSection(); |
| 389 | void finalize() override; |
| 390 | void writeTo(uint8_t *Buf) override; |
| 391 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 392 | // Adds symbols to the hash table. |
| 393 | // Sorts the input to satisfy GNU hash section requirements. |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 394 | void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 395 | |
| 396 | private: |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 397 | static unsigned calcNBuckets(unsigned NumHashed); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 398 | static unsigned calcMaskWords(unsigned NumHashed); |
| 399 | |
| 400 | void writeHeader(uint8_t *&Buf); |
| 401 | void writeBloomFilter(uint8_t *&Buf); |
| 402 | void writeHashTable(uint8_t *Buf); |
| 403 | |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 404 | struct SymbolData { |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 405 | SymbolBody *Body; |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 406 | size_t STName; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 407 | uint32_t Hash; |
| 408 | }; |
| 409 | |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 410 | std::vector<SymbolData> Symbols; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 411 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 412 | unsigned MaskWords; |
| 413 | unsigned NBuckets; |
| 414 | unsigned Shift2; |
| 415 | }; |
| 416 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 417 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 418 | class DynamicSection final : public OutputSectionBase<ELFT> { |
| 419 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 420 | typedef typename ELFT::Dyn Elf_Dyn; |
| 421 | typedef typename ELFT::Rel Elf_Rel; |
| 422 | typedef typename ELFT::Rela Elf_Rela; |
| 423 | typedef typename ELFT::Shdr Elf_Shdr; |
| 424 | typedef typename ELFT::Sym Elf_Sym; |
| 425 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 426 | |
Rui Ueyama | 909cc68 | 2016-02-02 03:11:27 +0000 | [diff] [blame] | 427 | // The .dynamic section contains information for the dynamic linker. |
| 428 | // The section consists of fixed size entries, which consist of |
| 429 | // type and value fields. Value are one of plain integers, symbol |
| 430 | // addresses, or section addresses. This struct represents the entry. |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 431 | struct Entry { |
| 432 | int32_t Tag; |
| 433 | union { |
| 434 | OutputSectionBase<ELFT> *OutSec; |
| 435 | uint64_t Val; |
| 436 | const SymbolBody *Sym; |
| 437 | }; |
| 438 | enum KindT { SecAddr, SymAddr, PlainInt } Kind; |
| 439 | Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec) |
| 440 | : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {} |
| 441 | Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {} |
| 442 | Entry(int32_t Tag, const SymbolBody *Sym) |
| 443 | : Tag(Tag), Sym(Sym), Kind(SymAddr) {} |
| 444 | }; |
Rui Ueyama | 909cc68 | 2016-02-02 03:11:27 +0000 | [diff] [blame] | 445 | |
| 446 | // finalize() fills this vector with the section contents. finalize() |
| 447 | // cannot directly create final section contents because when the |
| 448 | // function is called, symbol or section addresses are not fixed yet. |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 449 | std::vector<Entry> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 450 | |
| 451 | public: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 452 | DynamicSection(SymbolTable<ELFT> &SymTab); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 453 | void finalize() override; |
| 454 | void writeTo(uint8_t *Buf) override; |
| 455 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 456 | OutputSectionBase<ELFT> *PreInitArraySec = nullptr; |
| 457 | OutputSectionBase<ELFT> *InitArraySec = nullptr; |
| 458 | OutputSectionBase<ELFT> *FiniArraySec = nullptr; |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 459 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 460 | private: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 461 | SymbolTable<ELFT> &SymTab; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 462 | }; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 463 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 464 | template <class ELFT> |
| 465 | class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> { |
| 466 | typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo; |
| 467 | |
| 468 | public: |
| 469 | MipsReginfoOutputSection(); |
| 470 | void writeTo(uint8_t *Buf) override; |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 471 | void addSection(InputSectionBase<ELFT> *S) override; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 472 | |
| 473 | private: |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 474 | uint32_t GprMask = 0; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 475 | }; |
| 476 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 477 | // --eh-frame-hdr option tells linker to construct a header for all the |
| 478 | // .eh_frame sections. This header is placed to a section named .eh_frame_hdr |
| 479 | // and also to a PT_GNU_EH_FRAME segment. |
| 480 | // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by |
| 481 | // calling dl_iterate_phdr. |
| 482 | // This section contains a lookup table for quick binary search of FDEs. |
| 483 | // Detailed info about internals can be found in Ian Lance Taylor's blog: |
| 484 | // http://www.airs.com/blog/archives/460 (".eh_frame") |
| 485 | // http://www.airs.com/blog/archives/462 (".eh_frame_hdr") |
| 486 | template <class ELFT> |
| 487 | class EhFrameHeader final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 488 | typedef typename ELFT::uint uintX_t; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 489 | |
| 490 | public: |
| 491 | EhFrameHeader(); |
| 492 | void writeTo(uint8_t *Buf) override; |
| 493 | |
| 494 | void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel); |
| 495 | void assignEhFrame(EHOutputSection<ELFT> *Sec); |
| 496 | void reserveFde(); |
| 497 | |
| 498 | bool Live = false; |
| 499 | |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame^] | 500 | EHOutputSection<ELFT> *Sec = nullptr; |
| 501 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 502 | private: |
| 503 | struct FdeData { |
| 504 | uint8_t Enc; |
| 505 | size_t Off; |
| 506 | uint8_t *PCRel; |
| 507 | }; |
| 508 | |
| 509 | uintX_t getFdePc(uintX_t EhVA, const FdeData &F); |
| 510 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 511 | std::vector<FdeData> FdeList; |
| 512 | }; |
| 513 | |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 514 | template <class ELFT> |
| 515 | class BuildIdSection final : public OutputSectionBase<ELFT> { |
| 516 | public: |
| 517 | BuildIdSection(); |
| 518 | void writeTo(uint8_t *Buf) override; |
| 519 | void update(ArrayRef<uint8_t> Buf); |
| 520 | void writeBuildId(); |
| 521 | |
| 522 | private: |
| 523 | uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis |
| 524 | uint8_t *HashBuf; |
| 525 | }; |
| 526 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 527 | // All output sections that are hadnled by the linker specially are |
| 528 | // globally accessible. Writer initializes them, so don't use them |
| 529 | // until Writer is initialized. |
| 530 | template <class ELFT> struct Out { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 531 | typedef typename ELFT::uint uintX_t; |
| 532 | typedef typename ELFT::Phdr Elf_Phdr; |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 533 | static BuildIdSection<ELFT> *BuildId; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 534 | static DynamicSection<ELFT> *Dynamic; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 535 | static EhFrameHeader<ELFT> *EhFrameHdr; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 536 | static GnuHashTableSection<ELFT> *GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 537 | static GotPltSection<ELFT> *GotPlt; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 538 | static GotSection<ELFT> *Got; |
| 539 | static HashTableSection<ELFT> *HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 540 | static InterpSection<ELFT> *Interp; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 541 | static OutputSection<ELFT> *Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 542 | static OutputSection<ELFT> *MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 543 | static OutputSectionBase<ELFT> *Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 544 | static uint8_t *OpdBuf; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 545 | static PltSection<ELFT> *Plt; |
| 546 | static RelocationSection<ELFT> *RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 547 | static RelocationSection<ELFT> *RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 548 | static StringTableSection<ELFT> *DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 549 | static StringTableSection<ELFT> *ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 550 | static StringTableSection<ELFT> *StrTab; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 551 | static SymbolTableSection<ELFT> *DynSymTab; |
| 552 | static SymbolTableSection<ELFT> *SymTab; |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 553 | static Elf_Phdr *TlsPhdr; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 554 | static OutputSectionBase<ELFT> *ElfHeader; |
| 555 | static OutputSectionBase<ELFT> *ProgramHeaders; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 556 | }; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 557 | |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 558 | template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 559 | template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 560 | template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 561 | template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 562 | template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 563 | template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got; |
| 564 | template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 565 | template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp; |
Rafael Espindola | d7a267b | 2015-11-03 22:01:20 +0000 | [diff] [blame] | 566 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 567 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 568 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 569 | template <class ELFT> uint8_t *Out<ELFT>::OpdBuf; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 570 | template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt; |
| 571 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 572 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 573 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 574 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 575 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 576 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab; |
| 577 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 578 | template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 579 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader; |
| 580 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders; |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 581 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 582 | } // namespace elf |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 583 | } // namespace lld |
| 584 | |
| 585 | #endif // LLD_ELF_OUTPUT_SECTIONS_H |