LLVM  9.0.0svn
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 
20 namespace llvm {
21 namespace wasm {
22 
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
29 // Wasm uses a 64k page size
30 const uint32_t WasmPageSize = 65536;
31 
35 };
36 
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
43 };
44 
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;
49 };
50 
52  uint8_t Prefix;
53  std::string Name;
54 };
55 
56 struct WasmExport {
58  uint8_t Kind;
60 };
61 
62 struct WasmLimits {
63  uint8_t Flags;
66 };
67 
68 struct WasmTable {
69  uint8_t ElemType;
71 };
72 
73 struct WasmInitExpr {
74  uint8_t Opcode;
75  union {
76  int32_t Int32;
77  int64_t Int64;
78  int32_t Float32;
79  int64_t Float64;
81  } Value;
82 };
83 
85  uint8_t Type;
86  bool Mutable;
87 };
88 
89 struct WasmGlobal {
93  StringRef SymbolName; // from the "linking" section
94 };
95 
96 struct WasmEventType {
97  // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
100 };
101 
102 struct WasmEvent {
105  StringRef SymbolName; // from the "linking" section
106 };
107 
108 struct WasmImport {
111  uint8_t Kind;
112  union {
118  };
119 };
120 
122  uint8_t Type;
124 };
125 
126 struct WasmFunction {
128  std::vector<WasmLocalDecl> Locals;
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
136 };
137 
140  uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
141  WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
143  StringRef Name; // from the "segment info" section
146  uint32_t Comdat; // from the "comdat info" section
147 };
148 
152  std::vector<uint32_t> Functions;
153 };
154 
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.
161 };
162 
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.
168 };
169 
170 struct WasmInitFunc {
173 };
174 
177  uint8_t Kind;
179  StringRef ImportModule; // For undefined symbols the module of the import
180  StringRef ImportName; // For undefined symbols the name of the import
181  union {
182  // For function or global symbols, the index in function or global index
183  // space.
185  // For a data symbols, the address of the data relative to segment.
187  };
188 };
189 
193 };
194 
197  std::vector<WasmInitFunc> InitFunctions;
198  std::vector<StringRef> Comdats;
199  std::vector<WasmSymbolInfo> SymbolTable;
200 };
201 
202 enum : unsigned {
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
217 };
218 
219 // Type immediate encodings used in various contexts.
220 enum : unsigned {
229  WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
230 };
231 
232 // Kinds of externals (for imports and exports).
233 enum : unsigned {
239 };
240 
241 // Opcodes used in initializer expressions.
242 enum : unsigned {
252 };
253 
254 enum : unsigned {
257 };
258 
259 enum : unsigned {
262 };
263 
264 // Feature policy prefixes used in the custom "target_features" section
265 enum : uint8_t {
269 };
270 
271 // Kind codes used in the custom "name" section
272 enum : unsigned {
275 };
276 
277 // Kind codes used in the custom "linking" section
278 enum : unsigned {
283 };
284 
285 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
286 enum : unsigned {
289 };
290 
291 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
292 enum WasmSymbolType : unsigned {
298 };
299 
300 // Kinds of event attributes.
301 enum WasmEventAttribute : unsigned {
303 };
304 
305 const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
306 const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
307 
308 const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
309 const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
310 const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
311 const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
312 const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
313 const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
314 const unsigned WASM_SYMBOL_EXPORTED = 0x20;
315 const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
316 
317 #define WASM_RELOC(name, value) name = value,
318 
319 enum : unsigned {
320 #include "WasmRelocs.def"
321 };
322 
323 #undef WASM_RELOC
324 
325 // Subset of types that a value can have
326 enum class ValType {
327  I32 = WASM_TYPE_I32,
328  I64 = WASM_TYPE_I64,
329  F32 = WASM_TYPE_F32,
330  F64 = WASM_TYPE_F64,
333 };
334 
338  // Support empty and tombstone instances, needed by DenseMap.
339  enum { Plain, Empty, Tombstone } State = Plain;
340 
342  SmallVector<ValType, 4> &&InParams)
343  : Returns(InReturns), Params(InParams) {}
344  WasmSignature() = default;
345 };
346 
347 // Useful comparison operators
348 inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
349  return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
350  LHS.Params == RHS.Params;
351 }
352 
353 inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
354  return !(LHS == RHS);
355 }
356 
357 inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
358  return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
359 }
360 
361 inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
362  return !(LHS == RHS);
363 }
364 
365 std::string toString(WasmSymbolType type);
366 std::string relocTypetoString(uint32_t type);
367 
368 } // end namespace wasm
369 } // end namespace llvm
370 
371 #endif
uint32_t TableAlignment
Definition: Wasm.h:41
std::vector< WasmInitFunc > InitFunctions
Definition: Wasm.h:197
WasmSymbolType
Definition: Wasm.h:292
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const unsigned WASM_SYMBOL_BINDING_LOCAL
Definition: Wasm.h:310
StringRef SymbolName
Definition: Wasm.h:105
WasmGlobalType Type
Definition: Wasm.h:91
const uint32_t WasmMetadataVersion
Definition: Wasm.h:28
std::vector< std::pair< std::string, std::string > > Languages
Definition: Wasm.h:46
StringRef ImportModule
Definition: Wasm.h:179
std::vector< StringRef > Comdats
Definition: Wasm.h:198
WasmEventType Type
Definition: Wasm.h:104
StringRef SymbolName
Definition: Wasm.h:133
const unsigned WASM_SYMBOL_UNDEFINED
Definition: Wasm.h:313
bool operator==(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:348
WasmTable Table
Definition: Wasm.h:115
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:336
StringRef Module
Definition: Wasm.h:109
bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS)
Definition: Wasm.h:353
uint32_t CodeOffset
Definition: Wasm.h:132
const uint32_t WasmVersion
Definition: Wasm.h:26
SmallVector< ValType, 4 > Params
Definition: Wasm.h:337
ArrayRef< uint8_t > Content
Definition: Wasm.h:142
ArrayRef< uint8_t > Body
Definition: Wasm.h:129
uint32_t Index
Definition: Wasm.h:103
uint32_t Attribute
Definition: Wasm.h:98
std::string relocTypetoString(uint32_t type)
Definition: Wasm.cpp:27
uint32_t Initial
Definition: Wasm.h:64
const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT
Definition: Wasm.h:311
uint32_t SigIndex
Definition: Wasm.h:113
WasmLimits Memory
Definition: Wasm.h:116
std::vector< uint32_t > Functions
Definition: Wasm.h:152
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
Definition: Wasm.h:312
uint8_t ElemType
Definition: Wasm.h:69
uint32_t MemoryAlignment
Definition: Wasm.h:39
const unsigned WASM_SYMBOL_BINDING_WEAK
Definition: Wasm.h:309
uint32_t CodeSectionOffset
Definition: Wasm.h:130
WasmInitExpr Offset
Definition: Wasm.h:141
WasmEventType Event
Definition: Wasm.h:117
const unsigned WASM_SYMBOL_VISIBILITY_MASK
Definition: Wasm.h:306
StringRef DebugName
Definition: Wasm.h:134
std::vector< std::pair< std::string, std::string > > SDKs
Definition: Wasm.h:48
const char WasmMagic[]
Definition: Wasm.h:24
std::vector< StringRef > Needed
Definition: Wasm.h:42
std::string toString(WasmSymbolType type)
Definition: Wasm.cpp:11
WasmInitExpr InitExpr
Definition: Wasm.h:92
const unsigned WASM_SYMBOL_EXPLICIT_NAME
Definition: Wasm.h:315
enum llvm::wasm::WasmSignature::@152 State
const unsigned WASM_SYMBOL_BINDING_GLOBAL
Definition: Wasm.h:308
WasmEventAttribute
Definition: Wasm.h:301
std::vector< std::pair< std::string, std::string > > Tools
Definition: Wasm.h:47
WasmSignature(SmallVector< ValType, 1 > &&InReturns, SmallVector< ValType, 4 > &&InParams)
Definition: Wasm.h:341
WasmLimits Limits
Definition: Wasm.h:70
WasmGlobalType Global
Definition: Wasm.h:114
const unsigned WASM_SYMBOL_BINDING_MASK
Definition: Wasm.h:305
uint32_t Index
Definition: Wasm.h:59
WasmDataReference DataRef
Definition: Wasm.h:186
uint32_t Index
Definition: Wasm.h:90
StringRef Name
Definition: Wasm.h:57
const uint32_t WasmPageSize
Definition: Wasm.h:30
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
StringRef SymbolName
Definition: Wasm.h:93
const unsigned WASM_SYMBOL_EXPORTED
Definition: Wasm.h:314
std::vector< WasmSymbolInfo > SymbolTable
Definition: Wasm.h:199
std::vector< WasmLocalDecl > Locals
Definition: Wasm.h:128
WasmInitExpr Offset
Definition: Wasm.h:151
uint32_t Maximum
Definition: Wasm.h:65
StringRef Field
Definition: Wasm.h:110
StringRef ImportName
Definition: Wasm.h:180