交叉編譯用於生成aarch64指令的GCC (9.2)

參考

Building GCC as a cross compiler for Raspberry Pilinux

How to Build a GCC Cross-Compilerandroid

 

環境

PC: ubuntu18.04 64位 (Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz)c++

GCC:gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)git

 

正文

下面是咱們交叉編譯GCC用到的三個軟件包:binutils,glibc以及gcc,下載連接以下:github

binutils:https://mirrors.nju.edu.cn/gnu/binutils/我下載的是binutils-2.32.tar.xzbootstrap

glibc:https://ftp.gnu.org/gnu/glibc/,我下載的是glibc-2.30.tar.xzubuntu

gcc:https://github.com/gcc-mirror/gcc,我下載的是:gcc-9_2_0-release (切到tag標籤,選擇對應的tag)app

 

一、安裝編譯必備的軟件包

sudo apt update
sudo apt upgrade
sudo apt install build-essential gawk git texinfo bison flex

 

二、新建路徑,下面的全部操做都須要在一個終端裏完成,以避免環境變量發生變化

mkdir -p /home/pengdl/work/GNU/mk_cross_compiler/
cd /home/pengdl/work/GNU/mk_cross_compiler

 

三、下載軟件包並解壓

$ pwd
/home/pengdl/work/GNU/mk_cross_compiler
$ mkdir code
$ cd code
$ tar -xf ./binutils-2.32.tar.xz-
$ tar -xf ./glibc-2.30.tar.xz-
$ tar -xf ./gcc-9_2_0-release.tar.gz

 

4 、進入剛解壓的GCC源碼目錄,下載依賴的軟件包

cd gcc-9_2_0/
./contrib/download_prerequisites

 

五、建立存放交叉編譯可執行程序的路徑,並加入PATH中

cd /home/pengdl/work/GNU/mk_cross_compiler
mkdir cross-gcc
export PATH=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/bin:$PATH

 

六、安裝內核頭文件,這樣未來就能夠用本身的交叉編譯工具編譯調用對應kernel的系統調用的應用程序

cd /home/pengdl/aarch64/android_kernel/linux-4.14
make ARCH=arm64 INSTALL_HDR_PATH=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu headers_install

 

七、編譯安裝binutils

cd /home/pengdl/work/GNU/mk_cross_compiler
mkdir build-binutils
cd build-binutils/
../code/binutils-2.32/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --disable-multilib --with-arch=armv8
make -j8
make install

 

下面開始編譯glibc和gcc,因爲這兩個相互依賴,因此咱們先部分編譯gcc,而後部分編譯glibc,最後所有編譯gcc和glibc。工具

八、部分編譯gcc

cd /home/pengdl/work/GNU/mk_cross_compiler
mkdir build-gcc
cd build-gcc
../code/gcc-9_2_0/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --enable-languages=c,c++ --disable-multilib
make all-gcc -j8
make install-gcc

 

九、部分編譯glibc

cd /home/pengdl/work/GNU/mk_cross_compiler
mkdir build-glibc
cd build-glibc/
../code/glibc-2.30/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu --build=$MACHINE --host=aarch64-linux-gnu --target=aarch64-linux-gnu --with-headers=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/include --disable-multilib libc_cv_forced_unwind=yes

make install-bootstrap-headers=yes install-headers
make -j8 csu/subdir_lib
install csu/crt1.o csu/crti.o csu/crtn.o  /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/lib
aarch64-linux-gnu-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/lib/libc.so
touch /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/include/gnu/stubs.h

 

十、修改GCC 9.2的源碼,不然編譯會報錯

修改/home/pengdl/work/GNU/mk_cross_compiler/code/gcc-9_2_0/libsanitizer/asan/asan_linux.cc,在第77行加入以下代碼:測試

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

 

十一、接着部分編譯gcc

cd /home/pengdl/work/GNU/mk_cross_compiler/build-gcc
make -j8 all-target-libgcc
make install-target-libgcc

 

十二、全編譯glibc

cd /home/pengdl/work/GNU/mk_cross_compiler/build-glibc
make -j8
make install

 

1三、全編譯gcc

cd /home/pengdl/work/GNU/mk_cross_compiler/build-gcc
make -j8
make install

 

1四、測試

首先查看一下咱們本身編譯生成的gcc版本:

$ aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/libexec/gcc/aarch64-linux-gnu/9.2.0/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../code/gcc-9_2_0/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 9.2.0 (GCC) 

 

編寫一個hello_world.c:

$ cat hello_world.c 
#include <stdio.h>

int main(int argc, const char *argv[])
{
        printf("Hello World\n");
        return 0;
}

 

編譯測試:

$ aarch64-linux-gnu-gcc --static hello_world.c -o hello_world
$ qemu-aarch64 ./hello_world
Hello World

 

完。

相關文章
相關標籤/搜索