]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf arch common: Use perf_env e_machine rather than arch
authorIan Rogers <irogers@google.com>
Tue, 2 Jun 2026 15:25:07 +0000 (08:25 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 3 Jun 2026 19:48:23 +0000 (16:48 -0300)
Use the e_machine rather than arch string matching.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Honglei Wang <jameshongleiwang@126.com>
Cc: Jan Polensky <japo@linux.ibm.com>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/arch/common.c

index ad0cab830a4da31b300be7c0006cd46d1eee0ae5..5ad50e331c55db06f9df0e6abbfd81224d30569c 100644 (file)
@@ -1,13 +1,18 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "common.h"
+
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include <linux/zalloc.h>
 #include <unistd.h>
-#include "common.h"
-#include "../util/env.h"
+
+#include <dwarf-regs.h>
+
 #include "../util/debug.h"
-#include <linux/zalloc.h>
+#include "../util/env.h"
 
 static const char *const arc_triplets[] = {
        "arc-linux-",
@@ -141,11 +146,40 @@ static int lookup_triplets(const char *const *triplets, const char *name)
        return -1;
 }
 
+static bool is_native_compatible(struct perf_env *env, uint16_t target, uint16_t host)
+{
+       if (target != host) {
+               /* A 64-bit host can natively disassemble its 32-bit compat architecture */
+               if (host == EM_X86_64 && target == EM_386)
+                       return true;
+               if (host == EM_AARCH64 && target == EM_ARM)
+                       return true;
+               if (host == EM_PPC64 && target == EM_PPC)
+                       return true;
+               if (host == EM_SPARCV9 && target == EM_SPARC)
+                       return true;
+               return false;
+       }
+
+       /* target == host case */
+       if (target == EM_RISCV) {
+               bool target_is_64 = perf_env__kernel_is_64_bit(env);
+               bool host_is_64 = (sizeof(void *) == 8);
+
+               /* 32-bit host cannot natively disassemble 64-bit target */
+               if (!host_is_64 && target_is_64)
+                       return false;
+       }
+
+       return true;
+}
+
 static int perf_env__lookup_binutils_path(struct perf_env *env,
                                          const char *name, char **path)
 {
        int idx;
-       const char *arch = perf_env__arch(env), *cross_env;
+       uint16_t e_machine = perf_env__e_machine(env, /*e_flags=*/NULL);
+       const char *cross_env;
        const char *const *path_list;
        char *buf = NULL;
 
@@ -153,7 +187,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,
         * We don't need to try to find objdump path for native system.
         * Just use default binutils path (e.g.: "objdump").
         */
-       if (!strcmp(perf_env__arch(NULL), arch))
+       if (is_native_compatible(env, e_machine, EM_HOST))
                goto out;
 
        cross_env = getenv("CROSS_COMPILE");
@@ -170,30 +204,42 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,
                zfree(&buf);
        }
 
-       if (!strcmp(arch, "arc"))
+       switch (e_machine) {
+       case EM_ARC:
                path_list = arc_triplets;
-       else if (!strcmp(arch, "arm"))
+               break;
+       case EM_ARM:
                path_list = arm_triplets;
-       else if (!strcmp(arch, "arm64"))
+               break;
+       case EM_AARCH64:
                path_list = arm64_triplets;
-       else if (!strcmp(arch, "powerpc"))
+               break;
+       case EM_PPC:
+       case EM_PPC64:
                path_list = powerpc_triplets;
-       else if (!strcmp(arch, "riscv32"))
-               path_list = riscv32_triplets;
-       else if (!strcmp(arch, "riscv64"))
-               path_list = riscv64_triplets;
-       else if (!strcmp(arch, "sh"))
+               break;
+       case EM_RISCV:
+               path_list = perf_env__kernel_is_64_bit(env) ? riscv64_triplets : riscv32_triplets;
+               break;
+       case EM_SH:
                path_list = sh_triplets;
-       else if (!strcmp(arch, "s390"))
+               break;
+       case EM_S390:
                path_list = s390_triplets;
-       else if (!strcmp(arch, "sparc"))
+               break;
+       case EM_SPARC:
+       case EM_SPARCV9:
                path_list = sparc_triplets;
-       else if (!strcmp(arch, "x86"))
+               break;
+       case EM_X86_64:
+       case EM_386:
                path_list = x86_triplets;
-       else if (!strcmp(arch, "mips"))
+               break;
+       case EM_MIPS:
                path_list = mips_triplets;
-       else {
-               ui__error("binutils for %s not supported.\n", arch);
+               break;
+       default:
+               ui__error("binutils for %s not supported.\n", perf_env__arch(env));
                goto out_error;
        }
 
@@ -202,7 +248,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,
                ui__error("Please install %s for %s.\n"
                          "You can add it to PATH, set CROSS_COMPILE or "
                          "override the default using --%s.\n",
-                         name, arch, name);
+                         name, perf_env__arch(env), name);
                goto out_error;
        }
 
@@ -237,7 +283,7 @@ int perf_env__lookup_objdump(struct perf_env *env, char **path)
  */
 bool perf_env__single_address_space(struct perf_env *env)
 {
-       const char *arch = perf_env__arch(env);
+       uint16_t e_machine = perf_env__e_machine(env, /*e_flags=*/NULL);
 
-       return strcmp(arch, "s390") && strcmp(arch, "sparc");
+       return e_machine != EM_SPARC && e_machine != EM_SPARCV9 && e_machine != EM_S390;
 }