blob: 088f1dba535c3967f1bacbec35cc9ddc7af7d986 [file] [log] [blame]
Misha Brukman3b87f212004-08-04 21:19:49 +00001/*===-- 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 Brukman0abaaf42003-08-11 22:29:36 +000014
15#include "SysUtils.h"
Misha Brukman593ece02003-08-15 23:31:16 +000016#include "Config/unistd.h"
Chris Lattner335eb9d2004-01-10 19:12:09 +000017#include <errno.h>
18#include <stdlib.h>
Misha Brukman593ece02003-08-15 23:31:16 +000019#include <fcntl.h>
Misha Brukman0abaaf42003-08-11 22:29:36 +000020#include <stdio.h>
21#include <string.h>
22
23/*
24 * This is the expected header for all valid LLVM bytecode files.
25 * The first four characters must be exactly this.
26 */
27static const char llvmHeader[] = "llvm";
28
29/*
Misha Brukman0abaaf42003-08-11 22:29:36 +000030 * This replacement execve() function first checks the file to be executed
31 * to see if it is a valid LLVM bytecode file, and then either invokes our
32 * execution environment or passes it on to the system execve() call.
33 */
34int execve(const char *filename, char *const argv[], char *const envp[])
35{
36 /* Open the file, test to see if first four characters are "llvm" */
Misha Brukman593ece02003-08-15 23:31:16 +000037 size_t headerSize = strlen(llvmHeader);
38 char header[headerSize];
Misha Brukman2e1fbdd2003-09-29 22:37:00 +000039 char* realFilename = 0;
40 /*
41 * If the program is specified with a relative or absolute path,
42 * then just use the path and filename as is, otherwise search for it.
43 */
44 if (filename[0] != '.' && filename[0] != '/')
45 realFilename = FindExecutable(filename);
46 else
47 realFilename = (char*) filename;
48 if (!realFilename) {
49 fprintf(stderr, "Cannot find path to `%s', exiting.\n", filename);
50 return -1;
51 }
Misha Brukman593ece02003-08-15 23:31:16 +000052 errno = 0;
Misha Brukman2e1fbdd2003-09-29 22:37:00 +000053 int file = open(realFilename, O_RDONLY);
Misha Brukman0abaaf42003-08-11 22:29:36 +000054 /* Check validity of `file' */
Misha Brukman593ece02003-08-15 23:31:16 +000055 if (errno) return EIO;
Misha Brukman0abaaf42003-08-11 22:29:36 +000056 /* Read the header from the file */
Misha Brukman593ece02003-08-15 23:31:16 +000057 ssize_t bytesRead = read(file, header, headerSize);
58 close(file);
Misha Brukman00ce8402003-09-27 22:26:37 +000059 if (bytesRead != (ssize_t)headerSize) return EIO;
Misha Brukman593ece02003-08-15 23:31:16 +000060 if (!memcmp(llvmHeader, header, headerSize)) {
Misha Brukman0abaaf42003-08-11 22:29:36 +000061 /*
62 * This is a bytecode file, so execute the JIT with the program and
63 * parameters.
64 */
65 unsigned argvSize, idx;
66 for (argvSize = 0, idx = 0; argv[idx] && argv[idx][0]; ++idx)
67 ++argvSize;
68 char **LLIargs = (char**) malloc(sizeof(char*) * (argvSize+2));
69 char *LLIpath = FindExecutable("lli");
70 if (!LLIpath) {
71 fprintf(stderr, "Cannot find path to `lli', exiting.\n");
72 return -1;
73 }
74 LLIargs[0] = LLIpath;
Misha Brukman2e1fbdd2003-09-29 22:37:00 +000075 LLIargs[1] = realFilename;
Misha Brukman593ece02003-08-15 23:31:16 +000076 for (idx = 1; idx != argvSize; ++idx)
Misha Brukman0abaaf42003-08-11 22:29:36 +000077 LLIargs[idx+1] = argv[idx];
78 LLIargs[argvSize + 1] = '\0';
Misha Brukman0abaaf42003-08-11 22:29:36 +000079 return executeProgram(LLIpath, LLIargs, envp);
80 }
Misha Brukman593ece02003-08-15 23:31:16 +000081 return executeProgram(filename, argv, envp);
Misha Brukman0abaaf42003-08-11 22:29:36 +000082}