-
Notifications
You must be signed in to change notification settings - Fork 19
Building the Linux kernel
David Tarditi edited this page Sep 15, 2018
·
1 revision
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.
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
Add installation of prerequisites
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"
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
- Linux Kernel code (referred as kernel code or kernel going forward), is typically compiled using gcc.
- Certain gcc optimizations used by kernel code are available only in gcc and these optimizations are applied through use of compiler flags.
- There is no guaranteed binary interoperability for binary code that is generated by different compilers.
- However, the libraries that confirm to the platform ABI can interoperate though the libraries themselves are incompatible C libraries.
- https://softwareengineering.stackexchange.com/questions/235706/are-c-object-files-created-with-different-compilers-binary-compatible
- https://kevinaboos.wordpress.com/2013/10/03/static-analysis-of-linux-kernel-drivers-using-clang/
- LLVMLinux patches repo: https://github.com/Fuzion24/LLVM-Linux-Kernel
- Google's efforts: https://lkml.org/lkml/2017/8/22/912
- https://github.com/nathanchance/android-kernel-clang
- https://lwn.net/Articles/734071/
- ARM32: http://lists.llvm.org/pipermail/llvm-dev/2018-March/121810.html
- ChromiumOS kernel: https://chromium.googlesource.com/chromiumos/third_party/kernel/+/v4.9
- https://github.com/dnschneid/crouton/wiki/Build-chrome-os-kernel-and-kernel-modules
- Android kernel: https://android.googlesource.com/kernel/common/+log/f0907aa15ed9f9c7541bb244ed3f52c376ced19c
- https://clang.llvm.org/docs/CrossCompilation.html
- 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
- https://github.com/nathanchance/android-kernel-clang
- Linux kernel mainline v4.14-rc2 gets built with Chromium's clang. https://github.com/ramosian-glider/clang-kernel-build
- Cross compiling for other architectures fail
- Need to start taking patches from https://github.com/Fuzion24/ which contains all the patches of linuxLLVM project
- No real support for building ARM32 out there. Except https://www.quora.com/What-is-the-state-of-the-art-of-compiling-the-Linux-Kernel-using-LLVM-Clang
- Doesn't seem to have released kernel sources.