Stream: wasi

Topic: Issue with linking C++ programs on ARM/RISCV


view this post on Zulip Mats Brorsson (Jun 08 2023 at 09:38):

I managed to build wasi-sdk for RISC-V by using a riscv docker container image. However, I cannot compile and link C++ programs to webassembley. C programs work well. Here is the output I get for a simple program:

$ /opt/wasi-sdk/bin/clang++ -v --target=wasm32-unknown-wasi --sysroot=/opt/wasi-sdk/share/wasi-sy
sroot -W -std=c++11 -fvisibility=hidden -DNDEBUG=1 -fno-exceptions -O2   src/HashSet.cpp -o hashset.wasm
clang version 16.0.0
Target: wasm32-unknown-wasi
Thread model: posix
InstalledDir: /opt/wasi-sdk/bin
 "/opt/wasi-sdk/bin/clang-16" -cc1 -triple wasm32-unknown-wasi -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name HashSet.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/ubuntu/wasm/wabench/JetStream2/hashset -resource-dir /opt/wasi-sdk/lib/clang/16 -D NDEBUG=1 -isysroot /opt/wasi-sdk/share/wasi-sysroot -internal-isystem /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/c++/v1 -internal-isystem /opt/wasi-sdk/share/wasi-sysroot/include/c++/v1 -internal-isystem /opt/wasi-sdk/lib/clang/16/include -internal-isystem /opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi -internal-isystem /opt/wasi-sdk/share/wasi-sysroot/include -O2 -W -std=c++11 -fdeprecated-macro -fdebug-compilation-dir=/home/ubuntu/wasm/wabench/JetStream2/hashset -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /tmp/HashSet-91e8a4.o -x c++ src/HashSet.cpp
clang -cc1 version 16.0.0 based upon LLVM 16.0.0 default target wasm32-wasi
ignoring nonexistent directory "/opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi/c++/v1"
ignoring nonexistent directory "/opt/wasi-sdk/share/wasi-sysroot/include/wasm32-wasi"
#include "..." search starts here:
#include <...> search starts here:
 /opt/wasi-sdk/share/wasi-sysroot/include/c++/v1
 /opt/wasi-sdk/lib/clang/16/include
 /opt/wasi-sdk/share/wasi-sysroot/include
End of search list.
 "/opt/wasi-sdk/bin/wasm-ld" -m wasm32 -L/opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi /opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi/crt1-command.o /tmp/HashSet-91e8a4.o -lc++ -lc++abi -lc /opt/wasi-sdk/lib/clang/16/lib/wasi/libclang_rt.builtins-wasm32.a -o hashset.wasm
wasm-ld: error: /opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi/libc++abi.a(stdlib_new_delete.cpp.o): undefined symbol: __cxa_allocate_exception
wasm-ld: error: /opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi/libc++abi.a(stdlib_new_delete.cpp.o): undefined symbol: __cxa_throw
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

I am using the no-exceptions flag but still I get references to symbols related to exceptions (__cxa_allocate_exception and `__cxa_throw).

Any pointers on how to resolve this?

view this post on Zulip Andrew Brown (Jun 09 2023 at 17:56):

I see in https://github.com/WebAssembly/wasi-sdk/issues/329 that you brought up this issue and then realized libcxx was not being built with exceptions turned off. What might be confusing is that it is configured with exceptions turned off: https://github.com/WebAssembly/wasi-sdk/blob/6dde7bb8d639cafe94daf3f946cffeeec68b9f3f/Makefile#L166. But maybe an extra CMAKE_CXX_FLAGS addition might do the trick. I haven't looked much into this myself (apologies for not being more help!).

I have build wasi-sdk for risc-v to use on the VisionFive2 board from StarFive. I have issues with linking C++ programs as the linker insists on undefined references to __cxa_allocate_exceptionand ...
WASI-enabled WebAssembly C/C++ toolchain. Contribute to WebAssembly/wasi-sdk development by creating an account on GitHub.

view this post on Zulip Mats Brorsson (Jun 11 2023 at 05:53):

Thanks for the tip @Andrew Brown . I got a workaround working by setting LIBCXX_CXX_FLAGS in libcxx/CMakeLists.txt and LIBCXXABI_CXX_FLAGS in libcxxabi/CMakeLists.txt to -fno-exceptions.

It's a bit weird that they are not set correctly for RISC-V.


Last updated: Dec 23 2024 at 12:05 UTC