Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // LLVM 'GCCLD' UTILITY |
| 3 | // |
| 4 | // This utility is intended to be compatible with GCC, and follows standard |
| 5 | // system ld conventions. As such, the default output file is ./a.out. |
| 6 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 7 | // to execute the program. In this manner, the generated executable (a.out for |
| 8 | // example), is directly executable, whereas the bytecode file actually lives in |
| 9 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 10 | // |
| 11 | // Note that if someone (or a script) deletes the executable program generated, |
| 12 | // the .bc file will be left around. Considering that this is a temporary hack, |
| 13 | // I'm not to worried about this. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
| 20 | #include "llvm/Bytecode/Reader.h" |
| 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 24 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 25 | #include "Support/Signals.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 26 | #include <fstream> |
| 27 | #include <memory> |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 29 | #include <sys/types.h> // For FileExists |
| 30 | #include <sys/stat.h> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 31 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 32 | namespace { |
| 33 | cl::list<std::string> |
| 34 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 35 | cl::OneOrMore); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 37 | cl::opt<std::string> |
| 38 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 39 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 41 | cl::opt<bool> |
| 42 | Verbose("v", cl::desc("Print information about actions taken")); |
| 43 | |
| 44 | cl::list<std::string> |
| 45 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 46 | cl::value_desc("directory")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 48 | cl::list<std::string> |
| 49 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 50 | cl::value_desc("library prefix")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 51 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 52 | cl::opt<bool> |
| 53 | Strip("s", cl::desc("Strip symbol info from executable")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 54 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 55 | cl::opt<bool> |
| 56 | NoInternalize("disable-internalize", |
| 57 | cl::desc("Do not mark all symbols as internal")); |
| 58 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 59 | |
| 60 | // FileExists - Return true if the specified string is an openable file... |
| 61 | static inline bool FileExists(const std::string &FN) { |
| 62 | struct stat StatBuf; |
| 63 | return stat(FN.c_str(), &StatBuf) != -1; |
| 64 | } |
| 65 | |
| 66 | // LoadFile - Read the specified bytecode file in and return it. This routine |
| 67 | // searches the link path for the specified file to try to find it... |
| 68 | // |
| 69 | static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { |
| 70 | std::string Filename = FN; |
| 71 | std::string ErrorMessage; |
| 72 | |
| 73 | unsigned NextLibPathIdx = 0; |
| 74 | bool FoundAFile = false; |
| 75 | |
| 76 | while (1) { |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 77 | if (Verbose) std::cerr << "Loading '" << Filename << "'\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 78 | if (FileExists(Filename)) FoundAFile = true; |
| 79 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 80 | if (Result) return std::auto_ptr<Module>(Result); // Load successful! |
| 81 | |
| 82 | if (Verbose) { |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 83 | std::cerr << "Error opening bytecode file: '" << Filename << "'"; |
| 84 | if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage; |
| 85 | std::cerr << "\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (NextLibPathIdx == LibPaths.size()) break; |
| 89 | Filename = LibPaths[NextLibPathIdx++] + "/" + FN; |
| 90 | } |
| 91 | |
| 92 | if (FoundAFile) |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 93 | std::cerr << "Bytecode file '" << FN << "' corrupt! " |
| 94 | << "Use 'gccld -v ...' for more info.\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 95 | else |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 96 | std::cerr << "Could not locate bytecode file: '" << FN << "'\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 97 | return std::auto_ptr<Module>(); |
| 98 | } |
| 99 | |
| 100 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 101 | int main(int argc, char **argv) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 102 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 103 | |
| 104 | unsigned BaseArg = 0; |
| 105 | std::string ErrorMessage; |
| 106 | |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 107 | if (!Libraries.empty()) { |
| 108 | // Sort libraries list... |
Chris Lattner | c5d4493 | 2002-06-30 16:20:02 +0000 | [diff] [blame] | 109 | std::sort(Libraries.begin(), Libraries.end()); |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 110 | |
| 111 | // Remove duplicate libraries entries... |
| 112 | Libraries.erase(unique(Libraries.begin(), Libraries.end()), |
| 113 | Libraries.end()); |
| 114 | |
| 115 | // Add all of the libraries to the end of the link line... |
| 116 | for (unsigned i = 0; i < Libraries.size(); ++i) |
| 117 | InputFilenames.push_back("lib" + Libraries[i] + ".bc"); |
| 118 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 119 | |
| 120 | std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg])); |
| 121 | if (Composite.get() == 0) return 1; |
| 122 | |
| 123 | for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { |
| 124 | std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); |
| 125 | if (M.get() == 0) return 1; |
| 126 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 127 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 128 | |
| 129 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 130 | std::cerr << argv[0] << ": error linking in '" << InputFilenames[i] |
| 131 | << "': " << ErrorMessage << "\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 136 | // In addition to just linking the input from GCC, we also want to spiff it up |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 137 | // a little bit. Do this now. |
| 138 | // |
| 139 | PassManager Passes; |
| 140 | |
| 141 | // Linking modules together can lead to duplicated global constants, only keep |
| 142 | // one copy of each constant... |
| 143 | // |
| 144 | Passes.add(createConstantMergePass()); |
| 145 | |
Chris Lattner | 2b59837 | 2002-04-08 05:18:12 +0000 | [diff] [blame] | 146 | // If the -s command line option was specified, strip the symbols out of the |
| 147 | // resulting program to make it smaller. -s is a GCC option that we are |
| 148 | // supporting. |
| 149 | // |
| 150 | if (Strip) |
| 151 | Passes.add(createSymbolStrippingPass()); |
| 152 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 153 | // Often if the programmer does not specify proper prototypes for the |
| 154 | // functions they are calling, they end up calling a vararg version of the |
| 155 | // function that does not get a body filled in (the real function has typed |
| 156 | // arguments). This pass merges the two functions. |
| 157 | // |
| 158 | Passes.add(createFunctionResolvingPass()); |
| 159 | |
Chris Lattner | dabaa46 | 2003-04-16 21:43:22 +0000 | [diff] [blame] | 160 | if (!NoInternalize) { |
| 161 | // Now that composite has been compiled, scan through the module, looking |
| 162 | // for a main function. If main is defined, mark all other functions |
| 163 | // internal. |
| 164 | // |
| 165 | Passes.add(createInternalizePass()); |
| 166 | } |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 167 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 168 | // Now that we have optimized the program, discard unreachable functions... |
| 169 | // |
| 170 | Passes.add(createGlobalDCEPass()); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 171 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 172 | // Add the pass that writes bytecode to the output file... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 173 | std::ofstream Out((OutputFilename+".bc").c_str()); |
| 174 | if (!Out.good()) { |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 175 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 176 | << ".bc' for writing!\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 177 | return 1; |
| 178 | } |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 179 | Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 181 | // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT |
| 182 | RemoveFileOnSignal(OutputFilename+".bc"); |
| 183 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 184 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 185 | Passes.run(*Composite.get()); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 186 | Out.close(); |
| 187 | |
| 188 | // Output the script to start the program... |
| 189 | std::ofstream Out2(OutputFilename.c_str()); |
| 190 | if (!Out2.good()) { |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame^] | 191 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 192 | << "' for writing!\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 193 | return 1; |
| 194 | } |
Chris Lattner | debb5ee | 2002-12-14 21:28:32 +0000 | [diff] [blame] | 195 | Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 196 | Out2.close(); |
| 197 | |
| 198 | // Make the script executable... |
| 199 | chmod(OutputFilename.c_str(), 0755); |
| 200 | |
| 201 | return 0; |
| 202 | } |