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/ADT/CachedHashString.h"
28#include "llvm/MC/StringTableBuilder.h"
29#include "llvm/Object/Wasm.h"
30#include <optional>
31
32namespace lld {
33namespace wasm {
34
35class ObjFile;
36class OutputSegment;
37class OutputSection;
38
39class InputChunk {
40public:
41 enum Kind {
42 DataSegment,
43 Merge,
44 MergedChunk,
45 Function,
46 SyntheticFunction,
47 Section,
48 SyntheticDataSegment,
49 };
50
51 StringRef name;
52 StringRef debugName;
53
54 Kind kind() const { return (Kind)sectionKind; }
55
56 uint32_t getSize() const;
57 uint32_t getInputSize() const;
58
59 void writeTo(uint8_t *buf) const;
60 void relocate(uint8_t *buf) const;
61
62 ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
63 void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
64
65 // Translate an offset into the input chunk to an offset in the output
66 // section.
67 uint64_t getOffset(uint64_t offset) const;
68 // Translate an offset into the input chunk into an offset into the output
69 // chunk. For data segments (InputSegment) this will return and offset into
70 // the output segment. For MergeInputChunk, this will return an offset into
71 // the parent merged chunk. For other chunk types this is no-op and we just
72 // return unmodified offset.
73 uint64_t getChunkOffset(uint64_t offset) const;
74 uint64_t getVA(uint64_t offset = 0) const;
75
76 uint32_t getComdat() const { return comdat; }
77 StringRef getComdatName() const;
78 uint32_t getInputSectionOffset() const { return inputSectionOffset; }
79
80 size_t getNumRelocations() const { return relocations.size(); }
81 size_t getNumLiveRelocations() const;
82 void writeRelocations(llvm::raw_ostream &os) const;
83 bool generateRelocationCode(raw_ostream &os) const;
84
85 bool isTLS() const { return flags & llvm::wasm::WASM_SEG_FLAG_TLS; }
86 bool isRetained() const { return flags & llvm::wasm::WASM_SEG_FLAG_RETAIN; }
87
88 ObjFile *file;
89 OutputSection *outputSec = nullptr;
90 uint32_t comdat = UINT32_MAX;
91 uint32_t inputSectionOffset = 0;
92 uint32_t alignment;
93 uint32_t flags;
94
95 // Only applies to data segments.
96 uint32_t outputSegmentOffset = 0;
97 const OutputSegment *outputSeg = nullptr;
98
99 // After assignAddresses is called, this represents the offset from
100 // the beginning of the output section this chunk was assigned to.
101 //
102 // WASM sections can be up to 4GB. We use a larger, signed integer here to
103 // be able to detect section size overflow instead of a silent wrap-around
104 // and corrupted output sections.
105 int64_t outSecOff = 0;
106
107 uint8_t sectionKind : 3;
108
109 // Signals that the section is part of the output. The garbage collector,
110 // and COMDAT handling can set a sections' Live bit.
111 // If GC is disabled, all sections start out as live by default.
112 unsigned live : 1;
113
114 // Signals the chunk was discarded by COMDAT handling.
115 unsigned discarded : 1;
116
117protected:
118 InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
119 uint32_t flags = 0)
120 : name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
121 live(!ctx.arg.gcSections), discarded(false) {}
122 ArrayRef<uint8_t> data() const { return rawData; }
123 uint64_t getTombstone() const;
124
125 ArrayRef<WasmRelocation> relocations;
126 ArrayRef<uint8_t> rawData;
127};
128
129// Represents a WebAssembly data segment which can be included as part of
130// an output data segments. Note that in WebAssembly, unlike ELF and other
131// formats, used the term "data segment" to refer to the continuous regions of
132// memory that make on the data section. See:
133// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
134//
135// For example, by default, clang will produce a separate data section for
136// each global variable.
137class InputSegment : public InputChunk {
138public:
139 InputSegment(const WasmSegment &seg, ObjFile *f)
140 : InputChunk(f, InputChunk::DataSegment, seg.Data.Name,
141 seg.Data.Alignment, seg.Data.LinkingFlags),
142 segment(seg) {
143 rawData = segment.Data.Content;
144 comdat = segment.Data.Comdat;
145 inputSectionOffset = segment.SectionOffset;
146 }
147
148 static bool classof(const InputChunk *c) { return c->kind() == DataSegment; }
149
150protected:
151 const WasmSegment &segment;
152};
153
154class SyntheticMergedChunk;
155
156// Merge segment handling copied from lld/ELF/InputSection.h. Keep in sync
157// where possible.
158
159// SectionPiece represents a piece of splittable segment contents.
160// We allocate a lot of these and binary search on them. This means that they
161// have to be as compact as possible, which is why we don't store the size (can
162// be found by looking at the next one).
163struct SectionPiece {
164 SectionPiece(size_t off, uint32_t hash, bool live)
165 : inputOff(off), live(live || !ctx.arg.gcSections), hash(hash >> 1) {}
166
167 uint32_t inputOff;
168 uint32_t live : 1;
169 uint32_t hash : 31;
170 uint64_t outputOff = 0;
171};
172
173static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
174
175// This corresponds segments marked as WASM_SEG_FLAG_STRINGS.
176class MergeInputChunk : public InputChunk {
177public:
178 MergeInputChunk(const WasmSegment &seg, ObjFile *f)
179 : InputChunk(f, Merge, seg.Data.Name, seg.Data.Alignment,
180 seg.Data.LinkingFlags) {
181 rawData = seg.Data.Content;
182 comdat = seg.Data.Comdat;
183 inputSectionOffset = seg.SectionOffset;
184 }
185
186 MergeInputChunk(const WasmSection &s, ObjFile *f, uint32_t alignment)
187 : InputChunk(f, Merge, s.Name, alignment,
188 llvm::wasm::WASM_SEG_FLAG_STRINGS) {
189 assert(s.Type == llvm::wasm::WASM_SEC_CUSTOM);
190 comdat = s.Comdat;
191 rawData = s.Content;
192 }
193
194 static bool classof(const InputChunk *s) { return s->kind() == Merge; }
195 void splitIntoPieces();
196
197 // Translate an offset in the input section to an offset in the parent
198 // MergeSyntheticSection.
199 uint64_t getParentOffset(uint64_t offset) const;
200
201 // Splittable sections are handled as a sequence of data
202 // rather than a single large blob of data.
203 std::vector<SectionPiece> pieces;
204
205 // Returns I'th piece's data. This function is very hot when
206 // string merging is enabled, so we want to inline.
207 LLVM_ATTRIBUTE_ALWAYS_INLINE
208 llvm::CachedHashStringRef getData(size_t i) const {
209 size_t begin = pieces[i].inputOff;
210 size_t end =
211 (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
212 return {toStringRef(Input: data().slice(N: begin, M: end - begin)), pieces[i].hash};
213 }
214
215 // Returns the SectionPiece at a given input section offset.
216 SectionPiece *getSectionPiece(uint64_t offset);
217 const SectionPiece *getSectionPiece(uint64_t offset) const {
218 return const_cast<MergeInputChunk *>(this)->getSectionPiece(offset);
219 }
220
221 SyntheticMergedChunk *parent = nullptr;
222
223private:
224 void splitStrings(ArrayRef<uint8_t> a);
225};
226
227// SyntheticMergedChunk is a class that allows us to put mergeable
228// sections with different attributes in a single output sections. To do that we
229// put them into SyntheticMergedChunk synthetic input sections which are
230// attached to regular output sections.
231class SyntheticMergedChunk : public InputChunk {
232public:
233 SyntheticMergedChunk(StringRef name, uint32_t alignment, uint32_t flags)
234 : InputChunk(nullptr, InputChunk::MergedChunk, name, alignment, flags),
235 builder(llvm::StringTableBuilder::RAW, llvm::Align(1ULL << alignment)) {
236 }
237
238 static bool classof(const InputChunk *c) {
239 return c->kind() == InputChunk::MergedChunk;
240 }
241
242 void addMergeChunk(MergeInputChunk *ms) {
243 comdat = ms->getComdat();
244 alignment = std::max(a: alignment, b: ms->alignment);
245 ms->parent = this;
246 chunks.push_back(x: ms);
247 }
248
249 void finalizeContents();
250
251 llvm::StringTableBuilder builder;
252
253protected:
254 std::vector<MergeInputChunk *> chunks;
255};
256
257// Represents a single wasm function within and input file. These are
258// combined to create the final output CODE section.
259class InputFunction : public InputChunk {
260public:
261 InputFunction(const WasmSignature &s, const WasmFunction *func, ObjFile *f)
262 : InputChunk(f, InputChunk::Function, func->SymbolName), signature(s),
263 function(func),
264 exportName(func && func->ExportName ? (*func->ExportName).str()
265 : std::optional<std::string>()) {
266 inputSectionOffset = function->CodeSectionOffset;
267 rawData =
268 file->codeSection->Content.slice(N: inputSectionOffset, M: function->Size);
269 debugName = function->DebugName;
270 comdat = function->Comdat;
271 assert(s.Kind != WasmSignature::Placeholder);
272 }
273
274 InputFunction(StringRef name, const WasmSignature &s)
275 : InputChunk(nullptr, InputChunk::Function, name), signature(s) {
276 assert(s.Kind == WasmSignature::Function);
277 }
278
279 static bool classof(const InputChunk *c) {
280 return c->kind() == InputChunk::Function ||
281 c->kind() == InputChunk::SyntheticFunction;
282 }
283
284 std::optional<StringRef> getExportName() const {
285 return exportName ? std::optional<StringRef>(*exportName)
286 : std::optional<StringRef>();
287 }
288 void setExportName(std::string exportName) { this->exportName = exportName; }
289 uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
290 uint32_t getFunctionCodeOffset() const {
291 // For generated synthetic functions, such as unreachable stubs generated
292 // for signature mismatches, 'function' reference does not exist. This
293 // function is used to get function offsets for .debug_info section, and for
294 // those generated stubs function offsets are not meaningful anyway. So just
295 // return 0 in those cases.
296 return function ? function->CodeOffset : 0;
297 }
298 uint32_t getFunctionIndex() const { return *functionIndex; }
299 bool hasFunctionIndex() const { return functionIndex.has_value(); }
300 void setFunctionIndex(uint32_t index);
301 uint32_t getTableIndex() const { return *tableIndex; }
302 bool hasTableIndex() const { return tableIndex.has_value(); }
303 void setTableIndex(uint32_t index);
304 void writeCompressed(uint8_t *buf) const;
305
306 // The size of a given input function can depend on the values of the
307 // LEB relocations within it. This finalizeContents method is called after
308 // all the symbol values have be calculated but before getSize() is ever
309 // called.
310 void calculateSize();
311
312 const WasmSignature &signature;
313
314 uint32_t getCompressedSize() const {
315 assert(compressedSize);
316 return compressedSize;
317 }
318
319 const WasmFunction *function = nullptr;
320
321protected:
322 std::optional<std::string> exportName;
323 std::optional<uint32_t> functionIndex;
324 std::optional<uint32_t> tableIndex;
325 uint32_t compressedFuncSize = 0;
326 uint32_t compressedSize = 0;
327};
328
329class SyntheticFunction : public InputFunction {
330public:
331 SyntheticFunction(const WasmSignature &s, StringRef name,
332 StringRef debugName = {})
333 : InputFunction(name, s) {
334 sectionKind = InputChunk::SyntheticFunction;
335 this->debugName = debugName;
336 }
337
338 static bool classof(const InputChunk *c) {
339 return c->kind() == InputChunk::SyntheticFunction;
340 }
341
342 void setBody(ArrayRef<uint8_t> body) { rawData = body; }
343};
344
345class SyntheticInputSegment : public InputChunk {
346public:
347 SyntheticInputSegment(StringRef name, uint32_t alignment, uint32_t flags)
348 : InputChunk(nullptr, InputChunk::SyntheticDataSegment, name, alignment,
349 flags) {}
350
351 static bool classof(const InputChunk *c) {
352 return c->kind() == SyntheticDataSegment;
353 }
354
355 void setSize(uint32_t s) { size = s; }
356 uint32_t getSize() const { return size; }
357
358 void writeTo(uint8_t *buf) const;
359
360private:
361 uint32_t size = 0;
362};
363
364// Represents a single Wasm Section within an input file.
365class InputSection : public InputChunk {
366public:
367 InputSection(const WasmSection &s, ObjFile *f, uint32_t alignment)
368 : InputChunk(f, InputChunk::Section, s.Name, alignment),
369 tombstoneValue(getTombstoneForSection(name: s.Name)), section(s) {
370 assert(section.Type == llvm::wasm::WASM_SEC_CUSTOM);
371 comdat = section.Comdat;
372 rawData = section.Content;
373 }
374
375 static bool classof(const InputChunk *c) {
376 return c->kind() == InputChunk::Section;
377 }
378
379 const uint64_t tombstoneValue;
380
381protected:
382 static uint64_t getTombstoneForSection(StringRef name);
383 const WasmSection &section;
384};
385
386} // namespace wasm
387
388std::string toString(const wasm::InputChunk *);
389StringRef relocTypeToString(uint8_t relocType);
390
391} // namespace lld
392
393#endif // LLD_WASM_INPUT_CHUNKS_H
394