5.2. Embryo Toolchain

The Embryo toolchain contains the GNU Compiler Collection (GCC), which includes the C and C++ compilers, and Binutils, which includes an assembler and linker.

5.2.1. Installation of the Embryo Toolchain

We will be building Binutils and GCC from a combined source tree using the GCC Top Level Makefile. For more information about the Top Level Makefile build system see: http://gcc.gnu.org/install/ and http://gcc.gnu.org/wiki/Top-Level Bootstrap/.

This system will allow us to perform a 3 stage bootstrap, which will compile Binutils and GCC three times, using the latest build of each package for each new build. The resulting toolchain will be completely free from the host system's toolchain features or limitations, resulting in a sterile place to begin our HLFS system.

Unpack both the binutils-2.17 and gcc-core-4.1.2 source tarballs but do not change directory. Then combine them in the same directory with the following commands:

mv gcc-4.1.2/ embryo-toolchain
mv -v binutils-2.17 embryo-toolchain/
cd embryo-toolchain/
ln -vs binutils-2.17/{bfd,binutils,gas,gprof,ld,opcodes} .

The binutils-2.17 directories are symbolically linked because the Binutils include directory must remain available, and seperate from GCC's include directory, for binutils-2.17 to compile correctly.

Binutils-2.17 and GCC-4.1.2 do not use Posix compliant command syntax with the head and tail commands. Although this is not critical it should be fixed. Do so with the following commands:

cp -vi ltcf-c.sh{,.orig}
sed 's/head -1/head -n 1/g' ltcf-c.sh.orig > ltcf-c.sh
cp -vi ltcf-gcj.sh{,.orig}
sed 's/head -1/head -n 1/g' ltcf-gcj.sh.orig > ltcf-gcj.sh
cp -vi configure{,.orig}
sed 's/tail +16c/tail -c +16/g' configure.orig > configure
cp -vi gas/Makefile.in{,.orig}
sed 's/tail +16c/tail -c +16/g' gas/Makefile.in.orig > gas/Makefile.in
cp -vi gcc/Makefile.in{,.orig}
sed 's/tail +16c/tail -c +16/g' gcc/Makefile.in.orig > gcc/Makefile.in
cp -vi gcc/configure{,.orig}
sed 's/tail -3/tail -n 3/g' gcc/configure.orig > gcc/configure
cp -vi ld/testsuite/ld-bootstrap/bootstrap.exp{,.orig}
sed 's/tail +140/tail -n +140/g' \
    ld/testsuite/ld-bootstrap/bootstrap.exp.orig \
      > ld/testsuite/ld-bootstrap/bootstrap.exp

We want to make this a Position Independent Code (PIC) compiler so that everything we compile with this GCC will be built the same way in the next chapter. For example Glibc's utilities will use different assembly code if the compiler defines PIC. Packages like Glibc and Libibery do not use the “--with-picconfigure switch to add -fPIC to compiler flags. This sed command will hardcode -fPIC into GCC's specs:

cp -vi gcc/config/i386/linux.h{,.orig}
sed 's/^\(#define CC1_SPEC.*\)\("\)$/\1 %{fno-pic|fpic|fPIC:;:-fPIC}\2/' \
    gcc/config/i386/linux.h.orig > gcc/config/i386/linux.h

The GCC documentation recommends building outside of the source directory in a dedicated build/object directory:

mkdir -v ../embryo-build/
cd ../embryo-build/

Prepare the toolchain for compilation:

../embryo-toolchain/configure --prefix=/tools \
    --with-local-prefix=/tools --with-pic --disable-nls \
    --enable-languages=c --enable-checking \
    --enable-werror --enable-bootstrap

The meaning of the configure options:

--with-local-prefix=/tools

The purpose of this switch is to remove /usr/local/include from gcc's include search path. This is not absolutely essential, however, it helps to minimize the influence of the host system.

--with-pic

This switch compiles static and shared libraries from PIC objects, while normally only shared libraries are PIC. This is added so that -fPIC is added in stage 1 before the new PIC compiler is built.

--disable-nls

This disables internationalization as i18n is not needed for the temporary tools.

--enable-languages=c

This option ensures that only the C compiler is built.

--enable-werror

This switch enables the -Werror compiler flag during stage2 and later. The -Werror flag treats warnings as errors and will halt the build process if warnings are generated. This switch helps ensure our toolchain is built with compliant code.

--enable-checking

With this switch the compiler is built to perform internal consistency checks and adds error checking within the compiler.

--enable-bootstrap

This option enables a 3 stage bootstrap.

Compile the toolchain with a 3 stage bootstrap:

make

Install the toolchain:

make install

Create a symlink from gcc to cc. Some packages will need this:

ln -vs gcc /tools/bin/cc
[Important]

Important

Confirm the new compiler is defining PIC:

echo | cc -dM -E - | grep 'PIC'

This should return:

#define __PIC__ 1

Otherwise something went wrong and you might have to start over. Check that /tools is in your PATH, or check the make install output for errors.

Then, prepare the linker for the “Adjusting” phase later on:

ln -vs stage3-bfd/ bfd
ln -vs stage3-libiberty/ libiberty
cp -va stage3-ld/ tools-ld
make -C tools-ld/ clean
make -C tools-ld/ LIB_PATH=/tools/lib CC=/tools/bin/gcc
make -C tools-ld/ CC=/tools/bin/gcc EXEEXT=-new install-exec-local

Details on this package are located in Section 6.13.2, “Contents of Binutils” and Section 6.13.3, “Contents of GCC.”