1. 准备交叉工具链
llvm 使用了最新的llvm-17, 编译方法见:编译LoongArch的llvm交叉工具链
gcc 从linux 官方下载:http://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/13.2.0/x86_64-gcc-13.2.0-nolibc-loongarch64-linux.tar.xz
发布llvm和gcc 相应的环境变量
$ llc --version
LLVM (http://llvm.org/):LLVM version 17.0.0rcOptimized build.Default target: x86_64-unknown-linux-gnuHost CPU: haswellRegistered Targets:aarch64 - AArch64 (little endian)aarch64_32 - AArch64 (little endian ILP32)aarch64_be - AArch64 (big endian)amdgcn - AMD GCN GPUsarm - ARMarm64 - ARM64 (little endian)arm64_32 - ARM64 (little endian ILP32)armeb - ARM (big endian)avr - Atmel AVR Microcontrollerbpf - BPF (host endian)bpfeb - BPF (big endian)bpfel - BPF (little endian)hexagon - Hexagonlanai - Lanailoongarch32 - 32-bit LoongArchloongarch64 - 64-bit LoongArchmips - MIPS (32-bit big endian)mips64 - MIPS (64-bit big endian)mips64el - MIPS (64-bit little endian)mipsel - MIPS (32-bit little endian)msp430 - MSP430 [experimental]nvptx - NVIDIA PTX 32-bitnvptx64 - NVIDIA PTX 64-bitppc32 - PowerPC 32ppc32le - PowerPC 32 LEppc64 - PowerPC 64ppc64le - PowerPC 64 LEr600 - AMD GPUs HD2XXX-HD6XXXriscv32 - 32-bit RISC-Vriscv64 - 64-bit RISC-Vsparc - Sparcsparcel - Sparc LEsparcv9 - Sparc V9systemz - SystemZthumb - Thumbthumbeb - Thumb (big endian)ve - VEwasm32 - WebAssembly 32-bitwasm64 - WebAssembly 64-bitx86 - 32-bit X86: Pentium-Pro and abovex86-64 - 64-bit X86: EM64T and AMD64xcore - XCore
2. 下载内核源码
从https://www.kernel.org/ 下载最新的版本(linux-next) ,本次用的是linux 6.6rc1
3. 编译内核
$ cd linux-next
$ export ARCH=loongarch$ make O=./build_llvm LLVM=1 loongson3_defconfig
$ make O=./build_llvm LLVM=1 -j4
make[1]: 进入目录“/home/loongson/work/oh/kernel/linux-next-next-20230906/build_llvm”GEN MakefileCALL ../scripts/checksyscalls.shAS arch/loongarch/kernel/genex.o
<instantiation>:11:9: error: Cannot represent a difference across sections.dword 768b-766b^
<instantiation>:11:9: error: Cannot represent a difference across sections .dword 768b-766b^
<instantiation>:11:9: error: Cannot represent a difference across sections .dword 768b-766b^
<instantiation>:11:9: error: Cannot represent a difference across sections
出现上面的错误是因为不relocalable 特性,修改内核配置 关掉CONFIG_RELOCATABLE
CONFIG_CRASH_DUMP=n
CONFIG_RELOCATABLE=n
$ make O=./build_llvm LLVM=1 -j4../arch/loongarch/include/asm/percpu.h:20:4: error: compiler support for the model attribute is necessary when a recent assembler is used20 | # error compiler support for the model attribute is necessary when a recent assembler is used| ^
出现上面的错误是因为不module 也还不支持,修改内核配置 关掉CONFIG_MODULES
CONFIG_MODULES=n