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 basic indices are encoded with their respective value.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-tableidx}{\mathtt{tableidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-memidx}{\mathtt{memidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-globalidx}{\mathtt{globalidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-tagidx}{\mathtt{tagidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-elemidx}{\mathtt{elemidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-dataidx}{\mathtt{dataidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-localidx}{\mathtt{localidx}}} & ::= & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & x \\ & {\href{../binary/modules.html#binary-labelidx}{\mathtt{labelidx}}} & ::= & l{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & l \\ \end{array}\end{split}\]

External indices are encoded by a distiguishing byte followed by an encoding of their respective value.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-externidx}{\mathtt{externidx}}} & ::= & \mathtt{0x00}~~x{:}{\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-externidx}{\mathsf{func}}~x \\ & & | & \mathtt{0x01}~~x{:}{\href{../binary/modules.html#binary-tableidx}{\mathtt{tableidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-externidx}{\mathsf{table}}~x \\ & & | & \mathtt{0x02}~~x{:}{\href{../binary/modules.html#binary-memidx}{\mathtt{memidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-externidx}{\mathsf{memory}}~x \\ & & | & \mathtt{0x03}~~x{:}{\href{../binary/modules.html#binary-globalidx}{\mathtt{globalidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-externidx}{\mathsf{global}}~x \\ & & | & \mathtt{0x04}~~x{:}{\href{../binary/modules.html#binary-tagidx}{\mathtt{tagidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-externidx}{\mathsf{tag}}~x \\ \end{array}\end{split}\]

Sections

Each section consists of

  • a one-byte section id,

  • the \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em32}}}\) length 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 \(N\) and contents described by the grammar \({\mathtt{X}}\).

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{N}({\mathtt{X}}) & ::= & N{:}{\href{../binary/values.html#binary-byte}{\mathtt{byte}}}~~{\mathit{len}}{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{{\mathit{en}}^\ast}{:}{\mathtt{X}} & \quad\Rightarrow\quad{} & {{\mathit{en}}^\ast} & \quad \mbox{if}~ {\mathit{len}} = ||{\mathtt{X}}|| \\ & & | & \epsilon & \quad\Rightarrow\quad{} & \epsilon \\ \end{array}\end{split}\]

For most sections, the contents \({\mathtt{X}}\) encodes a list. In these cases, the empty result \(\epsilon\) is interpreted as the empty list.

Note

Other than for unknown custom sections, the \({\mathit{size}}\) is not required for decoding, but can be used to skip sections when navigating through a binary. The module is malformed if the size does not match the length of the binary contents \({\mathtt{X}}\).

The following section ids are used:

Id

Section

0

custom section

1

type section

2

import section

3

function section

4

table section

5

memory section

6

global section

7

export section

8

start section

9

element section

10

code section

11

data section

12

data count section

13

tag section

Note

Section ids do not always correspond to the order of sections in the encoding of a module.

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.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}} & ::= & {{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{0}({\href{../binary/modules.html#binary-customsec}{\mathtt{custom}}}) \\ & {\href{../binary/modules.html#binary-customsec}{\mathtt{custom}}} & ::= & {\href{../binary/values.html#binary-name}{\mathtt{name}}}~~{{\href{../binary/values.html#binary-byte}{\mathtt{byte}}}^\ast} \\ \end{array}\end{split}\]

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 the list of recursive types of a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-typesec}{\mathtt{typesec}}} & ::= & {{\mathit{ty}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{1}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-type}{\mathtt{type}}})) & \quad\Rightarrow\quad{} & {{\mathit{ty}}^\ast} \\ & {\href{../binary/modules.html#binary-type}{\mathtt{type}}} & ::= & {\mathit{qt}}{:}{\href{../binary/types.html#binary-rectype}{\mathtt{rectype}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-type}{\mathsf{type}}~{\mathit{qt}} \\ \end{array}\end{split}\]

Import Section

The import section has the id 2. It decodes into the list of imports of a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-importsec}{\mathtt{importsec}}} & ::= & {{\mathit{im}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{2}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-import}{\mathtt{import}}})) & \quad\Rightarrow\quad{} & {{\mathit{im}}^\ast} \\ & {\href{../binary/modules.html#binary-import}{\mathtt{import}}} & ::= & {\mathit{nm}}_1{:}{\href{../binary/values.html#binary-name}{\mathtt{name}}}~~{\mathit{nm}}_2{:}{\href{../binary/values.html#binary-name}{\mathtt{name}}}~~{\mathit{xt}}{:}{\href{../binary/types.html#binary-externtype}{\mathtt{externtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-import}{\mathsf{import}}~{\mathit{nm}}_1~{\mathit{nm}}_2~{\mathit{xt}} \\ \end{array}\end{split}\]

Function Section

The function section has the id 3. It decodes into a list of type indices that classify the functions defined by a module. The bodies of the respective functions are encoded separately in the code section.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-funcsec}{\mathtt{funcsec}}} & ::= & {x^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{3}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}})) & \quad\Rightarrow\quad{} & {x^\ast} \\ \end{array}\end{split}\]

