| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- InputFiles.h -------------------------------------------------------===// |
| 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 | |
| 13 | #include "lld/Core/LLVM.h" |
| 14 | #include "llvm/Object/ELF.h" |
| 15 | |
| 16 | namespace lld { |
| 17 | namespace elf2 { |
| 18 | class SymbolBody; |
| 19 | class Chunk; |
| 20 | |
| 21 | // The root class of input files. |
| 22 | class InputFile { |
| 23 | public: |
| 24 | enum Kind { ObjectKind }; |
| 25 | Kind kind() const { return FileKind; } |
| 26 | virtual ~InputFile() {} |
| 27 | |
| 28 | // Returns symbols defined by this file. |
| 29 | virtual ArrayRef<SymbolBody *> getSymbols() = 0; |
| 30 | |
| 31 | // Reads a file (constructors don't do that). |
| 32 | virtual void parse() = 0; |
| 33 | |
| 34 | protected: |
| 35 | explicit InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} |
| 36 | MemoryBufferRef MB; |
| 37 | |
| 38 | private: |
| 39 | const Kind FileKind; |
| 40 | }; |
| 41 | |
| 42 | // .o file. |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame^] | 43 | class ObjectFileBase : public InputFile { |
| 44 | public: |
| 45 | explicit ObjectFileBase(MemoryBufferRef M) : InputFile(ObjectKind, M) {} |
| 46 | static bool classof(const InputFile *F) { return F->kind() == ObjectKind; } |
| 47 | |
| 48 | ArrayRef<Chunk *> getChunks() { return Chunks; } |
| 49 | ArrayRef<SymbolBody *> getSymbols() override { return SymbolBodies; } |
| 50 | |
| 51 | protected: |
| 52 | // List of all chunks defined by this file. This includes both section |
| 53 | // chunks and non-section chunks for common symbols. |
| 54 | std::vector<Chunk *> Chunks; |
| 55 | |
| 56 | // List of all symbols referenced or defined by this file. |
| 57 | std::vector<SymbolBody *> SymbolBodies; |
| 58 | |
| 59 | llvm::BumpPtrAllocator Alloc; |
| 60 | }; |
| 61 | |
| 62 | template <class ELFT> class ObjectFile : public ObjectFileBase { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 63 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 64 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 65 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 66 | |
| 67 | public: |
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame^] | 68 | explicit ObjectFile(MemoryBufferRef M) : ObjectFileBase(M) {} |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 69 | void parse() override; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | |
| 71 | // Returns the underying ELF file. |
| 72 | llvm::object::ELFFile<ELFT> *getObj() { return ELFObj.get(); } |
| 73 | |
| 74 | private: |
| 75 | void initializeChunks(); |
| 76 | void initializeSymbols(); |
| 77 | |
| 78 | SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym); |
| 79 | |
| 80 | std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace elf2 |
| 84 | } // namespace lld |
| 85 | |
| 86 | #endif |