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