blob: f6ee6b889b0d27a3c17b34d1de8ea4dc582ea68c [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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
16namespace lld {
17namespace elf2 {
18class SymbolBody;
19class Chunk;
20
21// The root class of input files.
22class InputFile {
23public:
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
34protected:
35 explicit InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
36 MemoryBufferRef MB;
37
38private:
39 const Kind FileKind;
40};
41
42// .o file.
43template <class ELFT> class ObjectFile : public InputFile {
44 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
45 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
46 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
47
48public:
49 explicit ObjectFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
50 static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
51 void parse() override;
52 ArrayRef<Chunk *> getChunks() { return Chunks; }
53 ArrayRef<SymbolBody *> getSymbols() override { return SymbolBodies; }
54
55 // Returns the underying ELF file.
56 llvm::object::ELFFile<ELFT> *getObj() { return ELFObj.get(); }
57
58private:
59 void initializeChunks();
60 void initializeSymbols();
61
62 SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym);
63
64 std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj;
65 llvm::BumpPtrAllocator Alloc;
66
67 // List of all chunks defined by this file. This includes both section
68 // chunks and non-section chunks for common symbols.
69 std::vector<Chunk *> Chunks;
70
71 // List of all symbols referenced or defined by this file.
72 std::vector<SymbolBody *> SymbolBodies;
73};
74
75} // namespace elf2
76} // namespace lld
77
78#endif