Coverage Report

Created: 2019-01-18 03:29

/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
//                             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_WASM_INPUT_FILES_H
11
#define LLD_WASM_INPUT_FILES_H
12
13
#include "Symbols.h"
14
#include "lld/Common/LLVM.h"
15
#include "llvm/ADT/DenseMap.h"
16
#include "llvm/ADT/DenseSet.h"
17
#include "llvm/LTO/LTO.h"
18
#include "llvm/Object/Archive.h"
19
#include "llvm/Object/Wasm.h"
20
#include "llvm/Support/MemoryBuffer.h"
21
#include <vector>
22
23
namespace lld {
24
namespace wasm {
25
26
class InputChunk;
27
class InputFunction;
28
class InputSegment;
29
class InputGlobal;
30
class InputEvent;
31
class InputSection;
32
33
class InputFile {
34
public:
35
  enum Kind {
36
    ObjectKind,
37
    ArchiveKind,
38
    BitcodeKind,
39
  };
40
41
258
  virtual ~InputFile() {}
42
43
  // Returns the filename.
44
253
  StringRef getName() const { return MB.getBufferIdentifier(); }
45
46
  // Reads a file (the constructor doesn't do that).
47
  virtual void parse() = 0;
48
49
1.02k
  Kind kind() const { return FileKind; }
50
51
  // An archive file name if this file is created from an archive.
52
  StringRef ArchiveName;
53
54
70
  ArrayRef<Symbol *> getSymbols() const { return Symbols; }
55
56
protected:
57
258
  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
15
  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() 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
205
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
86
173
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
87
88
  void parse() override;
89
90
  // Returns the underlying wasm file.
91
1.60k
  const WasmObjectFile *getWasmObj() const { return WasmObj.get(); }
92
93
  void dumpInfo() const;
94
95
  uint32_t calcNewIndex(const WasmRelocation &Reloc) const;
96
  uint32_t calcNewValue(const WasmRelocation &Reloc) const;
97
  uint32_t calcNewAddend(const WasmRelocation &Reloc) const;
98
  uint32_t calcExpectedValue(const WasmRelocation &Reloc) const;
99
100
  const WasmSection *CodeSection = nullptr;
101
  const WasmSection *DataSection = nullptr;
102
  const WasmSection *ProducersSection = nullptr;
103
104
  // Maps input type indices to output type indices
105
  std::vector<uint32_t> TypeMap;
106
  std::vector<bool> TypeIsUsed;
107
  // Maps function indices to table indices
108
  std::vector<uint32_t> TableEntries;
109
  std::vector<bool> UsedComdats;
110
  std::vector<InputSegment *> Segments;
111
  std::vector<InputFunction *> Functions;
112
  std::vector<InputGlobal *> Globals;
113
  std::vector<InputEvent *> Events;
114
  std::vector<InputSection *> CustomSections;
115
  llvm::DenseMap<uint32_t, InputSection *> CustomSectionsByIndex;
116
117
149
  Symbol *getSymbol(uint32_t Index) const { return Symbols[Index]; }
118
  FunctionSymbol *getFunctionSymbol(uint32_t Index) const;
119
  DataSymbol *getDataSymbol(uint32_t Index) const;
120
  GlobalSymbol *getGlobalSymbol(uint32_t Index) const;
121
  SectionSymbol *getSectionSymbol(uint32_t Index) const;
122
  EventSymbol *getEventSymbol(uint32_t Index) const;
123
124
private:
125
  Symbol *createDefined(const WasmSymbol &Sym);
126
  Symbol *createUndefined(const WasmSymbol &Sym);
127
128
  bool isExcludedByComdat(InputChunk *Chunk) const;
129
130
  std::unique_ptr<WasmObjectFile> WasmObj;
131
};
132
133
class BitcodeFile : public InputFile {
134
public:
135
38
  explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
136
211
  static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
137
138
  void parse() override;
139
  std::unique_ptr<llvm::lto::InputFile> Obj;
140
};
141
142
// Will report a fatal() error if the input buffer is not a valid bitcode
143
// or wasm object file.
144
InputFile *createObjectFile(MemoryBufferRef MB);
145
146
// Opens a given file.
147
llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
148
149
} // namespace wasm
150
151
std::string toString(const wasm::InputFile *File);
152
153
} // namespace lld
154
155
#endif