N1064編譯鏈編譯

哞哞叫的狗發表於2024-08-19

環境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

相關文章