Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Checking the btf_type kind when fixing variable offsets

I encountered an issue when building the test_progs from the repository [1]:

$ pwd
/work/Qemu/x86_64/linux-6.10-rc2/tools/testing/selftests/bpf/

$ make test_progs V=1
[...]
./tools/sbin/bpftool gen object ./ip_check_defrag.bpf.linked2.o ./ip_check_defrag.bpf.linked1.o
libbpf: failed to find symbol for variable 'bpf_dynptr_slice' in section '.ksyms'
Error: failed to link './ip_check_defrag.bpf.linked1.o': No such file or directory (2)
[...]

Upon investigation, I discovered that the btf_types referenced in the '.ksyms'
section had a kind of BTF_KIND_FUNC instead of BTF_KIND_VAR:

$ bpftool btf dump file ./ip_check_defrag.bpf.linked1.o
[...]
[2] DATASEC '.ksyms' size=0 vlen=2
type_id=16 offset=0 size=0 (FUNC 'bpf_dynptr_from_skb')
type_id=17 offset=0 size=0 (FUNC 'bpf_dynptr_slice')
[...]
[16] FUNC 'bpf_dynptr_from_skb' type_id=82 linkage=extern
[17] FUNC 'bpf_dynptr_slice' type_id=85 linkage=extern
[...]

For a detailed analysis, please refer to [2]. We can add a kind checking to
fix the issue.

[1] https://github.com/eddyz87/bpf/tree/binsort-btf-dedup
[2] https://lore.kernel.org/all/0c0ef20c-c05e-4db9-bad7-2cbc0d6dfae7@oracle.com/

Fixes: 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support")
Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20240619122355.426405-1-dolinux.peng@gmail.com

authored by

Donglin Peng and committed by
Daniel Borkmann
cc5083d1 6ddf3a9a

+9 -2
+9 -2
tools/lib/bpf/linker.c
··· 2227 2227 vi = btf_var_secinfos(t); 2228 2228 for (j = 0, m = btf_vlen(t); j < m; j++, vi++) { 2229 2229 const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type); 2230 - const char *var_name = btf__str_by_offset(obj->btf, vt->name_off); 2231 - int var_linkage = btf_var(vt)->linkage; 2230 + const char *var_name; 2231 + int var_linkage; 2232 2232 Elf64_Sym *sym; 2233 + 2234 + /* could be a variable or function */ 2235 + if (!btf_is_var(vt)) 2236 + continue; 2237 + 2238 + var_name = btf__str_by_offset(obj->btf, vt->name_off); 2239 + var_linkage = btf_var(vt)->linkage; 2233 2240 2234 2241 /* no need to patch up static or extern vars */ 2235 2242 if (var_linkage != BTF_VAR_GLOBAL_ALLOCATED)