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