Table Section

The table section has the id 4. It decodes into the list of tables defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-tablesec}{\mathtt{tablesec}}} & ::= & {{\mathit{tab}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{4}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-table}{\mathtt{table}}})) & \quad\Rightarrow\quad{} & {{\mathit{tab}}^\ast} \\ & {\href{../binary/modules.html#binary-table}{\mathtt{table}}} & ::= & {\mathit{tt}}{:}{\href{../binary/types.html#binary-tabletype}{\mathtt{tabletype}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-table}{\mathsf{table}}~{\mathit{tt}}~(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}null}}~{\mathit{ht}}) & \quad \mbox{if}~ {\mathit{tt}} = {\mathit{at}}~{\mathit{lim}}~(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~{\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}^?}~{\mathit{ht}}) \\ & & | & \mathtt{0x40}~~\mathtt{0x00}~~{\mathit{tt}}{:}{\href{../binary/types.html#binary-tabletype}{\mathtt{tabletype}}}~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-table}{\mathsf{table}}~{\mathit{tt}}~e \\ \end{array}\end{split}\]

Note

The encoding of a table type cannot start with byte \(\mathtt{0x40}\), hence decoding is unambiguous. The zero byte following it is reserved for future extensions.

Memory Section

The memory section has the id 5. It decodes into the list of memories defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-memsec}{\mathtt{memsec}}} & ::= & {{\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{5}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-mem}{\mathtt{mem}}})) & \quad\Rightarrow\quad{} & {{\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}^\ast} \\ & {\href{../binary/modules.html#binary-mem}{\mathtt{mem}}} & ::= & {\mathit{mt}}{:}{\href{../binary/types.html#binary-memtype}{\mathtt{memtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-mem}{\mathsf{memory}}~{\mathit{mt}} \\ \end{array}\end{split}\]

Global Section

The global section has the id 6. It decodes into the list of globals defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-globalsec}{\mathtt{globalsec}}} & ::= & {{\mathit{glob}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{6}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-global}{\mathtt{global}}})) & \quad\Rightarrow\quad{} & {{\mathit{glob}}^\ast} \\ & {\href{../binary/modules.html#binary-global}{\mathtt{global}}} & ::= & {\mathit{gt}}{:}{\href{../binary/types.html#binary-globaltype}{\mathtt{globaltype}}}~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-global}{\mathsf{global}}~{\mathit{gt}}~e \\ \end{array}\end{split}\]

Export Section

The export section has the id 7. It decodes into the list of exports of a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-exportsec}{\mathtt{exportsec}}} & ::= & {{\mathit{ex}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{7}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-export}{\mathtt{export}}})) & \quad\Rightarrow\quad{} & {{\mathit{ex}}^\ast} \\ & {\href{../binary/modules.html#binary-export}{\mathtt{export}}} & ::= & {\mathit{nm}}{:}{\href{../binary/values.html#binary-name}{\mathtt{name}}}~~{\mathit{xx}}{:}{\href{../binary/modules.html#binary-externidx}{\mathtt{externidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-export}{\mathsf{export}}~{\mathit{nm}}~{\mathit{xx}} \\ \end{array}\end{split}\]

Start Section

The start section has the id 8. It decodes into the optional start function of a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-startsec}{\mathtt{startsec}}} & ::= & {{\href{../syntax/modules.html#syntax-start}{\mathit{start}}}^?}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{8}({\href{../binary/modules.html#binary-start}{\mathtt{start}}}) & \quad\Rightarrow\quad{} & {{\href{../syntax/modules.html#syntax-start}{\mathit{start}}}^?} \\ & {\href{../binary/modules.html#binary-start}{\mathtt{start}}} & ::= & x{:}{\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}} & \quad\Rightarrow\quad{} & (\href{../syntax/modules.html#syntax-start}{\mathsf{start}}~x) \\ \end{array}\end{split}\]

Element Section

The element section has the id 9. It decodes into the list of element segments defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-elemsec}{\mathtt{elemsec}}} & ::= & {{\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{9}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-elem}{\mathtt{elem}}})) & \quad\Rightarrow\quad{} & {{\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}^\ast} \\ & {\href{../binary/modules.html#binary-elemkind}{\mathtt{elemkind}}} & ::= & \mathtt{0x00} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}} \\ & {\href{../binary/modules.html#binary-elem}{\mathtt{elem}}} & ::= & 0{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~e_o{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{y^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}})~{(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~y)^\ast}~(\href{../syntax/modules.html#syntax-elemmode}{\mathsf{active}}~0~e_o) \\ \end{array} } \\ & & | & 1{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{\mathit{rt}}{:}{\href{../binary/modules.html#binary-elemkind}{\mathtt{elemkind}}}~~{y^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\mathit{rt}}~{(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~y)^\ast}~\href{../syntax/modules.html#syntax-elemmode}{\mathsf{passive}} \\ \end{array} } \\ & & | & 2{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~x{:}{\href{../binary/modules.html#binary-tableidx}{\mathtt{tableidx}}}~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{\mathit{rt}}{:}{\href{../binary/modules.html#binary-elemkind}{\mathtt{elemkind}}}~~{y^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\mathit{rt}}~{(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~y)^\ast}~(\href{../syntax/modules.html#syntax-elemmode}{\mathsf{active}}~x~e) \\ \end{array} } \\ & & | & 3{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{\mathit{rt}}{:}{\href{../binary/modules.html#binary-elemkind}{\mathtt{elemkind}}}~~{y^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-funcidx}{\mathtt{funcidx}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\mathit{rt}}~{(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref{.}func}}~y)^\ast}~\href{../syntax/modules.html#syntax-elemmode}{\mathsf{declare}} \\ \end{array} } \\ & & | & 4{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~e_{\mathsf{o}}{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{e^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}})~{e^\ast}~(\href{../syntax/modules.html#syntax-elemmode}{\mathsf{active}}~0~e_{\mathsf{o}}) \\ \end{array} } \\ & & | & 5{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{\mathit{rt}}{:}{\href{../binary/types.html#binary-reftype}{\mathtt{reftype}}}~~{e^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\mathit{rt}}~{e^\ast}~\href{../syntax/modules.html#syntax-elemmode}{\mathsf{passive}} \\ \end{array} } \\ & & | & 6{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~x{:}{\href{../binary/modules.html#binary-tableidx}{\mathtt{tableidx}}}~~e_{\mathsf{o}}{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{e^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}})~{e^\ast}~(\href{../syntax/modules.html#syntax-elemmode}{\mathsf{active}}~x~e_{\mathsf{o}}) \\ \end{array} } \\ & & | & 7{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{\mathit{rt}}{:}{\href{../binary/types.html#binary-reftype}{\mathtt{reftype}}}~~{e^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}) & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\mathit{rt}}~{e^\ast}~\href{../syntax/modules.html#syntax-elemmode}{\mathsf{declare}} \\ \end{array} } \\ \end{array}\end{split}\]

Note

The initial integer can be interpreted as a bitfield. Bit 0 distinguishes a passive or declarative segment from an active 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 the list of code entries that are pairs of lists of locals and expressions. They represent the body of the functions defined by a module. The types of the respective functions are encoded separately in the function section.

The encoding of each code entry consists of

  • the \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em32}}}\) length 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 list whose entries consist of

  • a \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em32}}}\) count,

  • a value type,

