4 The WebAssembly version of lld takes WebAssembly binaries as inputs and produces
5 a WebAssembly binary as its output. For the most part it tries to mimic the
6 behaviour of traditional ELF linkers and specifically the ELF lld port. Where
7 possible the command line flags and the semantics should be the same.
13 The WebAssembly object file format used by LLVM and LLD is specified as part of
14 the WebAssembly tool conventions on linking_.
16 This is the object format that the llvm will produce when run with the
17 ``wasm32-unknown-unknown`` target.
22 The WebAssembly version of lld is installed as **wasm-ld**. It shared many
23 common linker flags with **ld.lld** but also includes several
24 WebAssembly-specific options:
26 .. option:: --no-entry
28 Don't search for the entry point symbol (by default ``_start``).
30 .. option:: --export-table
32 Export the function table to the environment.
34 .. option:: --import-table
36 Import the function table from the environment.
38 .. option:: --export-all
40 Export all symbols (normally combined with --no-gc-sections)
42 Note that this will not export linker-generated mutable globals unless
43 the resulting binaryen already includes the 'mutable-globals' features
44 since that would otherwise create and invalid binaryen.
46 .. option:: --export-dynamic
48 When building an executable, export any non-hidden symbols. By default only
49 the entry point and any symbols marked as exports (either via the command line
50 or via the `export-name` source attribute) are exported.
52 .. option:: --global-base=<value>
54 Address at which to place global data.
56 .. option:: --no-merge-data-segments
58 Disable merging of data segments.
60 .. option:: --stack-first
62 Place stack at start of linear memory rather than after data.
64 .. option:: --compress-relocations
66 Relocation targets in the code section are 5-bytes wide in order to
67 potentially accommodate the largest LEB128 value. This option will cause the
68 linker to shrink the code section to remove any padding from the final
69 output. However because it affects code offset, this option is not
70 compatible with outputting debug information.
72 .. option:: --allow-undefined
74 Allow undefined symbols in linked binary. This is the legacy
75 flag which corresponds to ``--unresolve-symbols=ignore`` +
76 ``--import-undefined``.
78 .. option:: --unresolved-symbols=<method>
80 This is a more full featured version of ``--allow-undefined``.
81 The semanatics of the different methods are as follows:
85 Report all unresolved symbols. This is the default. Normally the linker
86 will generate an error message for each reported unresolved symbol but the
87 option ``--warn-unresolved-symbols`` can change this to a warning.
91 Resolve all undefined symbols to zero. For data and function addresses
92 this is trivial. For direct function calls, the linker will generate a
93 trapping stub function in place of the undefined function.
95 .. option:: --import-memory
97 Import memory from the environment.
99 .. option:: --import-undefined
101 Generate WebAssembly imports for undefined symbols, where possible. For
102 example, for function symbols this is always possible, but in general this
103 is not possible for undefined data symbols. Undefined data symbols will
104 still be reported as normal (in accordance with ``--unresolved-symbols``).
106 .. option:: --initial-memory=<value>
108 Initial size of the linear memory. Default: static data size.
110 .. option:: --max-memory=<value>
112 Maximum size of the linear memory. Default: unlimited.
114 By default the function table is neither imported nor exported, but defined
115 for internal use only.
120 In general, where possible, the WebAssembly linker attempts to emulate the
121 behaviour of a traditional ELF linker, and in particular the ELF port of lld.
122 For more specific details on how this is achieved see the tool conventions on
128 One way in which the WebAssembly linker differs from traditional native linkers
129 is that function signature checking is strict in WebAssembly. It is a
130 validation error for a module to contain a call site that doesn't agree with
131 the target signature. Even though this is undefined behaviour in C/C++, it is not
132 uncommon to find this in real-world C/C++ programs. For example, a call site in
133 one compilation unit which calls a function defined in another compilation
134 unit but with too many arguments.
136 In order not to generate such invalid modules, lld has two modes of handling such
137 mismatches: it can simply error-out or it can create stub functions that will
138 trap at runtime (functions that contain only an ``unreachable`` instruction)
139 and use these stub functions at the otherwise invalid call sites.
141 The default behaviour is to generate these stub function and to produce
142 a warning. The ``--fatal-warnings`` flag can be used to disable this behaviour
143 and error out if mismatched are found.
148 When building a shared library any symbols marked as ``visibility=default`` will
151 When building an executable, only the entry point (``_start``) and symbols with
152 the ``WASM_SYMBOL_EXPORTED`` flag are exported by default. In LLVM the
153 ``WASM_SYMBOL_EXPORTED`` flag is set by the ``wasm-export-name`` attribute which
154 in turn can be set using ``__attribute__((export_name))`` clang attribute.
156 In addition, symbols can be exported via the linker command line using
157 ``--export`` (which will error if the symbol is not found) or
158 ``--export-if-defined`` (which will not).
160 Finally, just like with native ELF linker the ``--export-dynamic`` flag can be
161 used to export symbols in the executable which are marked as
162 ``visibility=default``.
167 By default no undefined symbols are allowed in the final binary. The flag
168 ``--allow-undefined`` results in a WebAssembly import being defined for each
169 undefined symbol. It is then up to the runtime to provide such symbols.
171 Alternatively symbols can be marked in the source code as with the
172 ``import_name`` and/or ``import_module`` clang attributes which signals that
173 they are expected to be undefined at static link time.
178 Since WebAssembly is designed with size in mind the linker defaults to
179 ``--gc-sections`` which means that all unused functions and data segments will
180 be stripped from the binary.
182 The symbols which are preserved by default are:
184 - The entry point (by default ``_start``).
185 - Any symbol which is to be exported.
186 - Any symbol transitively referenced by the above.
188 Weak Undefined Functions
189 ~~~~~~~~~~~~~~~~~~~~~~~~
191 On native platforms, calls to weak undefined functions end up as calls to the
192 null function pointer. With WebAssembly, direct calls must reference a defined
193 function (with the correct signature). In order to handle this case the linker
194 will generate function a stub containing only the ``unreachable`` instruction
195 and use this for any direct references to an undefined weak function.
197 For example a runtime call to a weak undefined function ``foo`` will up trapping
198 on ``unreachable`` inside and linker-generated function called
204 - Merging of data section similar to ``SHF_MERGE`` in the ELF world is not
206 - No support for creating shared libraries. The spec for shared libraries in
207 WebAssembly is still in flux:
208 https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
210 .. _linking: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md