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