denoting count locals of the same value type.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-codesec}{\mathtt{codesec}}} & ::= & {{\mathit{code}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{10}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-code}{\mathtt{code}}})) & \quad\Rightarrow\quad{} & {{\mathit{code}}^\ast} \\ & {\href{../binary/modules.html#binary-code}{\mathtt{code}}} & ::= & {\mathit{len}}{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{\mathit{code}}{:}{\href{../binary/modules.html#binary-func}{\mathtt{func}}} & \quad\Rightarrow\quad{} & {\mathit{code}} & \quad \mbox{if}~ {\mathit{len}} = ||{\href{../binary/modules.html#binary-func}{\mathtt{func}}}|| \\ & {\href{../binary/modules.html#binary-func}{\mathtt{func}}} & ::= & {{{\mathit{loc}}^\ast}^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-local}{\mathtt{locals}}})~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}} & \quad\Rightarrow\quad{} & ({\href{../syntax/conventions.html#notation-concat}{\bigoplus}}\, {{{\mathit{loc}}^\ast}^\ast}, e) & \quad \mbox{if}~ {|{\href{../syntax/conventions.html#notation-concat}{\bigoplus}}\, {{{\mathit{loc}}^\ast}^\ast}|} < {2^{32}} \\ & {\href{../binary/modules.html#binary-local}{\mathtt{locals}}} & ::= & n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~t{:}{\href{../binary/types.html#binary-valtype}{\mathtt{valtype}}} & \quad\Rightarrow\quad{} & {(\href{../syntax/modules.html#syntax-local}{\mathsf{local}}~t)^{n}} \\ \end{array}\end{split}\]

Here, \({\mathit{code}}\) ranges over pairs \(({{\href{../syntax/modules.html#syntax-local}{\mathit{local}}}^\ast}, {\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}})\). Any code for which the length of the resulting sequence is out of bounds of the maximum size of a list is malformed.

Note

Like with sections, the code \({\mathit{size}}\) is not needed for decoding, but can be used to skip functions when navigating through a binary. The module is malformed if a size does not match the length of the respective function code.

Data Section

The data section has the id 11. It decodes into the list of data segments defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-datasec}{\mathtt{datasec}}} & ::= & {{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{11}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-data}{\mathtt{data}}})) & \quad\Rightarrow\quad{} & {{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast} \\ & {\href{../binary/modules.html#binary-data}{\mathtt{data}}} & ::= & 0{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{b^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/values.html#binary-byte}{\mathtt{byte}}}) & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-data}{\mathsf{data}}~{b^\ast}~(\href{../syntax/modules.html#syntax-datamode}{\mathsf{active}}~0~e) \\ & & | & 1{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~{b^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/values.html#binary-byte}{\mathtt{byte}}}) & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-data}{\mathsf{data}}~{b^\ast}~\href{../syntax/modules.html#syntax-datamode}{\mathsf{passive}} \\ & & | & 2{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}}~~x{:}{\href{../binary/modules.html#binary-memidx}{\mathtt{memidx}}}~~e{:}{\href{../binary/instructions.html#binary-expr}{\mathtt{expr}}}~~{b^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/values.html#binary-byte}{\mathtt{byte}}}) & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-data}{\mathsf{data}}~{b^\ast}~(\href{../syntax/modules.html#syntax-datamode}{\mathsf{active}}~x~e) \\ \end{array}\end{split}\]

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.

Data Count Section

The data count section has the id 12. It decodes into an optional \({\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em32}}}\) count that represents the number of data segments in the data section. If this count does not match the length of the data segment list, the module is malformed.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-datacntsec}{\mathtt{datacntsec}}} & ::= & {n^?}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{12}({\href{../binary/modules.html#binary-datacnt}{\mathtt{datacnt}}}) & \quad\Rightarrow\quad{} & {n^?} \\ & {\href{../binary/modules.html#binary-datacnt}{\mathtt{datacnt}}} & ::= & n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1789#1{{\mathtt{u}#1}}\mathdef1789{\mathtt{32}}}} & \quad\Rightarrow\quad{} & n \\ \end{array}\end{split}\]

Note

The data count section is used to simplify single-pass validation. Since the data section occurs after the code section, the \(\mathsf{memory{.}init}\) and \(\mathsf{data{.}drop}\) instructions would not be able to check whether the data segment index is valid until the data section is read. The data count section occurs before the code section, so a single-pass validator can use this count instead of deferring validation.

Tag Section

The tag section has the id 13. It decodes into the list of tags defined by a module.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-tagsec}{\mathtt{tagsec}}} & ::= & {{\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}^\ast}{:}{{\href{../binary/modules.html#binary-section}{\mathtt{section}}}}_{13}({\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-tag}{\mathtt{tag}}})) & \quad\Rightarrow\quad{} & {{\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}^\ast} \\ & {\href{../binary/modules.html#binary-tag}{\mathtt{tag}}} & ::= & x{:}{\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/modules.html#syntax-tag}{\mathsf{tag}}~x \\ \end{array}\end{split}\]

Modules

The encoding of a module starts with a preamble containing a 4-byte magic number (the string \(\def\mathdef1826#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef1826{\backslash0asm}\)) and a version field. The current version of the WebAssembly binary format is 1.

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 lists 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 list. Furthermore, it must be present if any data index occurs in the code section.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}} & {\href{../binary/modules.html#binary-magic}{\mathtt{magic}}} & ::= & \mathtt{0x00}~~\mathtt{0x61}~~\mathtt{0x73}~~\mathtt{0x6D} \\ & {\href{../binary/modules.html#binary-version}{\mathtt{version}}} & ::= & \mathtt{0x01}~~\mathtt{0x00}~~\mathtt{0x00}~~\mathtt{0x00} \\ & {\href{../binary/modules.html#binary-module}{\mathtt{module}}} & ::= & {\href{../binary/modules.html#binary-magic}{\mathtt{magic}}}~~{\href{../binary/modules.html#binary-version}{\mathtt{version}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/types.html#syntax-rectype}{\mathit{type}}}^\ast}{:}{\href{../binary/modules.html#binary-typesec}{\mathtt{typesec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-import}{\mathit{import}}}^\ast}{:}{\href{../binary/modules.html#binary-importsec}{\mathtt{importsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}}}^\ast}{:}{\href{../binary/modules.html#binary-funcsec}{\mathtt{funcsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-table}{\mathit{table}}}^\ast}{:}{\href{../binary/modules.html#binary-tablesec}{\mathtt{tablesec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}^\ast}{:}{\href{../binary/modules.html#binary-memsec}{\mathtt{memsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}^\ast}{:}{\href{../binary/modules.html#binary-tagsec}{\mathtt{tagsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-global}{\mathit{global}}}^\ast}{:}{\href{../binary/modules.html#binary-globalsec}{\mathtt{globalsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-export}{\mathit{export}}}^\ast}{:}{\href{../binary/modules.html#binary-exportsec}{\mathtt{exportsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-start}{\mathit{start}}}^?}{:}{\href{../binary/modules.html#binary-startsec}{\mathtt{startsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}^\ast}{:}{\href{../binary/modules.html#binary-elemsec}{\mathtt{elemsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{n^?}{:}{\href{../binary/modules.html#binary-datacntsec}{\mathtt{datacntsec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{({{\href{../syntax/modules.html#syntax-local}{\mathit{local}}}^\ast}, {\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}})^\ast}{:}{\href{../binary/modules.html#binary-codesec}{\mathtt{codesec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast}~~{{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast}{:}{\href{../binary/modules.html#binary-datasec}{\mathtt{datasec}}} \\ &&& {{\href{../binary/modules.html#binary-customsec}{\mathtt{customsec}}}^\ast} & \quad\Rightarrow\quad{} & & \\ &&& \multicolumn{4}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}l@{}l@{}} \href{../syntax/modules.html#syntax-module}{\mathsf{module}}~{{\href{../syntax/types.html#syntax-rectype}{\mathit{type}}}^\ast}~{{\href{../syntax/modules.html#syntax-import}{\mathit{import}}}^\ast}~{{\href{../syntax/modules.html#syntax-func}{\mathit{func}}}^\ast}~{{\href{../syntax/modules.html#syntax-global}{\mathit{global}}}^\ast}~{{\href{../syntax/modules.html#syntax-table}{\mathit{table}}}^\ast}~{{\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}^\ast}~{{\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}^\ast}~{{\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}^\ast}~{{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast}~{{\href{../syntax/modules.html#syntax-start}{\mathit{start}}}^?}~{{\href{../syntax/modules.html#syntax-export}{\mathit{export}}}^\ast} & & \\ \multicolumn{3}{@{}l@{}}{\quad \begin{array}[t]{@{}l@{}} \quad \begin{array}[t]{@{}l@{}} \mbox{if}~ (n = {|{{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast}|})^? \\ {\land}~ ({n^?} \neq \epsilon \lor {\href{../syntax/modules.html#syntax-dataidx}{\mathrm{dataidx}}}({{\href{../syntax/modules.html#syntax-func}{\mathit{func}}}^\ast}) = \epsilon) \\ {\land}~ ({\href{../syntax/modules.html#syntax-func}{\mathit{func}}} = \href{../syntax/modules.html#syntax-func}{\mathsf{func}}~{\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}}}~{{\href{../syntax/modules.html#syntax-local}{\mathit{local}}}^\ast}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}})^\ast \\ \end{array} \\ \end{array} } \\ \end{array} } \\ \end{array}\end{split}\]

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.