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

selftests/bpf: Test linking with duplicate extern functions

Previously when multiple BPF object files referencing the same extern
function (usually kfunc) are statically linked using `bpftool gen
object`, libbpf tries to get the nonexistent size of BTF_KIND_FUNC_PROTO
and fails. This test ensures it is fixed.

Signed-off-by: Eric Long <i@hack3r.moe>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241002-libbpf-dup-extern-funcs-v4-2-560eb460ff90@hack3r.moe

authored by

Eric Long and committed by
Andrii Nakryiko
3c591de2 4b146e95

+16
+8
tools/testing/selftests/bpf/progs/linked_funcs1.c
··· 63 63 /* here we'll force set_output_ctx2() to be __hidden in the final obj file */ 64 64 __hidden extern void set_output_ctx2(__u64 *ctx); 65 65 66 + void *bpf_cast_to_kern_ctx(void *obj) __ksym; 67 + 66 68 SEC("?raw_tp/sys_enter") 67 69 int BPF_PROG(handler1, struct pt_regs *regs, long id) 68 70 { ··· 86 84 set_output_weak(42); 87 85 88 86 return 0; 87 + } 88 + 89 + /* Generate BTF FUNC record and test linking with duplicate extern functions */ 90 + void kfunc_gen1(void) 91 + { 92 + bpf_cast_to_kern_ctx(0); 89 93 } 90 94 91 95 char LICENSE[] SEC("license") = "GPL";
+8
tools/testing/selftests/bpf/progs/linked_funcs2.c
··· 63 63 /* here we'll force set_output_ctx1() to be __hidden in the final obj file */ 64 64 __hidden extern void set_output_ctx1(__u64 *ctx); 65 65 66 + void *bpf_cast_to_kern_ctx(void *obj) __ksym; 67 + 66 68 SEC("?raw_tp/sys_enter") 67 69 int BPF_PROG(handler2, struct pt_regs *regs, long id) 68 70 { ··· 86 84 set_output_weak(42); 87 85 88 86 return 0; 87 + } 88 + 89 + /* Generate BTF FUNC record and test linking with duplicate extern functions */ 90 + void kfunc_gen2(void) 91 + { 92 + bpf_cast_to_kern_ctx(0); 89 93 } 90 94 91 95 char LICENSE[] SEC("license") = "GPL";