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