Fix header and function comments.
[oota-llvm.git] / lib / Linker / LinkArchives.cpp
1 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This file contains routines to handle linking together LLVM bytecode files,
11 // and to handle annoying things like static libraries.
12 //
13 //===----------------------------------------------------------------------===//
14
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>
24 //#include <memory>
25 #include <set>
26 #include <iostream>
27
28 using namespace llvm;
29
30 /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
31 /// name of each externally-visible symbol defined in M.
32 ///
33 static void 
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());
41 }
42
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 ///
55 static void
56 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
57   std::set<std::string> DefinedSymbols;
58   UndefinedSymbols.clear();
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
84 /// LinkInArchive - opens an archive library and link in all objects which
85 /// provide symbols that are currently undefined.
86 ///
87 /// Inputs:
88 ///  Filename - The pathname of the archive.
89 ///
90 /// Return Value:
91 ///  TRUE  - An error occurred.
92 ///  FALSE - No errors.
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
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);
108   
109   if (UndefinedSymbols.empty()) {
110     verbose("No symbols undefined, skipping library '" + 
111             Filename.toString() + "'");
112     return false;  // No need to link anything in!
113   }
114
115   std::string ErrMsg;
116   std::auto_ptr<Archive> AutoArch (
117     Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
118
119   Archive* arch = AutoArch.get();
120
121   if (!arch)
122     return error("Cannot read archive '" + Filename.toString() + 
123                  "': " + ErrMsg);
124
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
130   // While we are linking in object files, loop.
131   while (true) {     
132
133     // Find the modules we need to link into the target module
134     std::set<ModuleProvider*> Modules;
135     arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
136
137     // If we didn't find any more modules to link this time, we are done 
138     // searching this archive.
139     if (Modules.empty())
140       break;
141
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());
147
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) {
151
152       // Get the module we must link in.
153       std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
154       Module* aModule = AutoModule.get();
155
156       // Link it in
157       if (this->LinkInModule(aModule))
158         return error("Cannot link in module '" + 
159                      aModule->getModuleIdentifier() + "': " + Error);
160     }
161
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);
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;
178   }
179   
180   return false;
181 }