emscripten-core / emscripten-core/emscripten
Unable to load nested dynamic library when loading side module
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Please include the following in your bug report:
**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.18 (fae107463db4d20a276a27ede393048deaeebf76)
clang version 22.0.0git (https:/github.com/llvm/llvm-project c13ac9cadf1f9b4fa886b82d1e84a5feb0439023)
Target: wasm32-unknown-emscripten
Thread model: posix
```
I have a side module `side.wasm` that has a dependency on another side module `nested.wasm`. When loading `side.wasm` during run time, `nested.wasm` is detected as a required library and is loaded together. This works if `main.wasm`, `side.wasm` and `nested.wasm` are all in the same directory, but for my project, I have `side.wasm` and `nested.wasm` in a separate folder from `main.wasm`. When loading `side.wasm`, emscripten cannot find `nested.wasm` even though they are in the same folder. I have attached a sample app that showcases this problem. There are ways I can work around this issue but shouldn't `loadDynamicLibrary` look for libraries in parent library's folder?
[nested-dylink.zip](https://github.com/user-attachments/files/23357198/nested-dylink.zip)
build flags:
```
emcc ../../nested.cpp \
-s SIDE_MODULE=1 \
-o nested.wasm
emcc ../../side.cpp \
nested.wasm \
-s SIDE_MODULE=1 \
-o side.wasm
emcc ../worker.cpp \
-Llibs \
libs/side.wasm \
libs/nested.wasm \
-s EXPORTED_FUNCTIONS=_worker_main \
-s MAIN_MODULE=2 \
-s ENVIRONMENT=web,worker \
-s AUTOLOAD_DYLIBS=0 \
-s DYLINK_DEBUG=1 \
--post-js ../postjs.js \
-o worker.js
```
main module code that calls dlopen
```
void *handle = dlopen("libs/side.wasm", RTLD_NOW);
```
console logs:
```
worker.js:276 addRunDependency wasm-instantiate
worker.js:276 locateFile: worker.wasm scriptDirectory: http://localhost:8000/
worker.js:276 asynchronously preparing wasm
worker.js:276 run() called, but dependencies remain, so not running
worker.js:276 updateGOT: adding 29 symbols
worker.js:276 done updateGOT
worker.js:276 dylink needed: (2) ['side.wasm', 'nested.wasm']
worker.js:276 loadDylibs: no libraries to preload
worker.js:276 reportUndefinedSymbols
worker.js:276 done reportUndefinedSymbols
worker.js:276 removeRunDependency wasm-instantiate
worker.js:276 writeStackCookie: 0x00005f40
worker.js:276 initRuntime
worker.js:2077 calling dlopen libs/side.wasm
worker.js:276 dlopenInternal: libs/side.wasm
worker.js:276 loadDynamicLibrary: libs/side.wasm handle: 90072
worker.js:276 existing: ['__main__']
worker.js:276 checking preloadedWasm: libs/side.wasm: not found
worker.js:276 checking filesystem: libs/side.wasm: not found
worker.js:276 locateFile: libs/side.wasm scriptDirectory: http://localhost:8000/
worker.js:276 loadWebAssemblyModule: libs/side.wasm
worker.js:276 dylink needed: ['nested.wasm']
worker.js:276 loadDynamicLibrary: nested.wasm handle: undefined
worker.js:276 existing: (2) ['__main__', 'libs/side.wasm']
worker.js:276 checking preloadedWasm: nested.wasm: not found
worker.js:276 checking filesystem: nested.wasm: not found
worker.js:276 locateFile: nested.wasm scriptDirectory: http://localhost:8000/
worker.js:276 loadWebAssemblyModule: nested.wasm
worker.js:4749 Error in loading dynamic library libs/side.wasm: Error: need to see wasm magic number
dlopenInternal @ worker.js:4749
__dlopen_js @ worker.js:4755
$func73 @ worker.wasm:0x5abe
$func72 @ worker.wasm:0x5a45
$func22 @ worker.wasm:0x486c
$worker_main @ worker.wasm:0x47f7
(anonymous) @ worker.js:596
mainloop @ worker.js:5899
onmessage @ worker.js:5916
(anonymous) @ worker.js:5885
flushMessages @ worker.js:5884
messageResender @ worker.js:5891
setTimeout
messageResender @ worker.js:5893
setTimeout
messageResender @ worker.js:5893
setTimeout
onmessage @ worker.js:5908
worker.js:276 dlSetError: Could not load dynamic lib: libs/side.wasm
Error: need to see wasm magic number
worker.js:2077 dlopen failedCould not load dynamic lib: libs/side.wasm
worker.js:2077 Error: need to see wasm magic number
```
Contributor guide
Assessment
This issue has not been assessed yet.