Loading...
1#include <stdio.h>
2#include <sys/utsname.h>
3#include "common.h"
4#include "../util/debug.h"
5
6const char *const arm_triplets[] = {
7 "arm-eabi-",
8 "arm-linux-androideabi-",
9 "arm-unknown-linux-",
10 "arm-unknown-linux-gnu-",
11 "arm-unknown-linux-gnueabi-",
12 NULL
13};
14
15const char *const arm64_triplets[] = {
16 "aarch64-linux-android-",
17 NULL
18};
19
20const char *const powerpc_triplets[] = {
21 "powerpc-unknown-linux-gnu-",
22 "powerpc64-unknown-linux-gnu-",
23 NULL
24};
25
26const char *const s390_triplets[] = {
27 "s390-ibm-linux-",
28 NULL
29};
30
31const char *const sh_triplets[] = {
32 "sh-unknown-linux-gnu-",
33 "sh64-unknown-linux-gnu-",
34 NULL
35};
36
37const char *const sparc_triplets[] = {
38 "sparc-unknown-linux-gnu-",
39 "sparc64-unknown-linux-gnu-",
40 NULL
41};
42
43const char *const x86_triplets[] = {
44 "x86_64-pc-linux-gnu-",
45 "x86_64-unknown-linux-gnu-",
46 "i686-pc-linux-gnu-",
47 "i586-pc-linux-gnu-",
48 "i486-pc-linux-gnu-",
49 "i386-pc-linux-gnu-",
50 "i686-linux-android-",
51 "i686-android-linux-",
52 NULL
53};
54
55const char *const mips_triplets[] = {
56 "mips-unknown-linux-gnu-",
57 "mipsel-linux-android-",
58 NULL
59};
60
61static bool lookup_path(char *name)
62{
63 bool found = false;
64 char *path, *tmp = NULL;
65 char buf[PATH_MAX];
66 char *env = getenv("PATH");
67
68 if (!env)
69 return false;
70
71 env = strdup(env);
72 if (!env)
73 return false;
74
75 path = strtok_r(env, ":", &tmp);
76 while (path) {
77 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
78 if (access(buf, F_OK) == 0) {
79 found = true;
80 break;
81 }
82 path = strtok_r(NULL, ":", &tmp);
83 }
84 free(env);
85 return found;
86}
87
88static int lookup_triplets(const char *const *triplets, const char *name)
89{
90 int i;
91 char buf[PATH_MAX];
92
93 for (i = 0; triplets[i] != NULL; i++) {
94 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
95 if (lookup_path(buf))
96 return i;
97 }
98 return -1;
99}
100
101/*
102 * Return architecture name in a normalized form.
103 * The conversion logic comes from the Makefile.
104 */
105static const char *normalize_arch(char *arch)
106{
107 if (!strcmp(arch, "x86_64"))
108 return "x86";
109 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
110 return "x86";
111 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
112 return "sparc";
113 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
114 return "arm64";
115 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
116 return "arm";
117 if (!strncmp(arch, "s390", 4))
118 return "s390";
119 if (!strncmp(arch, "parisc", 6))
120 return "parisc";
121 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
122 return "powerpc";
123 if (!strncmp(arch, "mips", 4))
124 return "mips";
125 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
126 return "sh";
127
128 return arch;
129}
130
131static int perf_env__lookup_binutils_path(struct perf_env *env,
132 const char *name, const char **path)
133{
134 int idx;
135 const char *arch, *cross_env;
136 struct utsname uts;
137 const char *const *path_list;
138 char *buf = NULL;
139
140 arch = normalize_arch(env->arch);
141
142 if (uname(&uts) < 0)
143 goto out;
144
145 /*
146 * We don't need to try to find objdump path for native system.
147 * Just use default binutils path (e.g.: "objdump").
148 */
149 if (!strcmp(normalize_arch(uts.machine), arch))
150 goto out;
151
152 cross_env = getenv("CROSS_COMPILE");
153 if (cross_env) {
154 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
155 goto out_error;
156 if (buf[0] == '/') {
157 if (access(buf, F_OK) == 0)
158 goto out;
159 goto out_error;
160 }
161 if (lookup_path(buf))
162 goto out;
163 zfree(&buf);
164 }
165
166 if (!strcmp(arch, "arm"))
167 path_list = arm_triplets;
168 else if (!strcmp(arch, "arm64"))
169 path_list = arm64_triplets;
170 else if (!strcmp(arch, "powerpc"))
171 path_list = powerpc_triplets;
172 else if (!strcmp(arch, "sh"))
173 path_list = sh_triplets;
174 else if (!strcmp(arch, "s390"))
175 path_list = s390_triplets;
176 else if (!strcmp(arch, "sparc"))
177 path_list = sparc_triplets;
178 else if (!strcmp(arch, "x86"))
179 path_list = x86_triplets;
180 else if (!strcmp(arch, "mips"))
181 path_list = mips_triplets;
182 else {
183 ui__error("binutils for %s not supported.\n", arch);
184 goto out_error;
185 }
186
187 idx = lookup_triplets(path_list, name);
188 if (idx < 0) {
189 ui__error("Please install %s for %s.\n"
190 "You can add it to PATH, set CROSS_COMPILE or "
191 "override the default using --%s.\n",
192 name, arch, name);
193 goto out_error;
194 }
195
196 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
197 goto out_error;
198
199out:
200 *path = buf;
201 return 0;
202out_error:
203 free(buf);
204 *path = NULL;
205 return -1;
206}
207
208int perf_env__lookup_objdump(struct perf_env *env)
209{
210 /*
211 * For live mode, env->arch will be NULL and we can use
212 * the native objdump tool.
213 */
214 if (env->arch == NULL)
215 return 0;
216
217 return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);
218}
1// SPDX-License-Identifier: GPL-2.0
2#include <limits.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include "common.h"
8#include "../util/env.h"
9#include "../util/debug.h"
10#include <linux/zalloc.h>
11
12const char *const arc_triplets[] = {
13 "arc-linux-",
14 "arc-snps-linux-uclibc-",
15 "arc-snps-linux-gnu-",
16 NULL
17};
18
19const char *const arm_triplets[] = {
20 "arm-eabi-",
21 "arm-linux-androideabi-",
22 "arm-unknown-linux-",
23 "arm-unknown-linux-gnu-",
24 "arm-unknown-linux-gnueabi-",
25 "arm-linux-gnu-",
26 "arm-linux-gnueabihf-",
27 "arm-none-eabi-",
28 NULL
29};
30
31const char *const arm64_triplets[] = {
32 "aarch64-linux-android-",
33 "aarch64-linux-gnu-",
34 NULL
35};
36
37const char *const powerpc_triplets[] = {
38 "powerpc-unknown-linux-gnu-",
39 "powerpc-linux-gnu-",
40 "powerpc64-unknown-linux-gnu-",
41 "powerpc64-linux-gnu-",
42 "powerpc64le-linux-gnu-",
43 NULL
44};
45
46const char *const riscv32_triplets[] = {
47 "riscv32-unknown-linux-gnu-",
48 "riscv32-linux-android-",
49 "riscv32-linux-gnu-",
50 NULL
51};
52
53const char *const riscv64_triplets[] = {
54 "riscv64-unknown-linux-gnu-",
55 "riscv64-linux-android-",
56 "riscv64-linux-gnu-",
57 NULL
58};
59
60const char *const s390_triplets[] = {
61 "s390-ibm-linux-",
62 "s390x-linux-gnu-",
63 NULL
64};
65
66const char *const sh_triplets[] = {
67 "sh-unknown-linux-gnu-",
68 "sh-linux-gnu-",
69 NULL
70};
71
72const char *const sparc_triplets[] = {
73 "sparc-unknown-linux-gnu-",
74 "sparc64-unknown-linux-gnu-",
75 "sparc64-linux-gnu-",
76 NULL
77};
78
79const char *const x86_triplets[] = {
80 "x86_64-pc-linux-gnu-",
81 "x86_64-unknown-linux-gnu-",
82 "i686-pc-linux-gnu-",
83 "i586-pc-linux-gnu-",
84 "i486-pc-linux-gnu-",
85 "i386-pc-linux-gnu-",
86 "i686-linux-android-",
87 "i686-android-linux-",
88 "x86_64-linux-gnu-",
89 "i586-linux-gnu-",
90 NULL
91};
92
93const char *const mips_triplets[] = {
94 "mips-unknown-linux-gnu-",
95 "mipsel-linux-android-",
96 "mips-linux-gnu-",
97 "mips64-linux-gnu-",
98 "mips64el-linux-gnuabi64-",
99 "mips64-linux-gnuabi64-",
100 "mipsel-linux-gnu-",
101 NULL
102};
103
104static bool lookup_path(char *name)
105{
106 bool found = false;
107 char *path, *tmp = NULL;
108 char buf[PATH_MAX];
109 char *env = getenv("PATH");
110
111 if (!env)
112 return false;
113
114 env = strdup(env);
115 if (!env)
116 return false;
117
118 path = strtok_r(env, ":", &tmp);
119 while (path) {
120 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
121 if (access(buf, F_OK) == 0) {
122 found = true;
123 break;
124 }
125 path = strtok_r(NULL, ":", &tmp);
126 }
127 free(env);
128 return found;
129}
130
131static int lookup_triplets(const char *const *triplets, const char *name)
132{
133 int i;
134 char buf[PATH_MAX];
135
136 for (i = 0; triplets[i] != NULL; i++) {
137 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
138 if (lookup_path(buf))
139 return i;
140 }
141 return -1;
142}
143
144static int perf_env__lookup_binutils_path(struct perf_env *env,
145 const char *name, char **path)
146{
147 int idx;
148 const char *arch = perf_env__arch(env), *cross_env;
149 const char *const *path_list;
150 char *buf = NULL;
151
152 /*
153 * We don't need to try to find objdump path for native system.
154 * Just use default binutils path (e.g.: "objdump").
155 */
156 if (!strcmp(perf_env__arch(NULL), arch))
157 goto out;
158
159 cross_env = getenv("CROSS_COMPILE");
160 if (cross_env) {
161 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
162 goto out_error;
163 if (buf[0] == '/') {
164 if (access(buf, F_OK) == 0)
165 goto out;
166 goto out_error;
167 }
168 if (lookup_path(buf))
169 goto out;
170 zfree(&buf);
171 }
172
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;
195 else {
196 ui__error("binutils for %s not supported.\n", arch);
197 goto out_error;
198 }
199
200 idx = lookup_triplets(path_list, name);
201 if (idx < 0) {
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",
205 name, arch, name);
206 goto out_error;
207 }
208
209 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
210 goto out_error;
211
212out:
213 *path = buf;
214 return 0;
215out_error:
216 free(buf);
217 *path = NULL;
218 return -1;
219}
220
221int perf_env__lookup_objdump(struct perf_env *env, char **path)
222{
223 /*
224 * For live mode, env->arch will be NULL and we can use
225 * the native objdump tool.
226 */
227 if (env->arch == NULL)
228 return 0;
229
230 return perf_env__lookup_binutils_path(env, "objdump", path);
231}
232
233/*
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
236 * space.
237 */
238bool perf_env__single_address_space(struct perf_env *env)
239{
240 return strcmp(perf_env__arch(env), "sparc");
241}