#include "llvm/Linker.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/Bitcode/Archive.h"
#include "llvm/Config/config.h"
#include <memory>
#include <set>
using namespace llvm;
static void
GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
std::set<std::string> DefinedSymbols;
UndefinedSymbols.clear();
Function *Main = M->getFunction("main");
if (Main == 0 || Main->isDeclaration())
UndefinedSymbols.insert("main");
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
if (I->hasName()) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
assert(!I->hasDLLImportLinkage()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
}
for (Module::global_iterator I = M->global_begin(), E = M->global_end();
I != E; ++I)
if (I->hasName()) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
assert(!I->hasDLLImportLinkage()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
}
for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
I != E; ++I)
if (I->hasName())
DefinedSymbols.insert(I->getName());
for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
I != UndefinedSymbols.end(); )
if (DefinedSymbols.count(*I))
UndefinedSymbols.erase(I++); else
++I; }
bool
Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) {
if (!Filename.isArchive())
return error("File '" + Filename.toString() + "' is not an archive.");
verbose("Linking archive file '" + Filename.toString() + "'");
std::set<std::string> UndefinedSymbols;
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
if (UndefinedSymbols.empty()) {
verbose("No symbols undefined, skipping library '" +
Filename.toString() + "'");
return false; }
std::string ErrMsg;
std::auto_ptr<Archive> AutoArch (
Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Archive* arch = AutoArch.get();
if (!arch)
return error("Cannot read archive '" + Filename.toString() +
"': " + ErrMsg);
if (!arch->isBitcodeArchive()) {
is_native = true;
return false;
}
is_native = false;
std::set<std::string> NotDefinedByArchive;
std::set<std::string> CurrentlyUndefinedSymbols;
do {
CurrentlyUndefinedSymbols = UndefinedSymbols;
std::set<ModuleProvider*> Modules;
if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
return error("Cannot find symbols in '" + Filename.toString() +
"': " + ErrMsg);
if (Modules.empty())
break;
NotDefinedByArchive.insert(UndefinedSymbols.begin(),
UndefinedSymbols.end());
for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
I != E; ++I) {
std::string moduleErrorMsg;
std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg ));
if (!moduleErrorMsg.empty())
return error("Could not load a module: " + moduleErrorMsg);
Module* aModule = AutoModule.get();
if (aModule != NULL) {
verbose(" Linking in module: " + aModule->getModuleIdentifier());
if (LinkInModule(aModule, &moduleErrorMsg)) {
return error("Cannot link in module '" +
aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
}
}
}
GetAllUndefinedSymbols(Composite, UndefinedSymbols);
set_subtract(UndefinedSymbols, NotDefinedByArchive);
if (UndefinedSymbols.empty())
break;
} while (CurrentlyUndefinedSymbols != UndefinedSymbols);
return false;
}