LLVM  10.0.0
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/master/BinaryEncoding.md
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_BINARYFORMAT_WASM_H
15 #define LLVM_BINARYFORMAT_WASM_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace llvm {
22 namespace wasm {
23 
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
30 // Wasm uses a 64k page size
31 const uint32_t WasmPageSize = 65536;
32 
36 };
37 
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
44 };
45 
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;
50 };
51 
53  uint8_t Prefix;
54  std::string Name;
55 };
56 
57 struct WasmExport {
59  uint8_t Kind;
61 };
62 
63 struct WasmLimits {
64  uint8_t Flags;
67 };
68 
69 struct WasmTable {
70  uint8_t ElemType;
72 };
73 
74 struct WasmInitExpr {
75  uint8_t Opcode;
76  union {
77  int32_t Int32;
78  int64_t Int64;
79  int32_t Float32;
80  int64_t Float64;
82  } Value;
83 };
84 
86  uint8_t Type;
87  bool Mutable;
88 };
89 
90 struct WasmGlobal {
94  StringRef SymbolName; // from the "linking" section
95 };
96 
97 struct WasmEventType {
98  // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
101 };
102 
103 struct WasmEvent {
106  StringRef SymbolName; // from the "linking" section
107 };
108 
109 struct WasmImport {
112  uint8_t Kind;
113  union {
119  };
120 };
121 
123  uint8_t Type;
125 };
126 
127 struct WasmFunction {
129  std::vector<WasmLocalDecl> Locals;
133  uint32_t CodeOffset; // start of Locals and Body
134  StringRef ExportName; // from the "export" section
135  StringRef SymbolName; // from the "linking" section
136  StringRef DebugName; // from the "name" section
137  uint32_t Comdat; // from the "comdat info" section
138 };
139 
142  uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
143  WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
145  StringRef Name; // from the "segment info" section
148  uint32_t Comdat; // from the "comdat info" section
149 };
150 
154  std::vector<uint32_t> Functions;
155 };
156 
157 // Represents the location of a Wasm data symbol within a WasmDataSegment, as
158 // the index of the segment, and the offset and size within the segment.
163 };
164 
166  uint8_t Type; // The type of the relocation.
167  uint32_t Index; // Index into either symbol or type index space.
168  uint64_t Offset; // Offset from the start of the section.
169  int64_t Addend; // A value to add to the symbol.
170 };
171 
172 struct WasmInitFunc {
175 };
176 
179  uint8_t Kind;
181  StringRef ImportModule; // For undefined symbols the module of the import
182  StringRef ImportName; // For undefined symbols the name of the import
183  StringRef ExportName; // For symbols to be exported from the final module
184  union {
185  // For function or global symbols, the index in function or global index
186  // space.
188  // For a data symbols, the address of the data relative to segment.
190  };
191 };
192 
196 };
197 
200  std::vector<WasmInitFunc> InitFunctions;
201  std::vector<StringRef> Comdats;
202  std::vector<WasmSymbolInfo> SymbolTable;
203 };
204 
205 enum : unsigned {
206  WASM_SEC_CUSTOM = 0, // Custom / User-defined section
207  WASM_SEC_TYPE = 1, // Function signature declarations
208  WASM_SEC_IMPORT = 2, // Import declarations
209  WASM_SEC_FUNCTION = 3, // Function declarations
210  WASM_SEC_TABLE = 4, // Indirect function table and other tables
211  WASM_SEC_MEMORY = 5, // Memory attributes
212  WASM_SEC_GLOBAL = 6, // Global declarations
213  WASM_SEC_EXPORT = 7, // Exports
214  WASM_SEC_START = 8, // Start function declaration
215  WASM_SEC_ELEM = 9, // Elements section
216  WASM_SEC_CODE = 10, // Function bodies (code)
217  WASM_SEC_DATA = 11, // Data segments
218  WASM_SEC_DATACOUNT = 12, // Data segment count
219  WASM_SEC_EVENT = 13 // Event declarations
220 };
221 
222 // Type immediate encodings used in various contexts.
223 enum : unsigned {
232  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
233 };
234 
235 // Kinds of externals (for imports and exports).
236 enum : unsigned {
242 };
243 
244 // Opcodes used in initializer expressions.
245 enum : unsigned {
257 };
258 
259 // Opcodes used in synthetic functions.
260 enum : unsigned {
272 };
273 
274 enum : unsigned {
277 };
278 
279 enum : unsigned {
282 };
283 
284 // Feature policy prefixes used in the custom "target_features" section
285 enum : uint8_t {
289 };
290 
291 // Kind codes used in the custom "name" section
292 enum : unsigned {
295 };
296 
297 // Kind codes used in the custom "linking" section
298 enum : unsigned {
303 };
304 
305 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
306 enum : unsigned {
309 };
310 
311 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
312 enum WasmSymbolType : unsigned {
318 };
319 
320 // Kinds of event attributes.
321 enum WasmEventAttribute : unsigned {
323 };
324 
325 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
326 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
327 
328 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
329 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
330 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
331 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
332 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
333 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
334 const unsigned WASM_SYMBOL_EXPORTED = 0x20;
335 const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
336 const unsigned WASM_SYMBOL_NO_STRIP = 0x80;
337 
338 #define WASM_RELOC(name, value) name = value,
339 
340 enum : unsigned {
341 #include "WasmRelocs.def"
342 };
343 
344 #undef WASM_RELOC
345 
346 // Subset of types that a value can have
347 enum class ValType {
348  I32 = WASM_TYPE_I32,
349  I64 = WASM_TYPE_I64,
350  F32 = WASM_TYPE_F32,
351  F64 = WASM_TYPE_F64,
354 };
355 
359  // Support empty and tombstone instances, needed by DenseMap.
360  enum { Plain, Empty, Tombstone } State = Plain;
361 
363  SmallVector<ValType, 4> &&InParams)
364  : Returns(InReturns), Params(InParams) {}
365  WasmSignature() = default;
366 };
367 
368 // Useful comparison operators
369 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
370  return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
371  LHS.Params == RHS.Params;
372 }
373 
374 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
375  return !(LHS == RHS);
376 }
377 
378 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
379  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
380 }
381 
382 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
383  return !(LHS == RHS);
384 }
385 
386 std::string toString(WasmSymbolType type);
387 std::string relocTypetoString(uint32_t type);
388 bool relocTypeHasAddend(uint32_t type);
389 
390 } // end namespace wasm
391 } // end namespace llvm
392 
393 #endif
uint32_t TableAlignment
Definition: Wasm.h:42
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:200
WasmSymbolType
Definition: Wasm.h:312
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool relocTypeHasAddend(uint32_t type)
Definition: Wasm.cpp:39
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:330
StringRef SymbolName
Definition: Wasm.h:106
WasmGlobalType Type
Definition: Wasm.h:92
const uint32_t WasmMetadataVersion
Definition: Wasm.h:29
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:47
StringRef ImportModule
Definition: Wasm.h:181
std::vector< StringRef > Comdats
Definition: Wasm.h:201
WasmEventType Type
Definition: Wasm.h:105
StringRef SymbolName
Definition: Wasm.h:135
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:333
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:369
WasmTable Table
Definition: Wasm.h:116
StringRef ExportName
Definition: Wasm.h:183
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:357
const unsigned WASM_SYMBOL_NO_STRIP
Definition: Wasm.h:336
StringRef Module
Definition: Wasm.h:110
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:374
uint32_t CodeOffset
Definition: Wasm.h:133
const uint32_t WasmVersion
Definition: Wasm.h:27
SmallVector< ValType, 4 > Params
Definition: Wasm.h:358
ArrayRef< uint8_t > Content
Definition: Wasm.h:144
ArrayRef< uint8_t > Body
Definition: Wasm.h:130
uint32_t Index
Definition: Wasm.h:104
uint32_t Attribute
Definition: Wasm.h:99
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:27
uint32_t Initial
Definition: Wasm.h:65
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:331
uint32_t SigIndex
Definition: Wasm.h:114
WasmLimits Memory
Definition: Wasm.h:117
std::vector< uint32_t > Functions
Definition: Wasm.h:154
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:332
uint8_t ElemType
Definition: Wasm.h:70
uint32_t MemoryAlignment
Definition: Wasm.h:40
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:329
uint32_t CodeSectionOffset
Definition: Wasm.h:131
WasmInitExpr Offset
Definition: Wasm.h:143
WasmEventType Event
Definition: Wasm.h:118
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:326
StringRef DebugName
Definition: Wasm.h:136
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:49
const char WasmMagic[]
Definition: Wasm.h:25
std::vector< StringRef > Needed
Definition: Wasm.h:43
std::string toString(WasmSymbolType type)
Definition: Wasm.cpp:11
WasmInitExpr InitExpr
Definition: Wasm.h:93
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:335
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:328
WasmEventAttribute
Definition: Wasm.h:321
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:48
WasmSignature(SmallVector< ValType, 1 > &&InReturns, SmallVector< ValType, 4 > &&InParams)
Definition: Wasm.h:362
WasmLimits Limits
Definition: Wasm.h:71
enum llvm::wasm::WasmSignature::@156 State
WasmGlobalType Global
Definition: Wasm.h:115
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:325
uint32_t Index
Definition: Wasm.h:60
WasmDataReference DataRef
Definition: Wasm.h:189
uint32_t Index
Definition: Wasm.h:91
StringRef ExportName
Definition: Wasm.h:134
StringRef Name
Definition: Wasm.h:58
const uint32_t WasmPageSize
Definition: Wasm.h:31
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
StringRef SymbolName
Definition: Wasm.h:94
const unsigned WASM_SYMBOL_EXPORTED
Definition: Wasm.h:334
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:202
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:129
WasmInitExpr Offset
Definition: Wasm.h:153
uint32_t Maximum
Definition: Wasm.h:66
StringRef Field
Definition: Wasm.h:111
StringRef ImportName
Definition: Wasm.h:182