blob: a35991ce2d45c8ac4211878355664e2cfc209df8 [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 Spencerc2455ca2004-11-19 03:13:25 +000016#include "llvm/ADT/SetOperations.h"
Chris Lattner41528e62007-05-06 09:29:13 +000017#include "llvm/Bitcode/Archive.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Reid Spencer6b853b22004-12-20 04:15:44 +000019#include <memory>
Reid Spencer1cfa8d62004-11-12 20:34:32 +000020#include <set>
21using namespace llvm;
22
Reid Spencer1cfa8d62004-11-12 20:34:32 +000023/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
24/// exist in an LLVM module. This is a bit tricky because there may be two
25/// symbols with the same name but different LLVM types that will be resolved to
26/// each other but aren't currently (thus we need to treat it as resolved).
27///
28/// Inputs:
29/// M - The module in which to find undefined symbols.
30///
31/// Outputs:
32/// UndefinedSymbols - A set of C++ strings containing the name of all
33/// undefined symbols.
34///
Reid Spencerfe73bfc2004-12-13 02:59:29 +000035static void
36GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
Reid Spencer1cfa8d62004-11-12 20:34:32 +000037 std::set<std::string> DefinedSymbols;
Reid Spencerc2455ca2004-11-19 03:13:25 +000038 UndefinedSymbols.clear();
Chris Lattner018b0932005-03-15 23:03:34 +000039
40 // If the program doesn't define a main, try pulling one in from a .a file.
41 // This is needed for programs where the main function is defined in an
42 // archive, such f2c'd programs.
Reid Spencer1241d6d2007-02-05 21:19:13 +000043 Function *Main = M->getFunction("main");
Reid Spencer5301e7c2007-01-30 20:08:39 +000044 if (Main == 0 || Main->isDeclaration())
Chris Lattner018b0932005-03-15 23:03:34 +000045 UndefinedSymbols.insert("main");
Misha Brukman10468d82005-04-21 22:55:34 +000046
Reid Spencer1cfa8d62004-11-12 20:34:32 +000047 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
48 if (I->hasName()) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000049 if (I->isDeclaration())
Reid Spencer1cfa8d62004-11-12 20:34:32 +000050 UndefinedSymbols.insert(I->getName());
Rafael Espindola6de96a12009-01-15 20:18:42 +000051 else if (!I->hasLocalLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000052 assert(!I->hasDLLImportLinkage()
53 && "Found dllimported non-external symbol!");
Reid Spencer1cfa8d62004-11-12 20:34:32 +000054 DefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000055 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +000056 }
Anton Korobeynikov28179f72008-03-04 20:16:11 +000057
Chris Lattner018b0932005-03-15 23:03:34 +000058 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
59 I != E; ++I)
Reid Spencer1cfa8d62004-11-12 20:34:32 +000060 if (I->hasName()) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000061 if (I->isDeclaration())
Reid Spencer1cfa8d62004-11-12 20:34:32 +000062 UndefinedSymbols.insert(I->getName());
Rafael Espindola6de96a12009-01-15 20:18:42 +000063 else if (!I->hasLocalLinkage()) {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000064 assert(!I->hasDLLImportLinkage()
65 && "Found dllimported non-external symbol!");
Reid Spencer1cfa8d62004-11-12 20:34:32 +000066 DefinedSymbols.insert(I->getName());
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000067 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +000068 }
Misha Brukman10468d82005-04-21 22:55:34 +000069
Anton Korobeynikov28179f72008-03-04 20:16:11 +000070 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
71 I != E; ++I)
Anton Korobeynikov2591afc2008-03-11 00:24:53 +000072 if (I->hasName())
73 DefinedSymbols.insert(I->getName());
Anton Korobeynikov28179f72008-03-04 20:16:11 +000074
Reid Spencer1cfa8d62004-11-12 20:34:32 +000075 // 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
Reid Spencer1cfa8d62004-11-12 20:34:32 +000084/// LinkInArchive - opens an archive library and link in all objects which
85/// provide symbols that are currently undefined.
86///
87/// Inputs:
Reid Spencer1cfa8d62004-11-12 20:34:32 +000088/// Filename - The pathname of the archive.
Reid Spencer1cfa8d62004-11-12 20:34:32 +000089///
90/// Return Value:
91/// TRUE - An error occurred.
92/// FALSE - No errors.
Misha Brukman10468d82005-04-21 22:55:34 +000093bool
Reid Spencer8d4ff902007-04-30 00:29:39 +000094Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
Reid Spencerfe73bfc2004-12-13 02:59:29 +000095 // Make sure this is an archive file we're dealing with
96 if (!Filename.isArchive())
Chris Lattnerc521f542009-08-23 22:45:37 +000097 return error("File '" + Filename.str() + "' is not an archive.");
Reid Spencerfe73bfc2004-12-13 02:59:29 +000098
99 // Open the archive file
Chris Lattnerc521f542009-08-23 22:45:37 +0000100 verbose("Linking archive file '" + Filename.str() + "'");
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000101
Chris Lattner41528e62007-05-06 09:29:13 +0000102 // Find all of the symbols currently undefined in the bitcode program.
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000103 // If all the symbols are defined, the program is complete, and there is
104 // no reason to link in any archive files.
105 std::set<std::string> UndefinedSymbols;
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000106 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Misha Brukman10468d82005-04-21 22:55:34 +0000107
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000108 if (UndefinedSymbols.empty()) {
Chris Lattnerc521f542009-08-23 22:45:37 +0000109 verbose("No symbols undefined, skipping library '" + Filename.str() + "'");
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000110 return false; // No need to link anything in!
111 }
112
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000113 std::string ErrMsg;
Reid Spencer5208a112004-11-16 06:47:41 +0000114 std::auto_ptr<Archive> AutoArch (
Owen Anderson6773d382009-07-01 16:58:40 +0000115 Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg));
Reid Spencer5208a112004-11-16 06:47:41 +0000116
117 Archive* arch = AutoArch.get();
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000118
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000119 if (!arch)
Chris Lattnerc521f542009-08-23 22:45:37 +0000120 return error("Cannot read archive '" + Filename.str() +
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000121 "': " + ErrMsg);
Gabor Greife16561c2007-07-05 17:07:56 +0000122 if (!arch->isBitcodeArchive()) {
Reid Spencer8d4ff902007-04-30 00:29:39 +0000123 is_native = true;
124 return false;
125 }
126 is_native = false;
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000127
Reid Spencerc2455ca2004-11-19 03:13:25 +0000128 // Save a set of symbols that are not defined by the archive. Since we're
129 // entering a loop, there's no point searching for these multiple times. This
130 // variable is used to "set_subtract" from the set of undefined symbols.
131 std::set<std::string> NotDefinedByArchive;
132
Reid Spencer75db6642006-11-11 11:54:25 +0000133 // Save the current set of undefined symbols, because we may have to make
134 // multiple passes over the archive:
135 std::set<std::string> CurrentlyUndefinedSymbols;
136
137 do {
138 CurrentlyUndefinedSymbols = UndefinedSymbols;
Reid Spencerc2455ca2004-11-19 03:13:25 +0000139
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000140 // Find the modules we need to link into the target module. Note that arch
141 // keeps ownership of these modules and may return the same Module* from a
142 // subsequent call.
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000143 SmallVector<Module*, 16> Modules;
Reid Spencer546436c2006-07-07 20:56:50 +0000144 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
Chris Lattnerc521f542009-08-23 22:45:37 +0000145 return error("Cannot find symbols in '" + Filename.str() +
Reid Spencer546436c2006-07-07 20:56:50 +0000146 "': " + ErrMsg);
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000147
Misha Brukman10468d82005-04-21 22:55:34 +0000148 // If we didn't find any more modules to link this time, we are done
Reid Spencerc2455ca2004-11-19 03:13:25 +0000149 // searching this archive.
Reid Spencer9db61d92004-11-14 22:02:27 +0000150 if (Modules.empty())
151 break;
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000152
Reid Spencerc2455ca2004-11-19 03:13:25 +0000153 // 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(),
Reid Spencer33563252004-11-19 03:27:05 +0000157 UndefinedSymbols.end());
Reid Spencerc2455ca2004-11-19 03:13:25 +0000158
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000159 // Loop over all the Modules that we got back from the archive
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000160 for (SmallVectorImpl<Module*>::iterator I=Modules.begin(), E=Modules.end();
Reid Spencer9db61d92004-11-14 22:02:27 +0000161 I != E; ++I) {
Reid Spencerc2455ca2004-11-19 03:13:25 +0000162
Reid Spencer9db61d92004-11-14 22:02:27 +0000163 // Get the module we must link in.
Reid Spencer75db6642006-11-11 11:54:25 +0000164 std::string moduleErrorMsg;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000165 Module* aModule = *I;
Reid Spencer75db6642006-11-11 11:54:25 +0000166 if (aModule != NULL) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000167 if (aModule->MaterializeAll(&moduleErrorMsg))
168 return error("Could not load a module: " + moduleErrorMsg);
169
Reid Spencer75db6642006-11-11 11:54:25 +0000170 verbose(" Linking in module: " + aModule->getModuleIdentifier());
Chris Lattner71a44082005-02-13 15:26:14 +0000171
Reid Spencer75db6642006-11-11 11:54:25 +0000172 // Link it in
Chris Lattnerd1c4e752009-11-09 04:18:23 +0000173 if (LinkInModule(aModule, &moduleErrorMsg))
Reid Spencer75db6642006-11-11 11:54:25 +0000174 return error("Cannot link in module '" +
175 aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
Reid Spencer06d8e0f2006-11-11 20:27:49 +0000176 }
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000177 }
Reid Spencer75db6642006-11-11 11:54:25 +0000178
Reid Spencerc2455ca2004-11-19 03:13:25 +0000179 // Get the undefined symbols from the aggregate module. This recomputes the
180 // symbols we still need after the new modules have been linked in.
Reid Spencerfe73bfc2004-12-13 02:59:29 +0000181 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencerc2455ca2004-11-19 03:13:25 +0000182
183 // At this point we have two sets of undefined symbols: UndefinedSymbols
Misha Brukman10468d82005-04-21 22:55:34 +0000184 // which holds the undefined symbols from all the modules, and
Reid Spencerc2455ca2004-11-19 03:13:25 +0000185 // NotDefinedByArchive which holds symbols we know the archive doesn't
186 // define. There's no point searching for symbols that we won't find in the
187 // archive so we subtract these sets.
Chris Lattner08a5eb32005-02-13 17:50:16 +0000188 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Misha Brukman10468d82005-04-21 22:55:34 +0000189
Reid Spencerc2455ca2004-11-19 03:13:25 +0000190 // If there's no symbols left, no point in continuing to search the
191 // archive.
192 if (UndefinedSymbols.empty())
193 break;
Reid Spencer75db6642006-11-11 11:54:25 +0000194 } while (CurrentlyUndefinedSymbols != UndefinedSymbols);
Misha Brukman10468d82005-04-21 22:55:34 +0000195
Reid Spencer1cfa8d62004-11-12 20:34:32 +0000196 return false;
197}