8.3. musl libc final pass 1.2.6

The final musl pass rebuilds and reinstalls libc with mimalloc integration from inside the target environment.

Input assumption: musl-1.2.6.tar.gz, mimalloc-v3.3.0.tar.gz, musl-1.2.6-mimalloc.patch, mimalloc-3.3.0-for-musl.patch, and musl-1.2.6-runtime-lib-from-compiler.patch are already present in /sources.

Source URLs: https://musl.libc.org/releases/musl-1.2.6.tar.gz and https://github.com/microsoft/mimalloc/archive/refs/tags/v3.3.0.tar.gz

Licenses:

Dependencies:

musl is a lightweight C standard library implementation for Linux systems. we need it to provide the final installed libc used by the completed chapter 8 target environment.

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

Extract musl and Apply the First Patch

cd /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 compiler-rt builtins runtime patch

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

Configure, Build, and Install musl (Final Pass)

lbi_configure --with-malloc=mimalloc
make $LWI_MAKE_FLAGS
make install

WARNING: temporary command breakage can happen

WARNING: this step MAY temporarily remove your ability to run commands in chroot on some systems.

Do not panic.

Exit the chroot, ensure LBI_ROOT is set, and as root run:

ln -snf ./libc.so \
    "$LBI_ROOT/system/libraries/ld-musl-${LBI_ARCH}.so.1"
chmod 755 "$LBI_ROOT/system/libraries/libc.so"

Then re-enter chroot and continue.

Quick verification

ls -lh /system/libraries/libc.so \
       "/system/libraries/ld-musl-${LBI_ARCH}.so.1"
After this step is complete, you can remove the extracted source directories and source tarballs from /sources if you do not plan to rebuild musl again.

Command Explanations