blob: b3f02f3995fba3e713bea63876cb187dcc72f5b6 [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.
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000043class ObjectFileBase : public InputFile {
44public:
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
51protected:
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
62template <class ELFT> class ObjectFile : public ObjectFileBase {
Michael J. Spencer84487f12015-07-24 21:03:07 +000063 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
67public:
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000068 explicit ObjectFile(MemoryBufferRef M) : ObjectFileBase(M) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000069 void parse() override;
Michael J. Spencer84487f12015-07-24 21:03:07 +000070
71 // Returns the underying ELF file.
72 llvm::object::ELFFile<ELFT> *getObj() { return ELFObj.get(); }
73
74private:
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. Spencer84487f12015-07-24 21:03:07 +000081};
82
83} // namespace elf2
84} // namespace lld
85
86#endif