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