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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
kotovalexarian
Member
Member
Posts: 38
Joined: Tue Nov 24, 2020 10:17 am
Contact:

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

Post 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.
User avatar
kotovalexarian
Member
Member
Posts: 38
Joined: Tue Nov 24, 2020 10:17 am
Contact:

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

Post 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.
User avatar
eekee
Member
Member
Posts: 891
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

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

Post 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
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

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

Post 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.
Carpe diem!
User avatar
eekee
Member
Member
Posts: 891
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

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

Post by eekee »

It's been 18 years since I did LFS, I forgot about that symlink.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
User avatar
kotovalexarian
Member
Member
Posts: 38
Joined: Tue Nov 24, 2020 10:17 am
Contact:

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

Post 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.
Post Reply