linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink
@ 2025-11-23  2:32 Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 1/9] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

This cleans up some tech debt mentioned in the thread:
https://lore.kernel.org/lkml/aQt66zhfxSA80xwt@gentoo.org/

The perf tool has fallbacks for objdump but not in the case of
build-id and debuglink reading. Refactor the code so that in the case
of read_build_id it now looks like:

```
int filename__read_build_id(const char *filename, struct build_id *bid, bool block)
{
        struct kmod_path m = { .name = NULL, };
        char path[PATH_MAX];
        int err, fd;

        if (!filename)
                return -EFAULT;

        err = kmod_path__parse(&m, filename);
        if (err)
                return -1;

        if (m.comp) {
                int error = 0;

                fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
                if (fd < 0) {
                        pr_debug("Failed to decompress (error %d) %s\n",
                                 error, filename);
                        return -1;
                }
                lseek(fd, 0, SEEK_SET);
                filename = path;
                block = true;
        } else {
                fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
                if (fd < 0) {
                        pr_debug("Failed to open %s\n", filename);
                        return -1;
                }
        }

        err = libbfd__read_build_id(fd, filename, bid);
        if (err == 0)
                goto out;

        err = libelf__read_build_id(fd, filename, bid);
        if (err == 0)
                goto out;

        err = sym_min__read_build_id(fd, filename, bid);
...
```

That is what was previously symbol-elf decompression code is now made
common for all cases of reading. The handling of the block argument is
now common. There is a clear fallback from libbfd to libelf to the
symbol-minimal (sym_min) versions of the functions.

To support this there is refactoring of code into a perf-libelf.c
file, to follow patterns established for the capstone, LLVM and libbfd
code.

These changes conflict with James' removal of the block argument:
https://lore.kernel.org/lkml/20251121-james-perf-non-block-v1-1-6ab2702ab573@linaro.org/
I think we should do that change. It would be easy to apply that
change on these changes as rather than updating 3 functions, only 1
function needs updating.

Ian Rogers (9):
  perf symbol: Reduce scope of elf__needs_adjust_symbols
  perf symbol: Reduce scope of arch__sym_update
  perf symbol: Move libelf code to its own file/header
  perf symbol: Remove unused includes
  perf symbol: Move dso__load_bfd_symbols out of symbol.h
  perf symbol: Use fallbacks with filename__read_build_id
  perf symbol: Use fallbacks for filename__read_debuglink
  perf symbol: Use fallbacks for sysfs__read_build_id
  perf dso: Move type helpers out of symbol and use fallbacks

 tools/perf/arch/powerpc/util/sym-handling.c   |   7 -
 tools/perf/builtin-annotate.c                 |  47 +--
 tools/perf/builtin-c2c.c                      |  58 +--
 tools/perf/builtin-diff.c                     |  22 +-
 tools/perf/builtin-inject.c                   |  19 +-
 tools/perf/builtin-kmem.c                     |  41 ++-
 tools/perf/builtin-kwork.c                    |   9 +-
 tools/perf/builtin-mem.c                      |  16 +-
 tools/perf/builtin-report.c                   |  76 ++--
 tools/perf/builtin-sched.c                    |  50 ++-
 tools/perf/builtin-script.c                   |  89 ++---
 tools/perf/builtin-timechart.c                |  34 +-
 tools/perf/builtin-top.c                      |  85 +++--
 tools/perf/builtin-trace.c                    |  82 ++---
 .../scripts/python/Perf-Trace-Util/Context.c  |  11 +-
 tools/perf/tests/code-reading.c               |   1 +
 tools/perf/tests/hists_cumulate.c             |   1 +
 tools/perf/tests/hists_filter.c               |   1 +
 tools/perf/tests/hists_link.c                 |   1 +
 tools/perf/tests/hists_output.c               |   1 +
 tools/perf/tests/mmap-thread-lookup.c         |   1 +
 tools/perf/tests/pe-file-parsing.c            |   5 +-
 tools/perf/util/Build                         |   6 +-
 tools/perf/util/annotate-data.c               |   7 +-
 tools/perf/util/auxtrace.h                    |   1 +
 tools/perf/util/block-info.c                  |  11 +-
 tools/perf/util/build-id.c                    |  28 +-
 tools/perf/util/callchain.c                   |   1 +
 tools/perf/util/cs-etm.c                      |   1 +
 tools/perf/util/data-convert-json.c           |  30 +-
 tools/perf/util/db-export.c                   |  16 +-
 tools/perf/util/dlfilter.c                    |  11 +-
 tools/perf/util/dlfilter.h                    |   1 +
 tools/perf/util/dso.c                         |  57 +--
 tools/perf/util/event.c                       |  41 ++-
 tools/perf/util/evsel_fprintf.c               |  14 +-
 tools/perf/util/hist.c                        |  29 +-
 tools/perf/util/intel-pt.c                    |  35 +-
 tools/perf/util/kvm-stat.h                    |   5 +-
 tools/perf/util/libbfd.c                      |   4 +-
 tools/perf/util/libbfd.h                      |  15 +-
 tools/perf/util/machine.c                     |  34 +-
 tools/perf/util/perf-libelf.c                 | 286 +++++++++++++++
 tools/perf/util/perf-libelf.h                 |  65 ++++
 tools/perf/util/print_insn.c                  |  14 +-
 tools/perf/util/probe-event.c                 |   1 +
 tools/perf/util/probe-finder.c                |   1 +
 .../util/scripting-engines/trace-event-perl.c |   1 +
 .../scripting-engines/trace-event-python.c    |   1 +
 tools/perf/util/symbol-elf.c                  | 337 +-----------------
 tools/perf/util/symbol-minimal.c              |  95 +++--
 tools/perf/util/symbol-minimal.h              |  13 +
 tools/perf/util/symbol.c                      | 108 +++++-
 tools/perf/util/symbol.h                      |  44 +--
 tools/perf/util/symbol_fprintf.c              |   1 +
 tools/perf/util/thread-stack.c                |  12 +-
 tools/perf/util/thread.c                      |  18 +-
 tools/perf/util/unwind-libdw.c                |  15 +-
 58 files changed, 1085 insertions(+), 931 deletions(-)
 create mode 100644 tools/perf/util/perf-libelf.c
 create mode 100644 tools/perf/util/perf-libelf.h
 create mode 100644 tools/perf/util/symbol-minimal.h

-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 1/9] perf symbol: Reduce scope of elf__needs_adjust_symbols
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 2/9] perf symbol: Reduce scope of arch__sym_update Ian Rogers
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Function is only used by symsrc__init in symbol-elf.c, make static to
reduce scope. Switch to not passing the argument by value but as a
pointer.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/symbol-elf.c | 8 ++++----
 tools/perf/util/symbol.h     | 1 -
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 9e820599bab3..5cd4844f9bb4 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1051,15 +1051,15 @@ void symsrc__destroy(struct symsrc *ss)
 	close(ss->fd);
 }
 
-bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
+static bool elf__needs_adjust_symbols(const GElf_Ehdr *ehdr)
 {
 	/*
 	 * Usually vmlinux is an ELF file with type ET_EXEC for most
 	 * architectures; except Arm64 kernel is linked with option
 	 * '-share', so need to check type ET_DYN.
 	 */
-	return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
-	       ehdr.e_type == ET_DYN;
+	return ehdr->e_type == ET_EXEC || ehdr->e_type == ET_REL ||
+	       ehdr->e_type == ET_DYN;
 }
 
 static Elf *read_gnu_debugdata(struct dso *dso, Elf *elf, const char *name, int *fd_ret)
@@ -1232,7 +1232,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 	if (dso__kernel(dso) == DSO_SPACE__USER)
 		ss->adjust_symbols = true;
 	else
-		ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
+		ss->adjust_symbols = elf__needs_adjust_symbols(&ehdr);
 
 	ss->name   = strdup(name);
 	if (!ss->name) {
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 347106218799..d91dc89386e2 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -217,7 +217,6 @@ int setup_intlist(struct intlist **list, const char *list_str,
 		  const char *list_name);
 
 #ifdef HAVE_LIBELF_SUPPORT
-bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
 void arch__sym_update(struct symbol *s, GElf_Sym *sym);
 #endif
 
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 2/9] perf symbol: Reduce scope of arch__sym_update
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 1/9] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 3/9] perf symbol: Move libelf code to its own file/header Ian Rogers
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Rather than being a weak arch function, use the e_machine from the ELF
header to determine whether to update or not. This should make the
function work cross-platform, it also reduces the function's scope.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/arch/powerpc/util/sym-handling.c | 7 -------
 tools/perf/util/symbol-elf.c                | 9 ++++++---
 tools/perf/util/symbol.h                    | 4 ----
 3 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/tools/perf/arch/powerpc/util/sym-handling.c b/tools/perf/arch/powerpc/util/sym-handling.c
index 947bfad7aa59..afefa9bd0c93 100644
--- a/tools/perf/arch/powerpc/util/sym-handling.c
+++ b/tools/perf/arch/powerpc/util/sym-handling.c
@@ -66,13 +66,6 @@ const char *arch__normalize_symbol_name(const char *name)
 
 #if defined(_CALL_ELF) && _CALL_ELF == 2
 
-#ifdef HAVE_LIBELF_SUPPORT
-void arch__sym_update(struct symbol *s, GElf_Sym *sym)
-{
-	s->arch_sym = sym->st_other;
-}
-#endif
-
 #define PPC64LE_LEP_OFFSET	8
 
 void arch__fix_tev_from_maps(struct perf_probe_event *pev,
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 5cd4844f9bb4..629b272e0a13 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1337,8 +1337,11 @@ static u64 ref_reloc(struct kmap *kmap)
 	return 0;
 }
 
-void __weak arch__sym_update(struct symbol *s __maybe_unused,
-		GElf_Sym *sym __maybe_unused) { }
+static void arch__sym_update(int e_machine, const GElf_Sym *sym, struct symbol *s)
+{
+	if (e_machine == EM_PPC64 || e_machine == EM_PPC)
+		s->arch_sym = sym->st_other;
+}
 
 static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
 				      GElf_Sym *sym, GElf_Shdr *shdr,
@@ -1717,7 +1720,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		if (!f)
 			goto out_elf_end;
 
-		arch__sym_update(f, &sym);
+		arch__sym_update(ehdr.e_machine, &sym, f);
 
 		__symbols__insert(dso__symbols(curr_dso), f, dso__kernel(dso));
 		nr++;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index d91dc89386e2..78fb2ba69f65 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -216,10 +216,6 @@ int setup_list(struct strlist **list, const char *list_str,
 int setup_intlist(struct intlist **list, const char *list_str,
 		  const char *list_name);
 
-#ifdef HAVE_LIBELF_SUPPORT
-void arch__sym_update(struct symbol *s, GElf_Sym *sym);
-#endif
-
 const char *arch__normalize_symbol_name(const char *name);
 #define SYMBOL_A 0
 #define SYMBOL_B 1
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 3/9] perf symbol: Move libelf code to its own file/header
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 1/9] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 2/9] perf symbol: Reduce scope of arch__sym_update Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 4/9] perf symbol: Remove unused includes Ian Rogers
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Move perf declarations depending on libelf out of symbol.h into
perf-libelf.h. Move the corresponding C code into perf-libelf.c. This
is motivated by trying to make it clearer the symbol.[ch] is generic
code without library dependencies.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/Build          |  1 +
 tools/perf/util/perf-libelf.c  | 32 ++++++++++++++++++++++++++++++++
 tools/perf/util/perf-libelf.h  | 25 +++++++++++++++++++++++++
 tools/perf/util/probe-event.c  |  1 +
 tools/perf/util/probe-finder.c |  1 +
 tools/perf/util/symbol-elf.c   | 27 +--------------------------
 tools/perf/util/symbol.h       | 19 -------------------
 7 files changed, 61 insertions(+), 45 deletions(-)
 create mode 100644 tools/perf/util/perf-libelf.c
 create mode 100644 tools/perf/util/perf-libelf.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1c2a43e1dc68..dcac6f1e1d99 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -193,6 +193,7 @@ ifeq ($(CONFIG_LIBTRACEEVENT),y)
   perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_kwork_top.o
 endif
 
+perf-util-$(CONFIG_LIBELF) += perf-libelf.o
 perf-util-$(CONFIG_LIBELF) += symbol-elf.o
 perf-util-$(CONFIG_LIBELF) += probe-file.o
 perf-util-$(CONFIG_LIBELF) += probe-event.o
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
new file mode 100644
index 000000000000..abd55bd7f14b
--- /dev/null
+++ b/tools/perf/util/perf-libelf.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "perf-libelf.h"
+
+#include <stddef.h>
+#include <string.h>
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
+			     GElf_Shdr *shp, const char *name, size_t *idx)
+{
+	Elf_Scn *sec = NULL;
+	size_t cnt = 1;
+
+	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
+	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
+		return NULL;
+
+	while ((sec = elf_nextscn(elf, sec)) != NULL) {
+		char *str;
+
+		gelf_getshdr(sec, shp);
+		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
+		if (str && !strcmp(name, str)) {
+			if (idx)
+				*idx = cnt;
+			return sec;
+		}
+		++cnt;
+	}
+
+	return NULL;
+}
+
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
new file mode 100644
index 000000000000..5d7d18daf5ff
--- /dev/null
+++ b/tools/perf/util/perf-libelf.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LIBELF_H
+#define __PERF_LIBELF_H
+
+#ifdef HAVE_LIBELF_SUPPORT
+
+#include <libelf.h>
+#include <gelf.h>
+
+/*
+ * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
+ * for newer versions we can use mmap to reduce memory usage:
+ */
+#ifdef ELF_C_READ_MMAP
+# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
+#else
+# define PERF_ELF_C_READ_MMAP ELF_C_READ
+#endif
+
+Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name,
+			     size_t *idx);
+
+#endif // defined(HAVE_LIBELF_SUPPORT)
+
+#endif /* __PERF_LIBELF_H */
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 6ab2eb551b6c..c27ad9121230 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -23,6 +23,7 @@
 #include "build-id.h"
 #include "event.h"
 #include "namespaces.h"
+#include "perf-libelf.h"
 #include "strlist.h"
 #include "strfilter.h"
 #include "debug.h"
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 5ffd97ee4898..f94f742a3458 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -25,6 +25,7 @@
 #include "debug.h"
 #include "debuginfo.h"
 #include "intlist.h"
+#include "perf-libelf.h"
 #include "strbuf.h"
 #include "strlist.h"
 #include "symbol.h"
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 629b272e0a13..750a30b216d6 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -12,6 +12,7 @@
 #include "libbfd.h"
 #include "map.h"
 #include "maps.h"
+#include "perf-libelf.h"
 #include "symbol.h"
 #include "symsrc.h"
 #include "machine.h"
@@ -183,32 +184,6 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
 	return -1;
 }
 
-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
-			     GElf_Shdr *shp, const char *name, size_t *idx)
-{
-	Elf_Scn *sec = NULL;
-	size_t cnt = 1;
-
-	/* ELF is corrupted/truncated, avoid calling elf_strptr. */
-	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
-		return NULL;
-
-	while ((sec = elf_nextscn(elf, sec)) != NULL) {
-		char *str;
-
-		gelf_getshdr(sec, shp);
-		str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
-		if (str && !strcmp(name, str)) {
-			if (idx)
-				*idx = cnt;
-			return sec;
-		}
-		++cnt;
-	}
-
-	return NULL;
-}
-
 bool filename__has_section(const char *filename, const char *sec)
 {
 	int fd;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 78fb2ba69f65..0aa8680cbd3e 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -14,10 +14,6 @@
 #include "symbol_conf.h"
 #include "spark.h"
 
-#ifdef HAVE_LIBELF_SUPPORT
-#include <libelf.h>
-#include <gelf.h>
-#endif
 #include <elf.h>
 
 struct dso;
@@ -26,21 +22,6 @@ struct maps;
 struct option;
 struct build_id;
 
-/*
- * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
- * for newer versions we can use mmap to reduce memory usage:
- */
-#ifdef ELF_C_READ_MMAP
-# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
-#else
-# define PERF_ELF_C_READ_MMAP ELF_C_READ
-#endif
-
-#ifdef HAVE_LIBELF_SUPPORT
-Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
-			     GElf_Shdr *shp, const char *name, size_t *idx);
-#endif
-
 /**
  * A symtab entry. When allocated this may be preceded by an annotation (see
  * symbol__annotation) and/or a browser_index (see symbol__browser_index).
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 4/9] perf symbol: Remove unused includes
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (2 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 3/9] perf symbol: Move libelf code to its own file/header Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 5/9] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Remove the includes of addr_location.h, spark.h, <elf.h>,
<linux/refcount.h> and <stdint.h> as they're not used in the
file. This caused an issue particularly with addr_location.h where
there were lots of transitive dependencies. Resolve those by adding
the include to the C file.

When adding the necessary includes, sort the includes and fix any
other transitive dependency issues. Also, fix paths to files in util,
erroneously in ../, etc.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-annotate.c                 | 47 +++++-----
 tools/perf/builtin-c2c.c                      | 58 ++++++------
 tools/perf/builtin-diff.c                     | 22 ++---
 tools/perf/builtin-inject.c                   | 19 ++--
 tools/perf/builtin-kmem.c                     | 41 +++++----
 tools/perf/builtin-kwork.c                    |  9 +-
 tools/perf/builtin-mem.c                      | 16 ++--
 tools/perf/builtin-report.c                   | 76 ++++++++--------
 tools/perf/builtin-sched.c                    | 50 +++++------
 tools/perf/builtin-script.c                   | 89 ++++++++++---------
 tools/perf/builtin-timechart.c                | 34 +++----
 tools/perf/builtin-top.c                      | 85 +++++++++---------
 tools/perf/builtin-trace.c                    | 82 ++++++++---------
 .../scripts/python/Perf-Trace-Util/Context.c  | 11 +--
 tools/perf/tests/code-reading.c               |  1 +
 tools/perf/tests/hists_cumulate.c             |  1 +
 tools/perf/tests/hists_filter.c               |  1 +
 tools/perf/tests/hists_link.c                 |  1 +
 tools/perf/tests/hists_output.c               |  1 +
 tools/perf/tests/mmap-thread-lookup.c         |  1 +
 tools/perf/util/annotate-data.c               |  7 +-
 tools/perf/util/auxtrace.h                    |  1 +
 tools/perf/util/block-info.c                  | 11 +--
 tools/perf/util/build-id.c                    | 28 +++---
 tools/perf/util/callchain.c                   |  1 +
 tools/perf/util/cs-etm.c                      |  1 +
 tools/perf/util/data-convert-json.c           | 30 ++++---
 tools/perf/util/db-export.c                   | 16 ++--
 tools/perf/util/dlfilter.c                    | 11 +--
 tools/perf/util/dlfilter.h                    |  1 +
 tools/perf/util/event.c                       | 41 +++++----
 tools/perf/util/evsel_fprintf.c               | 14 +--
 tools/perf/util/hist.c                        | 29 +++---
 tools/perf/util/intel-pt.c                    | 35 ++++----
 tools/perf/util/kvm-stat.h                    |  5 +-
 tools/perf/util/machine.c                     | 34 +++----
 tools/perf/util/print_insn.c                  | 14 +--
 .../util/scripting-engines/trace-event-perl.c |  1 +
 .../scripting-engines/trace-event-python.c    |  1 +
 tools/perf/util/symbol.h                      | 14 ++-
 tools/perf/util/symbol_fprintf.c              |  1 +
 tools/perf/util/thread-stack.c                | 12 +--
 tools/perf/util/thread.c                      | 18 ++--
 tools/perf/util/unwind-libdw.c                | 15 ++--
 44 files changed, 518 insertions(+), 468 deletions(-)

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 9c27bb30b708..2967dd085b82 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -6,45 +6,46 @@
  * look up and read DSOs and symbol information and display
  * a histogram of results, along various sorting keys.
  */
+
+#include <dlfcn.h>
+#include <errno.h>
+#include <inttypes.h>
+
 #include "builtin.h"
 #include "perf.h"
 
-#include "util/color.h"
-#include <linux/list.h>
+#include "arch/common.h"
+#include "ui/progress.h"
+#include "util/addr_location.h"
+#include "util/annotate-data.h"
+#include "util/annotate.h"
+#include "util/block-range.h"
+#include "util/branch.h"
 #include "util/cache.h"
-#include <linux/rbtree.h>
-#include <linux/zalloc.h>
-#include "util/symbol.h"
-
+#include "util/color.h"
+#include "util/data.h"
 #include "util/debug.h"
-
+#include "util/dso.h"
+#include "util/event.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
-#include "util/annotate.h"
-#include "util/annotate-data.h"
-#include "util/event.h"
-#include <subcmd/parse-options.h>
-#include "util/parse-events.h"
-#include "util/sort.h"
 #include "util/hist.h"
-#include "util/dso.h"
 #include "util/machine.h"
 #include "util/map.h"
+#include "util/map_symbol.h"
+#include "util/parse-events.h"
 #include "util/session.h"
+#include "util/sort.h"
+#include "util/symbol.h"
 #include "util/tool.h"
-#include "util/data.h"
-#include "arch/common.h"
-#include "util/block-range.h"
-#include "util/map_symbol.h"
-#include "util/branch.h"
 #include "util/util.h"
-#include "ui/progress.h"
 
-#include <dlfcn.h>
-#include <errno.h>
 #include <linux/bitmap.h>
 #include <linux/err.h>
-#include <inttypes.h>
+#include <linux/list.h>
+#include <linux/rbtree.h>
+#include <linux/zalloc.h>
+#include <subcmd/parse-options.h>
 
 struct perf_annotate {
 	struct perf_tool tool;
diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 14c3823f8fed..6cc0060cacca 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -12,41 +12,45 @@
  */
 #include <errno.h>
 #include <inttypes.h>
+#include <sys/param.h>
+
+#include "builtin.h"
+
+#include "ui/browsers/hists.h"
+#include "ui/progress.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/cacheline.h"
+#include "util/data.h"
+#include "util/debug.h"
+#include "util/event.h"
+#include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/hist.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
+#include "util/mem-info.h"
+#include "util/mem2node.h"
+#include "util/pmus.h"
+#include "util/session.h"
+#include "util/sort.h"
+#include "util/string2.h"
+#include "util/symbol.h"
+#include "util/symbol.h"
+#include "util/thread.h"
+#include "util/tool.h"
+#include "util/util.h"
+
+#include <asm/bug.h>
 #include <linux/compiler.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/stringify.h>
 #include <linux/zalloc.h>
-#include <asm/bug.h>
-#include <sys/param.h>
-#include "debug.h"
-#include "builtin.h"
 #include <perf/cpumap.h>
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
-#include "map_symbol.h"
-#include "mem-events.h"
-#include "session.h"
-#include "hist.h"
-#include "sort.h"
-#include "tool.h"
-#include "cacheline.h"
-#include "data.h"
-#include "event.h"
-#include "evlist.h"
-#include "evsel.h"
-#include "ui/browsers/hists.h"
-#include "thread.h"
-#include "mem2node.h"
-#include "mem-info.h"
-#include "symbol.h"
-#include "ui/ui.h"
-#include "ui/progress.h"
-#include "pmus.h"
-#include "string2.h"
-#include "util/util.h"
-#include "util/symbol.h"
-#include "util/annotate.h"
 
 struct c2c_hists {
 	struct hists		hists;
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 53d5ea4a6a4f..9fc8a18647d1 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -8,25 +8,27 @@
 #include "builtin.h"
 #include "perf.h"
 
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/block-info.h"
+#include "util/config.h"
+#include "util/data.h"
 #include "util/debug.h"
 #include "util/event.h"
-#include "util/hist.h"
-#include "util/evsel.h"
 #include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/hist.h"
+#include "util/map.h"
 #include "util/session.h"
-#include "util/tool.h"
 #include "util/sort.h"
+#include "util/spark.h"
 #include "util/srcline.h"
+#include "util/stream.h"
 #include "util/symbol.h"
-#include "util/data.h"
-#include "util/config.h"
 #include "util/time-utils.h"
-#include "util/annotate.h"
-#include "util/map.h"
-#include "util/spark.h"
-#include "util/block-info.h"
-#include "util/stream.h"
+#include "util/tool.h"
 #include "util/util.h"
+
 #include <linux/err.h>
 #include <linux/zalloc.h>
 #include <subcmd/pager.h>
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index bd9245d2dd41..4bc9a3303bcb 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -8,26 +8,27 @@
  */
 #include "builtin.h"
 
+#include "util/addr_location.h"
+#include "util/auxtrace.h"
+#include "util/build-id.h"
 #include "util/color.h"
+#include "util/data.h"
+#include "util/debug.h"
 #include "util/dso.h"
-#include "util/vdso.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
+#include "util/jit.h"
 #include "util/map.h"
+#include "util/namespaces.h"
 #include "util/session.h"
-#include "util/tool.h"
-#include "util/debug.h"
-#include "util/build-id.h"
-#include "util/data.h"
-#include "util/auxtrace.h"
-#include "util/jit.h"
 #include "util/string2.h"
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
 #include "util/thread.h"
-#include "util/namespaces.h"
-#include "util/util.h"
+#include "util/tool.h"
 #include "util/tsc.h"
+#include "util/util.h"
+#include "util/vdso.h"
 
 #include <internal/lib.h>
 
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 7929a5fa5f46..e9f5b3ff24e7 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -1,42 +1,41 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <inttypes.h>
+#include <locale.h>
+#include <regex.h>
+
 #include "builtin.h"
 
+#include "util/addr_location.h"
+#include "util/callchain.h"
+#include "util/config.h"
+#include "util/cpumap.h"
+#include "util/data.h"
+#include "util/debug.h"
 #include "util/dso.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
-#include "util/config.h"
+#include "util/header.h"
 #include "util/map.h"
+#include "util/session.h"
+#include "util/string2.h"
 #include "util/symbol.h"
 #include "util/thread.h"
-#include "util/header.h"
-#include "util/session.h"
-#include "util/tool.h"
-#include "util/callchain.h"
 #include "util/time-utils.h"
-#include <linux/err.h>
-
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
+#include "util/tool.h"
 #include "util/trace-event.h"
-#include "util/data.h"
-#include "util/cpumap.h"
-
-#include "util/debug.h"
-#include "util/string2.h"
 #include "util/util.h"
 
+#include <event-parse.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/numa.h>
 #include <linux/rbtree.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <locale.h>
-#include <regex.h>
-
-#include <linux/ctype.h>
-#include <event-parse.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 
 static int	kmem_slab;
 static int	kmem_page;
diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 7f3068264568..4a274c550cdf 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -8,18 +8,19 @@
 #include "builtin.h"
 #include "perf.h"
 
+#include "util/addr_location.h"
+#include "util/callchain.h"
 #include "util/data.h"
+#include "util/debug.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
+#include "util/evsel_fprintf.h"
 #include "util/header.h"
 #include "util/kwork.h"
-#include "util/debug.h"
 #include "util/session.h"
+#include "util/string2.h"
 #include "util/symbol.h"
 #include "util/thread.h"
-#include "util/string2.h"
-#include "util/callchain.h"
-#include "util/evsel_fprintf.h"
 #include "util/util.h"
 
 #include <subcmd/pager.h>
diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index d43500b92a7b..bad3784bd119 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -7,24 +7,26 @@
 #include "builtin.h"
 #include "perf.h"
 
-#include <subcmd/parse-options.h>
+#include "util/addr_location.h"
 #include "util/auxtrace.h"
-#include "util/trace-event.h"
-#include "util/tool.h"
-#include "util/session.h"
 #include "util/data.h"
-#include "util/map_symbol.h"
-#include "util/mem-events.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/map.h"
-#include "util/symbol.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
 #include "util/pmus.h"
 #include "util/sample.h"
+#include "util/session.h"
 #include "util/sort.h"
 #include "util/string2.h"
+#include "util/symbol.h"
+#include "util/tool.h"
+#include "util/trace-event.h"
 #include "util/util.h"
+
 #include <linux/err.h>
+#include <subcmd/parse-options.h>
 
 #define MEM_OPERATION_LOAD	0x1
 #define MEM_OPERATION_STORE	0x2
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 2bc269f5fcef..cdc7dd8d6320 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -6,68 +6,66 @@
  * look up and read DSOs and symbol information and display
  * a histogram of results, along various sorting keys.
  */
-#include "builtin.h"
+#include <dlfcn.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <regex.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
-#include "util/config.h"
+#include "builtin.h"
+#include "perf.h"
 
+#include "arch/common.h"
+#include "ui/progress.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
 #include "util/annotate.h"
-#include "util/color.h"
-#include "util/dso.h"
-#include <linux/list.h>
-#include <linux/rbtree.h>
-#include <linux/err.h>
-#include <linux/zalloc.h>
-#include "util/map.h"
-#include "util/symbol.h"
-#include "util/map_symbol.h"
-#include "util/mem-events.h"
+#include "util/auxtrace.h"
+#include "util/block-info.h"
 #include "util/branch.h"
 #include "util/callchain.h"
-#include "util/values.h"
-
-#include "perf.h"
+#include "util/color.h"
+#include "util/config.h"
+#include "util/data.h"
 #include "util/debug.h"
+#include "util/dso.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evswitch.h"
 #include "util/header.h"
+#include "util/hist.h"
+#include "util/map.h"
+#include "util/map_symbol.h"
+#include "util/mem-events.h"
 #include "util/mem-info.h"
+#include "util/parse-events.h"
 #include "util/session.h"
+#include "util/sort.h"
 #include "util/srcline.h"
-#include "util/tool.h"
-
-#include <subcmd/parse-options.h>
-#include <subcmd/exec-cmd.h>
-#include "util/parse-events.h"
-
+#include "util/symbol.h"
 #include "util/thread.h"
-#include "util/sort.h"
-#include "util/hist.h"
-#include "util/data.h"
-#include "arch/common.h"
 #include "util/time-utils.h"
-#include "util/auxtrace.h"
+#include "util/tool.h"
 #include "util/units.h"
 #include "util/util.h" // perf_tip()
-#include "ui/ui.h"
-#include "ui/progress.h"
-#include "util/block-info.h"
+#include "util/values.h"
 
-#include <dlfcn.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <regex.h>
-#include <linux/ctype.h>
-#include <signal.h>
 #include <linux/bitmap.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <linux/list.h>
 #include <linux/list_sort.h>
+#include <linux/mman.h>
+#include <linux/rbtree.h>
 #include <linux/string.h>
 #include <linux/stringify.h>
 #include <linux/time64.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <linux/mman.h>
+#include <linux/zalloc.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/parse-options.h>
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index eca3b1c58c4b..bc9e098642df 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1,51 +1,49 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <inttypes.h>
+#include <errno.h>
+#include <semaphore.h>
+#include <pthread.h>
+#include <math.h>
+
 #include "builtin.h"
 #include "perf.h"
 #include "perf-sys.h"
 
+#include "util/addr_location.h"
+#include "util/callchain.h"
+#include "util/cloexec.h"
+#include "util/color.h"
 #include "util/cpumap.h"
+#include "util/debug.h"
+#include "util/event.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evsel_fprintf.h"
-#include "util/mutex.h"
-#include "util/symbol.h"
-#include "util/thread.h"
 #include "util/header.h"
+#include "util/mutex.h"
 #include "util/session.h"
-#include "util/tool.h"
-#include "util/cloexec.h"
-#include "util/thread_map.h"
-#include "util/color.h"
 #include "util/stat.h"
 #include "util/string2.h"
-#include "util/callchain.h"
+#include "util/symbol.h"
+#include "util/thread.h"
+#include "util/thread_map.h"
 #include "util/time-utils.h"
-
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
+#include "util/tool.h"
 #include "util/trace-event.h"
-
-#include "util/debug.h"
-#include "util/event.h"
 #include "util/util.h"
 
+#include <api/fs/fs.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/log2.h>
+#include <linux/time64.h>
 #include <linux/zalloc.h>
+#include <perf/cpumap.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 #include <sys/prctl.h>
 #include <sys/resource.h>
-#include <inttypes.h>
-
-#include <errno.h>
-#include <semaphore.h>
-#include <pthread.h>
-#include <math.h>
-#include <api/fs/fs.h>
-#include <perf/cpumap.h>
-#include <linux/time64.h>
-#include <linux/err.h>
-
-#include <linux/ctype.h>
 
 #define PR_SET_NAME		15               /* Set process name */
 #define MAX_CPUS		4096
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 011962e1ee0f..ba363884a79d 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -1,73 +1,76 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+
 #include "builtin.h"
+#include "perf.h"
 
+#include "ui/ui.h"
+#include "util/addr_location.h"
+#include "util/annotate.h"
+#include "util/archinsn.h"
+#include "util/auxtrace.h"
+#include "util/cgroup.h"
+#include "util/color.h"
 #include "util/counts.h"
+#include "util/cpumap.h"
+#include "util/data.h"
 #include "util/debug.h"
+#include "util/dlfilter.h"
 #include "util/dso.h"
-#include <subcmd/exec-cmd.h>
-#include "util/header.h"
-#include <subcmd/parse-options.h>
-#include "util/perf_regs.h"
-#include "util/session.h"
-#include "util/tool.h"
-#include "util/map.h"
-#include "util/srcline.h"
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/trace-event.h"
+#include "util/dump-insn.h"
 #include "util/env.h"
+#include "util/event.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evsel_fprintf.h"
 #include "util/evswitch.h"
+#include "util/header.h"
+#include "util/map.h"
+#include "util/mem-events.h"
+#include "util/mem-info.h"
+#include "util/metricgroup.h"
+#include "util/path.h"
+#include "util/perf_regs.h"
+#include "util/print_binary.h"
+#include "util/print_insn.h"
+#include "util/record.h"
+#include "util/session.h"
 #include "util/sort.h"
-#include "util/data.h"
-#include "util/auxtrace.h"
-#include "util/cpumap.h"
-#include "util/thread_map.h"
+#include "util/srcline.h"
 #include "util/stat.h"
-#include "util/color.h"
 #include "util/string2.h"
+#include "util/symbol.h"
 #include "util/thread-stack.h"
+#include "util/thread.h"
+#include "util/thread_map.h"
 #include "util/time-utils.h"
-#include "util/path.h"
-#include "util/event.h"
-#include "util/mem-info.h"
-#include "util/metricgroup.h"
-#include "ui/ui.h"
-#include "print_binary.h"
-#include "print_insn.h"
-#include "archinsn.h"
+#include "util/tool.h"
+#include "util/trace-event.h"
+#include "util/util.h"
+
+#include <asm/bug.h>
 #include <linux/bitmap.h>
 #include <linux/compiler.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/stringify.h>
 #include <linux/time64.h>
-#include <linux/zalloc.h>
 #include <linux/unaligned.h>
-#include <sys/utsname.h>
-#include "asm/bug.h"
-#include "util/mem-events.h"
-#include "util/dump-insn.h"
-#include <dirent.h>
-#include <errno.h>
-#include <inttypes.h>
+#include <linux/zalloc.h>
+#include <perf/evlist.h>
 #include <signal.h>
 #include <stdio.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 #include <sys/param.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
 #include <unistd.h>
-#include <subcmd/pager.h>
-#include <perf/evlist.h>
-#include <linux/err.h>
-#include "util/dlfilter.h"
-#include "util/record.h"
-#include "util/util.h"
-#include "util/cgroup.h"
-#include "util/annotate.h"
-#include "perf.h"
 
 #include <linux/ctype.h>
 #ifdef HAVE_LIBTRACEEVENT
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 22050c640dfa..3f464df723d4 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -12,33 +12,35 @@
 #include <inttypes.h>
 
 #include "builtin.h"
+
+#include "util/addr_location.h"
+#include "util/callchain.h"
 #include "util/color.h"
-#include <linux/list.h>
+#include "util/data.h"
+#include "util/debug.h"
+#include "util/event.h"
 #include "util/evlist.h" // for struct evsel_str_handler
 #include "util/evsel.h"
-#include <linux/kernel.h>
-#include <linux/rbtree.h>
-#include <linux/time64.h>
-#include <linux/zalloc.h>
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/callchain.h"
-
 #include "util/header.h"
-#include <subcmd/pager.h>
-#include <subcmd/parse-options.h>
 #include "util/parse-events.h"
-#include "util/event.h"
 #include "util/session.h"
+#include "util/string2.h"
 #include "util/svghelper.h"
+#include "util/symbol.h"
+#include "util/thread.h"
 #include "util/tool.h"
-#include "util/data.h"
-#include "util/debug.h"
-#include "util/string2.h"
 #include "util/tracepoint.h"
 #include "util/util.h"
-#include <linux/err.h>
+
 #include <event-parse.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/rbtree.h>
+#include <linux/time64.h>
+#include <linux/zalloc.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 
 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
 FILE *open_memstream(char **ptr, size_t *sizeloc);
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 710604c4f6f6..60cb08783457 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -16,77 +16,72 @@
  *   Mike Galbraith <efault@gmx.de>
  *   Paul Mackerras <paulus@samba.org>
  */
-#include "builtin.h"
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <poll.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <sys/uio.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
 
+#include "builtin.h"
 #include "perf.h"
 
+#include "arch/common.h"
+#include "ui/ui.h"
+#include "util/addr_location.h"
 #include "util/annotate.h"
 #include "util/bpf-event.h"
+#include "util/callchain.h"
 #include "util/cgroup.h"
-#include "util/config.h"
 #include "util/color.h"
+#include "util/config.h"
+#include "util/cpumap.h"
+#include "util/debug.h"
 #include "util/dso.h"
+#include "util/event.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evsel_config.h"
-#include "util/event.h"
+#include "util/intlist.h"
 #include "util/machine.h"
 #include "util/map.h"
 #include "util/mmap.h"
+#include "util/ordered-events.h"
+#include "util/parse-branch-options.h"
+#include "util/parse-events.h"
+#include "util/pfm.h"
 #include "util/session.h"
-#include "util/thread.h"
+#include "util/sort.h"
 #include "util/stat.h"
+#include "util/string2.h"
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
+#include "util/term.h"
+#include "util/thread.h"
 #include "util/top.h"
 #include "util/util.h"
-#include <linux/rbtree.h>
-#include <subcmd/parse-options.h>
-#include "util/parse-events.h"
-#include "util/callchain.h"
-#include "util/cpumap.h"
-#include "util/sort.h"
-#include "util/string2.h"
-#include "util/term.h"
-#include "util/intlist.h"
-#include "util/parse-branch-options.h"
-#include "arch/common.h"
-#include "ui/ui.h"
 
-#include "util/debug.h"
-#include "util/ordered-events.h"
-#include "util/pfm.h"
-
-#include <assert.h>
 #include <elf.h>
-#include <fcntl.h>
-
-#include <stdio.h>
-#include <termios.h>
-#include <unistd.h>
-#include <inttypes.h>
-
-#include <errno.h>
-#include <time.h>
-#include <sched.h>
-#include <signal.h>
-
-#include <sys/syscall.h>
-#include <sys/ioctl.h>
-#include <poll.h>
-#include <sys/prctl.h>
-#include <sys/wait.h>
-#include <sys/uio.h>
-#include <sys/utsname.h>
-#include <sys/mman.h>
-
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <linux/rbtree.h>
 #include <linux/stringify.h>
 #include <linux/time64.h>
 #include <linux/types.h>
-#include <linux/err.h>
-
-#include <linux/ctype.h>
 #include <perf/mmap.h>
+#include <subcmd/parse-options.h>
 
 static volatile sig_atomic_t done;
 static volatile sig_atomic_t resize;
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index a743bda294bd..f4791b9b8b3b 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -13,17 +13,22 @@
  *
  * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
  */
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/sysmacros.h>
 
-#include "util/record.h"
-#include <api/fs/tracing_path.h>
-#ifdef HAVE_LIBBPF_SUPPORT
-#include <bpf/bpf.h>
-#include <bpf/libbpf.h>
-#include <bpf/btf.h>
-#endif
-#include "util/bpf_map.h"
-#include "util/rlimit.h"
 #include "builtin.h"
+#include "perf.h"
+
+#include "trace/beauty/beauty.h"
+#include "util/addr_location.h"
+#include "util/bpf_map.h"
+#include "util/callchain.h"
 #include "util/cgroup.h"
 #include "util/color.h"
 #include "util/config.h"
@@ -31,47 +36,39 @@
 #include "util/dso.h"
 #include "util/env.h"
 #include "util/event.h"
+#include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/evsel_fprintf.h"
-#include "util/synthetic-events.h"
-#include "util/evlist.h"
 #include "util/evswitch.h"
 #include "util/hashmap.h"
-#include "util/mmap.h"
-#include <subcmd/pager.h>
-#include <subcmd/exec-cmd.h>
+#include "util/include/dwarf-regs.h"
+#include "util/intlist.h"
 #include "util/machine.h"
 #include "util/map.h"
-#include "util/symbol.h"
+#include "util/mmap.h"
+#include "util/parse-events.h"
 #include "util/path.h"
+#include "util/print_binary.h"
+#include "util/record.h"
+#include "util/rlimit.h"
 #include "util/session.h"
-#include "util/thread.h"
-#include <subcmd/parse-options.h>
+#include "util/stat.h"
+#include "util/string2.h"
 #include "util/strlist.h"
-#include "util/intlist.h"
+#include "util/symbol.h"
+#include "util/synthetic-events.h"
+#include "util/syscalltbl.h"
+#include "util/thread.h"
 #include "util/thread_map.h"
-#include "util/stat.h"
 #include "util/tool.h"
+#include "util/trace-event.h"
 #include "util/trace.h"
-#include "util/util.h"
-#include "trace/beauty/beauty.h"
-#include "trace-event.h"
-#include "util/parse-events.h"
+#include "util/trace_augment.h"
 #include "util/tracepoint.h"
-#include "callchain.h"
-#include "print_binary.h"
-#include "string2.h"
-#include "syscalltbl.h"
-#include "../perf.h"
-#include "trace_augment.h"
-#include "dwarf-regs.h"
+#include "util/util.h"
 
-#include <errno.h>
-#include <inttypes.h>
-#include <poll.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
+#include <api/fs/tracing_path.h>
+#include <linux/ctype.h>
 #include <linux/err.h>
 #include <linux/filter.h>
 #include <linux/kernel.h>
@@ -80,13 +77,18 @@
 #include <linux/stringify.h>
 #include <linux/time64.h>
 #include <linux/zalloc.h>
-#include <fcntl.h>
-#include <sys/sysmacros.h>
-
-#include <linux/ctype.h>
 #include <perf/mmap.h>
+#include <subcmd/exec-cmd.h>
+#include <subcmd/pager.h>
+#include <subcmd/parse-options.h>
 #include <tools/libc_compat.h>
 
+#ifdef HAVE_LIBBPF_SUPPORT
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+#include <bpf/btf.h>
+#endif
+
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
 #endif
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
index 60dcfe56d4d9..aeafcc71cb08 100644
--- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c
+++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
@@ -12,17 +12,18 @@
 #define PY_SSIZE_T_CLEAN
 
 #include <Python.h>
+#include "../../../util/addr_location.h"
+#include "../../../util/auxtrace.h"
 #include "../../../util/config.h"
-#include "../../../util/trace-event.h"
 #include "../../../util/event.h"
-#include "../../../util/symbol.h"
-#include "../../../util/thread.h"
 #include "../../../util/map.h"
 #include "../../../util/maps.h"
-#include "../../../util/auxtrace.h"
 #include "../../../util/session.h"
-#include "../../../util/srcline.h"
 #include "../../../util/srccode.h"
+#include "../../../util/srcline.h"
+#include "../../../util/symbol.h"
+#include "../../../util/thread.h"
+#include "../../../util/trace-event.h"
 
 #define _PyCapsule_GetPointer(arg1, arg2) \
   PyCapsule_GetPointer((arg1), (arg2))
diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
index 5927d1ea20e2..cff0ecb0a805 100644
--- a/tools/perf/tests/code-reading.c
+++ b/tools/perf/tests/code-reading.c
@@ -15,6 +15,7 @@
 #include <perf/evlist.h>
 #include <perf/mmap.h>
 
+#include "addr_location.h"
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
diff --git a/tools/perf/tests/hists_cumulate.c b/tools/perf/tests/hists_cumulate.c
index 3eb9ef8d7ec6..c0931f639fb0 100644
--- a/tools/perf/tests/hists_cumulate.c
+++ b/tools/perf/tests/hists_cumulate.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/event.h"
diff --git a/tools/perf/tests/hists_filter.c b/tools/perf/tests/hists_filter.c
index 1cebd20cc91c..24f98c874ba6 100644
--- a/tools/perf/tests/hists_filter.c
+++ b/tools/perf/tests/hists_filter.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/map.h"
 #include "util/symbol.h"
diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c
index 996f5f0b3bd1..04197fcdfa9b 100644
--- a/tools/perf/tests/hists_link.c
+++ b/tools/perf/tests/hists_link.c
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "tests.h"
+#include "addr_location.h"
 #include "debug.h"
 #include "symbol.h"
 #include "sort.h"
diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_output.c
index ee5ec8bda60e..5e121f2181de 100644
--- a/tools/perf/tests/hists_output.c
+++ b/tools/perf/tests/hists_output.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/addr_location.h"
 #include "util/debug.h"
 #include "util/dso.h"
 #include "util/event.h"
diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-thread-lookup.c
index 0c5619c6e6e9..f39dc0b28810 100644
--- a/tools/perf/tests/mmap-thread-lookup.c
+++ b/tools/perf/tests/mmap-thread-lookup.c
@@ -7,6 +7,7 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include "addr_location.h"
 #include "debug.h"
 #include "env.h"
 #include "event.h"
diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c
index 07cf9c334be0..85ab4330da0b 100644
--- a/tools/perf/util/annotate-data.c
+++ b/tools/perf/util/annotate-data.c
@@ -10,14 +10,15 @@
 #include <inttypes.h>
 #include <linux/zalloc.h>
 
-#include "annotate.h"
+#include "addr_location.h"
 #include "annotate-data.h"
-#include "debuginfo.h"
+#include "annotate.h"
 #include "debug.h"
+#include "debuginfo.h"
 #include "dso.h"
 #include "dwarf-regs.h"
-#include "evsel.h"
 #include "evlist.h"
+#include "evsel.h"
 #include "map.h"
 #include "map_symbol.h"
 #include "sort.h"
diff --git a/tools/perf/util/auxtrace.h b/tools/perf/util/auxtrace.h
index eee2c11f7666..a07e9b6ab24d 100644
--- a/tools/perf/util/auxtrace.h
+++ b/tools/perf/util/auxtrace.h
@@ -8,6 +8,7 @@
 #define __PERF_AUXTRACE_H
 
 #include <stdio.h> // FILE
+#include <unistd.h> // pid_t
 #include <linux/perf_event.h>
 #include <linux/types.h>
 #include <asm/barrier.h>
diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
index 649392bee7ed..15e42e9fb2e8 100644
--- a/tools/perf/util/block-info.c
+++ b/tools/perf/util/block-info.c
@@ -2,15 +2,16 @@
 #include <stdlib.h>
 #include <string.h>
 #include <linux/zalloc.h>
-#include "block-info.h"
-#include "sort.h"
+#include "addr_location.h"
 #include "annotate.h"
-#include "symbol.h"
+#include "block-info.h"
 #include "dso.h"
-#include "map.h"
-#include "srcline.h"
 #include "evlist.h"
 #include "hist.h"
+#include "map.h"
+#include "sort.h"
+#include "srcline.h"
+#include "symbol.h"
 #include "ui/browsers/hists.h"
 
 static struct block_header_column {
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 35505a1ffd11..f1cadf037d46 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -7,38 +7,40 @@
  * Copyright (C) 2009, 2010 Red Hat Inc.
  * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
  */
-#include "util.h" // lsdir(), mkdir_p(), rm_rf()
 #include <dirent.h>
 #include <errno.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include "util/copyfile.h"
-#include "dso.h"
+
+#include "addr_location.h"
 #include "build-id.h"
+#include "copyfile.h"
+#include "debug.h"
+#include "dso.h"
 #include "event.h"
-#include "namespaces.h"
+#include "header.h"
 #include "map.h"
+#include "namespaces.h"
+#include "path.h"
+#include "probe-file.h"
+#include "session.h"
+#include "strlist.h"
 #include "symbol.h"
 #include "thread.h"
-#include <linux/kernel.h>
-#include "debug.h"
-#include "session.h"
 #include "tool.h"
-#include "header.h"
+#include "util.h" // lsdir(), mkdir_p(), rm_rf()
 #include "vdso.h"
-#include "path.h"
-#include "probe-file.h"
-#include "strlist.h"
 
 #ifdef HAVE_DEBUGINFOD_SUPPORT
 #include <elfutils/debuginfod.h>
 #endif
 
+#include <asm/bug.h>
 #include <linux/ctype.h>
-#include <linux/zalloc.h>
+#include <linux/kernel.h>
 #include <linux/string.h>
-#include <asm/bug.h>
+#include <linux/zalloc.h>
 
 static bool no_buildid_cache;
 
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index d7b7eef740b9..9c295b38d1e1 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -21,6 +21,7 @@
 
 #include "asm/bug.h"
 
+#include "addr_location.h"
 #include "debug.h"
 #include "dso.h"
 #include "event.h"
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 25d56e0f1c07..cb499a81625a 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -17,6 +17,7 @@
 
 #include <stdlib.h>
 
+#include "addr_location.h"
 #include "auxtrace.h"
 #include "color.h"
 #include "cs-etm.h"
diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index 9dc1e184cf3c..aec1302e599b 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -12,20 +12,22 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "linux/compiler.h"
-#include "linux/err.h"
-#include "util/auxtrace.h"
-#include "util/debug.h"
-#include "util/dso.h"
-#include "util/event.h"
-#include "util/evsel.h"
-#include "util/evlist.h"
-#include "util/header.h"
-#include "util/map.h"
-#include "util/session.h"
-#include "util/symbol.h"
-#include "util/thread.h"
-#include "util/tool.h"
+#include "addr_location.h"
+#include "auxtrace.h"
+#include "debug.h"
+#include "dso.h"
+#include "event.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "header.h"
+#include "map.h"
+#include "session.h"
+#include "symbol.h"
+#include "thread.h"
+#include "tool.h"
+
+#include <linux/compiler.h>
+#include <linux/err.h>
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
index 8f52e8cefcf3..5c23b1b2eafb 100644
--- a/tools/perf/util/db-export.c
+++ b/tools/perf/util/db-export.c
@@ -7,18 +7,20 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include "addr_location.h"
+#include "call-path.h"
+#include "callchain.h"
+#include "comm.h"
+#include "db-export.h"
 #include "dso.h"
+#include "event.h"
 #include "evsel.h"
 #include "machine.h"
-#include "thread.h"
-#include "comm.h"
-#include "symbol.h"
 #include "map.h"
-#include "event.h"
+#include "symbol.h"
 #include "thread-stack.h"
-#include "callchain.h"
-#include "call-path.h"
-#include "db-export.h"
+#include "thread.h"
+
 #include <linux/zalloc.h>
 
 int db_export__init(struct db_export *dbe)
diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
index c0afcbd954f8..70e322de711f 100644
--- a/tools/perf/util/dlfilter.c
+++ b/tools/perf/util/dlfilter.c
@@ -13,17 +13,18 @@
 #include <linux/kernel.h>
 #include <linux/string.h>
 
+#include "../include/perf/perf_dlfilter.h"
+#include "addr_location.h"
 #include "debug.h"
+#include "dlfilter.h"
+#include "dso.h"
 #include "event.h"
 #include "evsel.h"
-#include "dso.h"
 #include "map.h"
+#include "srcline.h"
+#include "symbol.h"
 #include "thread.h"
 #include "trace-event.h"
-#include "symbol.h"
-#include "srcline.h"
-#include "dlfilter.h"
-#include "../include/perf/perf_dlfilter.h"
 
 static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
 {
diff --git a/tools/perf/util/dlfilter.h b/tools/perf/util/dlfilter.h
index cc4bb9657d05..522d8e707db0 100644
--- a/tools/perf/util/dlfilter.h
+++ b/tools/perf/util/dlfilter.h
@@ -13,6 +13,7 @@ struct perf_sample;
 struct evsel;
 struct machine;
 struct addr_location;
+struct option;
 struct perf_dlfilter_fns;
 struct perf_dlfilter_sample;
 struct perf_dlfilter_al;
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index fcf44149feb2..f4a3f76edad2 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -1,42 +1,45 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
-#include <linux/compiler.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <perf/cpumap.h>
-#include <perf/event.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
-#include <linux/perf_event.h>
-#include <linux/zalloc.h>
+
+#include "addr_location.h"
+#include "bpf-event.h"
 #include "cpumap.h"
+#include "debug.h"
 #include "dso.h"
 #include "event.h"
-#include "debug.h"
 #include "hist.h"
 #include "machine.h"
+#include "map.h"
+#include "print_binary.h"
+#include "session.h"
 #include "sort.h"
+#include "stat.h"
 #include "string2.h"
 #include "strlist.h"
+#include "symbol.h"
+#include "symbol/kallsyms.h"
 #include "thread.h"
 #include "thread_map.h"
 #include "time-utils.h"
-#include <linux/ctype.h>
-#include "map.h"
-#include "util/namespaces.h"
-#include "symbol.h"
-#include "symbol/kallsyms.h"
-#include "asm/bug.h"
-#include "stat.h"
-#include "session.h"
-#include "bpf-event.h"
-#include "print_binary.h"
 #include "tool.h"
 #include "util.h"
+#include "namespaces.h"
+
+#include <asm/bug.h>
+#include <linux/compiler.h>
+#include <linux/ctype.h>
+#include <linux/kernel.h>
+#include <linux/perf_event.h>
+#include <linux/types.h>
+#include <linux/zalloc.h>
+#include <perf/cpumap.h>
+#include <perf/event.h>
+#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
 
 static const char *perf_event__names[] = {
 	[0]					= "TOTAL",
diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprintf.c
index 103984b29b1e..abdd9985c4e3 100644
--- a/tools/perf/util/evsel_fprintf.c
+++ b/tools/perf/util/evsel_fprintf.c
@@ -2,16 +2,18 @@
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdbool.h>
-#include "util/evlist.h"
-#include "evsel.h"
-#include "util/evsel_fprintf.h"
-#include "util/event.h"
+
+#include "addr_location.h"
 #include "callchain.h"
+#include "dso.h"
+#include "event.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "evsel_fprintf.h"
 #include "map.h"
+#include "srcline.h"
 #include "strlist.h"
 #include "symbol.h"
-#include "srcline.h"
-#include "dso.h"
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 64ff427040c3..bd71e48c1183 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1,32 +1,35 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <math.h>
+#include <inttypes.h>
+#include <sys/param.h>
+
+#include "addr_location.h"
+#include "annotate.h"
+#include "block-info.h"
+#include "branch.h"
+#include "build-id.h"
 #include "callchain.h"
+#include "cgroup.h"
 #include "debug.h"
 #include "dso.h"
-#include "build-id.h"
+#include "evlist.h"
+#include "evsel.h"
 #include "hist.h"
 #include "kvm-stat.h"
 #include "map.h"
 #include "map_symbol.h"
-#include "branch.h"
 #include "mem-events.h"
 #include "mem-info.h"
-#include "session.h"
 #include "namespaces.h"
-#include "cgroup.h"
+#include "session.h"
 #include "sort.h"
-#include "units.h"
-#include "evlist.h"
-#include "evsel.h"
-#include "annotate.h"
 #include "srcline.h"
 #include "symbol.h"
 #include "thread.h"
-#include "block-info.h"
 #include "ui/progress.h"
-#include <errno.h>
-#include <math.h>
-#include <inttypes.h>
-#include <sys/param.h>
+#include "units.h"
+
 #include <linux/rbtree.h>
 #include <linux/string.h>
 #include <linux/time64.h>
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index fc9eec8b54b8..244749c89770 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -14,29 +14,30 @@
 #include <linux/types.h>
 #include <linux/zalloc.h>
 
-#include "session.h"
-#include "machine.h"
-#include "memswap.h"
-#include "sort.h"
-#include "tool.h"
+#include "addr_location.h"
+#include "auxtrace.h"
+#include "callchain.h"
+#include "color.h"
+#include "config.h"
+#include "debug.h"
+#include "dso.h"
 #include "event.h"
 #include "evlist.h"
 #include "evsel.h"
+#include "intel-pt.h"
+#include "machine.h"
 #include "map.h"
-#include "color.h"
-#include "thread.h"
-#include "thread-stack.h"
+#include "memswap.h"
+#include "perf_api_probe.h"
+#include "session.h"
+#include "sort.h"
 #include "symbol.h"
-#include "callchain.h"
-#include "dso.h"
-#include "debug.h"
-#include "auxtrace.h"
-#include "tsc.h"
-#include "intel-pt.h"
-#include "config.h"
-#include "util/perf_api_probe.h"
-#include "util/synthetic-events.h"
+#include "synthetic-events.h"
+#include "thread-stack.h"
+#include "thread.h"
 #include "time-utils.h"
+#include "tool.h"
+#include "tsc.h"
 
 #include "../arch/x86/include/uapi/asm/perf_regs.h"
 
diff --git a/tools/perf/util/kvm-stat.h b/tools/perf/util/kvm-stat.h
index a356b839c2ee..7b63b1ba1b8a 100644
--- a/tools/perf/util/kvm-stat.h
+++ b/tools/perf/util/kvm-stat.h
@@ -4,11 +4,12 @@
 
 #ifdef HAVE_KVM_STAT_SUPPORT
 
-#include "tool.h"
+#include "addr_location.h"
+#include "record.h"
 #include "sort.h"
 #include "stat.h"
 #include "symbol.h"
-#include "record.h"
+#include "tool.h"
 
 #include <errno.h>
 #include <stdlib.h>
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index b5dd42588c91..db8052443259 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -3,8 +3,19 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <regex.h>
+#include <stdbool.h>
 #include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "addr_location.h"
+#include "arm64-frame-pointer-unwind-support.h"
+#include "asm/bug.h"
+#include "bpf-event.h"
+#include "branch.h"
 #include "callchain.h"
+#include "cgroup.h"
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
@@ -14,37 +25,28 @@
 #include "machine.h"
 #include "map.h"
 #include "map_symbol.h"
-#include "branch.h"
 #include "mem-events.h"
 #include "mem-info.h"
 #include "path.h"
+#include "sort.h"
 #include "srcline.h"
+#include "strlist.h"
 #include "symbol.h"
 #include "synthetic-events.h"
-#include "sort.h"
-#include "strlist.h"
 #include "target.h"
 #include "thread.h"
+#include "unwind.h"
 #include "util.h"
 #include "vdso.h"
-#include <stdbool.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include "unwind.h"
-#include "linux/hash.h"
-#include "asm/bug.h"
-#include "bpf-event.h"
-#include <internal/lib.h> // page_size
-#include "cgroup.h"
-#include "arm64-frame-pointer-unwind-support.h"
-#include <api/io_dir.h>
 
+#include <api/io_dir.h>
+#include <internal/lib.h> // page_size
 #include <linux/ctype.h>
-#include <symbol/kallsyms.h>
+#include <linux/hash.h>
 #include <linux/mman.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
+#include <symbol/kallsyms.h>
 
 static struct dso *machine__kernel_dso(struct machine *machine)
 {
diff --git a/tools/perf/util/print_insn.c b/tools/perf/util/print_insn.c
index 02e6fbb8ca04..07f9c9f6e244 100644
--- a/tools/perf/util/print_insn.c
+++ b/tools/perf/util/print_insn.c
@@ -5,18 +5,20 @@
  * Author(s): Changbin Du <changbin.du@huawei.com>
  */
 #include <inttypes.h>
-#include <string.h>
 #include <stdbool.h>
+#include <string.h>
+
+#include "addr_location.h"
 #include "capstone.h"
 #include "debug.h"
+#include "dso.h"
+#include "dump-insn.h"
+#include "machine.h"
+#include "map.h"
+#include "print_insn.h"
 #include "sample.h"
 #include "symbol.h"
-#include "machine.h"
 #include "thread.h"
-#include "print_insn.h"
-#include "dump-insn.h"
-#include "map.h"
-#include "dso.h"
 
 size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)
 {
diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c
index e261a57b87d4..9116ffa252b4 100644
--- a/tools/perf/util/scripting-engines/trace-event-perl.c
+++ b/tools/perf/util/scripting-engines/trace-event-perl.c
@@ -35,6 +35,7 @@
 #include <EXTERN.h>
 #include <perl.h>
 
+#include "../addr_location.h"
 #include "../callchain.h"
 #include "../dso.h"
 #include "../machine.h"
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 6655c0bbe0d8..aacb4dc3cb90 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -34,6 +34,7 @@
 #include <event-parse.h>
 #endif
 
+#include "../addr_location.h"
 #include "../build-id.h"
 #include "../counts.h"
 #include "../debug.h"
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 0aa8680cbd3e..42d8e3fd1d01 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -2,20 +2,18 @@
 #ifndef __PERF_SYMBOL
 #define __PERF_SYMBOL 1
 
-#include <linux/types.h>
-#include <linux/refcount.h>
 #include <stdbool.h>
-#include <stdint.h>
+#include <stdio.h>
+#include <elf.h>
+
+#include <linux/types.h>
 #include <linux/list.h>
 #include <linux/rbtree.h>
-#include <stdio.h>
-#include "addr_location.h"
+
 #include "path.h"
 #include "symbol_conf.h"
-#include "spark.h"
-
-#include <elf.h>
 
+struct addr_location;
 struct dso;
 struct map;
 struct maps;
diff --git a/tools/perf/util/symbol_fprintf.c b/tools/perf/util/symbol_fprintf.c
index 53e1af4ed9ac..59aa97c0a997 100644
--- a/tools/perf/util/symbol_fprintf.c
+++ b/tools/perf/util/symbol_fprintf.c
@@ -3,6 +3,7 @@
 #include <inttypes.h>
 #include <stdio.h>
 
+#include "addr_location.h"
 #include "dso.h"
 #include "map.h"
 #include "symbol.h"
diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
index c6a0a27b12c2..0d0cbc1250e9 100644
--- a/tools/perf/util/thread-stack.c
+++ b/tools/perf/util/thread-stack.c
@@ -11,15 +11,17 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include "thread.h"
+
+#include "addr_location.h"
+#include "call-path.h"
+#include "comm.h"
+#include "debug.h"
+#include "env.h"
 #include "event.h"
 #include "machine.h"
-#include "env.h"
-#include "debug.h"
 #include "symbol.h"
-#include "comm.h"
-#include "call-path.h"
 #include "thread-stack.h"
+#include "thread.h"
 
 #define STACK_GROWTH 2048
 
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index aa9c58bbf9d3..bf051074d66c 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -7,18 +7,20 @@
 #include <string.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
-#include "dso.h"
-#include "session.h"
-#include "thread.h"
-#include "thread-stack.h"
-#include "debug.h"
-#include "namespaces.h"
+
+#include "addr_location.h"
+#include "callchain.h"
 #include "comm.h"
+#include "debug.h"
+#include "dso.h"
+#include "dwarf-regs.h"
 #include "map.h"
+#include "namespaces.h"
+#include "session.h"
 #include "symbol.h"
+#include "thread-stack.h"
+#include "thread.h"
 #include "unwind.h"
-#include "callchain.h"
-#include "dwarf-regs.h"
 
 #include <api/fs/fs.h>
 
diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c
index ae70fb56a057..552cffb4bb92 100644
--- a/tools/perf/util/unwind-libdw.c
+++ b/tools/perf/util/unwind-libdw.c
@@ -4,20 +4,23 @@
 #include <elfutils/libdwfl.h>
 #include <inttypes.h>
 #include <errno.h>
+
+#include "addr_location.h"
+#include "callchain.h"
 #include "debug.h"
 #include "dso.h"
-#include "unwind.h"
-#include "unwind-libdw.h"
+#include "env.h"
+#include "event.h"
 #include "machine.h"
 #include "map.h"
+#include "perf_regs.h"
 #include "symbol.h"
 #include "thread.h"
+#include "unwind-libdw.h"
+#include "unwind.h"
+
 #include <linux/types.h>
 #include <linux/zalloc.h>
-#include "event.h"
-#include "perf_regs.h"
-#include "callchain.h"
-#include "util/env.h"
 
 static char *debuginfo_path;
 
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 5/9] perf symbol: Move dso__load_bfd_symbols out of symbol.h
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (3 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 4/9] perf symbol: Remove unused includes Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 6/9] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Move into libbfd.h where the code is and to make it clear that
symbol.h and symbol.c are generic code. Add libbfd.h to symbol.c
includes and organize the includes.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/pe-file-parsing.c |  5 ++--
 tools/perf/util/libbfd.h           |  7 ++++++
 tools/perf/util/symbol.c           | 38 +++++++++++++++++-------------
 tools/perf/util/symbol.h           |  4 ----
 4 files changed, 31 insertions(+), 23 deletions(-)

diff --git a/tools/perf/tests/pe-file-parsing.c b/tools/perf/tests/pe-file-parsing.c
index 8b31d1d05f90..2459656b01ac 100644
--- a/tools/perf/tests/pe-file-parsing.c
+++ b/tools/perf/tests/pe-file-parsing.c
@@ -11,10 +11,11 @@
 #include <unistd.h>
 #include <subcmd/exec-cmd.h>
 
-#include "debug.h"
 #include "util/build-id.h"
-#include "util/symbol.h"
+#include "util/debug.h"
 #include "util/dso.h"
+#include "util/libbfd.h"
+#include "util/symbol.h"
 
 #include "tests.h"
 
diff --git a/tools/perf/util/libbfd.h b/tools/perf/util/libbfd.h
index e300f171d1bd..5b5149516f1a 100644
--- a/tools/perf/util/libbfd.h
+++ b/tools/perf/util/libbfd.h
@@ -21,6 +21,7 @@ int libbfd__addr2line(const char *dso_name, u64 addr,
 
 
 void dso__free_a2l_libbfd(struct dso *dso);
+int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
 
 int symbol__disassemble_libbfd(const char *filename, struct symbol *sym,
 			     struct annotate_args *args);
@@ -51,6 +52,12 @@ static inline void dso__free_a2l_libbfd(struct dso *dso __always_unused)
 {
 }
 
+static inline int dso__load_bfd_symbols(struct dso *dso __always_unused,
+					const char *debugfile __always_unused)
+{
+	return -1;
+}
+
 static inline int symbol__disassemble_libbfd(const char *filename __always_unused,
 					struct symbol *sym __always_unused,
 					struct annotate_args *args __always_unused)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index cc26b7bf302b..638015651547 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1,47 +1,51 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <dirent.h>
 #include <errno.h>
-#include <stdlib.h>
+#include <fcntl.h>
+#include <inttypes.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <linux/capability.h>
-#include <linux/kernel.h>
-#include <linux/mman.h>
-#include <linux/string.h>
-#include <linux/time64.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/param.h>
-#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <inttypes.h>
+
 #include "annotate.h"
 #include "build-id.h"
 #include "cap.h"
 #include "cpumap.h"
 #include "debug.h"
+#include "debug.h"
 #include "demangle-cxx.h"
 #include "demangle-java.h"
 #include "demangle-ocaml.h"
 #include "demangle-rust-v0.h"
 #include "dso.h"
-#include "util.h" // lsdir()
-#include "debug.h"
 #include "event.h"
+#include "header.h"
+#include "intlist.h"
+#include "libbfd.h"
 #include "machine.h"
 #include "map.h"
-#include "symbol.h"
 #include "map_symbol.h"
 #include "mem-events.h"
 #include "mem-info.h"
-#include "symsrc.h"
-#include "strlist.h"
-#include "intlist.h"
 #include "namespaces.h"
-#include "header.h"
 #include "path.h"
+#include "perf-libelf.h"
+#include "strlist.h"
+#include "symbol.h"
+#include "symsrc.h"
+#include "util.h" // lsdir()
+
+#include <linux/capability.h>
 #include <linux/ctype.h>
+#include <linux/kernel.h>
 #include <linux/log2.h>
+#include <linux/mman.h>
+#include <linux/string.h>
+#include <linux/time64.h>
 #include <linux/zalloc.h>
 
 #include <elf.h>
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 42d8e3fd1d01..d37d5a97bc6f 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -153,10 +153,6 @@ int symbol__config_symfs(const struct option *opt __maybe_unused,
 
 struct symsrc;
 
-#ifdef HAVE_LIBBFD_SUPPORT
-int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
-#endif
-
 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		  struct symsrc *runtime_ss, int kmodule);
 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 6/9] perf symbol: Use fallbacks with filename__read_build_id
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (4 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 5/9] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 7/9] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Try libbfd, libelf and then symbol-minimal when trying to read a build
ID. Always compile in symbol-minimal so that it is the fallback of
last resort. Rename libelf reading code. Move the decompression code
into symbol.c and allow its use with the symbol-minimal version.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/Build            |   5 +-
 tools/perf/util/libbfd.c         |   4 +-
 tools/perf/util/libbfd.h         |   8 +-
 tools/perf/util/perf-libelf.c    | 113 +++++++++++++++++++++++
 tools/perf/util/perf-libelf.h    |  20 ++++
 tools/perf/util/symbol-elf.c     | 154 +------------------------------
 tools/perf/util/symbol-minimal.c |  12 ++-
 tools/perf/util/symbol-minimal.h |   9 ++
 tools/perf/util/symbol.c         |  50 ++++++++++
 9 files changed, 210 insertions(+), 165 deletions(-)
 create mode 100644 tools/perf/util/symbol-minimal.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index dcac6f1e1d99..c40053db2212 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -54,6 +54,7 @@ perf-util-y += usage.o
 perf-util-y += dso.o
 perf-util-y += dsos.o
 perf-util-y += symbol.o
+perf-util-y += symbol-minimal.o
 perf-util-y += symbol_fprintf.o
 perf-util-y += map_symbol.o
 perf-util-y += color.o
@@ -209,10 +210,6 @@ ifdef hashmap
 perf-util-y += hashmap.o
 endif
 
-ifndef CONFIG_LIBELF
-perf-util-y += symbol-minimal.o
-endif
-
 ifndef CONFIG_SETNS
 perf-util-y += setns.o
 endif
diff --git a/tools/perf/util/libbfd.c b/tools/perf/util/libbfd.c
index 6434c2dccd4a..c438fbd03b24 100644
--- a/tools/perf/util/libbfd.c
+++ b/tools/perf/util/libbfd.c
@@ -418,13 +418,13 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
 	return err;
 }
 
-int libbfd__read_build_id(const char *filename, struct build_id *bid, bool block)
+int libbfd__read_build_id(int _fd, const char *filename, struct build_id *bid)
 {
 	size_t size = sizeof(bid->data);
 	int err = -1, fd;
 	bfd *abfd;
 
-	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
+	fd = dup(_fd);
 	if (fd < 0)
 		return -1;
 
diff --git a/tools/perf/util/libbfd.h b/tools/perf/util/libbfd.h
index 5b5149516f1a..72f1ad9a7578 100644
--- a/tools/perf/util/libbfd.h
+++ b/tools/perf/util/libbfd.h
@@ -26,7 +26,7 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
 int symbol__disassemble_libbfd(const char *filename, struct symbol *sym,
 			     struct annotate_args *args);
 
-int libbfd__read_build_id(const char *filename, struct build_id *bid, bool block);
+int libbfd__read_build_id(int fd, const char *filename, struct build_id *bid);
 
 int libbfd_filename__read_debuglink(const char *filename, char *debuglink, size_t size);
 
@@ -65,9 +65,9 @@ static inline int symbol__disassemble_libbfd(const char *filename __always_unuse
 	return -1;
 }
 
-static inline int libbfd__read_build_id(const char *filename __always_unused,
-					struct build_id *bid __always_unused,
-					bool block __always_unused)
+static inline int libbfd__read_build_id(int fd __always_unused,
+					const char *filename __always_unused,
+					struct build_id *bid __always_unused)
 {
 	return -1;
 }
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index abd55bd7f14b..c7fa08a177d0 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -3,6 +3,10 @@
 
 #include <stddef.h>
 #include <string.h>
+#include <unistd.h>
+
+#include "build-id.h"
+#include "debug.h"
 
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
 			     GElf_Shdr *shp, const char *name, size_t *idx)
@@ -30,3 +34,112 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
 	return NULL;
 }
 
+int __libelf__read_build_id(Elf *elf, void *bf, size_t size)
+{
+	int err = -1;
+	GElf_Ehdr ehdr;
+	GElf_Shdr shdr;
+	Elf_Data *data;
+	Elf_Scn *sec;
+	Elf_Kind ek;
+	void *ptr;
+
+	if (size < BUILD_ID_SIZE)
+		goto out;
+
+	ek = elf_kind(elf);
+	if (ek != ELF_K_ELF)
+		goto out;
+
+	if (gelf_getehdr(elf, &ehdr) == NULL) {
+		pr_err("%s: cannot get elf header.\n", __func__);
+		goto out;
+	}
+
+	/*
+	 * Check following sections for notes:
+	 *   '.note.gnu.build-id'
+	 *   '.notes'
+	 *   '.note' (VDSO specific)
+	 */
+	do {
+		sec = elf_section_by_name(elf, &ehdr, &shdr,
+					  ".note.gnu.build-id", NULL);
+		if (sec)
+			break;
+
+		sec = elf_section_by_name(elf, &ehdr, &shdr,
+					  ".notes", NULL);
+		if (sec)
+			break;
+
+		sec = elf_section_by_name(elf, &ehdr, &shdr,
+					  ".note", NULL);
+		if (sec)
+			break;
+
+		return err;
+
+	} while (0);
+
+	data = elf_getdata(sec, NULL);
+	if (data == NULL)
+		goto out;
+
+	ptr = data->d_buf;
+	while (ptr < (data->d_buf + data->d_size)) {
+		GElf_Nhdr *nhdr = ptr;
+		size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
+		       descsz = NOTE_ALIGN(nhdr->n_descsz);
+		const char *name;
+
+		ptr += sizeof(*nhdr);
+		name = ptr;
+		ptr += namesz;
+		if (nhdr->n_type == NT_GNU_BUILD_ID &&
+		    nhdr->n_namesz == sizeof("GNU")) {
+			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
+				size_t sz = min(size, descsz);
+
+				memcpy(bf, ptr, sz);
+				memset(bf + sz, 0, size - sz);
+				err = sz;
+				break;
+			}
+		}
+		ptr += descsz;
+	}
+
+out:
+	return err;
+}
+
+int libelf__read_build_id(int _fd, const char *filename, struct build_id *bid)
+{
+	size_t size = sizeof(bid->data);
+	int fd, err = -1;
+	Elf *elf;
+
+	if (size < BUILD_ID_SIZE)
+		goto out;
+
+	fd = dup(_fd);
+	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;
+	}
+
+	err = __libelf__read_build_id(elf, bid->data, size);
+	if (err > 0)
+		bid->size = err;
+
+	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 5d7d18daf5ff..931597493b1f 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
 
+struct build_id;
+
 #ifdef HAVE_LIBELF_SUPPORT
 
 #include <libelf.h>
@@ -17,9 +19,27 @@
 # 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);
+
+#else // !defined(HAVE_LIBELF_SUPPORT)
+
+static inline int libelf__read_build_id(int fd __always_unused,
+					const char *filename __always_unused,
+					struct build_id *bid __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 750a30b216d6..b301f1263ecc 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -751,158 +751,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	return 0;
 }
 
-/*
- * Align offset to 4 bytes as needed for note name and descriptor data.
- */
-#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-
-static int elf_read_build_id(Elf *elf, void *bf, size_t size)
-{
-	int err = -1;
-	GElf_Ehdr ehdr;
-	GElf_Shdr shdr;
-	Elf_Data *data;
-	Elf_Scn *sec;
-	Elf_Kind ek;
-	void *ptr;
-
-	if (size < BUILD_ID_SIZE)
-		goto out;
-
-	ek = elf_kind(elf);
-	if (ek != ELF_K_ELF)
-		goto out;
-
-	if (gelf_getehdr(elf, &ehdr) == NULL) {
-		pr_err("%s: cannot get elf header.\n", __func__);
-		goto out;
-	}
-
-	/*
-	 * Check following sections for notes:
-	 *   '.note.gnu.build-id'
-	 *   '.notes'
-	 *   '.note' (VDSO specific)
-	 */
-	do {
-		sec = elf_section_by_name(elf, &ehdr, &shdr,
-					  ".note.gnu.build-id", NULL);
-		if (sec)
-			break;
-
-		sec = elf_section_by_name(elf, &ehdr, &shdr,
-					  ".notes", NULL);
-		if (sec)
-			break;
-
-		sec = elf_section_by_name(elf, &ehdr, &shdr,
-					  ".note", NULL);
-		if (sec)
-			break;
-
-		return err;
-
-	} while (0);
-
-	data = elf_getdata(sec, NULL);
-	if (data == NULL)
-		goto out;
-
-	ptr = data->d_buf;
-	while (ptr < (data->d_buf + data->d_size)) {
-		GElf_Nhdr *nhdr = ptr;
-		size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
-		       descsz = NOTE_ALIGN(nhdr->n_descsz);
-		const char *name;
-
-		ptr += sizeof(*nhdr);
-		name = ptr;
-		ptr += namesz;
-		if (nhdr->n_type == NT_GNU_BUILD_ID &&
-		    nhdr->n_namesz == sizeof("GNU")) {
-			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
-				size_t sz = min(size, descsz);
-				memcpy(bf, ptr, sz);
-				memset(bf + sz, 0, size - sz);
-				err = sz;
-				break;
-			}
-		}
-		ptr += descsz;
-	}
-
-out:
-	return err;
-}
-
-static int read_build_id(const char *filename, struct build_id *bid, bool block)
-{
-	size_t size = sizeof(bid->data);
-	int fd, err;
-	Elf *elf;
-
-	err = libbfd__read_build_id(filename, bid, block);
-	if (err >= 0)
-		goto out;
-
-	if (size < BUILD_ID_SIZE)
-		goto out;
-
-	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
-	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;
-	}
-
-	err = elf_read_build_id(elf, bid->data, size);
-	if (err > 0)
-		bid->size = err;
-
-	elf_end(elf);
-out_close:
-	close(fd);
-out:
-	return err;
-}
-
-int filename__read_build_id(const char *filename, struct build_id *bid, bool block)
-{
-	struct kmod_path m = { .name = NULL, };
-	char path[PATH_MAX];
-	int err;
-
-	if (!filename)
-		return -EFAULT;
-
-	err = kmod_path__parse(&m, filename);
-	if (err)
-		return -1;
-
-	if (m.comp) {
-		int error = 0, fd;
-
-		fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
-		if (fd < 0) {
-			pr_debug("Failed to decompress (error %d) %s\n",
-				 error, filename);
-			return -1;
-		}
-		close(fd);
-		filename = path;
-		block = true;
-	}
-
-	err = read_build_id(filename, bid, block);
-
-	if (m.comp)
-		unlink(filename);
-	return err;
-}
-
 int sysfs__read_build_id(const char *filename, struct build_id *bid)
 {
 	size_t size = sizeof(bid->data);
@@ -1170,7 +1018,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 		struct build_id bid;
 		int size;
 
-		size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
+		size = __libelf__read_build_id(elf, build_id, BUILD_ID_SIZE);
 		if (size <= 0) {
 			*dso__load_errno(dso) = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
 			goto out_elf_end;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index aeb253248895..f9adeb09ef00 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -1,5 +1,7 @@
+#include "debug.h"
 #include "dso.h"
 #include "symbol.h"
+#include "symbol-minimal.h"
 #include "symsrc.h"
 
 #include <errno.h>
@@ -75,17 +77,19 @@ 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
  */
-int filename__read_build_id(const char *filename, struct build_id *bid, bool block)
+int sym_min__read_build_id(int _fd, const char *filename, struct build_id *bid)
 {
 	int fd, ret = -1;
 	bool need_swap = false, elf32;
@@ -102,7 +106,7 @@ int filename__read_build_id(const char *filename, struct build_id *bid, bool blo
 	void *phdr, *buf = NULL;
 	ssize_t phdr_size, ehdr_size, buf_size = 0;
 
-	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
+	fd = dup(_fd);
 	if (fd < 0)
 		return -1;
 
@@ -194,9 +198,12 @@ int filename__read_build_id(const char *filename, struct build_id *bid, bool blo
 	free(phdr);
 out:
 	close(fd);
+	if (ret)
+		pr_debug("Error reading build-id from %s\n", filename);
 	return ret;
 }
 
+#ifndef HAVE_LIBELF_SUPPORT
 int sysfs__read_build_id(const char *filename, struct build_id *bid)
 {
 	int fd;
@@ -358,3 +365,4 @@ bool filename__has_section(const char *filename __maybe_unused, const char *sec
 {
 	return false;
 }
+#endif // HAVE_LIBELF_SUPPORT
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
new file mode 100644
index 000000000000..185d98968212
--- /dev/null
+++ b/tools/perf/util/symbol-minimal.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_SYMBOL_MINIMAL_H
+#define __PERF_SYMBOL_MINIMAL_H
+
+struct build_id;
+
+int sym_min__read_build_id(int _fd, 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 638015651547..c8795e1da2df 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -35,6 +35,7 @@
 #include "path.h"
 #include "perf-libelf.h"
 #include "strlist.h"
+#include "symbol-minimal.h"
 #include "symbol.h"
 #include "symsrc.h"
 #include "util.h" // lsdir()
@@ -1999,6 +2000,55 @@ static bool filename__readable(const char *file)
 	return true;
 }
 
+int filename__read_build_id(const char *filename, struct build_id *bid, bool block)
+{
+	struct kmod_path m = { .name = NULL, };
+	char path[PATH_MAX];
+	int err, fd;
+
+	if (!filename)
+		return -EFAULT;
+
+	err = kmod_path__parse(&m, filename);
+	if (err)
+		return -1;
+
+	if (m.comp) {
+		int error = 0;
+
+		fd = filename__decompress(filename, path, sizeof(path), m.comp, &error);
+		if (fd < 0) {
+			pr_debug("Failed to decompress (error %d) %s\n",
+				 error, filename);
+			return -1;
+		}
+		lseek(fd, 0, SEEK_SET);
+		filename = path;
+		block = true;
+	} else {
+		fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
+		if (fd < 0) {
+			pr_debug("Failed to open %s\n", filename);
+			return -1;
+		}
+	}
+
+	err = libbfd__read_build_id(fd, filename, bid);
+	if (err == 0)
+		goto out;
+
+	err = libelf__read_build_id(fd, filename, bid);
+	if (err == 0)
+		goto out;
+
+	err = sym_min__read_build_id(fd, filename, bid);
+out:
+	close(fd);
+	if (m.comp)
+		unlink(filename);
+	return err;
+}
+
 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
 {
 	struct build_id bid = { .size = 0, };
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

* [PATCH v1 7/9] perf symbol: Use fallbacks for filename__read_debuglink
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (5 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 6/9] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 8/9] perf symbol: Use fallbacks for sysfs__read_build_id Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 9/9] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Try libbfd and then libelf in symbol.c. Remove the non-existent
symbol-minimal version.

Signed-off-by: Ian Rogers <irogers@google.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


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

* [PATCH v1 8/9] perf symbol: Use fallbacks for sysfs__read_build_id
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (6 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 7/9] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  2025-11-23  2:32 ` [PATCH v1 9/9] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

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


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

* [PATCH v1 9/9] perf dso: Move type helpers out of symbol and use fallbacks
  2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
                   ` (7 preceding siblings ...)
  2025-11-23  2:32 ` [PATCH v1 8/9] perf symbol: Use fallbacks for sysfs__read_build_id Ian Rogers
@ 2025-11-23  2:32 ` Ian Rogers
  8 siblings, 0 replies; 10+ messages in thread
From: Ian Rogers @ 2025-11-23  2:32 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Suzuki K Poulose, Mike Leach, James Clark,
	John Garry, Will Deacon, Leo Yan, Athira Rajeev, tanze,
	Aditya Bodkhe, Stephen Brennan, Andi Kleen, Chun-Tse Shao,
	Thomas Falcon, Dapeng Mi, Dr. David Alan Gilbert,
	Christophe Leroy, Krzysztof Łopatowski,
	Masami Hiramatsu (Google), Alexandre Ghiti, Haibo Xu,
	Sergei Trofimovich, linux-kernel, linux-perf-users

Fallback between libelf and symbol-minimal versions. Remove the
symbol.c code and just directly inline into the only caller in dso.c.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/dso.c            | 57 ++++++++++++++----------
 tools/perf/util/perf-libelf.c    | 33 ++++++++++++++
 tools/perf/util/perf-libelf.h    |  8 ++++
 tools/perf/util/symbol-elf.c     | 33 --------------
 tools/perf/util/symbol-minimal.c | 76 ++++++++++++++++----------------
 tools/perf/util/symbol-minimal.h |  3 ++
 tools/perf/util/symbol.h         |  2 -
 7 files changed, 115 insertions(+), 97 deletions(-)

diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 344e689567ee..421fa4d27d2a 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1,37 +1,42 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <asm/bug.h>
-#include <linux/kernel.h>
-#include <linux/string.h>
-#include <linux/zalloc.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
-#ifdef HAVE_LIBBPF_SUPPORT
-#include <bpf/libbpf.h>
-#include "bpf-event.h"
-#include "bpf-utils.h"
-#endif
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "annotate-data.h"
+#include "auxtrace.h"
 #include "compress.h"
+#include "debug.h"
+#include "dso.h"
+#include "dsos.h"
 #include "env.h"
+#include "machine.h"
+#include "map.h"
 #include "namespaces.h"
 #include "path.h"
-#include "map.h"
-#include "symbol.h"
+#include "perf-libelf.h"
 #include "srcline.h"
-#include "dso.h"
-#include "dsos.h"
-#include "machine.h"
-#include "auxtrace.h"
-#include "util.h" /* O_CLOEXEC for older systems */
-#include "debug.h"
 #include "string2.h"
+#include "symbol-minimal.h"
+#include "symbol.h"
+#include "util.h" /* O_CLOEXEC for older systems */
 #include "vdso.h"
-#include "annotate-data.h"
+
+#include <asm/bug.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/zalloc.h>
+
+#ifdef HAVE_LIBBPF_SUPPORT
+#include <bpf/libbpf.h>
+#include "bpf-event.h"
+#include "bpf-utils.h"
+#endif
 
 static const char * const debuglink_paths[] = {
 	"%.0s%s",
@@ -1747,7 +1752,11 @@ enum dso_type dso__type(struct dso *dso, struct machine *machine)
 	enum dso_type type = DSO__TYPE_UNKNOWN;
 
 	if (dso__data_get_fd(dso, machine, &fd)) {
-		type = dso__type_fd(fd);
+		type = libelf_dso__type_fd(fd);
+
+		if (type == DSO__TYPE_UNKNOWN)
+			type = sym_min_dso__type_fd(fd);
+
 		dso__data_put_fd(dso);
 	}
 
diff --git a/tools/perf/util/perf-libelf.c b/tools/perf/util/perf-libelf.c
index 861cb7ae9b45..d36b69ccccda 100644
--- a/tools/perf/util/perf-libelf.c
+++ b/tools/perf/util/perf-libelf.c
@@ -251,3 +251,36 @@ int libelf_filename__read_debuglink(const char *filename, char *debuglink, size_
 out:
 	return err;
 }
+
+enum dso_type libelf_dso__type_fd(int fd)
+{
+	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
+	GElf_Ehdr ehdr;
+	Elf_Kind ek;
+	Elf *elf;
+
+	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+	if (elf == NULL)
+		goto out;
+
+	ek = elf_kind(elf);
+	if (ek != ELF_K_ELF)
+		goto out_end;
+
+	if (gelf_getclass(elf) == ELFCLASS64) {
+		dso_type = DSO__TYPE_64BIT;
+		goto out_end;
+	}
+
+	if (gelf_getehdr(elf, &ehdr) == NULL)
+		goto out_end;
+
+	if (ehdr.e_machine == EM_X86_64)
+		dso_type = DSO__TYPE_X32BIT;
+	else
+		dso_type = DSO__TYPE_32BIT;
+out_end:
+	elf_end(elf);
+out:
+	return dso_type;
+}
diff --git a/tools/perf/util/perf-libelf.h b/tools/perf/util/perf-libelf.h
index 95fe8805aa41..976fe3fb5cd8 100644
--- a/tools/perf/util/perf-libelf.h
+++ b/tools/perf/util/perf-libelf.h
@@ -4,6 +4,8 @@
 
 #include <stddef.h>
 
+#include "dso.h"
+
 struct build_id;
 
 #ifdef HAVE_LIBELF_SUPPORT
@@ -29,6 +31,7 @@ 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);
+enum dso_type libelf_dso__type_fd(int fd);
 
 #else // !defined(HAVE_LIBELF_SUPPORT)
 
@@ -52,6 +55,11 @@ static inline int libelf_filename__read_debuglink(const char *filename __always_
 	return -1;
 }
 
+enum dso_type libelf_dso__type_fd(int fd __always_unused)
+{
+	return DSO__TYPE_UNKNOWN;
+}
+
 #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 5ddf8353e01b..3d385d749c53 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1572,39 +1572,6 @@ int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
 	return err;
 }
 
-enum dso_type dso__type_fd(int fd)
-{
-	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
-	GElf_Ehdr ehdr;
-	Elf_Kind ek;
-	Elf *elf;
-
-	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-	if (elf == NULL)
-		goto out;
-
-	ek = elf_kind(elf);
-	if (ek != ELF_K_ELF)
-		goto out_end;
-
-	if (gelf_getclass(elf) == ELFCLASS64) {
-		dso_type = DSO__TYPE_64BIT;
-		goto out_end;
-	}
-
-	if (gelf_getehdr(elf, &ehdr) == NULL)
-		goto out_end;
-
-	if (ehdr.e_machine == EM_X86_64)
-		dso_type = DSO__TYPE_X32BIT;
-	else
-		dso_type = DSO__TYPE_32BIT;
-out_end:
-	elf_end(elf);
-out:
-	return dso_type;
-}
-
 static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
 {
 	ssize_t r;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index cc9055ba2f2a..336886c2b1ff 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -225,6 +225,44 @@ int sym_min_sysfs__read_build_id(const char *filename, struct build_id *bid)
 	return ret;
 }
 
+static int fd__is_64_bit(int fd)
+{
+	u8 e_ident[EI_NIDENT];
+
+	if (lseek(fd, 0, SEEK_SET))
+		return -1;
+
+	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
+		return -1;
+
+	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+	    e_ident[EI_VERSION] != EV_CURRENT)
+		return -1;
+
+	return e_ident[EI_CLASS] == ELFCLASS64;
+}
+
+enum dso_type sym_min_dso__type_fd(int fd)
+{
+	Elf64_Ehdr ehdr;
+	int ret;
+
+	ret = fd__is_64_bit(fd);
+	if (ret < 0)
+		return DSO__TYPE_UNKNOWN;
+
+	if (ret)
+		return DSO__TYPE_64BIT;
+
+	if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
+		return DSO__TYPE_UNKNOWN;
+
+	if (ehdr.e_machine == EM_X86_64)
+		return DSO__TYPE_X32BIT;
+
+	return DSO__TYPE_32BIT;
+}
+
 #ifndef HAVE_LIBELF_SUPPORT
 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 	         enum dso_binary_type type)
@@ -271,44 +309,6 @@ int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
 	return 0;
 }
 
-static int fd__is_64_bit(int fd)
-{
-	u8 e_ident[EI_NIDENT];
-
-	if (lseek(fd, 0, SEEK_SET))
-		return -1;
-
-	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
-		return -1;
-
-	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
-	    e_ident[EI_VERSION] != EV_CURRENT)
-		return -1;
-
-	return e_ident[EI_CLASS] == ELFCLASS64;
-}
-
-enum dso_type dso__type_fd(int fd)
-{
-	Elf64_Ehdr ehdr;
-	int ret;
-
-	ret = fd__is_64_bit(fd);
-	if (ret < 0)
-		return DSO__TYPE_UNKNOWN;
-
-	if (ret)
-		return DSO__TYPE_64BIT;
-
-	if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
-		return DSO__TYPE_UNKNOWN;
-
-	if (ehdr.e_machine == EM_X86_64)
-		return DSO__TYPE_X32BIT;
-
-	return DSO__TYPE_32BIT;
-}
-
 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
 		  struct symsrc *ss,
 		  struct symsrc *runtime_ss __maybe_unused,
diff --git a/tools/perf/util/symbol-minimal.h b/tools/perf/util/symbol-minimal.h
index 5cce1f1f0f16..282466aca7d9 100644
--- a/tools/perf/util/symbol-minimal.h
+++ b/tools/perf/util/symbol-minimal.h
@@ -2,9 +2,12 @@
 #ifndef __PERF_SYMBOL_MINIMAL_H
 #define __PERF_SYMBOL_MINIMAL_H
 
+#include "dso.h"
+
 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);
+enum dso_type sym_min_dso__type_fd(int fd);
 
 #endif /* __PERF_SYMBOL_MINIMAL_H */
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index d37d5a97bc6f..25484b2e7fa4 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -117,8 +117,6 @@ struct symbol *dso__first_symbol(struct dso *dso);
 struct symbol *dso__last_symbol(struct dso *dso);
 struct symbol *dso__next_symbol(struct symbol *sym);
 
-enum dso_type dso__type_fd(int fd);
-
 int filename__read_build_id(const char *filename, struct build_id *id, bool block);
 int sysfs__read_build_id(const char *filename, struct build_id *bid);
 int modules__parse(const char *filename, void *arg,
-- 
2.52.0.rc2.455.g230fcf2819-goog


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

end of thread, other threads:[~2025-11-23  2:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-23  2:32 [PATCH v1 0/9] perf: Refactor/add fallbacks for reading build-id and debuglink Ian Rogers
2025-11-23  2:32 ` [PATCH v1 1/9] perf symbol: Reduce scope of elf__needs_adjust_symbols Ian Rogers
2025-11-23  2:32 ` [PATCH v1 2/9] perf symbol: Reduce scope of arch__sym_update Ian Rogers
2025-11-23  2:32 ` [PATCH v1 3/9] perf symbol: Move libelf code to its own file/header Ian Rogers
2025-11-23  2:32 ` [PATCH v1 4/9] perf symbol: Remove unused includes Ian Rogers
2025-11-23  2:32 ` [PATCH v1 5/9] perf symbol: Move dso__load_bfd_symbols out of symbol.h Ian Rogers
2025-11-23  2:32 ` [PATCH v1 6/9] perf symbol: Use fallbacks with filename__read_build_id Ian Rogers
2025-11-23  2:32 ` [PATCH v1 7/9] perf symbol: Use fallbacks for filename__read_debuglink Ian Rogers
2025-11-23  2:32 ` [PATCH v1 8/9] perf symbol: Use fallbacks for sysfs__read_build_id Ian Rogers
2025-11-23  2:32 ` [PATCH v1 9/9] perf dso: Move type helpers out of symbol and use fallbacks Ian Rogers

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).