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"
26 /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
27 /// name of each externally-visible symbol defined in M.
30 GetAllDefinedSymbols(Module
*M
, std::set
<std::string
> &DefinedSymbols
) {
31 for (Module::iterator I
= M
->begin(), E
= M
->end(); I
!= E
; ++I
)
32 if (I
->hasName() && !I
->isExternal() && !I
->hasInternalLinkage())
33 DefinedSymbols
.insert(I
->getName());
34 for (Module::global_iterator I
= M
->global_begin(), E
= M
->global_end();
36 if (I
->hasName() && !I
->isExternal() && !I
->hasInternalLinkage())
37 DefinedSymbols
.insert(I
->getName());
40 /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
41 /// exist in an LLVM module. This is a bit tricky because there may be two
42 /// symbols with the same name but different LLVM types that will be resolved to
43 /// each other but aren't currently (thus we need to treat it as resolved).
46 /// M - The module in which to find undefined symbols.
49 /// UndefinedSymbols - A set of C++ strings containing the name of all
50 /// undefined symbols.
53 GetAllUndefinedSymbols(Module
*M
, std::set
<std::string
> &UndefinedSymbols
) {
54 std::set
<std::string
> DefinedSymbols
;
55 UndefinedSymbols
.clear();
57 // If the program doesn't define a main, try pulling one in from a .a file.
58 // This is needed for programs where the main function is defined in an
59 // archive, such f2c'd programs.
60 Function
*Main
= M
->getMainFunction();
61 if (Main
== 0 || Main
->isExternal())
62 UndefinedSymbols
.insert("main");
64 for (Module::iterator I
= M
->begin(), E
= M
->end(); I
!= E
; ++I
)
67 UndefinedSymbols
.insert(I
->getName());
68 else if (!I
->hasInternalLinkage())
69 DefinedSymbols
.insert(I
->getName());
71 for (Module::global_iterator I
= M
->global_begin(), E
= M
->global_end();
75 UndefinedSymbols
.insert(I
->getName());
76 else if (!I
->hasInternalLinkage())
77 DefinedSymbols
.insert(I
->getName());
80 // Prune out any defined symbols from the undefined symbols set...
81 for (std::set
<std::string
>::iterator I
= UndefinedSymbols
.begin();
82 I
!= UndefinedSymbols
.end(); )
83 if (DefinedSymbols
.count(*I
))
84 UndefinedSymbols
.erase(I
++); // This symbol really is defined!
86 ++I
; // Keep this symbol in the undefined symbols list
89 /// LinkInArchive - opens an archive library and link in all objects which
90 /// provide symbols that are currently undefined.
93 /// Filename - The pathname of the archive.
96 /// TRUE - An error occurred.
97 /// FALSE - No errors.
99 Linker::LinkInArchive(const sys::Path
&Filename
) {
101 // Make sure this is an archive file we're dealing with
102 if (!Filename
.isArchive())
103 return error("File '" + Filename
.toString() + "' is not an archive.");
105 // Open the archive file
106 verbose("Linking archive file '" + Filename
.toString() + "'");
108 // Find all of the symbols currently undefined in the bytecode program.
109 // If all the symbols are defined, the program is complete, and there is
110 // no reason to link in any archive files.
111 std::set
<std::string
> UndefinedSymbols
;
112 GetAllUndefinedSymbols(Composite
, UndefinedSymbols
);
114 if (UndefinedSymbols
.empty()) {
115 verbose("No symbols undefined, skipping library '" +
116 Filename
.toString() + "'");
117 return false; // No need to link anything in!
121 std::auto_ptr
<Archive
> AutoArch (
122 Archive::OpenAndLoadSymbols(Filename
,&ErrMsg
));
124 Archive
* arch
= AutoArch
.get();
127 return error("Cannot read archive '" + Filename
.toString() +
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
;
135 // While we are linking in object files, loop.
138 // Find the modules we need to link into the target module
139 std::set
<ModuleProvider
*> Modules
;
140 arch
->findModulesDefiningSymbols(UndefinedSymbols
, Modules
);
142 // If we didn't find any more modules to link this time, we are done
143 // searching this archive.
147 // Any symbols remaining in UndefinedSymbols after
148 // findModulesDefiningSymbols are ones that the archive does not define. So
149 // we add them to the NotDefinedByArchive variable now.
150 NotDefinedByArchive
.insert(UndefinedSymbols
.begin(),
151 UndefinedSymbols
.end());
153 // Loop over all the ModuleProviders that we got back from the archive
154 for (std::set
<ModuleProvider
*>::iterator I
=Modules
.begin(), E
=Modules
.end();
157 // Get the module we must link in.
158 std::auto_ptr
<Module
> AutoModule( (*I
)->releaseModule() );
159 Module
* aModule
= AutoModule
.get();
161 verbose(" Linking in module: " + aModule
->getModuleIdentifier());
164 if (LinkInModule(aModule
))
165 return error("Cannot link in module '" +
166 aModule
->getModuleIdentifier() + "': " + Error
);
169 // Get the undefined symbols from the aggregate module. This recomputes the
170 // symbols we still need after the new modules have been linked in.
171 GetAllUndefinedSymbols(Composite
, UndefinedSymbols
);
173 // At this point we have two sets of undefined symbols: UndefinedSymbols
174 // which holds the undefined symbols from all the modules, and
175 // NotDefinedByArchive which holds symbols we know the archive doesn't
176 // define. There's no point searching for symbols that we won't find in the
177 // archive so we subtract these sets.
178 set_subtract(UndefinedSymbols
, NotDefinedByArchive
);
180 // If there's no symbols left, no point in continuing to search the
182 if (UndefinedSymbols
.empty())