Building Binutils & GCC in the Cygwin VM

  1. Update C:\Cygwin\opt\crosstool\src\build_gcc_cross.sh :

    #!/bin/bash
    
    export TARGET=i586-linux-gnu
    export PREFIX=/opt/crosstool/gcc-4.9.2-glibc-2.19/$TARGET
    
    cd /opt/crosstool/src/build-binutils
    find . -delete
    ../binutils-2.25/configure --target=$TARGET \
        --prefix=$PREFIX --with-sysroot=$TARGET \
        --disable-nls --with-arch=i586
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to configure binutils ==="
        exit 1
    fi
    make all
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make binutils ==="
        exit 1
    fi
    make install
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install binutils ==="
        exit 1
    fi
    
    
    export PATH=$PATH:$PREFIX/bin
    cd /opt/crosstool/src/build-gcc
    find . -delete
    ../gcc-4.9.2/configure --target=$TARGET --prefix=$PREFIX \
        --with-arch=i586 \
        --disable-sjlj-exceptions --enable-checking=release \
        --enable-linker-build-id --enable-gnu-unique-object \
        --disable-nls --enable-languages=c \
        --without-headers --disable-shared --disable-threads \
        --disable-multilib --disable-decimal-float \
        --disable-libmudflap --disable-libssp \
        --disable-libgomp --without-ppl --without-cloog \
        --with-gmp --with-mpfr --with-mpc
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to configure gcc ==="
        exit 1
    fi
    make all-gcc
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make gcc ==="
        exit 1
    fi
    make install-gcc
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install gcc ==="
        exit 1
    fi
    make all-target-libgcc
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make libgcc ==="
        exit 1
    fi
    make install-target-libgcc
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install libgcc ==="
        exit 1
    fi
    
    echo "=== build script: OK ==="
    

  2. Run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  3. If there are no errors, shutdown the Cygwin VM and make snapshot "SNAP-4".

Notes:

  • If there's complaint about missing file fenv.h, then you forgot to specify "--disable-decimal-float" when configuring gcc.
  • "--without-headers" means freestanding-mode only compiler without libc dependency.
  • Multilib is a special feature of GCC to allow building 32-bit targets on x86_64 platform. We don't use that one; we build two distinct cross-compilers instead.
  • "--with=i586" option is required to implicitly add "-march=i586" argument to the command line of newly built gcc compiler on every invokation, otherwise glibc will fail to build with the following errors:
    /sources/glibc-build/libc_pic.os: In function `__libc_fork':
    /sources/glibc_2.19/posix/../nptl/sysdeps/unix/sysv/linux/i386/../fork.c:79: undefined reference to `__sync_bool_compare_and_swap_4'
    /sources/glibc-build/libc_pic.os: In function `__nscd_drop_map_ref':
    /sources/glibc_2.19/nscd/nscd-client.h:320: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os: In function `nscd_getpw_r':
    /sources/glibc_2.19/nscd/nscd_getpw_r.c:233: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os: In function `__nscd_drop_map_ref':
    /sources/glibc_2.19/nscd/nscd-client.h:320: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os: In function `nscd_getgr_r':
    /sources/glibc_2.19/nscd/nscd_getgr_r.c:322: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os: In function `__nscd_drop_map_ref':
    /sources/glibc_2.19/nscd/nscd-client.h:320: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os:/sources/glibc_2.19/nscd/nscd_gethst_r.c:415: more undefined references to `__sync_fetch_and_add_4' follow
    /sources/glibc-build/libc_pic.os: In function `__nscd_get_map_ref':
    /sources/glibc_2.19/nscd/nscd_helper.c:433: undefined reference to `__sync_val_compare_and_swap_4'
    /sources/glibc-build/libc_pic.os: In function `*__GI___libc_freeres':
    /sources/glibc_2.19/malloc/set-freeres.c:39: undefined reference to `__sync_bool_compare_and_swap_4'

Additional steps to support 64-bit Linux target platform:

  1. Replace TARGET=i586-linux-gnu with TARGET=x86_64-linux-gnu and remove "--with=i586" in build_gcc_cross.sh
  2. Run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  3. If there are no errors, shutdown the Cygwin VM and make snapshot "SNAP-5".


>> Read next section or buy already prepared cross-compiler (€10) to save your time.