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> |
| 36 | typename llvm::object::ELFFile<ELFT>::uintX_t |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 37 | getSymVA(const ELFSymbolBody<ELFT> &S, const OutputSection<ELFT> &BssSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 38 | |
| 39 | template <class ELFT> |
| 40 | typename llvm::object::ELFFile<ELFT>::uintX_t |
| 41 | getLocalSymVA(const typename llvm::object::ELFFile<ELFT>::Elf_Sym *Sym, |
| 42 | const ObjectFile<ELFT> &File); |
| 43 | |
| 44 | bool includeInSymtab(const SymbolBody &B); |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 45 | bool includeInDynamicSymtab(const SymbolBody &B); |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 46 | bool shouldKeepInSymtab(StringRef SymName); |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 48 | // This represents a section in an output file. |
| 49 | // Different sub classes represent different types of sections. Some contain |
| 50 | // input sections, others are created by the linker. |
| 51 | // The writer creates multiple OutputSections and assign them unique, |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 52 | // non-overlapping file offsets and VAs. |
| 53 | template <bool Is64Bits> class OutputSectionBase { |
| 54 | public: |
| 55 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 56 | typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr, |
| 57 | llvm::ELF::Elf32_Shdr>::type HeaderT; |
| 58 | |
| 59 | OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags); |
| 60 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
| 61 | uintX_t getVA() const { return Header.sh_addr; } |
| 62 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
| 63 | template <llvm::object::endianness E> |
| 64 | void writeHeaderTo(typename llvm::object::ELFFile< |
| 65 | llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr); |
| 66 | StringRef getName() { return Name; } |
| 67 | void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; } |
| 68 | |
| 69 | unsigned getSectionIndex() const { return SectionIndex; } |
| 70 | void setSectionIndex(unsigned I) { SectionIndex = I; } |
| 71 | |
| 72 | // Returns the size of the section in the output file. |
| 73 | uintX_t getSize() { return Header.sh_size; } |
| 74 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
| 75 | uintX_t getFlags() { return Header.sh_flags; } |
| 76 | uintX_t getFileOff() { return Header.sh_offset; } |
| 77 | uintX_t getAlign() { |
| 78 | // The ELF spec states that a value of 0 means the section has no alignment |
| 79 | // constraits. |
| 80 | return std::max<uintX_t>(Header.sh_addralign, 1); |
| 81 | } |
| 82 | uint32_t getType() { return Header.sh_type; } |
| 83 | |
| 84 | static unsigned getAddrSize() { return Is64Bits ? 8 : 4; } |
| 85 | |
| 86 | virtual void finalize() {} |
| 87 | virtual void writeTo(uint8_t *Buf) = 0; |
| 88 | |
| 89 | protected: |
| 90 | StringRef Name; |
| 91 | HeaderT Header; |
| 92 | unsigned SectionIndex; |
| 93 | ~OutputSectionBase() = default; |
| 94 | }; |
| 95 | |
| 96 | template <class ELFT> |
| 97 | class GotSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 98 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 99 | typedef typename Base::uintX_t uintX_t; |
| 100 | |
| 101 | public: |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 102 | GotSection(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 103 | void finalize() override { |
| 104 | this->Header.sh_size = Entries.size() * this->getAddrSize(); |
| 105 | } |
| 106 | void writeTo(uint8_t *Buf) override {} |
| 107 | void addEntry(SymbolBody *Sym); |
| 108 | bool empty() const { return Entries.empty(); } |
| 109 | uintX_t getEntryAddr(const SymbolBody &B) const; |
| 110 | |
| 111 | private: |
| 112 | std::vector<const SymbolBody *> Entries; |
| 113 | }; |
| 114 | |
| 115 | template <class ELFT> |
| 116 | class PltSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 117 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 118 | typedef typename Base::uintX_t uintX_t; |
| 119 | |
| 120 | public: |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 121 | PltSection(const GotSection<ELFT> &GotSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 122 | void finalize() override { |
| 123 | this->Header.sh_size = Entries.size() * EntrySize; |
| 124 | } |
| 125 | void writeTo(uint8_t *Buf) override; |
| 126 | void addEntry(SymbolBody *Sym); |
| 127 | bool empty() const { return Entries.empty(); } |
| 128 | uintX_t getEntryAddr(const SymbolBody &B) const; |
| 129 | static const unsigned EntrySize = 8; |
| 130 | |
| 131 | private: |
| 132 | std::vector<const SymbolBody *> Entries; |
| 133 | const GotSection<ELFT> &GotSec; |
| 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; |
| 149 | SymbolTableSection(SymbolTable &Table, |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 150 | StringTableSection<ELFT::Is64Bits> &StrTabSec, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 151 | const OutputSection<ELFT> &BssSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 152 | |
| 153 | void finalize() override { |
| 154 | this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym); |
| 155 | this->Header.sh_link = StrTabSec.getSectionIndex(); |
| 156 | this->Header.sh_info = NumLocals + 1; |
| 157 | } |
| 158 | |
| 159 | void writeTo(uint8_t *Buf) override; |
| 160 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 161 | SymbolTable &getSymTable() const { return Table; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 162 | |
| 163 | void addSymbol(StringRef Name, bool isLocal = false) { |
| 164 | StrTabSec.add(Name); |
| 165 | ++NumVisible; |
| 166 | if (isLocal) |
| 167 | ++NumLocals; |
| 168 | } |
| 169 | |
Davide Italiano | e44456b | 2015-09-23 01:50:53 +0000 | [diff] [blame] | 170 | StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 171 | unsigned getNumSymbols() const { return NumVisible + 1; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 172 | |
| 173 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 174 | void writeLocalSymbols(uint8_t *&Buf); |
| 175 | void writeGlobalSymbols(uint8_t *&Buf); |
| 176 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 177 | SymbolTable &Table; |
| 178 | StringTableSection<ELFT::Is64Bits> &StrTabSec; |
| 179 | unsigned NumVisible = 0; |
| 180 | unsigned NumLocals = 0; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 181 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | template <class ELFT> |
| 185 | class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 186 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 187 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 188 | |
| 189 | public: |
| 190 | RelocationSection(SymbolTableSection<ELFT> &DynSymSec, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 191 | const GotSection<ELFT> &GotSec, bool IsRela); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 192 | void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); } |
| 193 | void finalize() override; |
| 194 | void writeTo(uint8_t *Buf) override; |
| 195 | bool hasRelocs() const { return !Relocs.empty(); } |
| 196 | bool isRela() const { return IsRela; } |
| 197 | |
| 198 | private: |
| 199 | std::vector<DynamicReloc<ELFT>> Relocs; |
| 200 | SymbolTableSection<ELFT> &DynSymSec; |
| 201 | const GotSection<ELFT> &GotSec; |
| 202 | const bool IsRela; |
| 203 | }; |
| 204 | |
| 205 | template <class ELFT> |
| 206 | class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 207 | public: |
| 208 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
| 209 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 210 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 211 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 212 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 213 | OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec, |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 214 | const OutputSection<ELFT> &BssSec, StringRef Name, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 215 | uint32_t sh_type, uintX_t sh_flags); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 216 | void addSection(InputSection<ELFT> *C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 217 | void writeTo(uint8_t *Buf) override; |
| 218 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 219 | private: |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 220 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 221 | const PltSection<ELFT> &PltSec; |
| 222 | const GotSection<ELFT> &GotSec; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 223 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | template <bool Is64Bits> |
| 227 | class InterpSection final : public OutputSectionBase<Is64Bits> { |
| 228 | public: |
| 229 | InterpSection(); |
| 230 | |
| 231 | void writeTo(uint8_t *Buf); |
| 232 | }; |
| 233 | |
| 234 | template <bool Is64Bits> |
| 235 | class StringTableSection final : public OutputSectionBase<Is64Bits> { |
| 236 | public: |
| 237 | typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 238 | StringTableSection(bool Dynamic); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 239 | void add(StringRef S) { StrTabBuilder.add(S); } |
| 240 | size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); } |
| 241 | StringRef data() const { return StrTabBuilder.data(); } |
| 242 | void writeTo(uint8_t *Buf) override; |
| 243 | |
| 244 | void finalize() override { |
| 245 | StrTabBuilder.finalize(llvm::StringTableBuilder::ELF); |
| 246 | this->Header.sh_size = StrTabBuilder.data().size(); |
| 247 | } |
| 248 | |
| 249 | bool isDynamic() const { return Dynamic; } |
| 250 | |
| 251 | private: |
| 252 | const bool Dynamic; |
| 253 | llvm::StringTableBuilder StrTabBuilder; |
| 254 | }; |
| 255 | |
| 256 | template <class ELFT> |
| 257 | class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 258 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| 259 | |
| 260 | public: |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 261 | HashTableSection(SymbolTableSection<ELFT> &DynSymSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 262 | void addSymbol(SymbolBody *S); |
| 263 | |
| 264 | void finalize() override { |
| 265 | this->Header.sh_link = DynSymSec.getSectionIndex(); |
| 266 | |
| 267 | assert(DynSymSec.getNumSymbols() == Hashes.size() + 1); |
| 268 | unsigned NumEntries = 2; // nbucket and nchain. |
| 269 | NumEntries += DynSymSec.getNumSymbols(); // The chain entries. |
| 270 | |
| 271 | // Create as many buckets as there are symbols. |
| 272 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 273 | // support for SHT_GNU_HASH is probably even more profitable. |
| 274 | NumEntries += DynSymSec.getNumSymbols(); |
| 275 | this->Header.sh_size = NumEntries * sizeof(Elf_Word); |
| 276 | } |
| 277 | |
| 278 | void writeTo(uint8_t *Buf) override { |
| 279 | unsigned NumSymbols = DynSymSec.getNumSymbols(); |
| 280 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 281 | *P++ = NumSymbols; // nbucket |
| 282 | *P++ = NumSymbols; // nchain |
| 283 | |
| 284 | Elf_Word *Buckets = P; |
| 285 | Elf_Word *Chains = P + NumSymbols; |
| 286 | |
| 287 | for (unsigned I = 1; I < NumSymbols; ++I) { |
| 288 | uint32_t Hash = Hashes[I - 1] % NumSymbols; |
| 289 | Chains[I] = Buckets[Hash]; |
| 290 | Buckets[Hash] = I; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; } |
| 295 | |
| 296 | private: |
| 297 | uint32_t hash(StringRef Name) { |
| 298 | uint32_t H = 0; |
| 299 | for (char C : Name) { |
| 300 | H = (H << 4) + C; |
| 301 | uint32_t G = H & 0xf0000000; |
| 302 | if (G) |
| 303 | H ^= G >> 24; |
| 304 | H &= ~G; |
| 305 | } |
| 306 | return H; |
| 307 | } |
| 308 | SymbolTableSection<ELFT> &DynSymSec; |
| 309 | std::vector<uint32_t> Hashes; |
| 310 | }; |
| 311 | |
| 312 | template <class ELFT> |
| 313 | class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 314 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 315 | typedef typename Base::HeaderT HeaderT; |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame^] | 316 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 317 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 318 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 319 | |
| 320 | public: |
| 321 | DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 322 | RelocationSection<ELFT> &RelaDynSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 323 | void finalize() override; |
| 324 | void writeTo(uint8_t *Buf) override; |
| 325 | |
| 326 | private: |
| 327 | HashTableSection<ELFT> &HashSec; |
| 328 | SymbolTableSection<ELFT> &DynSymSec; |
| 329 | StringTableSection<ELFT::Is64Bits> &DynStrSec; |
| 330 | RelocationSection<ELFT> &RelaDynSec; |
| 331 | SymbolTable &SymTab; |
| 332 | }; |
| 333 | } |
| 334 | } |
| 335 | #endif |