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