test_ksyms.c reads __start_BTF and __stop_BTF as typeless ksyms and
compares their distance with the size of /sys/kernel/btf/vmlinux, and
linked_vars2.c uses __start_BTF as its strong typeless ksym.  With
CONFIG_DEBUG_INFO_BTF_LAZY the image carries the vmlinux BTF only
compressed and these symbols do not exist, so libbpf cannot resolve the
externs and both tests fail to load their skeletons.

Make __start_BTF and __stop_BTF weak in test_ksyms.c and expect a size
of 0 when /proc/kallsyms has no __start_BTF, so the rest of the test
still runs, and let linked_vars2.c reference bpf_map_fops instead, which
every kernel that runs BPF has.

Signed-off-by: Jay Wang <[email protected]>
---
 tools/testing/selftests/bpf/prog_tests/ksyms.c   | 8 +++++++-
 tools/testing/selftests/bpf/progs/linked_vars2.c | 4 ++--
 tools/testing/selftests/bpf/progs/test_ksyms.c   | 5 +++--
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms.c 
b/tools/testing/selftests/bpf/prog_tests/ksyms.c
index dc7aab532fb1..fa97cc0ee3bf 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms.c
@@ -10,7 +10,7 @@ void test_ksyms(void)
        const char *btf_path = "/sys/kernel/btf/vmlinux";
        struct test_ksyms *skel;
        struct test_ksyms__data *data;
-       __u64 link_fops_addr, per_cpu_start_addr;
+       __u64 link_fops_addr, per_cpu_start_addr, btf_start_addr;
        struct stat st;
        __u64 btf_size;
        int err;
@@ -30,6 +30,12 @@ void test_ksyms(void)
        if (!ASSERT_OK(stat(btf_path, &st), "stat_btf"))
                return;
        btf_size = st.st_size;
+       /*
+        * With CONFIG_DEBUG_INFO_BTF_LAZY the image has the vmlinux BTF only
+        * compressed and no __start_BTF/__stop_BTF: the weak ksyms are 0.
+        */
+       if (kallsyms_find("__start_BTF", &btf_start_addr) == -ENOENT)
+               btf_size = 0;
 
        skel = test_ksyms__open_and_load();
        if (!ASSERT_OK_PTR(skel, "test_ksyms__open_and_load"))
diff --git a/tools/testing/selftests/bpf/progs/linked_vars2.c 
b/tools/testing/selftests/bpf/progs/linked_vars2.c
index e4f5bd388a3c..bbdc3b25b483 100644
--- a/tools/testing/selftests/bpf/progs/linked_vars2.c
+++ b/tools/testing/selftests/bpf/progs/linked_vars2.c
@@ -8,7 +8,7 @@
 extern int LINUX_KERNEL_VERSION __kconfig;
 /* when an extern is defined as both strong and weak, resulting symbol will be 
strong */
 extern bool CONFIG_BPF_SYSCALL __kconfig;
-extern const void __start_BTF __ksym;
+extern const void bpf_map_fops __ksym;
 
 int input_bss2;
 int input_data2 = 2;
@@ -47,7 +47,7 @@ int BPF_PROG(handler2)
         */
        output_sink2 = LINUX_KERNEL_VERSION
                       + CONFIG_BPF_SYSCALL
-                      + (long)&__start_BTF;
+                      + (long)&bpf_map_fops;
 
        return 0;
 }
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms.c 
b/tools/testing/selftests/bpf/progs/test_ksyms.c
index 6c9cbb5a3bdf..042943088225 100644
--- a/tools/testing/selftests/bpf/progs/test_ksyms.c
+++ b/tools/testing/selftests/bpf/progs/test_ksyms.c
@@ -11,8 +11,9 @@ __u64 out__btf_size = -1;
 __u64 out__per_cpu_start = -1;
 
 extern const void bpf_link_fops __ksym;
-extern const void __start_BTF __ksym;
-extern const void __stop_BTF __ksym;
+/* weak: CONFIG_DEBUG_INFO_BTF_LAZY keeps no uncompressed BTF in the image */
+extern const void __start_BTF __ksym __weak;
+extern const void __stop_BTF __ksym __weak;
 extern const void __per_cpu_start __ksym;
 /* non-existing symbol, weak, default to zero */
 extern const void bpf_link_fops1 __ksym __weak;
-- 
2.47.3


Reply via email to