From: Ian Rogers <irogers@google.com>
To: "Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Arnaldo Carvalho de Melo" <acme@kernel.org>,
"Namhyung Kim" <namhyung@kernel.org>,
"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
"Jiri Olsa" <jolsa@kernel.org>, "Ian Rogers" <irogers@google.com>,
"Adrian Hunter" <adrian.hunter@intel.com>,
"Suzuki K Poulose" <suzuki.poulose@arm.com>,
"Mike Leach" <mike.leach@linaro.org>,
"James Clark" <james.clark@linaro.org>,
"John Garry" <john.g.garry@oracle.com>,
"Will Deacon" <will@kernel.org>, "Leo Yan" <leo.yan@linux.dev>,
"Athira Rajeev" <atrajeev@linux.ibm.com>,
tanze <tanze@kylinos.cn>,
"Stephen Brennan" <stephen.s.brennan@oracle.com>,
"Andi Kleen" <ak@linux.intel.com>,
"Chun-Tse Shao" <ctshao@google.com>,
"Thomas Falcon" <thomas.falcon@intel.com>,
"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
"Dr. David Alan Gilbert" <linux@treblig.org>,
"Christophe Leroy" <christophe.leroy@csgroup.eu>,
"Krzysztof Łopatowski" <krzysztof.m.lopatowski@gmail.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
"Alexandre Ghiti" <alexghiti@rivosinc.com>,
"Haibo Xu" <haibo1.xu@intel.com>,
"Sergei Trofimovich" <slyich@gmail.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v2 08/10] perf symbol: Make a common sysfs__read_build_id
Date: Mon, 1 Dec 2025 12:55:07 -0800 [thread overview]
Message-ID: <20251201205509.195451-9-irogers@google.com> (raw)
In-Reply-To: <20251201205509.195451-1-irogers@google.com>
The libelf version of sysfs__read_build_id would read through the
file, the symbol-minimal version would read the whole file and then
use regular build-id parsing. Given we prefer the libelf code, make
both implementations use the libelf implementation and not require
reading the whole file. As the libelf implementation uses libelf
structs, move the code to symbol-minimal and use the
style/implementation that exists there already in the in memory
read_build_id code. Note, this means we could avoid reading phdrs
where read_build_id is used and switch to read_build_id_fd to
potentially avoid some memory allocation.
Clean up how bf was being used as it bound checked bf when a build ID
note was found but not when the name wasn't "GNU". Switch to just
lseek-ing forward the file to avoid any buffer overrun problems and
unnecessary reading.
Reduce scope of NOTE_ALIGN macro in perf-libelf.h.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/perf-libelf.c | 5 ++
tools/perf/util/perf-libelf.h | 5 --
tools/perf/util/symbol-elf.c | 50 --------------------
tools/perf/util/symbol-minimal.c | 80 +++++++++++++++++++++++---------
tools/perf/util/symbol-minimal.h | 1 +
tools/perf/util/symbol.c | 6 +++
6 files changed, 69 insertions(+), 78 deletions(-)
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index a8a8c39056da..4198f4dfad9f 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)
{
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 167679f9fc9b..2118325c04e5 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -21,11 +21,6 @@ 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);
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 9ceb746b6c59..cb890de31044 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 445307f230af..0e3a27dae9d6 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -77,6 +77,58 @@ static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
return -1;
}
+static int read_build_id_fd(int fd, struct build_id *bid, bool need_swap)
+{
+ size_t size = sizeof(bid->data);
+ struct {
+ u32 n_namesz;
+ u32 n_descsz;
+ u32 n_type;
+ } nhdr;
+
+ while (true) {
+ size_t namesz, descsz, n;
+ ssize_t err;
+
+ err = read(fd, &nhdr, sizeof(nhdr));
+ if (err != sizeof(nhdr))
+ return err == -1 ? -errno : -EIO;
+
+ if (need_swap) {
+ nhdr.n_namesz = bswap_32(nhdr.n_namesz);
+ nhdr.n_descsz = bswap_32(nhdr.n_descsz);
+ nhdr.n_type = bswap_32(nhdr.n_type);
+ }
+ namesz = NOTE_ALIGN(nhdr.n_namesz);
+ descsz = NOTE_ALIGN(nhdr.n_descsz);
+ n = namesz + descsz; /* Remaining bytes of this note. */
+
+ if (nhdr.n_type == NT_GNU_BUILD_ID && nhdr.n_namesz == sizeof("GNU")) {
+ char name[NOTE_ALIGN(sizeof("GNU"))];
+
+ err = read(fd, name, sizeof(name));
+ if (err != sizeof(name))
+ return err == -1 ? -errno : -EIO;
+
+ n = descsz;
+ if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
+ /* Successfully found build ID. */
+ ssize_t sz = min(descsz, size);
+
+ err = read(fd, bid->data, sz);
+ if (err != sz)
+ return err == -1 ? -errno : -EIO;
+
+ memset(bid->data + sz, 0, size - sz);
+ bid->size = sz;
+ return 0;
+ }
+ }
+ if (lseek(fd, n, SEEK_CUR) == -1)
+ return -errno;
+ }
+}
+
/*
* Just try PT_NOTE header otherwise fails
*/
@@ -194,38 +246,20 @@ 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;
- struct stat stbuf;
- size_t buf_size;
- void *buf;
+ int fd, ret;
fd = open(filename, O_RDONLY);
if (fd < 0)
- return -1;
-
- if (fstat(fd, &stbuf) < 0)
- goto out;
-
- buf_size = stbuf.st_size;
- buf = malloc(buf_size);
- if (buf == NULL)
- goto out;
-
- if (read(fd, buf, buf_size) != (ssize_t) buf_size)
- goto out_free;
+ return -errno;
- ret = read_build_id(buf, buf_size, bid, false);
-out_free:
- free(buf);
-out:
+ ret = read_build_id_fd(fd, bid, /*need_swap=*/false);
close(fd);
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 da75a1c159f2..76dc5b70350a 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -2050,6 +2050,12 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
return err;
}
+int sysfs__read_build_id(const char *filename, struct build_id *bid)
+{
+ /* Doesn't mmap file into memory. */
+ 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.158.g65b55ccf14-goog
next prev parent reply other threads:[~2025-12-01 20:55 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-01 20:54 [PATCH v2 00/10] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
2025-12-01 20:55 ` [PATCH v2 01/10] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
2025-12-01 20:55 ` [PATCH v2 02/10] perf symbol: Reduce scope of arch__sym_update Ian Rogers
2025-12-01 20:55 ` [PATCH v2 03/10] perf symbol: Move libelf code to its own file/header Ian Rogers
2025-12-01 20:55 ` [PATCH v2 04/10] perf symbol: Remove unused includes Ian Rogers
2025-12-01 20:55 ` [PATCH v2 05/10] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
2025-12-01 20:55 ` [PATCH v2 06/10] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
2025-12-01 20:55 ` [PATCH v2 07/10] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
2025-12-01 20:55 ` Ian Rogers [this message]
2025-12-01 20:55 ` [PATCH v2 09/10] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
2025-12-01 20:55 ` [PATCH v2 10/10] perf symbol: Fix ENOENT case for filename__read_build_id Ian Rogers
2025-12-05 19:40 ` Ian Rogers
2025-12-05 21:18 ` Namhyung Kim
2025-12-07 2:28 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251201205509.195451-9-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=alexghiti@rivosinc.com \
--cc=atrajeev@linux.ibm.com \
--cc=christophe.leroy@csgroup.eu \
--cc=ctshao@google.com \
--cc=dapeng1.mi@linux.intel.com \
--cc=haibo1.xu@intel.com \
--cc=james.clark@linaro.org \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=krzysztof.m.lopatowski@gmail.com \
--cc=leo.yan@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux@treblig.org \
--cc=mhiramat@kernel.org \
--cc=mike.leach@linaro.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=slyich@gmail.com \
--cc=stephen.s.brennan@oracle.com \
--cc=suzuki.poulose@arm.com \
--cc=tanze@kylinos.cn \
--cc=thomas.falcon@intel.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).