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. Link: https://github.com/libbpf/libbpf/issues/953 Signed-off-by: Eduard Zateev --- 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 diff --git a/tools/lib/bpf/elf.c b/tools/lib/bpf/elf.c index 700c3911a..76baaf778 100644 --- a/tools/lib/bpf/elf.c +++ b/tools/lib/bpf/elf.c @@ -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; diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 1e4af62e8..4125133d4 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -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) \ diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c index f0baf5738..340e3b55c 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -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")) diff --git a/tools/testing/selftests/bpf/uprobe_multi_stripped.c b/tools/testing/selftests/bpf/uprobe_multi_stripped.c new file mode 100644 index 000000000..9bdf7fb9b --- /dev/null +++ b/tools/testing/selftests/bpf/uprobe_multi_stripped.c @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +__attribute__((noinline, visibility("default"))) +int uprobe_multi_stripped_func(void) +{ + return 42; +} -- 2.55.0