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:
- MIT (musl)
- MIT (mimalloc)
Dependencies:
- make
- clang/llvm toolchain
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"
/sources if you do not plan to rebuild musl again.
Command Explanations
tar -xf musl-1.2.6.tar.gzandcd musl-...: Unpack and enter a clean musl source tree.patch -Np1 -i ../musl-1.2.6-mimalloc.patch: Applies the local musl integration patch using paths relative to the source root.mkdir -p src/malloc/mimalloc/upstream: Creates the destination for upstream mimalloc sources inside musl.tar -xf ../mimalloc-v3.3.0.tar.gz --strip-components=1 ...: Copies only the mimalloc source and include trees needed by the allocator integration.cp -v ... static.c: Places the mimalloc static allocator entry point where the musl patch expects it.patch -Np1 -i ../mimalloc-...andpatch -Np1 -i ../musl-...runtime...: Apply the remaining allocator and compiler-rt runtime patches.lbi_configure --with-malloc=mimalloc: Configures musl with the book's layout and selects mimalloc as the allocator.make $LWI_MAKE_FLAGSandmake install: Build and install the final musl pass.ln -snf ./libc.so ... ld-musl-${LBI_ARCH}.so.1: Provides the musl dynamic-loader soname expected by dynamically linked programs.chmod 755 ... libc.so: Ensures the dynamic loader/library is executable.