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