Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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