Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH 1/3] perf build-id: add shared is_valid_elf() helper
@ 2026-09-07  3:45 Athira Rajeev
  2026-09-07  3:45 ` [PATCH 2/3] perf annotate: fall back to original binary if build-id cache entry is not a valid ELF Athira Rajeev
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Athira Rajeev @ 2026-09-07  3:45 UTC (permalink / raw)
  To: acme, jolsa, adrian.hunter, maddy, irogers, namhyung
  Cc: linux-perf-users, linuxppc-dev, atrajeev, hbathini, tejas05,
	tshah, venkat88, narnalli, vpuliyal

Add is_valid_elf() to build-id.c and declare it in build-id.h so that
multiple perf subsystems can verify ELF magic without duplicating the
check.  The helper opens the file, reads the first SELFMAG (4) bytes,
and compares them against ELFMAG using memcmp().

Both <elf.h> and <fcntl.h> are already included in build-id.c, so no
new dependencies are added.

Reported-by: Narendra Nalli <narnalli@in.ibm.com>
Reported-by: Vijay Puliyala <vpuliyal@in.ibm.com>
Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>
---
 tools/perf/util/build-id.c | 14 ++++++++++++++
 tools/perf/util/build-id.h |  2 ++
 2 files changed, 16 insertions(+)

diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index eb95ab90f974..28b6b3f8d5d3 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -43,6 +43,20 @@
 
 static bool no_buildid_cache;
 
+bool is_valid_elf(const char *filename)
+{
+	unsigned char magic[SELFMAG];
+	int fd = open(filename, O_RDONLY);
+	bool valid = false;
+
+	if (fd < 0)
+		return false;
+	if (read(fd, magic, sizeof(magic)) == (ssize_t)sizeof(magic))
+		valid = (memcmp(magic, ELFMAG, SELFMAG) == 0);
+	close(fd);
+	return valid;
+}
+
 static int mark_dso_hit_callback(struct callchain_cursor_node *node, void *data __maybe_unused)
 {
 	struct map *map = node->ms.map;
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index 73bad90b06f9..dd5b4e3a4d3e 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -76,4 +76,6 @@ extern char buildid_dir[];
 void set_buildid_dir(const char *dir);
 void disable_buildid_cache(void);
 
+bool is_valid_elf(const char *filename);
+
 #endif
-- 
2.43.0


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

end of thread, other threads:[~2026-09-07  4:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-07  3:45 [PATCH 1/3] perf build-id: add shared is_valid_elf() helper Athira Rajeev
2026-09-07  3:45 ` [PATCH 2/3] perf annotate: fall back to original binary if build-id cache entry is not a valid ELF Athira Rajeev
2026-09-07  3:59   ` sashiko-bot
2026-09-07  3:45 ` [PATCH 3/3] perf buildid-cache: skip caching non-ELF files and treat unreadable build-id as mismatch Athira Rajeev
2026-09-07  4:01   ` sashiko-bot
2026-09-07  3:53 ` [PATCH 1/3] perf build-id: add shared is_valid_elf() helper sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox