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