环境ubuntu20.04
Arm官网有源码和编译链。但是没有编译手册。
从安装版本中拿到10.3-2021.07-x86_64-aarch64-none-linux-gnu-manifest.txt
根据里面的记录,去进行配置和编译。
官网下载: https://developer.arm.com/downloads/-/gnu-a
下载文件,存放到download中
$cd toolchain_build;
$mkdir build output src
解压文件
$cd download/
$for f in *.tar*; do tar -xf $f -C ../src; done
$cd ../
这里export TARGET PROJECT SYSROOT
$export TARGET=aarch64-none-linux-gnu
$export PROJECT=$PWD
$export SYSROOT=$PROJECT/output/$TARGET/libc
做准备工作,编译安装gmp mpc mpfr isl 到build/host-libs目录
$chmod u+x build_prerequisites.sh; ./build_prerequisites.sh$cd build/; mkdir build-binutils build_gcc1 build_gcc2 build_gcc3 build_gcc4 build_glibc
编译binutils
$cd build-binutils
$../../src/binutils/configure --enable-64-bit-bfd --enable-targets=arm-none-eabi,aarch64_be-none-linux-gnu,aarch64_be-none-elf,aarch64-none-linux-gnu,aarch64-none-linux-gnu_ilp32,aarch64-none-elf --target=aarch64-none-linux-gnu --with-bugurl="https://bugs.linaro.org/" --enable-gold --enable-initfini-array --enable-plugins --disable-doc --disable-gdb --disable-gdbtk --disable-nls --disable-tui --disable-werror --without-gdb --without-python --without-x --prefix=$PROJECT/output --with-build-sysroot=$SYSROOT --with-sysroot=/aarch64-none-linux-gnu/libc --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)'
$make -j8; make install
编译gcc1
$cd ../build_gcc1
$../../src/gcc/configure --target=aarch64-none-linux-gnu --prefix=$PROJECT/output --with-sysroot=/aarch64-none-linux-gnu/libc --with-build-sysroot=$SYSROOT --without-headers --with-newlib --with-bugurl="https://bugs.linaro.org/" --without-cloog --without-isl --disable-shared --disable-threads --disable-libatomic --disable-libsanitizer --disable-libssp --disable-libgomp --disable-libmudflap --disable-libquadmath --enable-checking=yes -disable-libstdcxx --disable-libvtv --enable-languages=c,c++ --with-gmp=$PROJECT/build/host-libs/ --with-mpfr=$PROJECT/build/host-libs/ --with-mpc=$PROJECT/build/host-libs/ --enable-fix-cortex-a53-843419 --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)'
$make -j8; make install
1.gcc1主要生成aarch64-none-linux-gnu-gcc供后续使用。所以下一步需要export PATH。
2.在官方文档上没有-disable-libstdcxx --disable-libvtv和c++,是因为glibc编译时出错
build_glibc/support/links-dso-program.o: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
经查links-dso-program.cc,是用g++编译成links-dso-program.o,而我们此时--enable-languages=c还没有编译出g++, 所以此处冒险在gcc1中把c++给添加上来。
此时在环境变量中添加上刚刚编译出来的aarch64-none-linux-gnu-gcc的路径,后面的编译中会使用到
$export PATH=$PATH:$PROJECT/output/bin
解压linux,并安装,后续编译会使用
$cd ../../src/linux-4.20.13
$make ARCH=arm64 headers_check
$make ARCH=arm64 headers_install INSTALL_HDR_PATH=$PROJECT/output/aarch64-none-linux-gnu/libc/usr/
第一次glibc编译,为gcc2的编译提供一些头文件和库文件
$cd ../../build/build_glibc/
$../../src/glibc_v2.33/configure --enable-shared --with-tls --disable-profile --disable-omitfp --disable-bounded --disable-sanity-checks --prefix=$SYSROOT/usr --with-headers=$SYSROOT/usr/include --includedir=$SYSROOT/usr/include --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)' --build=x86_64-unknown-linux-gnu --host=aarch64-none-linux-gnu --disable-werror --enable-obsolete-rpc --disable-profile --without-gd --without-cvs --without-selinux
$make install-bootstrap-headers=yes install-headers
$make -j8 csu/subdir_lib;
$install csu/crt1.o csu/crti.o csu/crtn.o $PROJECT/output/$TARGET/lib
$$TARGET-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $PROJECT/output/$TARGET/lib/libc.so
$touch $SYSROOT/usr/include/gnu/stubs.h
报错:
check_fds.c: Assembler messages:
check_fds.c:81: Error: no such instruction: 'brk '
make[2]: *** [../o-iterator.mk:9: /home/test/toolchain_build/build/build_glibc/csu/check_fds.o] Error 1
make[2]: *** Waiting for unfinished jobs....
../sysdeps/unix/sysv/linux/aarch64/__read_tp.S: Assembler messages:
../sysdeps/unix/sysv/linux/aarch64/__read_tp.S:22: Error: no such instruction: 'hint 34'
../sysdeps/unix/sysv/linux/aarch64/__read_tp.S:23: Error: no such instruction: 'mrs x0,tpidr_el0'
../sysdeps/aarch64/nptl/tls.h:95:21: error: ‘__builtin_thread_pointer’ is not supported on this target
95 | ((struct pthread *)__builtin_thread_pointer () - 1)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../csu/libc-start.c:322:30: note: in expansion of macro ‘THREAD_SELF’
322 | struct pthread *self = THREAD_SELF;
解决:在linux安装时执行make ARCH=arm64 headers_check
第二次gcc编译,gcc2
$cd ../build_gcc2
$../../src/gcc/configure --target=aarch64-none-linux-gnu --prefix=$PROJECT/output --with-sysroot=/aarch64-none-linux-gnu/libc --with-build-sysroot=$SYSROOT --with-bugurl="https://bugs.linaro.org/" --enable-shared --disable-libatomic --without-cloog --without-isl --disable-libssp --disable-libgomp --disable-libmudflap --disable-libquadmath --enable-checking=yes --enable-languages=c --with-gmp=$PROJECT/build/host-libs/ --with-mpfr=$PROJECT/build/host-libs/ --with-mpc=$PROJECT/build/host-libs/ --enable-fix-cortex-a53-843419 --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)'
make -j8
make install
注意:必须先执行glibc1并成功,否则会有如下这些错误:
aarch64-none-linux-gnu/libc/usr/include/features.h:497:10: fatal error: gnu/stubs.h: No such file or directory
: cannot find crti.o: No such file or directory
/home/test/toolchain_build/output/aarch64-none-linux-gnu/bin/ld: cannot find -lc
make[2]: *** [Makefile:994: libgcc_s.so] Error 1
make[2]: Leaving directory '/home/test/toolchain_build/build/build_gcc2/aarch64-none-linux-gnu/libgcc'
make[1]: *** [Makefile:11990: all-target-libgcc] Error 2
make[1]: Leaving directory '/home/test/toolchain_build/build/build_gcc2'
第二次glibc的编译,glibc2
$cd ../build_glibc
$make clean
$make -j8; make install
第三次gcc的编译,gcc3
$cd ../build_gcc3
$../../src/gcc/configure --target=aarch64-none-linux-gnu --prefix=$PROJECT/output --with-sysroot=/aarch64-none-linux-gnu/libc --with-build-sysroot=$SYSROOT --with-bugurl="https://bugs.linaro.org/" --enable-gnu-indirect-function --enable-shared --disable-libssp --disable-libmudflap --enable-checking=release --enable-languages=c,c++,fortran --with-gmp=$PROJECT/build/host-libs/ --with-mpfr=$PROJECT/build/host-libs/ --with-mpc=$PROJECT/build/host-libs/ --with-isl=$PROJECT/build/host-libs/ --enable-fix-cortex-a53-843419 --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)'
$cp $SYSROOT/usr/lib/* $PROJECT/output/$TARGET/lib/
$make -j8; make install
报错:cc1: error: no include path in which to search for stdc-predef.h
The directory that should contain system headers does not exist:
解决:这个时候不要怀疑别的,先检查报错的这个路径。
报错:C compiler cannot create executables.
查找对应的config.log。发现如下打印
/../sysdeps/aarch64/start.S:83: undefined reference to '__libc_csu_init', 这个是glibc编出来的库,但是现在找不到。
解决:找不到库,但是实际上已经编译出来了,我们做一下拷贝
cp $SYSROOT/usr/lib* $SYSROOT/../lib/
第四次gcc的编译,gcc4
$cd ../build_gcc4
$../../src/gcc/configure --target=aarch64-none-linux-gnu --prefix=$PROJECT/output --with-sysroot=$SYSROOT --with-bugurl="https://bugs.linaro.org/" --enable-shared --disable-libssp --disable-libmudflap --enable-checking=yes --enable-languages=c,c++,fortran --with-gmp=$PROJECT/build/host-libs/ --with-mpfr=$PROJECT/build/host-libs/ --with-mpc=$PROJECT/build/host-libs/ --with-isl=$PROJECT/build/host-libs/ --enable-fix-cortex-a53-843419 --with-pkgversion='GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)'
$make -j8; make install
参考文档:
https://docs.slackware.com/howtos:hardware:arm:gcc-10.x_aarch64_cross-compiler
https://jasonblog.github.io/note/raspberry_pi/how_to_build_a_gcc_cross-compiler.html
https://blog.csdn.net/birencs/article/details/124615193
https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/
https://github.com/apritzel/cross/blob/master/README.md
https://github.com/novelinux/compiler-gcc/blob/master/MakeCrossGcc.md
https://www.cnblogs.com/summitzhou/p/12503647.html