Now that BPF static linker supports static variable renames and thus non-unique static variables in BPF skeleton, add tests validating static variables are resolved properly during multi-file static linking. Signed-off-by: Andrii Nakryiko <andrii@xxxxxxxxxx> --- tools/testing/selftests/bpf/prog_tests/linked_vars.c | 12 ++++++++---- tools/testing/selftests/bpf/progs/linked_vars1.c | 4 +++- tools/testing/selftests/bpf/progs/linked_vars2.c | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/linked_vars.c b/tools/testing/selftests/bpf/prog_tests/linked_vars.c index f3d6ba31ef99..73f3fea41230 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_vars.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_vars.c @@ -14,8 +14,12 @@ void test_linked_vars(void) if (!ASSERT_OK_PTR(skel, "skel_open")) return; + skel->bss->linked_vars1__input_bss_static = 100; skel->bss->input_bss1 = 1000; + + skel->bss->linked_vars2__input_bss_static = 200; skel->bss->input_bss2 = 2000; + skel->bss->input_bss_weak = 3000; err = linked_vars__load(skel); @@ -29,11 +33,11 @@ void test_linked_vars(void) /* trigger */ syscall(SYS_getpgid); - ASSERT_EQ(skel->bss->output_bss1, 1000 + 2000 + 3000, "output_bss1"); - ASSERT_EQ(skel->bss->output_bss2, 1000 + 2000 + 3000, "output_bss2"); + ASSERT_EQ(skel->bss->output_bss1, 100 + 1000 + 2000 + 3000, "output_bss1"); + ASSERT_EQ(skel->bss->output_bss2, 200 + 1000 + 2000 + 3000, "output_bss2"); /* 10 comes from "winner" input_data_weak in first obj file */ - ASSERT_EQ(skel->bss->output_data1, 1 + 2 + 10, "output_bss1"); - ASSERT_EQ(skel->bss->output_data2, 1 + 2 + 10, "output_bss2"); + ASSERT_EQ(skel->bss->output_data1, 1 + 2 + 10, "output_data1"); + ASSERT_EQ(skel->bss->output_data2, 1 + 2 + 10, "output_data2"); /* 10 comes from "winner" input_rodata_weak in first obj file */ ASSERT_EQ(skel->bss->output_rodata1, 11 + 22 + 100, "output_weak1"); ASSERT_EQ(skel->bss->output_rodata2, 11 + 22 + 100, "output_weak2"); diff --git a/tools/testing/selftests/bpf/progs/linked_vars1.c b/tools/testing/selftests/bpf/progs/linked_vars1.c index bc96eff9b8c1..05c7a2739c88 100644 --- a/tools/testing/selftests/bpf/progs/linked_vars1.c +++ b/tools/testing/selftests/bpf/progs/linked_vars1.c @@ -10,6 +10,8 @@ extern int LINUX_KERNEL_VERSION __kconfig; extern bool CONFIG_BPF_SYSCALL __kconfig __weak; extern const void bpf_link_fops __ksym __weak; +static volatile int input_bss_static = 0; + int input_bss1 = 0; int input_data1 = 1; const volatile int input_rodata1 = 11; @@ -32,7 +34,7 @@ long output_sink1 = 0; static __noinline int get_bss_res(void) { /* just make sure all the relocations work against .text as well */ - return input_bss1 + input_bss2 + input_bss_weak; + return input_bss_static + input_bss1 + input_bss2 + input_bss_weak; } SEC("raw_tp/sys_enter") diff --git a/tools/testing/selftests/bpf/progs/linked_vars2.c b/tools/testing/selftests/bpf/progs/linked_vars2.c index 946c0ce4f505..c390ac667150 100644 --- a/tools/testing/selftests/bpf/progs/linked_vars2.c +++ b/tools/testing/selftests/bpf/progs/linked_vars2.c @@ -10,6 +10,8 @@ extern int LINUX_KERNEL_VERSION __kconfig; extern bool CONFIG_BPF_SYSCALL __kconfig; extern const void __start_BTF __ksym; +static volatile int input_bss_static = 0; + int input_bss2 = 0; int input_data2 = 2; const volatile int input_rodata2 = 22; @@ -38,7 +40,7 @@ static __noinline int get_data_res(void) SEC("raw_tp/sys_enter") int BPF_PROG(handler2) { - output_bss2 = input_bss1 + input_bss2 + input_bss_weak; + output_bss2 = input_bss_static + input_bss1 + input_bss2 + input_bss_weak; output_data2 = get_data_res(); output_rodata2 = input_rodata1 + input_rodata2 + input_rodata_weak; -- 2.30.2