[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251123023225.8069-8-irogers@google.com>
Date: Sat, 22 Nov 2025 18:32:23 -0800
From: Ian Rogers <irogers@...gle.com>
To: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>, Namhyung Kim <namhyung@...nel.org>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>, Jiri Olsa <jolsa@...nel.org>,
Ian Rogers <irogers@...gle.com>, Adrian Hunter <adrian.hunter@...el.com>,
Suzuki K Poulose <suzuki.poulose@....com>, Mike Leach <mike.leach@...aro.org>,
James Clark <james.clark@...aro.org>, John Garry <john.g.garry@...cle.com>,
Will Deacon <will@...nel.org>, Leo Yan <leo.yan@...ux.dev>,
Athira Rajeev <atrajeev@...ux.ibm.com>, tanze <tanze@...inos.cn>,
Aditya Bodkhe <aditya.b1@...ux.ibm.com>, Stephen Brennan <stephen.s.brennan@...cle.com>,
Andi Kleen <ak@...ux.intel.com>, Chun-Tse Shao <ctshao@...gle.com>,
Thomas Falcon <thomas.falcon@...el.com>, Dapeng Mi <dapeng1.mi@...ux.intel.com>,
"Dr. David Alan Gilbert" <linux@...blig.org>, Christophe Leroy <christophe.leroy@...roup.eu>,
"Krzysztof Ćopatowski" <krzysztof.m.lopatowski@...il.com>,
"Masami Hiramatsu (Google)" <mhiramat@...nel.org>, Alexandre Ghiti <alexghiti@...osinc.com>,
Haibo Xu <haibo1.xu@...el.com>, Sergei Trofimovich <slyich@...il.com>, linux-kernel@...r.kernel.org,
linux-perf-users@...r.kernel.org
Subject: [PATCH v1 7/9] perf symbol: Use fallbacks for filename__read_debuglink
Try libbfd and then libelf in symbol.c. Remove the non-existent
symbol-minimal version.
Signed-off-by: Ian Rogers <irogers@...gle.com>
---
tools/perf/util/perf-libelf.c | 52 +++++++++++++++++++++++++++++
tools/perf/util/perf-libelf.h | 10 ++++++
tools/perf/util/symbol-elf.c | 56 --------------------------------
tools/perf/util/symbol-minimal.c | 9 -----
tools/perf/util/symbol.c | 10 ++++++
5 files changed, 72 insertions(+), 65 deletions(-)
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index c7fa08a177d0..a8a8c39056da 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "perf-libelf.h"
+#include <fcntl.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
@@ -143,3 +144,54 @@ int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid)
out:
return err;
}
+
+int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_t size)
+{
+ int fd, err = -1;
+ Elf *elf;
+ GElf_Ehdr ehdr;
+ GElf_Shdr shdr;
+ Elf_Data *data;
+ Elf_Scn *sec;
+ Elf_Kind ek;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ goto out;
+
+ elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+ if (elf == NULL) {
+ pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
+ goto out_close;
+ }
+
+ ek = elf_kind(elf);
+ if (ek != ELF_K_ELF)
+ goto out_elf_end;
+
+ if (gelf_getehdr(elf, &ehdr) == NULL) {
+ pr_err("%s: cannot get elf header.\n", __func__);
+ goto out_elf_end;
+ }
+
+ sec = elf_section_by_name(elf, &ehdr, &shdr,
+ ".gnu_debuglink", NULL);
+ if (sec == NULL)
+ goto out_elf_end;
+
+ data = elf_getdata(sec, NULL);
+ if (data == NULL)
+ goto out_elf_end;
+
+ /* the start of this section is a zero-terminated string */
+ strncpy(debuglink, data->d_buf, size);
+
+ err = 0;
+
+out_elf_end:
+ elf_end(elf);
+out_close:
+ close(fd);
+out:
+ return err;
+}
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 931597493b1f..167679f9fc9b 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -2,6 +2,8 @@
#ifndef __PERF_LIBELF_H
#define __PERF_LIBELF_H
+#include <stddef.h>
+
struct build_id;
#ifdef HAVE_LIBELF_SUPPORT
@@ -30,6 +32,7 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char
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_filename__read_debuglink(const char *filename, char *debuglink, size_t size);
#else // !defined(HAVE_LIBELF_SUPPORT)
@@ -40,6 +43,13 @@ static inline int libelf__read_build_id(int fd __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)
+{
+ return -1;
+}
+
#endif // defined(HAVE_LIBELF_SUPPORT)
#endif /* __PERF_LIBELF_H */
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index b301f1263ecc..d4a1b01010c1 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -801,62 +801,6 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid)
return err;
}
-int filename__read_debuglink(const char *filename, char *debuglink,
- size_t size)
-{
- int fd, err = -1;
- Elf *elf;
- GElf_Ehdr ehdr;
- GElf_Shdr shdr;
- Elf_Data *data;
- Elf_Scn *sec;
- Elf_Kind ek;
-
- err = libbfd_filename__read_debuglink(filename, debuglink, size);
- if (err >= 0)
- goto out;
-
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- goto out;
-
- elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
- if (elf == NULL) {
- pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
- goto out_close;
- }
-
- ek = elf_kind(elf);
- if (ek != ELF_K_ELF)
- goto out_elf_end;
-
- if (gelf_getehdr(elf, &ehdr) == NULL) {
- pr_err("%s: cannot get elf header.\n", __func__);
- goto out_elf_end;
- }
-
- sec = elf_section_by_name(elf, &ehdr, &shdr,
- ".gnu_debuglink", NULL);
- if (sec == NULL)
- goto out_elf_end;
-
- data = elf_getdata(sec, NULL);
- if (data == NULL)
- goto out_elf_end;
-
- /* the start of this section is a zero-terminated string */
- strncpy(debuglink, data->d_buf, size);
-
- err = 0;
-
-out_elf_end:
- elf_end(elf);
-out_close:
- 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 f9adeb09ef00..8700c0628437 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -77,15 +77,6 @@ static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
return -1;
}
-#ifndef HAVE_LIBELF_SUPPORT
-int filename__read_debuglink(const char *filename __maybe_unused,
- char *debuglink __maybe_unused,
- size_t size __maybe_unused)
-{
- return -1;
-}
-#endif
-
/*
* Just try PT_NOTE header otherwise fails
*/
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index c8795e1da2df..1152d11a0366 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 filename__read_debuglink(const char *filename, char *debuglink, size_t size)
+{
+ int err = libbfd_filename__read_debuglink(filename, debuglink, size);
+
+ if (err == 0)
+ return 0;
+
+ return libelf_filename__read_debuglink(filename, debuglink, size);
+}
+
static char *dso__find_kallsyms(struct dso *dso, struct map *map)
{
struct build_id bid = { .size = 0, };
--
2.52.0.rc2.455.g230fcf2819-goog
Powered by blists - more mailing lists