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