Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 1 | //===- llvm-ld.cpp - LLVM 'ld' compatible linker --------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility is intended to be compatible with GCC, and follows standard |
| 11 | // system 'ld' conventions. As such, the default output file is ./a.out. |
| 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, |
| 19 | // I'm not too worried about this. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 23 | #include "llvm/LinkAllVMCore.h" |
Reid Spencer | 605b9e2 | 2004-11-14 23:00:08 +0000 | [diff] [blame] | 24 | #include "llvm/Linker.h" |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 25 | #include "llvm/System/Program.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/PassManager.h" |
| 28 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 29 | #include "llvm/Bytecode/Writer.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Reid Spencer | aefd04b | 2004-09-25 16:00:07 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/Target/TargetMachineRegistry.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/FileUtilities.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 35 | #include "llvm/Support/SystemUtils.h" |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 36 | #include "llvm/System/Signals.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 37 | #include <fstream> |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 38 | #include <iostream> |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 39 | #include <memory> |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 40 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 43 | // Input/Output Options |
| 44 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 45 | cl::desc("<input bytecode files>")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 46 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 47 | static cl::opt<std::string> OutputFilename("o", cl::init("a.out"), |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 48 | cl::desc("Override output filename"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 49 | cl::value_desc("filename")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 50 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | static cl::opt<bool> Verbose("v", |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 52 | cl::desc("Print information about actions taken")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 54 | static cl::list<std::string> LibPaths("L", cl::Prefix, |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 55 | cl::desc("Specify a library search path"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 56 | cl::value_desc("directory")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 58 | static cl::list<std::string> Libraries("l", cl::Prefix, |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 59 | cl::desc("Specify libraries to link to"), |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 60 | cl::value_desc("library prefix")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 61 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 62 | static cl::opt<bool> LinkAsLibrary("link-as-library", |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 63 | cl::desc("Link the .bc files together as a library, not an executable")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 65 | static cl::alias Relink("r", cl::aliasopt(LinkAsLibrary), |
| 66 | cl::desc("Alias for -link-as-library")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 67 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 68 | static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> |
Reid Spencer | aefd04b | 2004-09-25 16:00:07 +0000 | [diff] [blame] | 69 | MachineArch("march", cl::desc("Architecture to generate assembly for:")); |
| 70 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 71 | static cl::opt<bool> Native("native", |
| 72 | cl::desc("Generate a native binary instead of a shell script")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 73 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 74 | static cl::opt<bool>NativeCBE("native-cbe", |
| 75 | cl::desc("Generate a native binary with the C backend and GCC")); |
| 76 | |
| 77 | static cl::opt<bool>DisableCompression("disable-compression",cl::init(false), |
| 78 | cl::desc("Disable writing of compressed bytecode files")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 80 | static cl::list<std::string> PostLinkOpts("post-link-opts", |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 81 | cl::value_desc("path"), |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 82 | cl::desc("Run one or more optimization programs after linking")); |
| 83 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 84 | static cl::list<std::string> XLinker("Xlinker", cl::value_desc("option"), |
| 85 | cl::desc("Pass options to the system linker")); |
| 86 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 87 | // Compatibility options that are ignored but supported by LD |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 88 | static cl::opt<std::string> CO3("soname", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 89 | cl::desc("Compatibility option: ignored")); |
| 90 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 91 | static cl::opt<std::string> CO4("version-script", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 92 | cl::desc("Compatibility option: ignored")); |
| 93 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 94 | static cl::opt<bool> CO5("eh-frame-hdr", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 95 | cl::desc("Compatibility option: ignored")); |
| 96 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 97 | static cl::opt<std::string> CO6("h", cl::Hidden, |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 98 | cl::desc("Compatibility option: ignored")); |
| 99 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 100 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 101 | /// This is just for convenience so it doesn't have to be passed around |
| 102 | /// everywhere. |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 103 | static std::string progname; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 104 | |
| 105 | /// PrintAndReturn - Prints a message to standard error and returns true. |
| 106 | /// |
| 107 | /// Inputs: |
| 108 | /// progname - The name of the program (i.e. argv[0]). |
| 109 | /// Message - The message to print to standard error. |
| 110 | /// |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 111 | static int PrintAndReturn(const std::string &Message) { |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 112 | std::cerr << progname << ": " << Message << "\n"; |
| 113 | return 1; |
| 114 | } |
| 115 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 116 | /// CopyEnv - This function takes an array of environment variables and makes a |
| 117 | /// copy of it. This copy can then be manipulated any way the caller likes |
| 118 | /// without affecting the process's real environment. |
| 119 | /// |
| 120 | /// Inputs: |
| 121 | /// envp - An array of C strings containing an environment. |
| 122 | /// |
| 123 | /// Return value: |
| 124 | /// NULL - An error occurred. |
| 125 | /// |
| 126 | /// Otherwise, a pointer to a new array of C strings is returned. Every string |
| 127 | /// in the array is a duplicate of the one in the original array (i.e. we do |
| 128 | /// not copy the char *'s from one array to another). |
| 129 | /// |
| 130 | static char ** CopyEnv(char ** const envp) { |
| 131 | // Count the number of entries in the old list; |
| 132 | unsigned entries; // The number of entries in the old environment list |
| 133 | for (entries = 0; envp[entries] != NULL; entries++) |
| 134 | /*empty*/; |
| 135 | |
| 136 | // Add one more entry for the NULL pointer that ends the list. |
| 137 | ++entries; |
| 138 | |
| 139 | // If there are no entries at all, just return NULL. |
| 140 | if (entries == 0) |
| 141 | return NULL; |
| 142 | |
| 143 | // Allocate a new environment list. |
| 144 | char **newenv = new char* [entries]; |
| 145 | if ((newenv = new char* [entries]) == NULL) |
| 146 | return NULL; |
| 147 | |
| 148 | // Make a copy of the list. Don't forget the NULL that ends the list. |
| 149 | entries = 0; |
| 150 | while (envp[entries] != NULL) { |
| 151 | newenv[entries] = new char[strlen (envp[entries]) + 1]; |
| 152 | strcpy (newenv[entries], envp[entries]); |
| 153 | ++entries; |
| 154 | } |
| 155 | newenv[entries] = NULL; |
| 156 | |
| 157 | return newenv; |
| 158 | } |
| 159 | |
| 160 | |
| 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 | /// |
| 174 | static void RemoveEnv(const char * name, char ** const envp) { |
| 175 | for (unsigned index=0; envp[index] != NULL; index++) { |
| 176 | // Find the first equals sign in the array and make it an EOS character. |
| 177 | char *p = strchr (envp[index], '='); |
| 178 | if (p == NULL) |
| 179 | continue; |
| 180 | else |
| 181 | *p = '\0'; |
| 182 | |
| 183 | // Compare the two strings. If they are equal, zap this string. |
| 184 | // Otherwise, restore it. |
| 185 | if (!strcmp(name, envp[index])) |
| 186 | *envp[index] = '\0'; |
| 187 | else |
| 188 | *p = '='; |
| 189 | } |
| 190 | |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | /// GenerateBytecode - generates a bytecode file from the module provided |
| 195 | void GenerateBytecode(Module* M, const std::string& FileName) { |
| 196 | |
| 197 | // Create the output file. |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 198 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 199 | std::ios::binary; |
| 200 | std::ofstream Out(FileName.c_str(), io_mode); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 201 | if (!Out.good()) { |
| 202 | PrintAndReturn("error opening '" + FileName + "' for writing!"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 207 | // terminating signal. |
| 208 | sys::RemoveFileOnSignal(sys::Path(FileName)); |
| 209 | |
| 210 | // Write it out |
| 211 | WriteBytecodeToFile(M, Out, !DisableCompression); |
| 212 | |
| 213 | // Close the bytecode file. |
| 214 | Out.close(); |
| 215 | } |
| 216 | |
| 217 | /// GenerateAssembly - generates a native assembly language source file from the |
| 218 | /// specified bytecode file. |
| 219 | /// |
| 220 | /// Inputs: |
| 221 | /// InputFilename - The name of the output bytecode file. |
| 222 | /// OutputFilename - The name of the file to generate. |
| 223 | /// llc - The pathname to use for LLC. |
| 224 | /// envp - The environment to use when running LLC. |
| 225 | /// |
| 226 | /// Return non-zero value on error. |
| 227 | /// |
| 228 | static int GenerateAssembly(const std::string &OutputFilename, |
| 229 | const std::string &InputFilename, |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 230 | const sys::Path &llc) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 231 | // Run LLC to convert the bytecode file into assembly code. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 232 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 233 | args.push_back(llc.c_str()); |
| 234 | args.push_back("-f"); |
| 235 | args.push_back("-o"); |
| 236 | args.push_back(OutputFilename.c_str()); |
| 237 | args.push_back(InputFilename.c_str()); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 238 | args.push_back(0); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 239 | |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 240 | return sys::Program::ExecuteAndWait(llc,&args[0]); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /// GenerateAssembly - generates a native assembly language source file from the |
| 244 | /// specified bytecode file. |
| 245 | static int GenerateCFile(const std::string &OutputFile, |
| 246 | const std::string &InputFile, |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 247 | const sys::Path &llc) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 248 | // Run LLC to convert the bytecode file into C. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 249 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 250 | args.push_back(llc.c_str()); |
| 251 | args.push_back("-march=c"); |
| 252 | args.push_back("-f"); |
| 253 | args.push_back("-o"); |
| 254 | args.push_back(OutputFile.c_str()); |
| 255 | args.push_back(InputFile.c_str()); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 256 | args.push_back(0); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 257 | return sys::Program::ExecuteAndWait(llc, &args[0]); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /// GenerateNative - generates a native assembly language source file from the |
| 261 | /// specified assembly source file. |
| 262 | /// |
| 263 | /// Inputs: |
| 264 | /// InputFilename - The name of the output bytecode file. |
| 265 | /// OutputFilename - The name of the file to generate. |
| 266 | /// Libraries - The list of libraries with which to link. |
| 267 | /// LibPaths - The list of directories in which to find libraries. |
| 268 | /// gcc - The pathname to use for GGC. |
| 269 | /// envp - A copy of the process's current environment. |
| 270 | /// |
| 271 | /// Outputs: |
| 272 | /// None. |
| 273 | /// |
| 274 | /// Returns non-zero value on error. |
| 275 | /// |
| 276 | static int GenerateNative(const std::string &OutputFilename, |
| 277 | const std::string &InputFilename, |
| 278 | const std::vector<std::string> &Libraries, |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 279 | const sys::Path &gcc, char ** const envp) { |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 280 | // Remove these environment variables from the environment of the |
| 281 | // programs that we will execute. It appears that GCC sets these |
| 282 | // environment variables so that the programs it uses can configure |
| 283 | // themselves identically. |
| 284 | // |
| 285 | // However, when we invoke GCC below, we want it to use its normal |
| 286 | // configuration. Hence, we must sanitize its environment. |
| 287 | char ** clean_env = CopyEnv(envp); |
| 288 | if (clean_env == NULL) |
| 289 | return 1; |
| 290 | RemoveEnv("LIBRARY_PATH", clean_env); |
| 291 | RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); |
| 292 | RemoveEnv("GCC_EXEC_PREFIX", clean_env); |
| 293 | RemoveEnv("COMPILER_PATH", clean_env); |
| 294 | RemoveEnv("COLLECT_GCC", clean_env); |
| 295 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 296 | |
| 297 | // Run GCC to assemble and link the program into native code. |
| 298 | // |
| 299 | // Note: |
| 300 | // We can't just assemble and link the file with the system assembler |
| 301 | // and linker because we don't know where to put the _start symbol. |
| 302 | // GCC mysteriously knows how to do it. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 303 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 304 | args.push_back(gcc.c_str()); |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 305 | args.push_back("-fno-strict-aliasing"); |
| 306 | args.push_back("-O3"); |
| 307 | args.push_back("-o"); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 308 | args.push_back(OutputFilename.c_str()); |
| 309 | args.push_back(InputFilename.c_str()); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 310 | |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 311 | // Add in the library paths |
| 312 | for (unsigned index = 0; index < LibPaths.size(); index++) { |
| 313 | args.push_back("-L"); |
| 314 | args.push_back(LibPaths[index].c_str()); |
| 315 | } |
| 316 | |
| 317 | // Add the requested options |
| 318 | for (unsigned index = 0; index < XLinker.size(); index++) { |
| 319 | args.push_back(XLinker[index].c_str()); |
| 320 | args.push_back(Libraries[index].c_str()); |
| 321 | } |
| 322 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 323 | // Add in the libraries to link. |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 324 | for (unsigned index = 0; index < Libraries.size(); index++) |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 325 | if (Libraries[index] != "crtend") { |
| 326 | args.push_back("-l"); |
| 327 | args.push_back(Libraries[index].c_str()); |
| 328 | } |
Reid Spencer | af303d5 | 2006-06-07 23:03:13 +0000 | [diff] [blame^] | 329 | |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 330 | args.push_back(0); |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 331 | |
| 332 | // Run the compiler to assembly and link together the program. |
Chris Lattner | 2f86362 | 2006-05-14 18:38:13 +0000 | [diff] [blame] | 333 | int R = sys::Program::ExecuteAndWait(gcc, &args[0], (const char**)clean_env); |
| 334 | delete [] clean_env; |
| 335 | return R; |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 338 | /// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM |
| 339 | /// bytecode file for the program. |
| 340 | static void EmitShellScript(char **argv) { |
| 341 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 342 | // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To |
| 343 | // support windows systems, we copy the llvm-stub.exe executable from the |
| 344 | // build tree to the destination file. |
Reid Spencer | 8d8b41d | 2004-12-22 13:50:17 +0000 | [diff] [blame] | 345 | sys::Path llvmstub = FindExecutable("llvm-stub.exe", argv[0]); |
| 346 | if (llvmstub.isEmpty()) { |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 347 | std::cerr << "Could not find llvm-stub.exe executable!\n"; |
| 348 | exit(1); |
| 349 | } |
Reid Spencer | 8d8b41d | 2004-12-22 13:50:17 +0000 | [diff] [blame] | 350 | sys::CopyFile(sys::Path(OutputFilename), llvmstub); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 351 | return; |
| 352 | #endif |
| 353 | |
| 354 | // Output the script to start the program... |
| 355 | std::ofstream Out2(OutputFilename.c_str()); |
| 356 | if (!Out2.good()) |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 357 | exit(PrintAndReturn("error opening '" + OutputFilename + "' for writing!")); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 358 | |
| 359 | Out2 << "#!/bin/sh\n"; |
| 360 | // Allow user to setenv LLVMINTERP if lli is not in their PATH. |
| 361 | Out2 << "lli=${LLVMINTERP-lli}\n"; |
| 362 | Out2 << "exec $lli \\\n"; |
| 363 | // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib. |
| 364 | LibPaths.push_back("/lib"); |
| 365 | LibPaths.push_back("/usr/lib"); |
| 366 | LibPaths.push_back("/usr/X11R6/lib"); |
| 367 | // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a |
| 368 | // shared object at all! See RH 8: plain text. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 369 | std::vector<std::string>::iterator libc = |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 370 | std::find(Libraries.begin(), Libraries.end(), "c"); |
| 371 | if (libc != Libraries.end()) Libraries.erase(libc); |
| 372 | // List all the shared object (native) libraries this executable will need |
| 373 | // on the command line, so that we don't have to do this manually! |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 374 | for (std::vector<std::string>::iterator i = Libraries.begin(), |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 375 | e = Libraries.end(); i != e; ++i) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 376 | sys::Path FullLibraryPath = sys::Path::FindLibrary(*i); |
| 377 | if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary()) |
| 378 | Out2 << " -load=" << FullLibraryPath.toString() << " \\\n"; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 379 | } |
| 380 | Out2 << " $0.bc ${1+\"$@\"}\n"; |
| 381 | Out2.close(); |
| 382 | } |
| 383 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 384 | // BuildLinkItems -- This function generates a LinkItemList for the LinkItems |
| 385 | // linker function by combining the Files and Libraries in the order they were |
| 386 | // declared on the command line. |
| 387 | static void BuildLinkItems( |
| 388 | Linker::ItemList& Items, |
| 389 | const cl::list<std::string>& Files, |
| 390 | const cl::list<std::string>& Libraries) { |
| 391 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 392 | // Build the list of linkage items for LinkItems. |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 393 | |
| 394 | cl::list<std::string>::const_iterator fileIt = Files.begin(); |
| 395 | cl::list<std::string>::const_iterator libIt = Libraries.begin(); |
| 396 | |
| 397 | int libPos = -1, filePos = -1; |
Reid Spencer | 05f7e79 | 2004-12-13 17:18:19 +0000 | [diff] [blame] | 398 | while ( libIt != Libraries.end() || fileIt != Files.end() ) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 399 | if (libIt != Libraries.end()) |
| 400 | libPos = Libraries.getPosition(libIt - Libraries.begin()); |
| 401 | else |
| 402 | libPos = -1; |
| 403 | if (fileIt != Files.end()) |
| 404 | filePos = Files.getPosition(fileIt - Files.begin()); |
| 405 | else |
| 406 | filePos = -1; |
| 407 | |
| 408 | if (filePos != -1 && (libPos == -1 || filePos < libPos)) { |
| 409 | // Add a source file |
| 410 | Items.push_back(std::make_pair(*fileIt++, false)); |
| 411 | } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { |
| 412 | // Add a library |
| 413 | Items.push_back(std::make_pair(*libIt++, true)); |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Reid Spencer | 445564a | 2004-11-20 20:02:56 +0000 | [diff] [blame] | 418 | // Rightly this should go in a header file but it just seems such a waste. |
| 419 | namespace llvm { |
| 420 | extern void Optimize(Module*); |
| 421 | } |
| 422 | |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 423 | int main(int argc, char **argv, char **envp) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 424 | try { |
| 425 | // Initial global variable above for convenience printing of program name. |
| 426 | progname = sys::Path(argv[0]).getBasename(); |
Reid Spencer | 328ead9 | 2005-12-13 20:00:37 +0000 | [diff] [blame] | 427 | Linker TheLinker(progname, OutputFilename, Verbose); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 428 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 429 | // Parse the command line options |
| 430 | cl::ParseCommandLineOptions(argc, argv, " llvm linker\n"); |
| 431 | sys::PrintStackTraceOnErrorSignal(); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 432 | |
Reid Spencer | 78df5c3 | 2006-03-06 06:38:19 +0000 | [diff] [blame] | 433 | // Set up the library paths for the Linker |
| 434 | TheLinker.addPaths(LibPaths); |
| 435 | TheLinker.addSystemPaths(); |
| 436 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 437 | // Remove any consecutive duplicates of the same library... |
| 438 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 439 | Libraries.end()); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 440 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 441 | if (LinkAsLibrary) { |
| 442 | std::vector<sys::Path> Files; |
| 443 | for (unsigned i = 0; i < InputFilenames.size(); ++i ) |
| 444 | Files.push_back(sys::Path(InputFilenames[i])); |
| 445 | if (TheLinker.LinkInFiles(Files)) |
| 446 | return 1; // Error already printed |
Reid Spencer | 6b463b2 | 2004-12-08 05:17:40 +0000 | [diff] [blame] | 447 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 448 | // The libraries aren't linked in but are noted as "dependent" in the |
| 449 | // module. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 450 | for (cl::list<std::string>::const_iterator I = Libraries.begin(), |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 451 | E = Libraries.end(); I != E ; ++I) { |
| 452 | TheLinker.getModule()->addLibrary(*I); |
| 453 | } |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 454 | } else { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 455 | // Build a list of the items from our command line |
| 456 | Linker::ItemList Items; |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 457 | Linker::ItemList NativeItems; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 458 | BuildLinkItems(Items, InputFilenames, Libraries); |
| 459 | |
| 460 | // Link all the items together |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 461 | if (TheLinker.LinkInItems(Items,NativeItems) ) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 462 | return 1; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 463 | } |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 464 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 465 | std::auto_ptr<Module> Composite(TheLinker.releaseModule()); |
| 466 | |
| 467 | // Optimize the module |
| 468 | Optimize(Composite.get()); |
| 469 | |
| 470 | // Generate the bytecode for the optimized module. |
| 471 | std::string RealBytecodeOutput = OutputFilename; |
| 472 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 473 | GenerateBytecode(Composite.get(), RealBytecodeOutput); |
| 474 | |
| 475 | // If we are not linking a library, generate either a native executable |
| 476 | // or a JIT shell script, depending upon what the user wants. |
| 477 | if (!LinkAsLibrary) { |
Reid Spencer | 73a74be | 2005-12-21 05:03:23 +0000 | [diff] [blame] | 478 | // If the user wants to run a post-link optimization, run it now. |
| 479 | if (!PostLinkOpts.empty()) { |
| 480 | std::vector<std::string> opts = PostLinkOpts; |
| 481 | for (std::vector<std::string>::iterator I = opts.begin(), |
| 482 | E = opts.end(); I != E; ++I) { |
| 483 | sys::Path prog(*I); |
| 484 | if (!prog.canExecute()) { |
| 485 | prog = sys::Program::FindProgramByName(*I); |
| 486 | if (prog.isEmpty()) |
| 487 | return PrintAndReturn(std::string("Optimization program '") + *I + |
| 488 | "' is not found or not executable."); |
| 489 | } |
| 490 | // Get the program arguments |
| 491 | sys::Path tmp_output("opt_result"); |
| 492 | if (!tmp_output.createTemporaryFileOnDisk()) { |
| 493 | return PrintAndReturn( |
| 494 | "Can't create temporary file for post-link optimization"); |
| 495 | } |
| 496 | const char* args[4]; |
| 497 | args[0] = I->c_str(); |
| 498 | args[1] = RealBytecodeOutput.c_str(); |
| 499 | args[2] = tmp_output.c_str(); |
| 500 | args[3] = 0; |
| 501 | if (0 == sys::Program::ExecuteAndWait(prog, args)) { |
| 502 | if (tmp_output.isBytecodeFile()) { |
| 503 | sys::Path target(RealBytecodeOutput); |
| 504 | target.eraseFromDisk(); |
| 505 | tmp_output.renamePathOnDisk(target); |
| 506 | } else |
| 507 | return PrintAndReturn( |
| 508 | "Post-link optimization output is not bytecode"); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 513 | // If the user wants to generate a native executable, compile it from the |
| 514 | // bytecode file. |
| 515 | // |
| 516 | // Otherwise, create a script that will run the bytecode through the JIT. |
| 517 | if (Native) { |
| 518 | // Name of the Assembly Language output file |
| 519 | sys::Path AssemblyFile ( OutputFilename); |
| 520 | AssemblyFile.appendSuffix("s"); |
| 521 | |
| 522 | // Mark the output files for removal if we get an interrupt. |
| 523 | sys::RemoveFileOnSignal(AssemblyFile); |
| 524 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 525 | |
| 526 | // Determine the locations of the llc and gcc programs. |
| 527 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 528 | if (llc.isEmpty()) |
| 529 | return PrintAndReturn("Failed to find llc"); |
| 530 | |
| 531 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 532 | if (gcc.isEmpty()) |
| 533 | return PrintAndReturn("Failed to find gcc"); |
| 534 | |
| 535 | // Generate an assembly language file for the bytecode. |
| 536 | if (Verbose) std::cout << "Generating Assembly Code\n"; |
| 537 | GenerateAssembly(AssemblyFile.toString(), RealBytecodeOutput, llc); |
| 538 | if (Verbose) std::cout << "Generating Native Code\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 539 | GenerateNative(OutputFilename, AssemblyFile.toString(), Libraries, |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 540 | gcc, envp); |
| 541 | |
| 542 | // Remove the assembly language file. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 543 | AssemblyFile.eraseFromDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 544 | } else if (NativeCBE) { |
| 545 | sys::Path CFile (OutputFilename); |
| 546 | CFile.appendSuffix("cbe.c"); |
| 547 | |
| 548 | // Mark the output files for removal if we get an interrupt. |
| 549 | sys::RemoveFileOnSignal(CFile); |
| 550 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 551 | |
| 552 | // Determine the locations of the llc and gcc programs. |
| 553 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 554 | if (llc.isEmpty()) |
| 555 | return PrintAndReturn("Failed to find llc"); |
| 556 | |
| 557 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 558 | if (gcc.isEmpty()) |
| 559 | return PrintAndReturn("Failed to find gcc"); |
| 560 | |
| 561 | // Generate an assembly language file for the bytecode. |
| 562 | if (Verbose) std::cout << "Generating Assembly Code\n"; |
| 563 | GenerateCFile(CFile.toString(), RealBytecodeOutput, llc); |
| 564 | if (Verbose) std::cout << "Generating Native Code\n"; |
| 565 | GenerateNative(OutputFilename, CFile.toString(), Libraries, gcc, envp); |
| 566 | |
| 567 | // Remove the assembly language file. |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 568 | CFile.eraseFromDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 569 | |
| 570 | } else { |
| 571 | EmitShellScript(argv); |
| 572 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 573 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 574 | // Make the script executable... |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 575 | sys::Path(OutputFilename).makeExecutableOnDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 576 | |
| 577 | // Make the bytecode file readable and directly executable in LLEE as well |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 578 | sys::Path(RealBytecodeOutput).makeExecutableOnDisk(); |
| 579 | sys::Path(RealBytecodeOutput).makeReadableOnDisk(); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } catch (const std::string& msg) { |
| 584 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 585 | } catch (...) { |
| 586 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 587 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 588 | return 1; |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 589 | } |