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 arc_triplets[] = {
10 "arc-snps-linux-uclibc-",
11 "arc-snps-linux-gnu-",
15 const char *const arm_triplets[] = {
17 "arm-linux-androideabi-",
19 "arm-unknown-linux-gnu-",
20 "arm-unknown-linux-gnueabi-",
22 "arm-linux-gnueabihf-",
27 const char *const arm64_triplets[] = {
28 "aarch64-linux-android-",
33 const char *const powerpc_triplets[] = {
34 "powerpc-unknown-linux-gnu-",
36 "powerpc64-unknown-linux-gnu-",
37 "powerpc64-linux-gnu-",
38 "powerpc64le-linux-gnu-",
42 const char *const s390_triplets[] = {
48 const char *const sh_triplets[] = {
49 "sh-unknown-linux-gnu-",
50 "sh64-unknown-linux-gnu-",
56 const char *const sparc_triplets[] = {
57 "sparc-unknown-linux-gnu-",
58 "sparc64-unknown-linux-gnu-",
63 const char *const x86_triplets[] = {
64 "x86_64-pc-linux-gnu-",
65 "x86_64-unknown-linux-gnu-",
70 "i686-linux-android-",
71 "i686-android-linux-",
77 const char *const mips_triplets[] = {
78 "mips-unknown-linux-gnu-",
79 "mipsel-linux-android-",
82 "mips64el-linux-gnuabi64-",
83 "mips64-linux-gnuabi64-",
88 static bool lookup_path(char *name)
91 char *path, *tmp = NULL;
93 char *env = getenv("PATH");
102 path = strtok_r(env, ":", &tmp);
104 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
105 if (access(buf, F_OK) == 0) {
109 path = strtok_r(NULL, ":", &tmp);
115 static int lookup_triplets(const char *const *triplets, const char *name)
120 for (i = 0; triplets[i] != NULL; i++) {
121 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
122 if (lookup_path(buf))
128 static int perf_env__lookup_binutils_path(struct perf_env *env,
129 const char *name, const char **path)
132 const char *arch = perf_env__arch(env), *cross_env;
133 const char *const *path_list;
137 * We don't need to try to find objdump path for native system.
138 * Just use default binutils path (e.g.: "objdump").
140 if (!strcmp(perf_env__arch(NULL), arch))
143 cross_env = getenv("CROSS_COMPILE");
145 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
148 if (access(buf, F_OK) == 0)
152 if (lookup_path(buf))
157 if (!strcmp(arch, "arc"))
158 path_list = arc_triplets;
159 else if (!strcmp(arch, "arm"))
160 path_list = arm_triplets;
161 else if (!strcmp(arch, "arm64"))
162 path_list = arm64_triplets;
163 else if (!strcmp(arch, "powerpc"))
164 path_list = powerpc_triplets;
165 else if (!strcmp(arch, "sh"))
166 path_list = sh_triplets;
167 else if (!strcmp(arch, "s390"))
168 path_list = s390_triplets;
169 else if (!strcmp(arch, "sparc"))
170 path_list = sparc_triplets;
171 else if (!strcmp(arch, "x86"))
172 path_list = x86_triplets;
173 else if (!strcmp(arch, "mips"))
174 path_list = mips_triplets;
176 ui__error("binutils for %s not supported.\n", arch);
180 idx = lookup_triplets(path_list, name);
182 ui__error("Please install %s for %s.\n"
183 "You can add it to PATH, set CROSS_COMPILE or "
184 "override the default using --%s.\n",
189 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
201 int perf_env__lookup_objdump(struct perf_env *env, const char **path)
204 * For live mode, env->arch will be NULL and we can use
205 * the native objdump tool.
207 if (env->arch == NULL)
210 return perf_env__lookup_binutils_path(env, "objdump", path);
214 * Some architectures have a single address space for kernel and user addresses,
215 * which makes it possible to determine if an address is in kernel space or user
218 bool perf_env__single_address_space(struct perf_env *env)
220 return strcmp(perf_env__arch(env), "sparc");