Closed dvdfreitag closed 1 year ago
On further investigation, it seems this is the issue, from CMLFS/3-chroot/008-Adjust_Toolchain
:
echo " --sysroot=/usr -Wl,-dynamic-linker /lib/ld-musl-$(uname -m).so.1" > \
/llvmtools/bin/$(uname -m)-cmlfs-linux-musl.cfg
Here we are manually instructing clang to always insert -dynamic-linker
, but clang can already build working dynamic libraries with the correct Interpreter:
# echo "int main(){}" > t.c
# clang -o t t.c --verbose -Wl,--verbose
"/llvmtools/bin/clang-12" -cc1 -triple x86_64-pc-linux-musl -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name t.c -mrelocation-model pic -pic-level 2 -pic-is-pie -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 /llvmtools/lib/clang/12.0.0 -isysroot /llvmtools -internal-isystem /llvmtools/usr/local/include -internal-externc-isystem /llvmtools/include -internal-externc-isystem /llvmtools/usr/include -internal-isystem /llvmtools/lib/clang/12.0.0/include -fdebug-compilation-dir /sources/src/utmps-system/temp -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/t-edcaf0.o -x c t.c
# file t
t: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, not stripped
If I remove the -dynamic-linker
section from the config file,
echo " --sysroot=/usr" > /llvmtools/bin/$(uname -m)-cmlfs-linux-musl.cfg
And rebuild:
# x86_64-cmlfs-linux-musl-clang -o t t.c --verbose -Wl,--verbose
Configuration file: /llvmtools/bin/x86_64-cmlfs-linux-musl.cfg
"/llvmtools/bin/clang-12" -cc1 -triple x86_64-cmlfs-linux-musl -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name t.c -mrelocation-model pic -pic-level 2 -pic-is-pie -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 /llvmtools/lib/clang/12.0.0 -isysroot /usr -internal-isystem /usr/usr/local/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/usr/include -internal-isystem /llvmtools/lib/clang/12.0.0/include -fdebug-compilation-dir /sources/src/utmps-system/temp -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/t-9e1d1a.o -x c t.c
# file t
t: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, not stripped
Once this change has been made, with CC=x86_64-cmlfs-linux-musl-clang
:
# file utmps-utmpd
utmps-utmpd: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
# ./utmps-utmpd
utmps-utmpd: fatal: unable to get $IPCREMOTEEUID from environment
utmps
succeeds to build, and works without Segmentation Fault
I created CMLFS/3-chroot/008-Adjust_Toolchain
because the llvmtools toolchain produced binaries that looked for an interpreter in /llvmtools instead on the final system root.
LLVM docs don't seem to be much help when trying to build sandboxed toolchains.
I had trouble starting up a new build with LLVM-12.x and moved on to LLVM-15.0.5. Of course, there seems to be drastic changes in LLVM source from 12.x to 15.x which required me to rethink the method to cross compile clang.
After many painful days of failure, I got LLVM-15.0.5 to cross compile. By now, stage 1 clang [installed in llvmtools] links all binaries with -dynamic-linker /lib/ld-musl-$(uname -m).so.1
without a config file. So, stage 1 clang only needs to be re-configured to change sysroot from /llvmtools
to /usr
... just as you have mentioned above
This has been addressed via commit 0bfc298e97abf3a13c1df0cf2bb120b1c8198e8b
commit bc471a8289ff19ff30934c62df0605cd4e7c1673 now recognizes this change
Issue still present.
Stage1 clang outputs compiled code that uses the dynamic linux loader in /llvmtools/lib
. After the system libc [musl] is built, then stage1 clang needs to be reconfigured to produce compiled code that uses the dynamic linux loader in /lib
Either I add patchelf
to llvmtools to manually patch shared binaries to use the dynamic linux loader in /lib
[until the stage2 clang, the final system's compiler is built] OR .... compile stage1 clang twice, one per dynamic loader path.
Stage0 clang now builds the final system's default compiler which by default produces binaries that use the dynamic linker in /lib/ld-musl-$(uname m).so.1
as this change was done with commit 550cf2c3fc9990ea85f367d0215912214f7e0eb2
Now when utmps is built [in chroot], the final system's clang builds it with no cfg. Therefore, static binaries are not built with -dynamic-linker
.
This change is present on the mussel-bootsrapped
branch. Once I confirm this branch can indeed build a usable CMLFS system, I will merge it into master and CMLFS will then be built with mussel.
I have found the reason why
utmps
fails to build using Clang 12 from/llvmtools/bin
. Since I am trying to build the end system without gcc or binutils, this was a pretty big issue for me.After some short investigation, I found that somehow the static binaries generated by
utmps
were claiming to have an interpreter:And when debugging with
lldb
:But
ldd
correctly recognizes the binaries:I found it extremely strange why a statically linked binary would have an interpreter listed by file, and why
lldb
showed the source of the Segmentation Fault was from withinlibc.so
. So I did a bit more digging.With
CC=x86_64-cmlfs-linux-musl-clang
, Clang seems to always pass-dynamic-linker
, even for-static
(for brevity the linked objects are replaced with ...):With
CC=clang
, the-dynamic-linker
flag is no longer present, however some of the paths are broken, and I must manually pass-L/usr/lib
:Here's how those are symlinked:
Moreover, when building with
CC=clang
, the build not only succeeds,utmps-utmpd
runs without issue:This seems to suggest to me that the out of tree cross compilation of LLVM for
/llvmtools
is somehow broken.The host system is musl Void Linux running under WSL 2 on x86_64. The target system is also x86_64.