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