Modules¶
The binary encoding of modules is organized into sections. Most sections correspond to one component of a module record, except that function definitions are split into two sections, separating their type declarations in the function section from their bodies in the code section.
Note
This separation enables parallel and streaming compilation of the functions in a module.
Indices¶
All indices are encoded with their respective value.
Sections¶
Each section consists of
a one-byte section id,
the
size of the contents, in bytes,the actual contents, whose structure is dependent on the section id.
Every section is optional; an omitted section is equivalent to the section being present with empty contents.
The following parameterized grammar rule defines the generic structure of a section with id
For most sections, the contents
Note
Other than for unknown custom sections,
the
The following section ids are used:
Id |
Section |
---|---|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
Custom Section¶
Custom sections have the id 0. They are intended to be used for debugging information or third-party extensions, and are ignored by the WebAssembly semantics. Their contents consist of a name further identifying the custom section, followed by an uninterpreted sequence of bytes for custom use.
Note
If an implementation interprets the data of a custom section, then errors in that data, or the placement of the section, must not invalidate the module.
Type Section¶
The type section has the id 1.
It decodes into a vector of function types that represent the
Import Section¶
The import section has the id 2.
It decodes into a vector of imports that represent the
Function Section¶
The function section has the id 3.
It decodes into a vector of type indices that represent the
Table Section¶
The table section has the id 4.
It decodes into a vector of tables that represent the
Memory Section¶
The memory section has the id 5.
It decodes into a vector of memories that represent the
Global Section¶
The global section has the id 6.
It decodes into a vector of globals that represent the
Export Section¶
The export section has the id 7.
It decodes into a vector of exports that represent the
Start Section¶
The start section has the id 8.
It decodes into an optional start function that represents the
Element Section¶
The element section has the id 9.
It decodes into a vector of element segments that represent the
Note
The initial integer can be interpreted as a bitfield. Bit 0 indicates a passive or declarative segment, bit 1 indicates the presence of an explicit table index for an active segment and otherwise distinguishes passive from declarative segments, bit 2 indicates the use of element type and element expressions instead of element kind and element indices.
Additional element kinds may be added in future versions of WebAssembly.
Code Section¶
The code section has the id 10.
It decodes into a vector of code entries that are pairs of value type vectors and expressions.
They represent the
The encoding of each code entry consists of
the
size of the function code in bytes,the actual function code, which in turn consists of
the declaration of locals,
the function body as an expression.
Local declarations are compressed into a vector whose entries consist of
a
count,a value type,
denoting count locals of the same value type.
Here,
Note
Like with sections, the code
Data Section¶
The data section has the id 11.
It decodes into a vector of data segments that represent the
Note
The initial integer can be interpreted as a bitfield. Bit 0 indicates a passive segment, bit 1 indicates the presence of an explicit memory index for an active segment.
In the current version of WebAssembly, at most one memory may be defined or
imported in a single module, so all valid active data
segments have a
Data Count Section¶
The data count section has the id 12. It decodes into an optional u32 that represents the number of data segments in the data section. If this count does not match the length of the data segment vector, the module is malformed.
Note
The data count section is used to simplify single-pass validation. Since the
data section occurs after the code section, the
Modules¶
The encoding of a module starts with a preamble containing a 4-byte magic number (the string
The preamble is followed by a sequence of sections. Custom sections may be inserted at any place in this sequence, while other sections must occur at most once and in the prescribed order. All sections can be empty.
The lengths of vectors produced by the (possibly empty) function and code section must match up.
Similarly, the optional data count must match the length of the data segment vector. Furthermore, it must be present if any data index occurs in the code section.
where for each
Note
The version of the WebAssembly binary format may increase in the future if backward-incompatible changes have to be made to the format. However, such changes are expected to occur very infrequently, if ever. The binary format is intended to be forward-compatible, such that future extensions can be made without incrementing its version.