2004-11-12 20:34:32 +00:00
|
|
|
//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-19 20:24:23 +00:00
|
|
|
//
|
2007-05-06 09:29:13 +00:00
|
|
|
// This file contains routines to handle linking together LLVM bitcode files,
|
2003-09-30 03:24:28 +00:00
|
|
|
// and to handle annoying things like static libraries.
|
2003-09-19 20:24:23 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
#include "llvm/Linker.h"
|
2003-09-19 20:24:23 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-11-19 03:13:25 +00:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2007-05-06 09:29:13 +00:00
|
|
|
#include "llvm/Bitcode/Archive.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2004-12-20 04:15:44 +00:00
|
|
|
#include <memory>
|
2003-09-19 20:24:23 +00:00
|
|
|
#include <set>
|
2004-12-13 02:59:29 +00:00
|
|
|
using namespace llvm;
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
|
|
|
|
/// exist in an LLVM module. This is a bit tricky because there may be two
|
|
|
|
/// symbols with the same name but different LLVM types that will be resolved to
|
|
|
|
/// each other but aren't currently (thus we need to treat it as resolved).
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// M - The module in which to find undefined symbols.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// UndefinedSymbols - A set of C++ strings containing the name of all
|
|
|
|
/// undefined symbols.
|
|
|
|
///
|
2004-12-13 02:59:29 +00:00
|
|
|
static void
|
|
|
|
GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
|
2003-09-19 20:24:23 +00:00
|
|
|
std::set<std::string> DefinedSymbols;
|
2004-11-19 03:13:25 +00:00
|
|
|
UndefinedSymbols.clear();
|
2005-03-15 23:03:34 +00:00
|
|
|
|
|
|
|
// If the program doesn't define a main, try pulling one in from a .a file.
|
|
|
|
// This is needed for programs where the main function is defined in an
|
|
|
|
// archive, such f2c'd programs.
|
2007-02-05 21:19:13 +00:00
|
|
|
Function *Main = M->getFunction("main");
|
2007-01-30 20:08:39 +00:00
|
|
|
if (Main == 0 || Main->isDeclaration())
|
2005-03-15 23:03:34 +00:00
|
|
|
UndefinedSymbols.insert("main");
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (I->hasName()) {
|
2007-01-30 20:08:39 +00:00
|
|
|
if (I->isDeclaration())
|
2003-09-19 20:24:23 +00:00
|
|
|
UndefinedSymbols.insert(I->getName());
|
2009-01-15 20:18:42 +00:00
|
|
|
else if (!I->hasLocalLinkage()) {
|
2006-09-14 18:23:27 +00:00
|
|
|
assert(!I->hasDLLImportLinkage()
|
|
|
|
&& "Found dllimported non-external symbol!");
|
2003-09-19 20:24:23 +00:00
|
|
|
DefinedSymbols.insert(I->getName());
|
2006-09-14 18:23:27 +00:00
|
|
|
}
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
2008-03-04 20:16:11 +00:00
|
|
|
|
2005-03-15 23:03:34 +00:00
|
|
|
for (Module::global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I)
|
2003-09-19 20:24:23 +00:00
|
|
|
if (I->hasName()) {
|
2007-01-30 20:08:39 +00:00
|
|
|
if (I->isDeclaration())
|
2003-09-19 20:24:23 +00:00
|
|
|
UndefinedSymbols.insert(I->getName());
|
2009-01-15 20:18:42 +00:00
|
|
|
else if (!I->hasLocalLinkage()) {
|
2006-09-14 18:23:27 +00:00
|
|
|
assert(!I->hasDLLImportLinkage()
|
|
|
|
&& "Found dllimported non-external symbol!");
|
2003-09-19 20:24:23 +00:00
|
|
|
DefinedSymbols.insert(I->getName());
|
2006-09-14 18:23:27 +00:00
|
|
|
}
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2008-03-04 20:16:11 +00:00
|
|
|
for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I)
|
2008-03-11 00:24:53 +00:00
|
|
|
if (I->hasName())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
2008-03-04 20:16:11 +00:00
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
// Prune out any defined symbols from the undefined symbols set...
|
|
|
|
for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
|
|
|
|
I != UndefinedSymbols.end(); )
|
|
|
|
if (DefinedSymbols.count(*I))
|
|
|
|
UndefinedSymbols.erase(I++); // This symbol really is defined!
|
|
|
|
else
|
|
|
|
++I; // Keep this symbol in the undefined symbols list
|
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// LinkInArchive - opens an archive library and link in all objects which
|
|
|
|
/// provide symbols that are currently undefined.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// Filename - The pathname of the archive.
|
|
|
|
///
|
|
|
|
/// Return Value:
|
|
|
|
/// TRUE - An error occurred.
|
|
|
|
/// FALSE - No errors.
|
2005-04-21 22:55:34 +00:00
|
|
|
bool
|
2007-04-30 00:29:39 +00:00
|
|
|
Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
|
2004-12-13 02:59:29 +00:00
|
|
|
// Make sure this is an archive file we're dealing with
|
|
|
|
if (!Filename.isArchive())
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("File '" + Filename.str() + "' is not an archive.");
|
2004-12-13 02:59:29 +00:00
|
|
|
|
|
|
|
// Open the archive file
|
2009-08-23 22:45:37 +00:00
|
|
|
verbose("Linking archive file '" + Filename.str() + "'");
|
2004-12-13 02:59:29 +00:00
|
|
|
|
2007-05-06 09:29:13 +00:00
|
|
|
// Find all of the symbols currently undefined in the bitcode program.
|
2003-09-19 20:24:23 +00:00
|
|
|
// If all the symbols are defined, the program is complete, and there is
|
|
|
|
// no reason to link in any archive files.
|
|
|
|
std::set<std::string> UndefinedSymbols;
|
2004-12-13 02:59:29 +00:00
|
|
|
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-09-29 22:16:43 +00:00
|
|
|
if (UndefinedSymbols.empty()) {
|
2009-08-23 22:45:37 +00:00
|
|
|
verbose("No symbols undefined, skipping library '" + Filename.str() + "'");
|
2003-09-19 20:24:23 +00:00
|
|
|
return false; // No need to link anything in!
|
|
|
|
}
|
|
|
|
|
2004-12-13 02:59:29 +00:00
|
|
|
std::string ErrMsg;
|
2004-11-16 06:47:41 +00:00
|
|
|
std::auto_ptr<Archive> AutoArch (
|
2009-07-01 16:58:40 +00:00
|
|
|
Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg));
|
2004-11-16 06:47:41 +00:00
|
|
|
|
|
|
|
Archive* arch = AutoArch.get();
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2004-12-13 02:59:29 +00:00
|
|
|
if (!arch)
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot read archive '" + Filename.str() +
|
2004-12-13 02:59:29 +00:00
|
|
|
"': " + ErrMsg);
|
2007-07-05 17:07:56 +00:00
|
|
|
if (!arch->isBitcodeArchive()) {
|
2007-04-30 00:29:39 +00:00
|
|
|
is_native = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
is_native = false;
|
2004-12-13 02:59:29 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Save a set of symbols that are not defined by the archive. Since we're
|
|
|
|
// entering a loop, there's no point searching for these multiple times. This
|
|
|
|
// variable is used to "set_subtract" from the set of undefined symbols.
|
|
|
|
std::set<std::string> NotDefinedByArchive;
|
|
|
|
|
2006-11-11 11:54:25 +00:00
|
|
|
// Save the current set of undefined symbols, because we may have to make
|
|
|
|
// multiple passes over the archive:
|
|
|
|
std::set<std::string> CurrentlyUndefinedSymbols;
|
|
|
|
|
|
|
|
do {
|
|
|
|
CurrentlyUndefinedSymbols = UndefinedSymbols;
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
// Find the modules we need to link into the target module. Note that arch
|
|
|
|
// keeps ownership of these modules and may return the same Module* from a
|
|
|
|
// subsequent call.
|
|
|
|
std::set<Module*> Modules;
|
2006-07-07 20:56:50 +00:00
|
|
|
if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot find symbols in '" + Filename.str() +
|
2006-07-07 20:56:50 +00:00
|
|
|
"': " + ErrMsg);
|
2004-11-14 22:02:27 +00:00
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
// If we didn't find any more modules to link this time, we are done
|
2004-11-19 03:13:25 +00:00
|
|
|
// searching this archive.
|
2004-11-14 22:02:27 +00:00
|
|
|
if (Modules.empty())
|
|
|
|
break;
|
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Any symbols remaining in UndefinedSymbols after
|
|
|
|
// findModulesDefiningSymbols are ones that the archive does not define. So
|
|
|
|
// we add them to the NotDefinedByArchive variable now.
|
|
|
|
NotDefinedByArchive.insert(UndefinedSymbols.begin(),
|
2004-11-19 03:27:05 +00:00
|
|
|
UndefinedSymbols.end());
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
// Loop over all the Modules that we got back from the archive
|
|
|
|
for (std::set<Module*>::iterator I=Modules.begin(), E=Modules.end();
|
2004-11-14 22:02:27 +00:00
|
|
|
I != E; ++I) {
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
// Get the module we must link in.
|
2006-11-11 11:54:25 +00:00
|
|
|
std::string moduleErrorMsg;
|
2010-01-27 20:34:15 +00:00
|
|
|
Module* aModule = *I;
|
2006-11-11 11:54:25 +00:00
|
|
|
if (aModule != NULL) {
|
2010-01-27 20:34:15 +00:00
|
|
|
if (aModule->MaterializeAll(&moduleErrorMsg))
|
|
|
|
return error("Could not load a module: " + moduleErrorMsg);
|
|
|
|
|
2006-11-11 11:54:25 +00:00
|
|
|
verbose(" Linking in module: " + aModule->getModuleIdentifier());
|
|
|
|
|
|
|
|
// Link it in
|
2009-11-09 04:18:23 +00:00
|
|
|
if (LinkInModule(aModule, &moduleErrorMsg))
|
2006-11-11 11:54:25 +00:00
|
|
|
return error("Cannot link in module '" +
|
|
|
|
aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
|
2006-11-11 20:27:49 +00:00
|
|
|
}
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
2006-11-11 11:54:25 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Get the undefined symbols from the aggregate module. This recomputes the
|
|
|
|
// symbols we still need after the new modules have been linked in.
|
2004-12-13 02:59:29 +00:00
|
|
|
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
|
2004-11-19 03:13:25 +00:00
|
|
|
|
|
|
|
// At this point we have two sets of undefined symbols: UndefinedSymbols
|
2005-04-21 22:55:34 +00:00
|
|
|
// which holds the undefined symbols from all the modules, and
|
2004-11-19 03:13:25 +00:00
|
|
|
// NotDefinedByArchive which holds symbols we know the archive doesn't
|
|
|
|
// define. There's no point searching for symbols that we won't find in the
|
|
|
|
// archive so we subtract these sets.
|
2005-02-13 17:50:16 +00:00
|
|
|
set_subtract(UndefinedSymbols, NotDefinedByArchive);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// If there's no symbols left, no point in continuing to search the
|
|
|
|
// archive.
|
|
|
|
if (UndefinedSymbols.empty())
|
|
|
|
break;
|
2006-11-11 11:54:25 +00:00
|
|
|
} while (CurrentlyUndefinedSymbols != UndefinedSymbols);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
return false;
|
|
|
|
}
|