Skip to contentSkip to conversation

perf symbol: Fix symbol loading from libbfd

PATCH Applied Namhyung Kim 3 participants 14 messages Oct 12 – Oct 15 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v3 1/4 perf symbol: Fix symbol loading from libbfd

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")
---
 tools/perf/tests/pe-file-parsing.c | 2 +-
 tools/perf/util/libbfd.c           | 2 +-
 tools/perf/util/symbol.c           | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/pe-file-parsing.c +1 -1
index 30c7da79e109b480..3979f38bf127c218 100644
@@ -59,7 +59,7 @@ static int run_dir(const char *d)
 	TEST_ASSERT_VAL("Failed to get dso", dso);
 
 	ret = dso__load_bfd_symbols(dso, debugfile);
-	TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
+	TEST_ASSERT_VAL("Failed to load symbols", ret > 0);
 
 	dso__sort_by_name(dso);
 	sym = dso__find_symbol_by_name(dso, "main", &idx);
tools/perf/util/libbfd.c +1 -1
index a907d7c1ac948808..a4dcb406c79e9cee 100644
@@ -463,7 +463,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
 	symbols__fixup_duplicate(dso__symbols(dso));
 	dso__set_adjust_symbols(dso, true);
 
-	err = 0;
+	err = dso__has_symbols(dso);
 out_free:
 	free(symbols);
 out_close:
tools/perf/util/symbol.c +2 -2
index 116870595f75c7dd..2b1c1b8d9652e720 100644
@@ -1921,8 +1921,8 @@ int dso__load(struct dso *dso, struct map *map)
 		if (nsexit)
 			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
-		if (bfdrc == 0) {
-			ret = 0;
+		if (bfdrc > 0) {
+			ret = 1;
 			break;
 		}
 
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
author maintainer
#1 lore raw

PATCH v3 2/4 iperf symbol: Add missing symsrc__destroy() for libbfd

When libbfd loaded symbols and there's a pending syms_ss, it'd be lost or overwrite existing symbols. Let's destroy it before breaking the loop so that it can safely return from the function.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")
---
 tools/perf/util/symbol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/symbol.c +2 -1
index 2b1c1b8d9652e720..02845b69ae6b5fcf 100644
@@ -1923,7 +1923,7 @@ int dso__load(struct dso *dso, struct map *map)
 
 		if (bfdrc > 0) {
 			ret = 1;
-			break;
+			goto out_destroy;
 		}
 
 		if (!is_reg || sirc < 0)
@@ -1975,6 +1975,7 @@ int dso__load(struct dso *dso, struct map *map)
 			ret += nr_plt;
 	}
 
+out_destroy:
 	for (; ss_pos > 0; ss_pos--)
 		symsrc__destroy(&ss_[ss_pos - 1]);
 out_free:
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#2 lore raw

Re: [PATCH v3 2/4] iperf symbol: Add missing symsrc__destroy() for libbfd

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

When libbfd loaded symbols and there's a pending syms_ss, it'd be lost or overwrite existing symbols. Let's destroy it before breaking the loop so that it can safely return from the function.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#3 lore raw

PATCH v3 3/4 perf symbol: Set dso symtab type for libbfd

In reply to Namhyung Kim

It missed to set the dso symtab type when the symbols are loaded by libbfd. When it happens both syms_ss and runtime_ss may not be set and won't call dso__load_sym().

---
 tools/perf/util/symbol.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/symbol.c +1 -0
index 02845b69ae6b5fcf..b61a574ce9a30721 100644
@@ -1922,6 +1922,7 @@ int dso__load(struct dso *dso, struct map *map)
 			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
 		if (bfdrc > 0) {
+			dso__set_symtab_type(dso, symtab_type);
 			ret = 1;
 			goto out_destroy;
 		}
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#4 lore raw

Re: [PATCH v3 3/4] perf symbol: Set dso symtab type for libbfd

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf: missing dso__set_binary_type() initialization for libbfd bypass --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: missing dso__set_binary_type() initialization for libbfd bypass --

commit 3d059164c2c17ae3b0ec344af1e8527af000eee5
Author: Namhyung Kim <[email protected]>

perf symbol: Set dso symtab type for libbfd

It missed to set the dso symtab type when the symbols are loaded by libbfd. When it happens both syms_ss and runtime_ss may not be set and won't call dso__load_sym().

tools/perf/util/symbol.c int dso__load(struct dso *dso, struct map *map) · line 1928

