emscripten-core / emscripten-core/emscripten

Side module symbols are not exported on module creation

Open
#21,491 5 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

This broke somewhere between 3.1.8 and 3.1.55, but when a main module depends on a side module, the symbols from the side module are not re-exported. The `asm` object has been replaced with `wasmExports`, but this doesn't contain any symbols from any side module. Specifically this block of code has the issue:
```js
function receiveInstance(instance, module) {
wasmExports = instance.exports;

wasmExports = relocateExports(wasmExports, 1024);

var metadata = getDylinkMetadata(module);
mergeLibSymbols(wasmExports, 'main')
LDSO.init();
loadDylibs();

Module['wasmExports'] = wasmExports;

addOnInit(wasmExports['__wasm_call_ctors']);

__RELOC_FUNCS__.push(wasmExports['__wasm_apply_data_relocs']);

removeRunDependency('wasm-instantiate');
return wasmExports;
}
```
The call to `loadDylibs` -> `loadDynamicLibrary` -> `moduleLoaded` -> `mergeLibSymbols` has this note:
```js
var mergeLibSymbols = (exports, libName) => {
// add symbols into global namespace TODO: weak linking etc.
for (var [sym, exp] of Object.entries(exports)) {

// When RTLD_GLOBAL is enabled, the symbols defined by this shared object
// will be made available for symbol resolution of subsequently loaded
// shared objects.
//
// We should copy the symbols (which include methods and variables) from
// SIDE_MODULE to MAIN_MODULE.
const setImport = (target) => {
if (!isSymbolDefined(target)) {
wasmImports[target] = exp;
}
}
....
}
```
This shows (correctly) that the symbols are copied from the side module into the main module with `wasmImports`. But the symbols that are copied are not made available on the `Module` object. And the `wasmImports` object is unreachable--setting `-sEXPORTED_RUNTIME_METHODS=wasmImports,...` doesn't work. This seems like a major oversight.

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.55 (165133b1cc977f0b9a277e42ef809b823157189c)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 6c7805d5d186a6d1263f90b8033ad85e2d2633d7)
Target: wasm32-unknown-emscripten
Thread model: posix
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.