1 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains routines to handle linking together LLVM bitcode 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/Bitcode/Archive.h"
20 #include "llvm/Config/config.h"
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).
31 /// M - The module in which to find undefined symbols.
34 /// UndefinedSymbols - A set of C++ strings containing the name of all
35 /// undefined symbols.
38 GetAllUndefinedSymbols(Module
*M
, std::set
<std::string
> &UndefinedSymbols
) {
39 std::set
<std::string
> DefinedSymbols
;
40 UndefinedSymbols
.clear();
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");
49 for (Module::iterator I
= M
->begin(), E
= M
->end(); I
!= E
; ++I
)
51 if (I
->isDeclaration())
52 UndefinedSymbols
.insert(I
->getName());
53 else if (!I
->hasLocalLinkage()) {
54 assert(!I
->hasDLLImportLinkage()
55 && "Found dllimported non-external symbol!");
56 DefinedSymbols
.insert(I
->getName());
60 for (Module::global_iterator I
= M
->global_begin(), E
= M
->global_end();
63 if (I
->isDeclaration())
64 UndefinedSymbols
.insert(I
->getName());
65 else if (!I
->hasLocalLinkage()) {
66 assert(!I
->hasDLLImportLinkage()
67 && "Found dllimported non-external symbol!");
68 DefinedSymbols
.insert(I
->getName());
72 for (Module::alias_iterator I
= M
->alias_begin(), E
= M
->alias_end();
75 DefinedSymbols
.insert(I
->getName());
77 // 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!
83 ++I
; // Keep this symbol in the undefined symbols list
86 /// LinkInArchive - opens an archive library and link in all objects which
87 /// provide symbols that are currently undefined.
90 /// Filename - The pathname of the archive.
93 /// TRUE - An error occurred.
94 /// FALSE - No errors.
96 Linker::LinkInArchive(const sys::Path
&Filename
, bool &is_native
) {
97 // Make sure this is an archive file we're dealing with
98 if (!Filename
.isArchive())
99 return error("File '" + Filename
.toString() + "' is not an archive.");
101 // Open the archive file
102 verbose("Linking archive file '" + Filename
.toString() + "'");
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
);
110 if (UndefinedSymbols
.empty()) {
111 verbose("No symbols undefined, skipping library '" +
112 Filename
.toString() + "'");
113 return false; // No need to link anything in!
117 std::auto_ptr
<Archive
> AutoArch (
118 Archive::OpenAndLoadSymbols(Filename
,&ErrMsg
));
120 Archive
* arch
= AutoArch
.get();
123 return error("Cannot read archive '" + Filename
.toString() +
125 if (!arch
->isBitcodeArchive()) {
131 // Save a set of symbols that are not defined by the archive. Since we're
132 // entering a loop, there's no point searching for these multiple times. This
133 // variable is used to "set_subtract" from the set of undefined symbols.
134 std::set
<std::string
> NotDefinedByArchive
;
136 // Save the current set of undefined symbols, because we may have to make
137 // multiple passes over the archive:
138 std::set
<std::string
> CurrentlyUndefinedSymbols
;
141 CurrentlyUndefinedSymbols
= UndefinedSymbols
;
143 // Find the modules we need to link into the target module
144 std::set
<ModuleProvider
*> Modules
;
145 if (!arch
->findModulesDefiningSymbols(UndefinedSymbols
, Modules
, &ErrMsg
))
146 return error("Cannot find symbols in '" + Filename
.toString() +
149 // If we didn't find any more modules to link this time, we are done
150 // searching this archive.
154 // Any symbols remaining in UndefinedSymbols after
155 // findModulesDefiningSymbols are ones that the archive does not define. So
156 // we add them to the NotDefinedByArchive variable now.
157 NotDefinedByArchive
.insert(UndefinedSymbols
.begin(),
158 UndefinedSymbols
.end());
160 // Loop over all the ModuleProviders that we got back from the archive
161 for (std::set
<ModuleProvider
*>::iterator I
=Modules
.begin(), E
=Modules
.end();
164 // Get the module we must link in.
165 std::string moduleErrorMsg
;
166 std::auto_ptr
<Module
> AutoModule((*I
)->releaseModule( &moduleErrorMsg
));
167 if (!moduleErrorMsg
.empty())
168 return error("Could not load a module: " + moduleErrorMsg
);
170 Module
* aModule
= AutoModule
.get();
172 if (aModule
!= NULL
) {
173 verbose(" Linking in module: " + aModule
->getModuleIdentifier());
176 if (LinkInModule(aModule
, &moduleErrorMsg
)) {
177 return error("Cannot link in module '" +
178 aModule
->getModuleIdentifier() + "': " + moduleErrorMsg
);
183 // Get the undefined symbols from the aggregate module. This recomputes the
184 // symbols we still need after the new modules have been linked in.
185 GetAllUndefinedSymbols(Composite
, UndefinedSymbols
);
187 // At this point we have two sets of undefined symbols: UndefinedSymbols
188 // which holds the undefined symbols from all the modules, and
189 // NotDefinedByArchive which holds symbols we know the archive doesn't
190 // define. There's no point searching for symbols that we won't find in the
191 // archive so we subtract these sets.
192 set_subtract(UndefinedSymbols
, NotDefinedByArchive
);
194 // If there's no symbols left, no point in continuing to search the
196 if (UndefinedSymbols
.empty())
198 } while (CurrentlyUndefinedSymbols
!= UndefinedSymbols
);