Building GLibc in the Cygwin VM

  1. Add the following lines to C:\cygwin\usr\include\cygwin\stat.h (before the last #endif):
    #ifndef stat64
    #define stat64 stat
    #endif
  2. 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
    export PATH=$PATH:$PREFIX/bin
    
    cd /opt/crosstool/src/build-glibc
    find . -delete
    ../glibc-2.19/configure --prefix=/usr \
        --with-headers=$PREFIX/usr/include \
        --enable-add-ons=nptl --enable-kernel=3.0.0 \
        --with-binutils=$PREFIX --build=i686-pc-cygwin \
        --host=$TARGET --enable-shared --disable-profile \
        --with-tls --with-__thread --without-gd --without-cvs \
        libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes libc_cv_ssp=no
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to configure glibc ==="
        exit 1
    fi
    make
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make glibc ==="
        exit 1
    fi
    make install_root=$PREFIX install
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install glibc ==="
        exit 1
    fi
    
    echo "=== build script: OK ==="
  3. Run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  4. If there are no errors, shudown Cygwin VM and make snapshot "SNAP-6".
  5. Replace TARGET=i586-linux-gnu with TARGET=x86_64-linux-gnu, then run C:\Cygwin\opt\crosstool\src\build_gcc_cross.cmd and wait until it completes.
  6. If there are no errors, shudown Cygwin VM and make snapshot "SNAP-7".
  7. Undo stat64 preprocessor hack in C:\cygwin\usr\include\cygwin\stat.h.

Notes:

  • Glibc build will take a lot of time, 4-5 times more than binutils + gcc.
  • libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes options are here to disable linker tests. Because we run glibc build in the cross-environment, these tests create binary files which can't be executed and so fail the test.
  • nptl is a modern library to support POSIX threads in Linux; and it obsoletes linuxthreads, so they should be disabled.
  • "--enable-kernel" sets minimum version of Linux kernel for this glibc to run on. 3.0.0 should be good enough these days.
  • "--prefix=/usr" is not dangerous and will not hurt your /usr in host system, unless you call "make install" in plain and wrong way. This is an old glibc joke.
  • If glibc build for x86 drops out with complaints about missing __sync_bool_compare_and_swap_4 then you have probably misconfigured gcc on the previous step (forgot about "--with-arch=i586"). Try again!
  • If you want to build glibc for i686-linux-gnu or i686-pc-linux-gnu target, then you're likely to get the following error messages:
    ../sysdeps/i386/fpu/s_frexp.S: Assembler messages:
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: invalid identifier for ".ifdef"
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk at end of line, first unrecognized character is `1'
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk at end of line, first unrecognized character is `1'
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk at end of line, first unrecognized character is `1'
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: unrecognized symbol type ""
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk at end of line, first unrecognized character is `1'
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk at end of line, first unrecognized character is `1'
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: expected comma after name `' in .size directive
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: ".endif" without ".if"
    ../sysdeps/i386/fpu/s_frexp.S:66: Error: junk `.get_pc_thunk.dx' after expression

    In order to fix or prevent these errors, make the following corrections in the build_gcc_cross.sh:
    make asm-CPPFLAGS="-D __i686=__i686"
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make glibc ==="
        exit 1
    fi
    make asm-CPPFLAGS="-D __i686=__i686" install_root=$PREFIX install
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install glibc ==="
        exit 1
    fi
  • Also you may get error message about various missing syscalls (__-prefixed) if you forgot to replace .oS with .oZ in makefiles (Cygwin case-insensitive filesystem).
  • Without libc_cv_ssp=no configure option, you may encounter the following error:
    /opt/crosstool/src/build-glibc/resolv/libresolv_pic.a(gethnamaddr.os): In function `getanswer':
    /opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:180: undefined reference to `__stack_chk_guard'
    /opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:483: undefined reference to `__stack_chk_guard'
    /opt/crosstool/src/build-glibc/resolv/libresolv_pic.a(gethnamaddr.os): In function `__GI_res_gethostbyname2':
    /opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:510: undefined reference to `__stack_chk_guard'
    /opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:636: undefined reference to `__stack_chk_guard'
    /opt/crosstool/src/build-glibc/resolv/libresolv_pic.a(gethnamaddr.os): In function `res_gethostbyaddr':
    /opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:644: undefined reference to `__stack_chk_guard'
    /opt/crosstool/src/build-glibc/resolv/libresolv_pic.a(gethnamaddr.os):/opt/crosstool/src/glibc-2.19/resolv/gethnamaddr.c:783: more undefined references to `__stack_chk_guard' follow
    collect2: error: ld returned 1 exit status
    make[2]: *** [../Makerules:438: /opt/crosstool/src/build-glibc/resolv/libresolv.so] Error 1
    make[2]: Leaving directory '/opt/crosstool/src/glibc-2.19/resolv'
    make[1]: *** [Makefile:213: resolv/others] Error 2
    make[1]: Leaving directory '/opt/crosstool/src/glibc-2.19'
    make: *** [Makefile:9: all] Error 2
  • Without applying glibc--cygwin-sunrpc-hdrs-libs.diff patch, you may encounter the following error:
    /opt/crosstool/src/build-glibc/sunrpc/cross-rpc_main.o:rpc_main.c:(.text+0x1f): undefined reference to `libintl_setlocale'
    collect2: error: ld returned 1 exit status
    make[2]: *** [Makefile:169: /opt/crosstool/src/build-glibc/sunrpc/cross-rpcgen] Error 1
    make[2]: Leaving directory '/opt/crosstool/src/glibc-2.19/sunrpc'
    make[1]: *** [Makefile:213: sunrpc/others] Error 2
    make[1]: Leaving directory '/opt/crosstool/src/glibc-2.19'
    make: *** [Makefile:9: all] Error 2
    === build script: failed to make glibc ===
  • If you didn't add stat64 preprocessor hack to C:\cygwin\usr\include\cygwin\stat.h as explained above, you will get the following error during build:
    rpc_main.c: In function `find_cpp':
    rpc_main.c:329:17: error: storage size of `buf' isn't known
    rpc_main.c: In function `checkfiles':
    rpc_main.c:1117:17: error: storage size of `buf' isn't known
    Makefile:165: recipe for target 
    `/opt/crosstool/src/build-glibc/sunrpc/cross-rpc_main.o' failed
    make[2]: *** [/opt/crosstool/src/build-glibc/sunrpc/cross-rpc_main.o] Error 1
    make[2]: Leaving directory `/opt/crosstool/src/glibc-2.19/sunrpc'
    Makefile:213: recipe for target `sunrpc/others' failed
    make[1]: *** [sunrpc/others] Error 2
    make[1]: Leaving directory `/opt/crosstool/src/glibc-2.19'
    Makefile:9: recipe for target `all' failed
    make: *** [all] Error 2


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