This section explains some of the rationale and technical details behind the overall build method. Don't try to immediately understand everything in this section. Most of this information will be clearer after performing an actual build. Come back and re-read this chapter at any time during the build process.
The overall goal of Chapter 5 and Chapter 6 is to produce a temporary area containing a set of tools that are known to be good, and that are isolated from the host system. By using the chroot command, the compilations in the remaining chapters will be isolated 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.
This build process is based on cross-compilation. Cross-compilation is normally used to build a compiler and its associated toolchain for a machine different from the one that is used for the build. This is not strictly necessary 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 one great advantage: anything that is cross-compiled cannot depend on the host environment.
The LFS book is not (and does not contain) a general tutorial to build a cross- (or native) toolchain. Don't use the commands in the book for a cross-toolchain for some purpose other than building LFS, unless you really understand what you are doing.
It's known installing GCC pass 2 will break the cross-toolchain. We don't consider it a bug because GCC pass 2 is the last package to be cross-compiled in the book, and we won't “fix” it until we really need to cross-compile some package after GCC pass 2 in the future.
Cross-compilation involves some concepts that deserve a section of their own. Although this section may be omitted on a first reading, coming back to it later will help you gain a fuller understanding of the process.
Let us first define some terms used in this context.
is the machine where we build programs. Note that this machine is also referred to as the “host.”
is the machine/system where the built programs will run. Note that this use of “host” is not the same as in other sections.
is only used for compilers. It is the machine the compiler produces code for. It may be different from both the build and the host.
As an example, let us imagine the following scenario (sometimes referred to as “Canadian Cross”). We have a compiler on a slow machine only, let's call it machine A, and the compiler ccA. We also have a fast machine (B), but no compiler for (B), and we want to produce code for a third, slow machine (C). We will build a compiler for machine C in three stages.
| Stage | Build | Host | Target | Action |
|---|---|---|---|---|
| 1 | A | A | B | Build cross-compiler cc1 using ccA on machine A. |
| 2 | A | B | C | Build cross-compiler cc2 using cc1 on machine A. |
| 3 | B | C | C | Build compiler ccC using cc2 on machine B. |
Then, all the programs needed by machine C can be compiled using cc2 on the fast machine B. Note that unless B can run programs produced for C, there is no way to test the newly built programs until machine C itself is running. For example, to run a test suite on ccC, we may want to add a fourth stage:
| Stage | Build | Host | Target | Action |
|---|---|---|---|---|
| 4 | C | C | C | Rebuild and test ccC using ccC on machine C. |
In the example above, only cc1 and cc2 are cross-compilers, that is, they produce code for a machine different from the one they are run on. The other compilers ccA and ccC produce code for the machine they are run on. Such compilers are called native compilers.
All the cross-compiled packages in this book use an autoconf-based building system. The autoconf-based building system accepts system types in the form cpu-vendor-kernel-os, referred to as the system triplet. Since the vendor field is often irrelevant, autoconf lets you omit it.
An astute reader may wonder why a “triplet” refers to a
four component name. The kernel field and the os field began as a
single “system” field. Such a three-field form is
still valid today for some systems, for example, x86_64-unknown-freebsd. But two systems can
share the same kernel and still be too different to use the same
triplet to describe them. For example, Android running on a
mobile phone is completely different from Ubuntu running on an
ARM64 server, even though they are both running on the same type
of CPU (ARM64) and using the same kernel (Linux).
Without an emulation layer, you cannot run an executable for a
server on a mobile phone or vice versa. So the “system” field has been
divided into kernel and os fields, to designate these systems
unambiguously. In our example, the Android system is designated
aarch64-unknown-linux-android, and
the Ubuntu system is designated aarch64-unknown-linux-gnu.
The word “triplet” remains embedded in the lexicon. A
simple way to determine your system triplet is to run the
config.guess script
that comes with the source for many packages. Unpack the binutils
sources, run the script ./config.guess, and note the
output. For example, for a 32-bit Intel processor the output will
be i686-pc-linux-gnu. On a
64-bit system it will be x86_64-pc-linux-gnu. On most Linux
systems the even simpler gcc
-dumpmachine command will give you similar
information.
You should also be aware of the name of the platform's dynamic
linker, often referred to as the dynamic loader (not to be
confused with the standard linker ld that is part of binutils).
The dynamic linker provided by package glibc finds and loads the
shared libraries needed by a program, prepares the program to
run, and then runs it. The name of the dynamic linker for a
32-bit Intel machine is ld-linux.so.2; it's ld-linux-x86-64.so.2 on 64-bit systems. A
sure-fire way to determine the name of the dynamic linker is to
inspect a random binary from the host system by running:
readelf -l <name of binary>
| grep interpreter and noting the output. The
authoritative reference covering all platforms is in a Glibc
wiki page.
There are two key points for a cross-compilation:
When producing and processing the machine code supposed to be executed on “the host,” the cross-toolchain must be used. Note that the native toolchain from “the build” may be still invoked to generate machine code supposed to be executed on “the build.” For exa