Skip to contentSkip to conversation

resolve_btfids: Implement --patch_btfids

PATCH Applied Ihor Solodrai 3 participants 3 messages Dec 31, 2025

Subsystems (4)
lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH v2 1/1 resolve_btfids: Implement --patch_btfids

Recent changes in BTF generation [1] rely on ${OBJCOPY} command to update .BTF_ids section data in target ELF files.

This exposed a bug in llvm-objcopy --update-section code path, that may lead to corruption of a target ELF file. Specifically, because of the bug st_shndx of some symbols may be (incorrectly) set to 0xffff (SHN_XINDEX) [2][3].

While there is a pending fix for LLVM, it'll take some time before it lands (likely in 22.x). And the kernel build must keep working with older LLVM toolchains in the foreseeable future.

Using GNU objcopy for .BTF_ids update would work, but it would require changes to LLVM-based build process, likely breaking existing build environments as discussed in [2].

To work around llvm-objcopy bug, implement --patch_btfids code path in resolve_btfids as a drop-in replacement for:

    ${OBJCOPY} --update-section .BTF_ids=${btf_ids} ${elf}

Which works specifically for .BTF_ids section:

    ${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${elf}

This feature in resolve_btfids can be removed at some point in the future, when llvm-objcopy with a relevant bugfix becomes common.

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://github.com/llvm/llvm-project/issues/168060#issuecomment-3533552952

---

Successful BPF CI run: https://github.com/kernel-patches/bpf/actions/runs/20608321584

---
 scripts/gen-btf.sh                   |   2 +-
 scripts/link-vmlinux.sh              |   2 +-
 tools/bpf/resolve_btfids/main.c      | 117 +++++++++++++++++++++++++++
 tools/testing/selftests/bpf/Makefile |   2 +-
 4 files changed, 120 insertions(+), 3 deletions(-)
scripts/gen-btf.sh +1 -1
index 12244dbe097c..0aec86615416 100755
@@ -123,7 +123,7 @@ embed_btf_data()
 	fi
 	local btf_ids="${ELF_FILE}.BTF_ids"
 	if [ -f "${btf_ids}" ]; then
-		${OBJCOPY} --update-section .BTF_ids=${btf_ids} ${ELF_FILE}
+		${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${ELF_FILE}
 	fi
 }
 
scripts/link-vmlinux.sh +1 -1
index e2207e612ac3..1915adf3249b 100755
@@ -266,7 +266,7 @@ vmlinux_link "${VMLINUX}"
 
 if is_enabled CONFIG_DEBUG_INFO_BTF; then
 	info OBJCOPY ${btfids_vmlinux}
-	${OBJCOPY} --update-section .BTF_ids=${btfids_vmlinux} ${VMLINUX}
+	${RESOLVE_BTFIDS} --patch_btfids ${btfids_vmlinux} ${VMLINUX}
 fi
 
 mksysmap "${VMLINUX}" System.map
tools/bpf/resolve_btfids/main.c +117 -0

143 lines not on this page · raw message

tools/testing/selftests/bpf/Makefile +1 -1
index f28a32b16ff0..9488d076c740 100644
@@ -657,7 +657,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
 	$$(if $$(TEST_NEEDS_BTFIDS),						\
 		$$(call msg,BTFIDS,$(TRUNNER_BINARY),$$@)			\
 		$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@;	\
-		$(OBJCOPY) --update-section [email protected]_ids $$@)
+		$(RESOLVE_BTFIDS) --patch_btfids [email protected]_ids $$@)
 
 $(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d:			\
 			    $(TRUNNER_TESTS_DIR)/%.c			\
Signature · 1 line
2.52.0
#1 lore raw
Quoted 63 lines · Ihor Solodrai

Recent changes in BTF generation [1] rely on ${OBJCOPY} command to update .BTF_ids section data in target ELF files.

This exposed a bug in llvm-objcopy --update-section code path, that may lead to corruption of a target ELF file. Specifically, because of the bug st_shndx of some symbols may be (incorrectly) set to 0xffff (SHN_XINDEX) [2][3].

While there is a pending fix for LLVM, it'll take some time before it lands (likely in 22.x). And the kernel build must keep working with older LLVM toolchains in the foreseeable future.

Using GNU objcopy for .BTF_ids update would work, but it would require changes to LLVM-based build process, likely breaking existing build environments as discussed in [2].

To work around llvm-objcopy bug, implement --patch_btfids code path in resolve_btfids as a drop-in replacement for:

    ${OBJCOPY} --update-section .BTF_ids=${btf_ids} ${elf}

Which works specifically for .BTF_ids section:

    ${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${elf}

This feature in resolve_btfids can be removed at some point in the future, when llvm-objcopy with a relevant bugfix becomes common.

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://github.com/llvm/llvm-project/issues/168060#issuecomment-3533552952

---

Successful BPF CI run: https://github.com/kernel-patches/bpf/actions/runs/20608321584

---
 scripts/gen-btf.sh                   |   2 +-
 scripts/link-vmlinux.sh              |   2 +-
 tools/bpf/resolve_btfids/main.c      | 117 +++++++++++++++++++++++++++
 tools/testing/selftests/bpf/Makefile |   2 +-
 4 files changed, 120 insertions(+), 3 deletions(-)
scripts/gen-btf.sh +1 -1
index 12244dbe097c..0aec86615416 100755
@@ -123,7 +123,7 @@ embed_btf_data()
        fi
        local btf_ids="${ELF_FILE}.BTF_ids"
        if [ -f "${btf_ids}" ]; then
-               ${OBJCOPY} --update-section .BTF_ids=${btf_ids} ${ELF_FILE}
+               ${RESOLVE_BTFIDS} --patch_btfids ${btf_ids} ${ELF_FILE}
        fi
 }
 
scripts/link-vmlinux.sh +0 -0
index e2207e612ac3..1915adf3249b 100755

scripts/link-vmlinux.sh vmlinux_link "${VMLINUX}" · line 270

Quoted 6 lines
scripts/link-vmlinux.sh +1 -1
@@ -266,7 +266,7 @@ vmlinux_link "${VMLINUX}"
 
 if is_enabled CONFIG_DEBUG_INFO_BTF; then
        info OBJCOPY ${btfids_vmlinux}
-       ${OBJCOPY} --update-section .BTF_ids=${btfids_vmlinux} ${VMLINUX}
+       ${RESOLVE_BTFIDS} --patch_btfids ${btfids_vmlinux} ${VMLINUX}
 fi

Applied, but please follow up to reduce the verbosity
OBJCOPY net/mptcp/mptcp_diag.ko.BTF
OBJCOPY net/bridge/br_netfilter.ko.BTF
OBJCOPY net/bridge/netfilter/ebt_log.ko.BTF
OBJCOPY net/caif/caif.ko.BTF
OBJCOPY net/bridge/netfilter/ebt_ip.ko.BTF
BTFIDS net/netfilter/ipvs/ip_vs.ko
OBJCOPY net/netfilter/ipvs/ip_vs.ko.BTF
BTFIDS drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.ko OBJCOPY drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.ko.BTF

All of these OBJCOPY lines are not correct anymore and can be simply removed. No need to yell at the user about these steps.

imo BTFIDS lines can be removed too. They just scroll on the screen.

#2 lore raw
In reply to Ihor Solodrai
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Tue, 30 Dec 2025 17:25:57 -0800 you wrote:

Recent changes in BTF generation [1] rely on ${OBJCOPY} command to update .BTF_ids section data in target ELF files.

This exposed a bug in llvm-objcopy --update-section code path, that may lead to corruption of a target ELF file. Specifically, because of the bug st_shndx of some symbols may be (incorrectly) set to 0xffff (SHN_XINDEX) [2][3].

[...]

Here is the summary with links:
- [bpf-next,v2] resolve_btfids: Implement --patch_btfids

    https://git.kernel.org/bpf/bpf-next/c/1a8fa7faf489

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html