[PATCH v2 08/10] selftests/bpf: Provide weak definitions for cross-test functions
From: Ricardo B. Marlière
Date: Fri Apr 03 2026 - 11:02:39 EST
Some test files reference functions defined in other test translation
units. When those units are not compiled (e.g. because a BPF skeleton
could not be generated), the link step fails with undefined references.
Replace forward declarations with weak stub definitions for:
- uprobe_multi_func_{1,2,3}() in bpf_cookie.c (defined in
uprobe_multi_test.c)
- stack_mprotect() in bpf_cookie.c and iters.c (defined in test_lsm.c)
The linker will prefer the strong definitions from the original objects
when they are present, and fall back to the stubs otherwise.
Signed-off-by: Ricardo B. Marliere <rbm@xxxxxxxx>
---
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 20 +++++++++++++++-----
tools/testing/selftests/bpf/prog_tests/iters.c | 10 +++++++++-
2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 35adc3f6d443..1619490f1e5c 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -252,10 +252,13 @@ static void kprobe_multi_attach_api_subtest(void)
kprobe_multi__destroy(skel);
}
-/* defined in prog_tests/uprobe_multi_test.c */
-void uprobe_multi_func_1(void);
-void uprobe_multi_func_2(void);
-void uprobe_multi_func_3(void);
+/*
+ * Weak stubs; the noinline definitions in uprobe_multi_test.c take
+ * precedence when that translation unit is compiled and linked.
+ */
+noinline __weak void uprobe_multi_func_1(void) {}
+noinline __weak void uprobe_multi_func_2(void) {}
+noinline __weak void uprobe_multi_func_3(void) {}
static void uprobe_multi_test_run(struct uprobe_multi *skel)
{
@@ -574,7 +577,14 @@ static void tracing_subtest(struct test_bpf_cookie *skel)
close(fmod_ret_fd);
}
-int stack_mprotect(void);
+/*
+ * Weak stub for stack_mprotect(); the real definition lives in
+ * test_lsm.c and takes precedence when that object is linked in.
+ */
+__weak int stack_mprotect(void)
+{
+ return 0;
+}
static void lsm_subtest(struct test_bpf_cookie *skel)
{
diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c
index a539980a2fbe..6888bc9173fd 100644
--- a/tools/testing/selftests/bpf/prog_tests/iters.c
+++ b/tools/testing/selftests/bpf/prog_tests/iters.c
@@ -7,6 +7,7 @@
#include <unistd.h>
#include <malloc.h>
#include <stdlib.h>
+#include <linux/compiler.h>
#include <test_progs.h>
#include "cgroup_helpers.h"
@@ -202,7 +203,14 @@ static void subtest_task_iters(void)
iters_task__destroy(skel);
}
-extern int stack_mprotect(void);
+/*
+ * Weak stub for stack_mprotect(); the real definition lives in
+ * test_lsm.c and takes precedence when that object is linked in.
+ */
+__weak int stack_mprotect(void)
+{
+ return 0;
+}
static void subtest_css_task_iters(void)
{
--
2.53.0