Skip to contentSkip to conversation

kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping

PATCH Superseded Yafang Shao 2 participants 5 messages Jul 12

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping

After switching to the kernel's default package scripts for our local kernel RPM builds, we noticed that module BTF entries were missing:

  $ ls /sys/kernel/btf/
  vmlinux    <<<< only vmlinux, no module BTF

Root cause: find-debuginfo.sh (from the debugedit package) prefers eu-strip over strip when elfutils is installed, which is the common case on RHEL 9. eu-strip removes non-allocated ELF sections, including the .BTF section that contains BPF Type Format information for kernel modules. Without .BTF, BPF tools (bpftool, bcc, bpftrace) cannot resolve kernel types at runtime, and /sys/kernel/btf/<module> entries are not created when modules are loaded.

Fix by passing --keep-section .BTF via _find_debuginfo_opts, which adds -K .BTF to the eu-strip/strip command, preserving the .BTF section while allowing normal debuginfo extraction to proceed.

After this change, all module BTF files are properly generated:

  $ ls /sys/kernel/btf/
  aesni_intel         drm                 i2c_i801            mfd_core
  ahci                drm_client_lib      i2c_mux             net_failover
  backlight           drm_kms_helper      i2c_smbus           pcspkr
  ccp                 drm_shmem_helper    input_leds          qemu_fw_cfg
  dm_log              failover            intel_rapl_common   sch_fq_codel
  dm_mirror           fat                 intel_rapl_msr      serio_raw
  dm_mod              fuse                irqbypass           sunrpc
  dm_region_hash      gf128mul            iTCO_wdt            vfat
  virtio_balloon      virtio_console      virtio_dma_buf      virtio_gpu
  virtio_net          virtio_rng          virtio_blk          vmlinux
  xfs
Assisted-by Comagic:DeepSeek-V4-Flash
---
 scripts/package/kernel.spec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/package/kernel.spec +1 -1
index c732415662ef..abc73de019e0 100644
@@ -67,7 +67,7 @@ This package provides debug information for the kernel image and modules from th
 %undefine _unique_debug_srcs
 %undefine _debugsource_packages
 %undefine _debuginfo_subpackages
-%global _find_debuginfo_opts -r
+%global _find_debuginfo_opts -r --keep-section .BTF
 %global _missing_build_ids_terminate_build 1
 %global _no_recompute_build_ids 1
 %{debug_package}
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/1 kbuild: rpm-pkg: append %{?dist} macro to Release tag

Add support for the %{?dist} macro in the kernel.spec file. This enables building and releasing kernel RPMs with a custom distribution suffix (e.g., via rpmbuild's --define option) to better match production environment tracking.

---
 scripts/package/kernel.spec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/package/kernel.spec +1 -1
index b3c956205af0..c732415662ef 100644
@@ -6,7 +6,7 @@
 Name: kernel
 Summary: The Linux Kernel
 Version: %(echo %{KERNELRELEASE} | sed -e 's/-/_/g')
-Release: %{pkg_release}
+Release: %{pkg_release}%{?dist}
 License: GPL
 Group: System Environment/Kernel
 Vendor: The Linux Community
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH] kbuild: rpm-pkg: append %{?dist} macro to Release tag

Quoted 25 lines · Yafang Shao

