Skip to content

Building the Linux kernel

David Tarditi edited this page Sep 15, 2018 · 1 revision

Overview

The Linux kernel can be built with clang/LLVM for X64 and ARM64. We can use the Checked C version of clang/LLVM instead, allowing us to try out using Checked C in the kernel. With some patches the Linux kernel can be built using clang/LLVM for ARM32, but it is not booting with those patches.

Repository

For X64 and ARM64, a stable recent version of the Linux kernel can be used. For example, Linux 4.19 RC3 can be used. For ARM32, a fork of 4.19 RC1 with patches applied is available at ttps://github.com/Prabhuk/linux_kernel_clang_build

TODO

Add installation of prerequisites

Instructions to build the kernel for X86_64

Please follow the instructions to build Checked C Clang.

git clone https://github.com/Prabhuk/linux_kernel_clang_build -b x86_64_build
cd linux_kernel_clang_build
make CC="clang -march=x86-64 -Qunused-arguments" HOSTCC="clang"  defconfig
make CC="clang -march=x86-64 -Qunused-arguments" HOSTCC="clang"  -j 4

Boot the built image under QEMU:

sudo qemu-system-x86_64 -enable-kvm -M pc -kernel $KPATH/arch/x86/boot/bzImage -hda $KPATH/qemu-image.img -m 1024 -net none -serial stdio -append "root=/dev/sda single console=ttyS0 hung_task_panic=1 earlyprintk=ttyS0,115200"

Instructions to build ARM

git clone https://github.com/Prabhuk/linux_kernel_clang_build -b x86_64_build
cd linux_kernel_clang_build
make ARCH=arm CC="clang  -target arm-linux-gnueabi -gcc-toolchain --sysroot=/ -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error -Qunused-arguments" HOSTCC=clang CROSS_COMPILE=arm-linux-gnueabi- vexpress_defconfig
make ARCH=arm CC="clang  -target arm-linux-gnueabi -gcc-toolchain --sysroot=/ -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error -Qunused-arguments" HOSTCC=clang CROSS_COMPILE=arm-linux-gnueabi- -j 4

Notes

Observations:

  1. Linux Kernel code (referred as kernel code or kernel going forward), is typically compiled using gcc.
  2. Certain gcc optimizations used by kernel code are available only in gcc and these optimizations are applied through use of compiler flags.
  3. There is no guaranteed binary interoperability for binary code that is generated by different compilers.
  4. However, the libraries that confirm to the platform ABI can interoperate though the libraries themselves are incompatible C libraries.

References:

  1. https://softwareengineering.stackexchange.com/questions/235706/are-c-object-files-created-with-different-compilers-binary-compatible
  2. https://kevinaboos.wordpress.com/2013/10/03/static-analysis-of-linux-kernel-drivers-using-clang/
  3. LLVMLinux patches repo: https://github.com/Fuzion24/LLVM-Linux-Kernel
  4. Google's efforts: https://lkml.org/lkml/2017/8/22/912
  5. https://github.com/nathanchance/android-kernel-clang
  6. https://lwn.net/Articles/734071/
  7. ARM32: http://lists.llvm.org/pipermail/llvm-dev/2018-March/121810.html
  8. ChromiumOS kernel: https://chromium.googlesource.com/chromiumos/third_party/kernel/+/v4.9
  9. https://github.com/dnschneid/crouton/wiki/Build-chrome-os-kernel-and-kernel-modules
  10. Android kernel: https://android.googlesource.com/kernel/common/+log/f0907aa15ed9f9c7541bb244ed3f52c376ced19c
  11. https://clang.llvm.org/docs/CrossCompilation.html

Building for ARM 64 and x86 architectures:

  1. Android linux kernel (tested for 4.9) gets built with aosp's clang and gcc (for linking) tool chains for ARM 64 and x86, x86_64 architectures
  2. https://github.com/nathanchance/android-kernel-clang

Building for x86: For 4.14rc2

  1. Linux kernel mainline v4.14-rc2 gets built with Chromium's clang. https://github.com/ramosian-glider/clang-kernel-build
  2. Cross compiling for other architectures fail

Building for ARM