Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

35.2. musl-1.2.6

musl C standard library and dynamic linker

Version 1.2.6, release 1
Shelf toolchain — The native compilers, the linker, the C library, and the LLVM family
Phase 20_selfhost (Self-Hosting Bootstrap), 3 of 15 in build order
Licence MIT
Homepage https://musl.libc.org/
Build time 0.2 SBU
Installed size 3.9 MiB in 235 files
Recipe ports/core/toolchain/musl/kpkgbuild

About

musl is KDOS’s C library and its dynamic linker in one file: /usr/lib/libc.so is the library, and /lib/ld-musl-x86_64.so.1, the interpreter every dynamically linked program on the system names, is a link to it. Why KDOS is built on musl rather than glibc, and what that costs, is in musl as the host C library and How KDOS differs.

This port rebuilds, inside the chroot and with the bootstrap’s native compiler, the musl that Part III cross-compiled, and kpkg adopts the files that step installed. Every package of every later phase is linked against this build.

musl’s configure is a short script of its own, not an autoconf one. It reads CC and CFLAGS from the environment and takes its options in its own form.

Sources

Dependencies

Depends on: nothing a recipe names.

Required by (1): musl-ldd.

Patches

build.sh applies these; kpkg applies nothing by itself.

tz-reload.patch makes tzset() follow a replaced zone file. musl keys its loaded zone by the TZ string alone, and that string is the same before and after a zone change: /etc/localtime when TZ is unset, :/etc/localtime under the profile kdos-settingsd writes. Without the patch no running program ever re-reads the link, even when it calls tzset() itself, and the panel clock and every other program started before the change keep the old zone until they restart. With it, a tzset() whose zone came from a path (TZ unset, :/path, an absolute or ./ path) stats that path, following the link, and reloads the zone when the device, inode, size or modification time differs from the file loaded. A zone name or a POSIX rule is never re-read. localtime_r, mktime and strftime take no stat and keep the cached zone until something calls tzset(); Qt calls it before every local-time conversion, so Qt and KDE programs follow within one conversion, and a C program that never calls it keeps its zone as before. This is glibc’s rule for tzset().

A zone re-read under an unchanged TZ leaves the previous zone file mapped: one mapping is kept per zone change, for the life of the program. tm_zone in every struct tm already filled points into that file’s abbreviations, as tzname[] does for a zone file with no POSIX footer, and other threads read both without musl’s lock; Qt reads tm_zone after releasing its own. Unmapping it would turn the next such read into a fault. Only a change of the TZ string itself unmaps the old file, as upstream musl does. A struct tm filled under the old zone afterwards gives "" from strftime’s %Z, which checks tm_zone against the zone now loaded. The patch is to be offered upstream on the musl mailing list.

The cross musl of Part III is built without the patch: this port replaces it before any shipped program is linked.

Differs from the bootstrap build. musl libc builds this source too, in script/phases/10_bootstrap/020_musl_libc.sh, before kpkg exists.

Only in the bootstrap build: --syslibdir=/lib.

Only in this build: --enable-optimize.

Installation

From the repository root, inside the build container, this is the line kdosbuild runs for the port:

script/chroot/exec.sh bash -c 'source script/phases/20_selfhost/phase.env && kpkg install musl'

What kpkg does

kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/toolchain/musl.

1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.

cd /ports/core/toolchain/musl
echo 'd585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a  musl-1.2.6.tar.gz' | sha256sum -c -

2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.

SRC_ROOT=/var/cache/kpkg/work/musl
SRC=$SRC_ROOT/musl-1.2.6
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf musl-1.2.6.tar.gz -C "$SRC" --strip-components=1

Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.

3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:

patch -p1 -i "$PORT_SRC/tz-reload.patch"
./configure \
	--prefix=/usr \
	--enable-optimize
make
make DESTDIR=$PKG install
install -Dm755 "$PORT_SRC/ldconfig" "$PKG/usr/sbin/ldconfig"

The switches, and why each is set:

The common switches of each build system are explained in the Flag glossary.

4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/musl, prunes what the image does not ship, lints the tree and rolls it reproducibly:

tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
    --mtime=@$SOURCE_DATE_EPOCH \
    --use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
    -cf musl-1.2.6-1.tar.xz -C "$PKG" .

5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/musl, and runs the triggers its paths call for.

Other files in the port directory, read by build.sh: ldconfig.

Contents

Installed programs: ldconfig.

Installed libraries: libc.a, libc.so, libcrypt.a, libdl.a, libm.a, libpthread.a, libresolv.a, librt.a, libutil.a, libxnet.a.

Installed directories: /usr/include/arpa, /usr/include/bits, /usr/include/net, /usr/include/netinet, /usr/include/netpacket, /usr/share/licenses/musl.

Notes

ldconfig, which build.sh installs into /usr/sbin from the port directory, is a script that does nothing and exits 0. musl’s loader keeps no cache: it reads its search path from /etc/ld-musl-x86_64.path on every program start (see Other machine files), so there is nothing to rebuild. The script answers the makefiles and install scripts that run ldconfig after installing a library, whatever arguments they pass.

The loader’s path is written into every dynamically linked program as its interpreter, so it can never move. /lib is a link to /usr/lib on KDOS, which is why the package records the loader under ./lib/ and the file lands in /usr/lib.

The tree links with -z pack-relative-relocs, which writes relative relocations as DT_RELR, a form musl loads from release 1.2.4 on (see The release flags). A binary from this tree does not run under an older C library.