Misha Brukman | 3b87f21 | 2004-08-04 21:19:49 +0000 | [diff] [blame] | 1 | /*===-- ExecveHandler.c - Replaces execve() to run LLVM files -------------===*\ |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file was developed by the LLVM research group and is distributed under |
| 6 | * the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | *===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements a replacement execve() to spawn off LLVM programs to run |
| 11 | * transparently, without needing to be (JIT-)compiled manually by the user. |
| 12 | * |
| 13 | \*===----------------------------------------------------------------------===*/ |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 14 | |
| 15 | #include "SysUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Config/unistd.h" |
Chris Lattner | 335eb9d | 2004-01-10 19:12:09 +0000 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdlib.h> |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 19 | #include <fcntl.h> |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* |
Reid Spencer | 31391b6 | 2004-11-09 20:21:25 +0000 | [diff] [blame] | 24 | * These are the expected headers for all valid LLVM bytecode files. |
| 25 | * The first four characters must be one of these. |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 26 | */ |
Reid Spencer | 31391b6 | 2004-11-09 20:21:25 +0000 | [diff] [blame] | 27 | static const char llvmHeaderUncompressed[] = "llvm"; |
| 28 | static const char llvmHeaderCompressed[] = "llvc"; |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 31 | * This replacement execve() function first checks the file to be executed |
| 32 | * to see if it is a valid LLVM bytecode file, and then either invokes our |
| 33 | * execution environment or passes it on to the system execve() call. |
| 34 | */ |
| 35 | int execve(const char *filename, char *const argv[], char *const envp[]) |
| 36 | { |
| 37 | /* Open the file, test to see if first four characters are "llvm" */ |
Reid Spencer | 31391b6 | 2004-11-09 20:21:25 +0000 | [diff] [blame] | 38 | size_t headerSize = strlen(llvmHeaderCompressed); |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 39 | char header[headerSize]; |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 40 | char* realFilename = 0; |
| 41 | /* |
| 42 | * If the program is specified with a relative or absolute path, |
| 43 | * then just use the path and filename as is, otherwise search for it. |
| 44 | */ |
| 45 | if (filename[0] != '.' && filename[0] != '/') |
| 46 | realFilename = FindExecutable(filename); |
| 47 | else |
| 48 | realFilename = (char*) filename; |
| 49 | if (!realFilename) { |
| 50 | fprintf(stderr, "Cannot find path to `%s', exiting.\n", filename); |
| 51 | return -1; |
| 52 | } |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 53 | errno = 0; |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 54 | int file = open(realFilename, O_RDONLY); |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 55 | /* Check validity of `file' */ |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 56 | if (errno) return EIO; |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 57 | /* Read the header from the file */ |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 58 | ssize_t bytesRead = read(file, header, headerSize); |
| 59 | close(file); |
Misha Brukman | 00ce840 | 2003-09-27 22:26:37 +0000 | [diff] [blame] | 60 | if (bytesRead != (ssize_t)headerSize) return EIO; |
Reid Spencer | 31391b6 | 2004-11-09 20:21:25 +0000 | [diff] [blame] | 61 | if (!memcmp(llvmHeaderCompressed, header, headerSize) || |
| 62 | !memcmp(llvmHeaderUncompressed, header, headerSize)) { |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 63 | /* |
| 64 | * This is a bytecode file, so execute the JIT with the program and |
| 65 | * parameters. |
| 66 | */ |
| 67 | unsigned argvSize, idx; |
| 68 | for (argvSize = 0, idx = 0; argv[idx] && argv[idx][0]; ++idx) |
| 69 | ++argvSize; |
| 70 | char **LLIargs = (char**) malloc(sizeof(char*) * (argvSize+2)); |
| 71 | char *LLIpath = FindExecutable("lli"); |
| 72 | if (!LLIpath) { |
| 73 | fprintf(stderr, "Cannot find path to `lli', exiting.\n"); |
| 74 | return -1; |
| 75 | } |
| 76 | LLIargs[0] = LLIpath; |
Misha Brukman | 2e1fbdd | 2003-09-29 22:37:00 +0000 | [diff] [blame] | 77 | LLIargs[1] = realFilename; |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 78 | for (idx = 1; idx != argvSize; ++idx) |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 79 | LLIargs[idx+1] = argv[idx]; |
| 80 | LLIargs[argvSize + 1] = '\0'; |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 81 | return executeProgram(LLIpath, LLIargs, envp); |
| 82 | } |
Misha Brukman | 593ece0 | 2003-08-15 23:31:16 +0000 | [diff] [blame] | 83 | return executeProgram(filename, argv, envp); |
Misha Brukman | 0abaaf4 | 2003-08-11 22:29:36 +0000 | [diff] [blame] | 84 | } |