Handle gcc __builtin_strcmp using 128/256 bit vectors with sse4.1, avx/avx2
[valgrind.git] / coregrind / launcher-linux.c
blob715fdab818800e54cb6b193a8bcd2cc65cdcd60b
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Launching valgrind m_launcher.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Julian Seward
12 jseward@acm.org
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 /* Note: this is a "normal" program and not part of Valgrind proper,
31 and so it doesn't have to conform to Valgrind's arcane rules on
32 no-glibc-usage etc. */
34 /* Include valgrind headers before system headers to avoid problems
35 with the system headers #defining things which are used as names
36 of structure members in vki headers. */
38 #include "pub_core_debuglog.h"
39 #include "pub_core_vki.h" // Avoids warnings from
40 // pub_core_libcfile.h
41 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
42 #include "pub_core_ume.h"
44 #include <assert.h>
45 #include <ctype.h>
46 #include <elf.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 #ifndef EM_X86_64
55 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
56 #endif
58 #ifndef EM_AARCH64
59 #define EM_AARCH64 183 // ditto
60 #endif
62 #ifndef EM_PPC64
63 #define EM_PPC64 21 // ditto
64 #endif
66 #ifndef EM_NANOMIPS
67 #define EM_NANOMIPS 249
68 #endif
70 #ifndef E_MIPS_ABI_O32
71 #define E_MIPS_ABI_O32 0x00001000
72 #endif
74 #ifndef E_MIPS_ABI2
75 #define E_MIPS_ABI2 0x00000020
76 #endif
78 /* Report fatal errors */
79 __attribute__((noreturn))
80 static void barf ( const char *format, ... )
82 va_list vargs;
84 va_start(vargs, format);
85 fprintf(stderr, "valgrind: Cannot continue: ");
86 vfprintf(stderr, format, vargs);
87 fprintf(stderr, "\n");
88 va_end(vargs);
90 exit(1);
91 /*NOTREACHED*/
92 assert(0);
95 /* Search the path for the client program */
96 static char *find_client(const char *clientname)
98 char *fullname;
99 const char *path = getenv("PATH");
100 const char *colon;
102 assert(clientname != NULL);
104 if (path == NULL) return strdup(clientname);
106 /* Make the size of the FULLNAME buffer large enough. */
107 unsigned need = strlen(path) + strlen("/") + strlen(clientname) + 1;
109 fullname = malloc(need);
110 if (fullname == NULL)
111 barf("malloc of fullname failed.");
113 while (path)
115 if ((colon = strchr(path, ':')) == NULL)
117 strcpy(fullname, path);
118 path = NULL;
120 else
122 strncpy(fullname, path, colon - path);
123 fullname[colon - path] = '\0';
124 path = colon + 1;
127 strcat(fullname, "/");
128 strcat(fullname, clientname);
130 if (access(fullname, R_OK|X_OK) == 0)
131 return fullname;
132 else if (access(fullname, X_OK) == 0)
133 barf("Need read permission on %s", fullname);
135 free(fullname);
137 return strdup(clientname);
140 /* Examine the client and work out which platform it is for */
141 static const char *select_platform(const char *clientname)
143 int fd;
144 union {
145 char c[4096];
146 Elf32_Ehdr ehdr32;
147 Elf64_Ehdr ehdr64;
148 } header;
149 ssize_t n_bytes;
150 const char *platform = NULL;
151 char *client;
153 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
155 if (strchr(clientname, '/') == NULL)
156 client = find_client(clientname);
157 else
158 client = strdup(clientname);
160 if (strcmp (client, clientname) != 0)
161 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", client);
163 if ((fd = open(client, O_RDONLY)) < 0) {
164 return_null:
165 free (client);
166 return NULL;
168 // barf("open(%s): %s", clientname, strerror(errno));
170 VG_(debugLog)(2, "launcher", "opened '%s'\n", client);
172 n_bytes = read(fd, header.c, sizeof(header));
173 close(fd);
174 if (n_bytes < 2) {
175 goto return_null;
178 VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
179 (long int)n_bytes, client);
181 if (header.c[0] == '#' && header.c[1] == '!') {
182 int i = 2;
184 STATIC_ASSERT(VKI_BINPRM_BUF_SIZE < sizeof header);
185 if (n_bytes > VKI_BINPRM_BUF_SIZE)
186 n_bytes = VKI_BINPRM_BUF_SIZE - 1;
187 header.c[n_bytes] = '\0';
188 char *eol = strchr(header.c, '\n');
189 if (eol != NULL)
190 *eol = '\0';
192 // Skip whitespace.
193 while (header.c[i] == ' '|| header.c[i] == '\t')
194 i++;
196 // Get the interpreter name.
197 const char *interp = header.c + i;
199 if (header.c[i] == '\0') {
200 // No interpreter was found; fall back to default shell
201 # if defined(VGPV_arm_linux_android) \
202 || defined(VGPV_x86_linux_android) \
203 || defined(VGPV_mips32_linux_android) \
204 || defined(VGPV_arm64_linux_android)
205 interp = "/system/bin/sh";
206 # else
207 interp = "/bin/sh";
208 # endif
209 } else {
210 while (header.c[i]) {
211 if (header.c[i] == ' ' || header.c[i] == '\t') break;
212 i++;
214 header.c[i] = '\0';
217 platform = select_platform(interp);
219 } else if (n_bytes >= SELFMAG && memcmp(header.c, ELFMAG, SELFMAG) == 0) {
221 if (n_bytes >= sizeof(Elf32_Ehdr) && header.c[EI_CLASS] == ELFCLASS32) {
223 if (header.c[EI_DATA] == ELFDATA2LSB) {
224 # if defined(VGO_solaris)
225 if (header.ehdr32.e_machine == EM_386 &&
226 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
227 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) {
228 platform = "x86-solaris";
230 else
231 # endif
232 if (header.ehdr32.e_machine == EM_386 &&
233 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
234 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
235 platform = "x86-linux";
237 else
238 if (header.ehdr32.e_machine == EM_ARM &&
239 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
240 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
241 platform = "arm-linux";
243 else
244 if (header.ehdr32.e_machine == EM_MIPS &&
245 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
246 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
247 (header.ehdr32.e_flags & E_MIPS_ABI_O32)) {
248 platform = "mips32-linux";
250 else
251 if (header.ehdr32.e_machine == EM_MIPS &&
252 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
253 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
254 (header.ehdr32.e_flags & E_MIPS_ABI2)) {
255 platform = "mips64-linux";
257 else
258 if (header.ehdr32.e_machine == EM_NANOMIPS &&
259 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
260 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
261 platform = "nanomips-linux";
264 else if (header.c[EI_DATA] == ELFDATA2MSB) {
265 if (header.ehdr32.e_machine == EM_PPC &&
266 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
267 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
268 platform = "ppc32-linux";
270 else
271 if (header.ehdr32.e_machine == EM_MIPS &&
272 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
273 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
274 (header.ehdr32.e_flags & E_MIPS_ABI_O32)) {
275 platform = "mips32-linux";
277 else
278 if (header.ehdr32.e_machine == EM_MIPS &&
279 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
280 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
281 (header.ehdr32.e_flags & E_MIPS_ABI2)) {
282 platform = "mips64-linux";
284 else
285 if (header.ehdr32.e_machine == EM_NANOMIPS &&
286 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
287 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
288 platform = "nanomips-linux";
292 } else if (n_bytes >= sizeof(Elf64_Ehdr) && header.c[EI_CLASS] == ELFCLASS64) {
294 if (header.c[EI_DATA] == ELFDATA2LSB) {
295 # if defined(VGO_solaris)
296 if (header.ehdr64.e_machine == EM_X86_64 &&
297 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
298 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) {
299 platform = "amd64-solaris";
301 else
302 # endif
303 if (header.ehdr64.e_machine == EM_X86_64 &&
304 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
305 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
306 platform = "amd64-linux";
307 } else if (header.ehdr64.e_machine == EM_MIPS &&
308 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
309 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
310 platform = "mips64-linux";
311 } else if (header.ehdr64.e_machine == EM_AARCH64 &&
312 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
313 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
314 platform = "arm64-linux";
315 } else if (header.ehdr64.e_machine == EM_PPC64 &&
316 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
317 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
318 platform = "ppc64le-linux";
320 } else if (header.c[EI_DATA] == ELFDATA2MSB) {
321 # if !defined(VGPV_arm_linux_android) \
322 && !defined(VGPV_x86_linux_android) \
323 && !defined(VGPV_mips32_linux_android) \
324 && !defined(VGPV_arm64_linux_android)
325 if (header.ehdr64.e_machine == EM_PPC64 &&
326 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
327 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
328 platform = "ppc64be-linux";
330 else
331 if (header.ehdr64.e_machine == EM_S390 &&
332 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
333 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
334 platform = "s390x-linux";
335 } else if (header.ehdr64.e_machine == EM_MIPS &&
336 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
337 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
338 platform = "mips64-linux";
340 # endif
345 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
346 platform ? platform : "unknown");
348 free (client);
350 return platform;
353 /* Where we expect to find all our aux files */
354 static const char *valgrind_lib = VG_LIBDIR;
356 int main(int argc, char** argv, char** envp)
358 int i, j, loglevel, r;
359 const char *toolname = NULL;
360 const char *clientname = NULL;
361 const char *platform;
362 const char *default_platform;
363 const char *cp;
364 const char *linkname;
365 char *toolfile;
366 const char *launcher_name;
367 char* new_line;
368 char** new_env;
370 /* Start the debugging-log system ASAP. First find out how many
371 "-d"s were specified. This is a pre-scan of the command line.
372 At the same time, look for the tool name. */
373 loglevel = 0;
374 for (i = 1; i < argc; i++) {
375 if (argv[i][0] != '-') {
376 clientname = argv[i];
377 break;
379 if (0 == strcmp(argv[i], "--")) {
380 if (i+1 < argc)
381 clientname = argv[i+1];
382 break;
384 if (0 == strcmp(argv[i], "-d"))
385 loglevel++;
386 if (0 == strncmp(argv[i], "--tool=", 7))
387 toolname = argv[i] + 7;
390 /* ... and start the debug logger. Now we can safely emit logging
391 messages all through startup. */
392 VG_(debugLog_startup)(loglevel, "Stage 1");
394 /* Make sure we know which tool we're using */
395 if (toolname) {
396 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
397 } else {
398 VG_(debugLog)(1, "launcher",
399 "no tool requested, defaulting to 'memcheck'\n");
400 toolname = "memcheck";
403 /* Select a platform to use if we can't decide that by looking at
404 the executable (eg because it's a shell script). VG_PLATFORM is the
405 default_platform. Its value is defined in coregrind/Makefile.am and
406 typically it is the primary build target. Unless the primary build
407 target is not built is not built in which case VG_PLATFORM is the
408 secondary build target. */
409 # if defined(VGO_linux)
410 if ((0==strcmp(VG_PLATFORM,"x86-linux")) ||
411 (0==strcmp(VG_PLATFORM,"amd64-linux")) ||
412 (0==strcmp(VG_PLATFORM,"ppc32-linux")) ||
413 (0==strcmp(VG_PLATFORM,"ppc64be-linux")) ||
414 (0==strcmp(VG_PLATFORM,"ppc64le-linux")) ||
415 (0==strcmp(VG_PLATFORM,"arm-linux")) ||
416 (0==strcmp(VG_PLATFORM,"arm64-linux")) ||
417 (0==strcmp(VG_PLATFORM,"s390x-linux")) ||
418 (0==strcmp(VG_PLATFORM,"mips32-linux")) ||
419 (0==strcmp(VG_PLATFORM,"mips64-linux")) ||
420 (0==strcmp(VG_PLATFORM,"nanomips-linux")))
421 default_platform = VG_PLATFORM;
422 # elif defined(VGO_solaris)
423 if ((0==strcmp(VG_PLATFORM,"x86-solaris")) ||
424 (0==strcmp(VG_PLATFORM,"amd64-solaris")))
425 default_platform = SOLARIS_LAUNCHER_DEFAULT_PLATFORM;
426 # else
427 # error Unknown OS
428 # endif
429 else
430 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
432 /* Work out what platform to use, or use the default platform if
433 not possible. */
434 if (clientname == NULL) {
435 VG_(debugLog)(1, "launcher",
436 "no client specified, defaulting platform to '%s'\n",
437 default_platform);
438 platform = default_platform;
439 } else if ((platform = select_platform(clientname)) != NULL) {
440 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
441 } else {
442 VG_(debugLog)(1, "launcher",
443 "no platform detected, defaulting platform to '%s'\n",
444 default_platform);
445 platform = default_platform;
448 /* Figure out the name of this executable (viz, the launcher), so
449 we can tell stage2. stage2 will use the name for recursive
450 invocations of valgrind on child processes. */
451 # if defined(VGO_linux)
452 linkname = "/proc/self/exe";
453 # elif defined(VGO_solaris)
454 linkname = "/proc/self/path/a.out";
455 # else
456 # error Unknown OS
457 # endif
458 unsigned bufsiz = 0;
459 char *buf = NULL;
461 while (42) {
462 bufsiz += 500;
463 buf = realloc(buf, bufsiz);
464 if (buf == NULL)
465 barf("realloc of buf failed.");
466 r = readlink(linkname, buf, bufsiz);
467 if (r == -1) {
468 /* If /proc/self/exe (/proc/self/path/a.out) can't be followed, don't
469 give up. Instead continue with an empty string for VALGRIND_LAUNCHER.
470 In the sys_execve wrapper, this is tested, and if found to be empty,
471 fail the execve. */
472 fprintf(stderr, "valgrind: warning (non-fatal): "
473 "readlink(\"%s\") failed.\n", linkname);
474 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
475 "will not work.\n");
476 launcher_name = "";
477 break;
479 if (r == bufsiz) continue; // buffer to small; retry
481 assert(r < bufsiz); // paranoia
483 buf[r] = '\0';
484 launcher_name = buf;
485 break;
488 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
489 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
490 + strlen(launcher_name) + 1);
491 if (new_line == NULL)
492 barf("malloc of new_line failed.");
493 strcpy(new_line, VALGRIND_LAUNCHER);
494 strcat(new_line, "=");
495 strcat(new_line, launcher_name);
497 for (j = 0; envp[j]; j++)
499 new_env = malloc((j+2) * sizeof(char*));
500 if (new_env == NULL)
501 barf("malloc of new_env failed.");
502 for (i = 0; i < j; i++)
503 new_env[i] = envp[i];
504 new_env[i++] = new_line;
505 new_env[i++] = NULL;
506 assert(i == j+2);
508 /* Establish the correct VALGRIND_LIB. */
509 cp = getenv(VALGRIND_LIB);
511 if (cp != NULL)
512 valgrind_lib = cp;
514 /* Build the stage2 invocation, and execve it. Bye! */
515 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
516 if (toolfile == NULL)
517 barf("malloc of toolfile failed.");
518 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
520 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
522 execve(toolfile, argv, new_env);
524 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
525 toolname, platform, strerror(errno));
527 exit(1);