Add support for the %{?dist} macro in the kernel.spec file. This enables building and releasing kernel RPMs with a custom distribution suffix (e.g., via rpmbuild's --define option) to better match production environment tracking.

---
 scripts/package/kernel.spec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/package/kernel.spec +1 -2
index b3c956205af0..c732415662ef 100644
@@ -6,7 +6,7 @@
 Name: kernel
 Summary: The Linux Kernel
 Version: %(echo %{KERNELRELEASE} | sed -e 's/-/_/g')
-Release: %{pkg_release}
+Release: %{pkg_release}%{?dist}
 License: GPL
 Group: System Environment/Kernel
 Vendor: The Linux Community
--
Quoted 2 lines

2.47.3

oops!
Apologies for the confusion. Please disregard the patch, as it was an accepted patch accidentally included in the same directory and sent alongside the intended one:

https://lore.kernel.org/linux-kbuild/[email protected]/

--
Regards
Yafang

#3 lore raw
In reply to Yafang Shao
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Unconditionally passing `--keep-section .BTF` to `find-debuginfo.sh` breaks RPM builds on older…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Unconditionally passing --keep-section .BTF to find-debuginfo.sh breaks RPM builds on older distributions. --

--- Patch [1]: [PATCH] kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping --- commit cf88f1e030f88c3862874c09c0ef198fa1f1e9ca
Author: Yafang Shao <[email protected]>

kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping

This commit modifies the kernel RPM spec file to pass the --keep-section .BTF flag to the find-debuginfo.sh script. This preserves the .BTF section during debuginfo extraction, ensuring that BPF tools can successfully resolve kernel types at runtime for modules.

Quoted 4 lines
scripts/package/kernel.spec +0 -0
index c732415662ef2..abc73de019e0c 100644

[ ... ]

scripts/package/kernel.spec This package provides debug information for the kernel image and modules from th · line 70

Quoted 6 lines
scripts/package/kernel.spec +1 -1
@@ -67,7 +67,7 @@ This package provides debug information for the kernel image and modules from th
 %undefine _unique_debug_srcs
 %undefine _debugsource_packages
 %undefine _debuginfo_subpackages
-%global _find_debuginfo_opts -r
+%global _find_debuginfo_opts -r --keep-section .BTF

[Severity: Low]
Will unconditionally passing --keep-section .BTF break the build on older distributions?

Older versions of the find-debuginfo.sh script (such as those shipped with RPM 4.14 on RHEL 8, CentOS 7, or Ubuntu 20.04) do not support the --keep-section option.

Because those legacy scripts parse arguments strictly, they will exit with a usage error when encountering an unrecognized flag, which completely breaks the make rpm-pkg and make binrpm-pkg build targets on those systems.

Is there a way to verify feature support or utilize RPM conditionals before applying this flag to the host script?

Quoted 3 lines
scripts/package/kernel.spec +0 -0
 %global _missing_build_ids_terminate_build 1
 %global _no_recompute_build_ids 1
 %{debug_package}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
Quoted 36 lines · On Sun, Jul 12, 2026 at 2:37 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Unconditionally passing --keep-section .BTF to find-debuginfo.sh breaks RPM builds on older distributions. --

--- Patch [1]: [PATCH] kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping --- commit cf88f1e030f88c3862874c09c0ef198fa1f1e9ca
Author: Yafang Shao <[email protected]>

kbuild: rpm-pkg: Preserve .BTF section in kernel modules during debuginfo stripping

This commit modifies the kernel RPM spec file to pass the --keep-section .BTF flag to the find-debuginfo.sh script. This preserves the .BTF section during debuginfo extraction, ensuring that BPF tools can successfully resolve kernel types at runtime for modules.

Quoted 4 lines
scripts/package/kernel.spec +0 -0
index c732415662ef2..abc73de019e0c 100644

[ ... ]

scripts/package/kernel.spec This package provides debug information for the kernel image and modules from th · line 70

Quoted 6 lines
scripts/package/kernel.spec +1 -1
@@ -67,7 +67,7 @@ This package provides debug information for the kernel image and modules from th
 %undefine _unique_debug_srcs
 %undefine _debugsource_packages
 %undefine _debuginfo_subpackages
-%global _find_debuginfo_opts -r
+%global _find_debuginfo_opts -r --keep-section .BTF

[Severity: Low]
Will unconditionally passing --keep-section .BTF break the build on older distributions?

Older versions of the find-debuginfo.sh script (such as those shipped with RPM 4.14 on RHEL 8, CentOS 7, or Ubuntu 20.04) do not support the --keep-section option.

Quoted 6 lines

Because those legacy scripts parse arguments strictly, they will exit with a usage error when encountering an unrecognized flag, which completely breaks the make rpm-pkg and make binrpm-pkg build targets on those systems.

Is there a way to verify feature support or utilize RPM conditionals before applying this flag to the host script?

What about the following change?

scripts/package/kernel.spec +0 -0
index c732415662ef..2e16de54ed74 100644
@@ -67,7 +67,7 @@ This package provides debug information for the

kernel image and modules from th
%undefine _unique_debug_srcs
%undefine _debugsource_packages
%undefine _debuginfo_subpackages
-%global _find_debuginfo_opts -r
+%global _find_debuginfo_opts -r%{?with_keep_section: --keep-section .BTF}
%global _missing_build_ids_terminate_build 1
%global _no_recompute_build_ids 1
%{debug_package}

scripts/package/mkspec +5 -0
index c604f8c174e2..00acef348ba8 100755
@@ -65,6 +65,11 @@ fi
 echo "%define with_debuginfo_manual $with_debuginfo_manual"
 echo "%define with_debuginfo_rpm $with_debuginfo_rpm"
 
+# Detect if find-debuginfo.sh supports --keep-section (RHEL 9+)
+if find-debuginfo.sh --help 2>&1 | grep -q keep-section; then
+       echo "%global with_keep_section 1"
+fi
+
 cat<<EOF
 %define ARCH ${ARCH}
 %define KERNELRELEASE ${KERNELRELEASE}
Signature · 2 lines
Regards
Yafang