blob: 76d81c219426b0932ac10c191f6793638560e925 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains routines to handle linking together LLVM bitcode 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/Bitcode/Archive.h"
20#include "llvm/Config/config.h"
21#include <memory>
22#include <set>
23using namespace llvm;
24
25/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
26/// exist in an LLVM module. This is a bit tricky because there may be two
27/// symbols with the same name but different LLVM types that will be resolved to
28/// each other but aren't currently (thus we need to treat it as resolved).
29///
30/// Inputs:
31/// M - The module in which to find undefined symbols.
32///
33/// Outputs:
34/// UndefinedSymbols - A set of C++ strings containing the name of all
35/// undefined symbols.
36///
37static void
38GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
39 std::set<std::string> DefinedSymbols;
40 UndefinedSymbols.clear();
41
42 // If the program doesn't define a main, try pulling one in from a .a file.
43 // This is needed for programs where the main function is defined in an
44 // archive, such f2c'd programs.
45 Function *Main = M->getFunction("main");
46 if (Main == 0 || Main->isDeclaration())
47 UndefinedSymbols.insert("main");
48
49 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
50 if (I->hasName()) {
51 if (I->isDeclaration())
52 UndefinedSymbols.insert(I->getName());
Rafael Espindolaa168fc92009-01-15 20:18:42 +000053 else if (!I->hasLocalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 assert(!I->hasDLLImportLinkage()
55 && "Found dllimported non-external symbol!");
56 DefinedSymbols.insert(I->getName());
57 }
58 }
Anton Korobeynikov555f0712008-03-04 20:16:11 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
61 I != E; ++I)
62 if (I->hasName()) {
63 if (I->isDeclaration())
64 UndefinedSymbols.insert(I->getName());
Rafael Espindolaa168fc92009-01-15 20:18:42 +000065 else if (!I->hasLocalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 assert(!I->hasDLLImportLinkage()
67 && "Found dllimported non-external symbol!");
68 DefinedSymbols.insert(I->getName());
69 }
70 }
71
Anton Korobeynikov555f0712008-03-04 20:16:11 +000072 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
73 I != E; ++I)
Anton Korobeynikov574c0f82008-03-11 00:24:53 +000074 if (I->hasName())
75 DefinedSymbols.insert(I->getName());
Anton Korobeynikov555f0712008-03-04 20:16:11 +000076
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 // Prune out any defined symbols from the undefined symbols set...
78 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
79 I != UndefinedSymbols.end(); )
80 if (DefinedSymbols.count(*I))
81 UndefinedSymbols.erase(I++); // This symbol really is defined!
82 else
83 ++I; // Keep this symbol in the undefined symbols list
84}
85
86/// LinkInArchive - opens an archive library and link in all objects which
87/// provide symbols that are currently undefined.
88///
89/// Inputs:
90/// Filename - The pathname of the archive.
91///
92/// Return Value:
93/// TRUE - An error occurred.
94/// FALSE - No errors.
95bool
96Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Make sure this is an archive file we're dealing with
98 if (!Filename.isArchive())
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000099 return error("File '" + Filename.str() + "' is not an archive.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 // Open the archive file
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000102 verbose("Linking archive file '" + Filename.str() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
104 // Find all of the symbols currently undefined in the bitcode program.
105 // If all the symbols are defined, the program is complete, and there is
106 // no reason to link in any archive files.
107 std::set<std::string> UndefinedSymbols;
108 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
109
110 if (UndefinedSymbols.empty()) {
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000111 verbose("No symbols undefined, skipping library '" + Filename.str() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return false; // No need to link anything in!
113 }
114
115 std::string ErrMsg;
116 std::auto_ptr<Archive> AutoArch (
Owen Anderson25209b42009-07-01 16:58:40 +0000117 Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118
119 Archive* arch = AutoArch.get();
120
121 if (!arch)
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000122 return error("Cannot read archive '" + Filename.str() +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 "': " + ErrMsg);
124 if (!arch->isBitcodeArchive()) {
125 is_native = true;
126 return false;
127 }
128 is_native = false;
129
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
135 // Save the current set of undefined symbols, because we may have to make
136 // multiple passes over the archive:
137 std::set<std::string> CurrentlyUndefinedSymbols;
138
139 do {
140 CurrentlyUndefinedSymbols = UndefinedSymbols;
141
142 // Find the modules we need to link into the target module
143 std::set<ModuleProvider*> Modules;
144 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000145 return error("Cannot find symbols in '" + Filename.str() +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 "': " + ErrMsg);
147
148 // If we didn't find any more modules to link this time, we are done
149 // searching this archive.
150 if (Modules.empty())
151 break;
152
153 // Any symbols remaining in UndefinedSymbols after
154 // findModulesDefiningSymbols are ones that the archive does not define. So
155 // we add them to the NotDefinedByArchive variable now.
156 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
157 UndefinedSymbols.end());
158
159 // Loop over all the ModuleProviders that we got back from the archive
160 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
161 I != E; ++I) {
162
163 // Get the module we must link in.
164 std::string moduleErrorMsg;
165 std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg ));
Reid Spencer0a21de62007-07-22 21:39:37 +0000166 if (!moduleErrorMsg.empty())
167 return error("Could not load a module: " + moduleErrorMsg);
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 Module* aModule = AutoModule.get();
170
171 if (aModule != NULL) {
172 verbose(" Linking in module: " + aModule->getModuleIdentifier());
173
174 // Link it in
175 if (LinkInModule(aModule, &moduleErrorMsg)) {
176 return error("Cannot link in module '" +
177 aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
178 }
179 }
180 }
181
182 // Get the undefined symbols from the aggregate module. This recomputes the
183 // symbols we still need after the new modules have been linked in.
184 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
185
186 // At this point we have two sets of undefined symbols: UndefinedSymbols
187 // which holds the undefined symbols from all the modules, and
188 // NotDefinedByArchive which holds symbols we know the archive doesn't
189 // define. There's no point searching for symbols that we won't find in the
190 // archive so we subtract these sets.
191 set_subtract(UndefinedSymbols, NotDefinedByArchive);
192
193 // If there's no symbols left, no point in continuing to search the
194 // archive.
195 if (UndefinedSymbols.empty())
196 break;
197 } while (CurrentlyUndefinedSymbols != UndefinedSymbols);
198
199 return false;
200}