Skip to content

Include dynlink.c in every PIC build of libc - #28403

Open
juj wants to merge 1 commit into
emscripten-core:mainfrom
juj:libc_dynlink_in_pic_side_module
Open

juj wants to merge 1 commit into
emscripten-core:mainfrom
juj:libc_dynlink_in_pic_side_module

Conversation

@juj

@juj juj commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Main modules and side modules share the pic library directory, but dynlink.c was added to libc only under -sMAIN_MODULE. A side module linked with an explicit -lc builds libc too, so when such a link was the first one to populate the cache, pic/libc.a was cached without dlopen, __dlsym and _emscripten_find_dylib, and every later -sMAIN_MODULE link failed with undefined symbols until the cache was cleared.

This happens in the test suite on a clean cache when a failing core0.test_dylink_zlib_configure is retried: zlib's Makefile links libz.so with -lc.

Main modules and side modules share the `pic` library directory, but
`dynlink.c` was added to libc only under `-sMAIN_MODULE`. A side module
linked with an explicit `-lc` builds libc too, so when such a link was
the first one to populate the cache, `pic/libc.a` was cached without
`dlopen`, `__dlsym` and `_emscripten_find_dylib`, and every later
`-sMAIN_MODULE` link failed with undefined symbols until the cache was
cleared.

This happens in the test suite on a clean cache when a failing
`core0.test_dylink_zlib_configure` is retried: zlib's Makefile links
`libz.so` with `-lc`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kleisauke

Copy link
Copy Markdown
Collaborator

A side module linked with an explicit -lc builds libc too [...]

I think this should be avoided. See e.g. #16680 (comment) and PR rust-lang/libc#4002.

@juj

juj commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, that's interesting comment. This issue happens in the existing core test suite. Depending on the order of the test run, the dynlink.c function would end up being missing.

Even if that use case is not intended, what do you think of the fix itself?

@juj

juj commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

E.g. core0 suite:

cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/libembind-rtti.a... (this will be cached in "/Users/clb/buildbot/apple-m4-2024-16gb-tahoe-26_0/emscripten_mac26_0_arm64/build/emscripten/main/cache/sysroot/lib/wasm32-emscripten/libembind-rtti.a" for subsequent builds)
error: undefined symbol: _emscripten_find_dylib (referenced by $findLibraryFS, referenced by $loadDynamicLibrary, referenced by $loadDylibs, referenced by root reference (e.g. compiled C/C++ code))
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
warning: __emscripten_find_dylib may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
    at finalCombiner (file:///Users/clb/buildbot/apple-m4-2024-16gb-tahoe-26_0/emscripten_mac26_0_arm64/build/emscripten/main/dist/compiler.mjs:703:29)
    at runJSify (file:///Users/clb/buildbot/apple-m4-2024-16gb-tahoe-26_0/emscripten_mac26_0_arm64/build/emscripten/main/dist/compiler.mjs:736:3)
    at file:///Users/clb/buildbot/apple-m4-2024-16gb-tahoe-26_0/emscripten_mac26_0_arm64/build/emscripten/main/dist/compiler.mjs:793:8
    at ModuleJob.run (node:internal/modules/esm/module_job:439:25)
    at async node:internal/modules/esm/loader:643:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

@kleisauke

Copy link
Copy Markdown
Collaborator

With commit madler/zlib@8dcc311, I think you could do:

--- a/test/common.py
+++ b/test/common.py
@@ -1606,7 +1606,9 @@ class RunnerCore(RetryableTestCase, metaclass=RunnerMeta):
                              make=['cmake', '--build', '.', '--'],
                              make_args=[])
     else:
-      rtn = self.get_library(os.path.join('third_party', 'zlib'), target, make_args=['libz.a', target])
+      rtn = self.get_library(os.path.join('third_party', 'zlib'), target,
+                             configure_args=['LDSHAREDLIBC='],
+                             make_args=['libz.a', target])
     self.cflags = old_args
     return rtn
 

(untested)

But perhaps -lc should be ignored for side modules (just like -lc++, e.g. grep for that in https://github.com/kleisauke/wasm-vips/blob/master/build.sh).

@kleisauke

Copy link
Copy Markdown
Collaborator

That said, zlib's configure script is a bit basic, so I usually build it with CMake instead (or just use zlib-ng if Wasm SIMD is the baseline).

@kleisauke

Copy link
Copy Markdown
Collaborator

This should fix the issue:

--- a/test/common.py
+++ b/test/common.py
@@ -1606,7 +1606,18 @@ class RunnerCore(RetryableTestCase, metaclass=RunnerMeta):
                              make=['cmake', '--build', '.', '--'],
                              make_args=[])
     else:
-      rtn = self.get_library(os.path.join('third_party', 'zlib'), target, make_args=['libz.a', target])
+      if target == 'libz.a':
+        env_init = {}
+      else:
+        env_init = {
+          # Avoid linking with -lc in side modules.
+          # https://github.com/emscripten-core/emscripten/issues/16680#issuecomment-1558015445
+          'LDSHAREDLIBC': '',
+        }
+
+      rtn = self.get_library(os.path.join('third_party', 'zlib'), target,
+                             env_init=env_init,
+                             make_args=['libz.a', target])
     self.cflags = old_args
     return rtn

But only if the system libraries are already cached. For example, if you do:

$ ./emcc --clear-cache
$ ./test/runner core0.test_dylink_zlib_configure

it fails with:

make: *** No rule to make target 'libz.so.1.2.5'.  Stop.

due to this output check:

echo Checking for shared library support...
# we must test in two steps (cc then ld), required at least on SunOS 4.x
if test "`($CC -w -c $SFLAGS $test.c) 2>&1`" = "" &&
test "`($LDSHARED $SFLAGS -o $test$shared_ext $test.o) 2>&1`" = ""; then

which was fixed upstream with commit madler/zlib@2d55657.

@kleisauke

Copy link
Copy Markdown
Collaborator

... perhaps we could just use the zlib sources from the port (instead of test/third_party/zlib), see e.g commit kleisauke@ede23ea.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants