Coverage Report

Created: 2019-05-19 14:56

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