Compiling GCC 11.5 x64 to AArch64 Cross Toolchain on CentOS 6.10

Prerequisites

Prepare the following source packages

binutils-2.29

gcc-11.5.0

glibc-2.17

linux-3.10.99

First, build a GCC 5.2, x64 is sufficient. C++11 is required, the default GCC in the repository is 4.4.7

The ARM64 version of glibc 2.17 is required

linux-3.10.99 provides header files for glibc 2.17

Set up environment variables

export PKG=/soft/pkg

export SRC=/dd/gcc-srcexport BUILD=/dd/gcc-buildexport TARGET=aarch64-linux-gnuexport PREFIX=/dd/aarch64-linux-gnu-gcc-11.5.0export PATH=${PREFIX}/bin:$PATH

mkdir -p ${SRC}

cd ${SRC}tar -xf ${PKG}/gcc-11.5.0.tar.bz2tar -xf ${PKG}/binutils-2.29.tar.bz2tar -xf ${PKG}/linux-3.10.99.tar.xztar -xf ${PKG}/glibc-2.17.tar.xz

Linux kernel installation=========================================================cd ${SRC}/linux-3.10.99/make ARCH=arm64 INSTALL_HDR_PATH=${PREFIX}/${TARGET} headers_install

Binutils installation=========================================================mkdir -p ${BUILD}/binutils-2.29/cd ${BUILD}/binutils-2.29/${SRC}/binutils-2.29/configure –prefix=${PREFIX} –target=${TARGET} –disable-multilibmake -j8make install

First GCC compiler build=========================================================

cd ${SRC}/gcc-11.5.0./contrib/download_prerequisites

The script automatically installs several dependency libraries: mpfr, gmp, mpc, isl

mkdir -p ${BUILD}/gcc-11.5.0/cd ${BUILD}/gcc-11.5.0/${SRC}/gcc-11.5.0/configure –prefix=${PREFIX} –target=${TARGET} –disable-multilib –disable-bootstrap –enable-languages=c –disable-werror –disable-nls –without-headers –disable-threads –disable-shared –disable-libsanitizer –disable-libssp –disable-libquadmath –disable-libgomp –disable-libvtv –disable-checking

make all-gcc -j8

make install-gcc

make all-target-libgcc -j8

make install-target-libgcc

Building glibc completely: Many other articles online build glibc twice, which is actually unnecessary. If you disable most non-core libraries as I did above, you can build the GCC compiler and the libgcc required by glibc=========================================================mkdir -p ${BUILD}/glibc-2.17/cd ${BUILD}/glibc-2.17/${SRC}/glibc-2.17/configure –prefix=${PREFIX}/${TARGET} –build=x86_64-linux-gnu –host=${TARGET} –target=${TARGET} –with-headers=${PREFIX}/${TARGET}/include –disable-multilib libc_cv_forced_unwind=yes

make -j8make install

The above configure may report an error because it is too old to recognize GCC 11.5. You need to modify the configure file to check the cc version code, adding |11.* in the case statement.

Second full GCC build method 1=========================================================cd ${BUILD}/gcc-11.5.0/

rm -rf *

${SRC}/gcc-11.5.0/configure –prefix=${PREFIX} –target=${TARGET} –disable-multilib –disable-bootstrap –enable-languages=c,c++ –disable-werror –disable-nls

make -j8make install

The GCC compilation may also report an error, indicating that the PATH_MAX macro cannot be found. Adding it does not hurt, continue compiling after modification.

Correction: The generated GCC has shortcomings, refer toSelf-built cross-compiler cannot find PATH_MAX – CSDN Blog

Second full GCC build method 2=========================================================cd ${BUILD}/gcc-11.5.0/

rm -rf *

${SRC}/gcc-11.5.0/configure –prefix=${PREFIX} –target=${TARGET} –disable-multilib –disable-bootstrap –enable-languages=c,c++ –disable-werror –disable-nls –with-native-system-header-dir=/include –with-sysroot=${PREFIX}/${TARGET}

make -j8make install

The above (the value of –with-sysroot) + (the value of –with-native-system-header-dir) combined is the include search directory.

Compilation can succeed, but the content path of the libc.so script needs to be changed. This is a glibc compilation configuration issue; the two libraries in glibc need to change absolute paths to relative paths, otherwise it reports that they cannot be found. After modification, they can be found. This also makes it easier to copy to other machines.

Compiling GCC 11.5 x64 to AArch64 Cross Toolchain on CentOS 6.10EditCompiling GCC 11.5 x64 to AArch64 Cross Toolchain on CentOS 6.10Edit–with-native-system-header-dir must be an absolute path, default is /usr/include. The directory specified by the –with-sysroot parameter is treated as the target root directory, and then –with-native-system-header-dir specifies the directory inside the root directory.

Changing this parameter to –with-sysroot=/ –with-build-sysroot=${PREFIX}/${TARGET} –with-native-system-header-dir=/include can also compile successfully.

–with-build-sysroot is the target root directory during compilation, and is only effective when it is different from –with-sysroot.

Using this method 2 to compile GCC does not have the PATH_MAX issue.

–with-sysroot specifies a new method for the target headers and libraries for cross-compilation.

–with-headers and –with-libs can also solve the PATH_MAX issue, but these two parameters should be deprecated and are no longer encouraged for use.

Leave a Comment