LLVM 18.0.0git
Wasm.h
Go to the documentation of this file.
1//===- Wasm.h - Wasm object file format -------------------------*- 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// This file defines manifest constants for the wasm object file format.
10// See: https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_BINARYFORMAT_WASM_H
15#define LLVM_BINARYFORMAT_WASM_H
16
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringRef.h"
20#include <optional>
21
22namespace llvm {
23namespace wasm {
24
25// Object file magic string.
26const char WasmMagic[] = {'\0', 'a', 's', 'm'};
27// Wasm binary format version
29// Wasm linking metadata version
31// Wasm uses a 64k page size
32const uint32_t WasmPageSize = 65536;
33
37};
38
43};
44
48};
49
51 uint32_t MemorySize; // Memory size in bytes
52 uint32_t MemoryAlignment; // P2 alignment of memory
53 uint32_t TableSize; // Table size in elements
54 uint32_t TableAlignment; // P2 alignment of table
55 std::vector<StringRef> Needed; // Shared library dependencies
56 std::vector<WasmDylinkImportInfo> ImportInfo;
57 std::vector<WasmDylinkExportInfo> ExportInfo;
58};
59
61 std::vector<std::pair<std::string, std::string>> Languages;
62 std::vector<std::pair<std::string, std::string>> Tools;
63 std::vector<std::pair<std::string, std::string>> SDKs;
64};
65
67 uint8_t Prefix;
68 std::string Name;
69};
70
71struct WasmExport {
73 uint8_t Kind;
75};
76
77struct WasmLimits {
78 uint8_t Flags;
81};
82
84 uint8_t ElemType;
86};
87
88struct WasmTable {
91 StringRef SymbolName; // from the "linking" section
92};
93
95 uint8_t Opcode;
96 union {
97 int32_t Int32;
98 int64_t Int64;
103};
104
106 uint8_t Extended; // Set to non-zero if extended const is used (i.e. more than
107 // one instruction)
110};
111
113 uint8_t Type;
115};
116
121 StringRef SymbolName; // from the "linking" section
122};
123
124struct WasmTag {
127 StringRef SymbolName; // from the "linking" section
128};
129
133 uint8_t Kind;
134 union {
139 };
140};
141
143 uint8_t Type;
145};
146
150 std::vector<WasmLocalDecl> Locals;
154 uint32_t CodeOffset; // start of Locals and Body
155 std::optional<StringRef> ExportName; // from the "export" section
156 StringRef SymbolName; // from the "linking" section
157 StringRef DebugName; // from the "name" section
158 uint32_t Comdat; // from the "comdat info" section
159};
160
163 // Present if InitFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX.
165 // Present if InitFlags & WASM_DATA_SEGMENT_IS_PASSIVE == 0.
167
169 StringRef Name; // from the "segment info" section
172 uint32_t Comdat; // from the "comdat info" section
173};
174
178 uint8_t ElemKind;
180 std::vector<uint32_t> Functions;
181};
182
183// Represents the location of a Wasm data symbol within a WasmDataSegment, as
184// the index of the segment, and the offset and size within the segment.
189};
190
192 uint8_t Type; // The type of the relocation.
193 uint32_t Index; // Index into either symbol or type index space.
194 uint64_t Offset; // Offset from the start of the section.
195 int64_t Addend; // A value to add to the symbol.
196};
197
201};
202
205 uint8_t Kind;
207 // For undefined symbols the module of the import
208 std::optional<StringRef> ImportModule;
209 // For undefined symbols the name of the import
210 std::optional<StringRef> ImportName;
211 // For symbols to be exported from the final module
212 std::optional<StringRef> ExportName;
213 union {
214 // For function, table, or global symbols, the index in function, table, or
215 // global index space.
217 // For a data symbols, the address of the data relative to segment.
219 };
220};
221
222enum class NameType {
223 FUNCTION,
224 GLOBAL,
226};
227
232};
233
236 std::vector<WasmInitFunc> InitFunctions;
237 std::vector<StringRef> Comdats;
238 std::vector<WasmSymbolInfo> SymbolTable;
239};
240
241enum : unsigned {
242 WASM_SEC_CUSTOM = 0, // Custom / User-defined section
243 WASM_SEC_TYPE = 1, // Function signature declarations
244 WASM_SEC_IMPORT = 2, // Import declarations
245 WASM_SEC_FUNCTION = 3, // Function declarations
246 WASM_SEC_TABLE = 4, // Indirect function table and other tables
247 WASM_SEC_MEMORY = 5, // Memory attributes
248 WASM_SEC_GLOBAL = 6, // Global declarations
249 WASM_SEC_EXPORT = 7, // Exports
250 WASM_SEC_START = 8, // Start function declaration
251 WASM_SEC_ELEM = 9, // Elements section
252 WASM_SEC_CODE = 10, // Function bodies (code)
253 WASM_SEC_DATA = 11, // Data segments
254 WASM_SEC_DATACOUNT = 12, // Data segment count
255 WASM_SEC_TAG = 13, // Tag declarations
257};
258
259// Type immediate encodings used in various contexts.
260enum : unsigned {
269 WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
270};
271
272// Kinds of externals (for imports and exports).
273enum : unsigned {
279};
280
281// Opcodes used in initializer expressions.
282enum : unsigned {
303};
304
305// Opcodes used in synthetic functions.
306enum : unsigned {
321};
322
323enum : unsigned {
328};
329
330enum : unsigned {
333};
334
335enum : unsigned {
339};
341
342// Feature policy prefixes used in the custom "target_features" section
343enum : uint8_t {
347};
348
349// Kind codes used in the custom "name" section
350enum : unsigned {
356};
357
358// Kind codes used in the custom "linking" section
359enum : unsigned {
364};
365
366// Kind codes used in the custom "dylink" section
367enum : unsigned {
372};
373
374// Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
375enum : unsigned {
378 // GLOBAL, TAG, and TABLE are in here but LLVM doesn't use them yet.
380};
381
382// Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
383enum WasmSymbolType : unsigned {
390};
391
392enum WasmSegmentFlag : unsigned {
395};
396
397// Kinds of tag attributes.
398enum WasmTagAttribute : uint8_t {
400};
401
402const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
403const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
404
405const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
406const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
407const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
409const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
410const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
411const unsigned WASM_SYMBOL_EXPORTED = 0x20;
412const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
413const unsigned WASM_SYMBOL_NO_STRIP = 0x80;
414const unsigned WASM_SYMBOL_TLS = 0x100;
415const unsigned WASM_SYMBOL_ABSOLUTE = 0x200;
416
417#define WASM_RELOC(name, value) name = value,
418
419enum : unsigned {
420#include "WasmRelocs.def"
421};
422
423#undef WASM_RELOC
424
425// Subset of types that a value can have
426enum class ValType {
434};
435
439 // Support empty and tombstone instances, needed by DenseMap.
441
443 SmallVector<ValType, 4> &&InParams)
444 : Returns(InReturns), Params(InParams) {}
445 WasmSignature() = default;
446};
447
448// Useful comparison operators
449inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
450 return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
451 LHS.Params == RHS.Params;
452}
453
454inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
455 return !(LHS == RHS);
456}
457
458inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
459 return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
460}
461
462inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
463 return !(LHS == RHS);
464}
465
466inline bool operator==(const WasmLimits &LHS, const WasmLimits &RHS) {
467 return LHS.Flags == RHS.Flags && LHS.Minimum == RHS.Minimum &&
468 (LHS.Flags & WASM_LIMITS_FLAG_HAS_MAX ? LHS.Maximum == RHS.Maximum
469 : true);
470}
471
472inline bool operator==(const WasmTableType &LHS, const WasmTableType &RHS) {
473 return LHS.ElemType == RHS.ElemType && LHS.Limits == RHS.Limits;
474}
475
480
481} // end namespace wasm
482} // end namespace llvm
483
484#endif
This file defines the SmallVector class.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
@ WASM_INIT_FUNCS
Definition: Wasm.h:361
@ WASM_COMDAT_INFO
Definition: Wasm.h:362
@ WASM_SEGMENT_INFO
Definition: Wasm.h:360
@ WASM_SYMBOL_TABLE
Definition: Wasm.h:363
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:410
const uint32_t WasmPageSize
Definition: Wasm.h:32
const unsigned WASM_SYMBOL_NO_STRIP
Definition: Wasm.h:413
@ WASM_OPCODE_BR
Definition: Wasm.h:308
@ WASM_OPCODE_I32_ATOMIC_STORE
Definition: Wasm.h:319
@ WASM_OPCODE_MEMORY_FILL
Definition: Wasm.h:314
@ WASM_OPCODE_DATA_DROP
Definition: Wasm.h:315
@ WASM_OPCODE_BR_TABLE
Definition: Wasm.h:309
@ WASM_OPCODE_I32_ATOMIC_WAIT
Definition: Wasm.h:318
@ WASM_OPCODE_MISC_PREFIX
Definition: Wasm.h:312
@ WASM_OPCODE_BLOCK
Definition: Wasm.h:307
@ WASM_OPCODE_ATOMIC_NOTIFY
Definition: Wasm.h:317
@ WASM_OPCODE_RETURN
Definition: Wasm.h:310
@ WASM_OPCODE_I32_RMW_CMPXCHG
Definition: Wasm.h:320
@ WASM_OPCODE_MEMORY_INIT
Definition: Wasm.h:313
@ WASM_OPCODE_DROP
Definition: Wasm.h:311
@ WASM_OPCODE_ATOMICS_PREFIX
Definition: Wasm.h:316
const char WasmMagic[]
Definition: Wasm.h:26
@ WASM_COMDAT_SECTION
Definition: Wasm.h:379
@ WASM_COMDAT_FUNCTION
Definition: Wasm.h:377
@ WASM_COMDAT_DATA
Definition: Wasm.h:376
@ WASM_FEATURE_PREFIX_USED
Definition: Wasm.h:344
@ WASM_FEATURE_PREFIX_REQUIRED
Definition: Wasm.h:345
@ WASM_FEATURE_PREFIX_DISALLOWED
Definition: Wasm.h:346
@ WASM_DATA_SEGMENT_IS_PASSIVE
Definition: Wasm.h:331
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
Definition: Wasm.h:332
@ WASM_OPCODE_LOCAL_SET
Definition: Wasm.h:286
@ WASM_OPCODE_CALL
Definition: Wasm.h:284
@ WASM_OPCODE_LOCAL_GET
Definition: Wasm.h:285
@ WASM_OPCODE_I64_ADD
Definition: Wasm.h:299
@ WASM_OPCODE_I32_SUB
Definition: Wasm.h:297
@ WASM_OPCODE_F64_CONST
Definition: Wasm.h:295
@ WASM_OPCODE_END
Definition: Wasm.h:283
@ WASM_OPCODE_I64_MUL
Definition: Wasm.h:301
@ WASM_OPCODE_GLOBAL_SET
Definition: Wasm.h:289
@ WASM_OPCODE_REF_NULL
Definition: Wasm.h:302
@ WASM_OPCODE_F32_CONST
Definition: Wasm.h:294
@ WASM_OPCODE_GLOBAL_GET
Definition: Wasm.h:288
@ WASM_OPCODE_I64_SUB
Definition: Wasm.h:300
@ WASM_OPCODE_I32_MUL
Definition: Wasm.h:298
@ WASM_OPCODE_I32_ADD
Definition: Wasm.h:296
@ WASM_OPCODE_LOCAL_TEE
Definition: Wasm.h:287
@ WASM_OPCODE_I32_STORE
Definition: Wasm.h:290
@ WASM_OPCODE_I64_CONST
Definition: Wasm.h:293
@ WASM_OPCODE_I64_STORE
Definition: Wasm.h:291
@ WASM_OPCODE_I32_CONST
Definition: Wasm.h:292
@ WASM_NAMES_LOCAL
Definition: Wasm.h:353
@ WASM_NAMES_DATA_SEGMENT
Definition: Wasm.h:355
@ WASM_NAMES_GLOBAL
Definition: Wasm.h:354
@ WASM_NAMES_FUNCTION
Definition: Wasm.h:352
@ WASM_NAMES_MODULE
Definition: Wasm.h:351
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:405
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND
Definition: Wasm.h:340
WasmTagAttribute
Definition: Wasm.h:398
@ WASM_TAG_ATTRIBUTE_EXCEPTION
Definition: Wasm.h:399
const unsigned WASM_SYMBOL_TLS
Definition: Wasm.h:414
@ WASM_LIMITS_FLAG_HAS_MAX
Definition: Wasm.h:325
@ WASM_LIMITS_FLAG_IS_SHARED
Definition: Wasm.h:326
@ WASM_LIMITS_FLAG_IS_64
Definition: Wasm.h:327
@ WASM_LIMITS_FLAG_NONE
Definition: Wasm.h:324
const uint32_t WasmMetadataVersion
Definition: Wasm.h:30
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:406
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:407
llvm::StringRef toString(WasmSymbolType type)
Definition: Wasm.cpp:11
WasmSymbolType
Definition: Wasm.h:383
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:386
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:385
@ WASM_SYMBOL_TYPE_TAG
Definition: Wasm.h:388
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:389
@ WASM_SYMBOL_TYPE_SECTION
Definition: Wasm.h:387
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:384
const uint32_t WasmVersion
Definition: Wasm.h:28
@ WASM_ELEM_SEGMENT_HAS_INIT_EXPRS
Definition: Wasm.h:338
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
Definition: Wasm.h:337
@ WASM_ELEM_SEGMENT_IS_PASSIVE
Definition: Wasm.h:336
@ WASM_DYLINK_NEEDED
Definition: Wasm.h:369
@ WASM_DYLINK_MEM_INFO
Definition: Wasm.h:368
@ WASM_DYLINK_EXPORT_INFO
Definition: Wasm.h:370
@ WASM_DYLINK_IMPORT_INFO
Definition: Wasm.h:371
const unsigned WASM_SYMBOL_EXPORTED
Definition: Wasm.h:411
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:402
bool relocTypeHasAddend(uint32_t type)
Definition: Wasm.cpp:66
@ WASM_TYPE_I64
Definition: Wasm.h:262
@ WASM_TYPE_F64
Definition: Wasm.h:264
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:266
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:267
@ WASM_TYPE_FUNC
Definition: Wasm.h:268
@ WASM_TYPE_I32
Definition: Wasm.h:261
@ WASM_TYPE_NORESULT
Definition: Wasm.h:269
@ WASM_TYPE_F32
Definition: Wasm.h:263
@ WASM_TYPE_V128
Definition: Wasm.h:265
@ WASM_SEC_CODE
Definition: Wasm.h:252
@ WASM_SEC_MEMORY
Definition: Wasm.h:247
@ WASM_SEC_IMPORT
Definition: Wasm.h:244
@ WASM_SEC_EXPORT
Definition: Wasm.h:249
@ WASM_SEC_DATACOUNT
Definition: Wasm.h:254
@ WASM_SEC_LAST_KNOWN
Definition: Wasm.h:256
@ WASM_SEC_CUSTOM
Definition: Wasm.h:242
@ WASM_SEC_FUNCTION
Definition: Wasm.h:245
@ WASM_SEC_ELEM
Definition: Wasm.h:251
@ WASM_SEC_START
Definition: Wasm.h:250
@ WASM_SEC_TABLE
Definition: Wasm.h:246
@ WASM_SEC_TYPE
Definition: Wasm.h:243
@ WASM_SEC_TAG
Definition: Wasm.h:255
@ WASM_SEC_GLOBAL
Definition: Wasm.h:248
@ WASM_SEC_DATA
Definition: Wasm.h:253
WasmSegmentFlag
Definition: Wasm.h:392
@ WASM_SEG_FLAG_TLS
Definition: Wasm.h:394
@ WASM_SEG_FLAG_STRINGS
Definition: Wasm.h:393
@ WASM_EXTERNAL_TABLE
Definition: Wasm.h:275
@ WASM_EXTERNAL_FUNCTION
Definition: Wasm.h:274
@ WASM_EXTERNAL_TAG
Definition: Wasm.h:278
@ WASM_EXTERNAL_MEMORY
Definition: Wasm.h:276
@ WASM_EXTERNAL_GLOBAL
Definition: Wasm.h:277
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:454
llvm::StringRef sectionTypeToString(uint32_t type)
Definition: Wasm.cpp:41
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:412
const unsigned WASM_SYMBOL_ABSOLUTE
Definition: Wasm.h:415
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:449
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:403
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:409
llvm::StringRef relocTypetoString(uint32_t type)
Definition: Wasm.cpp:29
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:408
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ArrayRef< uint8_t > Content
Definition: Wasm.h:168
WasmInitExpr Offset
Definition: Wasm.h:166
uint32_t MemoryAlignment
Definition: Wasm.h:52
std::vector< StringRef > Needed
Definition: Wasm.h:55
std::vector< WasmDylinkExportInfo > ExportInfo
Definition: Wasm.h:57
std::vector< WasmDylinkImportInfo > ImportInfo
Definition: Wasm.h:56
uint32_t TableAlignment
Definition: Wasm.h:54
WasmInitExpr Offset
Definition: Wasm.h:179
std::vector< uint32_t > Functions
Definition: Wasm.h:180
StringRef Name
Definition: Wasm.h:72
uint32_t Index
Definition: Wasm.h:74
uint32_t CodeOffset
Definition: Wasm.h:154
ArrayRef< uint8_t > Body
Definition: Wasm.h:151
std::optional< StringRef > ExportName
Definition: Wasm.h:155
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:150
StringRef SymbolName
Definition: Wasm.h:156
uint32_t CodeSectionOffset
Definition: Wasm.h:152
StringRef DebugName
Definition: Wasm.h:157
WasmInitExpr InitExpr
Definition: Wasm.h:120
StringRef SymbolName
Definition: Wasm.h:121
WasmGlobalType Type
Definition: Wasm.h:119
uint32_t Index
Definition: Wasm.h:118
WasmLimits Memory
Definition: Wasm.h:138
StringRef Field
Definition: Wasm.h:132
WasmGlobalType Global
Definition: Wasm.h:136
StringRef Module
Definition: Wasm.h:131
uint32_t SigIndex
Definition: Wasm.h:135
WasmTableType Table
Definition: Wasm.h:137
union llvm::wasm::WasmInitExprMVP::@178 Value
WasmInitExprMVP Inst
Definition: Wasm.h:108
ArrayRef< uint8_t > Body
Definition: Wasm.h:109
uint64_t Minimum
Definition: Wasm.h:79
uint64_t Maximum
Definition: Wasm.h:80
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:238
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:236
std::vector< StringRef > Comdats
Definition: Wasm.h:237
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:63
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:61
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:62
WasmSignature(SmallVector< ValType, 1 > &&InReturns, SmallVector< ValType, 4 > &&InParams)
Definition: Wasm.h:442
enum llvm::wasm::WasmSignature::@183 State
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:437
SmallVector< ValType, 4 > Params
Definition: Wasm.h:438
std::optional< StringRef > ExportName
Definition: Wasm.h:212
WasmDataReference DataRef
Definition: Wasm.h:218
std::optional< StringRef > ImportModule
Definition: Wasm.h:208
std::optional< StringRef > ImportName
Definition: Wasm.h:210
WasmLimits Limits
Definition: Wasm.h:85
WasmTableType Type
Definition: Wasm.h:90
StringRef SymbolName
Definition: Wasm.h:91
uint32_t Index
Definition: Wasm.h:89
uint32_t Index
Definition: Wasm.h:125
uint32_t SigIndex
Definition: Wasm.h:126
StringRef SymbolName
Definition: Wasm.h:127