5.5. Musl-1.2.5

The Musl Package contains an alternative main C library. This library provides the basic routines for allocating memory, searching directories, opening and closing files, reading and writing files, string handling, pattern matching, arithmetic, and so on.

Approximate build time: 1.4 SBU
Required disk space: 870 MB

5.5.1. Installation of Musl

First, create a symbolic link for LSB compliance. Additionally, for x86_64, create a compatibility symbolic link required for proper operation of the dynamic library loader.

case $(uname -m) in
    i?86)   ln -sfv ld-musl.so.1 $LFS/lib/ld-lsb.so.3
    ;;
    x86_64) ln -sfv ../lib/ld-musl-x86_64.so.1 $LFS/lib64
            ln -sfv ../lib/ld-musl-x86_64.so.1 $LFS/lib64/ld-lsb-x86-64.so.3
    ;;
esac
[Note]

Note

The above command is correct. The ln command has several syntactic versions, so be sure to check info coreutils ln and ln(1) before reporting what may appear to be an error.

The malloc implementation used by Musl is slow. Apply the following patch to use rpmalloc as a faster alternative:

patch -Np1 -i ../musl-1.2.5-rpmalloc.patch

Patch some vulnerabilities:

patch -Np1 -i ../musl-1.2.5-iconv-fix.patch

Prepare Musl for compilation:

./configure --prefix=/usr \
    --host=$LFS_TGT             \
    --build=x86_64-pc-linux-gnu \
    --with-malloc=rpmalloc

The meaning of the configure options:

--host=$LFS_TGT, --build=x86_64-pc-linux-gnu

The combined effect of these switches is that Musl's build system configures itself to be cross-compiled, using the cross-linker and cross-compiler in $LFS/tools.

--with-malloc=rpmalloc

This makes Musl use the just-patched rpmalloc implementation

Compile the package:

make

Install the package:

[Warning]

Warning

If LFS is not properly set, and despite the recommendations, you are building as root, the next command will install the newly built Musl to your host system, which will almost certainly render it unusable. So double-check that the environment is correctly set, and that you are not root, before running the following command.

make DESTDIR=$LFS install

The meaning of the make install option:

DESTDIR=$LFS

The DESTDIR make variable is used by almost all packages to define the location where the package should be installed. If it is not set, it defaults to the root (/) directory. Here we specify that the package is installed in $LFS, which will become the root directory in Section 7.4 - Entering the Chroot Environment.

Now that our cross toolchain is in place, it is important to ensure that compiling and linking will work as expected. We do this by performing some sanity checks:

echo 'int main(){}' | $LFS_TGT-gcc -xc -
readelf -l a.out | grep ld-musl

There should be no errors, and the output of the last command will be (allowing for platform-specific differences in the dynamic linker name):

[Requesting program interpreter: /lib/ld-musl-x86-64.so.1]

Note that for 32-bit machines, the interpreter name will be /lib/ld-musl.so.1. If the output is not as shown above, or there is no output at all, then something is wrong. Investigate and retrace the steps to find out where the problem is and correct it. This issue must be resolved before continuing.

rm -v a.out
[Note]

Note

Building the packages in the next chapter will serve as an additional check that the toolchain has been built properly. If some package, especially Binutils-pass2 or GCC-pass2, fails to build, it is an indication that something has gone wrong with the preceding Binutils, GCC, or Musl installations.