Details on this package are located in Section 8.31.2, “Contents of GCC.”
The GCC package contains the GNU compiler collection, which includes the C and C++ compilers.
GCC requires the GMP, MPFR and MPC packages. As these packages may not be included in your host distribution, they will be built with GCC. Unpack each package into the GCC source directory and rename the resulting directories so the GCC build procedures will automatically use them:
There are frequent misunderstandings about this chapter. The procedures are the same as every other chapter, as explained earlier (Package build instructions). First, extract the gcc-15.2.0 tarball from the sources directory, and then change to the directory created. Only then should you proceed with the instructions below.
tar -xf ../mpfr-4.2.2.tar.xz mv -v mpfr-4.2.2 mpfr tar -xf ../gmp-6.3.0.tar.xz mv -v gmp-6.3.0 gmp tar -xf ../mpc-1.3.1.tar.gz mv -v mpc-1.3.1 mpc
Change the default directory name for libraries:
sed -e '/m64=/s/lib64/lib/' \
-e '/m32=/s/m32=.*/m32=..\/lib32$(call if_multiarch,:i386-linux-gnu)/' \
-i.orig gcc/config/i386/t-linux64
This example demonstrates the use of the -i.orig switch. It makes the
sed copy the
t-linux64 file to t-linux64.orig, and then edit the original
t-linux64 file inplace. So you may
run diff -u
gcc/config/i386/t-linux64{.orig,} to visualize
the change done by the sed command afterwards. We'll
simply use -i (which just
edits the original file inplace without copying it) for all other
packages in the book, but you can change it to -i.orig in any case you want to
keep a copy of the original file.
Make -mstackrealign a default for
32-bit objects:
sed '/STACK_REALIGN_DEFAULT/s/0/(!TARGET_64BIT \&\& TARGET_SSE)/' \
-i gcc/config/i386/i386.h
The -mstackrealign has a special
purpose for 32-bit. All applications have a stack frame
alignment. Typically in old applications (and also some
applications in today's age) that are built for 32-bit, typically
for Windows, have a stack frame alignment of 4 bytes. The x86
System V ABI Processor Supplement demands that both 32-bit and
64-bit applications have a stack frame alignment of 16 bytes.
The stack frame alignment is necessary for the x86 assembly
movaps and movdqa instructions to work, which are used as
faster alternatives to movups. When
the stack frame alignment is not correct, those assembly
instructions will trigger a General Protection Error, leading to
a SIGSEGV kill signal.
In order to prevent this, -mstackrealign offers different potential stack
frame alignments if necessary. In order to ensure that every
piece of software has a good stack frame alignment, every package
is compiled with -mstackrealign,
which the above makes it the default for targeting 32-bit. Glibc
is the main one that needs it.
The GCC documentation recommends building GCC in a dedicated build directory:
mkdir -v build cd build
Prepare GCC for compilation:
../configure \
--target=$LFS_TGT \
--prefix=$LFS/tools \
--with-glibc-version=2.43 \
--with-sysroot=$LFS \
--with-newlib \
--without-headers \
--enable-default-pie \
--enable-default-ssp \
--enable-initfini-array \
--disable-nls \
--disable-shared \
--enable-multilib \
--with-multilib-list=m64,m32 \
--disable-decimal-float \
--disable-threads \
--disable-libatomic \
--disable-libgomp \
--disable-libquadmath \
--disable-libssp \
--disable-libvtv \
--disable-libstdcxx \
--enable-languages=c,c++
The meaning of the configure options:
--with-glibc-version=2.43
This option specifies the version of Glibc which will be used on the target. It is not relevant to the libc of the host distro because everything compiled by pass1 GCC will run in the chroot environment, which is isolated from libc of the host distro.
--with-newlib
Since a working C library is not yet available, this ensures that the inhibit_libc constant is defined when building libgcc. This prevents the compiling of any code that requires libc support.
--without-headers
When creating a complete cross-compiler, GCC requires standard headers compatible with the target system. For our purposes these headers will not be needed. This switch prevents GCC from looking for them.
--enable-default-pie and
--enable-default-ssp
Those switches allow GCC to compile programs with some hardening security features (more information on those in the note on PIE and SSP in chapter 8) by default. They are not strictly needed at this stage, since the compiler will only produce temporary executables. But it is cleaner to have the temporary packages be as close as possible to the final ones.
--disable-shared
This switch forces GCC to link its internal libraries statically. We need this because the shared libraries require Glibc, which is not yet installed on the target system.
--enable-multilib
--with-multilib-list=...
These parameters both enable multilib and which architectures to support.
--disable-threads, --disable-libatomic,
--disable-libgomp, --disable-libquadmath, --disable-libssp,
--disable-libvtv, --disable-libstdcxx
These switches disable support for threading, libatomic, libgomp, libquadmath, libssp, libvtv, and the C++ standard library respectively. These features may fail to compile when building a cross-compiler and are not necessary for the task of cross-compiling the temporary libc.
--enable-languages=c,c++
This option ensures that only the C and C++ compilers are built. These are the only languages needed now.
Compile GCC by running:
make
Install the package:
make install
This build of GCC has installed a couple of internal system
headers. Normally one of them, limits.h, would in turn include the corresponding
system limits.h header, in this case,
$LFS/usr/include/limits.h. However,
at the time of this build of GCC $LFS/usr/include/limits.h does not exist, so the
internal header that has just been installed is a partial,
self-contained file and does not include the extended features of
the system header. This is adequate for building Glibc, but the
full internal header will be needed later. Create a full version of
the internal header using a command that is identical to what the
GCC build system does in normal circumstances:
The command below shows an example of nested command substitution
using two methods: backquotes and a $() construct. It could be rewritten using the
same method for both substitutions, but is shown this way to
demonstrate how they can be mixed. Generally the $() method is preferred.
cd .. cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \ `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include/limits.h
Details on this package are located in Section 8.31.2, “Contents of GCC.”