• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
29 const uint32_t WasmMetadataVersion = 0x2;
30 // Wasm uses a 64k page size
31 const uint32_t WasmPageSize = 65536;
32 
33 struct WasmObjectHeader {
34   StringRef Magic;
35   uint32_t Version;
36 };
37 
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
44 };
45 
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;
50 };
51 
52 struct WasmFeatureEntry {
53   uint8_t Prefix;
54   std::string Name;
55 };
56 
57 struct WasmExport {
58   StringRef Name;
59   uint8_t Kind;
60   uint32_t Index;
61 };
62 
63 struct WasmLimits {
64   uint8_t Flags;
65   uint32_t Initial;
66   uint32_t Maximum;
67 };
68 
69 struct WasmTable {
70   uint8_t ElemType;
71   WasmLimits Limits;
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;
81     uint32_t Global;
82   } Value;
83 };
84 
85 struct WasmGlobalType {
86   uint8_t Type;
87   bool Mutable;
88 };
89 
90 struct WasmGlobal {
91   uint32_t Index;
92   WasmGlobalType Type;
93   WasmInitExpr InitExpr;
94   StringRef SymbolName; // from the "linking" section
95 };
96 
97 struct WasmEventType {
98   // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
99   uint32_t Attribute;
100   uint32_t SigIndex;
101 };
102 
103 struct WasmEvent {
104   uint32_t Index;
105   WasmEventType Type;
106   StringRef SymbolName; // from the "linking" section
107 };
108 
109 struct WasmImport {
110   StringRef Module;
111   StringRef Field;
112   uint8_t Kind;
113   union {
114     uint32_t SigIndex;
115     WasmGlobalType Global;
116     WasmTable Table;
117     WasmLimits Memory;
118     WasmEventType Event;
119   };
120 };
121 
122 struct WasmLocalDecl {
123   uint8_t Type;
124   uint32_t Count;
125 };
126 
127 struct WasmFunction {
128   uint32_t Index;
129   std::vector<WasmLocalDecl> Locals;
130   ArrayRef<uint8_t> Body;
131   uint32_t CodeSectionOffset;
132   uint32_t Size;
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 
140 struct WasmDataSegment {
141   uint32_t InitFlags;
142   uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
143   WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
144   ArrayRef<uint8_t> Content;
145   StringRef Name; // from the "segment info" section
146   uint32_t Alignment;
147   uint32_t LinkerFlags;
148   uint32_t Comdat; // from the "comdat info" section
149 };
150 
151 struct WasmElemSegment {
152   uint32_t TableIndex;
153   WasmInitExpr Offset;
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.
159 struct WasmDataReference {
160   uint32_t Segment;
161   uint32_t Offset;
162   uint32_t Size;
163 };
164 
165 struct WasmRelocation {
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 {
173   uint32_t Priority;
174   uint32_t Symbol;
175 };
176 
177 struct WasmSymbolInfo {
178   StringRef Name;
179   uint8_t Kind;
180   uint32_t Flags;
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.
187     uint32_t ElementIndex;
188     // For a data symbols, the address of the data relative to segment.
189     WasmDataReference DataRef;
190   };
191 };
192 
193 struct WasmFunctionName {
194   uint32_t Index;
195   StringRef Name;
196 };
197 
198 struct WasmLinkingData {
199   uint32_t Version;
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 {
224   WASM_TYPE_I32 = 0x7F,
225   WASM_TYPE_I64 = 0x7E,
226   WASM_TYPE_F32 = 0x7D,
227   WASM_TYPE_F64 = 0x7C,
228   WASM_TYPE_V128 = 0x7B,
229   WASM_TYPE_FUNCREF = 0x70,
230   WASM_TYPE_EXNREF = 0x68,
231   WASM_TYPE_FUNC = 0x60,
232   WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
233 };
234 
235 // Kinds of externals (for imports and exports).
236 enum : unsigned {
237   WASM_EXTERNAL_FUNCTION = 0x0,
238   WASM_EXTERNAL_TABLE = 0x1,
239   WASM_EXTERNAL_MEMORY = 0x2,
240   WASM_EXTERNAL_GLOBAL = 0x3,
241   WASM_EXTERNAL_EVENT = 0x4,
242 };
243 
244 // Opcodes used in initializer expressions.
245 enum : unsigned {
246   WASM_OPCODE_END = 0x0b,
247   WASM_OPCODE_CALL = 0x10,
248   WASM_OPCODE_LOCAL_GET = 0x20,
249   WASM_OPCODE_GLOBAL_GET = 0x23,
250   WASM_OPCODE_GLOBAL_SET = 0x24,
251   WASM_OPCODE_I32_STORE = 0x36,
252   WASM_OPCODE_I32_CONST = 0x41,
253   WASM_OPCODE_I64_CONST = 0x42,
254   WASM_OPCODE_F32_CONST = 0x43,
255   WASM_OPCODE_F64_CONST = 0x44,
256   WASM_OPCODE_I32_ADD = 0x6a,
257 };
258 
259 // Opcodes used in synthetic functions.
260 enum : unsigned {
261   WASM_OPCODE_IF = 0x04,
262   WASM_OPCODE_ELSE = 0x05,
263   WASM_OPCODE_DROP = 0x1a,
264   WASM_OPCODE_MISC_PREFIX = 0xfc,
265   WASM_OPCODE_MEMORY_INIT = 0x08,
266   WASM_OPCODE_DATA_DROP = 0x09,
267   WASM_OPCODE_ATOMICS_PREFIX = 0xfe,
268   WASM_OPCODE_ATOMIC_NOTIFY = 0x00,
269   WASM_OPCODE_I32_ATOMIC_WAIT = 0x01,
270   WASM_OPCODE_I32_ATOMIC_STORE = 0x17,
271   WASM_OPCODE_I32_RMW_CMPXCHG = 0x48,
272 };
273 
274 enum : unsigned {
275   WASM_LIMITS_FLAG_HAS_MAX = 0x1,
276   WASM_LIMITS_FLAG_IS_SHARED = 0x2,
277 };
278 
279 enum : unsigned {
280   WASM_SEGMENT_IS_PASSIVE = 0x01,
281   WASM_SEGMENT_HAS_MEMINDEX = 0x02,
282 };
283 
284 // Feature policy prefixes used in the custom "target_features" section
285 enum : uint8_t {
286   WASM_FEATURE_PREFIX_USED = '+',
287   WASM_FEATURE_PREFIX_REQUIRED = '=',
288   WASM_FEATURE_PREFIX_DISALLOWED = '-',
289 };
290 
291 // Kind codes used in the custom "name" section
292 enum : unsigned {
293   WASM_NAMES_FUNCTION = 0x1,
294   WASM_NAMES_LOCAL = 0x2,
295 };
296 
297 // Kind codes used in the custom "linking" section
298 enum : unsigned {
299   WASM_SEGMENT_INFO = 0x5,
300   WASM_INIT_FUNCS = 0x6,
301   WASM_COMDAT_INFO = 0x7,
302   WASM_SYMBOL_TABLE = 0x8,
303 };
304 
305 // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
306 enum : unsigned {
307   WASM_COMDAT_DATA = 0x0,
308   WASM_COMDAT_FUNCTION = 0x1,
309 };
310 
311 // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
312 enum WasmSymbolType : unsigned {
313   WASM_SYMBOL_TYPE_FUNCTION = 0x0,
314   WASM_SYMBOL_TYPE_DATA = 0x1,
315   WASM_SYMBOL_TYPE_GLOBAL = 0x2,
316   WASM_SYMBOL_TYPE_SECTION = 0x3,
317   WASM_SYMBOL_TYPE_EVENT = 0x4,
318 };
319 
320 // Kinds of event attributes.
321 enum WasmEventAttribute : unsigned {
322   WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0,
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,
352   V128 = WASM_TYPE_V128,
353   EXNREF = WASM_TYPE_EXNREF,
354 };
355 
356 struct WasmSignature {
357   SmallVector<ValType, 1> Returns;
358   SmallVector<ValType, 4> Params;
359   // Support empty and tombstone instances, needed by DenseMap.
360   enum { Plain, Empty, Tombstone } State = Plain;
361 
WasmSignatureWasmSignature362   WasmSignature(SmallVector<ValType, 1> &&InReturns,
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
394