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