Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +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 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e7fca51 | 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 | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 11 | // system 'ld' conventions. As such, the default output file is ./a.out. |
Chris Lattner | e7fca51 | 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 | 69a7960 | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 19 | // I'm not too worried about this. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 23 | #include "gccld.h" |
Reid Spencer | 605b9e2 | 2004-11-14 23:00:08 +0000 | [diff] [blame] | 24 | #include "llvm/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 26 | #include "llvm/PassManager.h" |
| 27 | #include "llvm/Bytecode/Reader.h" |
| 28 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 34 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 36 | #include <fstream> |
| 37 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 40 | namespace { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 41 | cl::list<std::string> |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 42 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 43 | cl::OneOrMore); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 44 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 45 | cl::opt<std::string> |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 46 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 47 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 49 | cl::opt<bool> |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 50 | Verbose("v", cl::desc("Print information about actions taken")); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 51 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 52 | cl::list<std::string> |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 53 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 54 | cl::value_desc("directory")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 56 | cl::list<std::string> |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 57 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 58 | cl::value_desc("library prefix")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 60 | cl::opt<bool> |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 61 | Strip("strip-all", cl::desc("Strip all symbol info from executable")); |
| 62 | cl::opt<bool> |
| 63 | StripDebug("strip-debug", |
| 64 | cl::desc("Strip debugger symbol info from executable")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 66 | cl::opt<bool> |
| 67 | NoInternalize("disable-internalize", |
| 68 | cl::desc("Do not mark all symbols as internal")); |
Chris Lattner | fe32bf5 | 2003-11-05 06:05:21 +0000 | [diff] [blame] | 69 | cl::alias |
Chris Lattner | a2b2dc9 | 2003-08-22 19:18:45 +0000 | [diff] [blame] | 70 | ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), |
| 71 | cl::aliasopt(NoInternalize)); |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 73 | cl::opt<bool> |
| 74 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 75 | " library, not an executable")); |
Chris Lattner | fe32bf5 | 2003-11-05 06:05:21 +0000 | [diff] [blame] | 76 | cl::alias |
| 77 | Relink("r", cl::desc("Alias for -link-as-library"), |
| 78 | cl::aliasopt(LinkAsLibrary)); |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 79 | |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 80 | cl::opt<bool> |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 81 | Native("native", |
| 82 | cl::desc("Generate a native binary instead of a shell script")); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 83 | cl::opt<bool> |
Chris Lattner | 69e8d28 | 2004-04-06 16:43:13 +0000 | [diff] [blame] | 84 | NativeCBE("native-cbe", |
| 85 | cl::desc("Generate a native binary with the C backend and GCC")); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 86 | |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 87 | cl::opt<bool> |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 88 | SaveTemps("save-temps", |
| 89 | cl::desc("Do not delete temporary files")); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 90 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 91 | cl::opt<std::string> |
| 92 | RPath("rpath", |
| 93 | cl::desc("Set runtime shared library search path (requires -native or" |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 94 | " -native-cbe)"), |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 95 | cl::Prefix, cl::value_desc("directory")); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 96 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 97 | cl::opt<std::string> |
| 98 | SOName("soname", |
| 99 | cl::desc("Set internal name of shared library (requires -native or" |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 100 | " -native-cbe)"), |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 101 | cl::Prefix, cl::value_desc("name")); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 102 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 103 | // Compatibility options that are ignored but supported by LD |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 104 | cl::opt<std::string> |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 105 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 106 | cl::opt<bool> |
| 107 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
John Criswell | aa2a47d | 2003-12-09 15:39:11 +0000 | [diff] [blame] | 108 | cl::opt<std::string> |
| 109 | CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 110 | |
| 111 | cl::alias A0("s", cl::desc("Alias for --strip-all"), |
| 112 | cl::aliasopt(Strip)); |
| 113 | cl::alias A1("S", cl::desc("Alias for --strip-debug"), |
| 114 | cl::aliasopt(StripDebug)); |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 115 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 118 | /// PrintAndReturn - Prints a message to standard error and returns true. |
Misha Brukman | 9839969 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 119 | /// |
| 120 | /// Inputs: |
| 121 | /// progname - The name of the program (i.e. argv[0]). |
| 122 | /// Message - The message to print to standard error. |
Misha Brukman | 9839969 | 2003-11-20 06:21:54 +0000 | [diff] [blame] | 123 | /// |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 124 | static int PrintAndReturn(const char *progname, const std::string &Message) { |
| 125 | std::cerr << progname << ": " << Message << "\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 126 | return 1; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 129 | /// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM |
| 130 | /// bytecode file for the program. |
| 131 | static void EmitShellScript(char **argv) { |
Chris Lattner | 681692d | 2004-06-02 00:53:57 +0000 | [diff] [blame] | 132 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 133 | // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To |
| 134 | // support windows systems, we copy the llvm-stub.exe executable from the |
| 135 | // build tree to the destination file. |
Reid Spencer | 93f8f55 | 2004-12-13 23:44:23 +0000 | [diff] [blame] | 136 | std::string llvmstub = FindExecutable("llvm-stub.exe", argv[0]).toString(); |
Chris Lattner | 681692d | 2004-06-02 00:53:57 +0000 | [diff] [blame] | 137 | if (llvmstub.empty()) { |
| 138 | std::cerr << "Could not find llvm-stub.exe executable!\n"; |
| 139 | exit(1); |
| 140 | } |
Reid Spencer | 682084a | 2004-12-21 03:24:02 +0000 | [diff] [blame] | 141 | sys::CopyFile(sys::Path(OutputFilename), sys::Path(llvmstub)); |
Chris Lattner | 681692d | 2004-06-02 00:53:57 +0000 | [diff] [blame] | 142 | return; |
| 143 | #endif |
| 144 | |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 145 | // Output the script to start the program... |
| 146 | std::ofstream Out2(OutputFilename.c_str()); |
| 147 | if (!Out2.good()) |
| 148 | exit(PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 149 | "' for writing!")); |
| 150 | |
| 151 | Out2 << "#!/bin/sh\n"; |
| 152 | // Allow user to setenv LLVMINTERP if lli is not in their PATH. |
| 153 | Out2 << "lli=${LLVMINTERP-lli}\n"; |
| 154 | Out2 << "exec $lli \\\n"; |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 156 | // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a |
| 157 | // shared object at all! See RH 8: plain text. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 158 | std::vector<std::string>::iterator libc = |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 159 | std::find(Libraries.begin(), Libraries.end(), "c"); |
| 160 | if (libc != Libraries.end()) Libraries.erase(libc); |
| 161 | // List all the shared object (native) libraries this executable will need |
| 162 | // 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] | 163 | for (std::vector<std::string>::iterator i = Libraries.begin(), |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 164 | e = Libraries.end(); i != e; ++i) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 165 | sys::Path FullLibraryPath = sys::Path::FindLibrary(*i); |
| 166 | if (!FullLibraryPath.isEmpty() && FullLibraryPath.isDynamicLibrary()) |
| 167 | Out2 << " -load=" << FullLibraryPath.toString() << " \\\n"; |
Chris Lattner | 7e88d41 | 2004-06-02 00:10:19 +0000 | [diff] [blame] | 168 | } |
| 169 | Out2 << " $0.bc ${1+\"$@\"}\n"; |
| 170 | Out2.close(); |
| 171 | } |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 172 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 173 | // BuildLinkItems -- This function generates a LinkItemList for the LinkItems |
| 174 | // linker function by combining the Files and Libraries in the order they were |
| 175 | // declared on the command line. |
| 176 | static void BuildLinkItems( |
| 177 | Linker::ItemList& Items, |
| 178 | const cl::list<std::string>& Files, |
| 179 | const cl::list<std::string>& Libraries) { |
| 180 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 181 | // Build the list of linkage items for LinkItems. |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 182 | |
| 183 | cl::list<std::string>::const_iterator fileIt = Files.begin(); |
| 184 | cl::list<std::string>::const_iterator libIt = Libraries.begin(); |
| 185 | |
| 186 | int libPos = -1, filePos = -1; |
Reid Spencer | 05f7e79 | 2004-12-13 17:18:19 +0000 | [diff] [blame] | 187 | while ( libIt != Libraries.end() || fileIt != Files.end() ) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 188 | if (libIt != Libraries.end()) |
| 189 | libPos = Libraries.getPosition(libIt - Libraries.begin()); |
| 190 | else |
| 191 | libPos = -1; |
| 192 | if (fileIt != Files.end()) |
| 193 | filePos = Files.getPosition(fileIt - Files.begin()); |
| 194 | else |
| 195 | filePos = -1; |
| 196 | |
| 197 | if (filePos != -1 && (libPos == -1 || filePos < libPos)) { |
| 198 | // Add a source file |
| 199 | Items.push_back(std::make_pair(*fileIt++, false)); |
| 200 | } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { |
| 201 | // Add a library |
| 202 | Items.push_back(std::make_pair(*libIt++, true)); |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
Reid Spencer | 05f7e79 | 2004-12-13 17:18:19 +0000 | [diff] [blame] | 206 | |
Reid Spencer | 93f8f55 | 2004-12-13 23:44:23 +0000 | [diff] [blame] | 207 | int main(int argc, char **argv, char **envp ) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 208 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 209 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 210 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 211 | int exitCode = 0; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 212 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 213 | std::string ProgName = sys::Path(argv[0]).getBasename(); |
| 214 | Linker TheLinker(ProgName, Verbose); |
| 215 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 216 | try { |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 217 | // Remove any consecutive duplicates of the same library... |
| 218 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 219 | Libraries.end()); |
Chris Lattner | fd35c64 | 2003-11-03 17:27:17 +0000 | [diff] [blame] | 220 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 221 | TheLinker.addPaths(LibPaths); |
| 222 | TheLinker.addSystemPaths(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 223 | |
Reid Spencer | c191d49 | 2004-12-05 19:15:29 +0000 | [diff] [blame] | 224 | if (LinkAsLibrary) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 225 | std::vector<sys::Path> Files; |
| 226 | for (unsigned i = 0; i < InputFilenames.size(); ++i ) |
| 227 | Files.push_back(sys::Path(InputFilenames[i])); |
| 228 | |
| 229 | if (TheLinker.LinkInFiles(Files)) |
| 230 | return 1; // Error already printed by linker |
| 231 | |
Reid Spencer | 6b463b2 | 2004-12-08 05:17:40 +0000 | [diff] [blame] | 232 | // The libraries aren't linked in but are noted as "dependent" in the |
| 233 | // module. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 234 | for (cl::list<std::string>::const_iterator I = Libraries.begin(), |
Reid Spencer | 6b463b2 | 2004-12-08 05:17:40 +0000 | [diff] [blame] | 235 | E = Libraries.end(); I != E ; ++I) { |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 236 | TheLinker.getModule()->addLibrary(*I); |
Reid Spencer | 6b463b2 | 2004-12-08 05:17:40 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Reid Spencer | c191d49 | 2004-12-05 19:15:29 +0000 | [diff] [blame] | 239 | } else { |
| 240 | // Build a list of the items from our command line |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 241 | Linker::ItemList Items; |
Reid Spencer | c191d49 | 2004-12-05 19:15:29 +0000 | [diff] [blame] | 242 | BuildLinkItems(Items, InputFilenames, Libraries); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 243 | |
Reid Spencer | c191d49 | 2004-12-05 19:15:29 +0000 | [diff] [blame] | 244 | // Link all the items together |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 245 | if (TheLinker.LinkInItems(Items)) |
Reid Spencer | c191d49 | 2004-12-05 19:15:29 +0000 | [diff] [blame] | 246 | return 1; // Error already printed |
| 247 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 248 | |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 249 | // We're done with the Linker, so tell it to release its module |
| 250 | std::auto_ptr<Module> Composite(TheLinker.releaseModule()); |
| 251 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 252 | // Create the output file. |
| 253 | std::string RealBytecodeOutput = OutputFilename; |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 254 | if (!LinkAsLibrary || Native || NativeCBE) RealBytecodeOutput += ".bc"; |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 255 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 256 | std::ios::binary; |
| 257 | std::ofstream Out(RealBytecodeOutput.c_str(), io_mode); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 258 | if (!Out.good()) |
| 259 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 260 | "' for writing!"); |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 261 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 262 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 263 | // SIGINT signal. |
| 264 | sys::RemoveFileOnSignal(sys::Path(RealBytecodeOutput)); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 265 | |
Chris Lattner | bcfe1e2 | 2004-12-12 07:53:51 +0000 | [diff] [blame] | 266 | // Strip everything if Strip is set, otherwise if stripdebug is set, just |
| 267 | // strip debug info. |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 268 | int StripLevel = Strip ? 2 : (StripDebug ? 1 : 0); |
Chris Lattner | bcfe1e2 | 2004-12-12 07:53:51 +0000 | [diff] [blame] | 269 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 270 | // Internalize the module if neither -disable-internalize nor |
Chris Lattner | bcfe1e2 | 2004-12-12 07:53:51 +0000 | [diff] [blame] | 271 | // -link-as-library are passed in. |
| 272 | bool ShouldInternalize = !NoInternalize & !LinkAsLibrary; |
| 273 | |
| 274 | // Generate the bytecode file. |
| 275 | if (GenerateBytecode(Composite.get(), StripLevel, ShouldInternalize, &Out)){ |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 276 | Out.close(); |
| 277 | return PrintAndReturn(argv[0], "error generating bytecode"); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 278 | } |
Misha Brukman | c1fdca8 | 2003-08-20 20:38:15 +0000 | [diff] [blame] | 279 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 280 | // Close the bytecode file. |
| 281 | Out.close(); |
| 282 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 283 | // Generate either a native file or a JIT shell script. If the user wants |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 284 | // to generate a native file, compile it from the bytecode file. Otherwise, |
| 285 | // if the target is not a library, create a script that will run the |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 286 | // bytecode through the JIT. |
| 287 | if (Native) { |
| 288 | // Name of the Assembly Language output file |
| 289 | sys::Path AssemblyFile (OutputFilename); |
| 290 | AssemblyFile.appendSuffix("s"); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 291 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 292 | // Mark the output files for removal if we get an interrupt. |
| 293 | sys::RemoveFileOnSignal(AssemblyFile); |
| 294 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 295 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 296 | // Determine the locations of the llc and gcc programs. |
| 297 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 298 | if (llc.isEmpty()) |
| 299 | return PrintAndReturn(argv[0], "Failed to find llc"); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 300 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 301 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 302 | if (gcc.isEmpty()) |
| 303 | return PrintAndReturn(argv[0], "Failed to find gcc"); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 304 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 305 | // Generate an assembly language file for the bytecode. |
| 306 | if (Verbose) std::cout << "Generating Assembly Code\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 307 | GenerateAssembly(AssemblyFile.toString(), RealBytecodeOutput, llc, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 308 | Verbose); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 309 | if (Verbose) std::cout << "Generating Native Code\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 310 | GenerateNative(OutputFilename, AssemblyFile.toString(), |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 311 | LibPaths, Libraries, gcc, envp, LinkAsLibrary, RPath, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 312 | SOName, Verbose); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 313 | |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 314 | if (!SaveTemps) { |
| 315 | // Remove the assembly language file. |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 316 | AssemblyFile.destroy(); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 317 | // Remove the bytecode language file. |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 318 | sys::Path(RealBytecodeOutput).destroy(); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 319 | } |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 320 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 321 | } else if (NativeCBE) { |
| 322 | sys::Path CFile (OutputFilename); |
| 323 | CFile.appendSuffix("cbe.c"); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 324 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 325 | // Mark the output files for removal if we get an interrupt. |
| 326 | sys::RemoveFileOnSignal(CFile); |
| 327 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 328 | |
| 329 | // Determine the locations of the llc and gcc programs. |
| 330 | sys::Path llc = FindExecutable("llc", argv[0]); |
| 331 | if (llc.isEmpty()) |
| 332 | return PrintAndReturn(argv[0], "Failed to find llc"); |
| 333 | |
| 334 | sys::Path gcc = FindExecutable("gcc", argv[0]); |
| 335 | if (gcc.isEmpty()) |
| 336 | return PrintAndReturn(argv[0], "Failed to find gcc"); |
| 337 | |
| 338 | // Generate an assembly language file for the bytecode. |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 339 | if (Verbose) std::cout << "Generating C Source Code\n"; |
| 340 | GenerateCFile(CFile.toString(), RealBytecodeOutput, llc, Verbose); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 341 | if (Verbose) std::cout << "Generating Native Code\n"; |
| 342 | GenerateNative(OutputFilename, CFile.toString(), |
| 343 | LibPaths, Libraries, gcc, envp, LinkAsLibrary, RPath, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 344 | SOName, Verbose); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 345 | |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 346 | if (!SaveTemps) { |
| 347 | // Remove the assembly language file. |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 348 | CFile.destroy(); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 349 | // Remove the bytecode language file. |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 350 | sys::Path(RealBytecodeOutput).destroy(); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 351 | } |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 352 | |
| 353 | } else if (!LinkAsLibrary) { |
| 354 | EmitShellScript(argv); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 355 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 356 | // Make the bytecode file readable and directly executable in LLEE |
Reid Spencer | 319cdec | 2004-12-13 20:03:02 +0000 | [diff] [blame] | 357 | sys::Path(RealBytecodeOutput).makeExecutable(); |
| 358 | sys::Path(RealBytecodeOutput).makeReadable(); |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 359 | } |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 360 | |
| 361 | // Make the output, whether native or script, executable as well... |
| 362 | sys::Path(OutputFilename).makeExecutable(); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 363 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 364 | } catch (const char*msg) { |
| 365 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 366 | exitCode = 1; |
| 367 | } catch (const std::string& msg) { |
| 368 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 369 | exitCode = 2; |
| 370 | } catch (...) { |
| 371 | // This really shouldn't happen, but just in case .... |
Reid Spencer | c406413 | 2004-12-13 03:01:14 +0000 | [diff] [blame] | 372 | std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n"; |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 373 | exitCode = 3; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 374 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 375 | |
Reid Spencer | 4c1af90 | 2004-11-14 22:17:03 +0000 | [diff] [blame] | 376 | return exitCode; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 377 | } |