The overall goal of Chapter 5 and Chapter 6 is to produce a temporary area that contains a known-good set of tools that can be isolated from the host system. By using chroot, the commands in the remaining chapters will be contained within that environment, ensuring a clean, trouble-free build of the target LFS system. The build process has been designed to minimize the risks for new readers and to provide the most educational value at the same time.
The build process is based on the process of cross-compilation. Cross-compilation is normally used for building a compiler and its toolchain for a machine different from the one that is used for the build. This is not strictly needed for LFS, since the machine where the new system will run is the same as the one used for the build. But cross-compilation has the great advantage that anything that is cross-compiled cannot depend on the host environment.
During the process of building Linux From Scratch, we also need the platform's dynamic linker (or dynamic loader).
The dynamic linker provided by Glibc finds and loads the shared libraries needed by a program, prepares the program to run, and then runs it.
To determine the linker name, I've inspected bash binary with readelf:
$readelf-l/bin/bash|grepinterpreter[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
The cross-compiler will be installed in a separate $LFS/tools directory, since it will not be part of the final system.
Compiling the cross-compiler (Cross Compilation Toolchain, Cross Toolchain)
The programs compiled in this chapter will be installed under the $LFS/tools directory to keep them separate from the files installed in the following chapters. The libraries, on the other hand, are installed into their final place, since they pertain to the system we want to build.
The Binutils package contains a linker, an assembler, and other tools for handling object files. It's important to install it first since both Glibc and GCC perform various tests on the available linker and assembler to determine which of their own features to enable.
Here, btw, I am checking how long the compilation of binutils took, to use it as a reference value for ... with time command.
I've disabed features I won't need to cross-compile libc. The --without-headers option is interesting, as when building a real cross-compiler, I'd need the headers compatible with the target architecture. LFS works on the same machine, so I don't need them.
Ulltimately I've had to create a full version of the internal header (as what was compiled until now was partial):
This specifies the installation directory for include files. Because libstdc++ is the standard C++ library for LFS, this directory should match the location where the C++ compiler ($LFS_TGT-g++) would search for the standard C+ + include files. In a normal build, this information is automatically passed to the libstdc++ configure options from the top level directory. In our case, this information must be explicitly given. The C++ compiler will prepend the sysroot path $LFS (specified building GCC pass 1) to the include file search path, so it will actually search in $LFS/ tools/$LFS_TGT/include/c++/12.2.0. The combination of the DESTDIR variable (in the make install command below) and this switch ensures to install the headers there.
That concludes building the Cross Compilation Toolchain