Building the x86_64 cross-compiling toolchain

This is based on a post by Andrew Walrond to the x86-64.org discusstion mailing list

LFS 5.0 uses a toolchain based binutils-2.14, gcc-3.3.1, Linux 2.4.22, and glibc-2.3.2. However, because of difficulties in cross-compiling those versions of gcc and glibc, gcc-3.2.3 and glibc-2.3.2 are used instead. Also, Linux 2.4.23 is used in place of 2.4.22.

The steps below will build a toolchain for x86-64 without multilib support; that is, without the ability to build ia32 binaries. It will be installed at /opt/x86-64. You will need the glibc-2.3.2-sscanf-1.patch from LFS. This patch can be applied, and is needed, against glibc-2.3.1.

export CFLAGS=''
export CXXFLAGS=''
export _POSIX2_VERSION='199209'
export PATH=$PATH:/opt/x86-64/bin

tar xjvf binutils-2.14.tar.bz2
mkdir binutils-build
cd binutils-build
../binutils-2.14/configure --prefix=/opt/x86-64 --target=x86_64-unknown-linux 
make -j4
make install
cd ..
rm -rf binutils-2.14/ binutils-build/

tar xjvf gcc-3.2.3.tar.bz2
mkdir gcc-build
cd gcc-build
export CFLAGS='-Dinhibit_libc'
../gcc-3.2.3/configure --prefix=/opt/x86-64 --target=x86_64-unknown-linux --enable-languages=c --disable-shared --disable-multilib --enable-threads=single
make -j4
make install
cd ..
rm -rf gcc-build/ gcc-3.2.3/

tar xjvf linux-2.4.23.tar.bz2
cd linux-2.4.23
make mrproper
yes "" | make config
make include/linux/version.h
mkdir -p /opt/x86-64/x86_64-unknown-linux/include
cp -r include/linux/ include/asm-x86_64/ /opt/x86-64/x86_64-unknown-linux/include 
ln -s asm-x86_64 /opt/x86-64/x86_64-unknown-linux/include/asm
cd ..
rm -rf linux-2.4.23
unset CFLAGS

tar xjvf glibc-2.3.1.tar.bz2
cd glibc-2.3.1
tar xjvf glibc-linuxthreads-2.3.1.tar.bz2
patch -Np1 -i ../glibc-2.3.2-sscanf-1.patch 
mkdir ../glibc-build
cd ../glibc-build
echo 'BUILD_CC=gcc' > configparms
echo 'CC=x86_64-unknown-linux-gcc' >> configparms 
../glibc-2.3.1/configure --prefix=/opt/x86-64 \
    --with-headers=/opt/x86-64/x86_64-unknown-linux/include \
    --without-cvs --enable-kernel=2.4 --enable-add-ons \
    --disable-profile \
    --build=i686-pc-linux-gnu --host=x86_64-unknown-linux
make -j4
make install
cd ..
rm -rf glibc-2.3.1/ glibc-build/

Finally, to be able to run x86-64 binaries, the loader is expected to be in /lib64, so create this softlink:

ln -s /opt/x86-64/lib/ /lib64

Using the cross-compiler may also require that all the libs appear in /opt/x86-64/x86_64-unknown-linux/lib. As it is, some are in that directory and others are in /opt/x86-64/lib. Softlink the ones from /opt/x86-64/lib to /opt/x86-64/x86_64-unknown-linux/lib:


(cd /opt/x86-64/x86_64-unknown-linux/lib && ln -s ../../lib/* .)

Next: building an x86-64 kernel.