10. musl libc
musl is KDOS’s C library: the standard C functions, the POSIX interfaces, the thread library and the dynamic linker, all in one libc.so. With it in build/fs, the cross compiler can link a complete program for the first time, and every later step depends on that. This page shows the commands of script/phases/10_bootstrap/020_musl_libc.sh, explains its switches, and lists what it installs.
Source: the archive of the musl port
Installs into: build/fs/usr and the dynamic linker’s link in build/fs/lib
Approximate build time: 0.1 SBU
Installation of musl
MUSL_SRC=$(extract_port_source musl)
cd "$MUSL_SRC"
./configure \
CROSS_COMPILE=$KDOS_TARGET- \
--prefix=/usr \
--syslibdir=/lib
make
make DESTDIR=$SYSROOT install
script/phases/10_bootstrap/020_musl_libc.sh, lines 23–31
musl’s configure is its own short script, not autoconf, and takes its options in its own way.
The meaning of the configure options
CROSS_COMPILE=$KDOS_TARGET--
The prefix of the tools to build with. musl runs
x86_64-kdos-linux-musl-gcc,x86_64-kdos-linux-musl-arandx86_64-kdos-linux-musl-ranlib, the cross toolchain of Part II. musl has no--host; this variable is how it is cross-compiled. --prefix=/usr-
Install the library in
/usr/liband its headers in/usr/include. --syslibdir=/lib-
Where the dynamic linker goes. The path,
/lib/ld-musl-x86_64.so.1, is written into every dynamically linked program as its interpreter, so it must never move./libis musl’s default, so this states it. On KDOS/libis a link to/usr/lib(see The file system), and the dynamic linker is a link tolibc.soitself.
The CFLAGS of the environment, -O2 -pipe -std=gnu99 from script/env/host.env, reach musl’s configure, and because they name an optimisation level, musl uses them as they are and compiles none of its components at -O3. They come after musl’s own flags on every compile line, so -std=gnu99 replaces the -std=c99 musl asks for. See below for the port, which differs here.
make DESTDIR=$SYSROOT install puts the files under build/fs while the paths compiled into the library stay /usr and /lib.
Contents of musl
Installed libraries: libc.so, which is also the dynamic linker, and libc.a, in /usr/lib; ld-musl-x86_64.so.1 in /lib, a link to libc.so. musl puts all of its functions in libc, and installs libcrypt.a, libdl.a, libm.a, libpthread.a, libresolv.a, librt.a, libutil.a and libxnet.a as empty archives, so that a link line naming -lm or -lpthread still succeeds.
Installed start files: crt1.o, Scrt1.o, rcrt1.o, crti.o and crtn.o in /usr/lib, the code the linker places around main: crt1.o for a program that is not position-independent, Scrt1.o for a PIE, rcrt1.o for a static PIE.
Installed headers: the C library’s headers in /usr/include, beside the kernel’s.
Built again later
The musl port, in 20_selfhost, rebuilds musl inside the chroot with the bootstrap’s native compiler, and kpkg adopts the files this step installed. It differs in four ways:
- It applies
tz-reload.patch. With it, a program that callstzset()re-reads/etc/localtime, or the fileTZnames, when that file has been replaced since it was loaded, so the program picks up a new zone. A program that only converts times, or whoseTZis a zone name or a POSIX rule, keeps the zone it loaded. This step builds musl without the patch. - It gives no
CROSS_COMPILEand no--syslibdir. It builds natively, and the dynamic linker’s place is the same default. - It passes
--enable-optimize, so musl compiles its internal code,mallocand its string functions at-O3, which the plain configure above does not. - It installs
/usr/sbin/ldconfig, a script that does nothing and exits 0. musl’s dynamic linker keeps no cache to rebuild, and the script answers the makefiles and install scripts that runldconfigafter installing a library.
See also
- Part III: the bootstrap
- Linux API headers: the headers musl is compiled against
- libstdc++: the next library, built on this one
- How KDOS differs: what building on musl means for the system