summaryrefslogtreecommitdiffstats
path: root/lld/ELF/InputFiles.h
blob: acbcd2d7f363bc30a78d039d707bdab03d18b029 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//===- 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 "Config.h"
#include "InputSection.h"
#include "Error.h"
#include "Symbols.h"

#include "lld/Core/LLVM.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ELF.h"

namespace lld {
namespace elf2 {

using llvm::object::Archive;

class InputFile;
class Lazy;
class SymbolBody;

// The root class of input files.
class InputFile {
public:
  enum Kind { ObjectKind, SharedKind, ArchiveKind };
  Kind kind() const { return FileKind; }
  virtual ~InputFile() {}

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

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

private:
  const Kind FileKind;
};

template <typename ELFT> class ELFFileBase : public InputFile {
public:
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;

  ELFFileBase(Kind K, ELFKind EKind, MemoryBufferRef M);
  static bool classof(const InputFile *F) {
    Kind K = F->kind();
    return K == ObjectKind || K == SharedKind;
  }

  ELFKind getELFKind() const { return EKind; }

  const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
  llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }

  uint16_t getEMachine() const { return getObj().getHeader()->e_machine; }
  uint8_t getOSABI() const {
    return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
  }

  StringRef getStringTable() const { return StringTable; }

protected:
  const ELFKind EKind;
  llvm::object::ELFFile<ELFT> ELFObj;
  const Elf_Shdr *Symtab = nullptr;
  StringRef StringTable;
  void initStringTable();
  Elf_Sym_Range getNonLocalSymbols();
  Elf_Sym_Range getSymbolsHelper(bool);
};

// .o file.
template <typename ELFT> class ObjectFileBase : public ELFFileBase<ELFT> {
  typedef ELFFileBase<ELFT> Base;

public:
  ObjectFileBase(ELFKind EKind, MemoryBufferRef M)
      : ELFFileBase<ELFT>(Base::ObjectKind, EKind, M) {}
  static bool classof(const InputFile *F) {
    return F->kind() == Base::ObjectKind;
  }

  ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; }
  virtual void parse(llvm::DenseSet<StringRef> &Comdats) = 0;

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

  llvm::BumpPtrAllocator Alloc;
};

template <class ELFT> 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;
}

template <class ELFT> class ObjectFile : public ObjectFileBase<ELFT> {
  typedef ObjectFileBase<ELFT> Base;
  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;

  typedef llvm::support::detail::packed_endian_specific_integral<
      uint32_t, ELFT::TargetEndianness, 2> GroupEntryType;
  StringRef getShtGroupSignature(const Elf_Shdr &Sec);
  ArrayRef<GroupEntryType> getShtGroupEntries(const Elf_Shdr &Sec);

public:
  static bool classof(const InputFile *F) {
    return F->kind() == Base::ObjectKind &&
           cast<ELFFileBase<ELFT>>(F)->getELFKind() == getStaticELFKind<ELFT>();
  }

  explicit ObjectFile(MemoryBufferRef M);
  void parse(llvm::DenseSet<StringRef> &Comdats) override;

  ArrayRef<InputSection<ELFT> *> getSections() const { return Sections; }

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

  Elf_Sym_Range getLocalSymbols();

  const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
  ArrayRef<Elf_Word> getSymbolTableShndx() const { return SymtabSHNDX; };

private:
  void initializeSections(llvm::DenseSet<StringRef> &Comdats);
  void initializeSymbols();

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

  // List of all sections defined by this file.
  std::vector<InputSection<ELFT> *> Sections;

  ArrayRef<Elf_Word> SymtabSHNDX;
};

class ArchiveFile : public InputFile {
public:
  explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
  static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
  void parse();

  // Returns a memory buffer for a given symbol. An empty memory buffer
  // is returned if we have already returned the same memory buffer.
  // (So that we don't instantiate same members more than once.)
  MemoryBufferRef getMember(const Archive::Symbol *Sym);

  llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; }
  std::vector<MemoryBufferRef> getMembers();

private:
  std::unique_ptr<Archive> File;
  std::vector<Lazy> LazySymbols;
  llvm::DenseSet<uint64_t> Seen;
};

// .so file.
template <typename ELFT> class SharedFileBase : public ELFFileBase<ELFT> {
  typedef ELFFileBase<ELFT> Base;

protected:
  StringRef SoName;

public:
  SharedFileBase(ELFKind EKind, MemoryBufferRef M)
      : ELFFileBase<ELFT>(Base::SharedKind, EKind, M) {
    AsNeeded = Config->AsNeeded;
  }
  static bool classof(const InputFile *F) {
    return F->kind() == Base::SharedKind;
  }
  StringRef getSoName() const { return SoName; }
  virtual void parseSoName() = 0;
  virtual void parse() = 0;

  // Used for --as-needed
  bool AsNeeded = false;
  bool IsUsed = false;
  bool isNeeded() const { return !AsNeeded || IsUsed; }
};

template <class ELFT> class SharedFile : public SharedFileBase<ELFT> {
  typedef SharedFileBase<ELFT> Base;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
  typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;

  std::vector<SharedSymbol<ELFT>> SymbolBodies;

public:
  llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() {
    return SymbolBodies;
  }

  static bool classof(const InputFile *F) {
    return F->kind() == Base::SharedKind &&
           cast<ELFFileBase<ELFT>>(F)->getELFKind() == getStaticELFKind<ELFT>();
  }

  explicit SharedFile(MemoryBufferRef M);

  void parseSoName() override;
  void parse() override;
};

template <typename T>
std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
  std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);

  if (!Config->FirstElf)
    Config->FirstElf = Ret.get();

  if (Config->ElfKind == ELFNoneKind) {
    Config->ElfKind = Ret->getELFKind();
    Config->EMachine = Ret->getEMachine();
  }

  return std::move(Ret);
}

template <template <class> class T>
std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
  using namespace llvm;

  std::pair<unsigned char, unsigned char> Type =
    object::getElfArchType(MB.getBuffer());
  if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
    error("Invalid data encoding: " + MB.getBufferIdentifier());

  if (Type.first == ELF::ELFCLASS32) {
    if (Type.second == ELF::ELFDATA2LSB)
      return createELFFileAux<T<object::ELF32LE>>(MB);
    return createELFFileAux<T<object::ELF32BE>>(MB);
  }
  if (Type.first == ELF::ELFCLASS64) {
    if (Type.second == ELF::ELFDATA2LSB)
      return createELFFileAux<T<object::ELF64LE>>(MB);
    return createELFFileAux<T<object::ELF64BE>>(MB);
  }
  error("Invalid file class: " + MB.getBufferIdentifier());
}

} // namespace elf2
} // namespace lld

#endif
OpenPOWER on IntegriCloud