Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // This utility is intended to be compatible with GCC, and follows standard |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 4 | // system 'ld' conventions. As such, the default output file is ./a.out. |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 5 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 6 | // to execute the program. In this manner, the generated executable (a.out for |
| 7 | // example), is directly executable, whereas the bytecode file actually lives in |
| 8 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 9 | // |
| 10 | // Note that if someone (or a script) deletes the executable program generated, |
| 11 | // 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] | 12 | // I'm not too worried about this. |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 7608a46 | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/Bytecode/Reader.h" |
| 20 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | d571e2a | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.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" |
John Criswell | 06327da | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 24 | #include "Support/FileUtilities.h" |
John Criswell | f22d845 | 2003-09-17 15:20:51 +0000 | [diff] [blame] | 25 | #include "Support/SystemUtils.h" |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 27 | #include "Support/Signals.h" |
John Criswell | a3ce8b4 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 28 | #include "Config/unistd.h" |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 29 | #include "util.h" |
| 30 | |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 31 | #include <fstream> |
| 32 | #include <memory> |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 33 | #include <set> |
Chris Lattner | 6f0d453 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 35 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 36 | // |
| 37 | // External function prototypes |
| 38 | // |
| 39 | extern int |
| 40 | GenerateBytecode (Module * M, |
| 41 | bool Strip, |
| 42 | bool Internalize, |
| 43 | std::ofstream * Out); |
| 44 | |
| 45 | extern int |
| 46 | generate_assembly (std::string OutputFilename, |
| 47 | std::string InputFilename, |
| 48 | std::string llc, |
| 49 | char ** const envp); |
| 50 | extern int |
| 51 | generate_native (std::string OutputFilename, |
| 52 | std::string InputFilename, |
| 53 | std::vector<std::string> Libraries, |
| 54 | std::string gcc, |
| 55 | char ** const envp); |
| 56 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 57 | namespace { |
| 58 | cl::list<std::string> |
| 59 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 60 | cl::OneOrMore); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 62 | cl::opt<std::string> |
| 63 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 64 | cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 66 | cl::opt<bool> |
| 67 | Verbose("v", cl::desc("Print information about actions taken")); |
| 68 | |
| 69 | cl::list<std::string> |
| 70 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 71 | cl::value_desc("directory")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 73 | cl::list<std::string> |
| 74 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 75 | cl::value_desc("library prefix")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 77 | cl::opt<bool> |
| 78 | Strip("s", cl::desc("Strip symbol info from executable")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 80 | cl::opt<bool> |
| 81 | NoInternalize("disable-internalize", |
| 82 | cl::desc("Do not mark all symbols as internal")); |
Chris Lattner | b4d9921 | 2003-08-22 19:18:45 +0000 | [diff] [blame] | 83 | static cl::alias |
| 84 | ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), |
| 85 | cl::aliasopt(NoInternalize)); |
Chris Lattner | 602d209 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 87 | cl::opt<bool> |
| 88 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 89 | " library, not an executable")); |
| 90 | |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 91 | cl::opt<bool> |
| 92 | Native("native", cl::desc("Generate a native binary instead of a shell script")); |
| 93 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 94 | // Compatibility options that are ignored but supported by LD |
Chris Lattner | 602d209 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 95 | cl::opt<std::string> |
| 96 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 97 | cl::opt<std::string> |
| 98 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 99 | cl::opt<bool> |
| 100 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | e320c92 | 2003-05-27 19:15:11 +0000 | [diff] [blame] | 101 | cl::opt<bool> |
| 102 | CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 104 | |
| 105 | // FileExists - Return true if the specified string is an openable file... |
| 106 | static inline bool FileExists(const std::string &FN) { |
John Criswell | a3ce8b4 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 107 | return access(FN.c_str(), F_OK) != -1; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 111 | // LoadObject - Read the specified "object file", which should not search the |
| 112 | // library path to find it. |
Chris Lattner | a2d3504 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 113 | static inline std::auto_ptr<Module> LoadObject(std::string FN, |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 114 | std::string &OutErrorMessage) { |
| 115 | if (Verbose) std::cerr << "Loading '" << FN << "'\n"; |
| 116 | if (!FileExists(FN)) { |
Chris Lattner | a2d3504 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 117 | // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would |
| 118 | // otherwise fail. This is used to locate objects like crtend.o. |
| 119 | // |
| 120 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 121 | if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN)) |
| 122 | FN = std::string(SearchPath)+"/"+FN; |
| 123 | else { |
| 124 | OutErrorMessage = "could not find input file '" + FN + "'!"; |
| 125 | return std::auto_ptr<Module>(); |
| 126 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 129 | std::string ErrorMessage; |
| 130 | Module *Result = ParseBytecodeFile(FN, &ErrorMessage); |
| 131 | if (Result) return std::auto_ptr<Module>(Result); |
| 132 | |
| 133 | OutErrorMessage = "Bytecode file '" + FN + "' corrupt!"; |
| 134 | if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 135 | return std::auto_ptr<Module>(); |
| 136 | } |
| 137 | |
| 138 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 139 | static Module *LoadSingleLibraryObject(const std::string &Filename) { |
| 140 | std::string ErrorMessage; |
| 141 | std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage); |
| 142 | if (M.get() == 0 && Verbose) { |
| 143 | std::cerr << "Error loading '" + Filename + "'"; |
| 144 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 145 | std::cerr << "\n"; |
| 146 | } |
| 147 | |
| 148 | return M.release(); |
| 149 | } |
| 150 | |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 151 | // LoadLibraryExactName - This looks for a file with a known name and tries to |
| 152 | // load it, similarly to LoadLibraryFromDirectory(). |
Chris Lattner | 4c46c95 | 2003-05-29 15:13:15 +0000 | [diff] [blame] | 153 | static inline bool LoadLibraryExactName(const std::string &FileName, |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 154 | std::vector<Module*> &Objects, bool &isArchive) { |
| 155 | if (Verbose) std::cerr << " Considering '" << FileName << "'\n"; |
| 156 | if (FileExists(FileName)) { |
Chris Lattner | 4c46c95 | 2003-05-29 15:13:15 +0000 | [diff] [blame] | 157 | if (IsArchive(FileName)) { |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 158 | std::string ErrorMessage; |
| 159 | if (Verbose) std::cerr << " Loading '" << FileName << "'\n"; |
| 160 | if (!ReadArchiveFile(FileName, Objects, &ErrorMessage)) { |
| 161 | isArchive = true; |
| 162 | return false; // Success! |
| 163 | } |
| 164 | if (Verbose) { |
| 165 | std::cerr << " Error loading archive '" + FileName + "'"; |
| 166 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 167 | std::cerr << "\n"; |
| 168 | } |
| 169 | } else { |
| 170 | if (Module *M = LoadSingleLibraryObject(FileName)) { |
| 171 | isArchive = false; |
| 172 | Objects.push_back(M); |
| 173 | return false; |
| 174 | } |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 180 | // LoadLibrary - Try to load a library named LIBNAME that contains |
| 181 | // LLVM bytecode. If SEARCH is true, then search for a file named |
| 182 | // libLIBNAME.{a,so,bc} in the current library search path. Otherwise, |
| 183 | // assume LIBNAME is the real name of the library file. This method puts |
| 184 | // the loaded modules into the Objects list, and sets isArchive to true if |
| 185 | // a .a file was loaded. It returns true if no library is found or if an |
| 186 | // error occurs; otherwise it returns false. |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 187 | // |
| 188 | static inline bool LoadLibrary(const std::string &LibName, |
| 189 | std::vector<Module*> &Objects, bool &isArchive, |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 190 | bool search, std::string &ErrorMessage) { |
| 191 | if (search) { |
| 192 | // First, try the current directory. Then, iterate over the |
| 193 | // directories in LibPaths, looking for a suitable match for LibName |
| 194 | // in each one. |
| 195 | for (unsigned NextLibPathIdx = 0; NextLibPathIdx != LibPaths.size(); |
Chris Lattner | 4c46c95 | 2003-05-29 15:13:15 +0000 | [diff] [blame] | 196 | ++NextLibPathIdx) { |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 197 | std::string Directory = LibPaths[NextLibPathIdx] + "/"; |
| 198 | if (!LoadLibraryExactName(Directory + "lib" + LibName + ".a", |
| 199 | Objects, isArchive)) |
| 200 | return false; |
| 201 | if (!LoadLibraryExactName(Directory + "lib" + LibName + ".so", |
| 202 | Objects, isArchive)) |
| 203 | return false; |
| 204 | if (!LoadLibraryExactName(Directory + "lib" + LibName + ".bc", |
| 205 | Objects, isArchive)) |
| 206 | return false; |
| 207 | } |
| 208 | } else { |
| 209 | // If they said no searching, then assume LibName is the real name. |
| 210 | if (!LoadLibraryExactName(LibName, Objects, isArchive)) |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 211 | return false; |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 213 | ErrorMessage = "error linking library '-l" + LibName+ "': library not found!"; |
| 214 | return true; |
| 215 | } |
| 216 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 217 | |
| 218 | static bool LinkLibrary(Module *M, const std::string &LibName, |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 219 | bool search, std::string &ErrorMessage) { |
Chris Lattner | a2d3504 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 220 | std::set<std::string> UndefinedSymbols; |
| 221 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 222 | if (UndefinedSymbols.empty()) { |
| 223 | if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; |
| 224 | return false; // No need to link anything in! |
| 225 | } |
| 226 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 227 | std::vector<Module*> Objects; |
| 228 | bool isArchive; |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 229 | if (LoadLibrary(LibName, Objects, isArchive, search, ErrorMessage)) |
| 230 | return true; |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 231 | |
| 232 | // Figure out which symbols are defined by all of the modules in the .a file |
| 233 | std::vector<std::set<std::string> > DefinedSymbols; |
| 234 | DefinedSymbols.resize(Objects.size()); |
| 235 | for (unsigned i = 0; i != Objects.size(); ++i) |
| 236 | GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); |
| 237 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 238 | bool Linked = true; |
| 239 | while (Linked) { // While we are linking in object files, loop. |
| 240 | Linked = false; |
| 241 | |
| 242 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 243 | // Consider whether we need to link in this module... we only need to |
| 244 | // link it in if it defines some symbol which is so far undefined. |
| 245 | // |
| 246 | const std::set<std::string> &DefSymbols = DefinedSymbols[i]; |
| 247 | |
| 248 | bool ObjectRequired = false; |
| 249 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), |
| 250 | E = UndefinedSymbols.end(); I != E; ++I) |
| 251 | if (DefSymbols.count(*I)) { |
| 252 | if (Verbose) |
| 253 | std::cerr << " Found object providing symbol '" << *I << "'...\n"; |
| 254 | ObjectRequired = true; |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | // We DO need to link this object into the program... |
| 259 | if (ObjectRequired) { |
| 260 | if (LinkModules(M, Objects[i], &ErrorMessage)) |
| 261 | return true; // Couldn't link in the right object file... |
| 262 | |
| 263 | // Since we have linked in this object, delete it from the list of |
| 264 | // objects to consider in this archive file. |
| 265 | std::swap(Objects[i], Objects.back()); |
| 266 | std::swap(DefinedSymbols[i], DefinedSymbols.back()); |
| 267 | Objects.pop_back(); |
| 268 | DefinedSymbols.pop_back(); |
| 269 | --i; // Do not skip an entry |
| 270 | |
| 271 | // The undefined symbols set should have shrunk. |
| 272 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 273 | Linked = true; // We have linked something in! |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 281 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 282 | int |
| 283 | main(int argc, char **argv, char ** envp) |
John Criswell | f22d845 | 2003-09-17 15:20:51 +0000 | [diff] [blame] | 284 | { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 285 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 287 | std::string ErrorMessage; |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 288 | std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage)); |
| 289 | if (Composite.get() == 0) |
| 290 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 291 | |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 292 | // We always look first in the current directory when searching for libraries. |
| 293 | LibPaths.insert(LibPaths.begin(), "."); |
| 294 | |
Chris Lattner | da3bc21 | 2003-04-21 19:53:24 +0000 | [diff] [blame] | 295 | // If the user specied an extra search path in their environment, respect it. |
| 296 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) |
| 297 | LibPaths.push_back(SearchPath); |
| 298 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 299 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 300 | // A user may specify an ar archive without -l, perhaps because it |
| 301 | // is not installed as a library. Detect that and link the library. |
Chris Lattner | 4c46c95 | 2003-05-29 15:13:15 +0000 | [diff] [blame] | 302 | if (IsArchive(InputFilenames[i])) { |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 303 | if (Verbose) std::cerr << "Linking archive '" << InputFilenames[i] |
| 304 | << "'\n"; |
Chris Lattner | 4c46c95 | 2003-05-29 15:13:15 +0000 | [diff] [blame] | 305 | if (LinkLibrary(Composite.get(), InputFilenames[i], false, ErrorMessage)) |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 306 | return PrintAndReturn(argv[0], ErrorMessage, |
| 307 | ": error linking in '" + InputFilenames[i] + "'"); |
| 308 | continue; |
| 309 | } |
| 310 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 311 | std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage)); |
| 312 | if (M.get() == 0) |
| 313 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 2b3a5db | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 315 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 317 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) |
| 318 | return PrintAndReturn(argv[0], ErrorMessage, |
| 319 | ": error linking in '" + InputFilenames[i] + "'"); |
| 320 | } |
| 321 | |
Chris Lattner | 4b462c0 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 322 | // Remove any consecutive duplicates of the same library... |
| 323 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 324 | Libraries.end()); |
| 325 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 326 | // Link in all of the libraries next... |
| 327 | for (unsigned i = 0; i != Libraries.size(); ++i) { |
| 328 | if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n"; |
Brian Gaeke | 5bfa37f | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 329 | if (LinkLibrary(Composite.get(), Libraries[i], true, ErrorMessage)) |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 330 | if (!Native) |
| 331 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 334 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 335 | // Create the output file. |
Chris Lattner | 3b08c2f | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 336 | // |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 337 | std::string RealBytecodeOutput = OutputFilename; |
| 338 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 339 | std::ofstream Out(RealBytecodeOutput.c_str()); |
| 340 | if (!Out.good()) |
| 341 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 342 | "' for writing!"); |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 343 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 344 | // |
| 345 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 346 | // SIGINT signal. |
| 347 | // |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 348 | RemoveFileOnSignal(RealBytecodeOutput); |
Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 349 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 350 | // |
| 351 | // Generate the bytecode file. |
| 352 | // |
| 353 | if (GenerateBytecode (Composite.get(), Strip, !NoInternalize, &Out)) |
| 354 | { |
| 355 | Out.close(); |
| 356 | return PrintAndReturn(argv[0], "error generating bytcode"); |
| 357 | } |
| 358 | |
| 359 | // |
| 360 | // Close the bytecode file. |
| 361 | // |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 362 | Out.close(); |
| 363 | |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 364 | // |
| 365 | // If we are not linking a library, generate either a native executable |
| 366 | // or a JIT shell script, depending upon what the user wants. |
| 367 | // |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 368 | if (!LinkAsLibrary) { |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 369 | // |
| 370 | // If the user wants to generate a native executable, compile it from the |
| 371 | // bytecode file. |
| 372 | // |
| 373 | // Otherwise, create a script that will run the bytecode through the JIT. |
| 374 | // |
| 375 | if (Native) |
| 376 | { |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 377 | // Name of the Assembly Language output file |
| 378 | std::string AssemblyFile = OutputFilename + ".s"; |
| 379 | |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 380 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 381 | // Mark the output files for removal if we get an interrupt. |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 382 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 383 | RemoveFileOnSignal (AssemblyFile); |
| 384 | RemoveFileOnSignal (OutputFilename); |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 385 | |
| 386 | // |
John Criswell | 0217b1b | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 387 | // Determine the locations of the llc and gcc programs. |
| 388 | // |
| 389 | std::string llc=FindExecutable ("llc", argv[0]); |
| 390 | std::string gcc=FindExecutable ("gcc", argv[0]); |
| 391 | if (llc.empty()) |
| 392 | { |
| 393 | return PrintAndReturn (argv[0], "Failed to find llc"); |
| 394 | } |
| 395 | |
| 396 | if (gcc.empty()) |
| 397 | { |
| 398 | return PrintAndReturn (argv[0], "Failed to find gcc"); |
| 399 | } |
| 400 | |
| 401 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 402 | // Generate an assembly language file for the bytecode. |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 403 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 404 | generate_assembly (AssemblyFile, RealBytecodeOutput, llc, envp); |
| 405 | generate_native (OutputFilename, AssemblyFile, Libraries, gcc, envp); |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 406 | |
| 407 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 408 | // Remove the assembly language file. |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 409 | // |
John Criswell | 8ecc302 | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 410 | removeFile (AssemblyFile); |
John Criswell | 1997a34 | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 411 | } |
| 412 | else |
| 413 | { |
| 414 | // Output the script to start the program... |
| 415 | std::ofstream Out2(OutputFilename.c_str()); |
| 416 | if (!Out2.good()) |
| 417 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 418 | "' for writing!"); |
| 419 | Out2 << "#!/bin/sh\nlli -q $0.bc $*\n"; |
| 420 | Out2.close(); |
| 421 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 423 | // Make the script executable... |
John Criswell | 06327da | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 424 | MakeFileExecutable (OutputFilename); |
Misha Brukman | 9086994 | 2003-08-20 20:38:15 +0000 | [diff] [blame] | 425 | |
John Criswell | a3ce8b4 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 426 | // Make the bytecode file readable and directly executable in LLEE as well |
John Criswell | 06327da | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 427 | MakeFileExecutable (RealBytecodeOutput); |
John Criswell | a3ce8b4 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 428 | MakeFileReadable (RealBytecodeOutput); |
Chris Lattner | 1d49617 | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | 5ff2e05 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | } |