交叉编译用于生成aarch64指令的GCC (9.2)

参考

Building GCC as a cross compiler for Raspberry Pi

How to Build a GCC Cross-Compiler

 

环境

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

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

 

正文

下面是我们交叉编译GCC用到的三个软件包:binutils,glibc以及gcc,下载链接如下:

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

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

gcc:https://github.com/gcc-mirror/gcc,我下载的是:gcc-9_2_0-release (切到tag标签,选择对应的tag)

 

1、安装编译必备的软件包

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

 

2、新建路径,下面的所有操作都需要在一个终端里完成,以免环境变量发生变化

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

 

3、下载软件包并解压

$ 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

 

5、创建存放交叉编译可执行程序的路径,并加入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

 

6、安装内核头文件,这样将来就可以用自己的交叉编译工具编译调用对应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

 

7、编译安装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。

8、部分编译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

 

9、部分编译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

 

10、修改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

 

11、接着部分编译gcc

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

 

12、全编译glibc

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

 

13、全编译gcc

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

 

14、测试

首先查看一下我们自己编译生成的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 

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

 

完。

转载于:https://www.cnblogs.com/pengdonglin137/p/11565115.html

你可能感兴趣的:(交叉编译用于生成aarch64指令的GCC (9.2))