5.4. musl libc pass 2 1.2.6

musl pass 2 builds and installs the full target libc with mimalloc integration, using the controlled toolchain from the previous pass.

Input assumption: musl-1.2.6.tar.gz and mimalloc-v3.3.0.tar.gz are already present in $LBI_SOURCES, and the three musl-related patches distributed with the website (musl-1.2.6-mimalloc.patch, mimalloc-3.3.0-for-musl.patch, and musl-1.2.6-runtime-lib-from-compiler.patch) have been placed in $LBI_SOURCES.

musl is a lightweight C standard library implementation for Linux systems. we need it to provide the full target libc in pass 2, with the allocator integration used by this book.

mimalloc is a general-purpose memory allocator library. we need it to provide the upstream allocator sources embedded into musl for this pass.

Extract musl and Apply the First Patch

cd "$LBI_SOURCES"
tar -xf musl-1.2.6.tar.gz
cd musl-1.2.6
patch -Np1 -i ../musl-1.2.6-mimalloc.patch

Add mimalloc Upstream Sources (src and include)

mkdir -p src/malloc/mimalloc/upstream
tar -xf ../mimalloc-v3.3.0.tar.gz \
    --strip-components=1 \
    -C src/malloc/mimalloc/upstream \
    mimalloc-3.3.0/src mimalloc-3.3.0/include

# The second patch updates this file in-place.
cp -v src/malloc/mimalloc/upstream/src/static.c src/malloc/mimalloc/static.c

Apply the Second Patch

patch -Np1 -i ../mimalloc-3.3.0-for-musl.patch

Apply the final patch for compiler-rt builtins support

patch -Np1 -i ../musl-1.2.6-runtime-lib-from-compiler.patch

Configure, Build, and Install musl Pass 2

lbi_configure --target="$LBI_TARGET" --with-malloc=mimalloc
make $LWI_MAKE_FLAGS
make install DESTDIR="$LBI_ROOT"

# musl can install this loader link as an absolute path.
# Rewrite it to a relative link so it resolves correctly from the mounted target tree.
ln -snf ./libc.so \
    "$LBI_ROOT/system/libraries/ld-musl-${LBI_ARCH}.so.1"

ls -lh "$LBI_ROOT/usr/lib/ld-musl-${LBI_ARCH}.so.1"