Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/InputChunks.h
Line
Count
Source (jump to first uncovered line)
1
//===- InputChunks.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
// An InputChunks represents an indivisible opaque region of a input wasm file.
11
// i.e. a single wasm data segment or a single wasm function.
12
//
13
// They are written directly to the mmap'd output file after which relocations
14
// are applied.  Because each Chunk is independent they can be written in
15
// parallel.
16
//
17
// Chunks are also unit on which garbage collection (--gc-sections) operates.
18
//
19
//===----------------------------------------------------------------------===//
20
21
#ifndef LLD_WASM_INPUT_CHUNKS_H
22
#define LLD_WASM_INPUT_CHUNKS_H
23
24
#include "Config.h"
25
#include "InputFiles.h"
26
#include "lld/Common/ErrorHandler.h"
27
#include "llvm/Object/Wasm.h"
28
29
using llvm::object::WasmSection;
30
using llvm::object::WasmSegment;
31
using llvm::wasm::WasmFunction;
32
using llvm::wasm::WasmRelocation;
33
using llvm::wasm::WasmSignature;
34
35
namespace llvm {
36
class raw_ostream;
37
}
38
39
namespace lld {
40
namespace wasm {
41
42
class ObjFile;
43
class OutputSegment;
44
45
class InputChunk {
46
public:
47
  enum Kind { DataSegment, Function, SyntheticFunction, Section };
48
49
0
  Kind kind() const { return SectionKind; }
50
51
0
  virtual uint32_t getSize() const { return data().size(); }
52
53
  void copyRelocations(const WasmSection &Section);
54
55
  virtual void writeTo(uint8_t *SectionStart) const;
56
57
0
  ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
58
59
  virtual StringRef getName() const = 0;
60
  virtual StringRef getDebugName() const = 0;
61
  virtual uint32_t getComdat() const = 0;
62
  StringRef getComdatName() const;
63
64
0
  size_t NumRelocations() const { return Relocations.size(); }
65
  void writeRelocations(llvm::raw_ostream &OS) const;
66
67
  ObjFile *File;
68
  int32_t OutputOffset = 0;
69
70
  // Signals that the section is part of the output.  The garbage collector,
71
  // and COMDAT handling can set a sections' Live bit.
72
  // If GC is disabled, all sections start out as live by default.
73
  unsigned Live : 1;
74
75
protected:
76
  InputChunk(ObjFile *F, Kind K)
77
0
      : File(F), Live(!Config->GcSections), SectionKind(K) {}
78
0
  virtual ~InputChunk() = default;
79
  virtual ArrayRef<uint8_t> data() const = 0;
80
  virtual uint32_t getInputSectionOffset() const = 0;
81
0
  virtual uint32_t getInputSize() const { return getSize(); };
82
83
  // Verifies the existing data at relocation targets matches our expectations.
84
  // This is performed only debug builds as an extra sanity check.
85
  void verifyRelocTargets() const;
86
87
  std::vector<WasmRelocation> Relocations;
88
  Kind SectionKind;
89
};
90
91
// Represents a WebAssembly data segment which can be included as part of
92
// an output data segments.  Note that in WebAssembly, unlike ELF and other
93
// formats, used the term "data segment" to refer to the continous regions of
94
// memory that make on the data section. See:
95
// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
96
//
97
// For example, by default, clang will produce a separate data section for
98
// each global variable.
99
class InputSegment : public InputChunk {
100
public:
101
  InputSegment(const WasmSegment &Seg, ObjFile *F)
102
0
      : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
103
104
0
  static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
105
106
0
  uint32_t getAlignment() const { return Segment.Data.Alignment; }
107
0
  StringRef getName() const override { return Segment.Data.Name; }
108
0
  StringRef getDebugName() const override { return StringRef(); }
109
0
  uint32_t getComdat() const override { return Segment.Data.Comdat; }
110
111
  const OutputSegment *OutputSeg = nullptr;
112
  int32_t OutputSegmentOffset = 0;
113
114
protected:
115
0
  ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
116
0
  uint32_t getInputSectionOffset() const override {
117
0
    return Segment.SectionOffset;
118
0
  }
119
120
  const WasmSegment &Segment;
121
};
122
123
// Represents a single wasm function within and input file.  These are
124
// combined to create the final output CODE section.
125
class InputFunction : public InputChunk {
126
public:
127
  InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F)
128
0
      : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
129
130
0
  static bool classof(const InputChunk *C) {
131
0
    return C->kind() == InputChunk::Function ||
132
0
           C->kind() == InputChunk::SyntheticFunction;
133
0
  }
134
135
  void writeTo(uint8_t *SectionStart) const override;
136
0
  StringRef getName() const override { return Function->SymbolName; }
137
0
  StringRef getDebugName() const override { return Function->DebugName; }
138
0
  uint32_t getComdat() const override { return Function->Comdat; }
139
0
  uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
140
0
  uint32_t getFunctionCodeOffset() const { return Function->CodeOffset; }
141
0
  uint32_t getSize() const override {
142
0
    if (Config->CompressRelocTargets && File) {
143
0
      assert(CompressedSize);
144
0
      return CompressedSize;
145
0
    }
146
0
    return data().size();
147
0
  }
148
0
  uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); }
