forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputChunks.h
355 lines (290 loc) · 11.8 KB
/
InputChunks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//===- InputChunks.h --------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// An InputChunks represents an indivisible opaque region of a input wasm file.
// i.e. a single wasm data segment or a single wasm function.
//
// They are written directly to the mmap'd output file after which relocations
// are applied. Because each Chunk is independent they can be written in
// parallel.
//
// Chunks are also unit on which garbage collection (--gc-sections) operates.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_WASM_INPUT_CHUNKS_H
#define LLD_WASM_INPUT_CHUNKS_H
#include "Config.h"
#include "InputFiles.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/LLVM.h"
#include "llvm/ADT/CachedHashString.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/Wasm.h"
namespace lld {
namespace wasm {
class ObjFile;
class OutputSegment;
class OutputSection;
class InputChunk {
public:
enum Kind {
DataSegment,
Merge,
MergedChunk,
Function,
SyntheticFunction,
Section,
};
StringRef name;
StringRef debugName;
StringRef getName() const { return name; }
StringRef getDebugName() const { return debugName; }
Kind kind() const { return (Kind)sectionKind; }
uint32_t getSize() const;
uint32_t getInputSize() const;
void writeTo(uint8_t *buf) const;
void relocate(uint8_t *buf) const;
ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
// Translate an offset into the input chunk to an offset in the output
// section.
uint64_t getOffset(uint64_t offset) const;
// Translate an offset into the input chunk into an offset into the output
// chunk. For data segments (InputSegment) this will return and offset into
// the output segment. For MergeInputChunk, this will return an offset into
// the parent merged chunk. For other chunk types this is no-op and we just
// return unmodified offset.
uint64_t getChunkOffset(uint64_t offset) const;
uint64_t getVA(uint64_t offset = 0) const;
uint32_t getComdat() const { return comdat; }
StringRef getComdatName() const;
uint32_t getInputSectionOffset() const { return inputSectionOffset; }
size_t getNumRelocations() const { return relocations.size(); }
void writeRelocations(llvm::raw_ostream &os) const;
void generateRelocationCode(raw_ostream &os) const;
bool isTLS() const { return flags & llvm::wasm::WASM_SEG_FLAG_TLS; }
ObjFile *file;
OutputSection *outputSec = nullptr;
uint32_t comdat = UINT32_MAX;
uint32_t inputSectionOffset = 0;
uint32_t alignment;
uint32_t flags;
// Only applies to data segments.
uint32_t outputSegmentOffset = 0;
const OutputSegment *outputSeg = nullptr;
// After assignAddresses is called, this represents the offset from
// the beginning of the output section this chunk was assigned to.
int32_t outSecOff = 0;
uint8_t sectionKind : 3;
// Signals that the section is part of the output. The garbage collector,
// and COMDAT handling can set a sections' Live bit.
// If GC is disabled, all sections start out as live by default.
unsigned live : 1;
// Signals the chunk was discarded by COMDAT handling.
unsigned discarded : 1;
protected:
InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
uint32_t flags = 0)
: name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
live(!config->gcSections), discarded(false) {}
ArrayRef<uint8_t> data() const { return rawData; }
uint64_t getTombstone() const;
ArrayRef<WasmRelocation> relocations;
ArrayRef<uint8_t> rawData;
};
// Represents a WebAssembly data segment which can be included as part of
// an output data segments. Note that in WebAssembly, unlike ELF and other
// formats, used the term "data segment" to refer to the continuous regions of
// memory that make on the data section. See:
// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
//
// For example, by default, clang will produce a separate data section for
// each global variable.
class InputSegment : public InputChunk {
public:
InputSegment(const WasmSegment &seg, ObjFile *f)
: InputChunk(f, InputChunk::DataSegment, seg.Data.Name,
seg.Data.Alignment, seg.Data.LinkingFlags),
segment(seg) {
rawData = segment.Data.Content;
comdat = segment.Data.Comdat;
inputSectionOffset = segment.SectionOffset;
}
static bool classof(const InputChunk *c) { return c->kind() == DataSegment; }
protected:
const WasmSegment &segment;
};
class SyntheticMergedChunk;
// Merge segment handling copied from lld/ELF/InputSection.h. Keep in sync
// where possible.
// SectionPiece represents a piece of splittable segment contents.
// We allocate a lot of these and binary search on them. This means that they
// have to be as compact as possible, which is why we don't store the size (can
// be found by looking at the next one).
struct SectionPiece {
SectionPiece(size_t off, uint32_t hash, bool live)
: inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
uint32_t inputOff;
uint32_t live : 1;
uint32_t hash : 31;
uint64_t outputOff = 0;
};
static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
// This corresponds segments marked as WASM_SEG_FLAG_STRINGS.
class MergeInputChunk : public InputChunk {
public:
MergeInputChunk(const WasmSegment &seg, ObjFile *f)
: InputChunk(f, Merge, seg.Data.Name, seg.Data.Alignment,
seg.Data.LinkingFlags) {
rawData = seg.Data.Content;
comdat = seg.Data.Comdat;
inputSectionOffset = seg.SectionOffset;
}
MergeInputChunk(const WasmSection &s, ObjFile *f)
: InputChunk(f, Merge, s.Name, 0, llvm::wasm::WASM_SEG_FLAG_STRINGS) {
assert(s.Type == llvm::wasm::WASM_SEC_CUSTOM);
comdat = s.Comdat;
rawData = s.Content;
}
static bool classof(const InputChunk *s) { return s->kind() == Merge; }
void splitIntoPieces();
// Translate an offset in the input section to an offset in the parent
// MergeSyntheticSection.
uint64_t getParentOffset(uint64_t offset) const;
// Splittable sections are handled as a sequence of data
// rather than a single large blob of data.
std::vector<SectionPiece> pieces;
// Returns I'th piece's data. This function is very hot when
// string merging is enabled, so we want to inline.
LLVM_ATTRIBUTE_ALWAYS_INLINE
llvm::CachedHashStringRef getData(size_t i) const {
size_t begin = pieces[i].inputOff;
size_t end =
(pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
}
// Returns the SectionPiece at a given input section offset.
SectionPiece *getSectionPiece(uint64_t offset);
const SectionPiece *getSectionPiece(uint64_t offset) const {
return const_cast<MergeInputChunk *>(this)->getSectionPiece(offset);
}
SyntheticMergedChunk *parent = nullptr;
private:
void splitStrings(ArrayRef<uint8_t> a);
};
// SyntheticMergedChunk is a class that allows us to put mergeable
// sections with different attributes in a single output sections. To do that we
// put them into SyntheticMergedChunk synthetic input sections which are
// attached to regular output sections.
class SyntheticMergedChunk : public InputChunk {
public:
SyntheticMergedChunk(StringRef name, uint32_t alignment, uint32_t flags)
: InputChunk(nullptr, InputChunk::MergedChunk, name, alignment, flags),
builder(llvm::StringTableBuilder::RAW, 1ULL << alignment) {}
static bool classof(const InputChunk *c) {
return c->kind() == InputChunk::MergedChunk;
}
void addMergeChunk(MergeInputChunk *ms) {
comdat = ms->getComdat();
ms->parent = this;
chunks.push_back(ms);
}
void finalizeContents();
llvm::StringTableBuilder builder;
protected:
std::vector<MergeInputChunk *> chunks;
};
// Represents a single wasm function within and input file. These are
// combined to create the final output CODE section.
class InputFunction : public InputChunk {
public:
InputFunction(const WasmSignature &s, const WasmFunction *func, ObjFile *f)
: InputChunk(f, InputChunk::Function, func->SymbolName), signature(s),
function(func), exportName(func && func->ExportName.hasValue()
? (*func->ExportName).str()
: llvm::Optional<std::string>()) {
inputSectionOffset = function->CodeSectionOffset;
rawData =
file->codeSection->Content.slice(inputSectionOffset, function->Size);
debugName = function->DebugName;
comdat = function->Comdat;
}
InputFunction(StringRef name, const WasmSignature &s)
: InputChunk(nullptr, InputChunk::Function, name), signature(s) {}
static bool classof(const InputChunk *c) {
return c->kind() == InputChunk::Function ||
c->kind() == InputChunk::SyntheticFunction;
}
llvm::Optional<StringRef> getExportName() const {
return exportName.hasValue() ? llvm::Optional<StringRef>(*exportName)
: llvm::Optional<StringRef>();
}
void setExportName(std::string exportName) { this->exportName = exportName; }
uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
uint32_t getFunctionCodeOffset() const { return function->CodeOffset; }
uint32_t getFunctionIndex() const { return functionIndex.getValue(); }
bool hasFunctionIndex() const { return functionIndex.hasValue(); }
void setFunctionIndex(uint32_t index);
uint32_t getTableIndex() const { return tableIndex.getValue(); }
bool hasTableIndex() const { return tableIndex.hasValue(); }
void setTableIndex(uint32_t index);
void writeCompressed(uint8_t *buf) const;
// The size of a given input function can depend on the values of the
// LEB relocations within it. This finalizeContents method is called after
// all the symbol values have be calculated but before getSize() is ever
// called.
void calculateSize();
const WasmSignature &signature;
uint32_t getCompressedSize() const {
assert(compressedSize);
return compressedSize;
}
const WasmFunction *function;
protected:
llvm::Optional<std::string> exportName;
llvm::Optional<uint32_t> functionIndex;
llvm::Optional<uint32_t> tableIndex;
uint32_t compressedFuncSize = 0;
uint32_t compressedSize = 0;
};
class SyntheticFunction : public InputFunction {
public:
SyntheticFunction(const WasmSignature &s, StringRef name,
StringRef debugName = {})
: InputFunction(name, s) {
sectionKind = InputChunk::SyntheticFunction;
this->debugName = debugName;
}
static bool classof(const InputChunk *c) {
return c->kind() == InputChunk::SyntheticFunction;
}
void setBody(ArrayRef<uint8_t> body) { rawData = body; }
};
// Represents a single Wasm Section within an input file.
class InputSection : public InputChunk {
public:
InputSection(const WasmSection &s, ObjFile *f)
: InputChunk(f, InputChunk::Section, s.Name),
tombstoneValue(getTombstoneForSection(s.Name)), section(s) {
assert(section.Type == llvm::wasm::WASM_SEC_CUSTOM);
comdat = section.Comdat;
rawData = section.Content;
}
static bool classof(const InputChunk *c) {
return c->kind() == InputChunk::Section;
}
const uint64_t tombstoneValue;
protected:
static uint64_t getTombstoneForSection(StringRef name);
const WasmSection §ion;
};
} // namespace wasm
std::string toString(const wasm::InputChunk *);
StringRef relocTypeToString(uint8_t relocType);
} // namespace lld
#endif // LLD_WASM_INPUT_CHUNKS_H