WebAssembly / wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
Apache License 2.0
1.27k stars 190 forks source link

lto link error undefined symbol: setjmp #479

Open calvin2021y opened 2 months ago

calvin2021y commented 2 months ago

I try build with --target=wasm32-wasi -mllvm -wasm-enable-sjlj and -flto=full

get this error:

undefined symbol: setjmp
undefined symbol: longjmp
nm test.o |grep jmp
         U longjmp
         U setjmp

If I remove -flto=full, all work as expect:

 nm test.o |grep jmp
         U __c_longjmp
         U __wasm_longjmp
         U saveSetjmp
         U testSetjmp
yamt commented 2 months ago

i guess you need -Wl,-llvm,-wasm-enable-sjlj.

calvin2021y commented 2 months ago

my link step not call from clang, so I add -mllvm -wasm-enable-sjlj into wasm-ld. this will work like -Wl,-llvm,-wasm-enable-sjlj ?

I get this error when link:

wasm-ld: error: /wasi/share/wasi-sysroot/lib/wasm32-wasi/llvm-lto/18.1.2-wasi-sdk/libsetjmp.a(rt.o): attempt to add bitcode file after LTO (__wasm_longjmp)
Error: wasm-ld failed with status: 1
sbc100 commented 2 months ago

The options for a quick fix would be to either (1) remove -flto from you link command or (2) add -Wl,-u__wasm_longjmp (which would force that symbol to be included prior to LTO).

calvin2021y commented 2 months ago

after add -Wl,-u__wasm_longjmp, the problem fixed.

any suggestion how to run the binary with -wasm-enable-sjl? I try wasm-interp and wasmer, not work.

yamt commented 2 months ago

my link step not call from clang, so I add -mllvm -wasm-enable-sjlj into wasm-ld. this will work like -Wl,-llvm,-wasm-enable-sjlj ?

I get this error when link:

wasm-ld: error: /wasi/share/wasi-sysroot/lib/wasm32-wasi/llvm-lto/18.1.2-wasi-sdk/libsetjmp.a(rt.o): attempt to add bitcode file after LTO (__wasm_longjmp)
Error: wasm-ld failed with status: 1

this looks like a bug. a fix: https://github.com/WebAssembly/wasi-libc/pull/529 for now, you can stop using lto as a workaround.

yamt commented 2 months ago

after add -Wl,-u__wasm_longjmp, the problem fixed.

any suggestion how to run the binary with -wasm-enable-sjl? I try wasm-interp and wasmer, not work.

you can't run it.

calvin2021y commented 2 months ago

thanks for explain.

Is there a link I can download llvm19 sdk ? (like nightly build results)

after llvm 19 release, can I run the wasm binary in browser ?

yamt commented 2 months ago

thanks for explain.

Is there a link I can download llvm19 sdk ? (like nightly build results)

after llvm 19 release, can I run the wasm binary in browser ?

if you are more interested in browsers, i guess it's better to try emscripten. (i'm not familiar with it.)

sbc100 commented 2 months ago

Yes, emscripten has supported setjmp/longjmp for many years out-of-the-box.