[PATCH v1 9/9] perf dso: Move type helpers out of symbol and use fallbacks

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

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