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