From 1f6f08c1ad84a3db4267d0339591f4e210d217f6 Mon Sep 17 00:00:00 2001 From: ebasov Date: Mon, 10 Feb 2025 15:29:12 +0300 Subject: [PATCH] Enable build with Clang for KMSAN kernel --- SPECS/kernel-6.1.spec | 67 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/SPECS/kernel-6.1.spec b/SPECS/kernel-6.1.spec index 4fa2cf7..5d19e18 100644 --- a/SPECS/kernel-6.1.spec +++ b/SPECS/kernel-6.1.spec @@ -2,7 +2,21 @@ %define patchlevel 1 # Disable LTO in userspace packages. %global _lto_cflags %{nil} - + +# Option to enable compiling with clang instead of gcc. +%bcond_with toolchain_clang + +%if %{with toolchain_clang} +%global toolchain clang +%endif + +# Compile the kernel with LTO (only supported when building with clang). +%bcond_with clang_lto + +%if %{with clang_lto} && %{without toolchain_clang} +{error:clang_lto requires --with toolchain_clang} +%endif + # The libexec directory is not used by the linker, so the shared object there # should not be exported to RPM provides. %global __provides_exclude_from ^%{_libexecdir}/kselftests @@ -49,7 +63,7 @@ %endif # Set pkg_release. -%global pkg_release 3%{?buildid}.%{?lvc}%{?dist}.inferit.fuzzing +%global pkg_release 4%{?buildid}.%{?lvc}%{?dist}.inferit.fuzzing # Architectures upon which we can sign the kernel # for secure boot authentication. @@ -156,6 +170,21 @@ %define _use_vdso 0 %endif +%if %{with toolchain_clang} +%ifarch aarch64 noarch +%global llvm_ias 0 +%else +%global llvm_ias 1 +%endif +%global clang_make_opts HOSTCC=clang CC=clang LLVM_IAS=%{llvm_ias} +%if %{with clang_lto} +%global clang_make_opts %{clang_make_opts} LD=ld.lld HOSTLD=ld.lld AR=llvm-ar NM=llvm-nm HOSTAR=llvm-ar HOSTNM=llvm-nm +%endif +%global make_opts %{make_opts} %{clang_make_opts} +# clang does not support the -fdump-ipa-clones option +%global with_ipaclones 0 +%endif + # # Packages that need to be installed before the kernel is installed, # as they will be used by the %%post scripts. @@ -185,6 +214,16 @@ BuildRequires: gzip, hmaccalc, hostname, kernel-rpm-macros >= 185-9, kmod, m4, m BuildRequires: patch, perl-Carp, perl-devel, perl-generators, perl-interpreter, python3-devel BuildRequires: redhat-rpm-config, tar, which, xz +%if %{with toolchain_clang} +BuildRequires: clang +%endif + +%if %{with clang_lto} +BuildRequires: llvm +BuildRequires: lld +%endif + + %ifarch x86_64 aarch64 BuildRequires: bpftool, openssl-devel %endif @@ -599,6 +638,7 @@ pathfix.py -i "%{__python3} %{py3_shbang_opts}" -n -p \ tools \ scripts \ Documentation \ + scripts/clang-tools \ 2>&1 | grep -Ev 'recursedown|no change' mv COPYING COPYING-%{version}-%{release} @@ -665,6 +705,13 @@ cat rheldup3.pem rhelkpatch1.pem rhelimaca1.pem > certs/rhel.pem cd configs/ + %if %{with clang_lto} +for i in *aarch64*.config *x86_64*.config; do + sed -i 's/# CONFIG_LTO_CLANG_THIN is not set/CONFIG_LTO_CLANG_THIN=y/' $i + sed -i 's/CONFIG_LTO_NONE=y/# CONFIG_LTO_NONE is not set/' $i +done +%endif + for i in *.config; do sed -i 's@CONFIG_SYSTEM_TRUSTED_KEYS="*"@CONFIG_SYSTEM_TRUSTED_KEYS="certs/rhel.pem"@' $i done @@ -774,10 +821,16 @@ BuildKernel() { perl -p -i -e "s/^CONFIG_BUILD_SALT.*/CONFIG_BUILD_SALT=\"%{KVERREL}\"/" .config %if %{with_std} + if [ $Variant == "kmsan" ]; then + %{make} %{clang_make_opts} %{?_smp_mflags} ARCH=%{bldarch} %{make_target} + + %{make} %{clang_make_opts} %{?_smp_mflags} ARCH=%{bldarch} modules || exit 1 + else %{make} %{?_smp_mflags} ARCH=%{bldarch} %{make_target} %{make} %{?_smp_mflags} ARCH=%{bldarch} modules || exit 1 - + fi + %ifarch aarch64 %{make} %{?_smp_mflags} ARCH=%{bldarch} dtbs %endif @@ -851,8 +904,11 @@ BuildKernel() { # Override mod-fw because we don't want it to install any firmware. # We'll get it from the linux-firmware package and we don't want conflicts. + if [ $Variant == "kmsan" ]; then + %{make} %{clang_make_opts} %{?_smp_mflags} ARCH=%{bldarch} INSTALL_MOD_PATH=$RPM_BUILD_ROOT modules_install KERNELRELEASE=$KernelVer mod-fw= + else %{make} %{?_smp_mflags} ARCH=%{bldarch} INSTALL_MOD_PATH=$RPM_BUILD_ROOT modules_install KERNELRELEASE=$KernelVer mod-fw= - + fi # Add a noop %%defattr statement because rpm doesn't like empty file list files. echo '%%defattr(-,-,-)' > ../%{name}${Variant:+-${Variant}}-ldsoconf.list @@ -1746,6 +1802,9 @@ fi %endif %changelog +* Mon Feb 10 2025 Eduard Basov - 6.1.124-4.lvc13.inferit.fuzzing +- Added build with clang + * Fri Feb 07 2025 Eduard Basov - 6.1.124-3.lvc13.inferit.fuzzing - Added more options - Disable build with GCC 13