Skip to contentSkip to conversation

resolve_btfids: Implement --patch_btfids

PATCH Superseded Ihor Solodrai 2 participants 3 messages Dec 31, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH 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      | 116 +++++++++++++++++++++++++++
 tools/testing/selftests/bpf/Makefile |   2 +-
 4 files changed, 119 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 +116 -0

147 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
Should the file descriptor fd be closed here? Looking at elf_collect() and its cleanup in main(), the pattern is to call elf_end() and then close(fd) separately. Without close(fd), error paths that…
Quoted 4 lines
tools/bpf/resolve_btfids/main.c +0 -0
index 2cbc252259be..1269efa06853 100644

[ ... ]

Quoted 26 lines
tools/bpf/resolve_btfids/main.c +26 -0
+static int patch_btfids(const char *btfids_path, const char *elf_path)
+{
+	Elf_Scn *scn = NULL;
+	FILE *btfids_file;
+	size_t shdrstrndx;
+	int fd, err = -1;
+	Elf_Data *data;
+	struct stat st;
+	GElf_Shdr sh;
+	char *name;
+	Elf *elf;
+
+	elf_version(EV_CURRENT);
+
+	fd = open(elf_path, O_RDWR, 0666);
+	if (fd < 0) {
+		pr_err("FAILED to open %s: %s\n", elf_path, strerror(errno));
+		return -1;
+	}
+
+	elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
+	if (!elf) {
+		close(fd);
+		pr_err("FAILED cannot create ELF descriptor: %s\n", elf_errmsg(-1));
+		return -1;
+	}

[ ... ]

tools/bpf/resolve_btfids/main.c static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, cons · line 971

Quoted 5 lines
tools/bpf/resolve_btfids/main.c +5 -0
+	err = 0;
+out:
+	elf_end(elf);
+	return err;
+}

Should the file descriptor fd be closed here? Looking at elf_collect() and its cleanup in main(), the pattern is to call elf_end() and then close(fd) separately. Without close(fd), error paths that reach 'goto out' after successful open() and elf_begin() will leak the file descriptor.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20608784032

author reviewer
#2 lore raw
Quoted 36 lines · On 12/30/25 4:26 PM, [email protected] wrote:
Quoted 4 lines
tools/bpf/resolve_btfids/main.c +0 -0
index 2cbc252259be..1269efa06853 100644

[ ... ]

Quoted 26 lines
tools/bpf/resolve_btfids/main.c +26 -0
+static int patch_btfids(const char *btfids_path, const char *elf_path)
+{
+	Elf_Scn *scn = NULL;
+	FILE *btfids_file;
+	size_t shdrstrndx;
+	int fd, err = -1;
+	Elf_Data *data;
+	struct stat st;
+	GElf_Shdr sh;
+	char *name;
+	Elf *elf;
+
+	elf_version(EV_CURRENT);
+
+	fd = open(elf_path, O_RDWR, 0666);
+	if (fd < 0) {
+		pr_err("FAILED to open %s: %s\n", elf_path, strerror(errno));
+		return -1;
+	}
+
+	elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
+	if (!elf) {
+		close(fd);
+		pr_err("FAILED cannot create ELF descriptor: %s\n", elf_errmsg(-1));
+		return -1;
+	}

[ ... ]

Quoted 10 lines

tools/bpf/resolve_btfids/main.c static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, cons · line 971

Quoted 5 lines
tools/bpf/resolve_btfids/main.c +5 -0
+	err = 0;
+out:
+	elf_end(elf);
+	return err;
+}

Should the file descriptor fd be closed here? Looking at elf_collect() and its cleanup in main(), the pattern is to call elf_end() and then close(fd) separately. Without close(fd), error paths that reach 'goto out' after successful open() and elf_begin() will leak the file descriptor.

Correct. I missed close(fd) on the happy path.

Quoted 7 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20608784032