2015-04-02 06:34:45 +02:00
|
|
|
//===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
|
|
|
|
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2018-06-26 23:35:48 +02:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2015-04-02 06:34:45 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
|
|
|
|
: InitList(
|
|
|
|
GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
|
|
|
|
I((InitList && End) ? InitList->getNumOperands() : 0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const {
|
|
|
|
assert(InitList == Other.InitList && "Incomparable iterators.");
|
|
|
|
return I == Other.I;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const {
|
|
|
|
return !(*this == Other);
|
|
|
|
}
|
|
|
|
|
|
|
|
CtorDtorIterator& CtorDtorIterator::operator++() {
|
|
|
|
++I;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CtorDtorIterator CtorDtorIterator::operator++(int) {
|
|
|
|
CtorDtorIterator Temp = *this;
|
|
|
|
++I;
|
|
|
|
return Temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
CtorDtorIterator::Element CtorDtorIterator::operator*() const {
|
|
|
|
ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
|
|
|
|
assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors");
|
|
|
|
|
|
|
|
Constant *FuncC = CS->getOperand(1);
|
|
|
|
Function *Func = nullptr;
|
|
|
|
|
|
|
|
// Extract function pointer, pulling off any casts.
|
|
|
|
while (FuncC) {
|
|
|
|
if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
|
|
|
|
Func = F;
|
|
|
|
break;
|
|
|
|
} else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
|
|
|
|
if (CE->isCast())
|
|
|
|
FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// This isn't anything we recognize. Bail out with Func left set to null.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
|
2018-03-15 01:30:14 +01:00
|
|
|
Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
|
2018-06-27 00:43:01 +02:00
|
|
|
if (Data && !isa<GlobalValue>(Data))
|
2018-06-26 23:35:48 +02:00
|
|
|
Data = nullptr;
|
2015-04-02 06:34:45 +02:00
|
|
|
return Element(Priority->getZExtValue(), Func, Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator_range<CtorDtorIterator> getConstructors(const Module &M) {
|
|
|
|
const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
|
|
|
|
return make_range(CtorDtorIterator(CtorsList, false),
|
|
|
|
CtorDtorIterator(CtorsList, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator_range<CtorDtorIterator> getDestructors(const Module &M) {
|
|
|
|
const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
|
|
|
|
return make_range(CtorDtorIterator(DtorsList, false),
|
|
|
|
CtorDtorIterator(DtorsList, true));
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:56:10 +02:00
|
|
|
void CtorDtorRunner::add(iterator_range<CtorDtorIterator> CtorDtors) {
|
2018-10-31 01:23:23 +01:00
|
|
|
if (empty(CtorDtors))
|
2018-06-26 23:35:48 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
MangleAndInterner Mangle(
|
2018-08-17 23:18:18 +02:00
|
|
|
JD.getExecutionSession(),
|
2018-06-26 23:35:48 +02:00
|
|
|
(*CtorDtors.begin()).Func->getParent()->getDataLayout());
|
|
|
|
|
|
|
|
for (const auto &CtorDtor : CtorDtors) {
|
|
|
|
assert(CtorDtor.Func && CtorDtor.Func->hasName() &&
|
|
|
|
"Ctor/Dtor function must be named to be runnable under the JIT");
|
|
|
|
|
2018-10-09 22:44:32 +02:00
|
|
|
// FIXME: Maybe use a symbol promoter here instead.
|
|
|
|
if (CtorDtor.Func->hasLocalLinkage()) {
|
|
|
|
CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility);
|
|
|
|
}
|
|
|
|
|
2018-06-26 23:35:48 +02:00
|
|
|
if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
|
|
|
|
dbgs() << " Skipping because why now?\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CtorDtorsByPriority[CtorDtor.Priority].push_back(
|
|
|
|
Mangle(CtorDtor.Func->getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:56:10 +02:00
|
|
|
Error CtorDtorRunner::run() {
|
2018-06-26 23:35:48 +02:00
|
|
|
using CtorDtorTy = void (*)();
|
|
|
|
|
|
|
|
SymbolNameSet Names;
|
|
|
|
|
|
|
|
for (auto &KV : CtorDtorsByPriority) {
|
|
|
|
for (auto &Name : KV.second) {
|
|
|
|
auto Added = Names.insert(Name).second;
|
|
|
|
(void)Added;
|
|
|
|
assert(Added && "Ctor/Dtor names clashed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:53:40 +02:00
|
|
|
auto &ES = JD.getExecutionSession();
|
2018-10-23 22:54:43 +02:00
|
|
|
if (auto CtorDtorMap =
|
2018-10-24 01:01:39 +02:00
|
|
|
ES.lookup(JITDylibSearchList({{&JD, true}}), std::move(Names),
|
|
|
|
NoDependenciesToRegister, true)) {
|
2018-06-26 23:35:48 +02:00
|
|
|
for (auto &KV : CtorDtorsByPriority) {
|
|
|
|
for (auto &Name : KV.second) {
|
|
|
|
assert(CtorDtorMap->count(Name) && "No entry for Name");
|
|
|
|
auto CtorDtor = reinterpret_cast<CtorDtorTy>(
|
|
|
|
static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
|
|
|
|
CtorDtor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
} else
|
|
|
|
return CtorDtorMap.takeError();
|
|
|
|
|
|
|
|
CtorDtorsByPriority.clear();
|
2018-06-27 00:30:42 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2018-06-26 23:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalCXXRuntimeOverridesBase::runDestructors() {
|
2015-04-02 06:34:45 +02:00
|
|
|
auto& CXXDestructorDataPairs = DSOHandleOverride;
|
|
|
|
for (auto &P : CXXDestructorDataPairs)
|
|
|
|
P.first(P.second);
|
|
|
|
CXXDestructorDataPairs.clear();
|
|
|
|
}
|
|
|
|
|
2018-06-26 23:35:48 +02:00
|
|
|
int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
|
|
|
|
void *Arg,
|
|
|
|
void *DSOHandle) {
|
2015-04-02 06:34:45 +02:00
|
|
|
auto& CXXDestructorDataPairs =
|
|
|
|
*reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
|
|
|
|
CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:56:10 +02:00
|
|
|
Error LocalCXXRuntimeOverrides::enable(JITDylib &JD,
|
2018-08-17 23:18:18 +02:00
|
|
|
MangleAndInterner &Mangle) {
|
2018-10-16 00:27:02 +02:00
|
|
|
SymbolMap RuntimeInterposes;
|
|
|
|
RuntimeInterposes[Mangle("__dso_handle")] =
|
|
|
|
JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
|
|
|
|
JITSymbolFlags::Exported);
|
|
|
|
RuntimeInterposes[Mangle("__cxa_atexit")] =
|
|
|
|
JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
|
|
|
|
JITSymbolFlags::Exported);
|
2018-06-26 23:35:48 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
|
2018-06-26 23:35:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
|
2018-06-26 23:35:48 +02:00
|
|
|
sys::DynamicLibrary Dylib, const DataLayout &DL, SymbolPredicate Allow)
|
|
|
|
: Dylib(std::move(Dylib)), Allow(std::move(Allow)),
|
|
|
|
GlobalPrefix(DL.getGlobalPrefix()) {}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
Expected<DynamicLibrarySearchGenerator>
|
|
|
|
DynamicLibrarySearchGenerator::Load(const char *FileName, const DataLayout &DL,
|
|
|
|
SymbolPredicate Allow) {
|
2018-10-01 02:59:28 +02:00
|
|
|
std::string ErrMsg;
|
|
|
|
auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
|
|
|
|
if (!Lib.isValid())
|
|
|
|
return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
|
2018-10-15 07:07:54 +02:00
|
|
|
return DynamicLibrarySearchGenerator(std::move(Lib), DL, std::move(Allow));
|
2018-10-01 02:59:28 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
SymbolNameSet DynamicLibrarySearchGenerator::
|
2018-08-17 23:18:18 +02:00
|
|
|
operator()(JITDylib &JD, const SymbolNameSet &Names) {
|
2018-06-26 23:35:48 +02:00
|
|
|
orc::SymbolNameSet Added;
|
|
|
|
orc::SymbolMap NewSymbols;
|
|
|
|
|
|
|
|
bool HasGlobalPrefix = (GlobalPrefix != '\0');
|
|
|
|
|
|
|
|
for (auto &Name : Names) {
|
2018-10-15 07:07:54 +02:00
|
|
|
if ((*Name).empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Allow && !Allow(Name))
|
2018-06-26 23:35:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string Tmp((*Name).data() + (HasGlobalPrefix ? 1 : 0), (*Name).size());
|
|
|
|
if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
|
|
|
|
Added.insert(Name);
|
|
|
|
NewSymbols[Name] = JITEvaluatedSymbol(
|
|
|
|
static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
|
|
|
|
JITSymbolFlags::Exported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
// Add any new symbols to JD. Since the generator is only called for symbols
|
|
|
|
// that are not already defined, this will never trigger a duplicate
|
2018-06-26 23:35:48 +02:00
|
|
|
// definition error, so we can wrap this call in a 'cantFail'.
|
|
|
|
if (!NewSymbols.empty())
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols(std::move(NewSymbols))));
|
2018-06-26 23:35:48 +02:00
|
|
|
|
|
|
|
return Added;
|
|
|
|
}
|
|
|
|
|
2015-04-02 06:34:45 +02:00
|
|
|
} // End namespace orc.
|
|
|
|
} // End namespace llvm.
|