1 // SPDX-License-Identifier: GPL-2.0
8 #include "../util/env.h"
9 #include "../util/debug.h"
10 #include <linux/zalloc.h>
12 const char *const arc_triplets[] = {
14 "arc-snps-linux-uclibc-",
15 "arc-snps-linux-gnu-",
19 const char *const arm_triplets[] = {
21 "arm-linux-androideabi-",
23 "arm-unknown-linux-gnu-",
24 "arm-unknown-linux-gnueabi-",
26 "arm-linux-gnueabihf-",
31 const char *const arm64_triplets[] = {
32 "aarch64-linux-android-",
37 const char *const powerpc_triplets[] = {
38 "powerpc-unknown-linux-gnu-",
40 "powerpc64-unknown-linux-gnu-",
41 "powerpc64-linux-gnu-",
42 "powerpc64le-linux-gnu-",
46 const char *const s390_triplets[] = {
52 const char *const sh_triplets[] = {
53 "sh-unknown-linux-gnu-",
58 const char *const sparc_triplets[] = {
59 "sparc-unknown-linux-gnu-",
60 "sparc64-unknown-linux-gnu-",
65 const char *const x86_triplets[] = {
66 "x86_64-pc-linux-gnu-",
67 "x86_64-unknown-linux-gnu-",
72 "i686-linux-android-",
73 "i686-android-linux-",
79 const char *const mips_triplets[] = {
80 "mips-unknown-linux-gnu-",
81 "mipsel-linux-android-",
84 "mips64el-linux-gnuabi64-",
85 "mips64-linux-gnuabi64-",
90 static bool lookup_path(char *name)
93 char *path, *tmp = NULL;
95 char *env = getenv("PATH");
104 path = strtok_r(env, ":", &tmp);
106 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
107 if (access(buf, F_OK) == 0) {
111 path = strtok_r(NULL, ":", &tmp);
117 static int lookup_triplets(const char *const *triplets, const char *name)
122 for (i = 0; triplets[i] != NULL; i++) {
123 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
124 if (lookup_path(buf))
130 static int perf_env__lookup_binutils_path(struct perf_env *env,
131 const char *name, char **path)
134 const char *arch = perf_env__arch(env), *cross_env;
135 const char *const *path_list;
139 * We don't need to try to find objdump path for native system.
140 * Just use default binutils path (e.g.: "objdump").
142 if (!strcmp(perf_env__arch(NULL), arch))
145 cross_env = getenv("CROSS_COMPILE");
147 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
150 if (access(buf, F_OK) == 0)
154 if (lookup_path(buf))
159 if (!strcmp(arch, "arc"))
160 path_list = arc_triplets;
161 else if (!strcmp(arch, "arm"))
162 path_list = arm_triplets;
163 else if (!strcmp(arch, "arm64"))
164 path_list = arm64_triplets;
165 else if (!strcmp(arch, "powerpc"))
166 path_list = powerpc_triplets;
167 else if (!strcmp(arch, "sh"))
168 path_list = sh_triplets;
169 else if (!strcmp(arch, "s390"))
170 path_list = s390_triplets;
171 else if (!strcmp(arch, "sparc"))
172 path_list = sparc_triplets;
173 else if (!strcmp(arch, "x86"))
174 path_list = x86_triplets;
175 else if (!strcmp(arch, "mips"))
176 path_list = mips_triplets;
178 ui__error("binutils for %s not supported.\n", arch);
182 idx = lookup_triplets(path_list, name);
184 ui__error("Please install %s for %s.\n"
185 "You can add it to PATH, set CROSS_COMPILE or "
186 "override the default using --%s.\n",
191 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
203 int perf_env__lookup_objdump(struct perf_env *env, char **path)
206 * For live mode, env->arch will be NULL and we can use
207 * the native objdump tool.
209 if (env->arch == NULL)
212 return perf_env__lookup_binutils_path(env, "objdump", path);
216 * Some architectures have a single address space for kernel and user addresses,
217 * which makes it possible to determine if an address is in kernel space or user
220 bool perf_env__single_address_space(struct perf_env *env)
222 return strcmp(perf_env__arch(env), "sparc");