selective focus photography of computer code monitor display

Building the kernel with clang: messing around

The Linux kernel is an ingenious piece of software and I’m pretty confident in compiling it by myself since quite a while – so I was trying to step up the game by doing debian packages. But now as the Kernel supports LTO, it’s time for me to give it a spin. But to be honest, useful documentation is sparse – so here are my notes. The kernel config needs to have the following CONFIG-options to work with LTO enabled – or even give you the option

# CONFIG_STACKPROTECTOR is not set
CONFIG_LTO=y
CONFIG_LTO_CLANG=y
CONFIG_ARCH_SUPPORTS_LTO_CLANG=y
CONFIG_ARCH_SUPPORTS_LTO_CLANG_THIN=y
CONFIG_HAS_LTO_CLANG=y
# CONFIG_LTO_NONE is not set
CONFIG_LTO_CLANG_FULL=y
# CONFIG_LTO_CLANG_THIN is not set

Compiling itself is pretty much straight forward as soon as you get the clang-11 binaries (default debian 11) linked to their base name without the version. Update-alternatives takes care of that

update-alternatives \
        --verbose \
        --install /usr/bin/llvm-config       llvm-config      /usr/bin/llvm-config-11 100 \
        --slave   /usr/bin/llvm-ar           llvm-ar          /usr/bin/llvm-ar-11 \
        --slave   /usr/bin/llvm-as           llvm-as          /usr/bin/llvm-as-11 \
        --slave   /usr/bin/llvm-bcanalyzer   llvm-bcanalyzer  /usr/bin/llvm-bcanalyzer-11 \
        --slave   /usr/bin/llvm-cov          llvm-cov         /usr/bin/llvm-cov-11 \
        --slave   /usr/bin/llvm-diff         llvm-diff        /usr/bin/llvm-diff-11 \
        --slave   /usr/bin/llvm-dis          llvm-dis         /usr/bin/llvm-dis-11 \
        --slave   /usr/bin/llvm-dwarfdump    llvm-dwarfdump   /usr/bin/llvm-dwarfdump-11 \
        --slave   /usr/bin/llvm-extract      llvm-extract     /usr/bin/llvm-extract-11 \
        --slave   /usr/bin/llvm-link         llvm-link        /usr/bin/llvm-link-11 \
        --slave   /usr/bin/llvm-mc           llvm-mc          /usr/bin/llvm-mc-11 \
        --slave   /usr/bin/llvm-nm           llvm-nm          /usr/bin/llvm-nm-11 \
        --slave   /usr/bin/llvm-objdump      llvm-objdump     /usr/bin/llvm-objdump-11 \
        --slave   /usr/bin/llvm-ranlib       llvm-ranlib      /usr/bin/llvm-ranlib-11 \
        --slave   /usr/bin/llvm-readobj      llvm-readobj     /usr/bin/llvm-readobj-11 \
        --slave   /usr/bin/llvm-rtdyld       llvm-rtdyld      /usr/bin/llvm-rtdyld-11 \
        --slave   /usr/bin/llvm-size         llvm-size        /usr/bin/llvm-size-11 \
        --slave   /usr/bin/llvm-stress       llvm-stress      /usr/bin/llvm-stress-11 \
        --slave   /usr/bin/llvm-symbolizer   llvm-symbolizer  /usr/bin/llvm-symbolizer-11 \
        --slave   /usr/bin/llvm-tblgen       llvm-tblgen      /usr/bin/llvm-tblgen-11 \
        --slave   /usr/bin/llvm-objcopy      llvm-objcopy     /usr/bin/llvm-objcopy-11 \
        --slave   /usr/bin/llvm-strip        llvm-strip       /usr/bin/llvm-strip-11

update-alternatives \
        --verbose \
        --install /usr/bin/clang                 clang                 /usr/bin/clang-11 100 \
        --slave   /usr/bin/clang++               clang++               /usr/bin/clang++-11  \
        --slave   /usr/bin/asan_symbolize        asan_symbolize        /usr/bin/asan_symbolize-11 \
        --slave   /usr/bin/clang-cpp             clang-cpp             /usr/bin/clang-cpp-11 \
        --slave   /usr/bin/ld.lld                ld.lld                /usr/bin/ld.lld-11

As this is finally taken care of, we can finally try compiling with make LLVM=1 LLVM_IAS=1 CC=clang HOSTCC=clang LD=ld.lld -j5 deb-pkg – but be aware: LTO needs quite some RAM to get its stuff done. About the results and the speed, I still need to do some tests to see how those kernels act – especially if I can

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *