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