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