Skip to content

Commit

Permalink
add shared library support
Browse files Browse the repository at this point in the history
This adds support for building WASI shared libraries per
https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md.

For the time being, the goal is to allow "pseudo-dynamic" linking using the
Component Model per
https://github.com/WebAssembly/component-model/blob/main/design/mvp/examples/SharedEverythingDynamicLinking.md.
This requires all libraries to be available when the component is created, but
still allows runtime symbol resolution via `dlopen`/`dlsym` backed by a static
lookup table.  This is sufficient to support Python native extensions, for
example.  A complete demo using `wit-component` is available at
https://github.com/dicej/component-linking-demo.

This commit adds support for building `libc.so`, `libc++.so`, and `libc++abi.so`
alongside their static counterparts.

Notes:

- I had to refactor `errno` support a bit to avoid a spurious `_ZTH5errno` (AKA "thread-local initialization routine for errno") import in `libc++.so`.
- Long double print and scan are included by default in `libc.so` rather than in a separate library.
- `__main_argc_argv` is now a weak symbol since it's not relevant for reactors.
- `dlclose`/`dlsym` rely on a lookup table provided by the "dynamic" linker via `__wasm_set_libraries`.  Not all flags are supported yet, and unrecognized flags will result in an error.
- This requires https://reviews.llvm.org/D153293, which we will need to backport to LLVM 16 until 17 is released.  I'll open a `wasi-sdk` PR with that change and various Makefile tweaks to support shared libraries.
- `libc.so` is temporarily disabled for the `wasi-threads` build until someone can make `wasi_thread_start.s` position-independent.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Jul 28, 2023
1 parent 9f51a71 commit 5362139
Show file tree
Hide file tree
Showing 14 changed files with 1,028 additions and 816 deletions.
373 changes: 197 additions & 176 deletions Makefile

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ __do_orphaned_stdio_locks
__dummy_reference
__duplocale
__env_rm_add
__errno
__errno_location
__exp2f_data
__exp_data
Expand Down Expand Up @@ -401,6 +402,7 @@ __wasilibc_tell
__wasilibc_unlinkat
__wasilibc_utimens
__wasm_call_dtors
__wasm_set_libraries
__wcscoll_l
__wcsftime_l
__wcsxfrm_l
Expand Down Expand Up @@ -573,6 +575,10 @@ difftime
dirfd
dirname
div
dlclose
dlerror
dlopen
dlsym
dprintf
drand48
drem
Expand All @@ -588,7 +594,6 @@ erfcf
erfcl
erff
erfl
errno
exit
exp
exp10
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi-threads/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <crypt.h>
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <endian.h>
#include <err.h>
#include <errno.h>
Expand Down
Loading

0 comments on commit 5362139

Please sign in to comment.