[PATCH v2 17/30] perf dwarf-regs: Add e_machine__same_arch()

From: Ian Rogers

Date: Fri Nov 13 2026 - 14:59:25 EST


The 32-bit and 64-bit variants of some architectures have different ELF
machines, such as EM_386 and EM_X86_64, while others, such as RISC-V,
MIPS and PA-RISC, use one ELF machine for both. Code wanting to know if
two ELF machines are the same architecture either normalized the ELF
machine itself, as in pmu_events_map__e_machine_matches(), or compared
the strings from perf_env__arch(). perf_env__arch() maps EM_386 and
EM_X86_64 to "x86", but distinguishes "riscv" and "riscv64", "mips" and
"mips64", and "parisc" and "parisc64" by whether the kernel is 64-bit,
and computing it requires string lookups.

Add e_machine__64bit(), giving the ELF machine of the 64-bit variant of
an architecture, and e_machine__same_arch() next to EM_HOST. EM_ARM and
EM_AARCH64 remain distinct architectures. Use them when matching
pmu-events tables and in is_pmu_core_len(), which compares a perf_env's
architecture with the host's.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/pmu-events/empty-pmu-events.c | 6 +---
tools/perf/pmu-events/jevents.py | 6 +---
tools/perf/util/include/dwarf-regs.h | 37 ++++++++++++++++++++++++
tools/perf/util/pmu.c | 5 +++-
4 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-events/empty-pmu-events.c
index 872c6ca6e817..3870356485ef 100644
--- a/tools/perf/pmu-events/empty-pmu-events.c
+++ b/tools/perf/pmu-events/empty-pmu-events.c
@@ -5783,11 +5783,7 @@ int pmu_metrics_table__find_metric(const struct pmu_metrics_table *table,
static bool pmu_events_map__e_machine_matches(const struct pmu_events_map *map,
uint16_t e_machine)
{
- if (e_machine == EM_386)
- e_machine = EM_X86_64;
- else if (e_machine == EM_PPC)
- e_machine = EM_PPC64;
- return map->e_machine == e_machine;
+ return e_machine__same_arch(map->e_machine, e_machine);
}

static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu)
diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
index 802ae2ad4405..6adb385b38e5 100755
--- a/tools/perf/pmu-events/jevents.py
+++ b/tools/perf/pmu-events/jevents.py
@@ -1201,11 +1201,7 @@ int pmu_metrics_table__find_metric(const struct pmu_metrics_table *table,
static bool pmu_events_map__e_machine_matches(const struct pmu_events_map *map,
\t\t\t\t\t uint16_t e_machine)
{
-\tif (e_machine == EM_386)
-\t\te_machine = EM_X86_64;
-\telse if (e_machine == EM_PPC)
-\t\te_machine = EM_PPC64;
-\treturn map->e_machine == e_machine;
+\treturn e_machine__same_arch(map->e_machine, e_machine);
}

static const struct pmu_events_map *map_for_cpu(struct perf_cpu cpu)
diff --git a/tools/perf/util/include/dwarf-regs.h b/tools/perf/util/include/dwarf-regs.h
index 1da915c2683a..07f285a94e03 100644
--- a/tools/perf/util/include/dwarf-regs.h
+++ b/tools/perf/util/include/dwarf-regs.h
@@ -85,6 +85,43 @@
#define EF_HOST 0
#endif

+/**
+ * e_machine__64bit() - The ELF machine of the 64-bit variant of an architecture.
+ * @e_machine: An ELF machine, such as EM_386.
+ *
+ * The 32-bit and 64-bit variants of some architectures have different ELF
+ * machines, for example EM_386 and EM_X86_64, whereas others, like RISC-V,
+ * MIPS and PA-RISC, use the same ELF machine for both. EM_ARM and EM_AARCH64
+ * are considered distinct architectures.
+ *
+ * Return: The 64-bit ELF machine, or @e_machine if there isn't one.
+ */
+static inline uint16_t e_machine__64bit(uint16_t e_machine)
+{
+ switch (e_machine) {
+ case EM_386:
+ return EM_X86_64;
+ case EM_PPC:
+ return EM_PPC64;
+ case EM_SPARC:
+ case EM_SPARC32PLUS:
+ return EM_SPARCV9;
+ default:
+ return e_machine;
+ }
+}
+
+/**
+ * e_machine__same_arch() - Are two ELF machines the same architecture,
+ * ignoring differences in word size?
+ * @a: An ELF machine.
+ * @b: Another ELF machine.
+ */
+static inline bool e_machine__same_arch(uint16_t a, uint16_t b)
+{
+ return e_machine__64bit(a) == e_machine__64bit(b);
+}
+
#define DWARF_REG_PC 0xd3af9c /* random number */
#define DWARF_REG_FB 0xd3affb /* random number */

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 06c4912d2f65..d5384c074265 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -34,6 +34,8 @@
#include "print-events.h"
#include "hashmap.h"
#include "header.h"
+#include "dwarf-regs.h"
+#include "env.h"
#include "string2.h"
#include "strbuf.h"
#include "fncache.h"
@@ -2092,7 +2094,8 @@ bool is_pmu_core_len(struct perf_env *env, const char *name, size_t len)
* isn't portable, for example, big.LITTLE ARM and Apple core PMUs are
* named after their CPU rather than "cpu_core" and "cpu_atom".
*/
- if (env && strcmp(perf_env__arch(env), perf_env__arch(/*env=*/NULL))) {
+ if (env && !e_machine__same_arch(perf_env__e_machine(env, /*e_flags=*/NULL),
+ perf_env__e_machine(host_env(), /*e_flags=*/NULL))) {
pr_debug("Can't identify core PMUs of a %s perf.data file recorded without hybrid topology\n",
perf_env__arch(env));
return false;
--
2.56.1.703.gf848ed807c-goog