149
0
  bool hasFunctionIndex() const { return FunctionIndex.hasValue(); }
150
  void setFunctionIndex(uint32_t Index);
151
0
  uint32_t getTableIndex() const { return TableIndex.getValue(); }
152
0
  bool hasTableIndex() const { return TableIndex.hasValue(); }
153
  void setTableIndex(uint32_t Index);
154
155
  // The size of a given input function can depend on the values of the
156
  // LEB relocations within it.  This finalizeContents method is called after
157
  // all the symbol values have be calcualted but before getSize() is ever
158
  // called.
159
  void calculateSize();
160
161
  const WasmSignature &Signature;
162
163
protected:
164
0
  ArrayRef<uint8_t> data() const override {
165
0
    assert(!Config->CompressRelocTargets);
166
0
    return File->CodeSection->Content.slice(getInputSectionOffset(),
167
0
                                            Function->Size);
168
0
  }
169
170
0
  uint32_t getInputSize() const override { return Function->Size; }
171
172
0
  uint32_t getInputSectionOffset() const override {
173
0
    return Function->CodeSectionOffset;
174
0
  }
175
176
  const WasmFunction *Function;
177
  llvm::Optional<uint32_t> FunctionIndex;
178
  llvm::Optional<uint32_t> TableIndex;
179
  uint32_t CompressedFuncSize = 0;
180
  uint32_t CompressedSize = 0;
181
};
182
183
class SyntheticFunction : public InputFunction {
184
public:
185
  SyntheticFunction(const WasmSignature &S, StringRef Name,
186
                    StringRef DebugName = {})
187
0
      : InputFunction(S, nullptr, nullptr), Name(Name), DebugName(DebugName) {
188
0
    SectionKind = InputChunk::SyntheticFunction;
189
0
  }
190
191
0
  static bool classof(const InputChunk *C) {
192
0
    return C->kind() == InputChunk::SyntheticFunction;
193
0
  }
194
195
0
  StringRef getName() const override { return Name; }
196
0
  StringRef getDebugName() const override { return DebugName; }
197
0
  uint32_t getComdat() const override { return UINT32_MAX; }
198
199
0
  void setBody(ArrayRef<uint8_t> Body_) { Body = Body_; }
200
201
protected:
202
0
  ArrayRef<uint8_t> data() const override { return Body; }
203
204
  StringRef Name;
205
  StringRef DebugName;
206
  ArrayRef<uint8_t> Body;
207
};
208
209
// Represents a single Wasm Section within an input file.
210
class InputSection : public InputChunk {
211
public:
212
  InputSection(const WasmSection &S, ObjFile *F)
213
0
      : InputChunk(F, InputChunk::Section), Section(S) {
214
0
    assert(Section.Type == llvm::wasm::WASM_SEC_CUSTOM);
215
0
  }
216
217
0
  StringRef getName() const override { return Section.Name; }
218
0
  StringRef getDebugName() const override { return StringRef(); }
219
0
  uint32_t getComdat() const override { return UINT32_MAX; }
220
221
protected:
222
0
  ArrayRef<uint8_t> data() const override { return Section.Content; }
223
224
  // Offset within the input section.  This is only zero since this chunk
225
  // type represents an entire input section, not part of one.
226
0
  uint32_t getInputSectionOffset() const override { return 0; }
227
228
  const WasmSection &Section;
229
};
230
231
} // namespace wasm
232
233
std::string toString(const wasm::InputChunk *);
234
} // namespace lld
235
236
#endif // LLD_WASM_INPUT_CHUNKS_H