| Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- InputFiles.h ---------------------------------------------*- C++ -*-===// |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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_INPUT_FILES_H |
| 11 | #define LLD_ELF_INPUT_FILES_H |
| 12 | |
| Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 13 | #include "InputSection.h" |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 14 | #include "Error.h" |
| Rafael Espindola | f2f41a9 | 2015-08-28 02:40:04 +0000 | [diff] [blame] | 15 | #include "Symbols.h" |
| 16 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | #include "lld/Core/LLVM.h" |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/Object/Archive.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | #include "llvm/Object/ELF.h" |
| 22 | |
| 23 | namespace lld { |
| 24 | namespace elf2 { |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 25 | |
| 26 | using llvm::object::Archive; |
| 27 | |
| 28 | class Lazy; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | class SymbolBody; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | |
| 31 | // The root class of input files. |
| 32 | class InputFile { |
| 33 | public: |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 34 | enum Kind { ObjectKind, SharedKind, ArchiveKind }; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 35 | Kind kind() const { return FileKind; } |
| 36 | virtual ~InputFile() {} |
| 37 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 38 | // Reads a file (constructors don't do that). |
| 39 | virtual void parse() = 0; |
| 40 | |
| Rafael Espindola | 3c9cb4b | 2015-08-05 12:03:34 +0000 | [diff] [blame] | 41 | StringRef getName() const { return MB.getBufferIdentifier(); } |
| 42 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 43 | protected: |
| 44 | explicit InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} |
| 45 | MemoryBufferRef MB; |
| 46 | |
| 47 | private: |
| 48 | const Kind FileKind; |
| 49 | }; |
| 50 | |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 51 | enum ELFKind { ELF32LEKind, ELF32BEKind, ELF64LEKind, ELF64BEKind }; |
| 52 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 53 | class ELFFileBase : public InputFile { |
| 54 | public: |
| 55 | explicit ELFFileBase(Kind K, ELFKind EKind, MemoryBufferRef M) |
| 56 | : InputFile(K, M), EKind(EKind) {} |
| 57 | static bool classof(const InputFile *F) { |
| 58 | Kind K = F->kind(); |
| 59 | return K == ObjectKind || K == SharedKind; |
| 60 | } |
| 61 | |
| 62 | bool isCompatibleWith(const ELFFileBase &Other) const; |
| 63 | ELFKind getELFKind() const { return EKind; } |
| 64 | |
| Rafael Espindola | 503fe94 | 2015-09-22 16:53:55 +0000 | [diff] [blame] | 65 | uint16_t getEMachine() const; |
| 66 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 67 | protected: |
| 68 | const ELFKind EKind; |
| 69 | }; |
| 70 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 71 | // .o file. |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 72 | class ObjectFileBase : public ELFFileBase { |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 73 | public: |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 74 | explicit ObjectFileBase(ELFKind EKind, MemoryBufferRef M) |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 75 | : ELFFileBase(ObjectKind, EKind, M) {} |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 76 | static bool classof(const InputFile *F) { return F->kind() == ObjectKind; } |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 77 | |
| Rafael Espindola | 8788e1a | 2015-09-02 23:01:37 +0000 | [diff] [blame] | 78 | ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; } |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 79 | |
| 80 | protected: |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 81 | // List of all symbols referenced or defined by this file. |
| 82 | std::vector<SymbolBody *> SymbolBodies; |
| 83 | |
| 84 | llvm::BumpPtrAllocator Alloc; |
| 85 | }; |
| 86 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 87 | template <class ELFT> static ELFKind getStaticELFKind() { |
| 88 | if (!ELFT::Is64Bits) { |
| 89 | if (ELFT::TargetEndianness == llvm::support::little) |
| 90 | return ELF32LEKind; |
| 91 | return ELF32BEKind; |
| 92 | } |
| 93 | if (ELFT::TargetEndianness == llvm::support::little) |
| 94 | return ELF64LEKind; |
| 95 | return ELF64BEKind; |
| 96 | } |
| 97 | |
| 98 | template <class ELFT> class ELFData { |
| 99 | public: |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 100 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 101 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 102 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 103 | llvm::object::ELFFile<ELFT> *getObj() const { return ELFObj.get(); } |
| 104 | |
| 105 | uint16_t getEMachine() const { return getObj()->getHeader()->e_machine; } |
| 106 | |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 107 | StringRef getStringTable() const { return StringTable; } |
| 108 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 109 | protected: |
| 110 | std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj; |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 111 | const Elf_Shdr *Symtab = nullptr; |
| 112 | StringRef StringTable; |
| 113 | Elf_Sym_Range getNonLocalSymbols(); |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 114 | Elf_Sym_Range getSymbolsHelper(bool); |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 115 | |
| 116 | void openELF(MemoryBufferRef MB); |
| 117 | }; |
| 118 | |
| 119 | template <class ELFT> |
| 120 | class ObjectFile : public ObjectFileBase, public ELFData<ELFT> { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 121 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 122 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 123 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 124 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 125 | |
| 126 | public: |
| Rafael Espindola | 503fe94 | 2015-09-22 16:53:55 +0000 | [diff] [blame] | 127 | using ELFData<ELFT>::getEMachine; |
| Rafael Espindola | 3c9cb4b | 2015-08-05 12:03:34 +0000 | [diff] [blame] | 128 | |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 129 | static bool classof(const InputFile *F) { |
| 130 | return F->kind() == ObjectKind && |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 131 | cast<ELFFileBase>(F)->getELFKind() == getStaticELFKind<ELFT>(); |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 132 | } |
| Rafael Espindola | 3c9cb4b | 2015-08-05 12:03:34 +0000 | [diff] [blame] | 133 | |
| Rafael Espindola | 905ad34 | 2015-09-02 20:43:43 +0000 | [diff] [blame] | 134 | explicit ObjectFile(MemoryBufferRef M) |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 135 | : ObjectFileBase(getStaticELFKind<ELFT>(), M) {} |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 136 | void parse() override; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 137 | |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 138 | ArrayRef<InputSection<ELFT> *> getSections() const { return Sections; } |
| Rafael Espindola | e7a00e3 | 2015-08-05 13:55:34 +0000 | [diff] [blame] | 139 | |
| Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 140 | SymbolBody *getSymbolBody(uint32_t SymbolIndex) const { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 141 | uint32_t FirstNonLocal = this->Symtab->sh_info; |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 142 | if (SymbolIndex < FirstNonLocal) |
| 143 | return nullptr; |
| 144 | return SymbolBodies[SymbolIndex - FirstNonLocal]->getReplacement(); |
| 145 | } |
| 146 | |
| Davide Italiano | 6d328d3 | 2015-09-16 20:45:57 +0000 | [diff] [blame] | 147 | Elf_Sym_Range getLocalSymbols(); |
| 148 | |
| Davide Italiano | f7892a1 | 2015-09-18 18:28:08 +0000 | [diff] [blame] | 149 | const Elf_Shdr *getSymbolTable() const { return this->Symtab; }; |
| 150 | ArrayRef<Elf_Word> getSymbolTableShndx() const { return SymtabSHNDX; }; |
| Davide Italiano | b5b47b4 | 2015-09-18 01:08:17 +0000 | [diff] [blame] | 151 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 152 | private: |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 153 | void initializeSections(); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 154 | void initializeSymbols(); |
| 155 | |
| 156 | SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym); |
| 157 | |
| Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 158 | // List of all sections defined by this file. |
| 159 | std::vector<InputSection<ELFT> *> Sections; |
| Rafael Espindola | d8340da | 2015-08-10 15:12:17 +0000 | [diff] [blame] | 160 | |
| Rafael Espindola | 2034822 | 2015-08-24 21:43:25 +0000 | [diff] [blame] | 161 | ArrayRef<Elf_Word> SymtabSHNDX; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 164 | class ArchiveFile : public InputFile { |
| 165 | public: |
| 166 | explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {} |
| 167 | static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; } |
| 168 | void parse() override; |
| 169 | |
| 170 | // Returns a memory buffer for a given symbol. An empty memory buffer |
| 171 | // is returned if we have already returned the same memory buffer. |
| 172 | // (So that we don't instantiate same members more than once.) |
| 173 | MemoryBufferRef getMember(const Archive::Symbol *Sym); |
| 174 | |
| 175 | llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; } |
| 176 | |
| 177 | private: |
| 178 | std::unique_ptr<Archive> File; |
| 179 | std::vector<Lazy> LazySymbols; |
| 180 | llvm::DenseSet<uint64_t> Seen; |
| 181 | }; |
| 182 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 183 | // .so file. |
| 184 | class SharedFileBase : public ELFFileBase { |
| 185 | public: |
| 186 | explicit SharedFileBase(ELFKind EKind, MemoryBufferRef M) |
| 187 | : ELFFileBase(SharedKind, EKind, M) {} |
| 188 | static bool classof(const InputFile *F) { return F->kind() == SharedKind; } |
| 189 | }; |
| 190 | |
| 191 | template <class ELFT> |
| 192 | class SharedFile : public SharedFileBase, public ELFData<ELFT> { |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 193 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 194 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 195 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 196 | |
| 197 | std::vector<SharedSymbol<ELFT>> SymbolBodies; |
| 198 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 199 | public: |
| Rafael Espindola | 503fe94 | 2015-09-22 16:53:55 +0000 | [diff] [blame] | 200 | using ELFData<ELFT>::getEMachine; |
| Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 201 | llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() { |
| 202 | return SymbolBodies; |
| 203 | } |
| 204 | |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 205 | static bool classof(const InputFile *F) { |
| 206 | return F->kind() == SharedKind && |
| 207 | cast<ELFFileBase>(F)->getELFKind() == getStaticELFKind<ELFT>(); |
| 208 | } |
| 209 | |
| 210 | explicit SharedFile(MemoryBufferRef M) |
| 211 | : SharedFileBase(getStaticELFKind<ELFT>(), M) {} |
| 212 | |
| 213 | void parse() override; |
| 214 | }; |
| 215 | |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 216 | template <template <class> class T> |
| 217 | std::unique_ptr<ELFFileBase> createELFFile(MemoryBufferRef MB) { |
| 218 | using namespace llvm; |
| 219 | |
| 220 | std::pair<unsigned char, unsigned char> Type = |
| 221 | object::getElfArchType(MB.getBuffer()); |
| 222 | if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB) |
| 223 | error("Invalid data encoding"); |
| 224 | |
| 225 | if (Type.first == ELF::ELFCLASS32) { |
| 226 | if (Type.second == ELF::ELFDATA2LSB) |
| 227 | return make_unique<T<object::ELF32LE>>(MB); |
| 228 | return make_unique<T<object::ELF32BE>>(MB); |
| 229 | } |
| 230 | if (Type.first == ELF::ELFCLASS64) { |
| 231 | if (Type.second == ELF::ELFDATA2LSB) |
| 232 | return make_unique<T<object::ELF64LE>>(MB); |
| 233 | return make_unique<T<object::ELF64BE>>(MB); |
| 234 | } |
| 235 | error("Invalid file class"); |
| 236 | } |
| 237 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 238 | } // namespace elf2 |
| 239 | } // namespace lld |
| 240 | |
| 241 | #endif |