Quoted 9 lines
tools/perf/util/symbol.c +1 -0
index 4878d4292eb85..a90ce714b174b 100644
@@ -1925,6 +1925,7 @@ int dso__load(struct dso *dso, struct map *map)
 			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
 		if (bfdrc > 0) {
+			dso__set_symtab_type(dso, symtab_type);

[Severity: Medium]
When dso__load_bfd_symbols() succeeds and we take this early return path, does this code also need to set the DSO's binary_type and cross-platform architecture flags?

By taking this goto out_destroy, the rest of the DSO initialization loop is bypassed. This leaves the binary_type as DSO_BINARY_TYPE__NOT_FOUND. Later, when dso__data_fd() attempts to read instruction bytes for perf annotate or Intel PT decoding, try_to_open_dso() will restrict its search to only BUILD_ID_CACHE and SYSTEM_PATH_DSO since the actual discovery path wasn't recorded.

Additionally, bypassing dso__load_sym() means is_64_bit won't be set, which causes it to incorrectly default to the host perf binary's pointer size. This breaks cross-platform analysis of 32-bit files on 64-bit hosts.

Quoted 3 lines
tools/perf/util/symbol.c +0 -0
 			ret = 1;
 			goto out_destroy;
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#5 lore raw

Re: [PATCH v3 3/4] perf symbol: Set dso symtab type for libbfd

Quoted 28 lines · On Tue, Oct 13, 2026 at 12:08:01AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: missing dso__set_binary_type() initialization for libbfd bypass --

commit 3d059164c2c17ae3b0ec344af1e8527af000eee5
Author: Namhyung Kim <[email protected]>

perf symbol: Set dso symtab type for libbfd

It missed to set the dso symtab type when the symbols are loaded by libbfd. When it happens both syms_ss and runtime_ss may not be set and won't call dso__load_sym().

tools/perf/util/symbol.c int dso__load(struct dso *dso, struct map *map) · line 1928

Quoted 9 lines
tools/perf/util/symbol.c +1 -0
index 4878d4292eb85..a90ce714b174b 100644
@@ -1925,6 +1925,7 @@ int dso__load(struct dso *dso, struct map *map)
 			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
 		if (bfdrc > 0) {
+			dso__set_symtab_type(dso, symtab_type);

[Severity: Medium]
When dso__load_bfd_symbols() succeeds and we take this early return path, does this code also need to set the DSO's binary_type and cross-platform architecture flags?

Quoted 6 lines

By taking this goto out_destroy, the rest of the DSO initialization loop is bypassed. This leaves the binary_type as DSO_BINARY_TYPE__NOT_FOUND. Later, when dso__data_fd() attempts to read instruction bytes for perf annotate or Intel PT decoding, try_to_open_dso() will restrict its search to only BUILD_ID_CACHE and SYSTEM_PATH_DSO since the actual discovery path wasn't recorded.

As I said before, it should be handled in a separate patch series.

Quoted 4 lines

Additionally, bypassing dso__load_sym() means is_64_bit won't be set, which causes it to incorrectly default to the host perf binary's pointer size. This breaks cross-platform analysis of 32-bit files on 64-bit hosts.

Looks like a pre-existing issue. I'll add it in a separate patch.

Thanks,
Namhyung

Quoted 7 lines
Quoted 4 lines
tools/perf/util/symbol.c +0 -0
 			ret = 1;
 			goto out_destroy;
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#6 lore raw

PATCH v3 4/4 perf test: Update PE file test for other locations

In reply to Namhyung Kim

The test__pe_file_parsing() expects the test is in the tools/perf directory, but it could be root or a separate build directory. Let's update the logic to try other paths like in the shell_tests__dir_fd().

Also add some debug messages to inform users why it's skipped.

---
 tools/perf/tests/pe-file-parsing.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
tools/perf/tests/pe-file-parsing.c +12 -8
index 3979f38bf127c218..190520ffc3ad6d0c 100644
@@ -74,17 +74,20 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
 {
 	struct stat st;
 	char path_dir[PATH_MAX];
-
-	/* First try development tree tests. */
-	if (!lstat("./tests", &st))
-		return run_dir("./tests");
-
-	/* Then installed path. */
+	const char * const tests_dirs[] = {
+		"./tools/perf/tests",
+		"./tests",
+		"./source/tests",
+		path_dir,
+	};
 	snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
 
-	if (!lstat(path_dir, &st))
-		return run_dir(path_dir);
+	for (size_t i = 0; i < ARRAY_SIZE(tests_dirs); ++i) {
+		if (!lstat(tests_dirs[i], &st))
+			return run_dir(tests_dirs[i]);
+	}
 
+	pr_debug("cannot find the tests directory\n");
 	return TEST_SKIP;
 }
 
@@ -93,6 +96,7 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
 static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
 			  int subtest __maybe_unused)
 {
+	pr_debug("libbfd is not available\n");
 	return TEST_SKIP;
 }
 
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#7 lore raw

Re: [PATCH v3 4/4] perf test: Update PE file test for other locations

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf test: unconditional leak of get_argv_exec_path() and NULL deref --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf test: unconditional leak of get_argv_exec_path() and NULL deref --

commit 9107444f4267d3db60d0e4c357f3b28957bdb1bc
Author: Namhyung Kim <[email protected]>

perf test: Update PE file test for other locations

This commit updates the PE file parsing test to check multiple directory paths for the test data. The logic is modified to try other paths similar to the shell_tests__dir_fd() fallback mechanism.

Quoted 11 lines
tools/perf/tests/pe-file-parsing.c +0 -3
index 3979f38bf127c..190520ffc3ad6 100644
@@ -74,17 +74,20 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
 {
 	struct stat st;
 	char path_dir[PATH_MAX];
-
-	/* First try development tree tests. */
-	if (!lstat("./tests", &st))

tools/perf/tests/pe-file-parsing.c static int test__pe_file_parsing(struct test_suite *test __maybe_unused, · line 83

Quoted 10 lines
tools/perf/tests/pe-file-parsing.c +6 -3
-		return run_dir("./tests");
-
-	/* Then installed path. */
+	const char * const tests_dirs[] = {
+		"./tools/perf/tests",
+		"./tests",
+		"./source/tests",
+		path_dir,
+	};
 	snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());

[Severity: Low]
Can this code in test__pe_file_parsing() lead to a memory leak and a potential NULL pointer dereference?

The get_argv_exec_path() function returns a dynamically allocated string that must be freed by the caller. Passing it directly to snprintf() inline causes the pointer to be permanently lost.

Additionally, since the patch moved this snprintf() call before the directory search loop, it now unconditionally allocates this string even if an earlier directory in the tests_dirs array would have succeeded.

Furthermore, if get_argv_exec_path() fails under memory pressure and returns NULL, snprintf("%s") could crash on strictly compliant libcs like musl.

Quoted 7 lines
tools/perf/tests/pe-file-parsing.c +4 -2
-	if (!lstat(path_dir, &st))
-		return run_dir(path_dir);
+	for (size_t i = 0; i < ARRAY_SIZE(tests_dirs); ++i) {
+		if (!lstat(tests_dirs[i], &st))
+			return run_dir(tests_dirs[i]);
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#8 lore raw

Re: [PATCH v3 4/4] perf test: Update PE file test for other locations

Quoted 43 lines · On Tue, Oct 13, 2026 at 12:08:54AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf test: unconditional leak of get_argv_exec_path() and NULL deref --

commit 9107444f4267d3db60d0e4c357f3b28957bdb1bc
Author: Namhyung Kim <[email protected]>

perf test: Update PE file test for other locations

This commit updates the PE file parsing test to check multiple directory paths for the test data. The logic is modified to try other paths similar to the shell_tests__dir_fd() fallback mechanism.

Quoted 11 lines
tools/perf/tests/pe-file-parsing.c +0 -3
index 3979f38bf127c..190520ffc3ad6 100644
@@ -74,17 +74,20 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
 {
 	struct stat st;
 	char path_dir[PATH_MAX];
-
-	/* First try development tree tests. */
-	if (!lstat("./tests", &st))

tools/perf/tests/pe-file-parsing.c static int test__pe_file_parsing(struct test_suite *test __maybe_unused, · line 83

Quoted 10 lines
tools/perf/tests/pe-file-parsing.c +6 -3
-		return run_dir("./tests");
-
-	/* Then installed path. */
+	const char * const tests_dirs[] = {
+		"./tools/perf/tests",
+		"./tests",
+		"./source/tests",
+		path_dir,
+	};
 	snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());

[Severity: Low]
Can this code in test__pe_file_parsing() lead to a memory leak and a potential NULL pointer dereference?

The get_argv_exec_path() function returns a dynamically allocated string that must be freed by the caller. Passing it directly to snprintf() inline causes the pointer to be permanently lost.

Quoted 6 lines

Additionally, since the patch moved this snprintf() call before the directory search loop, it now unconditionally allocates this string even if an earlier directory in the tests_dirs array would have succeeded.

Furthermore, if get_argv_exec_path() fails under memory pressure and returns NULL, snprintf("%s") could crash on strictly compliant libcs like musl.

These are pre-existing issues, will fix separately.

Thanks,
Namhyung

Quoted 11 lines
Quoted 8 lines
tools/perf/tests/pe-file-parsing.c +4 -2
-	if (!lstat(path_dir, &st))
-		return run_dir(path_dir);
+	for (size_t i = 0; i < ARRAY_SIZE(tests_dirs); ++i) {
+		if (!lstat(tests_dirs[i], &st))
+			return run_dir(tests_dirs[i]);
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw
In reply to Namhyung Kim
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#10 lore raw
In reply to Namhyung Kim
Quoted 11 lines · Namhyung Kim

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")

Thanks, applied to perf-tools-next, for v7.4.

- Arnaldo

author maintainer
#11 lore raw
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 6 lines · Namhyung Kim

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Quoted 7 lines
Quoted 5 lines
Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")

Thanks, applied to perf-tools-next, for v7.4.

Thanks Arnaldo!

As you may know, this is from my other series on cleaning up symtab and binary types. I'd like to land it as well so that I can continue working on the related issues. There are some Sashiko feedback left, but I think they are either pre-existing bugs or known issues.

Thanks,
Namhyung

#12 lore raw
Quoted 14 lines · Namhyung Kim
Quoted 13 lines · Arnaldo Carvalho de Melo
Quoted 11 lines · Namhyung Kim

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")
Quoted 8 lines
Quoted 1 line

Thanks, applied to perf-tools-next, for v7.4.

Thanks Arnaldo!

As you may know, this is from my other series on cleaning up symtab and binary types. I'd like to land it as well so that I can continue working on the related issues. There are some Sashiko feedback left, but I think they are either pre-existing bugs or known issues.

Right, its now at tmp.perf-tools-next, while I test build in my container set, will push it to perf-tools-next next.

- Arnaldo

maintainer Reviewed-by Ian Rogers
#13 lore raw
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 18 lines · Namhyung Kim
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 6 lines · Namhyung Kim

The dso__load() should return a positive number (number of symbols loaded) when it succeded to load symbols. But dso__load_bfd_symbols() only returns 0 and it's hard to check if it actually loaded symbols or not. Let's return the value of dso__has_symbol() and check it in dso__load() so that it can return 1 for success.

Quoted 7 lines
Quoted 5 lines
Fixes eac9a4342e5447cad ("perf symbols: Try reading the symbol table with libbfd")

Thanks, applied to perf-tools-next, for v7.4.

Thanks Arnaldo!

Quoted 7 lines
Quoted 4 lines

As you may know, this is from my other series on cleaning up symtab and binary types. I'd like to land it as well so that I can continue working on the related issues. There are some Sashiko feedback left, but I think they are either pre-existing bugs or known issues.

Right, its now at tmp.perf-tools-next, while I test build in my container set, will push it to perf-tools-next next.

Building it with CC=clang I get:

LD /tmp/build/perf-tools-next/tests/perf-test-in.o
util/dso.c:942:7: error: mutex '_dso__data_open_lock' is not held on every path through here [-Werror,-Wthread-safety-analysis]
942 | if (dso__data_open_cnt) {
| ^
util/dso.c:940:4: note: mutex acquired here

  940 |                         mutex_lock(dso__data_open_lock());
      |                         ^

util/dso.c:942:7: error: reading variable 'dso__data_open_cnt' requires holding mutex '_dso__data_open_lock' CC /tmp/build/perf-tools-next/util/maps.o
[-Werror,-Wthread-safety-analysis]
942 | if (dso__data_open_cnt) {
| ^
util/dso.c:943:4: error: calling function 'close_first_dso' requires holding mutex '_dso__data_open_lock' exclusively [-Werror,-Wthread-safety-analysis]

  943 |                         close_first_dso();
      |                         ^
util/dso.c:949:3: error: releasing mutex '_dso__data_open_lock' that was not held [-Werror,-Wthread-safety-analysis]
  949 |                 mutex_unlock(dso__data_open_lock());
      |                 ^

4 errors generated.
make[4]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:95: /tmp/build/perf-tools-next/util/dso.o] Error 1
CC /tmp/build/perf-tools-next/util/thread.o

Bisected to:

⬢ [acme@tb-phoenix2-perf perf-tools-next]$ git bisect good 3759ca226d57d06c4c52a1e64890faf58a29ebf1 is the first 'bad' commit commit 3759ca226d57d06c4c52a1e64890faf58a29ebf1

Author: Namhyung Kim <[email protected]>
Date:   Tue Oct 13 10:21:51 2026 -0700

perf tools: Factor out dso__find_binary_type()

Separate out the logic to detect binary type of a DSO. It checks if the candidate file contains ".text" section. There could be data-only DSOs but as our use cases focus on the code (perf annotate or intel PT), we can ignore them for now.

But it also can fail due to open file limit. Handle the case by retrying with _dso__data_open_lock.

Introduce bintype_searched flag and it accessors to prevent repeated (failed) search.

 tools/perf/util/dso.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
 tools/perf/util/dso.h |  13 +++++++++++++
 2 files changed, 139 insertions(+), 12 deletions(-)

⬢ [acme@tb-phoenix2-perf perf-tools-next]$