2018-05-21 23:45:40 +00:00
|
|
|
//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
|
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-21 23:45:40 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
2020-01-17 14:48:48 -08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2018-07-20 18:31:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
|
2018-08-17 21:18:18 +00:00
|
|
|
class JITDylibSearchOrderResolver : public JITSymbolResolver {
|
2018-07-20 18:31:50 +00:00
|
|
|
public:
|
2018-08-17 21:18:18 +00:00
|
|
|
JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
|
2018-07-20 18:31:50 +00:00
|
|
|
|
2020-07-16 20:38:41 -07:00
|
|
|
void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
|
2018-08-17 21:18:18 +00:00
|
|
|
auto &ES = MR.getTargetJITDylib().getExecutionSession();
|
2019-11-25 21:57:27 -08:00
|
|
|
SymbolLookupSet InternedSymbols;
|
2018-07-20 18:31:50 +00:00
|
|
|
|
2018-09-25 19:48:46 +00:00
|
|
|
// Intern the requested symbols: lookup takes interned strings.
|
2018-07-20 18:31:50 +00:00
|
|
|
for (auto &S : Symbols)
|
2019-11-25 21:57:27 -08:00
|
|
|
InternedSymbols.add(ES.intern(S));
|
2018-07-20 18:31:50 +00:00
|
|
|
|
2018-09-25 19:48:46 +00:00
|
|
|
// Build an OnResolve callback to unwrap the interned strings and pass them
|
|
|
|
|
// to the OnResolved callback.
|
|
|
|
|
auto OnResolvedWithUnwrap =
|
2019-09-13 11:35:33 +00:00
|
|
|
[OnResolved = std::move(OnResolved)](
|
|
|
|
|
Expected<SymbolMap> InternedResult) mutable {
|
2018-09-25 19:48:46 +00:00
|
|
|
if (!InternedResult) {
|
|
|
|
|
OnResolved(InternedResult.takeError());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LookupResult Result;
|
|
|
|
|
for (auto &KV : *InternedResult)
|
|
|
|
|
Result[*KV.first] = std::move(KV.second);
|
|
|
|
|
OnResolved(Result);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Register dependencies for all symbols contained in this set.
|
2018-07-20 22:22:19 +00:00
|
|
|
auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
|
2018-07-21 00:12:05 +00:00
|
|
|
MR.addDependenciesForAll(Deps);
|
2018-07-20 18:31:50 +00:00
|
|
|
};
|
|
|
|
|
|
2020-05-04 16:43:42 -07:00
|
|
|
JITDylibSearchOrder LinkOrder;
|
|
|
|
|
MR.getTargetJITDylib().withLinkOrderDo(
|
|
|
|
|
[&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
|
|
|
|
|
ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
|
2019-11-25 21:57:27 -08:00
|
|
|
SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
|
|
|
|
|
RegisterDependencies);
|
2018-07-20 18:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-16 20:38:41 -07:00
|
|
|
Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
|
2018-08-28 21:18:05 +00:00
|
|
|
LookupSet Result;
|
2018-07-20 22:22:19 +00:00
|
|
|
|
2018-08-28 21:18:05 +00:00
|
|
|
for (auto &KV : MR.getSymbols()) {
|
|
|
|
|
if (Symbols.count(*KV.first))
|
|
|
|
|
Result.insert(*KV.first);
|
|
|
|
|
}
|
2018-07-20 18:31:50 +00:00
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MaterializationResponsibility &MR;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
2018-05-21 23:45:40 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
namespace orc {
|
|
|
|
|
|
2018-10-15 22:56:10 +00:00
|
|
|
RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
|
2019-05-01 22:40:23 +00:00
|
|
|
ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
|
|
|
|
|
: ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2019-12-20 21:08:35 -08:00
|
|
|
RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
|
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
2020-03-19 16:14:18 -07:00
|
|
|
for (auto &MemMgr : MemMgrs) {
|
|
|
|
|
for (auto *L : EventListeners)
|
|
|
|
|
L->notifyFreeingObject(
|
|
|
|
|
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr.get())));
|
2019-12-20 21:08:35 -08:00
|
|
|
MemMgr->deregisterEHFrames();
|
2020-03-19 16:14:18 -07:00
|
|
|
}
|
2019-12-20 21:08:35 -08:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 22:56:10 +00:00
|
|
|
void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R,
|
2018-10-16 20:13:06 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> O) {
|
2018-05-23 21:27:01 +00:00
|
|
|
assert(O && "Object must not be null");
|
2020-02-24 12:10:13 +01:00
|
|
|
|
2018-09-25 22:57:44 +00: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-21 23:45:40 +00:00
|
|
|
auto &ES = getExecutionSession();
|
|
|
|
|
|
2020-03-19 16:14:18 -07:00
|
|
|
auto Obj = object::ObjectFile::createObjectFile(*O);
|
2018-09-25 22:57:44 +00:00
|
|
|
|
|
|
|
|
if (!Obj) {
|
|
|
|
|
getExecutionSession().reportError(Obj.takeError());
|
|
|
|
|
SharedR->failMaterialization();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collect the internal symbols from the object file: We will need to
|
|
|
|
|
// filter these later.
|
|
|
|
|
auto InternalSymbols = std::make_shared<std::set<StringRef>>();
|
|
|
|
|
{
|
|
|
|
|
for (auto &Sym : (*Obj)->symbols()) {
|
2020-03-03 16:02:46 -08:00
|
|
|
|
|
|
|
|
// Skip file symbols.
|
|
|
|
|
if (auto SymType = Sym.getType()) {
|
|
|
|
|
if (*SymType == object::SymbolRef::ST_File)
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
ES.reportError(SymType.takeError());
|
|
|
|
|
R.failMaterialization();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 20:24:21 +08:00
|
|
|
Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
|
|
|
|
|
if (!SymFlagsOrErr) {
|
|
|
|
|
// TODO: Test this error.
|
|
|
|
|
ES.reportError(SymFlagsOrErr.takeError());
|
|
|
|
|
R.failMaterialization();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 16:02:46 -08:00
|
|
|
// Don't include symbols that aren't global.
|
2020-04-10 20:24:21 +08:00
|
|
|
if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
|
2018-09-25 22:57:44 +00:00
|
|
|
if (auto SymName = Sym.getName())
|
|
|
|
|
InternalSymbols->insert(*SymName);
|
|
|
|
|
else {
|
|
|
|
|
ES.reportError(SymName.takeError());
|
|
|
|
|
R.failMaterialization();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-21 23:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 20:13:06 +00:00
|
|
|
auto K = R.getVModuleKey();
|
2018-10-22 21:17:56 +00:00
|
|
|
RuntimeDyld::MemoryManager *MemMgr = nullptr;
|
|
|
|
|
|
|
|
|
|
// Create a record a memory manager for this object.
|
|
|
|
|
{
|
|
|
|
|
auto Tmp = GetMemoryManager();
|
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
|
|
|
|
MemMgrs.push_back(std::move(Tmp));
|
|
|
|
|
MemMgr = MemMgrs.back().get();
|
|
|
|
|
}
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2018-09-25 22:57:44 +00:00
|
|
|
JITDylibSearchOrderResolver Resolver(*SharedR);
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2018-09-25 22:57:44 +00:00
|
|
|
jitLinkForORC(
|
2020-03-19 16:14:18 -07:00
|
|
|
object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
|
|
|
|
|
*MemMgr, Resolver, ProcessAllSections,
|
|
|
|
|
[this, K, SharedR, MemMgr, InternalSymbols](
|
|
|
|
|
const object::ObjectFile &Obj,
|
2018-09-25 22:57:44 +00:00
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
|
|
|
|
|
std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
|
2020-03-19 16:14:18 -07:00
|
|
|
return onObjLoad(K, *SharedR, Obj, MemMgr, std::move(LoadedObjInfo),
|
2018-09-25 22:57:44 +00:00
|
|
|
ResolvedSymbols, *InternalSymbols);
|
|
|
|
|
},
|
2020-03-19 16:14:18 -07:00
|
|
|
[this, K, SharedR, MemMgr](object::OwningBinary<object::ObjectFile> Obj,
|
|
|
|
|
Error Err) mutable {
|
|
|
|
|
onObjEmit(K, *SharedR, std::move(Obj), MemMgr, std::move(Err));
|
2018-09-25 22:57:44 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 16:14:18 -07:00
|
|
|
void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
|
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
|
|
|
|
assert(llvm::none_of(EventListeners,
|
|
|
|
|
[&](JITEventListener *O) { return O == &L; }) &&
|
|
|
|
|
"Listener has already been registered");
|
|
|
|
|
EventListeners.push_back(&L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTDyldObjectLinkingLayer::unregisterJITEventListener(JITEventListener &L) {
|
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
|
|
|
|
auto I = llvm::find(EventListeners, &L);
|
|
|
|
|
assert(I != EventListeners.end() && "Listener not registered");
|
|
|
|
|
EventListeners.erase(I);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 22:56:10 +00:00
|
|
|
Error RTDyldObjectLinkingLayer::onObjLoad(
|
2020-03-19 16:14:18 -07:00
|
|
|
VModuleKey K, MaterializationResponsibility &R,
|
|
|
|
|
const object::ObjectFile &Obj, RuntimeDyld::MemoryManager *MemMgr,
|
2018-09-25 22:57:44 +00:00
|
|
|
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
|
|
|
|
|
std::map<StringRef, JITEvaluatedSymbol> Resolved,
|
|
|
|
|
std::set<StringRef> &InternalSymbols) {
|
|
|
|
|
SymbolFlagsMap ExtraSymbolsToClaim;
|
|
|
|
|
SymbolMap Symbols;
|
2020-01-17 14:48:48 -08:00
|
|
|
|
|
|
|
|
// Hack to support COFF constant pool comdats introduced during compilation:
|
|
|
|
|
// (See http://llvm.org/PR40074)
|
|
|
|
|
if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
|
|
|
|
|
auto &ES = getExecutionSession();
|
|
|
|
|
|
|
|
|
|
// For all resolved symbols that are not already in the responsibilty set:
|
|
|
|
|
// check whether the symbol is in a comdat section and if so mark it as
|
|
|
|
|
// weak.
|
|
|
|
|
for (auto &Sym : COFFObj->symbols()) {
|
2020-04-10 20:24:21 +08:00
|
|
|
// getFlags() on COFF symbols can't fail.
|
|
|
|
|
uint32_t SymFlags = cantFail(Sym.getFlags());
|
|
|
|
|
if (SymFlags & object::BasicSymbolRef::SF_Undefined)
|
2020-01-17 14:48:48 -08:00
|
|
|
continue;
|
|
|
|
|
auto Name = Sym.getName();
|
|
|
|
|
if (!Name)
|
|
|
|
|
return Name.takeError();
|
|
|
|
|
auto I = Resolved.find(*Name);
|
|
|
|
|
|
|
|
|
|
// Skip unresolved symbols, internal symbols, and symbols that are
|
|
|
|
|
// already in the responsibility set.
|
|
|
|
|
if (I == Resolved.end() || InternalSymbols.count(*Name) ||
|
|
|
|
|
R.getSymbols().count(ES.intern(*Name)))
|
|
|
|
|
continue;
|
|
|
|
|
auto Sec = Sym.getSection();
|
|
|
|
|
if (!Sec)
|
|
|
|
|
return Sec.takeError();
|
|
|
|
|
if (*Sec == COFFObj->section_end())
|
|
|
|
|
continue;
|
|
|
|
|
auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
|
|
|
|
|
if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
|
|
|
|
|
I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 22:57:44 +00:00
|
|
|
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;
|
|
|
|
|
|
2018-09-30 23:18:24 +00:00
|
|
|
auto InternedName = getExecutionSession().intern(KV.first);
|
2018-09-25 22:57:44 +00:00
|
|
|
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())
|
2019-05-28 23:35:44 +00:00
|
|
|
Flags = I->second;
|
2018-09-25 22:57:44 +00:00
|
|
|
else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
|
|
|
|
|
ExtraSymbolsToClaim[InternedName] = Flags;
|
2018-06-18 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-25 22:57:44 +00:00
|
|
|
Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
|
2018-05-21 23:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-17 14:48:48 -08:00
|
|
|
if (!ExtraSymbolsToClaim.empty()) {
|
2018-09-25 22:57:44 +00:00
|
|
|
if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
|
|
|
|
|
return Err;
|
|
|
|
|
|
2020-01-17 14:48:48 -08:00
|
|
|
// If we claimed responsibility for any weak symbols but were rejected then
|
|
|
|
|
// we need to remove them from the resolved set.
|
|
|
|
|
for (auto &KV : ExtraSymbolsToClaim)
|
|
|
|
|
if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
|
|
|
|
|
Symbols.erase(KV.first);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 20:37:31 +00:00
|
|
|
if (auto Err = R.notifyResolved(Symbols)) {
|
|
|
|
|
R.failMaterialization();
|
|
|
|
|
return Err;
|
|
|
|
|
}
|
2018-09-25 22:57:44 +00:00
|
|
|
|
2018-05-21 23:45:40 +00:00
|
|
|
if (NotifyLoaded)
|
2018-09-25 22:57:44 +00:00
|
|
|
NotifyLoaded(K, Obj, *LoadedObjInfo);
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2020-03-19 16:14:18 -07:00
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
|
|
|
|
assert(!LoadedObjInfos.count(MemMgr) && "Duplicate loaded info for MemMgr");
|
|
|
|
|
LoadedObjInfos[MemMgr] = std::move(LoadedObjInfo);
|
|
|
|
|
|
2018-09-25 22:57:44 +00:00
|
|
|
return Error::success();
|
|
|
|
|
}
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2019-05-01 22:40:23 +00:00
|
|
|
void RTDyldObjectLinkingLayer::onObjEmit(
|
2020-03-19 16:14:18 -07:00
|
|
|
VModuleKey K, MaterializationResponsibility &R,
|
|
|
|
|
object::OwningBinary<object::ObjectFile> O,
|
|
|
|
|
RuntimeDyld::MemoryManager *MemMgr, Error Err) {
|
2018-09-25 22:57:44 +00:00
|
|
|
if (Err) {
|
|
|
|
|
getExecutionSession().reportError(std::move(Err));
|
2018-05-21 23:45:40 +00:00
|
|
|
R.failMaterialization();
|
2018-06-18 18:01:43 +00:00
|
|
|
return;
|
2018-05-21 23:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-23 20:37:31 +00:00
|
|
|
if (auto Err = R.notifyEmitted()) {
|
|
|
|
|
getExecutionSession().reportError(std::move(Err));
|
|
|
|
|
R.failMaterialization();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-21 23:45:40 +00:00
|
|
|
|
2020-03-19 16:14:18 -07:00
|
|
|
std::unique_ptr<object::ObjectFile> Obj;
|
|
|
|
|
std::unique_ptr<MemoryBuffer> ObjBuffer;
|
|
|
|
|
std::tie(Obj, ObjBuffer) = O.takeBinary();
|
|
|
|
|
|
|
|
|
|
// Run EventListener notifyLoaded callbacks.
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
|
|
|
|
|
auto LOIItr = LoadedObjInfos.find(MemMgr);
|
|
|
|
|
assert(LOIItr != LoadedObjInfos.end() && "LoadedObjInfo missing");
|
|
|
|
|
for (auto *L : EventListeners)
|
|
|
|
|
L->notifyObjectLoaded(
|
|
|
|
|
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr)), *Obj,
|
|
|
|
|
*LOIItr->second);
|
|
|
|
|
LoadedObjInfos.erase(MemMgr);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-18 02:06:18 +00:00
|
|
|
if (NotifyEmitted)
|
2019-05-01 22:40:23 +00:00
|
|
|
NotifyEmitted(K, std::move(ObjBuffer));
|
2018-05-21 23:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 16:40:52 +00:00
|
|
|
LegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
|
|
|
|
|
ExecutionSession &ES, ResourcesGetter GetResources,
|
|
|
|
|
NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
|
|
|
|
|
NotifyFreedFtor NotifyFreed)
|
|
|
|
|
: ES(ES), GetResources(std::move(GetResources)),
|
|
|
|
|
NotifyLoaded(std::move(NotifyLoaded)),
|
|
|
|
|
NotifyFinalized(std::move(NotifyFinalized)),
|
|
|
|
|
NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
|
|
|
|
|
|
2018-05-21 23:45:40 +00:00
|
|
|
} // End namespace orc.
|
|
|
|
|
} // End namespace llvm.
|