blob: 974ad26e372922871a03a16e3dbaccded65abbe5 [file] [log] [blame]
Reid Spencer1cfa8d62004-11-12 20:34:32 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer1cfa8d62004-11-12 20:34:32 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer1cfa8d62004-11-12 20:34:32 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner41528e62007-05-06 09:29:13 +000010// This file contains routines to handle linking together LLVM bitcode files,
Reid Spencer1cfa8d62004-11-12 20:34:32 +000011// and to handle annoying things like static libraries.
12//
13//===----------------------------------------------------------------------===//
14
Reid Spencer9db61d92004-11-14 22:02:27 +000015#include "llvm/Linker.h"
Reid Spencer1cfa8d62004-11-12 20:34:32 +000016#include "llvm/Module.h"
Reid Spencer9db61d92004-11-14 22:02:27 +000017#include "llvm/ModuleProvider.h"
Reid Spencerc2455ca2004-11-19 03:13:25 +000018#include "llvm/ADT/SetOperations.h"
Chris Lattner41528e62007-05-06 09:29:13 +000019#include "llvm/Bitcode/Archive.h"
Reid Spencer1cfa8d62004-11-12 20:34:32 +000020#include "llvm/Config/config.h"
Reid Spencer6b853b22004-12-20 04:15:44 +000021#include <memory>
Reid Spencer1cfa8d62004-11-12 20:34:32 +000022#include <set>
23using namespace llvm;
24
Reid Spencer1cfa8d62004-11-12 20:34:32 +000025/// 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///
Reid Spencerfe73bfc2004-12-13 02:59:29 +000037static void
38GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
Reid Spencer1cfa8d62004-11-12 20:34:32 +000039 std::set<std::string> DefinedSymbols;
Reid Spencerc2455ca2004-11-19 03:13:25 +000040 UndefinedSymbols.clear();
Chris Lattner018b0932005-03-15 23:03:34 +000041
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.
Reid Spencer1241d6d2007-02-05 21:19:13 +000045 Function *Main = M->getFunction("main");
Reid Spencer5301e7c2007-01-30 20:08:39 +000046 if (Main == 0 || Main->isDeclaration())
Chris Lattner018b0932005-03-15 23:03:34 +000047 UndefinedSymbols.insert("main");
Misha Brukman10468d82005-04-21 22:55:34 +000048
Reid Spencer1cfa8d62004-11-12 20:34:32 +000049 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
50 if (I->hasName()) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000051 if (I->isDeclaration())
Reid Spencer1cfa8d62004-11-12 20:34:32 +000052 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000053 else if (!I->hasInternalLinkage()) {
54 assert(!I->hasDLLImportLinkage()
55 && "Found dllimported non-external symbol!");
Reid Spencer1cfa8d62004-11-12 20:34:32 +000056 DefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000057 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +000058 }
Chris Lattner018b0932005-03-15 23:03:34 +000059 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
60 I != E; ++I)
Reid Spencer1cfa8d62004-11-12 20:34:32 +000061 if (I->hasName()) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000062 if (I->isDeclaration())
Reid Spencer1cfa8d62004-11-12 20:34:32 +000063 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000064 else if (!I->hasInternalLinkage()) {
65 assert(!I->hasDLLImportLinkage()
66 && "Found dllimported non-external symbol!");
Reid Spencer1cfa8d62004-11-12 20:34:32 +000067 DefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000068 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +000069 }
Misha Brukman10468d82005-04-21 22:55:34 +000070
Reid Spencer1cfa8d62004-11-12 20:34:32 +000071 // Prune out any defined symbols from the undefined symbols set...
72 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
73 I != UndefinedSymbols.end(); )
74 if (DefinedSymbols.count(*I))
75 UndefinedSymbols.erase(I++); // This symbol really is defined!
76 else
77 ++I; // Keep this symbol in the undefined symbols list
78}
79
Reid Spencer1cfa8d62004-11-12 20:34:32 +000080/// LinkInArchive - opens an archive library and link in all objects which
81/// provide symbols that are currently undefined.
82///
83/// Inputs:
Reid Spencer1cfa8d62004-11-12 20:34:32 +000084/// Filename - The pathname of the archive.
Reid Spencer1cfa8d62004-11-12 20:34:32 +000085///
86/// Return Value:
87/// TRUE - An error occurred.
88/// FALSE - No errors.
Misha Brukman10468d82005-04-21 22:55:34 +000089bool
Reid Spencer8d4ff902007-04-30 00:29:39 +000090Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
Reid Spencerfe73bfc2004-12-13 02:59:29 +000091
92 // Make sure this is an archive file we're dealing with
93 if (!Filename.isArchive())
94 return error("File '" + Filename.toString() + "' is not an archive.");
95
96 // Open the archive file
97 verbose("Linking archive file '" + Filename.toString() + "'");
98
Chris Lattner41528e62007-05-06 09:29:13 +000099 // Find all of the symbols currently undefined in the bitcode program.
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000100 // If all the symbols are defined, the program is complete, and there is
101 // no reason to link in any archive files.
102 std::set<std::string> UndefinedSymbols;
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000103 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Misha Brukman10468d82005-04-21 22:55:34 +0000104
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000105 if (UndefinedSymbols.empty()) {
Misha Brukman10468d82005-04-21 22:55:34 +0000106 verbose("No symbols undefined, skipping library '" +
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000107 Filename.toString() + "'");
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000108 return false; // No need to link anything in!
109 }
110
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000111 std::string ErrMsg;
Reid Spencer5208a112004-11-16 06:47:41 +0000112 std::auto_ptr<Archive> AutoArch (
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000113 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Reid Spencer5208a112004-11-16 06:47:41 +0000114
115 Archive* arch = AutoArch.get();
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000116
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000117 if (!arch)
Misha Brukman10468d82005-04-21 22:55:34 +0000118 return error("Cannot read archive '" + Filename.toString() +
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000119 "': " + ErrMsg);
Gabor Greife16561c2007-07-05 17:07:56 +0000120 if (!arch->isBitcodeArchive()) {
Reid Spencer8d4ff902007-04-30 00:29:39 +0000121 is_native = true;
122 return false;
123 }
124 is_native = false;
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000125
Reid Spencerc2455ca2004-11-19 03:13:25 +0000126 // Save a set of symbols that are not defined by the archive. Since we're
127 // entering a loop, there's no point searching for these multiple times. This
128 // variable is used to "set_subtract" from the set of undefined symbols.
129 std::set<std::string> NotDefinedByArchive;
130
Reid Spencer75db6642006-11-11 11:54:25 +0000131 // Save the current set of undefined symbols, because we may have to make
132 // multiple passes over the archive:
133 std::set<std::string> CurrentlyUndefinedSymbols;
134
135 do {
136 CurrentlyUndefinedSymbols = UndefinedSymbols;
Reid Spencerc2455ca2004-11-19 03:13:25 +0000137
138 // Find the modules we need to link into the target module
Reid Spencer9db61d92004-11-14 22:02:27 +0000139 std::set<ModuleProvider*> Modules;
Reid Spencer546436c2006-07-07 20:56:50 +0000140 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
141 return error("Cannot find symbols in '" + Filename.toString() +
142 "': " + ErrMsg);
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000143
Misha Brukman10468d82005-04-21 22:55:34 +0000144 // If we didn't find any more modules to link this time, we are done
Reid Spencerc2455ca2004-11-19 03:13:25 +0000145 // searching this archive.
Reid Spencer9db61d92004-11-14 22:02:27 +0000146 if (Modules.empty())
147 break;
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000148
Reid Spencerc2455ca2004-11-19 03:13:25 +0000149 // Any symbols remaining in UndefinedSymbols after
150 // findModulesDefiningSymbols are ones that the archive does not define. So
151 // we add them to the NotDefinedByArchive variable now.
152 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
Reid Spencer33563252004-11-19 03:27:05 +0000153 UndefinedSymbols.end());
Reid Spencerc2455ca2004-11-19 03:13:25 +0000154
Reid Spencer9db61d92004-11-14 22:02:27 +0000155 // Loop over all the ModuleProviders that we got back from the archive
156 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
157 I != E; ++I) {
Reid Spencerc2455ca2004-11-19 03:13:25 +0000158
Reid Spencer9db61d92004-11-14 22:02:27 +0000159 // Get the module we must link in.
Reid Spencer75db6642006-11-11 11:54:25 +0000160 std::string moduleErrorMsg;
161 std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg ));
Reid Spencer71ce9272007-07-22 21:39:37 +0000162 if (!moduleErrorMsg.empty())
163 return error("Could not load a module: " + moduleErrorMsg);
164
Reid Spencer5208a112004-11-16 06:47:41 +0000165 Module* aModule = AutoModule.get();
166
Reid Spencer75db6642006-11-11 11:54:25 +0000167 if (aModule != NULL) {
168 verbose(" Linking in module: " + aModule->getModuleIdentifier());
Chris Lattner71a44082005-02-13 15:26:14 +0000169
Reid Spencer75db6642006-11-11 11:54:25 +0000170 // Link it in
171 if (LinkInModule(aModule, &moduleErrorMsg)) {
172 return error("Cannot link in module '" +
173 aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
174 }
Reid Spencer06d8e0f2006-11-11 20:27:49 +0000175 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000176 }
Reid Spencer75db6642006-11-11 11:54:25 +0000177
Reid Spencerc2455ca2004-11-19 03:13:25 +0000178 // Get the undefined symbols from the aggregate module. This recomputes the
179 // symbols we still need after the new modules have been linked in.
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000180 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencerc2455ca2004-11-19 03:13:25 +0000181
182 // At this point we have two sets of undefined symbols: UndefinedSymbols
Misha Brukman10468d82005-04-21 22:55:34 +0000183 // which holds the undefined symbols from all the modules, and
Reid Spencerc2455ca2004-11-19 03:13:25 +0000184 // NotDefinedByArchive which holds symbols we know the archive doesn't
185 // define. There's no point searching for symbols that we won't find in the
186 // archive so we subtract these sets.
Chris Lattner08a5eb32005-02-13 17:50:16 +0000187 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Misha Brukman10468d82005-04-21 22:55:34 +0000188
Reid Spencerc2455ca2004-11-19 03:13:25 +0000189 // If there's no symbols left, no point in continuing to search the
190 // archive.
191 if (UndefinedSymbols.empty())
192 break;
Reid Spencer75db6642006-11-11 11:54:25 +0000193 } while (CurrentlyUndefinedSymbols != UndefinedSymbols);
Misha Brukman10468d82005-04-21 22:55:34 +0000194
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000195 return false;
196}