1 // SPDX-License-Identifier: GPL-2.0
4 #include "../util/env.h"
5 #include "../util/util.h"
6 #include "../util/debug.h"
8 const char *const arm_triplets[] = {
10 "arm-linux-androideabi-",
12 "arm-unknown-linux-gnu-",
13 "arm-unknown-linux-gnueabi-",
15 "arm-linux-gnueabihf-",
20 const char *const arm64_triplets[] = {
21 "aarch64-linux-android-",
26 const char *const powerpc_triplets[] = {
27 "powerpc-unknown-linux-gnu-",
29 "powerpc64-unknown-linux-gnu-",
30 "powerpc64-linux-gnu-",
31 "powerpc64le-linux-gnu-",
35 const char *const s390_triplets[] = {
41 const char *const sh_triplets[] = {
42 "sh-unknown-linux-gnu-",
43 "sh64-unknown-linux-gnu-",
49 const char *const sparc_triplets[] = {
50 "sparc-unknown-linux-gnu-",
51 "sparc64-unknown-linux-gnu-",
56 const char *const x86_triplets[] = {
57 "x86_64-pc-linux-gnu-",
58 "x86_64-unknown-linux-gnu-",
63 "i686-linux-android-",
64 "i686-android-linux-",
70 const char *const mips_triplets[] = {
71 "mips-unknown-linux-gnu-",
72 "mipsel-linux-android-",
75 "mips64el-linux-gnuabi64-",
76 "mips64-linux-gnuabi64-",
81 static bool lookup_path(char *name)
84 char *path, *tmp = NULL;
86 char *env = getenv("PATH");
95 path = strtok_r(env, ":", &tmp);
97 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
98 if (access(buf, F_OK) == 0) {
102 path = strtok_r(NULL, ":", &tmp);
108 static int lookup_triplets(const char *const *triplets, const char *name)
113 for (i = 0; triplets[i] != NULL; i++) {
114 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
115 if (lookup_path(buf))
121 static int perf_env__lookup_binutils_path(struct perf_env *env,
122 const char *name, const char **path)
125 const char *arch = perf_env__arch(env), *cross_env;
126 const char *const *path_list;
130 * We don't need to try to find objdump path for native system.
131 * Just use default binutils path (e.g.: "objdump").
133 if (!strcmp(perf_env__arch(NULL), arch))
136 cross_env = getenv("CROSS_COMPILE");
138 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
141 if (access(buf, F_OK) == 0)
145 if (lookup_path(buf))
150 if (!strcmp(arch, "arm"))
151 path_list = arm_triplets;
152 else if (!strcmp(arch, "arm64"))
153 path_list = arm64_triplets;
154 else if (!strcmp(arch, "powerpc"))
155 path_list = powerpc_triplets;
156 else if (!strcmp(arch, "sh"))
157 path_list = sh_triplets;
158 else if (!strcmp(arch, "s390"))
159 path_list = s390_triplets;
160 else if (!strcmp(arch, "sparc"))
161 path_list = sparc_triplets;
162 else if (!strcmp(arch, "x86"))
163 path_list = x86_triplets;
164 else if (!strcmp(arch, "mips"))
165 path_list = mips_triplets;
167 ui__error("binutils for %s not supported.\n", arch);
171 idx = lookup_triplets(path_list, name);
173 ui__error("Please install %s for %s.\n"
174 "You can add it to PATH, set CROSS_COMPILE or "
175 "override the default using --%s.\n",
180 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
192 int perf_env__lookup_objdump(struct perf_env *env, const char **path)
195 * For live mode, env->arch will be NULL and we can use
196 * the native objdump tool.
198 if (env->arch == NULL)
201 return perf_env__lookup_binutils_path(env, "objdump", path);
205 * Some architectures have a single address space for kernel and user addresses,
206 * which makes it possible to determine if an address is in kernel space or user
209 bool perf_env__single_address_space(struct perf_env *env)
211 return strcmp(perf_env__arch(env), "sparc");