1 //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines manifest constants for the wasm object file format.
10 // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BINARYFORMAT_WASM_H
15 #define LLVM_BINARYFORMAT_WASM_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
23 // Object file magic string.
24 const char WasmMagic[] = {'\0', 'a', 's', 'm'};
25 // Wasm binary format version
26 const uint32_t WasmVersion = 0x1;
27 // Wasm linking metadata version
28 const uint32_t WasmMetadataVersion = 0x2;
29 // Wasm uses a 64k page size
30 const uint32_t WasmPageSize = 65536;
32 struct WasmObjectHeader {
37 struct WasmDylinkInfo {
38 uint32_t MemorySize; // Memory size in bytes
39 uint32_t MemoryAlignment; // P2 alignment of memory
40 uint32_t TableSize; // Table size in elements
41 uint32_t TableAlignment; // P2 alignment of table
42 std::vector<StringRef> Needed; // Shared library depenedencies
45 struct WasmProducerInfo {
46 std::vector<std::pair<std::string, std::string>> Languages;
47 std::vector<std::pair<std::string, std::string>> Tools;
48 std::vector<std::pair<std::string, std::string>> SDKs;
51 struct WasmFeatureEntry {
84 struct WasmGlobalType {
92 WasmInitExpr InitExpr;
93 StringRef SymbolName; // from the "linking" section
96 struct WasmEventType {
97 // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
105 StringRef SymbolName; // from the "linking" section
114 WasmGlobalType Global;
121 struct WasmLocalDecl {
126 struct WasmFunction {
128 std::vector<WasmLocalDecl> Locals;
129 ArrayRef<uint8_t> Body;
130 uint32_t CodeSectionOffset;
132 uint32_t CodeOffset; // start of Locals and Body
133 StringRef SymbolName; // from the "linking" section
134 StringRef DebugName; // from the "name" section
135 uint32_t Comdat; // from the "comdat info" section
138 struct WasmDataSegment {
140 uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
141 WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
142 ArrayRef<uint8_t> Content;
143 StringRef Name; // from the "segment info" section
145 uint32_t LinkerFlags;
146 uint32_t Comdat; // from the "comdat info" section
149 struct WasmElemSegment {
152 std::vector<uint32_t> Functions;
155 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
156 // the index of the segment, and the offset and size within the segment.
157 struct WasmDataReference {
163 struct WasmRelocation {
164 uint8_t Type; // The type of the relocation.
165 uint32_t Index; // Index into either symbol or type index space.
166 uint64_t Offset; // Offset from the start of the section.
167 int64_t Addend; // A value to add to the symbol.
170 struct WasmInitFunc {
175 struct WasmSymbolInfo {
179 StringRef ImportModule; // For undefined symbols the module of the import
180 StringRef ImportName; // For undefined symbols the name of the import
182 // For function or global symbols, the index in function or global index
184 uint32_t ElementIndex;
185 // For a data symbols, the address of the data relative to segment.
186 WasmDataReference DataRef;
190 struct WasmFunctionName {
195 struct WasmLinkingData {
197 std::vector<WasmInitFunc> InitFunctions;
198 std::vector<StringRef> Comdats;
199 std::vector<WasmSymbolInfo> SymbolTable;
203 WASM_SEC_CUSTOM = 0, // Custom / User-defined section
204 WASM_SEC_TYPE = 1, // Function signature declarations
205 WASM_SEC_IMPORT = 2, // Import declarations
206 WASM_SEC_FUNCTION = 3, // Function declarations
207 WASM_SEC_TABLE = 4, // Indirect function table and other tables
208 WASM_SEC_MEMORY = 5, // Memory attributes
209 WASM_SEC_GLOBAL = 6, // Global declarations
210 WASM_SEC_EXPORT = 7, // Exports
211 WASM_SEC_START = 8, // Start function declaration
212 WASM_SEC_ELEM = 9, // Elements section
213 WASM_SEC_CODE = 10, // Function bodies (code)
214 WASM_SEC_DATA = 11, // Data segments
215 WASM_SEC_DATACOUNT = 12, // Data segment count
216 WASM_SEC_EVENT = 13 // Event declarations
219 // Type immediate encodings used in various contexts.
221 WASM_TYPE_I32 = 0x7F,
222 WASM_TYPE_I64 = 0x7E,
223 WASM_TYPE_F32 = 0x7D,
224 WASM_TYPE_F64 = 0x7C,
225 WASM_TYPE_V128 = 0x7B,
226 WASM_TYPE_FUNCREF = 0x70,
227 WASM_TYPE_EXNREF = 0x68,
228 WASM_TYPE_FUNC = 0x60,
229 WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
232 // Kinds of externals (for imports and exports).
234 WASM_EXTERNAL_FUNCTION = 0x0,
235 WASM_EXTERNAL_TABLE = 0x1,
236 WASM_EXTERNAL_MEMORY = 0x2,
237 WASM_EXTERNAL_GLOBAL = 0x3,
238 WASM_EXTERNAL_EVENT = 0x4,
241 // Opcodes used in initializer expressions.
243 WASM_OPCODE_END = 0x0b,
244 WASM_OPCODE_CALL = 0x10,
245 WASM_OPCODE_LOCAL_GET = 0x20,
246 WASM_OPCODE_GLOBAL_GET = 0x23,
247 WASM_OPCODE_GLOBAL_SET = 0x24,
248 WASM_OPCODE_I32_STORE = 0x36,
249 WASM_OPCODE_I32_CONST = 0x41,
250 WASM_OPCODE_I64_CONST = 0x42,
251 WASM_OPCODE_F32_CONST = 0x43,
252 WASM_OPCODE_F64_CONST = 0x44,
253 WASM_OPCODE_I32_ADD = 0x6a,
254 WASM_OPCODE_MISC_PREFIX = 0xfc,
255 WASM_OPCODE_MEMORY_INIT = 0x08,
256 WASM_OPCODE_DATA_DROP = 0x09,
260 WASM_LIMITS_FLAG_HAS_MAX = 0x1,
261 WASM_LIMITS_FLAG_IS_SHARED = 0x2,
265 WASM_SEGMENT_IS_PASSIVE = 0x01,
266 WASM_SEGMENT_HAS_MEMINDEX = 0x02,
269 // Feature policy prefixes used in the custom "target_features" section
271 WASM_FEATURE_PREFIX_USED = '+',
272 WASM_FEATURE_PREFIX_REQUIRED = '=',
273 WASM_FEATURE_PREFIX_DISALLOWED = '-',
276 // Kind codes used in the custom "name" section
278 WASM_NAMES_FUNCTION = 0x1,
279 WASM_NAMES_LOCAL = 0x2,
282 // Kind codes used in the custom "linking" section
284 WASM_SEGMENT_INFO = 0x5,
285 WASM_INIT_FUNCS = 0x6,
286 WASM_COMDAT_INFO = 0x7,
287 WASM_SYMBOL_TABLE = 0x8,
290 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
292 WASM_COMDAT_DATA = 0x0,
293 WASM_COMDAT_FUNCTION = 0x1,
296 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
297 enum WasmSymbolType : unsigned {
298 WASM_SYMBOL_TYPE_FUNCTION = 0x0,
299 WASM_SYMBOL_TYPE_DATA = 0x1,
300 WASM_SYMBOL_TYPE_GLOBAL = 0x2,
301 WASM_SYMBOL_TYPE_SECTION = 0x3,
302 WASM_SYMBOL_TYPE_EVENT = 0x4,
305 // Kinds of event attributes.
306 enum WasmEventAttribute : unsigned {
307 WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0,
310 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
311 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
313 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
314 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
315 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
316 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
317 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
318 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
319 const unsigned WASM_SYMBOL_EXPORTED = 0x20;
320 const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
322 #define WASM_RELOC(name, value) name = value,
325 #include "WasmRelocs.def"
330 // Subset of types that a value can have
336 V128 = WASM_TYPE_V128,
337 EXNREF = WASM_TYPE_EXNREF,
340 struct WasmSignature {
341 SmallVector<ValType, 1> Returns;
342 SmallVector<ValType, 4> Params;
343 // Support empty and tombstone instances, needed by DenseMap.
344 enum { Plain, Empty, Tombstone } State = Plain;
346 WasmSignature(SmallVector<ValType, 1> &&InReturns,
347 SmallVector<ValType, 4> &&InParams)
348 : Returns(InReturns), Params(InParams) {}
349 WasmSignature() = default;
352 // Useful comparison operators
353 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
354 return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
355 LHS.Params == RHS.Params;
358 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
359 return !(LHS == RHS);
362 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
363 return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
366 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
367 return !(LHS == RHS);
370 std::string toString(WasmSymbolType type);
371 std::string relocTypetoString(uint32_t type);
372 bool relocTypeHasAddend(uint32_t type);
374 } // end namespace wasm
375 } // end namespace llvm