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