Using clang with `-rtlib=compiler-rt` and sanitizers fails on some Linux distributions
I get the error on Arch Linux and Alpine Linux. See the logs below.
On Debian everything builds fine (tested on clang 14 and 17, same result).
build-archlinux.log build-debian.log
The issue seems to be that on Debian clang(?) passes --as-needed -lgcc_s to a linker, and on Arch, Alpine and possibly other distros, it does not.
Related to abandoned issue #29055.
@llvm/issue-subscribers-clang-driver
This works as intended but may appear confusing.
In the presence of --rtlib=compiler-rt, the default value of --unwindlib= is none for non-Android-non-AIX OSes and with some other exceptions.
The driver links in neither libgcc/libgcc_s nor libunwind, hence the linker error.
--rtlib=compiler-rt users really need to specify --unwindlib=libunwind as well.
Technically Clang driver can report an error, but this would require another list of OSes to check...
% clang -fsanitize=address --rtlib=compiler-rt a.c -o a
ld.lld: error: undefined symbol: _Unwind_Backtrace
...
% clang -fsanitize=address --rtlib=compiler-rt --unwindlib=libunwind a.c -o a # good
@MaskRay, thanks for the info, didn't know about the -unwindlib flag. Wish it will be added to the manpage, right next to the -rtlib, so that ppl won't get confused like me.
-unwindlib=libgcc fixed it, and is more appropriate and less break-prone than manual -lgcc_s or -Wl,--as-needed,-lgcc_s.
Debian (and apt.llvm.org) seems to implicitly do this by default. And I'm genuinely interested why/how.
#29055 now seems to question the exact thing you answered here, so maybe you could comment on it and potentially close it for the sake of completeness?
Btw, I prefer libunwind over libgcc, but I can't get it to work on Arch :cry:. I get the same error, and strings /usr/lib/libunwind.so.8.0.1 | grep '_Unwind' doesn't return anything...
(`libunwind` is installed, btw)
[remorin@chuwawa:0 ~]$ paru -Qii libunwind
Name : libunwind
Version : 1.6.2-2
Description : Portable and efficient C programming interface (API) to determine the call-chain of a program
Architecture : x86_64
URL : https://www.nongnu.org/libunwind/
Licenses : GPL
Groups : None
Provides : None
Depends On : xz zlib
Optional Deps : None
Required By : gimp gperftools gstreamer lib32-libunwind libcamera libplacebo mesa vulkan-swrast
Optional For : None
Conflicts With : None
Replaces : None
Installed Size : 254.09 KiB
Packager : Jan Alexander Steffens (heftig) <[email protected]>
Build Date : Thu 17 Nov 2022 08:39:58 PM MSK
Install Date : Tue 02 May 2023 02:42:31 AM MSK
Install Reason : Installed as a dependency for another package
Install Script : No
Validated By : Signature
Backup Files :
(none)
[remorin@chuwawa:0 ~]$ ll /usr/lib/libunwind*
lrwxrwxrwx 1 root root 27 Nov 17 2022 /usr/lib/libunwind-coredump.so -> libunwind-coredump.so.0.0.0*
lrwxrwxrwx 1 root root 27 Nov 17 2022 /usr/lib/libunwind-coredump.so.0 -> libunwind-coredump.so.0.0.0*
-rwxr-xr-x 1 root root 18K Nov 17 2022 /usr/lib/libunwind-coredump.so.0.0.0*
lrwxrwxrwx 1 root root 19 Nov 17 2022 /usr/lib/libunwind-generic.so -> libunwind-x86_64.so*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-ptrace.so -> libunwind-ptrace.so.0.0.0*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-ptrace.so.0 -> libunwind-ptrace.so.0.0.0*
-rwxr-xr-x 1 root root 18K Nov 17 2022 /usr/lib/libunwind-ptrace.so.0.0.0*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-setjmp.so -> libunwind-setjmp.so.0.0.0*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-setjmp.so.0 -> libunwind-setjmp.so.0.0.0*
-rwxr-xr-x 1 root root 13K Nov 17 2022 /usr/lib/libunwind-setjmp.so.0.0.0*
lrwxrwxrwx 1 root root 18 Nov 17 2022 /usr/lib/libunwind.so -> libunwind.so.8.0.1*
lrwxrwxrwx 1 root root 18 Nov 17 2022 /usr/lib/libunwind.so.8 -> libunwind.so.8.0.1*
-rwxr-xr-x 1 root root 54K Nov 17 2022 /usr/lib/libunwind.so.8.0.1*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-x86_64.so -> libunwind-x86_64.so.8.0.1*
lrwxrwxrwx 1 root root 25 Nov 17 2022 /usr/lib/libunwind-x86_64.so.8 -> libunwind-x86_64.so.8.0.1*
-rwxr-xr-x 1 root root 66K Nov 17 2022 /usr/lib/libunwind-x86_64.so.8.0.1*
[remorin@chuwawa:0 ~]$ strings /usr/lib/libunwind* | grep '_Unwind'
[ble: exit 1]
[remorin@chuwawa:0 ~]$
I tested it on Alpine and Debian, and it works (even though I installed libunwind-dev 1.6.2 and NOT llvm-libunwind*; also the strings command above does return several __libunwind_Unwind_* symbols, Backtrace and GetIP are among them).
So, I'm not sure what to so at this point now...
~~Maybe reopen the issue with packaging and libunwind labels instead?~~
I will most likely open a new issue (maybe not in this repo?) about that, because I don't wanna change the current title and thus possibly mislead someone.
P.S. Forgor to include the logs: build-libunwind-arch.log build-libunwind-debian.log
-unwindlib=libgcc fixed it, and is more appropriate and less break-prone than manual -lgcc_s or -Wl,--as-needed,-lgcc_s.
--unwindlib=libgcc should work as well, but I think --rtlib=compiler-rt --unwindlib=libgcc is not thoroughly tested as most organizations use llvm-project libunwind with compiler-rt...
https://github.com/llvm/llvm-project/issues/29055 now seems to question the exact thing you answered here, so maybe you could comment on it and potentially close it for the sake of completeness?
Since nobody has responded yet, you may edit that comment to redirect people here.
I will most likely open a new issue (maybe not in this repo?) about that, because I don't wanna change the current title and thus possibly mislead someone.
Yes, a new issue seems appropriate.
Btw, I prefer
libunwindoverlibgcc, but I can't get it to work on Arch 😢. I get the same error, andstrings /usr/lib/libunwind.so.8.0.1 | grep '_Unwind'doesn't return anything... (libunwindis installed, btw)
@REmerald it's worth noting that libunwind installed by pacman is not LLVM libunwind, but an entirely different project. To get LLVM libunwind, you need to use the AUR package. Having said that, I also experience difficulty linking against this package too.