Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 s390_triplets[] = {
47 "s390-ibm-linux-",
48 "s390x-linux-gnu-",
49 NULL
50};
51
52const char *const sh_triplets[] = {
53 "sh-unknown-linux-gnu-",
54 "sh-linux-gnu-",
55 NULL
56};
57
58const char *const sparc_triplets[] = {
59 "sparc-unknown-linux-gnu-",
60 "sparc64-unknown-linux-gnu-",
61 "sparc64-linux-gnu-",
62 NULL
63};
64
65const char *const x86_triplets[] = {
66 "x86_64-pc-linux-gnu-",
67 "x86_64-unknown-linux-gnu-",
68 "i686-pc-linux-gnu-",
69 "i586-pc-linux-gnu-",
70 "i486-pc-linux-gnu-",
71 "i386-pc-linux-gnu-",
72 "i686-linux-android-",
73 "i686-android-linux-",
74 "x86_64-linux-gnu-",
75 "i586-linux-gnu-",
76 NULL
77};
78
79const char *const mips_triplets[] = {
80 "mips-unknown-linux-gnu-",
81 "mipsel-linux-android-",
82 "mips-linux-gnu-",
83 "mips64-linux-gnu-",
84 "mips64el-linux-gnuabi64-",
85 "mips64-linux-gnuabi64-",
86 "mipsel-linux-gnu-",
87 NULL
88};
89
90static bool lookup_path(char *name)
91{
92 bool found = false;
93 char *path, *tmp = NULL;
94 char buf[PATH_MAX];
95 char *env = getenv("PATH");
96
97 if (!env)
98 return false;
99
100 env = strdup(env);
101 if (!env)
102 return false;
103
104 path = strtok_r(env, ":", &tmp);
105 while (path) {
106 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
107 if (access(buf, F_OK) == 0) {
108 found = true;
109 break;
110 }
111 path = strtok_r(NULL, ":", &tmp);
112 }
113 free(env);
114 return found;
115}
116
117static int lookup_triplets(const char *const *triplets, const char *name)
118{
119 int i;
120 char buf[PATH_MAX];
121
122 for (i = 0; triplets[i] != NULL; i++) {
123 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
124 if (lookup_path(buf))
125 return i;
126 }
127 return -1;
128}
129
130static int perf_env__lookup_binutils_path(struct perf_env *env,
131 const char *name, char **path)
132{
133 int idx;
134 const char *arch = perf_env__arch(env), *cross_env;
135 const char *const *path_list;
136 char *buf = NULL;
137
138 /*
139 * We don't need to try to find objdump path for native system.
140 * Just use default binutils path (e.g.: "objdump").
141 */
142 if (!strcmp(perf_env__arch(NULL), arch))
143 goto out;
144
145 cross_env = getenv("CROSS_COMPILE");
146 if (cross_env) {
147 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
148 goto out_error;
149 if (buf[0] == '/') {
150 if (access(buf, F_OK) == 0)
151 goto out;
152 goto out_error;
153 }
154 if (lookup_path(buf))
155 goto out;
156 zfree(&buf);
157 }
158
159 if (!strcmp(arch, "arc"))
160 path_list = arc_triplets;
161 else if (!strcmp(arch, "arm"))
162 path_list = arm_triplets;
163 else if (!strcmp(arch, "arm64"))
164 path_list = arm64_triplets;
165 else if (!strcmp(arch, "powerpc"))
166 path_list = powerpc_triplets;
167 else if (!strcmp(arch, "sh"))
168 path_list = sh_triplets;
169 else if (!strcmp(arch, "s390"))
170 path_list = s390_triplets;
171 else if (!strcmp(arch, "sparc"))
172 path_list = sparc_triplets;
173 else if (!strcmp(arch, "x86"))
174 path_list = x86_triplets;
175 else if (!strcmp(arch, "mips"))
176 path_list = mips_triplets;
177 else {
178 ui__error("binutils for %s not supported.\n", arch);
179 goto out_error;
180 }
181
182 idx = lookup_triplets(path_list, name);
183 if (idx < 0) {
184 ui__error("Please install %s for %s.\n"
185 "You can add it to PATH, set CROSS_COMPILE or "
186 "override the default using --%s.\n",
187 name, arch, name);
188 goto out_error;
189 }
190
191 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
192 goto out_error;
193
194out:
195 *path = buf;
196 return 0;
197out_error:
198 free(buf);
199 *path = NULL;
200 return -1;
201}
202
203int perf_env__lookup_objdump(struct perf_env *env, char **path)
204{
205 /*
206 * For live mode, env->arch will be NULL and we can use
207 * the native objdump tool.
208 */
209 if (env->arch == NULL)
210 return 0;
211
212 return perf_env__lookup_binutils_path(env, "objdump", path);
213}
214
215/*
216 * Some architectures have a single address space for kernel and user addresses,
217 * which makes it possible to determine if an address is in kernel space or user
218 * space.
219 */
220bool perf_env__single_address_space(struct perf_env *env)
221{
222 return strcmp(perf_env__arch(env), "sparc");
223}