[PATCH v1 8/9] perf symbol: Use fallbacks for sysfs__read_build_id

Ian Rogers
2025-11-23T02:32:44Z

Try libelf then symbol-minimal version in symbol.c. Reduce scope of
NOTE_ALIGN macro.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/perf-libelf.c | 56 ++++++++++++++++++++++++++++++++
tools/perf/util/perf-libelf.h | 12 ++++---
tools/perf/util/symbol-elf.c | 50 ----------------------------
tools/perf/util/symbol-minimal.c | 4 +--
tools/perf/util/symbol-minimal.h | 1 +
tools/perf/util/symbol.c | 10 ++++++
6 files changed, 76 insertions(+), 57 deletions(-)

diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index a8a8c39056da..861cb7ae9b45 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -9,6 +9,11 @@
#include "build-id.h"
#include "debug.h"

+/*
+ * Align offset to 4 bytes as needed for note name and descriptor data.
+ */
+#define NOTE_ALIGN(n) (((n) + 3) & -4U)
+
Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
GElf_Shdr *shp, const char *name, size_t *idx)
{
@@ -145,6 +150,57 @@ int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid)
return err;
}

+int libelf_sysfs__read_build_id(const char *filename, struct build_id *bid)
+{
+ size_t size = sizeof(bid->data);
+ int fd, err = -1;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ goto out;
+
+ while (1) {
+ char bf[BUFSIZ];
+ GElf_Nhdr nhdr;
+ size_t namesz, descsz;
+
+ if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
+ break;
+
+ namesz = NOTE_ALIGN(nhdr.n_namesz);
+ descsz = NOTE_ALIGN(nhdr.n_descsz);
+ if (nhdr.n_type == NT_GNU_BUILD_ID &&
+ nhdr.n_namesz == sizeof("GNU")) {
+ if (read(fd, bf, namesz) != (ssize_t)namesz)
+ break;
+ if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
+ size_t sz = min(descsz, size);
+
+ if (read(fd, bid->data, sz) == (ssize_t)sz) {
+ memset(bid->data + sz, 0, size - sz);
+ bid->size = sz;
+ err = 0;
+ break;
+ }
+ } else if (read(fd, bf, descsz) != (ssize_t)descsz)
+ break;
+ } else {
+ int n = namesz + descsz;
+
+ if (n > (int)sizeof(bf)) {
+ n = sizeof(bf);
+ pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
+ __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
+ }
+ if (read(fd, bf, n) != n)
+ break;
+ }
+ }
+ close(fd);
+out:
+ return err;
+}
+
int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size)
{
int fd, err = -1;
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 167679f9fc9b..95fe8805aa41 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -21,17 +21,13 @@ struct build_id;
# define PERF_ELF_C_READ_MMAP ELF_C_READ
#endif

-/*
- * Align offset to 4 bytes as needed for note name and descriptor data.
- */
-#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-
Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
size_t *idx);

int __libelf__read_build_id(Elf *elf, void *bf, size_t size);

int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid);
+int libelf_sysfs__read_build_id(const char *filename, struct build_id *bid);
int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size);

#else // !defined(HAVE_LIBELF_SUPPORT)
@@ -43,6 +39,12 @@ static inline int libelf__read_build_id(int fd __always_unused,
return -1;
}

+static inline int libelf_sysfs__read_build_id(const char *filename __always_unused,
+ struct build_id *bid __always_unused)
+{
+ return -1;
+}
+
static inline int libelf_filename__read_debuglink(const char *filename __always_unused,
char *debuglink __always_unused,
size_t size __always_unused)
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index d4a1b01010c1..5ddf8353e01b 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -751,56 +751,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
return 0;
}

-int sysfs__read_build_id(const char *filename, struct build_id *bid)
-{
- size_t size = sizeof(bid->data);
- int fd, err = -1;
-
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- goto out;
-
- while (1) {
- char bf[BUFSIZ];
- GElf_Nhdr nhdr;
- size_t namesz, descsz;
-
- if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
- break;
-
- namesz = NOTE_ALIGN(nhdr.n_namesz);
- descsz = NOTE_ALIGN(nhdr.n_descsz);
- if (nhdr.n_type == NT_GNU_BUILD_ID &&
- nhdr.n_namesz == sizeof("GNU")) {
- if (read(fd, bf, namesz) != (ssize_t)namesz)
- break;
- if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
- size_t sz = min(descsz, size);
- if (read(fd, bid->data, sz) == (ssize_t)sz) {
- memset(bid->data + sz, 0, size - sz);
- bid->size = sz;
- err = 0;
- break;
- }
- } else if (read(fd, bf, descsz) != (ssize_t)descsz)
- break;
- } else {
- int n = namesz + descsz;
-
- if (n > (int)sizeof(bf)) {
- n = sizeof(bf);
- pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
- __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
- }
- if (read(fd, bf, n) != n)
- break;
- }
- }
- close(fd);
-out:
- return err;
-}
-
bool symsrc__possibly_runtime(struct symsrc *ss)
{
return ss->dynsym || ss->opdsec;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index 8700c0628437..cc9055ba2f2a 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -194,8 +194,7 @@ int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid)
return ret;
}

-#ifndef HAVE_LIBELF_SUPPORT
-int sysfs__read_build_id(const char *filename, struct build_id *bid)
+int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid)
{
int fd;
int ret = -1;
@@ -226,6 +225,7 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid)
return ret;
}

+#ifndef HAVE_LIBELF_SUPPORT
int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
enum dso_binary_type type)
{
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
index 185d98968212..5cce1f1f0f16 100644
--- a/tools/perf/util/symbol-minimal.h
+++ b/tools/perf/util/symbol-minimal.h
@@ -5,5 +5,6 @@
struct build_id;

int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid);
+int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid);

#endif /* __PERF_SYMBOL_MINIMAL_H */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 1152d11a0366..81f4428f7bed 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -2049,6 +2049,16 @@ int filename__read_build_id(const char *filename, struct build_id *bid, bool blo
return err;
}

+int sysfs__read_build_id(const char *filename, struct build_id *bid)
+{
+ int err = libelf_sysfs__read_build_id(filename, bid);
+
+ if (err == 0)
+ return 0;
+
+ return sym_min_sysfs__read_build_id(filename, bid);
+}
+
int filename__read_debuglink(const char *filename, char *debuglink, size_t size)
{
int err = libbfd_filename__read_debuglink(filename, debuglink, size);
--
2.52.0.rc2.455.g230fcf2819-goog