[PATCH v2 06/10] selftests/bpf: Allow test_progs to link with a partial object set

Ricardo B. Marlière posted 10 patches 3 days, 21 hours ago
There is a newer version of this series
[PATCH v2 06/10] selftests/bpf: Allow test_progs to link with a partial object set
Posted by Ricardo B. Marlière 3 days, 21 hours ago
When individual test files are skipped due to compilation failures, their
.test.o files are absent. The linker step currently lists all expected
.test.o files as explicit prerequisites, so make considers any missing one
an error.

Move TRUNNER_TEST_OBJS to the order-only prerequisite list so that their
absence does not prevent linking. Use $(wildcard ...) in the filter
expression passed to the linker so that only the objects that were actually
built are linked in.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 tools/testing/selftests/bpf/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 6a3835a58f87..ca64a7678dfe 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -738,14 +738,14 @@ endif
 # some X.test.o files have runtime dependencies on Y.bpf.o files
 $(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
 
-$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
-			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
+$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
 			     $(TRUNNER_LIB_OBJS)			\
 			     $(TRUNNER_BPFTOOL)				\
 			     $(OUTPUT)/veristat				\
-			     | $(TRUNNER_BINARY)-extras
+			     | $(TRUNNER_BINARY)-extras			\
+			     $(TRUNNER_TEST_OBJS)
 	$$(call msg,BINARY,,$$@)
-	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LLVM_LDLIBS) $$(LDFLAGS) $$(LLVM_LDFLAGS) -o $$@
+	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^ $$(wildcard $(TRUNNER_TEST_OBJS))) $$(LDLIBS) $$(LLVM_LDLIBS) $$(LDFLAGS) $$(LLVM_LDFLAGS) -o $$@
 	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \
 		   $(OUTPUT)/$(if $2,$2/)bpftool
 

-- 
2.53.0

Re: [PATCH v2 06/10] selftests/bpf: Allow test_progs to link with a partial object set
Posted by bot+bpf-ci@kernel.org 3 days, 20 hours ago
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index 6a3835a58f87..ca64a7678dfe 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile

[ ... ]

> -$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
> -			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
> +$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
>  			     $(TRUNNER_LIB_OBJS)			\
>  			     $(TRUNNER_BPFTOOL)				\
>  			     $(OUTPUT)/veristat				\
> -			     | $(TRUNNER_BINARY)-extras
> +			     | $(TRUNNER_BINARY)-extras			\
> +			     $(TRUNNER_TEST_OBJS)
>  	$$(call msg,BINARY,,$$@)
> -	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LLVM_LDLIBS) $$(LDFLAGS) $$(LLVM_LDFLAGS) -o $$@
> +	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^ $$(wildcard $(TRUNNER_TEST_OBJS))) $$(LDLIBS) $$(LLVM_LDLIBS) $$(LDFLAGS) $$(LLVM_LDFLAGS) -o $$@

Does this change the link order in a way that breaks GNU ld builds?

Before this patch, $^ expanded to:

    TRUNNER_TEST_OBJS TRUNNER_EXTRA_OBJS BPFOBJ TRUNNER_LIB_OBJS ...

After the filter, the linker saw:

    test*.test.o extra*.o libbpf.a lib*.o

After this patch, the filter over "$^ $(wildcard TEST_OBJS)" produces:

    extra*.o libbpf.a lib*.o test*.test.o

BPFOBJ is libbpf/libbpf.a (a static archive). GNU ld scans archives
once, left to right, pulling in only the members that resolve symbols
unresolved at that point. With the new ordering, libbpf.a is scanned
before any test object is seen, so libbpf symbols referenced
exclusively by test objects will remain unresolved.

For example, ring_buffer__new() (in libbpf's ringbuf.c) is called by
prog_tests/ringbuf.c, prog_tests/test_ima.c, and others, but is not
referenced by any TRUNNER_EXTRA_SOURCES file. With this link order,
the ringbuf.o archive member is never pulled in and the link fails
with unresolved symbols.

There is no --start-group or --whole-archive in the link flags, and
LDLIBS (-lelf -lz -lrt -lpthread) does not provide libbpf symbols.

One possible fix would be to append $$(BPFOBJ) after the wildcard
expansion in the filter expression, or to place the test objects
before libbpf.a in the argument list.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/23951016185