From 8314cf067518a24e0737ddc80cba49e7f702b5b6 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 8 Sep 2025 12:35:53 -0700 Subject: [PATCH] Fix weak symbol handling when loading multiple dynamic libraries. The `currentModuleWeakSymbols` global was being set the wrong time which would lead to the wrong set of symbols being used for a given dylib. Also have another version of this change that removes the global completely but this one is simpler. Fixes: #25214 --- src/lib/libdylink.js | 8 ++--- .../code_size/test_codesize_hello_dylink.json | 4 +-- test/test_core.py | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/lib/libdylink.js b/src/lib/libdylink.js index 488eb19bfa706..637aa7f4a384d 100644 --- a/src/lib/libdylink.js +++ b/src/lib/libdylink.js @@ -617,14 +617,13 @@ var LibraryDylink = { dbg('loadWebAssemblyModule:', libName); #endif var metadata = getDylinkMetadata(binary); - currentModuleWeakSymbols = metadata.weakImports; -#if ASSERTIONS - var originalTable = wasmTable; -#endif // loadModule loads the wasm module after all its dependencies have been loaded. // can be called both sync/async. function loadModule() { +#if ASSERTIONS + var originalTable = wasmTable; +#endif #if PTHREADS // The first thread to load a given module needs to allocate the static // table and memory regions. Later threads re-use the same table region @@ -744,6 +743,7 @@ var LibraryDylink = { } }; var proxy = new Proxy({}, proxyHandler); + currentModuleWeakSymbols = metadata.weakImports; var info = { 'GOT.mem': new Proxy({}, GOTHandler), 'GOT.func': new Proxy({}, GOTHandler), diff --git a/test/code_size/test_codesize_hello_dylink.json b/test/code_size/test_codesize_hello_dylink.json index 311d54f46d40c..86520d2e990cf 100644 --- a/test/code_size/test_codesize_hello_dylink.json +++ b/test/code_size/test_codesize_hello_dylink.json @@ -1,10 +1,10 @@ { "a.out.js": 26969, - "a.out.js.gz": 11478, + "a.out.js.gz": 11479, "a.out.nodebug.wasm": 18567, "a.out.nodebug.wasm.gz": 9199, "total": 45536, - "total_gz": 20677, + "total_gz": 20678, "sent": [ "__heap_base", "__indirect_function_table", diff --git a/test/test_core.py b/test/test_core.py index f18b81aaddfe4..6264c16263ad5 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5278,6 +5278,37 @@ def test_dylink_weak(self): def test_dylink_weak_undef(self): self.dylink_testf(test_file('core/test_dylink_weak_undef.c')) + @needs_dylink + def test_dylink_weak_multilib(self): + create_file('liba.c', ''' + #include + + extern int globalA __attribute__((weak)); + + void LibBFunc(); + + void LibAFunc() { + LibBFunc(); + printf("globalA %p\\n", &globalA); + } + ''') + create_file('libb.c', ''' + #include + + void LibBFunc() { + printf("Hello from b\\n"); + } + ''') + create_file('main.c', ''' + void LibAFunc(); + int main() { + LibAFunc(); + } + ''') + self.run_process([EMCC, 'libb.c', '-o', 'libb.so', '-sSIDE_MODULE'] + self.get_cflags()) + self.run_process([EMCC, 'liba.c', '-o', 'liba.so', '-sSIDE_MODULE', 'libb.so'] + self.get_cflags()) + self.do_runf('main.c', 'Hello from b\n', cflags=['-sMAIN_MODULE=2', '-L.', 'liba.so']) + @node_pthreads @needs_dylink def test_dylink_tls(self):