Skip to content

TypeError (sig undefined) when enabling dynamic linking with -sMAIN_MODULE=2 #22431

Description

@plagunas2

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.64
clang version 19.0.0git (https:/github.com/llvm/llvm-project 4d8e42ea6a89c73f90941fd1b6e899912e31dd34)
Target: wasm32-unknown-emscripten
Thread model: posix

I am trying to port Speed Dreams 2.2.1 with Emscripten on Ubuntu using the game's cmake. Unfortunately, the game relies heavily on loading shared libraries/modules using dlopen; building it statically allowed the game to start initializing on the browser, but failed when attempting to load the menu and other things (which are expected to be built as shared libraries). I've attempted building the game with dynamic linking enabled like so:

I referred to #16281 to enable automatic dynamic linking of shared libraries (as side modules). After some issues with duplicate symbols in some libraries and the main executable, I included -sSIDE_MODULE=2 and set main executable to use -sMAIN_MODULE=2, which seemed to fix the issue. For some reason, I was having issues with mismatched types (WASM_SYMBOL_TYPE_SECTION vs WASM_SYMBOL_TYPE_FUNCTION) with a certain library; building it statically resolved this (this library is never loaded with dlopen).

Main module

IF(EMSCRIPTEN)
	link_directories(${CMAKE_BINARY_DIR}/lib/games/speed-dreams-2/lib)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
ENDIF(EMSCRIPTEN)

SD_ADD_EXECUTABLE(${_TARGET_NAME} ${_SOURCES})

IF(EMSCRIPTEN)
	set(CMAKE_EXECUTABLE_SUFFIX ".html")
	set_target_properties(${_TARGET_NAME} PROPERTIES LINK_FLAGS "-sMAIN_MODULE=2 --preload-file ../../data")
ENDIF(EMSCRIPTEN)

When running the generated html, wasm, and js on a browser, I get this:

Chrome

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'slice')
at generateFuncType (speed-dreams-2.js:1:106138)
at convertJsFunctionToWasm (speed-dreams-2.js:1:106590)
at addFunction (speed-dreams-2.js:1:193354)
at reportUndefinedSymbols (speed-dreams-2.js:1:116925)
at loadDylibs (speed-dreams-2.js:1:117131)
at receiveInstance (speed-dreams-2.js:1:92954)
at receiveInstantiationResult (speed-dreams-2.js:1:93212)

Firefox

Uncaught (in promise) TypeError: sig is undefined
generateFuncType (http://localhost:8000/speed-dreams-2.js:1)
convertJsFunctionToWasm (http://localhost:8000/speed-dreams-2.js:1)
addFunction (http://localhost:8000/speed-dreams-2.js:1)
reportUndefinedSymbols (http://localhost:8000/speed-dreams-2.js:1)
loadDylibs (http://localhost:8000/speed-dreams-2.js:1)
receiveInstance (http://localhost:8000/speed-dreams-2.js:1)
receiveInstantiationResult (http://localhost:8000/speed-dreams-2.js:1)

The identified js code

var generateFuncType = (sig,target)=>{
    var sigRet = sig.slice(0, 1); //sig typeError here
    var sigParam = sig.slice(1); 
    var typeCodes = {
        i: 127,
        p: 127,
        j: 126,
        f: 125,
        d: 124,
        e: 111
    };
    target.push(96);
    uleb128Encode(sigParam.length, target);
    for (var i = 0; i < sigParam.length; ++i) {
        target.push(typeCodes[sigParam[i]])
    }
    if (sigRet == "v") {
        target.push(0)
    } else {
        target.push(1, typeCodes[sigRet])
    }
}

I'm getting the same issue when building Speed Dreams 2.3 on windows with dynamic linking enabled and sERROR_ON_UNDEFINED_SYMBOLS=0 in the main.cpp cmake file. Not entirely sure what the issue may be since I'm fairly new to Emscripten. Is a function not being properly defined somewhere in the wasm or js file?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions