blob: 2c4ed7fdc17a78e0cf70500cc65571842fe2e9f6 [file] [log] [blame]
Reid Spencerc408c452004-11-12 20:34:32 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
John Criswell71478b72003-09-19 20:24:23 +00009//
Chris Lattner4bcca0f2007-05-06 09:29:13 +000010// This file contains routines to handle linking together LLVM bitcode files,
Chris Lattnera58d2be2003-09-30 03:24:28 +000011// and to handle annoying things like static libraries.
John Criswell71478b72003-09-19 20:24:23 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencer8bbb17a2004-11-14 22:02:27 +000015#include "llvm/Linker.h"
John Criswell71478b72003-09-19 20:24:23 +000016#include "llvm/Module.h"
Reid Spencer3cf2c322004-11-19 03:13:25 +000017#include "llvm/ADT/SetOperations.h"
Chris Lattner4bcca0f2007-05-06 09:29:13 +000018#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Config/config.h"
Reid Spencer4ca8e712004-12-20 04:15:44 +000020#include <memory>
John Criswell71478b72003-09-19 20:24:23 +000021#include <set>
Chris Lattner6cc8ca92003-11-28 07:44:09 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Misha Brukman5208ba12003-09-30 18:09:32 +000024/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
25/// exist in an LLVM module. This is a bit tricky because there may be two
26/// symbols with the same name but different LLVM types that will be resolved to
27/// each other but aren't currently (thus we need to treat it as resolved).
28///
29/// Inputs:
30/// M - The module in which to find undefined symbols.
31///
32/// Outputs:
33/// UndefinedSymbols - A set of C++ strings containing the name of all
34/// undefined symbols.
35///
Reid Spencer7dde0e32004-12-13 02:59:29 +000036static void
37GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000038 std::set<std::string> DefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +000039 UndefinedSymbols.clear();
Chris Lattnerc72e5732005-03-15 23:03:34 +000040
41 // If the program doesn't define a main, try pulling one in from a .a file.
42 // This is needed for programs where the main function is defined in an
43 // archive, such f2c'd programs.
Reid Spencer688b0492007-02-05 21:19:13 +000044 Function *Main = M->getFunction("main");
Reid Spencer5cbf9852007-01-30 20:08:39 +000045 if (Main == 0 || Main->isDeclaration())
Chris Lattnerc72e5732005-03-15 23:03:34 +000046 UndefinedSymbols.insert("main");
Misha Brukmanf976c852005-04-21 22:55:34 +000047
John Criswell71478b72003-09-19 20:24:23 +000048 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
49 if (I->hasName()) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000050 if (I->isDeclaration())
John Criswell71478b72003-09-19 20:24:23 +000051 UndefinedSymbols.insert(I->getName());
Rafael Espindolabb46f522009-01-15 20:18:42 +000052 else if (!I->hasLocalLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000053 assert(!I->hasDLLImportLinkage()
54 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000055 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000056 }
John Criswell71478b72003-09-19 20:24:23 +000057 }
Anton Korobeynikoveba04152008-03-04 20:16:11 +000058
Chris Lattnerc72e5732005-03-15 23:03:34 +000059 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
60 I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000061 if (I->hasName()) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000062 if (I->isDeclaration())
John Criswell71478b72003-09-19 20:24:23 +000063 UndefinedSymbols.insert(I->getName());
Rafael Espindolabb46f522009-01-15 20:18:42 +000064 else if (!I->hasLocalLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000065 assert(!I->hasDLLImportLinkage()
66 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000067 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000068 }
John Criswell71478b72003-09-19 20:24:23 +000069 }
Misha Brukmanf976c852005-04-21 22:55:34 +000070
Anton Korobeynikoveba04152008-03-04 20:16:11 +000071 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
72 I != E; ++I)
Anton Korobeynikov7fcb6b62008-03-11 00:24:53 +000073 if (I->hasName())
74 DefinedSymbols.insert(I->getName());
Anton Korobeynikoveba04152008-03-04 20:16:11 +000075
John Criswell71478b72003-09-19 20:24:23 +000076 // Prune out any defined symbols from the undefined symbols set...
77 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
78 I != UndefinedSymbols.end(); )
79 if (DefinedSymbols.count(*I))
80 UndefinedSymbols.erase(I++); // This symbol really is defined!
81 else
82 ++I; // Keep this symbol in the undefined symbols list
83}
84
Misha Brukman5208ba12003-09-30 18:09:32 +000085/// LinkInArchive - opens an archive library and link in all objects which
86/// provide symbols that are currently undefined.
87///
88/// Inputs:
Misha Brukman5208ba12003-09-30 18:09:32 +000089/// Filename - The pathname of the archive.
Misha Brukman5208ba12003-09-30 18:09:32 +000090///
91/// Return Value:
92/// TRUE - An error occurred.
93/// FALSE - No errors.
Misha Brukmanf976c852005-04-21 22:55:34 +000094bool
Reid Spencerc9a83e42007-04-30 00:29:39 +000095Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
Reid Spencer7dde0e32004-12-13 02:59:29 +000096 // Make sure this is an archive file we're dealing with
97 if (!Filename.isArchive())
Chris Lattner74382b72009-08-23 22:45:37 +000098 return error("File '" + Filename.str() + "' is not an archive.");
Reid Spencer7dde0e32004-12-13 02:59:29 +000099
100 // Open the archive file
Chris Lattner74382b72009-08-23 22:45:37 +0000101 verbose("Linking archive file '" + Filename.str() + "'");
Reid Spencer7dde0e32004-12-13 02:59:29 +0000102
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000103 // Find all of the symbols currently undefined in the bitcode program.
John Criswell71478b72003-09-19 20:24:23 +0000104 // If all the symbols are defined, the program is complete, and there is
105 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000106 std::set<std::string> UndefinedSymbols;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000107 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Misha Brukmanf976c852005-04-21 22:55:34 +0000108
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000109 if (UndefinedSymbols.empty()) {
Chris Lattner74382b72009-08-23 22:45:37 +0000110 verbose("No symbols undefined, skipping library '" + Filename.str() + "'");
John Criswell71478b72003-09-19 20:24:23 +0000111 return false; // No need to link anything in!
112 }
113
Reid Spencer7dde0e32004-12-13 02:59:29 +0000114 std::string ErrMsg;
Reid Spencer99d36042004-11-16 06:47:41 +0000115 std::auto_ptr<Archive> AutoArch (
Owen Anderson8b477ed2009-07-01 16:58:40 +0000116 Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg));
Reid Spencer99d36042004-11-16 06:47:41 +0000117
118 Archive* arch = AutoArch.get();
John Criswell71478b72003-09-19 20:24:23 +0000119
Reid Spencer7dde0e32004-12-13 02:59:29 +0000120 if (!arch)
Chris Lattner74382b72009-08-23 22:45:37 +0000121 return error("Cannot read archive '" + Filename.str() +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000122 "': " + ErrMsg);
Gabor Greifa99be512007-07-05 17:07:56 +0000123 if (!arch->isBitcodeArchive()) {
Reid Spencerc9a83e42007-04-30 00:29:39 +0000124 is_native = true;
125 return false;
126 }
127 is_native = false;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000128
Reid Spencer3cf2c322004-11-19 03:13:25 +0000129 // Save a set of symbols that are not defined by the archive. Since we're
130 // entering a loop, there's no point searching for these multiple times. This
131 // variable is used to "set_subtract" from the set of undefined symbols.
132 std::set<std::string> NotDefinedByArchive;
133
Reid Spencer49521432006-11-11 11:54:25 +0000134 // Save the current set of undefined symbols, because we may have to make
135 // multiple passes over the archive:
136 std::set<std::string> CurrentlyUndefinedSymbols;
137
138 do {
139 CurrentlyUndefinedSymbols = UndefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +0000140
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000141 // Find the modules we need to link into the target module. Note that arch
142 // keeps ownership of these modules and may return the same Module* from a
143 // subsequent call.
144 std::set<Module*> Modules;
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000145 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
Chris Lattner74382b72009-08-23 22:45:37 +0000146 return error("Cannot find symbols in '" + Filename.str() +
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000147 "': " + ErrMsg);
John Criswell71478b72003-09-19 20:24:23 +0000148
Misha Brukmanf976c852005-04-21 22:55:34 +0000149 // If we didn't find any more modules to link this time, we are done
Reid Spencer3cf2c322004-11-19 03:13:25 +0000150 // searching this archive.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000151 if (Modules.empty())
152 break;
John Criswell71478b72003-09-19 20:24:23 +0000153
Reid Spencer3cf2c322004-11-19 03:13:25 +0000154 // 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(),
Reid Spencerb9371ce2004-11-19 03:27:05 +0000158 UndefinedSymbols.end());
Reid Spencer3cf2c322004-11-19 03:13:25 +0000159
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000160 // Loop over all the Modules that we got back from the archive
161 for (std::set<Module*>::iterator I=Modules.begin(), E=Modules.end();
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000162 I != E; ++I) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000163
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000164 // Get the module we must link in.
Reid Spencer49521432006-11-11 11:54:25 +0000165 std::string moduleErrorMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000166 Module* aModule = *I;
Reid Spencer49521432006-11-11 11:54:25 +0000167 if (aModule != NULL) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000168 if (aModule->MaterializeAll(&moduleErrorMsg))
169 return error("Could not load a module: " + moduleErrorMsg);
170
Reid Spencer49521432006-11-11 11:54:25 +0000171 verbose(" Linking in module: " + aModule->getModuleIdentifier());
Chris Lattner50c301b2005-02-13 15:26:14 +0000172
Reid Spencer49521432006-11-11 11:54:25 +0000173 // Link it in
Chris Lattner570a0b82009-11-09 04:18:23 +0000174 if (LinkInModule(aModule, &moduleErrorMsg))
Reid Spencer49521432006-11-11 11:54:25 +0000175 return error("Cannot link in module '" +
176 aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
Reid Spencer0288d182006-11-11 20:27:49 +0000177 }
John Criswell71478b72003-09-19 20:24:23 +0000178 }
Reid Spencer49521432006-11-11 11:54:25 +0000179
Reid Spencer3cf2c322004-11-19 03:13:25 +0000180 // Get the undefined symbols from the aggregate module. This recomputes the
181 // symbols we still need after the new modules have been linked in.
Reid Spencer7dde0e32004-12-13 02:59:29 +0000182 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000183
184 // At this point we have two sets of undefined symbols: UndefinedSymbols
Misha Brukmanf976c852005-04-21 22:55:34 +0000185 // which holds the undefined symbols from all the modules, and
Reid Spencer3cf2c322004-11-19 03:13:25 +0000186 // NotDefinedByArchive which holds symbols we know the archive doesn't
187 // define. There's no point searching for symbols that we won't find in the
188 // archive so we subtract these sets.
Chris Lattner12945ac2005-02-13 17:50:16 +0000189 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Misha Brukmanf976c852005-04-21 22:55:34 +0000190
Reid Spencer3cf2c322004-11-19 03:13:25 +0000191 // If there's no symbols left, no point in continuing to search the
192 // archive.
193 if (UndefinedSymbols.empty())
194 break;
Reid Spencer49521432006-11-11 11:54:25 +0000195 } while (CurrentlyUndefinedSymbols != UndefinedSymbols);
Misha Brukmanf976c852005-04-21 22:55:34 +0000196
John Criswell71478b72003-09-19 20:24:23 +0000197 return false;
198}