Reid Spencer | c408c45 | 2004-11-12 20:34:32 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 10 | // This file contains routines to handle linking together LLVM bytecode files, |
| 11 | // and to handle annoying things like static libraries. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 15 | #include "llvm/Linker.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 17 | #include "llvm/ModuleProvider.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 20 | #include "llvm/Bytecode/Archive.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Transforms/IPO.h" |
| 24 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | 008248f | 2004-06-23 17:33:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Linker.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Config/config.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 29 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/SystemUtils.h" |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 31 | #include <algorithm> |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 32 | #include <fstream> |
| 33 | #include <memory> |
| 34 | #include <set> |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Brian Gaeke | 0d723ac | 2003-11-11 21:54:01 +0000 | [diff] [blame] | 37 | /// FindLib - Try to convert Filename into the name of a file that we can open, |
| 38 | /// if it does not already name a file we can open, by first trying to open |
Misha Brukman | 7a46e4c | 2004-04-15 15:23:45 +0000 | [diff] [blame] | 39 | /// Filename, then libFilename.[suffix] for each of a set of several common |
Brian Gaeke | 0d723ac | 2003-11-11 21:54:01 +0000 | [diff] [blame] | 40 | /// library suffixes, in each of the directories in Paths and the directory |
| 41 | /// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns |
| 42 | /// an empty string if no matching file can be found. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 43 | /// |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 44 | std::string llvm::FindLib(const std::string &Filename, |
| 45 | const std::vector<std::string> &Paths, |
| 46 | bool SharedObjectOnly) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 47 | // Determine if the pathname can be found as it stands. |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 48 | if (FileOpenable(Filename)) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 49 | return Filename; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 50 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 51 | // If that doesn't work, convert the name into a library name. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 52 | std::string LibName = "lib" + Filename; |
| 53 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 54 | // Iterate over the directories in Paths to see if we can find the library |
| 55 | // there. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 56 | for (unsigned Index = 0; Index != Paths.size(); ++Index) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 57 | std::string Directory = Paths[Index] + "/"; |
| 58 | |
Misha Brukman | 84fbc65 | 2003-11-20 19:08:06 +0000 | [diff] [blame] | 59 | if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc")) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 60 | return Directory + LibName + ".bc"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 61 | |
John Criswell | 7f7d16b | 2004-01-26 20:59:41 +0000 | [diff] [blame] | 62 | if (FileOpenable(Directory + LibName + SHLIBEXT)) |
| 63 | return Directory + LibName + SHLIBEXT; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 64 | |
Misha Brukman | 84fbc65 | 2003-11-20 19:08:06 +0000 | [diff] [blame] | 65 | if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a")) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 66 | return Directory + LibName + ".a"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 67 | } |
| 68 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 69 | // One last hope: Check LLVM_LIB_SEARCH_PATH. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 70 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 71 | if (SearchPath == NULL) |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 72 | return std::string(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 73 | |
| 74 | LibName = std::string(SearchPath) + "/" + LibName; |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 75 | if (FileOpenable(LibName)) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 76 | return LibName; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 77 | |
| 78 | return std::string(); |
| 79 | } |
| 80 | |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 81 | /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the |
| 82 | /// name of each externally-visible symbol defined in M. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 83 | /// |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 84 | void llvm::GetAllDefinedSymbols(Module *M, |
| 85 | std::set<std::string> &DefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 86 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 87 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 88 | DefinedSymbols.insert(I->getName()); |
| 89 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 90 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 91 | DefinedSymbols.insert(I->getName()); |
| 92 | } |
| 93 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 94 | /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still |
| 95 | /// exist in an LLVM module. This is a bit tricky because there may be two |
| 96 | /// symbols with the same name but different LLVM types that will be resolved to |
| 97 | /// each other but aren't currently (thus we need to treat it as resolved). |
| 98 | /// |
| 99 | /// Inputs: |
| 100 | /// M - The module in which to find undefined symbols. |
| 101 | /// |
| 102 | /// Outputs: |
| 103 | /// UndefinedSymbols - A set of C++ strings containing the name of all |
| 104 | /// undefined symbols. |
| 105 | /// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 106 | void |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 107 | llvm::GetAllUndefinedSymbols(Module *M, |
| 108 | std::set<std::string> &UndefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 109 | std::set<std::string> DefinedSymbols; |
| 110 | UndefinedSymbols.clear(); // Start out empty |
| 111 | |
| 112 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 113 | if (I->hasName()) { |
| 114 | if (I->isExternal()) |
| 115 | UndefinedSymbols.insert(I->getName()); |
| 116 | else if (!I->hasInternalLinkage()) |
| 117 | DefinedSymbols.insert(I->getName()); |
| 118 | } |
| 119 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 120 | if (I->hasName()) { |
| 121 | if (I->isExternal()) |
| 122 | UndefinedSymbols.insert(I->getName()); |
| 123 | else if (!I->hasInternalLinkage()) |
| 124 | DefinedSymbols.insert(I->getName()); |
| 125 | } |
| 126 | |
| 127 | // Prune out any defined symbols from the undefined symbols set... |
| 128 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 129 | I != UndefinedSymbols.end(); ) |
| 130 | if (DefinedSymbols.count(*I)) |
| 131 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 132 | else |
| 133 | ++I; // Keep this symbol in the undefined symbols list |
| 134 | } |
| 135 | |
| 136 | |
Brian Gaeke | 0d723ac | 2003-11-11 21:54:01 +0000 | [diff] [blame] | 137 | /// LoadObject - Read in and parse the bytecode file named by FN and return the |
| 138 | /// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an |
| 139 | /// error occurs. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 140 | /// |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 141 | static std::auto_ptr<Module> LoadObject(const std::string &FN, |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 142 | std::string &ErrorMessage) { |
Brian Gaeke | 0d723ac | 2003-11-11 21:54:01 +0000 | [diff] [blame] | 143 | std::string ParserErrorMessage; |
| 144 | Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 145 | if (Result) return std::auto_ptr<Module>(Result); |
Brian Gaeke | 0d723ac | 2003-11-11 21:54:01 +0000 | [diff] [blame] | 146 | ErrorMessage = "Bytecode file '" + FN + "' could not be loaded"; |
| 147 | if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 148 | return std::auto_ptr<Module>(); |
| 149 | } |
| 150 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 151 | /// LinkInArchive - opens an archive library and link in all objects which |
| 152 | /// provide symbols that are currently undefined. |
| 153 | /// |
| 154 | /// Inputs: |
| 155 | /// M - The module in which to link the archives. |
| 156 | /// Filename - The pathname of the archive. |
| 157 | /// Verbose - Flags whether verbose messages should be printed. |
| 158 | /// |
| 159 | /// Outputs: |
| 160 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 161 | /// |
| 162 | /// Return Value: |
| 163 | /// TRUE - An error occurred. |
| 164 | /// FALSE - No errors. |
| 165 | /// |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 166 | bool llvm::LinkInArchive(Module *M, |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 167 | const std::string &Filename, |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 168 | std::string* ErrorMessage, |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 169 | bool Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 170 | { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 171 | // Find all of the symbols currently undefined in the bytecode program. |
| 172 | // If all the symbols are defined, the program is complete, and there is |
| 173 | // no reason to link in any archive files. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 174 | std::set<std::string> UndefinedSymbols; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 175 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 176 | if (UndefinedSymbols.empty()) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 177 | if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; |
| 178 | return false; // No need to link anything in! |
| 179 | } |
| 180 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 181 | // Open the archive file |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 182 | if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n"; |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 183 | Archive* arch = Archive::OpenAndLoadSymbols(sys::Path(Filename)); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 184 | |
| 185 | // While we are linking in object files, loop. |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 186 | while (true) { |
| 187 | std::set<ModuleProvider*> Modules; |
| 188 | // Find the modules we need to link |
| 189 | arch->findModulesDefiningSymbols(UndefinedSymbols,Modules); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 190 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 191 | // If we didn't find any more modules to link this time, we are done. |
| 192 | if (Modules.empty()) |
| 193 | break; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 194 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 195 | // Loop over all the ModuleProviders that we got back from the archive |
| 196 | for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end(); |
| 197 | I != E; ++I) { |
| 198 | // Get the module we must link in. |
| 199 | Module* aModule = (*I)->releaseModule(); |
| 200 | std::cout << "Linked: " << aModule->getModuleIdentifier() << "\n"; |
John Criswell | 1715ce0 | 2003-12-23 17:37:06 +0000 | [diff] [blame] | 201 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 202 | // Link it in |
| 203 | if (LinkModules(M, aModule, ErrorMessage)) { |
| 204 | // don't create a memory leak |
| 205 | delete aModule; |
| 206 | delete arch; |
| 207 | return true; // Couldn't link in the right object file... |
John Criswell | 1715ce0 | 2003-12-23 17:37:06 +0000 | [diff] [blame] | 208 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 209 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 210 | // Since we have linked in this object, throw it away now. |
| 211 | delete aModule; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 212 | } |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 213 | |
| 214 | // We have linked in a set of modules determined by the archive to satisfy |
| 215 | // our missing symbols. Linking in the new modules will have satisfied some |
| 216 | // symbols but may introduce additional missing symbols. We need to update |
| 217 | // the list of undefined symbols and try again until the archive doesn't |
| 218 | // have any modules that satisfy our symbols. |
| 219 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 225 | /// LinkInFile - opens a bytecode file and links in all objects which |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 226 | /// provide symbols that are currently undefined. |
| 227 | /// |
| 228 | /// Inputs: |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 229 | /// HeadModule - The module in which to link the bytecode file. |
| 230 | /// Filename - The pathname of the bytecode file. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 231 | /// Verbose - Flags whether verbose messages should be printed. |
| 232 | /// |
| 233 | /// Outputs: |
| 234 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 235 | /// |
| 236 | /// Return Value: |
| 237 | /// TRUE - An error occurred. |
| 238 | /// FALSE - No errors. |
| 239 | /// |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 240 | static bool LinkInFile(Module *HeadModule, |
| 241 | const std::string &Filename, |
| 242 | std::string &ErrorMessage, |
| 243 | bool Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 244 | { |
| 245 | std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage)); |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 246 | if (M.get() == 0) return true; |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 247 | bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage); |
| 248 | if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n"; |
| 249 | return Result; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 252 | /// LinkFiles - takes a module and a list of files and links them all together. |
| 253 | /// It locates the file either in the current directory, as its absolute |
| 254 | /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. |
| 255 | /// |
| 256 | /// Inputs: |
| 257 | /// progname - The name of the program (infamous argv[0]). |
| 258 | /// HeadModule - The module under which all files will be linked. |
| 259 | /// Files - A vector of C++ strings indicating the LLVM bytecode filenames |
| 260 | /// to be linked. The names can refer to a mixture of pure LLVM |
| 261 | /// bytecode files and archive (ar) formatted files. |
| 262 | /// Verbose - Flags whether verbose output should be printed while linking. |
| 263 | /// |
| 264 | /// Outputs: |
| 265 | /// HeadModule - The module will have the specified LLVM bytecode files linked |
| 266 | /// in. |
| 267 | /// |
| 268 | /// Return value: |
| 269 | /// FALSE - No errors. |
| 270 | /// TRUE - Some error occurred. |
| 271 | /// |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 272 | bool llvm::LinkFiles(const char *progname, Module *HeadModule, |
| 273 | const std::vector<std::string> &Files, bool Verbose) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 274 | // String in which to receive error messages. |
| 275 | std::string ErrorMessage; |
| 276 | |
| 277 | // Full pathname of the file |
| 278 | std::string Pathname; |
| 279 | |
| 280 | // Get the library search path from the environment |
| 281 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 282 | |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 283 | for (unsigned i = 0; i < Files.size(); ++i) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 284 | // Determine where this file lives. |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 285 | if (FileOpenable(Files[i])) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 286 | Pathname = Files[i]; |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 287 | } else { |
| 288 | if (SearchPath == NULL) { |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 289 | std::cerr << progname << ": Cannot find linker input file '" |
| 290 | << Files[i] << "'\n"; |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 291 | std::cerr << progname |
| 292 | << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| 296 | Pathname = std::string(SearchPath)+"/"+Files[i]; |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 297 | if (!FileOpenable(Pathname)) { |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 298 | std::cerr << progname << ": Cannot find linker input file '" |
| 299 | << Files[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | } |
| 303 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 304 | // A user may specify an ar archive without -l, perhaps because it |
| 305 | // is not installed as a library. Detect that and link the library. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 306 | if (IsArchive(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 307 | if (Verbose) |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 308 | std::cerr << "Trying to link archive '" << Pathname << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 309 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 310 | if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) { |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 311 | std::cerr << progname << ": Error linking in archive '" << Pathname |
| 312 | << "': " << ErrorMessage << "\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 313 | return true; |
| 314 | } |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 315 | } else if (IsBytecode(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 316 | if (Verbose) |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 317 | std::cerr << "Trying to link bytecode file '" << Pathname << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 318 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 319 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 320 | std::cerr << progname << ": Error linking in bytecode file '" |
| 321 | << Pathname << "': " << ErrorMessage << "\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 322 | return true; |
| 323 | } |
Misha Brukman | eda20f9 | 2004-11-08 22:03:10 +0000 | [diff] [blame] | 324 | } else { |
Misha Brukman | 669b524 | 2004-11-09 04:24:59 +0000 | [diff] [blame] | 325 | std::cerr << progname << ": Warning: invalid file `" << Pathname |
| 326 | << "' ignored.\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | return false; |
| 331 | } |
| 332 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 333 | /// LinkLibraries - takes the specified library files and links them into the |
| 334 | /// main bytecode object file. |
| 335 | /// |
| 336 | /// Inputs: |
| 337 | /// progname - The name of the program (infamous argv[0]). |
| 338 | /// HeadModule - The module into which all necessary libraries will be linked. |
| 339 | /// Libraries - The list of libraries to link into the module. |
| 340 | /// LibPaths - The list of library paths in which to find libraries. |
| 341 | /// Verbose - Flags whether verbose messages should be printed. |
| 342 | /// Native - Flags whether native code is being generated. |
| 343 | /// |
| 344 | /// Outputs: |
| 345 | /// HeadModule - The module will have all necessary libraries linked in. |
| 346 | /// |
| 347 | /// Return value: |
| 348 | /// FALSE - No error. |
| 349 | /// TRUE - Error. |
| 350 | /// |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 351 | void llvm::LinkLibraries(const char *progname, Module *HeadModule, |
| 352 | const std::vector<std::string> &Libraries, |
| 353 | const std::vector<std::string> &LibPaths, |
| 354 | bool Verbose, bool Native) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 355 | // String in which to receive error messages. |
| 356 | std::string ErrorMessage; |
| 357 | |
Brian Gaeke | f1fce08 | 2003-10-21 21:07:12 +0000 | [diff] [blame] | 358 | for (unsigned i = 0; i < Libraries.size(); ++i) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 359 | // Determine where this library lives. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 360 | std::string Pathname = FindLib(Libraries[i], LibPaths); |
| 361 | if (Pathname.empty()) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 362 | // If the pathname does not exist, then continue to the next one if |
| 363 | // we're doing a native link and give an error if we're doing a bytecode |
| 364 | // link. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 365 | if (!Native) { |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 366 | std::cerr << progname << ": WARNING: Cannot find library -l" |
| 367 | << Libraries[i] << "\n"; |
| 368 | continue; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 372 | // A user may specify an ar archive without -l, perhaps because it |
| 373 | // is not installed as a library. Detect that and link the library. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 374 | if (IsArchive(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 375 | if (Verbose) |
Brian Gaeke | 2282ae1 | 2003-11-16 23:07:13 +0000 | [diff] [blame] | 376 | std::cerr << "Trying to link archive '" << Pathname << "' (-l" |
| 377 | << Libraries[i] << ")\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 378 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame^] | 379 | if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) { |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 380 | std::cerr << progname << ": " << ErrorMessage |
| 381 | << ": Error linking in archive '" << Pathname << "' (-l" |
| 382 | << Libraries[i] << ")\n"; |
| 383 | exit(1); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 384 | } |
Brian Gaeke | ee8adb1 | 2003-11-11 18:27:37 +0000 | [diff] [blame] | 385 | } else if (IsBytecode(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 386 | if (Verbose) |
Brian Gaeke | 2282ae1 | 2003-11-16 23:07:13 +0000 | [diff] [blame] | 387 | std::cerr << "Trying to link bytecode file '" << Pathname |
| 388 | << "' (-l" << Libraries[i] << ")\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 389 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 390 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 391 | std::cerr << progname << ": " << ErrorMessage |
| 392 | << ": error linking in bytecode file '" << Pathname << "' (-l" |
| 393 | << Libraries[i] << ")\n"; |
| 394 | exit(1); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 398 | } |