2018-05-22 07:45:40 +08:00
|
|
|
//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
|
|
|
|
//
|
|
|
|
// 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/RTDyldObjectLinkingLayer.h"
|
|
|
|
|
2018-07-21 02:31:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
class JITDylibSearchOrderResolver : public JITSymbolResolver {
|
2018-07-21 02:31:50 +08:00
|
|
|
public:
|
2018-08-18 05:18:18 +08:00
|
|
|
JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2018-09-26 03:48:46 +08:00
|
|
|
void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) {
|
2018-08-18 05:18:18 +08:00
|
|
|
auto &ES = MR.getTargetJITDylib().getExecutionSession();
|
2018-07-21 02:31:50 +08:00
|
|
|
SymbolNameSet InternedSymbols;
|
|
|
|
|
2018-09-26 03:48:46 +08:00
|
|
|
// Intern the requested symbols: lookup takes interned strings.
|
2018-07-21 02:31:50 +08:00
|
|
|
for (auto &S : Symbols)
|
|
|
|
InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
|
|
|
|
|
2018-09-26 03:48:46 +08:00
|
|
|
// Build an OnResolve callback to unwrap the interned strings and pass them
|
|
|
|
// to the OnResolved callback.
|
|
|
|
// FIXME: Switch to move capture of OnResolved once we have c++14.
|
|
|
|
auto OnResolvedWithUnwrap =
|
|
|
|
[OnResolved](Expected<SymbolMap> InternedResult) {
|
|
|
|
if (!InternedResult) {
|
|
|
|
OnResolved(InternedResult.takeError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupResult Result;
|
|
|
|
for (auto &KV : *InternedResult)
|
|
|
|
Result[*KV.first] = std::move(KV.second);
|
|
|
|
OnResolved(Result);
|
|
|
|
};
|
|
|
|
|
|
|
|
// We're not waiting for symbols to be ready. Just log any errors.
|
|
|
|
auto OnReady = [&ES](Error Err) { ES.reportError(std::move(Err)); };
|
|
|
|
|
|
|
|
// Register dependencies for all symbols contained in this set.
|
2018-07-21 06:22:19 +08:00
|
|
|
auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
|
2018-07-21 08:12:05 +08:00
|
|
|
MR.addDependenciesForAll(Deps);
|
2018-07-21 02:31:50 +08:00
|
|
|
};
|
|
|
|
|
2018-09-26 03:48:46 +08:00
|
|
|
MR.getTargetJITDylib().withSearchOrderDo([&](const JITDylibList &JDs) {
|
|
|
|
ES.lookup(JDs, InternedSymbols, OnResolvedWithUnwrap, OnReady,
|
|
|
|
RegisterDependencies);
|
|
|
|
});
|
2018-07-21 02:31:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
|
|
|
|
LookupSet Result;
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
for (auto &KV : MR.getSymbols()) {
|
|
|
|
if (Symbols.count(*KV.first))
|
|
|
|
Result.insert(*KV.first);
|
|
|
|
}
|
2018-07-21 02:31:50 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MaterializationResponsibility &MR;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-05-22 07:45:40 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2(
|
2018-07-21 02:31:50 +08:00
|
|
|
ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
|
2018-08-18 10:06:18 +08:00
|
|
|
NotifyLoadedFunction NotifyLoaded, NotifyEmittedFunction NotifyEmitted)
|
2018-07-21 02:31:50 +08:00
|
|
|
: ObjectLayer(ES), GetMemoryManager(GetMemoryManager),
|
2018-05-22 07:45:40 +08:00
|
|
|
NotifyLoaded(std::move(NotifyLoaded)),
|
2018-08-31 08:53:17 +08:00
|
|
|
NotifyEmitted(std::move(NotifyEmitted)) {}
|
2018-05-22 07:45:40 +08:00
|
|
|
|
|
|
|
void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R,
|
|
|
|
VModuleKey K,
|
|
|
|
std::unique_ptr<MemoryBuffer> O) {
|
2018-05-24 05:27:01 +08:00
|
|
|
assert(O && "Object must not be null");
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
// This method launches an asynchronous link step that will fulfill our
|
|
|
|
// materialization responsibility. We need to switch R to be heap
|
|
|
|
// allocated before that happens so it can live as long as the asynchronous
|
|
|
|
// link needs it to (i.e. it must be able to outlive this method).
|
|
|
|
auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R));
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
auto &ES = getExecutionSession();
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
auto Obj = object::ObjectFile::createObjectFile(*O);
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
if (!Obj) {
|
|
|
|
getExecutionSession().reportError(Obj.takeError());
|
|
|
|
SharedR->failMaterialization();
|
|
|
|
return;
|
2018-05-22 07:45:40 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
// Collect the internal symbols from the object file: We will need to
|
|
|
|
// filter these later.
|
|
|
|
auto InternalSymbols = std::make_shared<std::set<StringRef>>();
|
2018-05-22 07:45:40 +08:00
|
|
|
{
|
2018-09-26 06:57:44 +08:00
|
|
|
for (auto &Sym : (*Obj)->symbols()) {
|
2018-06-19 02:01:43 +08:00
|
|
|
if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
|
|
|
|
if (auto SymName = Sym.getName())
|
2018-09-26 06:57:44 +08:00
|
|
|
InternalSymbols->insert(*SymName);
|
2018-06-19 02:01:43 +08:00
|
|
|
else {
|
|
|
|
ES.reportError(SymName.takeError());
|
|
|
|
R.failMaterialization();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 06:57:44 +08:00
|
|
|
}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
auto MemoryManager = GetMemoryManager(K);
|
|
|
|
auto &MemMgr = *MemoryManager;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
2018-08-31 08:53:17 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
assert(!MemMgrs.count(K) &&
|
|
|
|
"A memory manager already exists for this key?");
|
|
|
|
MemMgrs[K] = std::move(MemoryManager);
|
|
|
|
}
|
2018-08-31 08:53:17 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
JITDylibSearchOrderResolver Resolver(*SharedR);
|
|
|
|
|
|
|
|
/* Thoughts on proper cross-dylib weak symbol handling:
|
|
|
|
*
|
|
|
|
* Change selection of canonical defs to be a manually triggered process, and
|
|
|
|
* add a 'canonical' bit to symbol definitions. When canonical def selection
|
|
|
|
* is triggered, sweep the JITDylibs to mark defs as canonical, discard
|
|
|
|
* duplicate defs.
|
|
|
|
*/
|
|
|
|
jitLinkForORC(
|
|
|
|
**Obj, std::move(O), MemMgr, Resolver, ProcessAllSections,
|
|
|
|
[this, K, SharedR, &Obj, InternalSymbols](
|
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
|
|
|
|
std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
|
|
|
|
return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo),
|
|
|
|
ResolvedSymbols, *InternalSymbols);
|
|
|
|
},
|
|
|
|
[this, K, SharedR](Error Err) {
|
|
|
|
onObjEmit(K, *SharedR, std::move(Err));
|
|
|
|
});
|
|
|
|
}
|
2018-08-31 08:53:17 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
Error RTDyldObjectLinkingLayer2::onObjLoad(
|
|
|
|
VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj,
|
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
|
|
|
|
std::map<StringRef, JITEvaluatedSymbol> Resolved,
|
|
|
|
std::set<StringRef> &InternalSymbols) {
|
|
|
|
SymbolFlagsMap ExtraSymbolsToClaim;
|
|
|
|
SymbolMap Symbols;
|
|
|
|
for (auto &KV : Resolved) {
|
|
|
|
// Scan the symbols and add them to the Symbols map for resolution.
|
|
|
|
|
|
|
|
// We never claim internal symbols.
|
|
|
|
if (InternalSymbols.count(KV.first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto InternedName =
|
|
|
|
getExecutionSession().getSymbolStringPool().intern(KV.first);
|
|
|
|
auto Flags = KV.second.getFlags();
|
|
|
|
|
|
|
|
// Override object flags and claim responsibility for symbols if
|
|
|
|
// requested.
|
|
|
|
if (OverrideObjectFlags || AutoClaimObjectSymbols) {
|
|
|
|
auto I = R.getSymbols().find(InternedName);
|
|
|
|
|
|
|
|
if (OverrideObjectFlags && I != R.getSymbols().end())
|
|
|
|
Flags = JITSymbolFlags::stripTransientFlags(I->second);
|
|
|
|
else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
|
|
|
|
ExtraSymbolsToClaim[InternedName] = Flags;
|
2018-08-31 08:53:17 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
|
2018-05-22 07:45:40 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
if (!ExtraSymbolsToClaim.empty())
|
|
|
|
if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
|
|
|
|
return Err;
|
|
|
|
|
|
|
|
R.resolve(Symbols);
|
|
|
|
|
2018-05-22 07:45:40 +08:00
|
|
|
if (NotifyLoaded)
|
2018-09-26 06:57:44 +08:00
|
|
|
NotifyLoaded(K, Obj, *LoadedObjInfo);
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-09-26 06:57:44 +08:00
|
|
|
void RTDyldObjectLinkingLayer2::onObjEmit(VModuleKey K,
|
|
|
|
MaterializationResponsibility &R,
|
|
|
|
Error Err) {
|
|
|
|
if (Err) {
|
|
|
|
getExecutionSession().reportError(std::move(Err));
|
2018-05-22 07:45:40 +08:00
|
|
|
R.failMaterialization();
|
2018-06-19 02:01:43 +08:00
|
|
|
return;
|
2018-05-22 07:45:40 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 10:06:18 +08:00
|
|
|
R.emit();
|
2018-05-22 07:45:40 +08:00
|
|
|
|
2018-08-18 10:06:18 +08:00
|
|
|
if (NotifyEmitted)
|
|
|
|
NotifyEmitted(K);
|
2018-05-22 07:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End namespace orc.
|
|
|
|
} // End namespace llvm.
|