Repository navigation
Description
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?
Activity
kripken commentedon Aug 22, 2024
This sounds like an unknown dynamic linking bug. Specifically from that call stack it sounds like
sigis not filled in whenaddFunctionis called from the dylink code, which is not valid.@sbc100 is this a known issue?
sbc100 commentedon Aug 22, 2024
Can you catch the error in the debugger and see what
addFunctionis being called with?I looks like there is an assertion in
addFunctionto check that the given function has.sigattribute which you should be hitting? Are you running with-sASSERTIONS(or without passing-O1or above at link time which has the same effect)?plagunas2 commentedon Aug 23, 2024
When running with -sASSERTIONS:
The function
This is also nested within
I do have gl emulation enabled since the game (at least version 2.2.1) uses openGL 1.3. glUseProgram does not have .sig, but _emscripten_glUseProgram does:
Is this mainly a gl emulation issue or emulation + dynamic linking?
sbc100 commentedon Aug 23, 2024
Great, that provides a lot of info to go on. Can you perhaps share the full set of link flags you are using?
plagunas2 commentedon Aug 26, 2024
The main executable with -v:
Many of these were automatically included by Emscripten as I only really explicitly specified pic, gl emulation, --preload-data, sMain_module, and generating an html output.
Some additional info: forgot to mention this earlier, but I did build other libraries that were not loaded with dlopen as static. Everything loaded with dlopen was built as -sSide_Module=2. However, my Speed Dreams build on windows with the same problem was built completely as shared (so I'm guessing this isn't an issue with mixing static and shared).
If this helps, here are the link flags for the windows build:
sbc100 commentedon Aug 26, 2024
Sadly cmake is hiding the actual emcc command here. Can you run your make command with
VERBOSE=1?Can you explain why you have a different/specific "windows" build. Emscripten should work exactly the same whichever platform it runs on an produce identical results, but maybe I'm missing something here?
plagunas2 commentedon Aug 27, 2024
Ah, I started attempting to build Speed Dreams on windows, but changed over to Ubuntu to build a dependency with emconfigure and decided to stay there haha, nothing specifically to do with Emscripten. The game version is also different because I wanted to try out a version with less dependencies to worry about. I only returned to the windows build to see if the issue was related to the game's 2.2.1 code or if it was purely an Emscripten thing, hope that explains it! I'll refer to the Ubuntu build from now on :)
Here's Make with VERBOSE=1:
Btw, thanks for your patience and responsiveness!
Fix LEGACY_GL_EMULATION + MAIN_MODULE
Fix LEGACY_GL_EMULATION + MAIN_MODULE
Fix LEGACY_GL_EMULATION + MAIN_MODULE
sbc100 commentedon Aug 27, 2024
Fix for the missing sig issue is in #22456.
The other issue here looks like undefined symbols at link time. If that issue persists perhaps open a separate issue?
Fix LEGACY_GL_EMULATION + MAIN_MODULE
Fix LEGACY_GL_EMULATION + MAIN_MODULE
Fix LEGACY_GL_EMULATION + MAIN_MODULE
Fix LEGACY_GL_EMULATION + MAIN_MODULE (emscripten-core#22456)