1 package wasm
2 3 type section struct {
4 id sectionID
5 name string
6 size uint32
7 }
8 9 func (s *section) ID() uint8 { return uint8(s.id) }
10 func (s *section) Name() string { return s.name }
11 func (s *section) Size() uint32 { return s.size }
12 13 // A Section contains all the information for a single section in the WASM
14 // file. A file is built up of zero or more sections.
15 type Section interface {
16 // ID returns the WASM identifier of the section, for example 0x0A for the
17 // code section.
18 ID() uint8
19 20 // Name returns the name of the section.
21 Name() string
22 23 // Size returns the size of the section in bytes.
24 Size() uint32
25 }
26 27 // SectionCustom is a custom or name section added by the compiler that
28 // generated the WASM file.
29 type SectionCustom struct {
30 // SectionName is the name of the section as defined in the wasm file.
31 SectionName string
32 33 // Payload is the raw payload for the section.
34 Payload []byte
35 36 *section
37 }
38 39 // SectionType declares all function type definitions used in the module.
40 type SectionType struct {
41 // Entries are the entries in a Type section. Each entry declares one type.
42 Entries []FuncType
43 44 *section
45 }
46 47 // A FuncType is the description of a function signature.
48 type FuncType struct {
49 // Form is the value for a func type constructor (always 0x60, the op code
50 // for a function).
51 Form int8
52 53 // Params contains the parameter types of the function.
54 Params []int8
55 56 // ReturnCount returns the number of results from the function.
57 // The value will be 0 or 1.
58 //
59 // Future version may allow more: https://github.com/WebAssembly/design/issues/1146
60 ReturnCount uint8
61 62 // ReturnType is the result type if ReturnCount > 0.
63 ReturnTypes []int8
64 }
65 66 // SectionImport declares all imports defined by the module.
67 type SectionImport struct {
68 // Entries contains import entries to the module.
69 Entries []ImportEntry
70 71 *section
72 }
73 74 // ImportEntry describes an individual import to the module.
75 type ImportEntry struct {
76 // Module is the name of the module.
77 Module string
78 79 // Field is the field name being imported.
80 Field string
81 82 // Kind specified the type of import. The import type value will be set
83 // depending on the kind, the other ones will be nil.
84 Kind ExternalKind
85 86 // FunctionType describes a function import, if Kind == ExtKindFunction.
87 FunctionType *FunctionType
88 89 // TableType describes a table import, if Kind == ExtKindTable.
90 TableType *TableType
91 92 // MemoryType describes a memory import, if Kind == ExtKindMemory.
93 MemoryType *MemoryType
94 95 // GlobalType describes a global import, if Kind == ExtKindGlobal.
96 GlobalType *GlobalType
97 }
98 99 // FunctionType the type for a function import.
100 type FunctionType struct {
101 // Index is the index of the function signature.
102 Index uint32
103 }
104 105 // MemoryType is the type for a memory import.
106 type MemoryType struct {
107 // Limits contains memory limits defined by the import.
108 Limits ResizableLimits
109 }
110 111 // TableType is the type for a table import.
112 type TableType struct {
113 // ElemType specifies the type of the elements.
114 ElemType int8
115 116 // Limits specifies the resizable limits of the table.
117 Limits ResizableLimits
118 }
119 120 // GlobalType is the type for a global import.
121 type GlobalType struct {
122 // ContentType is the type of the value.
123 ContentType int8
124 125 // Mutable is true if the global value can be modified.
126 Mutable bool
127 }
128 129 // ResizableLimits describes the limits of a table or memory.
130 type ResizableLimits struct {
131 // Initial is the initial length of the memory.
132 Initial uint32
133 134 // Maximum is the maximum length of the memory. May not be set.
135 Maximum uint32
136 }
137 138 // SectionFunction declares the signatures of all functions in the modules.
139 // The definitions of the functions will be in the code section.
140 type SectionFunction struct {
141 // Types contains a sequence of indices into the type section.
142 Types []uint32
143 144 *section
145 }
146 147 // SectionTable declares a table section. A table is similar to linear memory,
148 // whose elements, instead of being bytes, are opaque values of a particular
149 // table element. This allows the table to contain values -- like GC
150 // references, raw OS handles, or native pointers -- that are accessed by
151 // WebAssembly code indirectly through an integer index.
152 //
153 // https://github.com/WebAssembly/design/blob/master/Semantics.md#table
154 type SectionTable struct {
155 Entries []TableType
156 157 *section
158 }
159 160 // SectionMemory declares a memory section. The section provides an internal
161 // definition of one linear memory.
162 //
163 // https://github.com/WebAssembly/design/blob/master/Modules.md#linear-memory-section
164 type SectionMemory struct {
165 Entries []MemoryType
166 167 *section
168 }
169 170 // SectionGlobal provides an internal definition of global variables.
171 //
172 // https://github.com/WebAssembly/design/blob/master/Modules.md#global-section
173 type SectionGlobal struct {
174 Globals []GlobalVariable
175 176 *section
177 }
178 179 // A GlobalVariable is a global variable defined by the module.
180 type GlobalVariable struct {
181 // Type is the type of the global variable.
182 Type GlobalType
183 184 // Init is an init expression (wasm bytecode) to set the initial value of
185 // the global variable.
186 Init []byte
187 }
188 189 // SectionExport declares exports from the WASM module.
190 //
191 // https://github.com/WebAssembly/design/blob/master/Modules.md#exports
192 type SectionExport struct {
193 Entries []ExportEntry
194 195 *section
196 }
197 198 // ExportEntry specifies an individual export from the module.
199 type ExportEntry struct {
200 // Field is the name of the field being exported.
201 Field string
202 203 // Kind is the kind of export.
204 Kind ExternalKind
205 206 // Index is the index into the corresponding index space.
207 //
208 // https://github.com/WebAssembly/design/blob/master/Modules.md#function-index-space
209 Index uint32
210 }
211 212 // SectionStart defines the start node, if the module has a start node defined.
213 //
214 // https://github.com/WebAssembly/design/blob/master/Modules.md#module-start-function
215 type SectionStart struct {
216 // Index is the index to the start function in the function index space.
217 //
218 // https://github.com/WebAssembly/design/blob/master/Modules.md#function-index-space
219 Index uint32
220 221 *section
222 }
223 224 // SectionElement defines element segments that initialize elements of imported
225 // or internally-defined tables with any other definition in the module.
226 //
227 // https://github.com/WebAssembly/design/blob/master/Modules.md#elements-section
228 type SectionElement struct {
229 // Entries contains the elements.
230 Entries []ElemSegment
231 232 *section
233 }
234 235 // An ElemSegment is an element segment. It initializes a table with initial
236 // values.
237 type ElemSegment struct {
238 // Index is the table index.
239 Index uint32
240 241 // Offset is an init expression (wasm bytecode) to compute the offset at
242 // which to place the elements.
243 Offset []byte
244 245 // Elems contains the sequence of function indicies.
246 Elems []uint32
247 }
248 249 // SectionCode contains a function body for every function in the module.
250 type SectionCode struct {
251 // Bodies contains all function bodies.
252 Bodies []FunctionBody
253 254 *section
255 }
256 257 // A FunctionBody is the body of a function.
258 type FunctionBody struct {
259 // Locals define the local variables of the function.
260 Locals []LocalEntry
261 262 // Code is the wasm bytecode of the function.
263 Code []byte
264 }
265 266 // LocalEntry is a local variable in a function.
267 type LocalEntry struct {
268 // Count specifies the number of the following type.
269 Count uint32
270 271 // Type is the type of the variable.
272 Type int8
273 }
274 275 // SectionData declares the initialized data that is loaded into the linear
276 // memory.
277 type SectionData struct {
278 // Entries contains the data segment entries.
279 Entries []DataSegment
280 281 *section
282 }
283 284 // A DataSegment is a segment of data in the Data section that is loaded into
285 // linear memory.
286 type DataSegment struct {
287 // Index is the linear memory index.
288 //
289 // https://github.com/WebAssembly/design/blob/master/Modules.md#linear-memory-index-space
290 Index uint32
291 292 // Offset is an init expression (wasm bytecode) that computes the offset to
293 // place the data.
294 Offset []byte
295 296 // Data is the raw data to be placed in memory.
297 Data []byte
298 }
299 300 // SectionDataCount is used to simplify single-pass validation and occurs before
301 // the code section. It is not part of the MVP but was added later.
302 type SectionDataCount struct {
303 // Number of data segments in the data section.
304 NumSegments uint32
305 306 *section
307 }
308 309 // SectionName is a custom section that provides debugging information, by
310 // matching indices to human readable names.
311 type SectionName struct {
312 // SectionName is the name of the name section. The value is always "name".
313 SectionName string
314 315 // Module is the name of the WASM module.
316 Module string
317 318 // Functions contains function name mappings.
319 Functions *NameMap
320 321 // Locals contains local function name mappings.
322 Locals *Locals
323 324 // UnknownSections contains unrecognized subsections.
325 UnknownSections []SectionNameUnknown
326 327 *section
328 }
329 330 // SectionLinking is a custom section used by Clang and LLVM.
331 type SectionLinking struct {
332 // SectionName is the name of the name section. The value is always "linking".
333 SectionName string
334 335 // Segments inside the object file. These only seem to contain data values.
336 Segments []LinkingSegment
337 338 // Symbol table of the object file.
339 Symbols []LinkingSymbol
340 341 // UnknownSections contains unrecognized subsections.
342 UnknownSections []SectionLinkingUnknown
343 344 *section
345 }
346 347 // A NameMap is a map that maps an index to a name.
348 type NameMap struct {
349 // Names contains a list of mappings in the NameMap.
350 Names []Naming
351 }
352 353 // Naming is a single function naming. It maps an index to a human readable
354 // function name.
355 type Naming struct {
356 // Index is the index that is being named.
357 Index uint32
358 359 // Name is a UTF-8 name.
360 Name string
361 }
362 363 // Locals assigns name maps to a subset of functions in the function index
364 // space (imports and module-defined).
365 type Locals struct {
366 // Funcs are the functions to be named.
367 Funcs []LocalName
368 }
369 370 // LocalName a name mapping for a local function name.
371 type LocalName struct {
372 // Index is the index of the function whose locals are being named.
373 Index uint32
374 375 // LocalMap is the name mapping for the function.
376 LocalMap NameMap
377 }
378 379 // SectionNameUnknown represents an unknown subsection of the custom name
380 // section.
381 type SectionNameUnknown struct {
382 // Type is the name type code for this subsection.
383 ID uint8
384 385 // Payload is the raw payload for the section.
386 Payload []byte
387 }
388 389 // SectionLinkingUnknown represents an unknown subsection of the custom linking
390 // section.
391 type SectionLinkingUnknown struct {
392 // Type is the type code for this subsection.
393 ID uint8
394 395 // Payload is the raw payload for the section.
396 Payload []byte
397 }
398 399 // LinkingSegment represents a segment as defined in the WebAssembly linking
400 // section.
401 type LinkingSegment struct {
402 Name string // segment name
403 Alignment uint32 // alignment, encoded as a power of 2
404 Flags uint32
405 }
406 407 // A single symbol in the linking section symbol table.
408 type LinkingSymbol struct {
409 // Symbol kind, like function, global, etc.
410 Kind LinkingSymbolKind
411 412 Flags LinkingSymbolFlags
413 414 // This field may be empty for some symbols (such as undefined symbols).
415 Name string
416 417 // Index, value depends on the symbol kind.
418 Index uint32
419 420 // These two fields are only set for defined data symbols.
421 Offset uint64
422 Size uint64
423 }
424 425 // Symbol kind, such as function or global.
426 type LinkingSymbolKind uint8
427 428 const (
429 LinkingSymbolKindFunction LinkingSymbolKind = 0 // SYMTAB_FUNCTION
430 LinkingSymbolKindData LinkingSymbolKind = 1 // SYMTAB_DATA
431 LinkingSymbolKindGlobal LinkingSymbolKind = 2 // SYMTAB_GLOBAL
432 LinkingSymbolKindSection LinkingSymbolKind = 3 // SYMTAB_SECTION
433 LinkingSymbolKindEvent LinkingSymbolKind = 4 // SYMTAB_EVENT
434 LinkingSymbolKindTable LinkingSymbolKind = 5 // SYMTAB_TABLE
435 )
436 437 func (k LinkingSymbolKind) String() string {
438 switch k {
439 case LinkingSymbolKindFunction:
440 return "function"
441 case LinkingSymbolKindData:
442 return "data"
443 case LinkingSymbolKindGlobal:
444 return "global"
445 case LinkingSymbolKindSection:
446 return "section"
447 case LinkingSymbolKindEvent:
448 return "event"
449 case LinkingSymbolKindTable:
450 return "table"
451 default:
452 return "unknown-symbol-kind"
453 }
454 }
455 456 // Symbol flags, such as the local/weak flags (similar to STB_WEAK etc).
457 type LinkingSymbolFlags uint32
458 459 const (
460 LinkingSymbolFlagBindingWeak LinkingSymbolFlags = 1 // WASM_SYM_BINDING_WEAK
461 LinkingSymbolFlagBindingLocal LinkingSymbolFlags = 2 // WASM_SYM_BINDING_LOCAL
462 LinkingSymbolFlagVisibilityHidden LinkingSymbolFlags = 4 // WASM_SYM_VISIBILITY_HIDDEN
463 LinkingSymbolFlagUndefined LinkingSymbolFlags = 0x10 // WASM_SYM_UNDEFINED
464 LinkingSymbolFlagExported LinkingSymbolFlags = 0x20 // WASM_SYM_EXPORTED
465 LinkingSymbolFlagExplicitName LinkingSymbolFlags = 0x40 // WASM_SYM_EXPLICIT_NAME
466 LinkingSymbolFlagNoStrip LinkingSymbolFlags = 0x80 // WASM_SYM_NO_STRIP
467 LinkingSymbolFlagTLS LinkingSymbolFlags = 0x100 // WASM_SYM_TLS
468 LinkingSymbolFlagAbsolute LinkingSymbolFlags = 0x200 // WASM_SYM_ABSOLUTE
469 )
470 471 // ExternalKind is set as the Kind for an import entry. The value specifies
472 // what type of import it is.
473 type ExternalKind uint8
474 475 const (
476 // ExtKindFunction is an imported function.
477 ExtKindFunction ExternalKind = iota
478 479 // ExtKindTable is an imported table.
480 ExtKindTable
481 482 // ExtKindMemory is imported memory.
483 ExtKindMemory
484 485 // ExtKindGlobal is an imported global.
486 ExtKindGlobal
487 )
488