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"
19 #include "llvm/ADT/StringRef.h"
24 // Object file magic string.
25 const char WasmMagic
[] = {'\0', 'a', 's', 'm'};
26 // Wasm binary format version
27 const uint32_t WasmVersion
= 0x1;
28 // Wasm linking metadata version
29 const uint32_t WasmMetadataVersion
= 0x2;
30 // Wasm uses a 64k page size
31 const uint32_t WasmPageSize
= 65536;
33 struct WasmObjectHeader
{
38 struct WasmDylinkInfo
{
39 uint32_t MemorySize
; // Memory size in bytes
40 uint32_t MemoryAlignment
; // P2 alignment of memory
41 uint32_t TableSize
; // Table size in elements
42 uint32_t TableAlignment
; // P2 alignment of table
43 std::vector
<StringRef
> Needed
; // Shared library depenedencies
46 struct WasmProducerInfo
{
47 std::vector
<std::pair
<std::string
, std::string
>> Languages
;
48 std::vector
<std::pair
<std::string
, std::string
>> Tools
;
49 std::vector
<std::pair
<std::string
, std::string
>> SDKs
;
52 struct WasmFeatureEntry
{
85 struct WasmGlobalType
{
93 WasmInitExpr InitExpr
;
94 StringRef SymbolName
; // from the "linking" section
97 struct WasmEventType
{
98 // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
106 StringRef SymbolName
; // from the "linking" section
115 WasmGlobalType Global
;
122 struct WasmLocalDecl
{
127 struct WasmFunction
{
129 std::vector
<WasmLocalDecl
> Locals
;
130 ArrayRef
<uint8_t> Body
;
131 uint32_t CodeSectionOffset
;
133 uint32_t CodeOffset
; // start of Locals and Body
134 StringRef SymbolName
; // from the "linking" section
135 StringRef DebugName
; // from the "name" section
136 uint32_t Comdat
; // from the "comdat info" section
139 struct WasmDataSegment
{
141 uint32_t MemoryIndex
; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
142 WasmInitExpr Offset
; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
143 ArrayRef
<uint8_t> Content
;
144 StringRef Name
; // from the "segment info" section
146 uint32_t LinkerFlags
;
147 uint32_t Comdat
; // from the "comdat info" section
150 struct WasmElemSegment
{
153 std::vector
<uint32_t> Functions
;
156 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
157 // the index of the segment, and the offset and size within the segment.
158 struct WasmDataReference
{
164 struct WasmRelocation
{
165 uint8_t Type
; // The type of the relocation.
166 uint32_t Index
; // Index into either symbol or type index space.
167 uint64_t Offset
; // Offset from the start of the section.
168 int64_t Addend
; // A value to add to the symbol.
171 struct WasmInitFunc
{
176 struct WasmSymbolInfo
{
180 StringRef ImportModule
; // For undefined symbols the module of the import
181 StringRef ImportName
; // For undefined symbols the name of the import
183 // For function or global symbols, the index in function or global index
185 uint32_t ElementIndex
;
186 // For a data symbols, the address of the data relative to segment.
187 WasmDataReference DataRef
;
191 struct WasmFunctionName
{
196 struct WasmLinkingData
{
198 std::vector
<WasmInitFunc
> InitFunctions
;
199 std::vector
<StringRef
> Comdats
;
200 std::vector
<WasmSymbolInfo
> SymbolTable
;
204 WASM_SEC_CUSTOM
= 0, // Custom / User-defined section
205 WASM_SEC_TYPE
= 1, // Function signature declarations
206 WASM_SEC_IMPORT
= 2, // Import declarations
207 WASM_SEC_FUNCTION
= 3, // Function declarations
208 WASM_SEC_TABLE
= 4, // Indirect function table and other tables
209 WASM_SEC_MEMORY
= 5, // Memory attributes
210 WASM_SEC_GLOBAL
= 6, // Global declarations
211 WASM_SEC_EXPORT
= 7, // Exports
212 WASM_SEC_START
= 8, // Start function declaration
213 WASM_SEC_ELEM
= 9, // Elements section
214 WASM_SEC_CODE
= 10, // Function bodies (code)
215 WASM_SEC_DATA
= 11, // Data segments
216 WASM_SEC_DATACOUNT
= 12, // Data segment count
217 WASM_SEC_EVENT
= 13 // Event declarations
220 // Type immediate encodings used in various contexts.
222 WASM_TYPE_I32
= 0x7F,
223 WASM_TYPE_I64
= 0x7E,
224 WASM_TYPE_F32
= 0x7D,
225 WASM_TYPE_F64
= 0x7C,
226 WASM_TYPE_V128
= 0x7B,
227 WASM_TYPE_FUNCREF
= 0x70,
228 WASM_TYPE_EXNREF
= 0x68,
229 WASM_TYPE_FUNC
= 0x60,
230 WASM_TYPE_NORESULT
= 0x40, // for blocks with no result values
233 // Kinds of externals (for imports and exports).
235 WASM_EXTERNAL_FUNCTION
= 0x0,
236 WASM_EXTERNAL_TABLE
= 0x1,
237 WASM_EXTERNAL_MEMORY
= 0x2,
238 WASM_EXTERNAL_GLOBAL
= 0x3,
239 WASM_EXTERNAL_EVENT
= 0x4,
242 // Opcodes used in initializer expressions.
244 WASM_OPCODE_END
= 0x0b,
245 WASM_OPCODE_CALL
= 0x10,
246 WASM_OPCODE_LOCAL_GET
= 0x20,
247 WASM_OPCODE_GLOBAL_GET
= 0x23,
248 WASM_OPCODE_GLOBAL_SET
= 0x24,
249 WASM_OPCODE_I32_STORE
= 0x36,
250 WASM_OPCODE_I32_CONST
= 0x41,
251 WASM_OPCODE_I64_CONST
= 0x42,
252 WASM_OPCODE_F32_CONST
= 0x43,
253 WASM_OPCODE_F64_CONST
= 0x44,
254 WASM_OPCODE_I32_ADD
= 0x6a,
257 // Opcodes used in synthetic functions.
259 WASM_OPCODE_IF
= 0x04,
260 WASM_OPCODE_ELSE
= 0x05,
261 WASM_OPCODE_DROP
= 0x1a,
262 WASM_OPCODE_MISC_PREFIX
= 0xfc,
263 WASM_OPCODE_MEMORY_INIT
= 0x08,
264 WASM_OPCODE_DATA_DROP
= 0x09,
265 WASM_OPCODE_ATOMICS_PREFIX
= 0xfe,
266 WASM_OPCODE_ATOMIC_NOTIFY
= 0x00,
267 WASM_OPCODE_I32_ATOMIC_WAIT
= 0x01,
268 WASM_OPCODE_I32_ATOMIC_STORE
= 0x17,
269 WASM_OPCODE_I32_RMW_CMPXCHG
= 0x48,
273 WASM_LIMITS_FLAG_HAS_MAX
= 0x1,
274 WASM_LIMITS_FLAG_IS_SHARED
= 0x2,
278 WASM_SEGMENT_IS_PASSIVE
= 0x01,
279 WASM_SEGMENT_HAS_MEMINDEX
= 0x02,
282 // Feature policy prefixes used in the custom "target_features" section
284 WASM_FEATURE_PREFIX_USED
= '+',
285 WASM_FEATURE_PREFIX_REQUIRED
= '=',
286 WASM_FEATURE_PREFIX_DISALLOWED
= '-',
289 // Kind codes used in the custom "name" section
291 WASM_NAMES_FUNCTION
= 0x1,
292 WASM_NAMES_LOCAL
= 0x2,
295 // Kind codes used in the custom "linking" section
297 WASM_SEGMENT_INFO
= 0x5,
298 WASM_INIT_FUNCS
= 0x6,
299 WASM_COMDAT_INFO
= 0x7,
300 WASM_SYMBOL_TABLE
= 0x8,
303 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
305 WASM_COMDAT_DATA
= 0x0,
306 WASM_COMDAT_FUNCTION
= 0x1,
309 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
310 enum WasmSymbolType
: unsigned {
311 WASM_SYMBOL_TYPE_FUNCTION
= 0x0,
312 WASM_SYMBOL_TYPE_DATA
= 0x1,
313 WASM_SYMBOL_TYPE_GLOBAL
= 0x2,
314 WASM_SYMBOL_TYPE_SECTION
= 0x3,
315 WASM_SYMBOL_TYPE_EVENT
= 0x4,
318 // Kinds of event attributes.
319 enum WasmEventAttribute
: unsigned {
320 WASM_EVENT_ATTRIBUTE_EXCEPTION
= 0x0,
323 const unsigned WASM_SYMBOL_BINDING_MASK
= 0x3;
324 const unsigned WASM_SYMBOL_VISIBILITY_MASK
= 0xc;
326 const unsigned WASM_SYMBOL_BINDING_GLOBAL
= 0x0;
327 const unsigned WASM_SYMBOL_BINDING_WEAK
= 0x1;
328 const unsigned WASM_SYMBOL_BINDING_LOCAL
= 0x2;
329 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
= 0x0;
330 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
= 0x4;
331 const unsigned WASM_SYMBOL_UNDEFINED
= 0x10;
332 const unsigned WASM_SYMBOL_EXPORTED
= 0x20;
333 const unsigned WASM_SYMBOL_EXPLICIT_NAME
= 0x40;
334 const unsigned WASM_SYMBOL_NO_STRIP
= 0x80;
336 #define WASM_RELOC(name, value) name = value,
339 #include "WasmRelocs.def"
344 // Subset of types that a value can have
350 V128
= WASM_TYPE_V128
,
351 EXNREF
= WASM_TYPE_EXNREF
,
354 struct WasmSignature
{
355 SmallVector
<ValType
, 1> Returns
;
356 SmallVector
<ValType
, 4> Params
;
357 // Support empty and tombstone instances, needed by DenseMap.
358 enum { Plain
, Empty
, Tombstone
} State
= Plain
;
360 WasmSignature(SmallVector
<ValType
, 1> &&InReturns
,
361 SmallVector
<ValType
, 4> &&InParams
)
362 : Returns(InReturns
), Params(InParams
) {}
363 WasmSignature() = default;
366 // Useful comparison operators
367 inline bool operator==(const WasmSignature
&LHS
, const WasmSignature
&RHS
) {
368 return LHS
.State
== RHS
.State
&& LHS
.Returns
== RHS
.Returns
&&
369 LHS
.Params
== RHS
.Params
;
372 inline bool operator!=(const WasmSignature
&LHS
, const WasmSignature
&RHS
) {
373 return !(LHS
== RHS
);
376 inline bool operator==(const WasmGlobalType
&LHS
, const WasmGlobalType
&RHS
) {
377 return LHS
.Type
== RHS
.Type
&& LHS
.Mutable
== RHS
.Mutable
;
380 inline bool operator!=(const WasmGlobalType
&LHS
, const WasmGlobalType
&RHS
) {
381 return !(LHS
== RHS
);
384 std::string
toString(WasmSymbolType type
);
385 std::string
relocTypetoString(uint32_t type
);
386 bool relocTypeHasAddend(uint32_t type
);
388 } // end namespace wasm
389 } // end namespace llvm