dslm4515 / BMLFS

Beyond Musl Linux From Scratch - Build Recipes for MLFS [Musl Linux From Source]
17 stars 4 forks source link

LLVM/Clang in non-Standard Prefix: Cannot Compile #27

Open dslm4515 opened 2 years ago

dslm4515 commented 2 years ago

Installed LLVM-14.0.0 in /opt/build.

LLVM compiles and installs, but cannot compile binaries/libraries:

$ echo 'int main(){}' > dummy.c
$ clang dummy.c -v -Wl,--verbose &> dummy.log
$ grep failed dummy.log
attempt to open crtbeginS.o failed
attempt to open /opt/build/bin/../lib/libgcc.so failed
attempt to open /opt/build/bin/../lib/libgcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/libgcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/libgcc.a failed
attempt to open /opt/X11/lib/libgcc.so failed
attempt to open /opt/X11/lib/libgcc.a failed
attempt to open /opt/build/lib/libgcc.so failed
attempt to open /opt/build/lib/libgcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.a failed
attempt to open /usr/local/lib64/libgcc.so failed
attempt to open /usr/local/lib64/libgcc.a failed
attempt to open /lib64/libgcc.so failed
attempt to open /lib64/libgcc.a failed
attempt to open /usr/lib64/libgcc.so failed
attempt to open /usr/lib64/libgcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.a failed
attempt to open /usr/local/lib/libgcc.so failed
attempt to open /usr/local/lib/libgcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/libgcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/libgcc.a failed
attempt to open /opt/build/bin/../lib/libgcc.so failed
attempt to open /opt/build/bin/../lib/gcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/gcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/gcc.a failed
attempt to open /opt/X11/lib/libgcc.so failed
attempt to open /opt/X11/lib/gcc.a failed
attempt to open /opt/build/lib/libgcc.so failed
attempt to open /opt/build/lib/gcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/gcc.a failed
attempt to open /usr/local/lib64/libgcc.so failed
attempt to open /usr/local/lib64/gcc.a failed
attempt to open /lib64/libgcc.so failed
attempt to open /lib64/gcc.a failed
attempt to open /usr/lib64/libgcc.so failed
attempt to open /usr/lib64/gcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib/gcc.a failed
attempt to open /usr/local/lib/libgcc.so failed
attempt to open /usr/local/lib/gcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/gcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/gcc.a failed
attempt to open /opt/build/bin/../lib/libgcc_s.so failed
attempt to open /opt/build/bin/../lib/libgcc_s.a failed
attempt to open /opt/build/bin/../lib/libgcc.so failed
attempt to open /opt/build/bin/../lib/libgcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/libgcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/libgcc.a failed
attempt to open /opt/X11/lib/libgcc.so failed
attempt to open /opt/X11/lib/libgcc.a failed
attempt to open /opt/build/lib/libgcc.so failed
attempt to open /opt/build/lib/libgcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.a failed
attempt to open /usr/local/lib64/libgcc.so failed
attempt to open /usr/local/lib64/libgcc.a failed
attempt to open /lib64/libgcc.so failed
attempt to open /lib64/libgcc.a failed
attempt to open /usr/lib64/libgcc.so failed
attempt to open /usr/lib64/libgcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.a failed
attempt to open /usr/local/lib/libgcc.so failed
attempt to open /usr/local/lib/libgcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/libgcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/libgcc.a failed
attempt to open /opt/build/bin/../lib/libgcc.so failed
attempt to open /opt/build/bin/../lib/gcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/gcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/gcc.a failed
attempt to open /opt/X11/lib/libgcc.so failed
attempt to open /opt/X11/lib/gcc.a failed
attempt to open /opt/build/lib/libgcc.so failed
attempt to open /opt/build/lib/gcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib64/gcc.a failed
attempt to open /usr/local/lib64/libgcc.so failed
attempt to open /usr/local/lib64/gcc.a failed
attempt to open /lib64/libgcc.so failed
attempt to open /lib64/gcc.a failed
attempt to open /usr/lib64/libgcc.so failed
attempt to open /usr/lib64/gcc.a failed
attempt to open /usr/x86_64-pc-linux-musl/lib/libgcc.so failed
attempt to open /usr/x86_64-pc-linux-musl/lib/gcc.a failed
attempt to open /usr/local/lib/libgcc.so failed
attempt to open /usr/local/lib/gcc.a failed
attempt to open /lib/libgcc.so failed
attempt to open /lib/gcc.a failed
attempt to open /usr/lib/libgcc.so failed
attempt to open /usr/lib/gcc.a failed
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

If trying to compile the Linux kernel with LLVM:

$ LLVM=1 LLVM_IAS=1 make -j4 menuconfig
  HOSTCC  scripts/basic/fixdep
/usr/bin/ld: cannot find crtbeginS.o: No such file or directory
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: cannot find -lgcc
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [scripts/Makefile.host:95: scripts/basic/fixdep] Error 1
make: *** [Makefile:555: scripts_basic] Error 2
dslm4515 commented 2 years ago

Per LLVM mailing lists,

The crt begin/end files are considered part of the standard library, not the compiler.

So ... Adding -B/usr/lib/gcc/x86_64-linux-musl/11.2.0 to clang enabled LLVM to find crtbeginS.o

$ clang -B/usr/lib/gcc/x86_64-linux-musl/11.2.0 dummy.c -v -Wl,--verbose &> dummy.log
$ grep crtbeginS dummy.log | grep attempt
attempt to open /usr/lib/gcc/x86_64-linux-musl/11.2.0/crtbeginS.o 
dslm4515 commented 2 years ago

Then linking the static gcc library fixed the other errors:

$ sudo ln -sv ../../../usr/lib/gcc/x86_64-linux-musl/11.2.0/libgcc.a /opt/build/lib/libgcc.a
dslm4515 commented 2 years ago

Configured clang:

$ sudo ln -s clang-14 /opt/build/bin/x86_64-linux-muls-clang
$ sudo ln -s clang-14 /opt/build/bin/x86_64-linux-musl-clang++
$ cat /opt/build/bin/x86_64-linux-musl.cfg
-B -B/usr/lib/gcc/x86_64-linux-musl/11.2.0

Linux kernel still wont compile with clang:

$ LLVM=1 LLVM_IAS=1  CC="x86_64-linux-musl-clang"  make menuconfig
  HOSTCC  scripts/basic/fixdep
/usr/bin/ld: cannot find crtbeginS.o: No such file or directory
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [scripts/Makefile.host:95: scripts/basic/fixdep] Error 1
make: *** [Makefile:555: scripts_basic] Error 2
dslm4515 commented 2 years ago

Kernel source now build after copying two static objects:

$ sudo cp /usr/lib/gcc/x86_64-linux-musl/11.2.0/crtbeginS.o /opt/build/lib/
$ sudo cp /usr/lib/gcc/x86_64-linux-musl/11.2.0/crtendS.o /opt/build/lib/