summaryrefslogtreecommitdiffstats
path: root/lld/ELF/InputFiles.h
blob: 66a8c9d5b48cd2ff8c0be0460e20c3873a86f1bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===- 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 "Chunks.h"
#include "lld/Core/LLVM.h"
#include "llvm/Object/ELF.h"

namespace lld {
namespace elf2 {
class SymbolBody;

// The root class of input files.
class InputFile {
public:
  enum Kind { Object32LEKind, Object32BEKind, Object64LEKind, Object64BEKind };
  Kind kind() const { return FileKind; }
  virtual ~InputFile() {}

  // Returns symbols defined by this file.
  virtual ArrayRef<SymbolBody *> getSymbols() = 0;

  // Reads a file (constructors don't do that).
  virtual void parse() = 0;

  StringRef getName() const { return MB.getBufferIdentifier(); }

protected:
  explicit InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
  MemoryBufferRef MB;

private:
  const Kind FileKind;
};

// .o file.
class ObjectFileBase : public InputFile {
public:
  explicit ObjectFileBase(Kind K, MemoryBufferRef M) : InputFile(K, M) {}
  static bool classof(const InputFile *F) {
    Kind K = F->kind();
    return K >= Object32LEKind && K <= Object64BEKind;
  }

  ArrayRef<SymbolBody *> getSymbols() override { return SymbolBodies; }

  virtual bool isCompatibleWith(const ObjectFileBase &Other) const = 0;

protected:
  // List of all symbols referenced or defined by this file.
  std::vector<SymbolBody *> SymbolBodies;

  llvm::BumpPtrAllocator Alloc;
};

template <class ELFT> class ObjectFile : public ObjectFileBase {
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;

public:
  bool isCompatibleWith(const ObjectFileBase &Other) const override;

  static Kind getKind() {
    if (!ELFT::Is64Bits) {
      if (ELFT::TargetEndianness == llvm::support::little)
        return Object32LEKind;
      return Object32BEKind;
    }
    if (ELFT::TargetEndianness == llvm::support::little)
      return Object64LEKind;
    return Object64BEKind;
  }

  static bool classof(const InputFile *F) { return F->kind() == getKind(); }

  explicit ObjectFile(MemoryBufferRef M) : ObjectFileBase(getKind(), M) {}
  void parse() override;

  // Returns the underying ELF file.
  llvm::object::ELFFile<ELFT> *getObj() const { return ELFObj.get(); }

  ArrayRef<SectionChunk<ELFT> *> getChunks() { return Chunks; }

private:
  void initializeChunks();
  void initializeSymbols();

  SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym);

  std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj;

  // List of all chunks defined by this file.
  std::vector<SectionChunk<ELFT> *> Chunks;
};

} // namespace elf2
} // namespace lld

#endif
OpenPOWER on IntegriCloud