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 | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 37 | getSymVA(const SymbolBody &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); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 43 | bool canBePreempted(const SymbolBody *Body); |
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); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 47 | |
| 48 | template <class ELFT> |
| 49 | bool shouldKeepInSymtab( |
| 50 | StringRef Name, 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: |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 106 | GotSection(const OutputSection<ELFT> &BssSec); |
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; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 117 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | template <class ELFT> |
| 121 | class PltSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 122 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 123 | typedef typename Base::uintX_t uintX_t; |
| 124 | |
| 125 | public: |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 126 | PltSection(const GotSection<ELFT> &GotSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 127 | void finalize() override { |
| 128 | this->Header.sh_size = Entries.size() * EntrySize; |
| 129 | } |
| 130 | void writeTo(uint8_t *Buf) override; |
| 131 | void addEntry(SymbolBody *Sym); |
| 132 | bool empty() const { return Entries.empty(); } |
| 133 | uintX_t getEntryAddr(const SymbolBody &B) const; |
| 134 | static const unsigned EntrySize = 8; |
| 135 | |
| 136 | private: |
| 137 | std::vector<const SymbolBody *> Entries; |
| 138 | const GotSection<ELFT> &GotSec; |
| 139 | }; |
| 140 | |
| 141 | template <class ELFT> struct DynamicReloc { |
| 142 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 143 | const InputSection<ELFT> &C; |
| 144 | const Elf_Rel &RI; |
| 145 | }; |
| 146 | |
| 147 | template <class ELFT> |
| 148 | class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 149 | public: |
| 150 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 151 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 152 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 153 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
| 154 | SymbolTableSection(SymbolTable &Table, |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 155 | StringTableSection<ELFT::Is64Bits> &StrTabSec, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 156 | const OutputSection<ELFT> &BssSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 157 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 158 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 159 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 160 | SymbolTable &getSymTable() const { return Table; } |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 161 | void addSymbol(StringRef Name, bool isLocal = false); |
Davide Italiano | e44456b | 2015-09-23 01:50:53 +0000 | [diff] [blame] | 162 | StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 163 | unsigned getNumSymbols() const { return NumVisible + 1; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 164 | |
| 165 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 166 | void writeLocalSymbols(uint8_t *&Buf); |
| 167 | void writeGlobalSymbols(uint8_t *&Buf); |
| 168 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 169 | SymbolTable &Table; |
| 170 | StringTableSection<ELFT::Is64Bits> &StrTabSec; |
| 171 | unsigned NumVisible = 0; |
| 172 | unsigned NumLocals = 0; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 173 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | template <class ELFT> |
| 177 | class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 178 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 179 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rafael Espindola | 3c83e2b | 2015-10-05 21:09:37 +0000 | [diff] [blame] | 180 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
| 183 | RelocationSection(SymbolTableSection<ELFT> &DynSymSec, |
Rafael Espindola | 9c3e4d2 | 2015-10-05 21:23:08 +0000 | [diff] [blame] | 184 | const GotSection<ELFT> &GotSec, |
| 185 | const OutputSection<ELFT> &BssSec, bool IsRela); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 186 | void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); } |
| 187 | void finalize() override; |
| 188 | void writeTo(uint8_t *Buf) override; |
| 189 | bool hasRelocs() const { return !Relocs.empty(); } |
| 190 | bool isRela() const { return IsRela; } |
| 191 | |
| 192 | private: |
| 193 | std::vector<DynamicReloc<ELFT>> Relocs; |
| 194 | SymbolTableSection<ELFT> &DynSymSec; |
| 195 | const GotSection<ELFT> &GotSec; |
Rafael Espindola | 9c3e4d2 | 2015-10-05 21:23:08 +0000 | [diff] [blame] | 196 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 197 | const bool IsRela; |
| 198 | }; |
| 199 | |
| 200 | template <class ELFT> |
| 201 | class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 202 | public: |
| 203 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
| 204 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 205 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 206 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 207 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 208 | OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec, |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 209 | const OutputSection<ELFT> &BssSec, StringRef Name, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 210 | uint32_t sh_type, uintX_t sh_flags); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 211 | void addSection(InputSection<ELFT> *C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 212 | void writeTo(uint8_t *Buf) override; |
| 213 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 214 | private: |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 215 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 216 | const PltSection<ELFT> &PltSec; |
| 217 | const GotSection<ELFT> &GotSec; |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 218 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | template <bool Is64Bits> |
| 222 | class InterpSection final : public OutputSectionBase<Is64Bits> { |
| 223 | public: |
| 224 | InterpSection(); |
| 225 | |
| 226 | void writeTo(uint8_t *Buf); |
| 227 | }; |
| 228 | |
| 229 | template <bool Is64Bits> |
| 230 | class StringTableSection final : public OutputSectionBase<Is64Bits> { |
| 231 | public: |
| 232 | typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 233 | StringTableSection(bool Dynamic); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 234 | void add(StringRef S) { StrTabBuilder.add(S); } |
| 235 | size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); } |
| 236 | StringRef data() const { return StrTabBuilder.data(); } |
| 237 | void writeTo(uint8_t *Buf) override; |
| 238 | |
| 239 | void finalize() override { |
| 240 | StrTabBuilder.finalize(llvm::StringTableBuilder::ELF); |
| 241 | this->Header.sh_size = StrTabBuilder.data().size(); |
| 242 | } |
| 243 | |
| 244 | bool isDynamic() const { return Dynamic; } |
| 245 | |
| 246 | private: |
| 247 | const bool Dynamic; |
| 248 | llvm::StringTableBuilder StrTabBuilder; |
| 249 | }; |
| 250 | |
| 251 | template <class ELFT> |
| 252 | class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 253 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| 254 | |
| 255 | public: |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 256 | HashTableSection(SymbolTableSection<ELFT> &DynSymSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 257 | void addSymbol(SymbolBody *S); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 258 | void finalize() override; |
| 259 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 260 | SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; } |
| 261 | |
| 262 | private: |
| 263 | uint32_t hash(StringRef Name) { |
| 264 | uint32_t H = 0; |
| 265 | for (char C : Name) { |
| 266 | H = (H << 4) + C; |
| 267 | uint32_t G = H & 0xf0000000; |
| 268 | if (G) |
| 269 | H ^= G >> 24; |
| 270 | H &= ~G; |
| 271 | } |
| 272 | return H; |
| 273 | } |
| 274 | SymbolTableSection<ELFT> &DynSymSec; |
| 275 | std::vector<uint32_t> Hashes; |
| 276 | }; |
| 277 | |
| 278 | template <class ELFT> |
| 279 | class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 280 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 281 | typedef typename Base::HeaderT HeaderT; |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 282 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 283 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 284 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Hal Finkel | d26da92 | 2015-10-02 16:21:30 +0000 | [diff] [blame] | 285 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 286 | |
| 287 | public: |
| 288 | DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec, |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 289 | RelocationSection<ELFT> &RelaDynSec, |
| 290 | const OutputSection<ELFT> &BssSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 291 | void finalize() override; |
| 292 | void writeTo(uint8_t *Buf) override; |
| 293 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 294 | OutputSection<ELFT> *PreInitArraySec = nullptr; |
| 295 | OutputSection<ELFT> *InitArraySec = nullptr; |
| 296 | OutputSection<ELFT> *FiniArraySec = nullptr; |
| 297 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 298 | private: |
| 299 | HashTableSection<ELFT> &HashSec; |
| 300 | SymbolTableSection<ELFT> &DynSymSec; |
| 301 | StringTableSection<ELFT::Is64Bits> &DynStrSec; |
| 302 | RelocationSection<ELFT> &RelaDynSec; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 303 | const OutputSection<ELFT> &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 304 | SymbolTable &SymTab; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 305 | const ELFSymbolBody<ELFT> *InitSym = nullptr; |
| 306 | const ELFSymbolBody<ELFT> *FiniSym = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 307 | }; |
| 308 | } |
| 309 | } |
| 310 | #endif |