1 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains routines to handle linking together LLVM bytecode files,
11 // and to handle annoying things like static libraries.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Linker.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/ADT/SetOperations.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "llvm/Bytecode/Archive.h"
21 #include "llvm/Config/config.h"
22 //#include "llvm/Support/SystemUtils.h"
23 //#include <algorithm>
30 /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
31 /// name of each externally-visible symbol defined in M.
34 GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
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());
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).
49 /// M - The module in which to find undefined symbols.
52 /// UndefinedSymbols - A set of C++ strings containing the name of all
53 /// undefined symbols.
56 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
57 std::set<std::string> DefinedSymbols;
58 UndefinedSymbols.clear();
60 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
63 UndefinedSymbols.insert(I->getName());
64 else if (!I->hasInternalLinkage())
65 DefinedSymbols.insert(I->getName());
67 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
70 UndefinedSymbols.insert(I->getName());
71 else if (!I->hasInternalLinkage())
72 DefinedSymbols.insert(I->getName());
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!
81 ++I; // Keep this symbol in the undefined symbols list
84 /// LinkInArchive - opens an archive library and link in all objects which
85 /// provide symbols that are currently undefined.
88 /// Filename - The pathname of the archive.
91 /// TRUE - An error occurred.
92 /// FALSE - No errors.
94 Linker::LinkInArchive(const sys::Path &Filename) {
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.");
100 // Open the archive file
101 verbose("Linking archive file '" + Filename.toString() + "'");
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.
106 std::set<std::string> UndefinedSymbols;
107 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
109 if (UndefinedSymbols.empty()) {
110 verbose("No symbols undefined, skipping library '" +
111 Filename.toString() + "'");
112 return false; // No need to link anything in!
116 std::auto_ptr<Archive> AutoArch (
117 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
119 Archive* arch = AutoArch.get();
122 return error("Cannot read archive '" + Filename.toString() +
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;
130 // While we are linking in object files, loop.
133 // Find the modules we need to link into the target module
134 std::set<ModuleProvider*> Modules;
135 arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
137 // If we didn't find any more modules to link this time, we are done
138 // searching this archive.
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(),
146 UndefinedSymbols.end());
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();
152 // Get the module we must link in.
153 std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
154 Module* aModule = AutoModule.get();
157 if (this->LinkInModule(aModule))
158 return error("Cannot link in module '" +
159 aModule->getModuleIdentifier() + "': " + Error);
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.
164 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
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);
174 // If there's no symbols left, no point in continuing to search the
176 if (UndefinedSymbols.empty())