All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libbpf: fix symbol resolution without .symtab
@ 2026-10-23 14:10 Eduard Zateev
  2026-10-23 14:28 ` sashiko-bot
  2026-10-23 15:59 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Eduard Zateev @ 2026-10-23 14:10 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, shuah, linux-kselftest,
	Eduard Zateev

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 <hackerowskiy@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf: fix symbol resolution without .symtab
  2026-10-23 14:10 [PATCH] libbpf: fix symbol resolution without .symtab Eduard Zateev
@ 2026-10-23 14:28 ` sashiko-bot
  2026-10-23 15:59 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-10-23 14:28 UTC (permalink / raw)
  To: Eduard Zateev, 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 <hackerowskiy@gmail.com>

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.

Link: https://github.com/libbpf/libbpf/issues/953

> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index 1e4af62e8312e..4125133d4dab6 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

[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.

[ ... ]

> @@ -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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261023141040.249597-1-hackerowskiy@gmail.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf: fix symbol resolution without .symtab
  2026-10-23 14:10 [PATCH] libbpf: fix symbol resolution without .symtab Eduard Zateev
  2026-10-23 14:28 ` sashiko-bot
@ 2026-10-23 15:59 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-10-23 15:59 UTC (permalink / raw)
  To: Eduard Zateev, bpf
  Cc: daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, shuah, linux-kselftest

On Fri, Oct 23, 2026 at 07:10 AM Eduard Zateev <hackerowskiy@gmail.com> wrote:
> -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.

> +	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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-10-23 15:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23 14:10 [PATCH] libbpf: fix symbol resolution without .symtab Eduard Zateev
2026-10-23 14:28 ` sashiko-bot
2026-10-23 15:59 ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.