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 | |
| 15 | #include "llvm/MC/StringTableBuilder.h" |
| 16 | #include "llvm/Object/ELF.h" |
| 17 | |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 18 | #include "Config.h" |
| 19 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 20 | #include <type_traits> |
| 21 | |
| 22 | namespace lld { |
| 23 | namespace elf2 { |
| 24 | |
| 25 | class SymbolBody; |
| 26 | class SymbolTable; |
| 27 | template <class ELFT> class SymbolTableSection; |
| 28 | template <bool Is64Bits> class StringTableSection; |
| 29 | template <class ELFT> class InputSection; |
| 30 | template <class ELFT> class OutputSection; |
| 31 | template <class ELFT> class ObjectFile; |
| 32 | template <class ELFT> class DefinedRegular; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 33 | template <class ELFT> class ELFSymbolBody; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 34 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 35 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 36 | typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 37 | |
| 38 | template <class ELFT> |
| 39 | typename llvm::object::ELFFile<ELFT>::uintX_t |
| 40 | getLocalSymVA(const typename llvm::object::ELFFile<ELFT>::Elf_Sym *Sym, |
| 41 | const ObjectFile<ELFT> &File); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 42 | bool canBePreempted(const SymbolBody *Body); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 43 | template <class ELFT> bool includeInSymtab(const SymbolBody &B); |
| 44 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 45 | bool includeInDynamicSymtab(const SymbolBody &B); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 46 | |
| 47 | template <class ELFT> |
| 48 | bool shouldKeepInSymtab( |
| 49 | StringRef Name, const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym); |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 50 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 51 | // This represents a section in an output file. |
| 52 | // Different sub classes represent different types of sections. Some contain |
| 53 | // input sections, others are created by the linker. |
| 54 | // The writer creates multiple OutputSections and assign them unique, |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 55 | // non-overlapping file offsets and VAs. |
| 56 | template <bool Is64Bits> class OutputSectionBase { |
| 57 | public: |
| 58 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 59 | typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr, |
| 60 | llvm::ELF::Elf32_Shdr>::type HeaderT; |
| 61 | |
| 62 | OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
| 63 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
| 64 | uintX_t getVA() const { return Header.sh_addr; } |
| 65 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
| 66 | template <llvm::object::endianness E> |
| 67 | void writeHeaderTo(typename llvm::object::ELFFile< |
| 68 | llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr); |
| 69 | StringRef getName() { return Name; } |
| 70 | void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; } |
| 71 | |
| 72 | unsigned getSectionIndex() const { return SectionIndex; } |
| 73 | void setSectionIndex(unsigned I) { SectionIndex = I; } |
| 74 | |
| 75 | // Returns the size of the section in the output file. |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 76 | uintX_t getSize() const { return Header.sh_size; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 77 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
| 78 | uintX_t getFlags() { return Header.sh_flags; } |
| 79 | uintX_t getFileOff() { return Header.sh_offset; } |
| 80 | uintX_t getAlign() { |
| 81 | // The ELF spec states that a value of 0 means the section has no alignment |
| 82 | // constraits. |
| 83 | return std::max<uintX_t>(Header.sh_addralign, 1); |
| 84 | } |
| 85 | uint32_t getType() { return Header.sh_type; } |
| 86 | |
| 87 | static unsigned getAddrSize() { return Is64Bits ? 8 : 4; } |
| 88 | |
| 89 | virtual void finalize() {} |
| 90 | virtual void writeTo(uint8_t *Buf) = 0; |
| 91 | |
| 92 | protected: |
| 93 | StringRef Name; |
| 94 | HeaderT Header; |
| 95 | unsigned SectionIndex; |
| 96 | ~OutputSectionBase() = default; |
| 97 | }; |
| 98 | |
| 99 | template <class ELFT> |
| 100 | class GotSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 101 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 102 | typedef typename Base::uintX_t uintX_t; |
| 103 | |
| 104 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 105 | GotSection(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 106 | void finalize() override { |
| 107 | this->Header.sh_size = Entries.size() * this->getAddrSize(); |
| 108 | } |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 109 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 110 | void addEntry(SymbolBody *Sym); |
| 111 | bool empty() const { return Entries.empty(); } |
| 112 | uintX_t getEntryAddr(const SymbolBody &B) const; |
| 113 | |
| 114 | private: |
| 115 | std::vector<const SymbolBody *> Entries; |
| 116 | }; |
| 117 | |
| 118 | template <class ELFT> |
| 119 | class PltSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 120 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 121 | typedef typename Base::uintX_t uintX_t; |
| 122 | |
| 123 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 124 | PltSection(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 125 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 126 | void writeTo(uint8_t *Buf) override; |
| 127 | void addEntry(SymbolBody *Sym); |
| 128 | bool empty() const { return Entries.empty(); } |
| 129 | uintX_t getEntryAddr(const SymbolBody &B) const; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | std::vector<const SymbolBody *> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | template <class ELFT> struct DynamicReloc { |
| 136 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 137 | const InputSection<ELFT> &C; |
| 138 | const Elf_Rel &RI; |
| 139 | }; |
| 140 | |
| 141 | template <class ELFT> |
| 142 | class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 143 | public: |
| 144 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 145 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 146 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 147 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
| 148 | SymbolTableSection(SymbolTable &Table, |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 149 | StringTableSection<ELFT::Is64Bits> &StrTabSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 151 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 152 | void writeTo(uint8_t *Buf) override; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 153 | void addSymbol(StringRef Name, bool isLocal = false); |
Davide Italiano | e44456b | 2015-09-23 01:50:53 +0000 | [diff] [blame] | 154 | StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 155 | unsigned getNumSymbols() const { return NumVisible + 1; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 156 | |
| 157 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 158 | void writeLocalSymbols(uint8_t *&Buf); |
| 159 | void writeGlobalSymbols(uint8_t *&Buf); |
| 160 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 161 | SymbolTable &Table; |
| 162 | StringTableSection<ELFT::Is64Bits> &StrTabSec; |
| 163 | unsigned NumVisible = 0; |
| 164 | unsigned NumLocals = 0; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | template <class ELFT> |
| 168 | class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 169 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 170 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rafael Espindola | 3c83e2b | 2015-10-05 21:09:37 +0000 | [diff] [blame] | 171 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 172 | |
| 173 | public: |
Rafael Espindola | d540919 | 2015-10-09 14:25:49 +0000 | [diff] [blame^] | 174 | RelocationSection(bool IsRela); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 175 | void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); } |
| 176 | void finalize() override; |
| 177 | void writeTo(uint8_t *Buf) override; |
| 178 | bool hasRelocs() const { return !Relocs.empty(); } |
| 179 | bool isRela() const { return IsRela; } |
| 180 | |
| 181 | private: |
| 182 | std::vector<DynamicReloc<ELFT>> Relocs; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 183 | const bool IsRela; |
| 184 | }; |
| 185 | |
| 186 | template <class ELFT> |
| 187 | class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 188 | public: |
| 189 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
| 190 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 191 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 192 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 193 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 194 | OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 195 | void addSection(InputSection<ELFT> *C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 196 | void writeTo(uint8_t *Buf) override; |
| 197 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 198 | private: |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 199 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | template <bool Is64Bits> |
| 203 | class InterpSection final : public OutputSectionBase<Is64Bits> { |
| 204 | public: |
| 205 | InterpSection(); |
| 206 | |
| 207 | void writeTo(uint8_t *Buf); |
| 208 | }; |
| 209 | |
| 210 | template <bool Is64Bits> |
| 211 | class StringTableSection final : public OutputSectionBase<Is64Bits> { |
| 212 | public: |
| 213 | typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 214 | StringTableSection(bool Dynamic); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 215 | void add(StringRef S) { StrTabBuilder.add(S); } |
| 216 | size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); } |
| 217 | StringRef data() const { return StrTabBuilder.data(); } |
| 218 | void writeTo(uint8_t *Buf) override; |
| 219 | |
| 220 | void finalize() override { |
| 221 | StrTabBuilder.finalize(llvm::StringTableBuilder::ELF); |
| 222 | this->Header.sh_size = StrTabBuilder.data().size(); |
| 223 | } |
| 224 | |
| 225 | bool isDynamic() const { return Dynamic; } |
| 226 | |
| 227 | private: |
| 228 | const bool Dynamic; |
| 229 | llvm::StringTableBuilder StrTabBuilder; |
| 230 | }; |
| 231 | |
| 232 | template <class ELFT> |
| 233 | class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 234 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| 235 | |
| 236 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 237 | HashTableSection(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 238 | void addSymbol(SymbolBody *S); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 239 | void finalize() override; |
| 240 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 241 | |
| 242 | private: |
| 243 | uint32_t hash(StringRef Name) { |
| 244 | uint32_t H = 0; |
| 245 | for (char C : Name) { |
| 246 | H = (H << 4) + C; |
| 247 | uint32_t G = H & 0xf0000000; |
| 248 | if (G) |
| 249 | H ^= G >> 24; |
| 250 | H &= ~G; |
| 251 | } |
| 252 | return H; |
| 253 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 254 | std::vector<uint32_t> Hashes; |
| 255 | }; |
| 256 | |
| 257 | template <class ELFT> |
| 258 | class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 259 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 260 | typedef typename Base::HeaderT HeaderT; |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 261 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 262 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 263 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Hal Finkel | d26da92 | 2015-10-02 16:21:30 +0000 | [diff] [blame] | 264 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 265 | |
| 266 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 267 | DynamicSection(SymbolTable &SymTab); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 268 | void finalize() override; |
| 269 | void writeTo(uint8_t *Buf) override; |
| 270 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 271 | OutputSection<ELFT> *PreInitArraySec = nullptr; |
| 272 | OutputSection<ELFT> *InitArraySec = nullptr; |
| 273 | OutputSection<ELFT> *FiniArraySec = nullptr; |
| 274 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 275 | private: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 276 | SymbolTable &SymTab; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 277 | const ELFSymbolBody<ELFT> *InitSym = nullptr; |
| 278 | const ELFSymbolBody<ELFT> *FiniSym = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 279 | }; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 280 | |
| 281 | // All output sections that are hadnled by the linker specially are |
| 282 | // globally accessible. Writer initializes them, so don't use them |
| 283 | // until Writer is initialized. |
| 284 | template <class ELFT> struct Out { |
| 285 | static DynamicSection<ELFT> *Dynamic; |
| 286 | static GotSection<ELFT> *Got; |
| 287 | static HashTableSection<ELFT> *HashTab; |
| 288 | static InterpSection<ELFT::Is64Bits> *Interp; |
| 289 | static OutputSection<ELFT> *Bss; |
| 290 | static PltSection<ELFT> *Plt; |
| 291 | static RelocationSection<ELFT> *RelaDyn; |
| 292 | static StringTableSection<ELFT::Is64Bits> *DynStrTab; |
| 293 | static StringTableSection<ELFT::Is64Bits> *StrTab; |
| 294 | static SymbolTableSection<ELFT> *DynSymTab; |
| 295 | static SymbolTableSection<ELFT> *SymTab; |
| 296 | }; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | #endif |