Skip to content

Instantly share code, notes, and snippets.

@chponte
Last active August 3, 2024 00:44
Show Gist options
  • Save chponte/7f27967fc72cf61e8c158923bf126b61 to your computer and use it in GitHub Desktop.
Save chponte/7f27967fc72cf61e8c158923bf126b61 to your computer and use it in GitHub Desktop.
Building a complete GNU toolchain, comprised of binutils + gcc + glibc

Building a complete GNU toolchain

Things required:

Environment variables

These variables control the target architecture, and installation directory.

export TARGET=x86_64-pc-linux-gnu
export PREFIX=$HOME/gcc/x.x.x
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PREFIX/bin:$PATH

Linux headers

cd linux-x.x.x
make ARCH=$(echo $TARGET | cut -d '-' -f 1) INSTALL_HDR_PATH=$TARGET_PREFIX headers_install

Binutils

mkdir binutils-build
cd binutils-build
../binutils-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls -v
make -j$(nproc) all install

GCC (first-stage)

May need the additional configure flag --disable-multilib if there are no 32-bit system libraries installed on the system. Note that --with-newlib does not refer to the newlib C library.

cd gcc-x.x.x
./contrib/download_prerequisites
cd ..
mkdir gcc-build
cd gcc-build
../gcc-x.x.x/configure --target=$TARGET --prefix=/tmp/gcc-first-stage --without-headers --with-newlib -v
make -j$(nproc) all-gcc all-target-libgcc install-gcc install-target-libgcc

glibc

cd glibc-x.x
CC=/tmp/gcc-first-stage/bin/gcc ../glibc-x.x/configure --target=$TARGET --prefix=$PREFIX --with-headers=${TARGET_PREFIX}/include --without-selinux
make -j$(nproc) all install

GCC (final)

May need again --disable-multilib.

cd gcc-build
rm -rf *
../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --enable-languages=c,c++
make -j$(nproc) all install

Using the new toolchain

In order to use the new compiler together with the new glibc, call gcc/g++ with the following linking flags:

-L$PREFIX/lib -L$PREFIX/lib64 -L$TARGET_PREFIX/lib -I$PREFIX/include -I$TARGET_PREFIX/include -Wl,--rpath=$PREFIX/lib -Wl,--rpath=$PREFIX/lib64 -Wl,--rpath=$TARGET_PREFIX/lib -Wl,--dynamic-linker=$PREFIX/lib/ld-linux-x86-64.so.2

References

  1. https://www6.software.ibm.com/developerworks/education/l-cross/l-cross-ltr.pdf
  2. https://stackoverflow.com/questions/62651270/glibc-configure-doesnt-recognize-linux-header-files
  3. https://stackoverflow.com/questions/22707265/how-to-build-libgcc-of-gcc-compiler
  4. https://gcc.gnu.org/install/configure.html
@Stantonparish
Copy link

Hi! thanks for your guide.
I have a problem. I have a structure like this:
.
├── bin
├── build
│   ├── binutils-build
│   ├── gcc-build
│   ├── gdb-build
│   └── glibc-build
└── sources
├── binutils-2.30
├── gcc-8.5.0
└── glibc-2.2.4

From glibc-build i launch:
CC=/tmp/gcc-first-stage/bin/gcc ../../sources/glibc-2.2.4/configure --target=$TARGET --prefix=$PREFIX --with-headers=${TARGET_PREFIX}/include --without-selinux

So when i configure glibc i obtain this error:

checking sysdep dirs... configure: error: The x86_64 is not supported.

@chponte
Copy link
Author

chponte commented Sep 8, 2022

Hi! thanks for your guide. I have a problem. I have a structure like this: . ├── bin ├── build │ ├── binutils-build │ ├── gcc-build │ ├── gdb-build │ └── glibc-build └── sources ├── binutils-2.30 ├── gcc-8.5.0 └── glibc-2.2.4

From glibc-build i launch: CC=/tmp/gcc-first-stage/bin/gcc ../../sources/glibc-2.2.4/configure --target=$TARGET --prefix=$PREFIX --with-headers=${TARGET_PREFIX}/include --without-selinux

So when i configure glibc i obtain this error:

checking sysdep dirs... configure: error: The x86_64 is not supported.

Hi Stantonparish,

Unfortunately, building the gnu toolchain from scratch is complicated, and the procedure sometimes differs from version to version. Since I wrote this gist, I've given up on building it manually and I usually resort to crosstool-ng. Although it's intended for building a cross-compiling toolchain, you can also use it to build a toolchain for the host platform.

Best of luck with it!

@skzzi
Copy link

skzzi commented Jun 21, 2024

Take a look at the Linux From Scratch project. They have a very good system for building a full toolchain from scratch.
https://www.linuxfromscratch.org/lfs/

@v-val
Copy link

v-val commented Jun 23, 2024

Seems this one

make -j$(nproc) all-gcc all-target-libgcc install-gcc install-target-libgcc

needs to be split to 2 sequential makes for gcc and libgcc.
(Had an error with libgcc configure failing to use xgcc which isn't built yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment