Cannot find -lgcc with new clang (worked with clang 7)

We have been using clang-7 for a few years for address sanitizer and it has been working well, but I want to see if there is anything new so I built clang-12, but when I try to build cmake fails fast because the compiler doesn’t work, cannot find -lgcc

I build all tools from scratch because that is required to import them into our custom build system. With clang-7 I built with gcc and used that, but now I’ve switched to a stage-2 build using the example files in clang/cmake/cache

when I first tried this I was also getting errors about unable to find crtbegin, but a -B to the proper directory killed that error (secondary question - why is the -B needed, shouldn’t clang be able to figure this out itself). Even though there is a libgcc.a in the same directory as crtbegin.o, it isn’t found. (is the .o/.a extension a factor?)

I have tried clang 9.01, 12.0.1, and 13-rc2 and get the same error.

Full output below: One thing I find weird is -fgnuc-version=4.2.1 - but my gcc version is 5.2.0

Run Build Command(s):/usr/bin/ninja cmTC_5a104 
[1/2] Building C object CMakeFiles/cmTC_5a104.dir/testCCompiler.c.o
LLVM.org clang version 12.0.1 (https://github.deere.com/Platform-health/clang.git 8d75984a760e897bf86a45ae924e7edac8d76990)
Target: x86_64-wrs-linux
Thread model: posix
InstalledDir: /opt/jdx/tools/usr/clang12/bin
Found candidate GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/gcc/x86_64-wrs-linux/5.2.0
 (in-process)
 "/opt/jdx/tools/usr/clang12/bin/clang-12" -cc1 -triple x86_64-wrs-linux -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name testCCompiler.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /opt/jdx/tools/usr/clang12/lib/clang/12.0.1 -isysroot /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox -internal-isystem /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/local/include -internal-isystem /opt/jdx/tools/usr/clang12/lib/clang/12.0.1/include -internal-externc-isystem /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/include -internal-externc-isystem /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/include -fdebug-compilation-dir /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/build/CMakeFiles/CMakeTmp -ferror-limit 19 -fgnuc-version=4.2.1 -faddrsig -o CMakeFiles/cmTC_5a104.dir/testCCompiler.c.o -x c testCCompiler.c
clang -cc1 version 12.0.1 based upon LLVM 12.0.1 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/local/include
 /opt/jdx/tools/usr/clang12/lib/clang/12.0.1/include
 /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/include
End of search list.
[2/2] Linking C executable cmTC_5a104
 FAILED: cmTC_5a104 
: && /opt/jdx/tools/usr/clang12/bin//clang-12 --sysroot=/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox -fno-omit-frame-pointer   -B /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/x86_64-wrs-linux/5.2.0 -v --target=x86_64-wrs-linux --target=x86_64-wrs-linux  --sysroot=/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox -rdynamic CMakeFiles/cmTC_5a104.dir/testCCompiler.c.o  -o cmTC_5a104   && :
LLVM.org clang version 12.0.1 (https://github.deere.com/Platform-health/clang.git 8d75984a760e897bf86a45ae924e7edac8d76990)
Target: x86_64-wrs-linux
Thread model: posix
InstalledDir: /opt/jdx/tools/usr/clang12/bin
Found candidate GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/gcc/x86_64-wrs-linux/5.2.0
 "/opt/jdx/tools/sysroots/x86_64-wrlinuxsdk-linux/usr/bin/x86_64-wrs-linux/x86_64-wrs-linux-ld" --sysroot=/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox -export-dynamic -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -export-dynamic -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o cmTC_5a104 /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib/../lib64/crt1.o /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib/../lib64/crti.o /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/x86_64-wrs-linux/5.2.0/crtbegin.o -L/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/lib/../lib64 -L/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib/../lib64 -L/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/lib -L/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib CMakeFiles/cmTC_5a104.dir/testCCompiler.c.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/x86_64-wrs-linux/5.2.0/crtend.o /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib/../lib64/crtn.o
/opt/jdx/tools/sysroots/x86_64-wrlinuxsdk-linux/usr/bin/x86_64-wrs-linux/x86_64-wrs-linux-ld: cannot find -lgcc
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

What’s the output of clang --verbose on your system?

/opt/jdx/tools/usr/clang12/bin/clang-12 --verbose
LLVM.org clang version 12.0.1 (Initiating SAML single sign-on 8d75984a760e897bf86a45ae924e7edac8d76990)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/jdx/tools/usr/clang12/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8

What is the base operating system you are using? Also, have you tried just doing a ‘normal’ build with gcc instead of the 2-stage build?

My best guess is that there is something wrong with your gcc installation. When you run clang --verbose you should be seeing one line that looks like this:

Selected GCC installation: /usr/bin/…/lib/gcc/x86_64-redhat-linux/8

Which I don’t see in your output. You might want to try clang --verbose with the sysroot pointing to the toolchain you want to use.

/opt/jdx/tools/usr/clang12/bin/clang-12 --verbose --target=x86_64-wrs-linux --sysroot=/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox
LLVM.org clang version 12.0.1 (Initiating SAML single sign-on 8d75984a760e897bf86a45ae924e7edac8d76990)
Target: x86_64-wrs-linux
Thread model: posix
InstalledDir: /opt/jdx/tools/usr/clang12/bin
Found candidate GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/gcc/x86_64-wrs-linux/5.2.0

Looks like it finding the correct installation, but not selecting it. Any idea why?

which x86_64-wrs-linux-gcc
/opt/jdx/tools/sysroots/x86_64-wrlinuxsdk-linux/usr/bin/x86_64-wrs-linux/x86_64-wrs-linux-gcc

Note that there is no program named “gcc” in the path, only the cross compile x86_64-wrs-linux-gcc. We are building on ubuntu x86_64 for embedded x86_64 linux - the target OS is thus 95% compatible with the host, but that last 5% has burned us more than once when someone figured out how to bypass the sysroot and linked to an incompatible host library, so now we do all builds in a docker that is carefully stripped to the bare minimum needed to build the target. It is entirely possible that we stripped one too many things out, but I don’t have a guess as to what this might be

Trying with clang built with gcc (instead of a stage-2 build) results in the same thing.

Just for reference, here is what clang-7 reports

/opt/jdx/tools/usr/clang7/bin/clang-7 --verbose --target=x86_64-wrs-linux --sysroot=/home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-wrs-linux
Thread model: posix
InstalledDir: /opt/jdx/tools/usr/clang7/bin
Found candidate GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/gcc/x86_64-wrs-linux/5.2.0
Found candidate GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/x86_64-wrs-linux/5.2.0
Selected GCC installation: /home/hm68841/workspace/gsx/jdx/wr8-baytrail_64-clang/buildbox/usr/lib64/x86_64-wrs-linux/5.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
strong text

If the crtbegin.o file exists in the GCC Installation Directory and is readable, that is usually enough for clang to choose a candidate install.

The gcc detection code is here if you want to try to debug it: https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Gnu.cpp#L1896

You could also try to do a git bisect to see which commit introduced this failure.

Thanks for that hint on where gcc is detected. I was able to discover someone (me back in the day) modified clang-7 to work in our project, the patches didn’t work for clang-12, but once I got on the right track it wasn’t hard to fix things. The libraries are not in one of the standard search paths clang looks in - easy enough to add more search paths…

Clang works for normal builds, but when I try to do address-sanitizer

cannot find libasan_preinit.o: No such file or directory
cannot find -lasan

I have files like

libclang_rt.asan_cxx-i386.a
libclang_rt.asan_cxx-x86_64.a
libclang_rt.asan-i386.a
libclang_rt.asan-preinit-i386.a
libclang_rt.asan-preinit-x86_64.a
libclang_rt.asan-x86_64.a
libclang_rt.asan-x86_64.so

I’m still digging into how this is supposed to work. I’m not sure if things are broken because I have the above files installed in a weird path (but they are in the clang-12 install), or if I need the build the libraries for my target. I’ll be digging into this more, but it is the end of the day so maybe someone else can give me some hints.

clang --print-resource-dir

Should tell you the directory clang expects the sanitizers to be in.

Turns out that one was a misconfiguration on my part. I incorrectly modified my address sanitizer build scripts, they were using gcc which we have configured without asan.

thanks for the help. I’ll probably have more questions later, but for now I have bugs in my code to fix.