| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// | 
| John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 9 | // | 
|  | 10 | // This utility is intended to be compatible with GCC, and follows standard | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 11 | // system 'ld' conventions.  As such, the default output file is ./a.out. | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 12 | // Additionally, this program outputs a shell script that is used to invoke LLI | 
|  | 13 | // to execute the program.  In this manner, the generated executable (a.out for | 
|  | 14 | // example), is directly executable, whereas the bytecode file actually lives in | 
|  | 15 | // the a.out.bc file generated by this program.  Also, Force is on by default. | 
|  | 16 | // | 
|  | 17 | // Note that if someone (or a script) deletes the executable program generated, | 
|  | 18 | // the .bc file will be left around.  Considering that this is a temporary hack, | 
| Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 19 | // I'm not too worried about this. | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 20 | // | 
|  | 21 | //===----------------------------------------------------------------------===// | 
|  | 22 |  | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 23 | #include "gccld.h" | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" | 
| Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 25 | #include "llvm/PassManager.h" | 
|  | 26 | #include "llvm/Bytecode/Reader.h" | 
|  | 27 | #include "llvm/Bytecode/WriteBytecodePass.h" | 
| Chris Lattner | d571e2a | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" | 
| Chris Lattner | 35c4541 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/IPO.h" | 
| Chris Lattner | 35c4541 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Linker.h" | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 32 | #include "Support/CommandLine.h" | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 33 | #include "Support/FileUtilities.h" | 
| Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 34 | #include "Support/Signals.h" | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 35 | #include "Support/SystemUtils.h" | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 36 | #include <fstream> | 
|  | 37 | #include <memory> | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 38 |  | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | using namespace llvm; | 
|  | 40 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 41 | namespace { | 
|  | 42 | cl::list<std::string> | 
|  | 43 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), | 
|  | 44 | cl::OneOrMore); | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 45 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 46 | cl::opt<std::string> | 
|  | 47 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), | 
|  | 48 | cl::value_desc("filename")); | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 49 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 50 | cl::opt<bool> | 
|  | 51 | Verbose("v", cl::desc("Print information about actions taken")); | 
|  | 52 |  | 
|  | 53 | cl::list<std::string> | 
|  | 54 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, | 
|  | 55 | cl::value_desc("directory")); | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 56 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 57 | cl::list<std::string> | 
|  | 58 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, | 
|  | 59 | cl::value_desc("library prefix")); | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 60 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 61 | cl::opt<bool> | 
|  | 62 | Strip("s", cl::desc("Strip symbol info from executable")); | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 63 |  | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 64 | cl::opt<bool> | 
|  | 65 | NoInternalize("disable-internalize", | 
|  | 66 | cl::desc("Do not mark all symbols as internal")); | 
| Chris Lattner | c17fe1c | 2003-11-05 06:05:21 +0000 | [diff] [blame] | 67 | cl::alias | 
| Chris Lattner | b4d9921 | 2003-08-22 19:18:45 +0000 | [diff] [blame] | 68 | ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), | 
|  | 69 | cl::aliasopt(NoInternalize)); | 
| Chris Lattner | 602d209 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 70 |  | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 71 | cl::opt<bool> | 
|  | 72 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" | 
|  | 73 | " library, not an executable")); | 
| Chris Lattner | c17fe1c | 2003-11-05 06:05:21 +0000 | [diff] [blame] | 74 | cl::alias | 
|  | 75 | Relink("r", cl::desc("Alias for -link-as-library"), | 
|  | 76 | cl::aliasopt(LinkAsLibrary)); | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 77 |  | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 78 | cl::opt<bool> | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 79 | Native("native", | 
|  | 80 | cl::desc("Generate a native binary instead of a shell script")); | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 81 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 82 | // Compatibility options that are ignored but supported by LD | 
| Chris Lattner | 602d209 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 83 | cl::opt<std::string> | 
|  | 84 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); | 
|  | 85 | cl::opt<std::string> | 
|  | 86 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); | 
|  | 87 | cl::opt<bool> | 
|  | 88 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); | 
| Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 89 | } | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 90 |  | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 91 | namespace llvm { | 
|  | 92 |  | 
| Misha Brukman | d16a037 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 93 | /// PrintAndReturn - Prints a message to standard error and returns a value | 
|  | 94 | /// usable for an exit status. | 
|  | 95 | /// | 
|  | 96 | /// Inputs: | 
|  | 97 | ///  progname - The name of the program (i.e. argv[0]). | 
|  | 98 | ///  Message  - The message to print to standard error. | 
|  | 99 | ///  Extra    - Extra information to print between the program name and thei | 
|  | 100 | ///             message.  It is optional. | 
|  | 101 | /// | 
|  | 102 | /// Return value: | 
|  | 103 | ///  Returns a value that can be used as the exit status (i.e. for exit()). | 
|  | 104 | /// | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 105 | int | 
| Misha Brukman | d16a037 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 106 | PrintAndReturn(const char *progname, | 
|  | 107 | const std::string &Message, | 
|  | 108 | const std::string &Extra) | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 109 | { | 
|  | 110 | std::cerr << progname << Extra << ": " << Message << "\n"; | 
|  | 111 | return 1; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Misha Brukman | d16a037 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 114 | /// CopyEnv - This function takes an array of environment variables and makes a | 
|  | 115 | /// copy of it.  This copy can then be manipulated any way the caller likes | 
|  | 116 | /// without affecting the process's real environment. | 
|  | 117 | /// | 
|  | 118 | /// Inputs: | 
|  | 119 | ///  envp - An array of C strings containing an environment. | 
|  | 120 | /// | 
|  | 121 | /// Return value: | 
|  | 122 | ///  NULL - An error occurred. | 
|  | 123 | /// | 
|  | 124 | ///  Otherwise, a pointer to a new array of C strings is returned.  Every string | 
|  | 125 | ///  in the array is a duplicate of the one in the original array (i.e. we do | 
|  | 126 | ///  not copy the char *'s from one array to another). | 
|  | 127 | /// | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 128 | char ** CopyEnv(char ** const envp) { | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 129 | // Count the number of entries in the old list; | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 130 | unsigned entries;   // The number of entries in the old environment list | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 131 | for (entries = 0; envp[entries] != NULL; entries++) | 
|  | 132 | { | 
|  | 133 | ; | 
| Chris Lattner | a2d3504 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 136 | // Add one more entry for the NULL pointer that ends the list. | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 137 | ++entries; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 138 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 139 | // If there are no entries at all, just return NULL. | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 140 | if (entries == 0) | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 141 | return NULL; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 142 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 143 | // Allocate a new environment list. | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 144 | char **newenv; | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 145 | if ((newenv = new (char *) [entries]) == NULL) | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 146 | return NULL; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 147 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 148 | // Make a copy of the list.  Don't forget the NULL that ends the list. | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 149 | entries = 0; | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 150 | while (envp[entries] != NULL) { | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 151 | newenv[entries] = new char[strlen (envp[entries]) + 1]; | 
|  | 152 | strcpy (newenv[entries], envp[entries]); | 
|  | 153 | ++entries; | 
|  | 154 | } | 
|  | 155 | newenv[entries] = NULL; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 156 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 157 | return newenv; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 |  | 
| Misha Brukman | d16a037 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 161 | /// RemoveEnv - Remove the specified environment variable from the environment | 
|  | 162 | /// array. | 
|  | 163 | /// | 
|  | 164 | /// Inputs: | 
|  | 165 | ///  name - The name of the variable to remove.  It cannot be NULL. | 
|  | 166 | ///  envp - The array of environment variables.  It cannot be NULL. | 
|  | 167 | /// | 
|  | 168 | /// Notes: | 
|  | 169 | ///  This is mainly done because functions to remove items from the environment | 
|  | 170 | ///  are not available across all platforms.  In particular, Solaris does not | 
|  | 171 | ///  seem to have an unsetenv() function or a setenv() function (or they are | 
|  | 172 | ///  undocumented if they do exist). | 
|  | 173 | /// | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 174 | void RemoveEnv(const char * name, char ** const envp) { | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 175 | for (unsigned index=0; envp[index] != NULL; index++) { | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 176 | // Find the first equals sign in the array and make it an EOS character. | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 177 | char *p = strchr (envp[index], '='); | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 178 | if (p == NULL) | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 179 | continue; | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 180 | else | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 181 | *p = '\0'; | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 182 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 183 | // Compare the two strings.  If they are equal, zap this string. | 
|  | 184 | // Otherwise, restore it. | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 185 | if (!strcmp(name, envp[index])) | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 186 | *envp[index] = '\0'; | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 187 | else | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 188 | *p = '='; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 189 | } | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 190 |  | 
|  | 191 | return; | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 194 | } // End llvm namespace | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 195 |  | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 196 | int main(int argc, char **argv, char **envp) { | 
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 197 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 198 |  | 
| Misha Brukman | d16a037 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 199 | std::string ModuleID("gccld-output"); | 
| Brian Gaeke | 1d9515b | 2003-11-05 22:13:00 +0000 | [diff] [blame] | 200 | std::auto_ptr<Module> Composite(new Module(ModuleID)); | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 201 |  | 
| Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 202 | // We always look first in the current directory when searching for libraries. | 
|  | 203 | LibPaths.insert(LibPaths.begin(), "."); | 
|  | 204 |  | 
| Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 205 | // If the user specified an extra search path in their environment, respect | 
|  | 206 | // it. | 
| Chris Lattner | da3bc21 | 2003-04-21 19:53:24 +0000 | [diff] [blame] | 207 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) | 
|  | 208 | LibPaths.push_back(SearchPath); | 
|  | 209 |  | 
| Chris Lattner | 4b462c0 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 210 | // Remove any consecutive duplicates of the same library... | 
|  | 211 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), | 
|  | 212 | Libraries.end()); | 
|  | 213 |  | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 214 | // Link in all of the files | 
| Brian Gaeke | 484f2c7 | 2003-09-30 14:03:48 +0000 | [diff] [blame] | 215 | if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose)) | 
|  | 216 | return 1; // Error already printed | 
| Chris Lattner | cbf2aeb | 2003-11-03 17:27:17 +0000 | [diff] [blame] | 217 |  | 
|  | 218 | if (!LinkAsLibrary) | 
|  | 219 | LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths, | 
|  | 220 | Verbose, Native); | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 221 |  | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 222 | // Link in all of the libraries next... | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 223 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 224 | // Create the output file. | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 225 | std::string RealBytecodeOutput = OutputFilename; | 
|  | 226 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; | 
|  | 227 | std::ofstream Out(RealBytecodeOutput.c_str()); | 
|  | 228 | if (!Out.good()) | 
|  | 229 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + | 
|  | 230 | "' for writing!"); | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 231 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 232 | // Ensure that the bytecode file gets removed from the disk if we get a | 
|  | 233 | // SIGINT signal. | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 234 | RemoveFileOnSignal(RealBytecodeOutput); | 
| Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 235 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 236 | // Generate the bytecode file. | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 237 | if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) { | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 238 | Out.close(); | 
| Brian Gaeke | 1d9515b | 2003-11-05 22:13:00 +0000 | [diff] [blame] | 239 | return PrintAndReturn(argv[0], "error generating bytecode"); | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 242 | // Close the bytecode file. | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 243 | Out.close(); | 
|  | 244 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 245 | // If we are not linking a library, generate either a native executable | 
|  | 246 | // or a JIT shell script, depending upon what the user wants. | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 247 | if (!LinkAsLibrary) { | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 248 | // If the user wants to generate a native executable, compile it from the | 
|  | 249 | // bytecode file. | 
|  | 250 | // | 
|  | 251 | // Otherwise, create a script that will run the bytecode through the JIT. | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 252 | if (Native) { | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 253 | // Name of the Assembly Language output file | 
|  | 254 | std::string AssemblyFile = OutputFilename + ".s"; | 
|  | 255 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 256 | // Mark the output files for removal if we get an interrupt. | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 257 | RemoveFileOnSignal(AssemblyFile); | 
|  | 258 | RemoveFileOnSignal(OutputFilename); | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 259 |  | 
| John Criswell | 0217b1b | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 260 | // Determine the locations of the llc and gcc programs. | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 261 | std::string llc = FindExecutable("llc", argv[0]); | 
|  | 262 | std::string gcc = FindExecutable("gcc", argv[0]); | 
| John Criswell | 0217b1b | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 263 | if (llc.empty()) | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 264 | return PrintAndReturn(argv[0], "Failed to find llc"); | 
| John Criswell | 0217b1b | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 265 |  | 
|  | 266 | if (gcc.empty()) | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 267 | return PrintAndReturn(argv[0], "Failed to find gcc"); | 
| John Criswell | 0217b1b | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 268 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 269 | // Generate an assembly language file for the bytecode. | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 270 | if (Verbose) std::cout << "Generating Assembly Code\n"; | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 271 | GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp); | 
| John Criswell | b533bde | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 272 | if (Verbose) std::cout << "Generating Native Code\n"; | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 273 | GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths, | 
|  | 274 | gcc, envp); | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 275 |  | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 276 | // Remove the assembly language file. | 
| John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 277 | removeFile (AssemblyFile); | 
| Chris Lattner | 65d93e2 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 278 | } else { | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 279 | // Output the script to start the program... | 
|  | 280 | std::ofstream Out2(OutputFilename.c_str()); | 
|  | 281 | if (!Out2.good()) | 
|  | 282 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + | 
|  | 283 | "' for writing!"); | 
| Misha Brukman | 1df1262 | 2003-11-20 19:08:42 +0000 | [diff] [blame] | 284 | Out2 << "#!/bin/sh\nlli \\\n"; | 
|  | 285 | // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib. | 
|  | 286 | LibPaths.push_back("/lib"); | 
|  | 287 | LibPaths.push_back("/usr/lib"); | 
| Misha Brukman | c10bf39 | 2003-11-24 05:31:57 +0000 | [diff] [blame^] | 288 | LibPaths.push_back("/usr/X11R6/lib"); | 
| Misha Brukman | 1df1262 | 2003-11-20 19:08:42 +0000 | [diff] [blame] | 289 | // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a | 
|  | 290 | // shared object at all! See RH 8: plain text. | 
|  | 291 | std::vector<std::string>::iterator libc = | 
|  | 292 | std::find(Libraries.begin(), Libraries.end(), "c"); | 
|  | 293 | if (libc != Libraries.end()) Libraries.erase(libc); | 
|  | 294 | // List all the shared object (native) libraries this executable will need | 
|  | 295 | // on the command line, so that we don't have to do this manually! | 
|  | 296 | for (std::vector<std::string>::iterator i = Libraries.begin(), | 
|  | 297 | e = Libraries.end(); i != e; ++i) { | 
|  | 298 | std::string FullLibraryPath = FindLib(*i, LibPaths, true); | 
| Misha Brukman | 44a562b | 2003-11-24 05:29:42 +0000 | [diff] [blame] | 299 | if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath)) | 
| Misha Brukman | 1df1262 | 2003-11-20 19:08:42 +0000 | [diff] [blame] | 300 | Out2 << "    -load=" << FullLibraryPath << " \\\n"; | 
|  | 301 | } | 
|  | 302 | Out2 << "    $0.bc $*\n"; | 
| John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 303 | Out2.close(); | 
|  | 304 | } | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 305 |  | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 306 | // Make the script executable... | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 307 | MakeFileExecutable(OutputFilename); | 
| Misha Brukman | 9086994 | 2003-08-20 20:38:15 +0000 | [diff] [blame] | 308 |  | 
| John Criswell | a3ce8b4 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 309 | // Make the bytecode file readable and directly executable in LLEE as well | 
| Misha Brukman | 2166363 | 2003-09-30 17:59:25 +0000 | [diff] [blame] | 310 | MakeFileExecutable(RealBytecodeOutput); | 
|  | 311 | MakeFileReadable(RealBytecodeOutput); | 
| Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 312 | } | 
| Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 313 |  | 
|  | 314 | return 0; | 
|  | 315 | } |