Chris Lattner | 5ff2e05 | 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 | 7608a46 | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 3b08c2f | 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 | 35c4541 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 35c4541 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 24 | #include "Support/CommandLine.h" |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 25 | #include "Support/Signals.h" |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 26 | #include <fstream> |
| 27 | #include <memory> |
Chris Lattner | 6f0d453 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 29 | #include <sys/types.h> // For FileExists |
| 30 | #include <sys/stat.h> |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 2b3a5db | 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 | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 2b3a5db | 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 | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 2b3a5db | 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 | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 2b3a5db | 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 | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 2b3a5db | 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 | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 2b3a5db | 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")); |
Chris Lattner | 602d209 | 2003-04-18 23:38:22 +0000 | [diff] [blame^] | 58 | |
| 59 | // Compatibility options that are ignored, but support by LD |
| 60 | cl::opt<std::string> |
| 61 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 62 | cl::opt<std::string> |
| 63 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 64 | cl::opt<bool> |
| 65 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 67 | |
| 68 | // FileExists - Return true if the specified string is an openable file... |
| 69 | static inline bool FileExists(const std::string &FN) { |
| 70 | struct stat StatBuf; |
| 71 | return stat(FN.c_str(), &StatBuf) != -1; |
| 72 | } |
| 73 | |
| 74 | // LoadFile - Read the specified bytecode file in and return it. This routine |
| 75 | // searches the link path for the specified file to try to find it... |
| 76 | // |
| 77 | static inline std::auto_ptr<Module> LoadFile(const std::string &FN) { |
| 78 | std::string Filename = FN; |
| 79 | std::string ErrorMessage; |
| 80 | |
| 81 | unsigned NextLibPathIdx = 0; |
| 82 | bool FoundAFile = false; |
| 83 | |
| 84 | while (1) { |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 85 | if (Verbose) std::cerr << "Loading '" << Filename << "'\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 86 | if (FileExists(Filename)) FoundAFile = true; |
| 87 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 88 | if (Result) return std::auto_ptr<Module>(Result); // Load successful! |
| 89 | |
| 90 | if (Verbose) { |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 91 | std::cerr << "Error opening bytecode file: '" << Filename << "'"; |
| 92 | if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage; |
| 93 | std::cerr << "\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (NextLibPathIdx == LibPaths.size()) break; |
| 97 | Filename = LibPaths[NextLibPathIdx++] + "/" + FN; |
| 98 | } |
| 99 | |
| 100 | if (FoundAFile) |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 101 | std::cerr << "Bytecode file '" << FN << "' corrupt! " |
| 102 | << "Use 'gccld -v ...' for more info.\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 103 | else |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 104 | std::cerr << "Could not locate bytecode file: '" << FN << "'\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 105 | return std::auto_ptr<Module>(); |
| 106 | } |
| 107 | |
| 108 | |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 109 | int main(int argc, char **argv) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 110 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 111 | |
| 112 | unsigned BaseArg = 0; |
| 113 | std::string ErrorMessage; |
| 114 | |
Chris Lattner | 6f0d453 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 115 | if (!Libraries.empty()) { |
| 116 | // Sort libraries list... |
Chris Lattner | c19be16 | 2002-06-30 16:20:02 +0000 | [diff] [blame] | 117 | std::sort(Libraries.begin(), Libraries.end()); |
Chris Lattner | 6f0d453 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 118 | |
| 119 | // Remove duplicate libraries entries... |
| 120 | Libraries.erase(unique(Libraries.begin(), Libraries.end()), |
| 121 | Libraries.end()); |
| 122 | |
| 123 | // Add all of the libraries to the end of the link line... |
| 124 | for (unsigned i = 0; i < Libraries.size(); ++i) |
| 125 | InputFilenames.push_back("lib" + Libraries[i] + ".bc"); |
| 126 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 127 | |
| 128 | std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg])); |
| 129 | if (Composite.get() == 0) return 1; |
| 130 | |
| 131 | for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) { |
| 132 | std::auto_ptr<Module> M(LoadFile(InputFilenames[i])); |
| 133 | if (M.get() == 0) return 1; |
| 134 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 135 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 136 | |
| 137 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) { |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 138 | std::cerr << argv[0] << ": error linking in '" << InputFilenames[i] |
| 139 | << "': " << ErrorMessage << "\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | |
Chris Lattner | c34061f | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 144 | // In addition to just linking the input from GCC, we also want to spiff it up |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 145 | // a little bit. Do this now. |
| 146 | // |
| 147 | PassManager Passes; |
| 148 | |
| 149 | // Linking modules together can lead to duplicated global constants, only keep |
| 150 | // one copy of each constant... |
| 151 | // |
| 152 | Passes.add(createConstantMergePass()); |
| 153 | |
Chris Lattner | 2b33d75 | 2002-04-08 05:18:12 +0000 | [diff] [blame] | 154 | // If the -s command line option was specified, strip the symbols out of the |
| 155 | // resulting program to make it smaller. -s is a GCC option that we are |
| 156 | // supporting. |
| 157 | // |
| 158 | if (Strip) |
| 159 | Passes.add(createSymbolStrippingPass()); |
| 160 | |
Chris Lattner | c34061f | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 161 | // Often if the programmer does not specify proper prototypes for the |
| 162 | // functions they are calling, they end up calling a vararg version of the |
| 163 | // function that does not get a body filled in (the real function has typed |
| 164 | // arguments). This pass merges the two functions. |
| 165 | // |
| 166 | Passes.add(createFunctionResolvingPass()); |
| 167 | |
Chris Lattner | a9a9880 | 2003-04-16 21:43:22 +0000 | [diff] [blame] | 168 | if (!NoInternalize) { |
| 169 | // Now that composite has been compiled, scan through the module, looking |
| 170 | // for a main function. If main is defined, mark all other functions |
| 171 | // internal. |
| 172 | // |
| 173 | Passes.add(createInternalizePass()); |
| 174 | } |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 176 | // Now that we have optimized the program, discard unreachable functions... |
| 177 | // |
| 178 | Passes.add(createGlobalDCEPass()); |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 180 | // Add the pass that writes bytecode to the output file... |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 181 | std::ofstream Out((OutputFilename+".bc").c_str()); |
| 182 | if (!Out.good()) { |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 183 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 184 | << ".bc' for writing!\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 185 | return 1; |
| 186 | } |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 187 | Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file... |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 188 | |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 189 | // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT |
| 190 | RemoveFileOnSignal(OutputFilename+".bc"); |
| 191 | |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 192 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 193 | Passes.run(*Composite.get()); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 194 | Out.close(); |
| 195 | |
| 196 | // Output the script to start the program... |
| 197 | std::ofstream Out2(OutputFilename.c_str()); |
| 198 | if (!Out2.good()) { |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 199 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 200 | << "' for writing!\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 201 | return 1; |
| 202 | } |
Chris Lattner | c451fb4 | 2002-12-14 21:28:32 +0000 | [diff] [blame] | 203 | Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 204 | Out2.close(); |
| 205 | |
| 206 | // Make the script executable... |
| 207 | chmod(OutputFilename.c_str(), 0755); |
| 208 | |
| 209 | return 0; |
| 210 | } |