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