1 //===- InputChunks.h --------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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
17 // Chunks are also unit on which garbage collection (--gc-sections) operates.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLD_WASM_INPUT_CHUNKS_H
22 #define LLD_WASM_INPUT_CHUNKS_H
25 #include "InputFiles.h"
26 #include "lld/Common/ErrorHandler.h"
27 #include "llvm/Object/Wasm.h"
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;
47 enum Kind { DataSegment, Function, SyntheticFunction, Section };
49 Kind kind() const { return SectionKind; }
51 virtual uint32_t getSize() const { return data().size(); }
53 void copyRelocations(const WasmSection &Section);
55 virtual void writeTo(uint8_t *SectionStart) const;
57 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
59 virtual StringRef getName() const = 0;
60 virtual StringRef getDebugName() const = 0;
61 virtual uint32_t getComdat() const = 0;
62 StringRef getComdatName() const;
64 size_t NumRelocations() const { return Relocations.size(); }
65 void writeRelocations(llvm::raw_ostream &OS) const;
68 int32_t OutputOffset = 0;
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.
76 InputChunk(ObjFile *F, Kind K)
77 : File(F), Live(!Config->GcSections), SectionKind(K) {}
78 virtual ~InputChunk() = default;
79 virtual ArrayRef<uint8_t> data() const = 0;
80 virtual uint32_t getInputSectionOffset() const = 0;
81 virtual uint32_t getInputSize() const { return getSize(); };
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;
87 std::vector<WasmRelocation> Relocations;
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
97 // For example, by default, clang will produce a separate data section for
98 // each global variable.
99 class InputSegment : public InputChunk {
101 InputSegment(const WasmSegment &Seg, ObjFile *F)
102 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
104 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
106 uint32_t getAlignment() const { return Segment.Data.Alignment; }
107 StringRef getName() const override { return Segment.Data.Name; }
108 StringRef getDebugName() const override { return StringRef(); }
109 uint32_t getComdat() const override { return Segment.Data.Comdat; }
111 const OutputSegment *OutputSeg = nullptr;
112 int32_t OutputSegmentOffset = 0;
115 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
116 uint32_t getInputSectionOffset() const override {
117 return Segment.SectionOffset;
120 const WasmSegment &Segment;
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 {
127 InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F)
128 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
130 static bool classof(const InputChunk *C) {
131 return C->kind() == InputChunk::Function ||
132 C->kind() == InputChunk::SyntheticFunction;
135 void writeTo(uint8_t *SectionStart) const override;
136 StringRef getName() const override { return Function->SymbolName; }
137 StringRef getDebugName() const override { return Function->DebugName; }
138 uint32_t getComdat() const override { return Function->Comdat; }
139 uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
140 uint32_t getFunctionCodeOffset() const { return Function->CodeOffset; }
141 uint32_t getSize() const override {
142 if (Config->CompressRelocTargets && File) {
143 assert(CompressedSize);
144 return CompressedSize;
146 return data().size();
148 uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); }
149 bool hasFunctionIndex() const { return FunctionIndex.hasValue(); }
150 void setFunctionIndex(uint32_t Index);
151 uint32_t getTableIndex() const { return TableIndex.getValue(); }
152 bool hasTableIndex() const { return TableIndex.hasValue(); }
153 void setTableIndex(uint32_t Index);
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
159 void calculateSize();
161 const WasmSignature &Signature;
164 ArrayRef<uint8_t> data() const override {
165 assert(!Config->CompressRelocTargets);
166 return File->CodeSection->Content.slice(getInputSectionOffset(),
170 uint32_t getInputSize() const override { return Function->Size; }
172 uint32_t getInputSectionOffset() const override {
173 return Function->CodeSectionOffset;
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;
183 class SyntheticFunction : public InputFunction {
185 SyntheticFunction(const WasmSignature &S, StringRef Name,
186 StringRef DebugName = {})
187 : InputFunction(S, nullptr, nullptr), Name(Name), DebugName(DebugName) {
188 SectionKind = InputChunk::SyntheticFunction;
191 static bool classof(const InputChunk *C) {
192 return C->kind() == InputChunk::SyntheticFunction;
195 StringRef getName() const override { return Name; }
196 StringRef getDebugName() const override { return DebugName; }
197 uint32_t getComdat() const override { return UINT32_MAX; }
199 void setBody(ArrayRef<uint8_t> Body_) { Body = Body_; }
202 ArrayRef<uint8_t> data() const override { return Body; }
206 ArrayRef<uint8_t> Body;
209 // Represents a single Wasm Section within an input file.
210 class InputSection : public InputChunk {
212 InputSection(const WasmSection &S, ObjFile *F)
213 : InputChunk(F, InputChunk::Section), Section(S) {
214 assert(Section.Type == llvm::wasm::WASM_SEC_CUSTOM);
217 StringRef getName() const override { return Section.Name; }
218 StringRef getDebugName() const override { return StringRef(); }
219 uint32_t getComdat() const override { return UINT32_MAX; }
222 ArrayRef<uint8_t> data() const override { return Section.Content; }
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 uint32_t getInputSectionOffset() const override { return 0; }
228 const WasmSection &Section;
233 std::string toString(const wasm::InputChunk *);
236 #endif // LLD_WASM_INPUT_CHUNKS_H