diff --git a/Doc/using/configure.rst b/Doc/using/configure.rst index 4f2e4dfc88d386..a4bd7784052db0 100644 --- a/Doc/using/configure.rst +++ b/Doc/using/configure.rst @@ -642,8 +642,17 @@ WebAssembly options Dynamic linking enables ``dlopen``. File size of the executable increases due to limited dead code elimination and additional features. + On WASI 0.1 (``wasm32-wasip1``), ``python.wasm`` is linked as a + position-independent executable. It cannot run on its own: the host needs a + dynamic loader for the WebAssembly `dynamic linking convention + `_, + which also provides ``dlopen``. + .. versionadded:: 3.11 + .. versionchanged:: next + On WASI 0.1, the option links a position-independent ``python.wasm``. + .. option:: --enable-wasm-pthreads Turn on pthreads support for WASM. diff --git a/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst b/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst new file mode 100644 index 00000000000000..faa2dc7b460a0d --- /dev/null +++ b/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst @@ -0,0 +1,3 @@ +:option:`--enable-wasm-dynamic-linking` now works for WASI 0.1 without extra +linker flags: ``python.wasm`` is linked as a position-independent executable, +and ``LDSHARED`` links extension modules that a dynamic loader can load. diff --git a/configure b/configure index d8d860b84a370a..5b191a8b27ea53 100755 --- a/configure +++ b/configure @@ -14834,9 +14834,12 @@ then LDCXXSHARED='$(CXX) -dynamiclib -F . -framework $(PYTHONFRAMEWORK)' BLDSHARED="$LDSHARED" ;; - Emscripten*|WASI*) + Emscripten*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; + WASI*) + LDSHARED='$(CC) -shared -Wl,--unresolved-symbols=import-dynamic' + LDCXXSHARED='$(CXX) -shared -Wl,--unresolved-symbols=import-dynamic';; Linux*|GNU*|QNX*|VxWorks*|Haiku*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; @@ -14867,12 +14870,6 @@ then NetBSD*|DragonFly*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; - WASI*) - if test "x$enable_wasm_dynamic_linking" = xyes -then : - - -fi;; CYGWIN*) LDSHARED='$(CC) -shared -Wl,--enable-auto-image-base' LDCXXSHARED='$(CXX) -shared -Wl,--enable-auto-image-base';; @@ -14978,6 +14975,15 @@ printf "%s\n" "#define THREAD_STACK_SIZE 0x$stack_size" >>confdefs.h then LINKFORSHARED="-Wl,--export-dynamic" fi;; + WASI*) + case $enable_wasm_dynamic_linking/$host in #( + yes/*-wasi|yes/*-wasip1*) : + + LINKFORSHARED="-Wl,-pie -Wl,--import-memory -Wl,--export-dynamic -Wl,--export=__main_argc_argv" + ;; #( + *) : + ;; +esac;; SunOS/5*) if test "$ac_cv_gcc_compat" = "yes"; then if $CC -Xlinker --help 2>&1 | grep export-dynamic >/dev/null then diff --git a/configure.ac b/configure.ac index 11fd97ac5b765f..6e5618c1933fb1 100644 --- a/configure.ac +++ b/configure.ac @@ -3948,9 +3948,14 @@ then LDCXXSHARED='$(CXX) -dynamiclib -F . -framework $(PYTHONFRAMEWORK)' BLDSHARED="$LDSHARED" ;; - Emscripten*|WASI*) + Emscripten*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; + WASI*) + dnl A module's undefined symbols (the interpreter's and libc's) + dnl become imports, which the dynamic loader resolves. + LDSHARED='$(CC) -shared -Wl,--unresolved-symbols=import-dynamic' + LDCXXSHARED='$(CXX) -shared -Wl,--unresolved-symbols=import-dynamic';; Linux*|GNU*|QNX*|VxWorks*|Haiku*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; @@ -3981,10 +3986,6 @@ then NetBSD*|DragonFly*) LDSHARED='$(CC) -shared' LDCXXSHARED='$(CXX) -shared';; - WASI*) - AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [ - dnl not implemented yet - ]);; CYGWIN*) LDSHARED='$(CC) -shared -Wl,--enable-auto-image-base' LDCXXSHARED='$(CXX) -shared -Wl,--enable-auto-image-base';; @@ -4083,6 +4084,16 @@ then then LINKFORSHARED="-Wl,--export-dynamic" fi;; + WASI*) + dnl Link python.wasm as a position-independent executable that + dnl shares its memory with the modules it loads and exports its + dnl symbols to them. main() is exported by name because libc.so + dnl calls it and CPython is compiled with hidden visibility. + dnl Only for WASI 0.1: later versions link a component instead. + AS_CASE([$enable_wasm_dynamic_linking/$host], + [yes/*-wasi|yes/*-wasip1*], [ + LINKFORSHARED="-Wl,-pie -Wl,--import-memory -Wl,--export-dynamic -Wl,--export=__main_argc_argv" + ]);; SunOS/5*) if test "$ac_cv_gcc_compat" = "yes"; then if $CC -Xlinker --help 2>&1 | grep export-dynamic >/dev/null then