llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.8k stars 11.45k forks source link

Linking against libclang_rt.builtins-wasm32.a fails with undefined symbols (__stack_chk_guard/__stack_chk_fail) #99642

Closed bw-86 closed 1 month ago

bw-86 commented 1 month ago

Hi everyone, I am on Debian Bookworm and installed the following packages from the llvm repository:

clang-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed]
clang-tools-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed]
libc++-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed]
libc++-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed]
libc++1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libc++abi-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed,automatic]
libc++abi-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libc++abi1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libclang-common-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libclang-cpp18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libclang-rt-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed]
libclang-rt-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libclang1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libllvm18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libunwind-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
libunwind-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
lld-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed]
llvm-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
llvm-18-linker-tools/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
llvm-18-runtime/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
llvm-18-tools/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]
llvm-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic]

I am trying to link a small hello world binary for the wasm32-wasi target. This is my source file (wasm.cpp):

#include <print>

int main() {
    std::print("HELLO!\n");
}

I get this error message:

$ clang++-18 -target wasm32-wasi -std=c++23 -fno-exceptions -v wasm.cpp 
Debian clang version 18.1.8 (++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143)
Target: wasm32-unknown-wasi
Thread model: posix
InstalledDir: /usr/bin
 "/usr/lib/llvm-18/bin/clang" -cc1 -triple wasm32-unknown-wasi -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name wasm.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=/tmp -v -fcoverage-compilation-dir=/tmp -resource-dir /usr/lib/llvm-18/lib/clang/18 -internal-isystem /usr/include/wasm32-wasi/c++/v1 -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include/wasm32-wasi -internal-isystem /usr/local/include -internal-isystem /usr/include/wasm32-wasi -internal-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcolor-diagnostics -dwarf-debug-flags "/usr/lib/llvm-18/bin/clang --driver-mode=g++ --target=wasm32-wasi -std=c++23 -fno-exceptions -v wasm.cpp -dumpdir a-" -o /tmp/wasm-43a0c3.o -x c++ wasm.cpp
clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/local/include/wasm32-wasi"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/wasm32-wasi/c++/v1
 /usr/lib/llvm-18/lib/clang/18/include
 /usr/local/include
 /usr/include/wasm32-wasi
 /usr/include
End of search list.
 "/usr/bin/wasm-ld-18" -m wasm32 -L/usr/lib/wasm32-wasi /usr/lib/wasm32-wasi/crt1-command.o /tmp/wasm-43a0c3.o -lc++ -lc++abi -lc /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a -o a.out
wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_guard
wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_guard
wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_fail
clang++-18: error: linker command failed with exit code 1 (use -v to see invocation)

It seems as if libclang_rt.builtins-wasm32.a was compiled with -fstack-protector, but there is no support for this with the chosen target. Adding -fno-stack-protector to my invocation does not change anything since the symbol references do not belong to my translation unit.

Am I doing something wrong here? Thanks for your help!

llvmbot commented 1 month ago

@llvm/issue-subscribers-backend-webassembly

Author: None (bw-86)

