Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/InputFiles.h
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLD_WASM_INPUT_FILES_H
10
#define LLD_WASM_INPUT_FILES_H
11
12
#include "Symbols.h"
13
#include "lld/Common/LLVM.h"
14
#include "llvm/ADT/DenseMap.h"
15
#include "llvm/ADT/DenseSet.h"
16
#include "llvm/LTO/LTO.h"
17
#include "llvm/Object/Archive.h"
18
#include "llvm/Object/Wasm.h"
19
#include "llvm/Support/MemoryBuffer.h"
20
#include <vector>
21
22
namespace lld {
23
namespace wasm {
24
25
class InputChunk;
26
class InputFunction;
27
class InputSegment;
28
class InputGlobal;
29
class InputEvent;
30
class InputSection;
31
32
class InputFile {
33
public:
34
  enum Kind {
35
    ObjectKind,
36
    SharedKind,
37
    ArchiveKind,
38
    BitcodeKind,
39
  };
40
41
348
  virtual ~InputFile() {}
42
43
  // Returns the filename.
44
349
  StringRef getName() const { return MB.getBufferIdentifier(); }
45
46
  // Reads a file (the constructor doesn't do that).
47
  virtual void parse(bool IgnoreComdats = false) = 0;
48
49
1.32k
  Kind kind() const { return FileKind; }
50
51
  // An archive file name if this file is created from an archive.
52
  StringRef ArchiveName;
53
54
495
  ArrayRef<Symbol *> getSymbols() const { return Symbols; }
55
56
protected:
57
348
  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
58
  MemoryBufferRef MB;
59
60
  // List of all symbols referenced or defined by this file.
61
  std::vector<Symbol *> Symbols;
62
63
private:
64
  const Kind FileKind;
65
};
66
67
// .a file (ar archive)
68
class ArchiveFile : public InputFile {
69
public:
70
16
  explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
71
0
  static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
72
73
  void addMember(const llvm::object::Archive::Symbol *Sym);
74
75
  void parse(bool IgnoreComdats) override;
76
77
private:
78
  std::unique_ptr<llvm::object::Archive> File;
79
  llvm::DenseSet<uint64_t> Seen;
80
};
81
82
// .o file (wasm object file)
83
class ObjFile : public InputFile {
84
public:
85
  explicit ObjFile(MemoryBufferRef M, StringRef ArchiveName)
86
286
      : InputFile(ObjectKind, M) {
87
286
    this->ArchiveName = ArchiveName;
88
286
  }
89
251
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
90
91
  void parse(bool IgnoreComdats) override;
92
93
  // Returns the underlying wasm file.
94
2.26k
  const WasmObjectFile *getWasmObj() const { return WasmObj.get(); }
95
96
  void dumpInfo() const;
97
98
  uint32_t calcNewIndex(const WasmRelocation &Reloc) const;
99
  uint32_t calcNewValue(const WasmRelocation &Reloc) const;
100
  uint32_t calcNewAddend(const WasmRelocation &Reloc) const;
101
  uint32_t calcExpectedValue(const WasmRelocation &Reloc) const;
102
8
  Symbol *getSymbol(const WasmRelocation &Reloc) const {
103
8
    return Symbols[Reloc.Index];
104
8
  };
105
106
  const WasmSection *CodeSection = nullptr;
107
  const WasmSection *DataSection = nullptr;
108
109
  // Maps input type indices to output type indices
110
  std::vector<uint32_t> TypeMap;
111
  std::vector<bool> TypeIsUsed;
112
  // Maps function indices to table indices
113
  std::vector<uint32_t> TableEntries;
114
  std::vector<bool> KeptComdats;
115
  std::vector<InputSegment *> Segments;
116
  std::vector<InputFunction *> Functions;
117
  std::vector<InputGlobal *> Globals;
118
  std::vector<InputEvent *> Events;
119
  std::vector<InputSection *> CustomSections;
120
  llvm::DenseMap<uint32_t, InputSection *> CustomSectionsByIndex;
121
122
177
  Symbol *getSymbol(uint32_t Index) const { return Symbols[Index]; }
123
  FunctionSymbol *getFunctionSymbol(uint32_t Index) const;
124
  DataSymbol *getDataSymbol(uint32_t Index) const;
125
  GlobalSymbol *getGlobalSymbol(uint32_t Index) const;
126
  SectionSymbol *getSectionSymbol(uint32_t Index) const;
127
  EventSymbol *getEventSymbol(uint32_t Index) const;
128
129
private:
130
  Symbol *createDefined(const WasmSymbol &Sym);
131
  Symbol *createUndefined(const WasmSymbol &Sym);
132
133
  bool isExcludedByComdat(InputChunk *Chunk) const;
134
135
  std::unique_ptr<WasmObjectFile> WasmObj;
136
};
137
138
// .so file.
139
class SharedFile : public InputFile {
140
public:
141
1
  explicit SharedFile(MemoryBufferRef M) : InputFile(SharedKind, M) {}
142
17
  static bool classof(const InputFile *F) { return F->kind() == SharedKind; }
143
144
1
  void parse(bool IgnoreComdats) override {}
145
};
146
147
// .bc file
148
class BitcodeFile : public InputFile {
149
public:
150
  explicit BitcodeFile(MemoryBufferRef M, StringRef ArchiveName)
151
45
      : InputFile(BitcodeKind, M) {
152
45
    this->ArchiveName = ArchiveName;
153
45
  }
154
294
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
155
156
  void parse(bool IgnoreComdats) override;
157
  std::unique_ptr<llvm::lto::InputFile> Obj;
158
};
159
160
// Will report a fatal() error if the input buffer is not a valid bitcode
161
// or wasm object file.
162
InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "");
163
164
// Opens a given file.
165
llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
166
167
} // namespace wasm
168
169
std::string toString(const wasm::InputFile *File);
170
171
} // namespace lld
172
173
#endif