1 //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines manifest constants for the wasm object file format.
11 // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BINARYFORMAT_WASM_H
16 #define LLVM_BINARYFORMAT_WASM_H
18 #include "llvm/ADT/ArrayRef.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 uses a 64k page size
28 const uint32_t WasmPageSize = 65536;
30 struct WasmObjectHeader {
35 struct WasmSignature {
36 std::vector<int32_t> ParamTypes;
71 WasmInitExpr InitExpr;
86 struct WasmLocalDecl {
92 std::vector<WasmLocalDecl> Locals;
93 ArrayRef<uint8_t> Body;
94 uint32_t CodeSectionOffset;
98 struct WasmDataSegment {
101 ArrayRef<uint8_t> Content;
107 struct WasmElemSegment {
110 std::vector<uint32_t> Functions;
113 struct WasmRelocation {
114 uint32_t Type; // The type of the relocation.
115 uint32_t Index; // Index into function to global index space.
116 uint64_t Offset; // Offset from the start of the section.
117 int64_t Addend; // A value to add to the symbol.
120 struct WasmInitFunc {
122 uint32_t FunctionIndex;
125 struct WasmLinkingData {
127 std::vector<WasmInitFunc> InitFunctions;
131 WASM_SEC_CUSTOM = 0, // Custom / User-defined section
132 WASM_SEC_TYPE = 1, // Function signature declarations
133 WASM_SEC_IMPORT = 2, // Import declarations
134 WASM_SEC_FUNCTION = 3, // Function declarations
135 WASM_SEC_TABLE = 4, // Indirect function table and other tables
136 WASM_SEC_MEMORY = 5, // Memory attributes
137 WASM_SEC_GLOBAL = 6, // Global declarations
138 WASM_SEC_EXPORT = 7, // Exports
139 WASM_SEC_START = 8, // Start function declaration
140 WASM_SEC_ELEM = 9, // Elements section
141 WASM_SEC_CODE = 10, // Function bodies (code)
142 WASM_SEC_DATA = 11 // Data segments
145 // Type immediate encodings used in various contexts.
147 WASM_TYPE_I32 = -0x01,
148 WASM_TYPE_I64 = -0x02,
149 WASM_TYPE_F32 = -0x03,
150 WASM_TYPE_F64 = -0x04,
151 WASM_TYPE_ANYFUNC = -0x10,
152 WASM_TYPE_FUNC = -0x20,
153 WASM_TYPE_NORESULT = -0x40, // for blocks with no result values
156 // Kinds of externals (for imports and exports).
158 WASM_EXTERNAL_FUNCTION = 0x0,
159 WASM_EXTERNAL_TABLE = 0x1,
160 WASM_EXTERNAL_MEMORY = 0x2,
161 WASM_EXTERNAL_GLOBAL = 0x3,
164 // Opcodes used in initializer expressions.
166 WASM_OPCODE_END = 0x0b,
167 WASM_OPCODE_GET_GLOBAL = 0x23,
168 WASM_OPCODE_I32_CONST = 0x41,
169 WASM_OPCODE_I64_CONST = 0x42,
170 WASM_OPCODE_F32_CONST = 0x43,
171 WASM_OPCODE_F64_CONST = 0x44,
175 WASM_NAMES_FUNCTION = 0x1,
176 WASM_NAMES_LOCAL = 0x2,
180 WASM_LIMITS_FLAG_HAS_MAX = 0x1,
183 // Subset of types that a value can have
191 // Linking metadata kinds.
193 WASM_SYMBOL_INFO = 0x2,
194 WASM_DATA_SIZE = 0x3,
195 WASM_SEGMENT_INFO = 0x5,
196 WASM_INIT_FUNCS = 0x6,
199 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
200 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0x4;
202 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
203 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
204 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
205 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
206 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
208 #define WASM_RELOC(name, value) name = value,
211 #include "WasmRelocs.def"
220 // The initial value for this global is either the value of an imported
221 // global, in which case InitialModule and InitialName specify the global
222 // import, or a value, in which case InitialModule is empty and InitialValue
224 StringRef InitialModule;
225 StringRef InitialName;
226 uint64_t InitialValue;
229 } // end namespace wasm
230 } // end namespace llvm