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