Page 1 of 1

How to build Hello, World! app with musl libc for chroot env

Posted: Thu Dec 10, 2020 4:30 am
by kotovalexarian
I've successfully built musl libc and installed it in sysroot directory /tailix so I even can run sudo chroot /tailix /lib/ld-musl-x86_64.so.1 and get the following result:

Code: Select all

musl libc (x86_64)
Version 1.2.1
Dynamic Program Loader
Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname [args] 
How can I build Hello, World! with my shared musl libc, install it into my sysroot and get it working? Preferably without using cross-compiler. All my tries with different cross- and native-compilers, different CFLAGS and LDFLAGS gives the following results for ldd hello:
  • libc.so => /tailix/usr/lib/libc.so
  • libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
But it never works in chroot.

Re: How to build Hello, World! app with musl libc for chroot

Posted: Fri Dec 11, 2020 2:00 am
by kotovalexarian
I found that if I build with

Code: Select all

ld -o hello /tailix/usr/lib/crt1.o hello.o -lc -L/tailix/usr/lib -rpath=/tailix/usr/lib
then it has ldd

Code: Select all

libc.so => /tailix/usr/lib/libc.so
and I can run it inside chroot with

Code: Select all

sudo chroot /tailix /lib/ld-musl-x86_64.so.1 -- /hello
However, it's almost useless because applications should be executed without such wrapper.

Re: How to build Hello, World! app with musl libc for chroot

Posted: Fri Dec 11, 2020 7:16 am
by eekee
kotovalexarian wrote:I can run it inside chroot with

Code: Select all

sudo chroot /tailix /lib/ld-musl-x86_64.so.1 -- /hello
However, it's almost useless because applications should be executed without such wrapper.
Something's wrong inside your chroot, and I think it's the dynamic linker. Your command there runs the program `/lib/ld-musl-x86_64.so.1` with arguments `-- /hello`. In Linux, a dynamically linked program is like a script: the dynamic linker is run with the program as its argument. I think your chroot contains a dynamic linker for the wrong libc. If I remember right, the dynamic linker is `/lib/ld.so`, so I think this will work:

Code: Select all

cp /tailix/lib/ld-musl-x86_64.so.1 /tailix/lib/ld.so

Re: How to build Hello, World! app with musl libc for chroot

Posted: Fri Dec 11, 2020 9:59 am
by nullplan
What you are doing looks like the first phase of a Linux from Scratch build. In that, the system is bootstrapped into some directory, say /tailix in your instance, and then that directory contains a symlink of the same name, pointing to dot. Reason for that is to isolate host and build environment.

So, you could get your stuff started, if you just

Code: Select all

ln -s /tailix/tailix .
The whole point is, however, to eventually switch into the chroot environment and then rebuild everything again, to get rid of the symlink.

I would suggest that you don't pursue that path. Rather, look for musl-cross-make and build your applications with that. To get started, maybe static linking is easier, at least until you have enough of an environment inside your chroot jail to build everything with dynamic linking support.

Re: How to build Hello, World! app with musl libc for chroot

Posted: Fri Dec 11, 2020 10:23 am
by eekee
It's been 18 years since I did LFS, I forgot about that symlink.

Re: How to build Hello, World! app with musl libc for chroot

Posted: Fri Dec 11, 2020 11:35 am
by kotovalexarian
eekee wrote:Your command there runs the program /lib/ld-musl-x86_64.so.1 with arguments -- /hello. In Linux, a dynamically linked program is like a script: the dynamic linker is run with the program as its argument.
That's exactly what my command is doing.
nullplan wrote:So, you could get your stuff started, if you just

Code: Select all

ln -s /tailix/tailix .
I thought so, but this doesn't work.
nullplan wrote:To get started, maybe static linking is easier, at least until you have enough of an environment inside your chroot jail to build everything with dynamic linking support.
Yes, I finally decided to use static linking for now.