A runtime linker for WebAssembly Dynamic Linking.
-
Compatible with WASI
-
Optional dlopen-like API
- Have a cache of loaded modules (struct module) so that they can be actually shared among programs. A simple mechanism for a limited list of libraries (eg. libc) might be good enough.
While this library is implemented using the toywasm API directly, it should be straightforward to port it to other runtime APIs like wasm-c-api except a few things listed below.
The wasm-c-api doesn't provide a way to query the contents of custom sections. You need something runtime-specific.
Maybe simply creating host functions (with wasm_func_new_with_env
)
and making it call the corresponding wasm function (with wasm_func_call
)
is enough for most cases. The approach is not safe if the call is made
with tail call instructions though. Also, with the exception-handling
proposal, such a host function need to propagate execeptions approprately.
As of writing this, wasm-c-api doesn't have an API to deal with exceptions.
For toywasm, we use the restart mechanism to call the function to maintain the tail call guarantee. As it doesn't leave host frames, it doesn't interfere exceptions either. See dyld_plt.c.
The import/export API of wasm-c-api is a bit low-level and cumbersome to use.
Also, the API to link host functions like WASI varies among runtimes. Eg. wasmer-c-api-wasi, wasmtime-c-api-wasi
If you see the error like the following when trying to load a PIE executable,
please make sure your executable imports env.memory
as specified in
WebAssembly Dynamic Linking.
(eg. via --import-memory
wasm-ld option.)
No entry for import env:__indirect_function_table
This error message is admittedly cryptic and probably needs some explanations. In short, it's the result of an attempt to load an PIE executable as a non-PIE executable.
We support both of PIE and non-PIE executables.
To determine if the executable is PIE or not, we make a guess by checking
if the executable imports env.memory
or not. If it does, we assume PIE.
Otherwise, non-PIE.
We provide env.__indirect_function_table
and other resources only for
PIE executables. (Non-PIE executables should provide them by exporting them.)