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 | |
| 13 | #include "lld/Core/LLVM.h" |
| 14 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/MapVector.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 16 | #include "llvm/MC/StringTableBuilder.h" |
| 17 | #include "llvm/Object/ELF.h" |
| 18 | |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 19 | #include "Config.h" |
| 20 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 21 | #include <type_traits> |
| 22 | |
| 23 | namespace lld { |
| 24 | namespace elf2 { |
| 25 | |
| 26 | class SymbolBody; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 27 | template <class ELFT> class SymbolTable; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 28 | template <class ELFT> class SymbolTableSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 29 | template <class ELFT> class StringTableSection; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 30 | template <class ELFT> class EHInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 31 | template <class ELFT> class InputSection; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 32 | template <class ELFT> class InputSectionBase; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 33 | template <class ELFT> class MergeInputSection; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 34 | template <class ELFT> class MipsReginfoInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 35 | template <class ELFT> class OutputSection; |
| 36 | template <class ELFT> class ObjectFile; |
| 37 | template <class ELFT> class DefinedRegular; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 38 | template <class ELFT> class ELFSymbolBody; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 39 | |
George Rimar | bfb7bf7 | 2015-12-21 10:00:12 +0000 | [diff] [blame^] | 40 | // Flag to force GOT to be in output if we have relocations |
| 41 | // that relies on its address. |
| 42 | extern bool HasGotOffRel; |
| 43 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 44 | template <class ELFT> |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 45 | static inline typename llvm::object::ELFFile<ELFT>::uintX_t |
| 46 | getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) { |
| 47 | return 0; |
| 48 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 49 | |
| 50 | template <class ELFT> |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 51 | static inline typename llvm::object::ELFFile<ELFT>::uintX_t |
| 52 | getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) { |
| 53 | return Rel.r_addend; |
| 54 | } |
| 55 | |
| 56 | template <class ELFT> |
| 57 | typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S); |
| 58 | |
| 59 | template <class ELFT, bool IsRela> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 60 | typename llvm::object::ELFFile<ELFT>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 61 | getLocalRelTarget(const ObjectFile<ELFT> &File, |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 62 | const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel); |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 63 | bool canBePreempted(const SymbolBody *Body, bool NeedsGot); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 64 | template <class ELFT> bool includeInSymtab(const SymbolBody &B); |
| 65 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 66 | bool includeInDynamicSymtab(const SymbolBody &B); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 67 | |
| 68 | template <class ELFT> |
| 69 | bool shouldKeepInSymtab( |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 70 | const ObjectFile<ELFT> &File, StringRef Name, |
| 71 | const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym); |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 72 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 73 | // This represents a section in an output file. |
| 74 | // Different sub classes represent different types of sections. Some contain |
| 75 | // input sections, others are created by the linker. |
| 76 | // The writer creates multiple OutputSections and assign them unique, |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 77 | // non-overlapping file offsets and VAs. |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 78 | template <class ELFT> class OutputSectionBase { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 79 | public: |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 80 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 81 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 82 | |
| 83 | OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
| 84 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
| 85 | uintX_t getVA() const { return Header.sh_addr; } |
| 86 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 87 | void writeHeaderTo(Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 88 | StringRef getName() { return Name; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 90 | unsigned SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 91 | |
| 92 | // Returns the size of the section in the output file. |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 93 | uintX_t getSize() const { return Header.sh_size; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 94 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
| 95 | uintX_t getFlags() { return Header.sh_flags; } |
| 96 | uintX_t getFileOff() { return Header.sh_offset; } |
| 97 | uintX_t getAlign() { |
| 98 | // The ELF spec states that a value of 0 means the section has no alignment |
| 99 | // constraits. |
| 100 | return std::max<uintX_t>(Header.sh_addralign, 1); |
| 101 | } |
| 102 | uint32_t getType() { return Header.sh_type; } |
Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 103 | void updateAlign(uintX_t Align) { |
| 104 | if (Align > Header.sh_addralign) |
| 105 | Header.sh_addralign = Align; |
| 106 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 107 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 108 | virtual void finalize() {} |
| 109 | virtual void writeTo(uint8_t *Buf) = 0; |
| 110 | |
| 111 | protected: |
| 112 | StringRef Name; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 113 | Elf_Shdr Header; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 114 | ~OutputSectionBase() = default; |
| 115 | }; |
| 116 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 117 | template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> { |
| 118 | typedef OutputSectionBase<ELFT> Base; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 119 | typedef typename Base::uintX_t uintX_t; |
| 120 | |
| 121 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 122 | GotSection(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 123 | void finalize() override; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 124 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 125 | void addEntry(SymbolBody *Sym); |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 126 | bool addDynTlsEntry(SymbolBody *Sym); |
George Rimar | 95c1a58 | 2015-12-07 08:02:20 +0000 | [diff] [blame] | 127 | bool addCurrentModuleTlsIndex(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 128 | bool empty() const { return Entries.empty(); } |
| 129 | uintX_t getEntryAddr(const SymbolBody &B) const; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 130 | uintX_t getGlobalDynAddr(const SymbolBody &B) const; |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 131 | uintX_t getNumEntries() const { return Entries.size(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 132 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 133 | // Returns the symbol which corresponds to the first entry of the global part |
| 134 | // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic |
| 135 | // table properties. |
| 136 | // Returns nullptr if the global part is empty. |
| 137 | const SymbolBody *getMipsFirstGlobalEntry() const; |
| 138 | |
| 139 | // Returns the number of entries in the local part of GOT including |
| 140 | // the number of reserved entries. This method is MIPS-specific. |
| 141 | unsigned getMipsLocalEntriesNum() const; |
| 142 | |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 143 | uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; } |
| 144 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 145 | private: |
| 146 | std::vector<const SymbolBody *> Entries; |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 147 | uint32_t LocalTlsIndexOff = -1; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 150 | template <class ELFT> |
| 151 | class GotPltSection final : public OutputSectionBase<ELFT> { |
| 152 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 153 | |
| 154 | public: |
| 155 | GotPltSection(); |
| 156 | void finalize() override; |
| 157 | void writeTo(uint8_t *Buf) override; |
| 158 | void addEntry(SymbolBody *Sym); |
| 159 | bool empty() const; |
| 160 | uintX_t getEntryAddr(const SymbolBody &B) const; |
| 161 | |
| 162 | private: |
| 163 | std::vector<const SymbolBody *> Entries; |
| 164 | }; |
| 165 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 166 | template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> { |
| 167 | typedef OutputSectionBase<ELFT> Base; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 168 | typedef typename Base::uintX_t uintX_t; |
| 169 | |
| 170 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 171 | PltSection(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 172 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 173 | void writeTo(uint8_t *Buf) override; |
| 174 | void addEntry(SymbolBody *Sym); |
| 175 | bool empty() const { return Entries.empty(); } |
| 176 | uintX_t getEntryAddr(const SymbolBody &B) const; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 177 | |
| 178 | private: |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 179 | std::vector<std::pair<const SymbolBody *, unsigned>> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | template <class ELFT> struct DynamicReloc { |
| 183 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 184 | InputSectionBase<ELFT> *C; |
| 185 | const Elf_Rel *RI; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 189 | class SymbolTableSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 190 | public: |
| 191 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 192 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 193 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 194 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 195 | SymbolTableSection(SymbolTable<ELFT> &Table, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 196 | StringTableSection<ELFT> &StrTabSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 197 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 198 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 199 | void writeTo(uint8_t *Buf) override; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 200 | void addLocalSymbol(StringRef Name); |
| 201 | void addSymbol(SymbolBody *Body); |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 202 | StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 203 | unsigned getNumSymbols() const { return NumVisible + 1; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 204 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 205 | ArrayRef<SymbolBody *> getSymbols() const { return Symbols; } |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 206 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 207 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 208 | void writeLocalSymbols(uint8_t *&Buf); |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 209 | void writeGlobalSymbols(uint8_t *Buf); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 210 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 211 | static uint8_t getSymbolBinding(SymbolBody *Body); |
| 212 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 213 | SymbolTable<ELFT> &Table; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 214 | StringTableSection<ELFT> &StrTabSec; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 215 | std::vector<SymbolBody *> Symbols; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 216 | unsigned NumVisible = 0; |
| 217 | unsigned NumLocals = 0; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 221 | class RelocationSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 222 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 223 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rafael Espindola | 3c83e2b | 2015-10-05 21:09:37 +0000 | [diff] [blame] | 224 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 225 | |
| 226 | public: |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 227 | RelocationSection(StringRef Name, bool IsRela); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 228 | void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); } |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 229 | unsigned getRelocOffset(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 230 | void finalize() override; |
| 231 | void writeTo(uint8_t *Buf) override; |
| 232 | bool hasRelocs() const { return !Relocs.empty(); } |
| 233 | bool isRela() const { return IsRela; } |
| 234 | |
| 235 | private: |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 236 | bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P, |
| 237 | Elf_Rel *N); |
| 238 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 239 | std::vector<DynamicReloc<ELFT>> Relocs; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 240 | const bool IsRela; |
| 241 | }; |
| 242 | |
| 243 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 244 | class OutputSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 245 | public: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 246 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 247 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 248 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 249 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 250 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 251 | OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 252 | void addSection(InputSection<ELFT> *C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 253 | void writeTo(uint8_t *Buf) override; |
| 254 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 255 | private: |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 256 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 259 | template <class ELFT> |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 260 | class MergeOutputSection final : public OutputSectionBase<ELFT> { |
| 261 | typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t; |
| 262 | |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 263 | bool shouldTailMerge() const; |
| 264 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 265 | public: |
| 266 | MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
| 267 | void addSection(MergeInputSection<ELFT> *S); |
| 268 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 269 | unsigned getOffset(StringRef Val); |
| 270 | void finalize() override; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 271 | |
| 272 | private: |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 273 | llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW}; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 276 | // FDE or CIE |
| 277 | template <class ELFT> struct EHRegion { |
| 278 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 279 | EHRegion(EHInputSection<ELFT> *S, unsigned Index); |
| 280 | StringRef data() const; |
| 281 | EHInputSection<ELFT> *S; |
| 282 | unsigned Index; |
| 283 | }; |
| 284 | |
| 285 | template <class ELFT> struct Cie : public EHRegion<ELFT> { |
| 286 | Cie(EHInputSection<ELFT> *S, unsigned Index); |
| 287 | std::vector<EHRegion<ELFT>> Fdes; |
| 288 | }; |
| 289 | |
| 290 | template <class ELFT> |
| 291 | class EHOutputSection final : public OutputSectionBase<ELFT> { |
| 292 | public: |
| 293 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 294 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 295 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 296 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 297 | EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
| 298 | void writeTo(uint8_t *Buf) override; |
| 299 | |
| 300 | template <bool IsRela> |
| 301 | void addSectionAux( |
| 302 | EHInputSection<ELFT> *S, |
| 303 | llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *> |
| 304 | Rels); |
| 305 | |
| 306 | void addSection(EHInputSection<ELFT> *S); |
| 307 | |
| 308 | private: |
George Rimar | 003be4f | 2015-12-17 09:23:40 +0000 | [diff] [blame] | 309 | uintX_t readEntryLength(ArrayRef<uint8_t> D); |
| 310 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 311 | std::vector<EHInputSection<ELFT> *> Sections; |
| 312 | std::vector<Cie<ELFT>> Cies; |
| 313 | |
| 314 | // Maps CIE content + personality to a index in Cies. |
| 315 | llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap; |
| 316 | }; |
| 317 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 318 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 319 | class InterpSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 320 | public: |
| 321 | InterpSection(); |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 322 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 325 | template <class ELFT> |
| 326 | class StringTableSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 327 | public: |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 328 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 329 | StringTableSection(StringRef Name, bool Dynamic); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 330 | void add(StringRef S) { StrTabBuilder.add(S); } |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 331 | size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 332 | StringRef data() const { return StrTabBuilder.data(); } |
| 333 | void writeTo(uint8_t *Buf) override; |
| 334 | |
| 335 | void finalize() override { |
Rafael Espindola | 6779a23 | 2015-10-23 21:48:35 +0000 | [diff] [blame] | 336 | StrTabBuilder.finalize(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 337 | this->Header.sh_size = StrTabBuilder.data().size(); |
| 338 | } |
| 339 | |
| 340 | bool isDynamic() const { return Dynamic; } |
| 341 | |
| 342 | private: |
| 343 | const bool Dynamic; |
Rafael Espindola | 6779a23 | 2015-10-23 21:48:35 +0000 | [diff] [blame] | 344 | llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF}; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 348 | class HashTableSection final : public OutputSectionBase<ELFT> { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 349 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| 350 | |
| 351 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 352 | HashTableSection(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 353 | void finalize() override; |
| 354 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 357 | // Outputs GNU Hash section. For detailed explanation see: |
| 358 | // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections |
| 359 | template <class ELFT> |
| 360 | class GnuHashTableSection final : public OutputSectionBase<ELFT> { |
| 361 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off; |
| 362 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| 363 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 364 | |
| 365 | public: |
| 366 | GnuHashTableSection(); |
| 367 | void finalize() override; |
| 368 | void writeTo(uint8_t *Buf) override; |
| 369 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 370 | // Adds symbols to the hash table. |
| 371 | // Sorts the input to satisfy GNU hash section requirements. |
| 372 | void addSymbols(std::vector<SymbolBody *> &Symbols); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 373 | |
| 374 | private: |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 375 | static unsigned calcNBuckets(unsigned NumHashed); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 376 | static unsigned calcMaskWords(unsigned NumHashed); |
| 377 | |
| 378 | void writeHeader(uint8_t *&Buf); |
| 379 | void writeBloomFilter(uint8_t *&Buf); |
| 380 | void writeHashTable(uint8_t *Buf); |
| 381 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 382 | struct HashedSymbolData { |
| 383 | SymbolBody *Body; |
| 384 | uint32_t Hash; |
| 385 | }; |
| 386 | |
| 387 | std::vector<HashedSymbolData> HashedSymbols; |
| 388 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 389 | unsigned MaskWords; |
| 390 | unsigned NBuckets; |
| 391 | unsigned Shift2; |
| 392 | }; |
| 393 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 394 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 395 | class DynamicSection final : public OutputSectionBase<ELFT> { |
| 396 | typedef OutputSectionBase<ELFT> Base; |
| 397 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn; |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 398 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 399 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 400 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 401 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 402 | |
| 403 | public: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 404 | DynamicSection(SymbolTable<ELFT> &SymTab); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 405 | void finalize() override; |
| 406 | void writeTo(uint8_t *Buf) override; |
| 407 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 408 | OutputSectionBase<ELFT> *PreInitArraySec = nullptr; |
| 409 | OutputSectionBase<ELFT> *InitArraySec = nullptr; |
| 410 | OutputSectionBase<ELFT> *FiniArraySec = nullptr; |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 411 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 412 | private: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 413 | SymbolTable<ELFT> &SymTab; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 414 | const ELFSymbolBody<ELFT> *InitSym = nullptr; |
| 415 | const ELFSymbolBody<ELFT> *FiniSym = nullptr; |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 416 | uint32_t DtFlags = 0; |
| 417 | uint32_t DtFlags1 = 0; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 418 | }; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 419 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 420 | template <class ELFT> |
| 421 | class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> { |
| 422 | typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo; |
| 423 | |
| 424 | public: |
| 425 | MipsReginfoOutputSection(); |
| 426 | void writeTo(uint8_t *Buf) override; |
| 427 | |
| 428 | void addSection(MipsReginfoInputSection<ELFT> *S); |
| 429 | |
| 430 | private: |
| 431 | uint32_t GeneralMask = 0; |
| 432 | }; |
| 433 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 434 | // All output sections that are hadnled by the linker specially are |
| 435 | // globally accessible. Writer initializes them, so don't use them |
| 436 | // until Writer is initialized. |
| 437 | template <class ELFT> struct Out { |
Michael J. Spencer | d77f0d2 | 2015-11-03 22:39:09 +0000 | [diff] [blame] | 438 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 439 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 440 | static DynamicSection<ELFT> *Dynamic; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 441 | static GnuHashTableSection<ELFT> *GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 442 | static GotPltSection<ELFT> *GotPlt; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 443 | static GotSection<ELFT> *Got; |
| 444 | static HashTableSection<ELFT> *HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 445 | static InterpSection<ELFT> *Interp; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 446 | static OutputSection<ELFT> *Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 447 | static OutputSection<ELFT> *MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 448 | static OutputSectionBase<ELFT> *Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 449 | static uint8_t *OpdBuf; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 450 | static PltSection<ELFT> *Plt; |
| 451 | static RelocationSection<ELFT> *RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 452 | static RelocationSection<ELFT> *RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 453 | static StringTableSection<ELFT> *DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 454 | static StringTableSection<ELFT> *ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 455 | static StringTableSection<ELFT> *StrTab; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 456 | static SymbolTableSection<ELFT> *DynSymTab; |
| 457 | static SymbolTableSection<ELFT> *SymTab; |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 458 | static Elf_Phdr *TlsPhdr; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 459 | }; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 460 | |
| 461 | template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 462 | template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 463 | template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 464 | template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got; |
| 465 | template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 466 | template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp; |
Rafael Espindola | d7a267b | 2015-11-03 22:01:20 +0000 | [diff] [blame] | 467 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 468 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 469 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 470 | template <class ELFT> uint8_t *Out<ELFT>::OpdBuf; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 471 | template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt; |
| 472 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 473 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 474 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 475 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 476 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 477 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab; |
| 478 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab; |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 479 | template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr; |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 480 | |
| 481 | } // namespace elf2 |
| 482 | } // namespace lld |
| 483 | |
| 484 | #endif // LLD_ELF_OUTPUT_SECTIONS_H |