1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/Object/COFF.h"
namespace {
using namespace llvm;
using namespace llvm::orc;
class JITDylibSearchOrderResolver : public JITSymbolResolver {
public:
JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
auto &ES = MR.getTargetJITDylib().getExecutionSession();
SymbolLookupSet InternedSymbols;
// Intern the requested symbols: lookup takes interned strings.
for (auto &S : Symbols)
InternedSymbols.add(ES.intern(S));
// Build an OnResolve callback to unwrap the interned strings and pass them
// to the OnResolved callback.
auto OnResolvedWithUnwrap =
[OnResolved = std::move(OnResolved)](
Expected<SymbolMap> InternedResult) mutable {
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.
auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
MR.addDependenciesForAll(Deps);
};
JITDylibSearchOrder LinkOrder;
MR.getTargetJITDylib().withLinkOrderDo(
[&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
RegisterDependencies);
}
Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
LookupSet Result;
for (auto &KV : MR.getSymbols()) {
if (Symbols.count(*KV.first))
Result.insert(*KV.first);
}
return Result;
}
private:
MaterializationResponsibility &MR;
};
} // end anonymous namespace
namespace llvm {
namespace orc {
char RTDyldObjectLinkingLayer::ID;
using BaseT = RTTIExtends<RTDyldObjectLinkingLayer, ObjectLayer>;
RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
: BaseT(ES), GetMemoryManager(GetMemoryManager) {
ES.registerResourceManager(*this);
}
RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
}
void RTDyldObjectLinkingLayer::emit(
std::unique_ptr<MaterializationResponsibility> R,
std::unique_ptr<MemoryBuffer> O) {
assert(O && "Object must not be null");
auto &ES = getExecutionSession();
auto Obj = object::ObjectFile::createObjectFile(*O);
if (!Obj) {
getExecutionSession().reportError(Obj.takeError());
R->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()) {
// Skip file symbols.
if (auto SymType = Sym.getType()) {
if (*SymType == object::SymbolRef::ST_File)
continue;
} else {
ES.reportError(SymType.takeError());
R->failMaterialization();
return;
}
Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
if (!SymFlagsOrErr) {
// TODO: Test this error.
ES.reportError(SymFlagsOrErr.takeError());
R->failMaterialization();
return;
}
// Don't include symbols that aren't global.
if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
if (auto SymName = Sym.getName())
InternalSymbols->insert(*SymName);
else {
ES.reportError(SymName.takeError());
R->failMaterialization();
return;
}
}
}
}
auto MemMgr = GetMemoryManager();
auto &MemMgrRef = *MemMgr;
// Switch to shared ownership of MR so that it can be captured by both
// lambdas below.
std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
JITDylibSearchOrderResolver Resolver(*SharedR);
jitLinkForORC(
object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
MemMgrRef, Resolver, ProcessAllSections,
[this, SharedR, &MemMgrRef, InternalSymbols](
const object::ObjectFile &Obj,
RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
ResolvedSymbols, *InternalSymbols);
},
[this, SharedR, MemMgr = std::move(MemMgr)](
object::OwningBinary<object::ObjectFile> Obj,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
Error Err) mutable {
onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
std::move(LoadedObjInfo), std::move(Err));
});
}
void RTDyldObjectLinkingLayer::registerJITEventListener(JITEventListener &L) {
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
assert(!llvm::is_contained(EventListeners, &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);
}
Error RTDyldObjectLinkingLayer::onObjLoad(
MaterializationResponsibility &R, const object::ObjectFile &Obj,
RuntimeDyld::MemoryManager &MemMgr,
RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
std::map<StringRef, JITEvaluatedSymbol> Resolved,
std::set<StringRef> &InternalSymbols) {
SymbolFlagsMap ExtraSymbolsToClaim;
SymbolMap Symbols;
// 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()) {
// getFlags() on COFF symbols can't fail.
uint32_t SymFlags = cantFail(Sym.getFlags());
if (SymFlags & object::BasicSymbolRef::SF_Undefined)
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);
}
}
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().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 = I->second;
else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
ExtraSymbolsToClaim[InternedName] = Flags;
}
Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
}
if (!ExtraSymbolsToClaim.empty()) {
if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
return Err;
// 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);
}
if (auto Err = R.notifyResolved(Symbols)) {
R.failMaterialization();
return Err;
}
if (NotifyLoaded)
NotifyLoaded(R, Obj, LoadedObjInfo);
return Error::success();
}
void RTDyldObjectLinkingLayer::onObjEmit(
MaterializationResponsibility &R,
object::OwningBinary<object::ObjectFile> O,
std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
if (Err) {
getExecutionSession().reportError(std::move(Err));
R.failMaterialization();
return;
}
if (auto Err = R.notifyEmitted()) {
getExecutionSession().reportError(std::move(Err));
R.failMaterialization();
return;
}
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);
for (auto *L : EventListeners)
L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
*LoadedObjInfo);
}
if (NotifyEmitted)
NotifyEmitted(R, std::move(ObjBuffer));
if (auto Err = R.withResourceKeyDo(
[&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
getExecutionSession().reportError(std::move(Err));
R.failMaterialization();
}
}
Error RTDyldObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
std::vector<MemoryManagerUP> MemMgrsToRemove;
getExecutionSession().runSessionLocked([&] {
auto I = MemMgrs.find(K);
if (I != MemMgrs.end()) {
std::swap(MemMgrsToRemove, I->second);
MemMgrs.erase(I);
}
});
{
std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
for (auto &MemMgr : MemMgrsToRemove) {
for (auto *L : EventListeners)
L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
MemMgr->deregisterEHFrames();
}
}
return Error::success();
}
void RTDyldObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
ResourceKey SrcKey) {
auto I = MemMgrs.find(SrcKey);
if (I != MemMgrs.end()) {
auto &SrcMemMgrs = I->second;
auto &DstMemMgrs = MemMgrs[DstKey];
DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
for (auto &MemMgr : SrcMemMgrs)
DstMemMgrs.push_back(std::move(MemMgr));
// Erase SrcKey entry using value rather than iterator I: I may have been
// invalidated when we looked up DstKey.
MemMgrs.erase(SrcKey);
}
}
} // End namespace orc.
} // End namespace llvm.
|