//===- InputFiles.h -------------------------------------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLD_ELF_INPUT_FILES_H #define LLD_ELF_INPUT_FILES_H #include "lld/Core/LLVM.h" #include "llvm/Object/ELF.h" namespace lld { namespace elf2 { class SymbolBody; class Chunk; // The root class of input files. class InputFile { public: enum Kind { ObjectKind }; Kind kind() const { return FileKind; } virtual ~InputFile() {} // Returns symbols defined by this file. virtual ArrayRef getSymbols() = 0; // Reads a file (constructors don't do that). virtual void parse() = 0; protected: explicit InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} MemoryBufferRef MB; private: const Kind FileKind; }; // .o file. template class ObjectFile : public InputFile { typedef typename llvm::object::ELFFile::Elf_Sym Elf_Sym; typedef typename llvm::object::ELFFile::Elf_Shdr Elf_Shdr; typedef typename llvm::object::ELFFile::Elf_Sym_Range Elf_Sym_Range; public: explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {} static bool classof(const InputFile *F) { return F->kind() == ObjectKind; } void parse() override; ArrayRef getChunks() { return Chunks; } ArrayRef getSymbols() override { return SymbolBodies; } // Returns the underying ELF file. llvm::object::ELFFile *getObj() { return ELFObj.get(); } private: void initializeChunks(); void initializeSymbols(); SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym); std::unique_ptr> ELFObj; llvm::BumpPtrAllocator Alloc; // List of all chunks defined by this file. This includes both section // chunks and non-section chunks for common symbols. std::vector Chunks; // List of all symbols referenced or defined by this file. std::vector SymbolBodies; }; } // namespace elf2 } // namespace lld #endif