2004-11-12 21:34:32 +01:00
|
|
|
//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2004-11-12 21:34:32 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2004-11-12 21:34:32 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-05-06 11:29:13 +02:00
|
|
|
// This file contains routines to handle linking together LLVM bitcode files,
|
2004-11-12 21:34:32 +01:00
|
|
|
// and to handle annoying things like static libraries.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 23:02:27 +01:00
|
|
|
#include "llvm/Linker.h"
|
2004-11-12 21:34:32 +01:00
|
|
|
#include "llvm/Module.h"
|
2004-11-14 23:02:27 +01:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2004-11-19 04:13:25 +01:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2007-05-06 11:29:13 +02:00
|
|
|
#include "llvm/Bitcode/Archive.h"
|
2004-11-12 21:34:32 +01:00
|
|
|
#include "llvm/Config/config.h"
|
2004-12-20 05:15:44 +01:00
|
|
|
#include <memory>
|
2004-11-12 21:34:32 +01:00
|
|
|
#include <set>
|
2004-12-13 03:59:29 +01:00
|
|
|
using namespace llvm;
|
2004-11-12 21:34:32 +01: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 03:59:29 +01:00
|
|
|
static void
|
|
|
|
GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
|
2004-11-12 21:34:32 +01:00
|
|
|
std::set<std::string> DefinedSymbols;
|
2004-11-19 04:13:25 +01:00
|
|
|
UndefinedSymbols.clear();
|
2005-03-16 00:03:34 +01: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 22:19:13 +01:00
|
|
|
Function *Main = M->getFunction("main");
|
2007-01-30 21:08:39 +01:00
|
|
|
if (Main == 0 || Main->isDeclaration())
|
2005-03-16 00:03:34 +01:00
|
|
|
UndefinedSymbols.insert("main");
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2004-11-12 21:34:32 +01:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (I->hasName()) {
|
2007-01-30 21:08:39 +01:00
|
|
|
if (I->isDeclaration())
|
2004-11-12 21:34:32 +01:00
|
|
|
UndefinedSymbols.insert(I->getName());
|
2009-01-15 21:18:42 +01:00
|
|
|
else if (!I->hasLocalLinkage()) {
|
2006-09-14 20:23:27 +02:00
|
|
|
assert(!I->hasDLLImportLinkage()
|
|
|
|
&& "Found dllimported non-external symbol!");
|
2004-11-12 21:34:32 +01:00
|
|
|
DefinedSymbols.insert(I->getName());
|
2006-09-14 20:23:27 +02:00
|
|
|
}
|
2004-11-12 21:34:32 +01:00
|
|
|
}
|
2008-03-04 21:16:11 +01:00
|
|
|
|
2005-03-16 00:03:34 +01:00
|
|
|
for (Module::global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I)
|
2004-11-12 21:34:32 +01:00
|
|
|
if (I->hasName()) {
|
2007-01-30 21:08:39 +01:00
|
|
|
if (I->isDeclaration())
|
2004-11-12 21:34:32 +01:00
|
|
|
UndefinedSymbols.insert(I->getName());
|
2009-01-15 21:18:42 +01:00
|
|
|
else if (!I->hasLocalLinkage()) {
|
2006-09-14 20:23:27 +02:00
|
|
|
assert(!I->hasDLLImportLinkage()
|
|
|
|
&& "Found dllimported non-external symbol!");
|
2004-11-12 21:34:32 +01:00
|
|
|
DefinedSymbols.insert(I->getName());
|
2006-09-14 20:23:27 +02:00
|
|
|
}
|
2004-11-12 21:34:32 +01:00
|
|
|
}
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2008-03-04 21:16:11 +01:00
|
|
|
for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I)
|
2008-03-11 01:24:53 +01:00
|
|
|
if (I->hasName())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
2008-03-04 21:16:11 +01:00
|
|
|
|
2004-11-12 21:34:32 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-22 00:55:34 +02:00
|
|
|
bool
|
2007-04-30 02:29:39 +02:00
|
|
|
Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
|
2004-12-13 03:59:29 +01:00
|
|
|
// Make sure this is an archive file we're dealing with
|
|
|
|
if (!Filename.isArchive())
|
2009-08-24 00:45:37 +02:00
|
|
|
return error("File '" + Filename.str() + "' is not an archive.");
|
2004-12-13 03:59:29 +01:00
|
|
|
|
|
|
|
// Open the archive file
|
2009-08-24 00:45:37 +02:00
|
|
|
verbose("Linking archive file '" + Filename.str() + "'");
|
2004-12-13 03:59:29 +01:00
|
|
|
|
2007-05-06 11:29:13 +02:00
|
|
|
// Find all of the symbols currently undefined in the bitcode program.
|
2004-11-12 21:34:32 +01: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 03:59:29 +01:00
|
|
|
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2004-11-12 21:34:32 +01:00
|
|
|
if (UndefinedSymbols.empty()) {
|
2009-08-24 00:45:37 +02:00
|
|
|
verbose("No symbols undefined, skipping library '" + Filename.str() + "'");
|
2004-11-12 21:34:32 +01:00
|
|
|
return false; // No need to link anything in!
|
|
|
|
}
|
|
|
|
|
2004-12-13 03:59:29 +01:00
|
|
|
std::string ErrMsg;
|
2004-11-16 07:47:41 +01:00
|
|
|
std::auto_ptr<Archive> AutoArch (
|
2009-07-01 18:58:40 +02:00
|
|
|
Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg));
|
2004-11-16 07:47:41 +01:00
|
|
|
|
|
|
|
Archive* arch = AutoArch.get();
|
2004-11-12 21:34:32 +01:00
|
|
|
|
2004-12-13 03:59:29 +01:00
|
|
|
if (!arch)
|
2009-08-24 00:45:37 +02:00
|
|
|
return error("Cannot read archive '" + Filename.str() +
|
2004-12-13 03:59:29 +01:00
|
|
|
"': " + ErrMsg);
|
2007-07-05 19:07:56 +02:00
|
|
|
if (!arch->isBitcodeArchive()) {
|
2007-04-30 02:29:39 +02:00
|
|
|
is_native = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
is_native = false;
|
2004-12-13 03:59:29 +01:00
|
|
|
|
2004-11-19 04:13:25 +01: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 12:54:25 +01: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 04:13:25 +01:00
|
|
|
|
|
|
|
// Find the modules we need to link into the target module
|
2004-11-14 23:02:27 +01:00
|
|
|
std::set<ModuleProvider*> Modules;
|
2006-07-07 22:56:50 +02:00
|
|
|
if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
|
2009-08-24 00:45:37 +02:00
|
|
|
return error("Cannot find symbols in '" + Filename.str() +
|
2006-07-07 22:56:50 +02:00
|
|
|
"': " + ErrMsg);
|
2004-11-14 23:02:27 +01:00
|
|
|
|
2005-04-22 00:55:34 +02:00
|
|
|
// If we didn't find any more modules to link this time, we are done
|
2004-11-19 04:13:25 +01:00
|
|
|
// searching this archive.
|
2004-11-14 23:02:27 +01:00
|
|
|
if (Modules.empty())
|
|
|
|
break;
|
|
|
|
|
2004-11-19 04:13:25 +01: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 04:27:05 +01:00
|
|
|
UndefinedSymbols.end());
|
2004-11-19 04:13:25 +01:00
|
|
|
|
2004-11-14 23:02:27 +01:00
|
|
|
// Loop over all the ModuleProviders that we got back from the archive
|
|
|
|
for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
|
|
|
|
I != E; ++I) {
|
2004-11-19 04:13:25 +01:00
|
|
|
|
2004-11-14 23:02:27 +01:00
|
|
|
// Get the module we must link in.
|
2006-11-11 12:54:25 +01:00
|
|
|
std::string moduleErrorMsg;
|
|
|
|
std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg ));
|
2007-07-22 23:39:37 +02:00
|
|
|
if (!moduleErrorMsg.empty())
|
|
|
|
return error("Could not load a module: " + moduleErrorMsg);
|
|
|
|
|
2004-11-16 07:47:41 +01:00
|
|
|
Module* aModule = AutoModule.get();
|
2004-11-14 23:02:27 +01:00
|
|
|
|
2006-11-11 12:54:25 +01:00
|
|
|
if (aModule != NULL) {
|
|
|
|
verbose(" Linking in module: " + aModule->getModuleIdentifier());
|
|
|
|
|
|
|
|
// Link it in
|
2009-11-09 05:18:23 +01:00
|
|
|
if (LinkInModule(aModule, &moduleErrorMsg))
|
2006-11-11 12:54:25 +01:00
|
|
|
return error("Cannot link in module '" +
|
|
|
|
aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
|
2006-11-11 21:27:49 +01:00
|
|
|
}
|
2004-11-12 21:34:32 +01:00
|
|
|
}
|
2006-11-11 12:54:25 +01:00
|
|
|
|
2004-11-19 04:13:25 +01: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 03:59:29 +01:00
|
|
|
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
|
2004-11-19 04:13:25 +01:00
|
|
|
|
|
|
|
// At this point we have two sets of undefined symbols: UndefinedSymbols
|
2005-04-22 00:55:34 +02:00
|
|
|
// which holds the undefined symbols from all the modules, and
|
2004-11-19 04:13:25 +01: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 18:50:16 +01:00
|
|
|
set_subtract(UndefinedSymbols, NotDefinedByArchive);
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2004-11-19 04:13:25 +01:00
|
|
|
// If there's no symbols left, no point in continuing to search the
|
|
|
|
// archive.
|
|
|
|
if (UndefinedSymbols.empty())
|
|
|
|
break;
|
2006-11-11 12:54:25 +01:00
|
|
|
} while (CurrentlyUndefinedSymbols != UndefinedSymbols);
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2004-11-12 21:34:32 +01:00
|
|
|
return false;
|
|
|
|
}
|