Reid Spencer | c408c45 | 2004-11-12 20:34:32 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 10 | // This file contains routines to handle linking together LLVM bitcode files, |
Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 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" |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SetOperations.h" |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/Archive.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Config/config.h" |
Reid Spencer | 4ca8e71 | 2004-12-20 04:15:44 +0000 | [diff] [blame] | 21 | #include <memory> |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 22 | #include <set> |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 25 | /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still |
| 26 | /// exist in an LLVM module. This is a bit tricky because there may be two |
| 27 | /// symbols with the same name but different LLVM types that will be resolved to |
| 28 | /// each other but aren't currently (thus we need to treat it as resolved). |
| 29 | /// |
| 30 | /// Inputs: |
| 31 | /// M - The module in which to find undefined symbols. |
| 32 | /// |
| 33 | /// Outputs: |
| 34 | /// UndefinedSymbols - A set of C++ strings containing the name of all |
| 35 | /// undefined symbols. |
| 36 | /// |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 37 | static void |
| 38 | GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 39 | std::set<std::string> DefinedSymbols; |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 40 | UndefinedSymbols.clear(); |
Chris Lattner | c72e573 | 2005-03-15 23:03:34 +0000 | [diff] [blame] | 41 | |
| 42 | // If the program doesn't define a main, try pulling one in from a .a file. |
| 43 | // This is needed for programs where the main function is defined in an |
| 44 | // archive, such f2c'd programs. |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 45 | Function *Main = M->getFunction("main"); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 46 | if (Main == 0 || Main->isDeclaration()) |
Chris Lattner | c72e573 | 2005-03-15 23:03:34 +0000 | [diff] [blame] | 47 | UndefinedSymbols.insert("main"); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 48 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 49 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 50 | if (I->hasName()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 51 | if (I->isDeclaration()) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 52 | UndefinedSymbols.insert(I->getName()); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 53 | else if (!I->hasInternalLinkage()) { |
| 54 | assert(!I->hasDLLImportLinkage() |
| 55 | && "Found dllimported non-external symbol!"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 56 | DefinedSymbols.insert(I->getName()); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 57 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 58 | } |
Anton Korobeynikov | eba0415 | 2008-03-04 20:16:11 +0000 | [diff] [blame^] | 59 | |
Chris Lattner | c72e573 | 2005-03-15 23:03:34 +0000 | [diff] [blame] | 60 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 61 | I != E; ++I) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 62 | if (I->hasName()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 63 | if (I->isDeclaration()) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 64 | UndefinedSymbols.insert(I->getName()); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 65 | else if (!I->hasInternalLinkage()) { |
| 66 | assert(!I->hasDLLImportLinkage() |
| 67 | && "Found dllimported non-external symbol!"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 68 | DefinedSymbols.insert(I->getName()); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 69 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 70 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 71 | |
Anton Korobeynikov | eba0415 | 2008-03-04 20:16:11 +0000 | [diff] [blame^] | 72 | for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 73 | I != E; ++I) |
| 74 | if (I->hasName()) { |
| 75 | const GlobalValue *Aliased = I->getAliasedGlobal(); |
| 76 | if (Aliased->isDeclaration()) |
| 77 | UndefinedSymbols.insert(I->getName()); |
| 78 | else |
| 79 | DefinedSymbols.insert(I->getName()); |
| 80 | } |
| 81 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 82 | // Prune out any defined symbols from the undefined symbols set... |
| 83 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 84 | I != UndefinedSymbols.end(); ) |
| 85 | if (DefinedSymbols.count(*I)) |
| 86 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 87 | else |
| 88 | ++I; // Keep this symbol in the undefined symbols list |
| 89 | } |
| 90 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 91 | /// LinkInArchive - opens an archive library and link in all objects which |
| 92 | /// provide symbols that are currently undefined. |
| 93 | /// |
| 94 | /// Inputs: |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 95 | /// Filename - The pathname of the archive. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 96 | /// |
| 97 | /// Return Value: |
| 98 | /// TRUE - An error occurred. |
| 99 | /// FALSE - No errors. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 100 | bool |
Reid Spencer | c9a83e4 | 2007-04-30 00:29:39 +0000 | [diff] [blame] | 101 | Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) { |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 102 | // Make sure this is an archive file we're dealing with |
| 103 | if (!Filename.isArchive()) |
| 104 | return error("File '" + Filename.toString() + "' is not an archive."); |
| 105 | |
| 106 | // Open the archive file |
| 107 | verbose("Linking archive file '" + Filename.toString() + "'"); |
| 108 | |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 109 | // Find all of the symbols currently undefined in the bitcode program. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 110 | // If all the symbols are defined, the program is complete, and there is |
| 111 | // no reason to link in any archive files. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 112 | std::set<std::string> UndefinedSymbols; |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 113 | GetAllUndefinedSymbols(Composite, UndefinedSymbols); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 114 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 115 | if (UndefinedSymbols.empty()) { |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 116 | verbose("No symbols undefined, skipping library '" + |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 117 | Filename.toString() + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 118 | return false; // No need to link anything in! |
| 119 | } |
| 120 | |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 121 | std::string ErrMsg; |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 122 | std::auto_ptr<Archive> AutoArch ( |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 123 | Archive::OpenAndLoadSymbols(Filename,&ErrMsg)); |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 124 | |
| 125 | Archive* arch = AutoArch.get(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 126 | |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 127 | if (!arch) |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 128 | return error("Cannot read archive '" + Filename.toString() + |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 129 | "': " + ErrMsg); |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 130 | if (!arch->isBitcodeArchive()) { |
Reid Spencer | c9a83e4 | 2007-04-30 00:29:39 +0000 | [diff] [blame] | 131 | is_native = true; |
| 132 | return false; |
| 133 | } |
| 134 | is_native = false; |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 135 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 136 | // Save a set of symbols that are not defined by the archive. Since we're |
| 137 | // entering a loop, there's no point searching for these multiple times. This |
| 138 | // variable is used to "set_subtract" from the set of undefined symbols. |
| 139 | std::set<std::string> NotDefinedByArchive; |
| 140 | |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 141 | // Save the current set of undefined symbols, because we may have to make |
| 142 | // multiple passes over the archive: |
| 143 | std::set<std::string> CurrentlyUndefinedSymbols; |
| 144 | |
| 145 | do { |
| 146 | CurrentlyUndefinedSymbols = UndefinedSymbols; |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 147 | |
| 148 | // Find the modules we need to link into the target module |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 149 | std::set<ModuleProvider*> Modules; |
Reid Spencer | 8d8a7ff | 2006-07-07 20:56:50 +0000 | [diff] [blame] | 150 | if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg)) |
| 151 | return error("Cannot find symbols in '" + Filename.toString() + |
| 152 | "': " + ErrMsg); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 153 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 154 | // If we didn't find any more modules to link this time, we are done |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 155 | // searching this archive. |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 156 | if (Modules.empty()) |
| 157 | break; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 158 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 159 | // Any symbols remaining in UndefinedSymbols after |
| 160 | // findModulesDefiningSymbols are ones that the archive does not define. So |
| 161 | // we add them to the NotDefinedByArchive variable now. |
| 162 | NotDefinedByArchive.insert(UndefinedSymbols.begin(), |
Reid Spencer | b9371ce | 2004-11-19 03:27:05 +0000 | [diff] [blame] | 163 | UndefinedSymbols.end()); |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 164 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 165 | // Loop over all the ModuleProviders that we got back from the archive |
| 166 | for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end(); |
| 167 | I != E; ++I) { |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 168 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 169 | // Get the module we must link in. |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 170 | std::string moduleErrorMsg; |
| 171 | std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg )); |
Reid Spencer | 858dd54 | 2007-07-22 21:39:37 +0000 | [diff] [blame] | 172 | if (!moduleErrorMsg.empty()) |
| 173 | return error("Could not load a module: " + moduleErrorMsg); |
| 174 | |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 175 | Module* aModule = AutoModule.get(); |
| 176 | |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 177 | if (aModule != NULL) { |
| 178 | verbose(" Linking in module: " + aModule->getModuleIdentifier()); |
Chris Lattner | 50c301b | 2005-02-13 15:26:14 +0000 | [diff] [blame] | 179 | |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 180 | // Link it in |
| 181 | if (LinkInModule(aModule, &moduleErrorMsg)) { |
| 182 | return error("Cannot link in module '" + |
| 183 | aModule->getModuleIdentifier() + "': " + moduleErrorMsg); |
| 184 | } |
Reid Spencer | 0288d18 | 2006-11-11 20:27:49 +0000 | [diff] [blame] | 185 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 186 | } |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 187 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 188 | // Get the undefined symbols from the aggregate module. This recomputes the |
| 189 | // symbols we still need after the new modules have been linked in. |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame] | 190 | GetAllUndefinedSymbols(Composite, UndefinedSymbols); |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 191 | |
| 192 | // At this point we have two sets of undefined symbols: UndefinedSymbols |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 193 | // which holds the undefined symbols from all the modules, and |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 194 | // NotDefinedByArchive which holds symbols we know the archive doesn't |
| 195 | // define. There's no point searching for symbols that we won't find in the |
| 196 | // archive so we subtract these sets. |
Chris Lattner | 12945ac | 2005-02-13 17:50:16 +0000 | [diff] [blame] | 197 | set_subtract(UndefinedSymbols, NotDefinedByArchive); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 199 | // If there's no symbols left, no point in continuing to search the |
| 200 | // archive. |
| 201 | if (UndefinedSymbols.empty()) |
| 202 | break; |
Reid Spencer | 4952143 | 2006-11-11 11:54:25 +0000 | [diff] [blame] | 203 | } while (CurrentlyUndefinedSymbols != UndefinedSymbols); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 204 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 205 | return false; |
| 206 | } |