1c94d393aSSam CleggWebAssembly lld port 2c94d393aSSam Clegg==================== 3c94d393aSSam Clegg 4c94d393aSSam CleggThe WebAssembly version of lld takes WebAssembly binaries as inputs and produces 5db8dd232SSam Clegga WebAssembly binary as its output. For the most part it tries to mimic the 6db8dd232SSam Cleggbehaviour of traditional ELF linkers and specifically the ELF lld port. Where 7f7f00ebcSSam Cleggpossible the command line flags and the semantics should be the same. 8c94d393aSSam Clegg 9c94d393aSSam Clegg 10c94d393aSSam CleggObject file format 11c94d393aSSam Clegg------------------ 12c94d393aSSam Clegg 13f7f00ebcSSam CleggThe WebAssembly object file format used by LLVM and LLD is specified as part of 14adf0aad7SSam Cleggthe WebAssembly tool conventions on linking_. 15c94d393aSSam Clegg 16f7f00ebcSSam CleggThis is the object format that the llvm will produce when run with the 17f7f00ebcSSam Clegg``wasm32-unknown-unknown`` target. 18c94d393aSSam Clegg 19db8dd232SSam CleggUsage 20db8dd232SSam Clegg----- 21db8dd232SSam Clegg 22db8dd232SSam CleggThe WebAssembly version of lld is installed as **wasm-ld**. It shared many 23db8dd232SSam Cleggcommon linker flags with **ld.lld** but also includes several 24db8dd232SSam CleggWebAssembly-specific options: 25db8dd232SSam Clegg 26db8dd232SSam Clegg.. option:: --no-entry 27db8dd232SSam Clegg 28db8dd232SSam Clegg Don't search for the entry point symbol (by default ``_start``). 29db8dd232SSam Clegg 30db8dd232SSam Clegg.. option:: --export-table 31db8dd232SSam Clegg 32db8dd232SSam Clegg Export the function table to the environment. 33db8dd232SSam Clegg 34db8dd232SSam Clegg.. option:: --import-table 35db8dd232SSam Clegg 36db8dd232SSam Clegg Import the function table from the environment. 37db8dd232SSam Clegg 38db8dd232SSam Clegg.. option:: --export-all 39db8dd232SSam Clegg 40db8dd232SSam Clegg Export all symbols (normally combined with --no-gc-sections) 41db8dd232SSam Clegg 423c45a06fSSam Clegg Note that this will not export linker-generated mutable globals unless 433c45a06fSSam Clegg the resulting binaryen already includes the 'mutable-globals' features 443c45a06fSSam Clegg since that would otherwise create and invalid binaryen. 453c45a06fSSam Clegg 46db8dd232SSam Clegg.. option:: --export-dynamic 47db8dd232SSam Clegg 48db8dd232SSam Clegg When building an executable, export any non-hidden symbols. By default only 49881d8778SSam Clegg the entry point and any symbols marked as exports (either via the command line 50881d8778SSam Clegg or via the `export-name` source attribute) are exported. 51db8dd232SSam Clegg 52db8dd232SSam Clegg.. option:: --global-base=<value> 53db8dd232SSam Clegg 54db8dd232SSam Clegg Address at which to place global data. 55db8dd232SSam Clegg 56db8dd232SSam Clegg.. option:: --no-merge-data-segments 57db8dd232SSam Clegg 58db8dd232SSam Clegg Disable merging of data segments. 59db8dd232SSam Clegg 60db8dd232SSam Clegg.. option:: --stack-first 61db8dd232SSam Clegg 62db8dd232SSam Clegg Place stack at start of linear memory rather than after data. 63db8dd232SSam Clegg 64db8dd232SSam Clegg.. option:: --compress-relocations 65db8dd232SSam Clegg 66efabe427SNico Weber Relocation targets in the code section are 5-bytes wide in order to 67efabe427SNico Weber potentially accommodate the largest LEB128 value. This option will cause the 68efabe427SNico Weber linker to shrink the code section to remove any padding from the final 69efabe427SNico Weber output. However because it affects code offset, this option is not 70efabe427SNico Weber compatible with outputting debug information. 71db8dd232SSam Clegg 72db8dd232SSam Clegg.. option:: --allow-undefined 73db8dd232SSam Clegg 74206884bfSSam Clegg Allow undefined symbols in linked binary. This is the legacy 75758633f9SSam Clegg flag which corresponds to ``--unresolve-symbols=ignore`` + 76758633f9SSam Clegg ``--import-undefined``. 77206884bfSSam Clegg 783111784fSSam Clegg.. option:: --allow-undefined-file=<filename> 793111784fSSam Clegg 803111784fSSam Clegg Like ``--allow-undefined``, but the filename specified a flat list of 813111784fSSam Clegg symbols, one per line, which are allowed to be undefined. 823111784fSSam Clegg 83206884bfSSam Clegg.. option:: --unresolved-symbols=<method> 84206884bfSSam Clegg 85206884bfSSam Clegg This is a more full featured version of ``--allow-undefined``. 86206884bfSSam Clegg The semanatics of the different methods are as follows: 87206884bfSSam Clegg 88206884bfSSam Clegg report-all: 89206884bfSSam Clegg 90206884bfSSam Clegg Report all unresolved symbols. This is the default. Normally the linker 91206884bfSSam Clegg will generate an error message for each reported unresolved symbol but the 92206884bfSSam Clegg option ``--warn-unresolved-symbols`` can change this to a warning. 93206884bfSSam Clegg 94206884bfSSam Clegg ignore-all: 95206884bfSSam Clegg 96206884bfSSam Clegg Resolve all undefined symbols to zero. For data and function addresses 97206884bfSSam Clegg this is trivial. For direct function calls, the linker will generate a 98206884bfSSam Clegg trapping stub function in place of the undefined function. 99206884bfSSam Clegg 10086c90f9bSSam Clegg import-dynamic: 10186c90f9bSSam Clegg 10286c90f9bSSam Clegg Undefined symbols generate WebAssembly imports, including undefined data 10386c90f9bSSam Clegg symbols. This is somewhat similar to the --import-undefined option but 10486c90f9bSSam Clegg works all symbol types. This options puts limitations on the type of 10586c90f9bSSam Clegg relocations that are allowed for imported data symbols. Relocations that 10686c90f9bSSam Clegg require absolute data addresses (i.e. All R_WASM_MEMORY_ADDR_I32) will 10786c90f9bSSam Clegg generate an error if they cannot be resolved statically. For clang/llvm 10886c90f9bSSam Clegg this means inputs should be compiled with `-fPIC` (i.e. `pic` or 10986c90f9bSSam Clegg `dynamic-no-pic` relocation models). This options is useful for linking 11086c90f9bSSam Clegg binaries that are themselves static (non-relocatable) but whose undefined 11186c90f9bSSam Clegg symbols are resolved by a dynamic linker. Since the dynamic linking API is 11286c90f9bSSam Clegg experimental, this option currently requires `--experimental-pic` to also 11386c90f9bSSam Clegg be specified. 11486c90f9bSSam Clegg 115db8dd232SSam Clegg.. option:: --import-memory 116db8dd232SSam Clegg 117db8dd232SSam Clegg Import memory from the environment. 118db8dd232SSam Clegg 119758633f9SSam Clegg.. option:: --import-undefined 120758633f9SSam Clegg 121758633f9SSam Clegg Generate WebAssembly imports for undefined symbols, where possible. For 122758633f9SSam Clegg example, for function symbols this is always possible, but in general this 123758633f9SSam Clegg is not possible for undefined data symbols. Undefined data symbols will 124758633f9SSam Clegg still be reported as normal (in accordance with ``--unresolved-symbols``). 125758633f9SSam Clegg 126b2cdf3ccSSingleAccretion.. option:: --initial-heap=<value> 127b2cdf3ccSSingleAccretion 128b2cdf3ccSSingleAccretion Initial size of the heap. Default: zero. 129b2cdf3ccSSingleAccretion 130db8dd232SSam Clegg.. option:: --initial-memory=<value> 131db8dd232SSam Clegg 132b2cdf3ccSSingleAccretion Initial size of the linear memory. Default: the sum of stack, static data and heap sizes. 133db8dd232SSam Clegg 134db8dd232SSam Clegg.. option:: --max-memory=<value> 135db8dd232SSam Clegg 136db8dd232SSam Clegg Maximum size of the linear memory. Default: unlimited. 137db8dd232SSam Clegg 138*cb4f94dbSSingleAccretion.. option:: --no-growable-memory 139*cb4f94dbSSingleAccretion 140*cb4f94dbSSingleAccretion Set maximum size of the linear memory to its initial size, disallowing memory growth. 141*cb4f94dbSSingleAccretion 142db8dd232SSam CleggBy default the function table is neither imported nor exported, but defined 143db8dd232SSam Cleggfor internal use only. 144db8dd232SSam Clegg 145f7f00ebcSSam CleggBehaviour 146adf0aad7SSam Clegg--------- 147adf0aad7SSam Clegg 148adf0aad7SSam CleggIn general, where possible, the WebAssembly linker attempts to emulate the 149f7f00ebcSSam Cleggbehaviour of a traditional ELF linker, and in particular the ELF port of lld. 150adf0aad7SSam CleggFor more specific details on how this is achieved see the tool conventions on 151adf0aad7SSam Clegglinking_. 152adf0aad7SSam Clegg 153f7f00ebcSSam CleggFunction Signatures 1546540e570SSam Clegg~~~~~~~~~~~~~~~~~~~ 1556540e570SSam Clegg 1566540e570SSam CleggOne way in which the WebAssembly linker differs from traditional native linkers 1576540e570SSam Cleggis that function signature checking is strict in WebAssembly. It is a 158f7f00ebcSSam Cleggvalidation error for a module to contain a call site that doesn't agree with 159f7f00ebcSSam Cleggthe target signature. Even though this is undefined behaviour in C/C++, it is not 160f7f00ebcSSam Clegguncommon to find this in real-world C/C++ programs. For example, a call site in 161f7f00ebcSSam Cleggone compilation unit which calls a function defined in another compilation 1626540e570SSam Cleggunit but with too many arguments. 1636540e570SSam Clegg 164f7f00ebcSSam CleggIn order not to generate such invalid modules, lld has two modes of handling such 165f7f00ebcSSam Cleggmismatches: it can simply error-out or it can create stub functions that will 1666540e570SSam Cleggtrap at runtime (functions that contain only an ``unreachable`` instruction) 1676540e570SSam Cleggand use these stub functions at the otherwise invalid call sites. 1686540e570SSam Clegg 169f7f00ebcSSam CleggThe default behaviour is to generate these stub function and to produce 1707cb9c8a5SDan Gohmana warning. The ``--fatal-warnings`` flag can be used to disable this behaviour 1716540e570SSam Cleggand error out if mismatched are found. 1726540e570SSam Clegg 17306f1a5c9SSam CleggExports 17406f1a5c9SSam Clegg~~~~~~~ 175adf0aad7SSam Clegg 176adf0aad7SSam CleggWhen building a shared library any symbols marked as ``visibility=default`` will 177881d8778SSam Cleggbe exported. 178881d8778SSam Clegg 179881d8778SSam CleggWhen building an executable, only the entry point (``_start``) and symbols with 180881d8778SSam Cleggthe ``WASM_SYMBOL_EXPORTED`` flag are exported by default. In LLVM the 181881d8778SSam Clegg``WASM_SYMBOL_EXPORTED`` flag is set by the ``wasm-export-name`` attribute which 182881d8778SSam Cleggin turn can be set using ``__attribute__((export_name))`` clang attribute. 183adf0aad7SSam Clegg 184adf0aad7SSam CleggIn addition, symbols can be exported via the linker command line using 185a6f40648SSam Clegg``--export`` (which will error if the symbol is not found) or 186a6f40648SSam Clegg``--export-if-defined`` (which will not). 187adf0aad7SSam Clegg 188adf0aad7SSam CleggFinally, just like with native ELF linker the ``--export-dynamic`` flag can be 189881d8778SSam Cleggused to export symbols in the executable which are marked as 190adf0aad7SSam Clegg``visibility=default``. 191adf0aad7SSam Clegg 19206f1a5c9SSam CleggImports 19306f1a5c9SSam Clegg~~~~~~~ 19406f1a5c9SSam Clegg 19506f1a5c9SSam CleggBy default no undefined symbols are allowed in the final binary. The flag 19606f1a5c9SSam Clegg``--allow-undefined`` results in a WebAssembly import being defined for each 19706f1a5c9SSam Cleggundefined symbol. It is then up to the runtime to provide such symbols. 1983111784fSSam Clegg``--allow-undefined-file`` is the same but allows a list of symbols to be 1993111784fSSam Cleggspecified. 20006f1a5c9SSam Clegg 201ff9bc0c0SNico WeberAlternatively symbols can be marked in the source code as with the 20206f1a5c9SSam Clegg``import_name`` and/or ``import_module`` clang attributes which signals that 20306f1a5c9SSam Cleggthey are expected to be undefined at static link time. 20406f1a5c9SSam Clegg 2053111784fSSam CleggStub Libraries 2063111784fSSam Clegg~~~~~~~~~~~~~~ 2073111784fSSam Clegg 2083111784fSSam CleggAnother way to specify imports and exports is via a "stub library". This 2093111784fSSam Cleggfeature is inspired by the ELF stub objects which are supported by the Solaris 2103111784fSSam Clegglinker. Stub libraries are text files that can be passed as normal linker 2113111784fSSam Clegginputs, similar to how linker scripts can be passed to the ELF linker. The stub 2123111784fSSam Clegglibrary is a stand-in for a set of symbols that will be available at runtime, 2133111784fSSam Cleggbut doesn't contain any actual code or data. Instead it contains just a list of 2143111784fSSam Cleggsymbols, one per line. Each symbol can specify zero or more dependencies. 2153111784fSSam CleggThese dependencies are symbols that must be defined, and exported, by the output 2163111784fSSam Cleggmodule if the symbol is question is imported/required by the output module. 2173111784fSSam Clegg 2183111784fSSam CleggFor example, imagine the runtime provides an external symbol ``foo`` that 2193111784fSSam Cleggdepends on the ``malloc`` and ``free``. This can be expressed simply as:: 2203111784fSSam Clegg 2213111784fSSam Clegg #STUB 2223111784fSSam Clegg foo: malloc,free 2233111784fSSam Clegg 2243111784fSSam CleggHere we are saying that ``foo`` is allowed to be imported (undefined) but that 2253111784fSSam Cleggif it is imported, then the output module must also export ``malloc`` and 2263111784fSSam Clegg``free`` to the runtime. If ``foo`` is imported (undefined), but the output 2273111784fSSam Cleggmodule does not define ``malloc`` and ``free`` then the link will fail. 2283111784fSSam Clegg 2293111784fSSam CleggStub libraries must begin with ``#STUB`` on a line by itself. 2303111784fSSam Clegg 231adf0aad7SSam CleggGarbage Collection 232adf0aad7SSam Clegg~~~~~~~~~~~~~~~~~~ 233db8dd232SSam Clegg 234db8dd232SSam CleggSince WebAssembly is designed with size in mind the linker defaults to 235db8dd232SSam Clegg``--gc-sections`` which means that all unused functions and data segments will 236db8dd232SSam Cleggbe stripped from the binary. 237db8dd232SSam Clegg 238db8dd232SSam CleggThe symbols which are preserved by default are: 239db8dd232SSam Clegg 240db8dd232SSam Clegg- The entry point (by default ``_start``). 241db8dd232SSam Clegg- Any symbol which is to be exported. 242db8dd232SSam Clegg- Any symbol transitively referenced by the above. 243db8dd232SSam Clegg 244230dc11dSSam CleggWeak Undefined Functions 245230dc11dSSam Clegg~~~~~~~~~~~~~~~~~~~~~~~~ 246230dc11dSSam Clegg 247230dc11dSSam CleggOn native platforms, calls to weak undefined functions end up as calls to the 248230dc11dSSam Cleggnull function pointer. With WebAssembly, direct calls must reference a defined 249230dc11dSSam Cleggfunction (with the correct signature). In order to handle this case the linker 250230dc11dSSam Cleggwill generate function a stub containing only the ``unreachable`` instruction 251230dc11dSSam Cleggand use this for any direct references to an undefined weak function. 252230dc11dSSam Clegg 253230dc11dSSam CleggFor example a runtime call to a weak undefined function ``foo`` will up trapping 254230dc11dSSam Cleggon ``unreachable`` inside and linker-generated function called 255230dc11dSSam Clegg``undefined:foo``. 256db8dd232SSam Clegg 257c94d393aSSam CleggMissing features 258c94d393aSSam Clegg---------------- 259c94d393aSSam Clegg 260db8dd232SSam Clegg- Merging of data section similar to ``SHF_MERGE`` in the ELF world is not 261db8dd232SSam Clegg supported. 262db8dd232SSam Clegg- No support for creating shared libraries. The spec for shared libraries in 263db8dd232SSam Clegg WebAssembly is still in flux: 264c71fbdd8SQuinn Pham https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md 265adf0aad7SSam Clegg 266c71fbdd8SQuinn Pham.. _linking: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md 267