Compile Kernel using llvm/clang

Reference

Tutorial

  • Start with an empty dir
1
2
3
git clone https://github.com/ramosian-glider/clang-kernel-build.git
cd clang-kernel-build
export WORLD=`pwd`
  • Install Clang from Chromium:
1
2
3
4
5
6
7
cd $WORLD
# Instruction taken from http://llvm.org/docs/LibFuzzer.html
mkdir TMP_CLANG
cd TMP_CLANG
git clone https://chromium.googlesource.com/chromium/src/tools/clang
cd ..
TMP_CLANG/clang/scripts/update.py

(To update Clang later on, do (cd TMP_CLANG/clang ; git pull) and run update.py again.)

  • Clone the linux source tree
1
2
3
4
cd $WORLD
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable
git reset --hard v4.16

(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
2
3
4
5
6
cd $WORLD
export CLANG_PATH=`pwd`/third_party/llvm-build/Release+Asserts/bin/
cd linux-stable
make CC=$CLANG_PATH/clang defconfig
make CC=$CLANG_PATH/clang kvmconfig
make CC=$CLANG_PATH/clang -j64 2>&1 | tee build.log
  • Set up the VM
1
2
3
4
cd $WORLD
wget https://raw.githubusercontent.com/google/sanitizers/master/address-sanitizer/kernel_buildbot/create_os_image.sh
# create_os_image.sh requires sudo
sh create_os_image.sh
  • Run the VM
1
2
3
4
cd $WORLD
./run_qemu.sh
# in a separate console:
ssh -i ssh/id_rsa -p 10023 root@localhost
  • 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

reference: ref1, ref2, ref3

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’

link

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

link

Undefined reference in amdgpu.ko