From 52096ec4e6fb77713cad6483ecfa6d53026cbce3 Mon Sep 17 00:00:00 2001 From: Yousef AbuHashem <70448611+Yousef02@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:33:26 -0800 Subject: [PATCH 1/2] 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 --- Doc/using/configure.rst | 9 ++++++++ ...-12-03-13-40-00.gh-issue-000000.dYn4Lk.rst | 3 +++ configure | 20 +++++++++++------- configure.ac | 21 ++++++++++++++----- 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-000000.dYn4Lk.rst 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-000000.dYn4Lk.rst b/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-000000.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-000000.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 From 3226cfff71c47f2fffb2646f3d26cf02b26387de Mon Sep 17 00:00:00 2001 From: Yousef AbuHashem <70448611+Yousef02@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:49:08 -0800 Subject: [PATCH 2/2] gh-163114: Name the issue in the NEWS entry Co-Authored-By: Claude Fable 5.2 --- ....dYn4Lk.rst => 2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/WASI/{2026-12-03-13-40-00.gh-issue-000000.dYn4Lk.rst => 2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst} (100%) diff --git a/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-000000.dYn4Lk.rst b/Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst similarity index 100% rename from Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-000000.dYn4Lk.rst rename to Misc/NEWS.d/next/WASI/2026-12-03-13-40-00.gh-issue-163114.dYn4Lk.rst