blob: 5f69fd0b745a45315efe5c698aac8edf7d9f3bb9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Irina Tirdea68e94f42012-10-16 02:33:38 +03002#include <stdio.h>
Irina Tirdea68e94f42012-10-16 02:33:38 +03003#include "common.h"
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -03004#include "../util/env.h"
Arnaldo Carvalho de Melo175729f2016-07-07 11:38:09 -03005#include "../util/util.h"
Irina Tirdea68e94f42012-10-16 02:33:38 +03006#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-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053014 "arm-linux-gnu-",
15 "arm-linux-gnueabihf-",
16 "arm-none-eabi-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030017 NULL
18};
19
Elliott Hughesc4d2df42014-08-14 12:39:20 -070020const char *const arm64_triplets[] = {
21 "aarch64-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053022 "aarch64-linux-gnu-",
Elliott Hughesc4d2df42014-08-14 12:39:20 -070023 NULL
24};
25
Irina Tirdea68e94f42012-10-16 02:33:38 +030026const char *const powerpc_triplets[] = {
27 "powerpc-unknown-linux-gnu-",
Kim Phillips6db47fd2017-05-29 14:27:54 -050028 "powerpc-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030029 "powerpc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053030 "powerpc64-linux-gnu-",
31 "powerpc64le-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030032 NULL
33};
34
35const char *const s390_triplets[] = {
36 "s390-ibm-linux-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053037 "s390x-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030038 NULL
39};
40
41const char *const sh_triplets[] = {
42 "sh-unknown-linux-gnu-",
43 "sh64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053044 "sh-linux-gnu-",
45 "sh64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030046 NULL
47};
48
49const char *const sparc_triplets[] = {
50 "sparc-unknown-linux-gnu-",
51 "sparc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053052 "sparc64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030053 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-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053065 "x86_64-linux-gnu-",
66 "i586-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030067 NULL
68};
69
70const char *const mips_triplets[] = {
71 "mips-unknown-linux-gnu-",
72 "mipsel-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053073 "mips-linux-gnu-",
74 "mips64-linux-gnu-",
75 "mips64el-linux-gnuabi64-",
76 "mips64-linux-gnuabi64-",
77 "mipsel-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030078 NULL
79};
80
81static bool lookup_path(char *name)
82{
83 bool found = false;
Martin Liška5bcaaca2015-05-26 11:41:37 -030084 char *path, *tmp = NULL;
Irina Tirdea68e94f42012-10-16 02:33:38 +030085 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
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300121static int perf_env__lookup_binutils_path(struct perf_env *env,
122 const char *name, const char **path)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300123{
124 int idx;
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -0300125 const char *arch = perf_env__arch(env), *cross_env;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300126 const char *const *path_list;
127 char *buf = NULL;
128
Irina Tirdea68e94f42012-10-16 02:33:38 +0300129 /*
130 * We don't need to try to find objdump path for native system.
131 * Just use default binutils path (e.g.: "objdump").
132 */
Arnaldo Carvalho de Melo4e8fbc12017-12-11 14:47:49 -0300133 if (!strcmp(perf_env__arch(NULL), arch))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300134 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;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300147 zfree(&buf);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300148 }
149
Irina Tirdea68e94f42012-10-16 02:33:38 +0300150 if (!strcmp(arch, "arm"))
151 path_list = arm_triplets;
Elliott Hughesc4d2df42014-08-14 12:39:20 -0700152 else if (!strcmp(arch, "arm64"))
153 path_list = arm64_triplets;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300154 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;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900162 else if (!strcmp(arch, "x86"))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300163 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
Arnaldo Carvalho de Melof178fd22018-05-28 14:24:45 -0300192int perf_env__lookup_objdump(struct perf_env *env, const char **path)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300193{
Namhyung Kimff6f7772012-11-07 10:30:15 +0900194 /*
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
Arnaldo Carvalho de Melof178fd22018-05-28 14:24:45 -0300201 return perf_env__lookup_binutils_path(env, "objdump", path);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300202}
Adrian Hunterec1891a2018-11-06 23:07:10 +0200203
204/*
205 * Some architectures have a single address space for kernel and user addresses,
206 * which makes it possible to determine if an address is in kernel space or user
207 * space.
208 */
209bool perf_env__single_address_space(struct perf_env *env)
210{
211 return strcmp(perf_env__arch(env), "sparc");
212}