I am currently building a cross-compiler targeting the PowerPC architecture. It is capable of building Linux binaries that work and can execute on the target, however I hit a problem when I enable the compiler flag "-D_FORTIFY_SOURCE=2". I build a simple hello world application as so:
#include <limits.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
return 0;
}
I compile this using my cross-compiler and get the following error:
$ powerpc-linux-gnu-gcc -D_FORTIFY_SOURCE=2 -O2 hello.c
In file included from /opt/crossgcc/powerpc-linux-gnu/include/stdlib.h:958:0,
from hello.c:2:
/opt/crossgcc/powerpc-linux-gnu/include/bits/stdlib.h: In function 'wctomb':
/opt/crossgcc/powerpc-linux-gnu/include/bits/stdlib.h:90:3: error: #error "Assumed value of MB_LEN_MAX wrong"
# error "Assumed value of MB_LEN_MAX wrong"
^
I believe this is because I am not bootstrapping my GCC build correctly, but as far as I can see I am building it correctly. The script I am taking to build my compiler are as follows:
#! /bin/bash
set -e
INSTALL_PATH=$PWD/output
TARGET=powerpc-linux-gnu
LINUX_ARCH=powerpc
PARALLEL_MAKE=-j4
BINUTILS_VERSION=binutils-2.32
GCC_VERSION=gcc-8.3.0
LINUX_KERNEL_VERSION=linux-5.1.9
GLIBC_VERSION=glibc-2.29
export PATH=$INSTALL_PATH/bin:$PATH
cd $GCC_VERSION
./contrib/download_prerequisites
cd ..
mkdir -p build-binutils
cd build-binutils
../$BINUTILS_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET
make $PARALLEL_MAKE
make install
cd ..
cd $LINUX_KERNEL_VERSION
make ARCH=$LINUX_ARCH INSTALL_HDR_PATH=$INSTALL_PATH/$TARGET headers_install
cd ..
# Build GCC compiler
mkdir -p build-gcc
cd build-gcc
../$GCC_VERSION/configure \
--prefix=$INSTALL_PATH \
--target=$TARGET \
--disable-silent-rules \
--with-gnu-as --with-gnu-ld \
--enable-languages="c,c++" \
--enable-theads=posix \
--enable-c99 \
--enable-long-long \
--enable-lto \
--enable-libssp \
--enable-secureplt \
--disable-libmudflag \
--enable-secureplt \
--disable-nls \
--with-long-double-128
make $PARALLEL_MAKE all-gcc
make install-gcc
cd ..
mkdir -p build-glibc
cd build-glibc
../$GLIBC_VERSION/configure \
--prefix=$INSTALL_PATH/$TARGET \
--build=$(gcc -dumpmachine) \
--host=$TARGET \
--target=$TARGET \
--with-headers=$INSTALL_PATH/$TARGET/include \
libc_cv_forced_unwind=yes
make install-bootstrap-headers=yes install-headers
make $PARALLEL_MAKE csu/subdir_lib
install csu/crt1.o csu/crti.o csu/crtn.o $INSTALL_PATH/$TARGET/lib
$TARGET-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $INSTALL_PATH/$TARGET/lib/libc.so
touch $INSTALL_PATH/$TARGET/include/gnu/stubs.h
cd ..
cd build-gcc
make $PARALLEL_MAKE all-target-libgcc
make install-target-libgcc
cd ..
cd build-glibc
make $PARALLEL_MAKE
make install
cd ..
cd build-gcc
make $PARALLEL_MAKE all
make install
cd ..
Am I incorrectly bootstrapping my GCC? Should I be doing something differently to make GCC "aware" of my glibc headers?
By default the limits.h
file is not properly configured, so you need to patch it.
To fix the problem you have to go inside GCC's source directory and type the command:
cd $GCC_VERSION
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
`dirname $(${TARGET}-gcc -print-libgcc-file-name)`/include-fixed/limits.h
This adds a #include_next <limits.h>
to the bottom which eventually picks up the right header.