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 riscv32_triplets[] = {
47 "riscv32-unknown-linux-gnu-",
48 "riscv32-linux-android-",
53 const char *const riscv64_triplets[] = {
54 "riscv64-unknown-linux-gnu-",
55 "riscv64-linux-android-",
60 const char *const s390_triplets[] = {
66 const char *const sh_triplets[] = {
67 "sh-unknown-linux-gnu-",
72 const char *const sparc_triplets[] = {
73 "sparc-unknown-linux-gnu-",
74 "sparc64-unknown-linux-gnu-",
79 const char *const x86_triplets[] = {
80 "x86_64-pc-linux-gnu-",
81 "x86_64-unknown-linux-gnu-",
86 "i686-linux-android-",
87 "i686-android-linux-",
93 const char *const mips_triplets[] = {
94 "mips-unknown-linux-gnu-",
95 "mipsel-linux-android-",
98 "mips64el-linux-gnuabi64-",
99 "mips64-linux-gnuabi64-",
104 static bool lookup_path(char *name)
107 char *path, *tmp = NULL;
109 char *env = getenv("PATH");
118 path = strtok_r(env, ":", &tmp);
120 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
121 if (access(buf, F_OK) == 0) {
125 path = strtok_r(NULL, ":", &tmp);
131 static int lookup_triplets(const char *const *triplets, const char *name)
136 for (i = 0; triplets[i] != NULL; i++) {
137 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
138 if (lookup_path(buf))
144 static int perf_env__lookup_binutils_path(struct perf_env *env,
145 const char *name, char **path)
148 const char *arch = perf_env__arch(env), *cross_env;
149 const char *const *path_list;
153 * We don't need to try to find objdump path for native system.
154 * Just use default binutils path (e.g.: "objdump").
156 if (!strcmp(perf_env__arch(NULL), arch))
159 cross_env = getenv("CROSS_COMPILE");
161 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
164 if (access(buf, F_OK) == 0)
168 if (lookup_path(buf))
173 if (!strcmp(arch, "arc"))
174 path_list = arc_triplets;
175 else if (!strcmp(arch, "arm"))
176 path_list = arm_triplets;
177 else if (!strcmp(arch, "arm64"))
178 path_list = arm64_triplets;
179 else if (!strcmp(arch, "powerpc"))
180 path_list = powerpc_triplets;
181 else if (!strcmp(arch, "riscv32"))
182 path_list = riscv32_triplets;
183 else if (!strcmp(arch, "riscv64"))
184 path_list = riscv64_triplets;
185 else if (!strcmp(arch, "sh"))
186 path_list = sh_triplets;
187 else if (!strcmp(arch, "s390"))
188 path_list = s390_triplets;
189 else if (!strcmp(arch, "sparc"))
190 path_list = sparc_triplets;
191 else if (!strcmp(arch, "x86"))
192 path_list = x86_triplets;
193 else if (!strcmp(arch, "mips"))
194 path_list = mips_triplets;
196 ui__error("binutils for %s not supported.\n", arch);
200 idx = lookup_triplets(path_list, name);
202 ui__error("Please install %s for %s.\n"
203 "You can add it to PATH, set CROSS_COMPILE or "
204 "override the default using --%s.\n",
209 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
221 int perf_env__lookup_objdump(struct perf_env *env, char **path)
224 * For live mode, env->arch will be NULL and we can use
225 * the native objdump tool.
227 if (env->arch == NULL)
230 return perf_env__lookup_binutils_path(env, "objdump", path);
234 * Some architectures have a single address space for kernel and user addresses,
235 * which makes it possible to determine if an address is in kernel space or user
238 bool perf_env__single_address_space(struct perf_env *env)
240 return strcmp(perf_env__arch(env), "sparc");