Building Binutils & GCC in the Cygwin VM

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

    #!/bin/bash
    
    export TARGET=i486-linux-gnu
    export PREFIX=/opt/crosstool/gcc-4.4.1-glibc-2.10.1/$TARGET
    
    cd /opt/crosstool/src/build-binutils
    ../binutils-2.20.1/configure --target=$TARGET \
        --prefix=$PREFIX --with-sysroot=$TARGET \
        --disable-nls
    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
    
    cd /opt/crosstool/src/build-gcc
    export PATH=$PATH:$PREFIX/bin
    ../gcc-4.4.1/configure --target=$TARGET --prefix=$PREFIX \
        --with-arch=i486 --disable-nls --enable-languages=c,c++ \
        --without-headers --disable-shared --disable-threads \
        --disable-multilib --disable-decimal-float \
        --disable-libmudflap --disable-libssp \
        --disable-libgomp --without-ppl --without-cloog \
        --with-gmp=/usr/local --with-mpfr=/usr/local \
        --with-mpc=/usr/local
    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
    cd $PREFIX/lib/gcc/$TARGET/4.4.1
    ln -s libgcc.a libgcc_eh.a
    
    echo "=== build script: OK ==="
    

  2. You may need to reapply dos2unix to build_gcc_cross.sh if your text editor does not preserve DOS/Unix line endings.
  3. Run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  4. If there are no errors, shudown 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.
  • If you don't make libgcc_eh.a symlink, you're likely to get the following error when building glibc:
    ld: cannot find -lgcc_eh
  • "--with-mpc=/usr/local" should be omitted if you are using older GCC version which does not depend on mpc library.
  • "--with=i486" option is required to implicitly add "-march=i486" 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.11.1/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.11.1/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.11.1/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.11.1/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.11.1/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.11.1/nscd/nscd-client.h:320: undefined reference to `__sync_fetch_and_add_4'
    /sources/glibc-build/libc_pic.os:/sources/glibc-2.11.1/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.11.1/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.11.1/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=i486-linux-gnu with TARGET=x86_64-linux-gnu and remove "--with=i486" in build_gcc_cross.sh
  2. You may need to reapply dos2unix to build_gcc_cross.sh if your text editor does not preserve DOS/Unix line endings.
  3. Run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  4. If there are no errors, shudown the Cygwin VM and make snapshot "SNAP-5".


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