Skip to content

Commit 52096ec

Browse files
Yousef02claude
andcommitted
Make --enable-wasm-dynamic-linking work for WASI 0.1
configure accepted the option for WASI but left the link flags unimplemented, so they had to be supplied through the environment. * LDSHARED imports a module's undefined symbols instead of failing on them, so the dynamic loader can resolve them. The branch that was meant to set it was unreachable. * On wasm32-wasip1, python.wasm is linked as a position-independent executable that imports its memory and exports its symbols. Later WASI versions link a component and are left as they are. The default build is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 64c20ff commit 52096ec

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

‎Doc/using/configure.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,17 @@ WebAssembly options
642642
Dynamic linking enables ``dlopen``. File size of the executable
643643
increases due to limited dead code elimination and additional features.
644644

645+
On WASI 0.1 (``wasm32-wasip1``), ``python.wasm`` is linked as a
646+
position-independent executable. It cannot run on its own: the host needs a
647+
dynamic loader for the WebAssembly `dynamic linking convention
648+
<https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>`_,
649+
which also provides ``dlopen``.
650+
645651
.. versionadded:: 3.11
646652

653+
.. versionchanged:: next
654+
On WASI 0.1, the option links a position-independent ``python.wasm``.
655+
647656
.. option:: --enable-wasm-pthreads
648657

649658
Turn on pthreads support for WASM.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:option:`--enable-wasm-dynamic-linking` now works for WASI 0.1 without extra
2+
linker flags: ``python.wasm`` is linked as a position-independent executable,
3+
and ``LDSHARED`` links extension modules that a dynamic loader can load.

‎configure‎

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,9 +3948,14 @@ then
39483948
LDCXXSHARED='$(CXX) -dynamiclib -F . -framework $(PYTHONFRAMEWORK)'
39493949
BLDSHARED="$LDSHARED"
39503950
;;
3951-
Emscripten*|WASI*)
3951+
Emscripten*)
39523952
LDSHARED='$(CC) -shared'
39533953
LDCXXSHARED='$(CXX) -shared';;
3954+
WASI*)
3955+
dnl A module's undefined symbols (the interpreter's and libc's)
3956+
dnl become imports, which the dynamic loader resolves.
3957+
LDSHARED='$(CC) -shared -Wl,--unresolved-symbols=import-dynamic'
3958+
LDCXXSHARED='$(CXX) -shared -Wl,--unresolved-symbols=import-dynamic';;
39543959
Linux*|GNU*|QNX*|VxWorks*|Haiku*)
39553960
LDSHARED='$(CC) -shared'
39563961
LDCXXSHARED='$(CXX) -shared';;
@@ -3981,10 +3986,6 @@ then
39813986
NetBSD*|DragonFly*)
39823987
LDSHARED='$(CC) -shared'
39833988
LDCXXSHARED='$(CXX) -shared';;
3984-
WASI*)
3985-
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
3986-
dnl not implemented yet
3987-
]);;
39883989
CYGWIN*)
39893990
LDSHARED='$(CC) -shared -Wl,--enable-auto-image-base'
39903991
LDCXXSHARED='$(CXX) -shared -Wl,--enable-auto-image-base';;
@@ -4083,6 +4084,16 @@ then
40834084
then
40844085
LINKFORSHARED="-Wl,--export-dynamic"
40854086
fi;;
4087+
WASI*)
4088+
dnl Link python.wasm as a position-independent executable that
4089+
dnl shares its memory with the modules it loads and exports its
4090+
dnl symbols to them. main() is exported by name because libc.so
4091+
dnl calls it and CPython is compiled with hidden visibility.
4092+
dnl Only for WASI 0.1: later versions link a component instead.
4093+
AS_CASE([$enable_wasm_dynamic_linking/$host],
4094+
[yes/*-wasi|yes/*-wasip1*], [
4095+
LINKFORSHARED="-Wl,-pie -Wl,--import-memory -Wl,--export-dynamic -Wl,--export=__main_argc_argv"
4096+
]);;
40864097
SunOS/5*) if test "$ac_cv_gcc_compat" = "yes"; then
40874098
if $CC -Xlinker --help 2>&1 | grep export-dynamic >/dev/null
40884099
then

0 commit comments

Comments
 (0)