首先下载linaro预编译好的arm64工具链,如下:
# mkdir aarch64-toolchain
# cd aarch64-toolchain
# wget https://releases.linaro.org/components/toolchain/binaries/5.3-2016.02/aarch64-linux-gnu/gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu.tar.xz
将工具链的bin目录添加到执行路径中:
# tar xf gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu.tar.xz
# export PATH=$PATH:$PWD/gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu/bin
测试一下是否能找到linaro工具链的命令,如aarch64-linux-gnu-gcc,正确执行说明上一步的路径设置成功,当前gcc的版本为5.3.1:
# aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
Target: aarch64-linux-gnu
Configured with: ...
Thread model: posix
gcc version 5.3.1 20160113 (Linaro GCC 5.3-2016.02)
编译LINUX内核
可以使用linaro提供的内核代码,如下:
# git clone git://git.linaro.org/kernel/linaro-aarch64.git
# cd linaro-aarch64
# git checkout -b linaro-aarch64 origin/linaro-aarch64
另外,就是使用官方版本的内核,此处使用Linux-3.10版本:
# mkdir kernel
# cd kernel
# wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.10.tar.xz
# tar -xf linux-3.10.tar.xz
# cd linux-3.10/
使用内核自带的ARM64默认配置文件,如下:
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig
结果,入到如下的问题:
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- vmlinux
CC kernel/bounds.s
In file included from include/linux/compiler.h:54:0,
from include/uapi/linux/stddef.h:1,
from include/linux/stddef.h:4,
from /home/kai/linux/linux-3.10/include/uapi/linux/posix_types.h:4,
from include/uapi/linux/types.h:13,
from include/linux/types.h:5,
from include/linux/page-flags.h:8,
from kernel/bounds.c:9:
include/linux/compiler-gcc.h:103:30: fatal error: linux/compiler-gcc5.h: No such file or directory
compilation terminated.
查看目录include/linux/下文件,确实没有compiler-gcc5.h。
$ ll include/linux/compiler-gcc*
-rw-rw-r-- 1 root root 635 7月 1 2013 include/linux/compiler-gcc3.h
-rw-rw-r-- 1 root root 2688 7月 1 2013 include/linux/compiler-gcc4.h
-rw-rw-r-- 1 root root 4074 7月 1 2013 include/linux/compiler-gcc.h
查看出问题的文件compile-gcc.h,第103行左右相关代码如下:其中宏定义__GNUC__在编译时为5,对应于文件compiler-gcc5.h,看来内核版本3.10还没有支持版本为5及以上的GCC编译器。将内核版本换为4.10。
#define __gcc_header(x) #x
#define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)
#define gcc_header(x) _gcc_header(x)
#include gcc_header(__GNUC__)
更换内核版本为4.10,编译顺利完成:
# wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.10.tar.xz
# tar -xf linux-4.10.tar.xz
# cd linux-4.10/
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image
scripts/kconfig/conf --silentoldconfig Kconfig
CHK include/config/kernel.release
UPD include/config/kernel.release
WRAP arch/arm64/include/generated/asm/bugs.h
...
...
LD vmlinux.o
MODPOST vmlinux.o
KSYM .tmp_kallsyms1.o
KSYM .tmp_kallsyms2.o
LD vmlinux
SORTEX vmlinux
SYSMAP System.map
OBJCOPY arch/arm64/boot/Image
编译完成。