xref: /openbmc/linux/tools/perf/arch/common.c (revision 6d99a79c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include "common.h"
4 #include "../util/env.h"
5 #include "../util/util.h"
6 #include "../util/debug.h"
7 
8 const char *const arc_triplets[] = {
9 	"arc-linux-",
10 	"arc-snps-linux-uclibc-",
11 	"arc-snps-linux-gnu-",
12 	NULL
13 };
14 
15 const char *const arm_triplets[] = {
16 	"arm-eabi-",
17 	"arm-linux-androideabi-",
18 	"arm-unknown-linux-",
19 	"arm-unknown-linux-gnu-",
20 	"arm-unknown-linux-gnueabi-",
21 	"arm-linux-gnu-",
22 	"arm-linux-gnueabihf-",
23 	"arm-none-eabi-",
24 	NULL
25 };
26 
27 const char *const arm64_triplets[] = {
28 	"aarch64-linux-android-",
29 	"aarch64-linux-gnu-",
30 	NULL
31 };
32 
33 const char *const powerpc_triplets[] = {
34 	"powerpc-unknown-linux-gnu-",
35 	"powerpc-linux-gnu-",
36 	"powerpc64-unknown-linux-gnu-",
37 	"powerpc64-linux-gnu-",
38 	"powerpc64le-linux-gnu-",
39 	NULL
40 };
41 
42 const char *const s390_triplets[] = {
43 	"s390-ibm-linux-",
44 	"s390x-linux-gnu-",
45 	NULL
46 };
47 
48 const char *const sh_triplets[] = {
49 	"sh-unknown-linux-gnu-",
50 	"sh64-unknown-linux-gnu-",
51 	"sh-linux-gnu-",
52 	"sh64-linux-gnu-",
53 	NULL
54 };
55 
56 const char *const sparc_triplets[] = {
57 	"sparc-unknown-linux-gnu-",
58 	"sparc64-unknown-linux-gnu-",
59 	"sparc64-linux-gnu-",
60 	NULL
61 };
62 
63 const char *const x86_triplets[] = {
64 	"x86_64-pc-linux-gnu-",
65 	"x86_64-unknown-linux-gnu-",
66 	"i686-pc-linux-gnu-",
67 	"i586-pc-linux-gnu-",
68 	"i486-pc-linux-gnu-",
69 	"i386-pc-linux-gnu-",
70 	"i686-linux-android-",
71 	"i686-android-linux-",
72 	"x86_64-linux-gnu-",
73 	"i586-linux-gnu-",
74 	NULL
75 };
76 
77 const char *const mips_triplets[] = {
78 	"mips-unknown-linux-gnu-",
79 	"mipsel-linux-android-",
80 	"mips-linux-gnu-",
81 	"mips64-linux-gnu-",
82 	"mips64el-linux-gnuabi64-",
83 	"mips64-linux-gnuabi64-",
84 	"mipsel-linux-gnu-",
85 	NULL
86 };
87 
88 static bool lookup_path(char *name)
89 {
90 	bool found = false;
91 	char *path, *tmp = NULL;
92 	char buf[PATH_MAX];
93 	char *env = getenv("PATH");
94 
95 	if (!env)
96 		return false;
97 
98 	env = strdup(env);
99 	if (!env)
100 		return false;
101 
102 	path = strtok_r(env, ":", &tmp);
103 	while (path) {
104 		scnprintf(buf, sizeof(buf), "%s/%s", path, name);
105 		if (access(buf, F_OK) == 0) {
106 			found = true;
107 			break;
108 		}
109 		path = strtok_r(NULL, ":", &tmp);
110 	}
111 	free(env);
112 	return found;
113 }
114 
115 static int lookup_triplets(const char *const *triplets, const char *name)
116 {
117 	int i;
118 	char buf[PATH_MAX];
119 
120 	for (i = 0; triplets[i] != NULL; i++) {
121 		scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
122 		if (lookup_path(buf))
123 			return i;
124 	}
125 	return -1;
126 }
127 
128 static int perf_env__lookup_binutils_path(struct perf_env *env,
129 					  const char *name, const char **path)
130 {
131 	int idx;
132 	const char *arch = perf_env__arch(env), *cross_env;
133 	const char *const *path_list;
134 	char *buf = NULL;
135 
136 	/*
137 	 * We don't need to try to find objdump path for native system.
138 	 * Just use default binutils path (e.g.: "objdump").
139 	 */
140 	if (!strcmp(perf_env__arch(NULL), arch))
141 		goto out;
142 
143 	cross_env = getenv("CROSS_COMPILE");
144 	if (cross_env) {
145 		if (asprintf(&buf, "%s%s", cross_env, name) < 0)
146 			goto out_error;
147 		if (buf[0] == '/') {
148 			if (access(buf, F_OK) == 0)
149 				goto out;
150 			goto out_error;
151 		}
152 		if (lookup_path(buf))
153 			goto out;
154 		zfree(&buf);
155 	}
156 
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;
175 	else {
176 		ui__error("binutils for %s not supported.\n", arch);
177 		goto out_error;
178 	}
179 
180 	idx = lookup_triplets(path_list, name);
181 	if (idx < 0) {
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",
185 			  name, arch, name);
186 		goto out_error;
187 	}
188 
189 	if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
190 		goto out_error;
191 
192 out:
193 	*path = buf;
194 	return 0;
195 out_error:
196 	free(buf);
197 	*path = NULL;
198 	return -1;
199 }
200 
201 int perf_env__lookup_objdump(struct perf_env *env, const char **path)
202 {
203 	/*
204 	 * For live mode, env->arch will be NULL and we can use
205 	 * the native objdump tool.
206 	 */
207 	if (env->arch == NULL)
208 		return 0;
209 
210 	return perf_env__lookup_binutils_path(env, "objdump", path);
211 }
212 
213 /*
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
216  * space.
217  */
218 bool perf_env__single_address_space(struct perf_env *env)
219 {
220 	return strcmp(perf_env__arch(env), "sparc");
221 }
222