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