Coverage Report

Created: 2018-01-17 17:22

/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 "lld/Common/LLVM.h"
14
#include "llvm/ADT/DenseMap.h"
15
#include "llvm/ADT/DenseSet.h"
16
#include "llvm/Object/Archive.h"
17
#include "llvm/Object/Wasm.h"
18
#include "llvm/Support/MemoryBuffer.h"
19
20
#include "Symbols.h"
21
#include "WriterUtils.h"
22
23
#include <vector>
24
25
using llvm::object::Archive;
26
using llvm::object::WasmObjectFile;
27
using llvm::object::WasmSection;
28
using llvm::object::WasmSymbol;
29
using llvm::wasm::WasmImport;
30
using llvm::wasm::WasmSignature;
31
using llvm::wasm::WasmRelocation;
32
33
namespace lld {
34
namespace wasm {
35
36
class InputChunk;
37
class InputFunction;
38
class InputSegment;
39
40
class InputFile {
41
public:
42
  enum Kind {
43
    ObjectKind,
44
    ArchiveKind,
45
  };
46
47
0
  virtual ~InputFile() {}
48
49
  // Returns the filename.
50
0
  StringRef getName() const { return MB.getBufferIdentifier(); }
51
52
  // Reads a file (the constructor doesn't do that).
53
  virtual void parse() = 0;
54
55
0
  Kind kind() const { return FileKind; }
56
57
  // An archive file name if this file is created from an archive.
58
  StringRef ParentName;
59
60
protected:
61
0
  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
62
  MemoryBufferRef MB;
63
64
private:
65
  const Kind FileKind;
66
};
67
68
// .a file (ar archive)
69
class ArchiveFile : public InputFile {
70
public:
71
0
  explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
72
0
  static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
73
74
  void addMember(const Archive::Symbol *Sym);
75
76
  void parse() override;
77
78
private:
79
  std::unique_ptr<Archive> File;
80
  llvm::DenseSet<uint64_t> Seen;
81
};
82
83
// .o file (wasm object file)
84
class ObjFile : public InputFile {
85
public:
86
0
  explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
87
0
  static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
88
89
  void parse() override;
90
91
  // Returns the underlying wasm file.
92
0
  const WasmObjectFile *getWasmObj() const { return WasmObj.get(); }
93
94
  void dumpInfo() const;
95
96
  uint32_t relocateFunctionIndex(uint32_t Original) const;
97
  uint32_t getRelocatedAddress(uint32_t Index) const;
98
  uint32_t calcNewIndex(const WasmRelocation &Reloc) const;
99
100
  const WasmSection *CodeSection = nullptr;
101
  const WasmSection *DataSection = nullptr;
102
103
  std::vector<uint32_t> TypeMap;
104
  std::vector<InputSegment *> Segments;
105
  std::vector<InputFunction *> Functions;
106
107
0
  ArrayRef<Symbol *> getSymbols() { return Symbols; }
108
0
  ArrayRef<Symbol *> getTableSymbols() { return TableSymbols; }
109
110
private:
111
  uint32_t relocateTypeIndex(uint32_t Original) const;
112
  uint32_t relocateGlobalIndex(uint32_t Original) const;
113
  uint32_t relocateTableIndex(uint32_t Original) const;
114
115
  Symbol *createDefined(const WasmSymbol &Sym, Symbol::Kind Kind,
116
                        const InputSegment *Segment = nullptr,
117
                        InputFunction *Function = nullptr,
118
                        uint32_t Address = UINT32_MAX);
119
  Symbol *createUndefined(const WasmSymbol &Sym, Symbol::Kind Kind,
120
                          const WasmSignature *Signature = nullptr);
121
  void initializeSymbols();
122
  InputSegment *getSegment(const WasmSymbol &WasmSym) const;
123
  const WasmSignature *getFunctionSig(const WasmSymbol &Sym) const;
124
  uint32_t getGlobalValue(const WasmSymbol &Sym) const;
125
  InputFunction *getFunction(const WasmSymbol &Sym) const;
126
  bool isExcludedByComdat(InputChunk *Chunk) const;
127
128
  // List of all symbols referenced or defined by this file.
129
  std::vector<Symbol *> Symbols;
130
131
  // List of all function symbols indexed by the function index space
132
  std::vector<Symbol *> FunctionSymbols;
133
134
  // List of all global symbols indexed by the global index space
135
  std::vector<Symbol *> GlobalSymbols;
136
137
  // List of all indirect symbols indexed by table index space.
138
  std::vector<Symbol *> TableSymbols;
139
140
  uint32_t NumGlobalImports = 0;
141
  uint32_t NumFunctionImports = 0;
142
  std::unique_ptr<WasmObjectFile> WasmObj;
143
};
144
145
// Opens a given file.
146
llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
147
148
} // namespace wasm
149
150
std::string toString(const wasm::InputFile *File);
151
152
} // namespace lld
153
154
#endif