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