Reference
Tutorial
- Start with an empty dir
1 | git clone https://github.com/ramosian-glider/clang-kernel-build.git |
- Install Clang from Chromium:
1 | cd $WORLD |
(To update Clang later on, do (cd TMP_CLANG/clang ; git pull) and run update.py again.)
- Clone the linux source tree
1 | cd $WORLD |
(Note that kernel version v4.16 or older is fine, otherwise the latest clang lacks asm-goto support(llvm already support))ref1 and ref2。
- Configure and build the kernel
1 | cd $WORLD |
- Set up the VM
1 | cd $WORLD |
- Run the VM
1 | cd $WORLD |
Compile with KASAN
Edit .config file and add
1
CONFIG_KASAN=y
Regenerage config file:
1
make oldconfig
Problems
unable reference to bcmp
Solutions: reference
Add ‘-fno-builtin-bcmp’ to CLANG_FLAGS
compiler lacks asm-goto support
While the LLVM has supported asm-goto already, it seems that clang doesn’t support asm-goto.
kernel v4.16及之前版本不会有asm-goto的问题
booting problem when compiling kernel with clang kasan.
Solutions: ref
some configuration may cause clang build error
- Add CONFIG_KALLSYMS_ALL=y cause boot Failed to start raise network interfaces.
- Add CONFIG_KASAN_INLINE=y cause boot Failed to start raise network interfaces.
Add CONFIG_DEBUG_VM, the boot will hang at
1
2
3
4
5
6
7
8
9
10[ 0.000000] Booting paravirtualized kernel on KVM
[ 0.000000] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1910969940391419 ns
[ 0.000000] setup_percpu: NR_CPUS:64 nr_cpumask_bits:64 nr_cpu_ids:4 nr_node_ids:1
[ 0.000000] percpu: Embedded 53 pages/cpu @ (ptrval) s178760 r8192 d30136 u524288
[ 0.000000] KVM setup async PF for cpu 0
[ 0.000000] kvm-stealtime: cpu 0, msr 3641e3c0
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 257895
[ 0.000000] Policy zone: DMA32
[ 0.000000] Kernel command line: console=ttyS0 root=/dev/sda earlyprintk=serial
[ 0.000000] Memory: 825300K/1048052K available (26636K kernel code, 1382K rwdata, 4256K rodata, 1804K init, 21016K bss, 222752K reserved, 0K cma-reserved)
clang error: unkown argument: ‘–mpreferred-stack-boundary=4’
in drivers/gpu/drm/amd/display/dc/cals/Makefile and drivers/gpu/drm/amd/display/dc/dml/Makefile file, they specify CFLAGS with mpreferred-stack-boundary, which is not supported by clang. Clang has the flag -mstack-alignment=4 that equals. So replace them
with -mstack-alignment=4 in these two files.
clang does not support vlais
comment out CONFIG_EXOFS_FS