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 | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 22 | //#include "llvm/Support/SystemUtils.h" |
| 23 | //#include <algorithm> |
| 24 | //#include <memory> |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 25 | #include <set> |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 26 | #include <iostream> |
| 27 | |
Chris Lattner | 6cc8ca9 | 2003-11-28 07:44:09 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Brian Gaeke | 3b3640a | 2003-11-05 22:12:52 +0000 | [diff] [blame] | 30 | /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the |
| 31 | /// name of each externally-visible symbol defined in M. |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 32 | /// |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 33 | static void |
| 34 | GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 35 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 36 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 37 | DefinedSymbols.insert(I->getName()); |
| 38 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 39 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 40 | DefinedSymbols.insert(I->getName()); |
| 41 | } |
| 42 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 43 | /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still |
| 44 | /// exist in an LLVM module. This is a bit tricky because there may be two |
| 45 | /// symbols with the same name but different LLVM types that will be resolved to |
| 46 | /// each other but aren't currently (thus we need to treat it as resolved). |
| 47 | /// |
| 48 | /// Inputs: |
| 49 | /// M - The module in which to find undefined symbols. |
| 50 | /// |
| 51 | /// Outputs: |
| 52 | /// UndefinedSymbols - A set of C++ strings containing the name of all |
| 53 | /// undefined symbols. |
| 54 | /// |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 55 | static void |
| 56 | GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 57 | std::set<std::string> DefinedSymbols; |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 58 | UndefinedSymbols.clear(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 59 | |
| 60 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 61 | if (I->hasName()) { |
| 62 | if (I->isExternal()) |
| 63 | UndefinedSymbols.insert(I->getName()); |
| 64 | else if (!I->hasInternalLinkage()) |
| 65 | DefinedSymbols.insert(I->getName()); |
| 66 | } |
| 67 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 68 | if (I->hasName()) { |
| 69 | if (I->isExternal()) |
| 70 | UndefinedSymbols.insert(I->getName()); |
| 71 | else if (!I->hasInternalLinkage()) |
| 72 | DefinedSymbols.insert(I->getName()); |
| 73 | } |
| 74 | |
| 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. |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 93 | bool |
| 94 | Linker::LinkInArchive(const sys::Path &Filename) { |
| 95 | |
| 96 | // Make sure this is an archive file we're dealing with |
| 97 | if (!Filename.isArchive()) |
| 98 | return error("File '" + Filename.toString() + "' is not an archive."); |
| 99 | |
| 100 | // Open the archive file |
| 101 | verbose("Linking archive file '" + Filename.toString() + "'"); |
| 102 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 103 | // Find all of the symbols currently undefined in the bytecode program. |
| 104 | // If all the symbols are defined, the program is complete, and there is |
| 105 | // no reason to link in any archive files. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 106 | std::set<std::string> UndefinedSymbols; |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 107 | GetAllUndefinedSymbols(Composite, UndefinedSymbols); |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 108 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 109 | if (UndefinedSymbols.empty()) { |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 110 | verbose("No symbols undefined, skipping library '" + |
| 111 | Filename.toString() + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 112 | return false; // No need to link anything in! |
| 113 | } |
| 114 | |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 115 | std::string ErrMsg; |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 116 | std::auto_ptr<Archive> AutoArch ( |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 117 | Archive::OpenAndLoadSymbols(Filename,&ErrMsg)); |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 118 | |
| 119 | Archive* arch = AutoArch.get(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 120 | |
Reid Spencer | 7dde0e3 | 2004-12-13 02:59:29 +0000 | [diff] [blame^] | 121 | if (!arch) |
| 122 | return error("Cannot read archive '" + Filename.toString() + |
| 123 | "': " + ErrMsg); |
| 124 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 125 | // Save a set of symbols that are not defined by the archive. Since we're |
| 126 | // entering a loop, there's no point searching for these multiple times. This |
| 127 | // variable is used to "set_subtract" from the set of undefined symbols. |
| 128 | std::set<std::string> NotDefinedByArchive; |
| 129 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 130 | // While we are linking in object files, loop. |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 131 | while (true) { |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 132 | |
| 133 | // Find the modules we need to link into the target module |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 134 | std::set<ModuleProvider*> Modules; |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 135 | arch->findModulesDefiningSymbols(UndefinedSymbols, Modules); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 136 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 137 | // If we didn't find any more modules to link this time, we are done |
| 138 | // searching this archive. |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 139 | if (Modules.empty()) |
| 140 | break; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 141 | |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 142 | // Any symbols remaining in UndefinedSymbols after |
| 143 | // findModulesDefiningSymbols are ones that the archive does not define. So |
| 144 | // we add them to the NotDefinedByArchive variable now. |
| 145 | NotDefinedByArchive.insert(UndefinedSymbols.begin(), |
Reid Spencer | b9371ce | 2004-11-19 03:27:05 +0000 | [diff] [blame] | 146 | UndefinedSymbols.end()); |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 147 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 148 | // Loop over all the ModuleProviders that we got back from the archive |
| 149 | for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end(); |
| 150 | I != E; ++I) { |
Reid Spencer | 3cf2c32 | 2004-11-19 03:13:25 +0000 | [diff] [blame] | 151 | |
Reid Spencer | 8bbb17a | 2004-11-14 22:02:27 +0000 | [diff] [blame] | 152 | // Get the module we must link in. |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 153 | std::auto_ptr<Module> AutoModule( (*I)->releaseModule() ); |
Reid Spencer | 99d3604 | 2004-11-16 06:47:41 +0000 | [diff] [blame] | 154 | Module* aModule = AutoModule.get(); |
| 155 | |
| 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 | } |