libbpf: fix symbol resolution without .symtab
Subsystems (4)
PATCH 1/1 libbpf: fix symbol resolution without .symtab
When a binary has .dynsym but no .symtab, elf_sym_iter_new() returns -ENOENT for the optional table. Keep checking the other table and clear that error; otherwise successful dynamic-symbol lookups are reported as failures.
Add a regression selftest for a stripped shared object and verify that unresolved symbols still fail.
--- tools/lib/bpf/elf.c | 5 ++- tools/testing/selftests/bpf/Makefile | 7 +++- .../bpf/prog_tests/uprobe_multi_test.c | 37 +++++++++++++++++++ .../selftests/bpf/uprobe_multi_stripped.c | 7 ++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/uprobe_multi_stripped.c
tools/lib/bpf/elf.c +4 -1
@@ -438,8 +438,11 @@ int elf_resolve_syms_offsets(const char *binary_path, int cnt,struct elf_sym *sym; err =elf_sym_iter_new (&iter, elf_fd.elf, binary_path, sh_types[i], st_type); -if (err == -ENOENT ) +if (err == -ENOENT ) { +/* A missing symbol table is OK; check the other one. */ + err =0 ;continue ; + }if (err) goto out;
tools/testing/selftests/bpf/Makefile +6 -1
@@ -61,7 +61,8 @@ TEST_GEN_PROGS_EXTENDED = \ xdp_synproxy \ xskxceiver -TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi +TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi \ + uprobe_multi_stripped.so ifneq ($(V),1) submake_extras := feature_display=0
@@ -666,6 +667,10 @@ $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld $(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) \ $(filter-out %.ld,$^) $(LDLIBS) -o $@ +$(OUTPUT)/uprobe_multi_stripped.so: uprobe_multi_stripped.c + $(call msg,BINARY,,$@) + $(Q)$(CC) $(CFLAGS) -fPIC -shared -Wl,-s $(LDFLAGS) $< -o $@ + EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ feature bpftool $(TEST_KMOD_TARGETS) \
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +37 -0
@@ -374,6 +374,41 @@ static void test_attach_api_syms(void)test_attach_api ("/proc/self/exe" ,NULL , &opts); } +static void test_attach_api_syms_stripped (void ) +{ +LIBBPF_OPTS (bpf_uprobe_multi_opts, opts); +const char *syms[] = {"uprobe_multi_stripped_func" }; +struct uprobe_multi *skel =NULL ; +struct bpf_link *link =NULL ; + + opts.syms = syms; + opts.cnt =ARRAY_SIZE (syms); + + skel =uprobe_multi__open_and_load (); +if (!ASSERT_OK_PTR (skel,"uprobe_multi__open_and_load" )) +return ; + + link =bpf_program__attach_uprobe_multi (skel->progs.uprobe,-1 , +"./uprobe_multi_stripped.so" ,NULL , &opts); +if (!ASSERT_OK_PTR (link,"attach_uprobe_multi_stripped" )) { + link =NULL ; + goto cleanup; + } +bpf_link__destroy (link); + link =NULL ; + +/* A missing symbol must still be reported as an error. */ + syms[0 ] ="uprobe_multi_missing_func" ; + link =bpf_program__attach_uprobe_multi (skel->progs.uprobe,-1 , +"./uprobe_multi_stripped.so" ,NULL , &opts); +if (!ASSERT_ERR_PTR (link,"attach_uprobe_multi_missing_symbol" )) +bpf_link__destroy (link); + link =NULL ; + +cleanup: +uprobe_multi__destroy (skel); +} +static void test_attach_api_fails (void ) {LIBBPF_OPTS (bpf_link_create_opts, opts);
@@ -1442,6 +1477,8 @@ void test_uprobe_multi_test(void)test_attach_api_pattern ();if (test__start_subtest ("attach_api_syms" ))test_attach_api_syms (); +if (test__start_subtest ("attach_api_syms_stripped" )) +test_attach_api_syms_stripped ();if (test__start_subtest ("link_api" ))test_link_api ();if (test__start_subtest ("link_api_path_fd" ))
tools/testing/selftests/bpf/uprobe_multi_stripped.c new file +7 -0
@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +__attribute__((noinline,visibility ("default" ))) +int uprobe_multi_stripped_func (void ) +{ +return 42 ; +}
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/bpf: missing uprobe_multi_stripped.so in RUNNER_EXTRA_FILES - [Low] selftests/bpf…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/bpf: missing uprobe_multi_stripped.so in RUNNER_EXTRA_FILES
- [Low] selftests/bpf: unfiltered -static flag breaks uprobe_multi_stripped.so build --
commit 4a70746dbfb8f05e040808945c59a7d7fbba3b95
Author: Eduard Zateev <[email protected]>
libbpf: fix symbol resolution without .symtab
This commit updates elf_resolve_syms_offsets() in libbpf to clear -ENOENT when one of the symbol tables (.symtab or .dynsym) is absent so symbol resolution succeeds against the remaining table. It also adds a regression selftest using a stripped shared object uprobe_multi_stripped.so.
tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS_EXTENDED = \ · line 65
Quoted 11 lines
tools/testing/selftests/bpf/Makefile +2
-1@@ -61,7 +61,8 @@ TEST_GEN_PROGS_EXTENDED = \ xdp_synproxy \ xskxceiver -TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi +TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi \ + uprobe_multi_stripped.so
[Severity: Medium]
Should $(OUTPUT)/uprobe_multi_stripped.so also be added to RUNNER_EXTRA_FILES in tools/testing/selftests/bpf/Makefile?
Currently RUNNER_EXTRA_FILES lists the other runtime fixtures:
RUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
$(OUTPUT)/liburandom_read.so \
$(OUTPUT)/xdp_synproxy \
$(OUTPUT)/sign-file \
$(OUTPUT)/uprobe_multi \Because test_progs depends on $(RUNNER_EXTRA_FILES) (or $(OUTPUT)/runner-extras) rather than $(TEST_GEN_FILES), running make test_progs on a clean tree will not build uprobe_multi_stripped.so.
In addition, when flavored test runners (test_progs-no_alu32, test_progs-cpuv4, test_progs-bpf_gcc) start, main() -> cd_flavor_subdir() in tools/testing/selftests/bpf/test_progs.c changes the working directory into the flavor subdirectory, where only $(RUNNER_EXTRA_FILES) are copied. When test_attach_api_syms_stripped() in tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c then calls:
link = bpf_program__attach_uprobe_multi(skel->progs.uprobe, -1,
"./uprobe_multi_stripped.so", NULL, &opts);opening ./uprobe_multi_stripped.so fails with -ENOENT.
[ ... ]
tools/testing/selftests/bpf/Makefile $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld · line 672
Quoted 7 lines
tools/testing/selftests/bpf/Makefile +3
-0@@ -666,6 +667,10 @@ $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld $(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) \ $(filter-out %.ld,$^) $(LDLIBS) -o $@ +$(OUTPUT)/uprobe_multi_stripped.so: uprobe_multi_stripped.c + $(call msg,BINARY,,$@) + $(Q)$(CC) $(CFLAGS) -fPIC -shared -Wl,-s $(LDFLAGS) $< -o $@
[Severity: Low]
Will this target fail to link when building the BPF selftests with -static in CFLAGS or LDFLAGS, since -static and -shared conflict when selecting startup objects?
In tools/testing/selftests/bpf/Makefile, the $(OUTPUT)/liburandom_read.so rule filters out -static:
$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
$(call msg,LIB,,$@)
$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
$(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \Would it make sense to use $(filter-out -static,$(CFLAGS) $(LDFLAGS)) for $(OUTPUT)/uprobe_multi_stripped.so as well?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS_EXTENDED = \ · line 65
Quoted 3 lines · Eduard Zateev
tools/testing/selftests/bpf/Makefile +2
-1-TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi +TEST_GEN_FILES += $(TEST_KMODS) liburandom_read.so urandom_read sign-file uprobe_multi \ + uprobe_multi_stripped.so
The test will fail in test_progs-no_alu32 and test_progs-cpuv4. cd_flavor_subdir() does chdir() into no_alu32/ and cpuv4/ and only RUNNER_EXTRA_FILES are copied there. uprobe_multi_stripped.so is not in that list, so elf_open("./uprobe_multi_stripped.so") returns -ENOENT and the first ASSERT_OK_PTR() fails.
'make test_progs' won't build it either.
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c static void test_attach_api_syms(void) · line 398
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +6
-0+if (!ASSERT_OK_PTR (link,"attach_uprobe_multi_stripped" )) { + link =NULL ; + goto cleanup; + } +bpf_link__destroy (link); + link =NULL ;
All three 'link = NULL' are dead code. Nothing reads it afterwards.
pw-bot: cr