Hi everyone, I am on Debian Bookworm and installed the following packages from the llvm repository: ``` clang-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed] clang-tools-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed] libc++-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed] libc++-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed] libc++1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libc++abi-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed,automatic] libc++abi-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libc++abi1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libclang-common-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libclang-cpp18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libclang-rt-18-dev-wasm32/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 all [installed] libclang-rt-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libclang1-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libllvm18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libunwind-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] libunwind-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] lld-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed] llvm-18-dev/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] llvm-18-linker-tools/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] llvm-18-runtime/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] llvm-18-tools/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] llvm-18/unknown,now 1:18.1.8~++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143 amd64 [installed,automatic] ``` I am trying to link a small hello world binary for the wasm32-wasi target. This is my source file (wasm.cpp): ``` #include <print> int main() { std::print("HELLO!\n"); } ``` I get this error message: ``` $ clang++-18 -target wasm32-wasi -std=c++23 -fno-exceptions -v wasm.cpp Debian clang version 18.1.8 (++20240718080534+3b5b5c1ec4a3-1~exp1~20240718200641.143) Target: wasm32-unknown-wasi Thread model: posix InstalledDir: /usr/bin "/usr/lib/llvm-18/bin/clang" -cc1 -triple wasm32-unknown-wasi -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name wasm.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=/tmp -v -fcoverage-compilation-dir=/tmp -resource-dir /usr/lib/llvm-18/lib/clang/18 -internal-isystem /usr/include/wasm32-wasi/c++/v1 -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include/wasm32-wasi -internal-isystem /usr/local/include -internal-isystem /usr/include/wasm32-wasi -internal-isystem /usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcolor-diagnostics -dwarf-debug-flags "/usr/lib/llvm-18/bin/clang --driver-mode=g++ --target=wasm32-wasi -std=c++23 -fno-exceptions -v wasm.cpp -dumpdir a-" -o /tmp/wasm-43a0c3.o -x c++ wasm.cpp clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-pc-linux-gnu ignoring nonexistent directory "/usr/local/include/wasm32-wasi" #include "..." search starts here: #include <...> search starts here: /usr/include/wasm32-wasi/c++/v1 /usr/lib/llvm-18/lib/clang/18/include /usr/local/include /usr/include/wasm32-wasi /usr/include End of search list. "/usr/bin/wasm-ld-18" -m wasm32 -L/usr/lib/wasm32-wasi /usr/lib/wasm32-wasi/crt1-command.o /tmp/wasm-43a0c3.o -lc++ -lc++abi -lc /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a -o a.out wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_guard wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_guard wasm-ld-18: error: /usr/lib/llvm-18/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a(umodti3.c.obj): undefined symbol: __stack_chk_fail clang++-18: error: linker command failed with exit code 1 (use -v to see invocation) ``` It seems as if libclang_rt.builtins-wasm32.a was compiled with `-fstack-protector`, but there is no support for this with the chosen target. Adding `-fno-stack-protector` to my invocation does not change anything since the symbol references do not belong to my translation unit. Am I doing something wrong here? Thanks for your help!
sbc100 commented 1 month ago

Those symbols should be defined in wasi-libc. The come from libc-top-half/musl/src/env/__stack_chk_fail.c which looks like its gets included: https://github.com/WebAssembly/wasi-libc/blob/b9ef79d7dbd47c6c5bafdae760823467c2f60b70/Makefile#L215.

It looks like this got added to wasi-libc in https://github.com/WebAssembly/wasi-libc/pull/351 which in turn was released in wasi-sdk-17. Can you tell which version of wasi-sdk/wasi-libc you have installed?

bw-86 commented 1 month ago

Oh, I see ... my wasi-libc comes from Debian stable, which surely is older than the llvm packages:

The llvm repositories do not contain matching versions I could install instead, is that right? I do have to install a recent one from https://github.com/WebAssembly/wasi-sdk instead?

Edit: But my wasi-libc was installed as a dependency of llvm packages?

$ apt-cache rdepends --installed wasi-libc 
wasi-libc
Reverse Depends:
  libc++abi-18-dev-wasm32
  libc++-18-dev-wasm32
  clang-18
sbc100 commented 1 month ago

Yup it just looks like the version of wasi-libc that is included with your linux distro doesn't have -fstack-protector support. You would need to somehow update the versions of wasi-libc you are using if you want this to work.

I don't think this is an llvm bug so this issue can probably be closed?

bw-86 commented 1 month ago

Okay, thank you. Unless you want to treat it as a packaging bug that llvm packages depend on a Debian package that not actually works?

sbc100 commented 1 month ago

If this is a packaging bug, the right place for filing packaging bugs is downstream with the Debian folks I believe.

The llvm project itself doesn't include any references to wasi-libc or how to package it as far as I know

bw-86 commented 1 month ago

Well, please see the apt-cache rdepends output in my comment above, which shows exactly that: Packages from llvm depending on a Debian package that is not actually compatible. I do not believe that the Debian project would feel responsible for that.

Then again, even the wasi-libc package in Debian Sid currently has a version of 0.0~git20240411.9e8c542-3, so I do not really see how the llvm packages should declare their dependencies better.

I will close this issue. Thanks to you, I know now how to proceed in fixing my system. Usability is not great, but I can live with it.

sbc100 commented 1 month ago

Oh, are you saying these are upstream llvm provides debian packages? I didn't know we did that. I that case it could conceivable be an issue for llvm to bump their wasi-libc dependency? I'm not sure who works on that.

bw-86 commented 4 weeks ago

Yes, I am using llvm packages from these repositories:

deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main
deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main

Should we reopen the issue then?