1WebAssembly lld port 2==================== 3 4The WebAssembly version of lld takes WebAssembly binaries as inputs and produces 5a WebAssembly binary as its output. For the most part it tries to mimic the 6behaviour of traditional ELF linkers and specifically the ELF lld port. Where 7possible the command line flags and the semantics should be the same. 8 9 10Object file format 11------------------ 12 13The WebAssembly object file format used by LLVM and LLD is specified as part of 14the WebAssembly tool conventions on linking_. 15 16This is the object format that the llvm will produce when run with the 17``wasm32-unknown-unknown`` target. 18 19Usage 20----- 21 22The WebAssembly version of lld is installed as **wasm-ld**. It shared many 23common linker flags with **ld.lld** but also includes several 24WebAssembly-specific options: 25 26.. option:: --no-entry 27 28 Don't search for the entry point symbol (by default ``_start``). 29 30.. option:: --export-table 31 32 Export the function table to the environment. 33 34.. option:: --import-table 35 36 Import the function table from the environment. 37 38.. option:: --export-all 39 40 Export all symbols (normally combined with --no-gc-sections) 41 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. 45 46.. option:: --export-dynamic 47 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. 51 52.. option:: --global-base=<value> 53 54 Address at which to place global data. 55 56.. option:: --no-merge-data-segments 57 58 Disable merging of data segments. 59 60.. option:: --stack-first 61 62 Place stack at start of linear memory rather than after data. 63 64.. option:: --compress-relocations 65 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. 71 72.. option:: --allow-undefined 73 74 Allow undefined symbols in linked binary. This is the legacy 75 flag which corresponds to ``--unresolve-symbols=ignore`` + 76 ``--import-undefined``. 77 78.. option:: --unresolved-symbols=<method> 79 80 This is a more full featured version of ``--allow-undefined``. 81 The semanatics of the different methods are as follows: 82 83 report-all: 84 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. 88 89 ignore-all: 90 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. 94 95 import-dynamic: 96 97 Undefined symbols generate WebAssembly imports, including undefined data 98 symbols. This is somewhat similar to the --import-undefined option but 99 works all symbol types. This options puts limitations on the type of 100 relocations that are allowed for imported data symbols. Relocations that 101 require absolute data addresses (i.e. All R_WASM_MEMORY_ADDR_I32) will 102 generate an error if they cannot be resolved statically. For clang/llvm 103 this means inputs should be compiled with `-fPIC` (i.e. `pic` or 104 `dynamic-no-pic` relocation models). This options is useful for linking 105 binaries that are themselves static (non-relocatable) but whose undefined 106 symbols are resolved by a dynamic linker. Since the dynamic linking API is 107 experimental, this option currently requires `--experimental-pic` to also 108 be specified. 109 110.. option:: --import-memory 111 112 Import memory from the environment. 113 114.. option:: --import-undefined 115 116 Generate WebAssembly imports for undefined symbols, where possible. For 117 example, for function symbols this is always possible, but in general this 118 is not possible for undefined data symbols. Undefined data symbols will 119 still be reported as normal (in accordance with ``--unresolved-symbols``). 120 121.. option:: --initial-memory=<value> 122 123 Initial size of the linear memory. Default: static data size. 124 125.. option:: --max-memory=<value> 126 127 Maximum size of the linear memory. Default: unlimited. 128 129By default the function table is neither imported nor exported, but defined 130for internal use only. 131 132Behaviour 133--------- 134 135In general, where possible, the WebAssembly linker attempts to emulate the 136behaviour of a traditional ELF linker, and in particular the ELF port of lld. 137For more specific details on how this is achieved see the tool conventions on 138linking_. 139 140Function Signatures 141~~~~~~~~~~~~~~~~~~~ 142 143One way in which the WebAssembly linker differs from traditional native linkers 144is that function signature checking is strict in WebAssembly. It is a 145validation error for a module to contain a call site that doesn't agree with 146the target signature. Even though this is undefined behaviour in C/C++, it is not 147uncommon to find this in real-world C/C++ programs. For example, a call site in 148one compilation unit which calls a function defined in another compilation 149unit but with too many arguments. 150 151In order not to generate such invalid modules, lld has two modes of handling such 152mismatches: it can simply error-out or it can create stub functions that will 153trap at runtime (functions that contain only an ``unreachable`` instruction) 154and use these stub functions at the otherwise invalid call sites. 155 156The default behaviour is to generate these stub function and to produce 157a warning. The ``--fatal-warnings`` flag can be used to disable this behaviour 158and error out if mismatched are found. 159 160Exports 161~~~~~~~ 162 163When building a shared library any symbols marked as ``visibility=default`` will 164be exported. 165 166When building an executable, only the entry point (``_start``) and symbols with 167the ``WASM_SYMBOL_EXPORTED`` flag are exported by default. In LLVM the 168``WASM_SYMBOL_EXPORTED`` flag is set by the ``wasm-export-name`` attribute which 169in turn can be set using ``__attribute__((export_name))`` clang attribute. 170 171In addition, symbols can be exported via the linker command line using 172``--export`` (which will error if the symbol is not found) or 173``--export-if-defined`` (which will not). 174 175Finally, just like with native ELF linker the ``--export-dynamic`` flag can be 176used to export symbols in the executable which are marked as 177``visibility=default``. 178 179Imports 180~~~~~~~ 181 182By default no undefined symbols are allowed in the final binary. The flag 183``--allow-undefined`` results in a WebAssembly import being defined for each 184undefined symbol. It is then up to the runtime to provide such symbols. 185 186Alternatively symbols can be marked in the source code as with the 187``import_name`` and/or ``import_module`` clang attributes which signals that 188they are expected to be undefined at static link time. 189 190Garbage Collection 191~~~~~~~~~~~~~~~~~~ 192 193Since WebAssembly is designed with size in mind the linker defaults to 194``--gc-sections`` which means that all unused functions and data segments will 195be stripped from the binary. 196 197The symbols which are preserved by default are: 198 199- The entry point (by default ``_start``). 200- Any symbol which is to be exported. 201- Any symbol transitively referenced by the above. 202 203Weak Undefined Functions 204~~~~~~~~~~~~~~~~~~~~~~~~ 205 206On native platforms, calls to weak undefined functions end up as calls to the 207null function pointer. With WebAssembly, direct calls must reference a defined 208function (with the correct signature). In order to handle this case the linker 209will generate function a stub containing only the ``unreachable`` instruction 210and use this for any direct references to an undefined weak function. 211 212For example a runtime call to a weak undefined function ``foo`` will up trapping 213on ``unreachable`` inside and linker-generated function called 214``undefined:foo``. 215 216Missing features 217---------------- 218 219- Merging of data section similar to ``SHF_MERGE`` in the ELF world is not 220 supported. 221- No support for creating shared libraries. The spec for shared libraries in 222 WebAssembly is still in flux: 223 https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md 224 225.. _linking: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md 226