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