summaryrefslogtreecommitdiffstats
path: root/lld/ELF/InputFiles.h
blob: 78996ee593d9b21a3fc609440b792feb3bf6ad6a (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
//
//                             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 "Symbols.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 { ObjectKind };
  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;
};

enum ELFKind { ELF32LEKind, ELF32BEKind, ELF64LEKind, ELF64BEKind };

// .o file.
class ObjectFileBase : public InputFile {
public:
  explicit ObjectFileBase(ELFKind EKind, MemoryBufferRef M)
      : InputFile(ObjectKind, M), EKind(EKind) {}
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }

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

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

  ELFKind getELFKind() const { return EKind; }

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

  llvm::BumpPtrAllocator Alloc;
  const ELFKind EKind;
};

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;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;

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

  static ELFKind getStaticELFKind() {
    if (!ELFT::Is64Bits) {
      if (ELFT::TargetEndianness == llvm::support::little)
        return ELF32LEKind;
      return ELF32BEKind;
    }
    if (ELFT::TargetEndianness == llvm::support::little)
      return ELF64LEKind;
    return ELF64BEKind;
  }

  static bool classof(const InputFile *F) {
    return F->kind() == ObjectKind &&
           cast<ObjectFileBase>(F)->getELFKind() == getStaticELFKind();
  }

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

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

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

  SymbolBody *getSymbolBody(uint32_t SymbolIndex) {
    uint32_t FirstNonLocal = Symtab->sh_info;
    if (SymbolIndex < FirstNonLocal)
      return nullptr;
    return SymbolBodies[SymbolIndex - FirstNonLocal]->getReplacement();
  }

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;

  const Elf_Shdr *Symtab = nullptr;
  ArrayRef<Elf_Word> SymtabSHNDX;
};

} // namespace elf2
} // namespace lld

#endif
OpenPOWER on IntegriCloud