blob: f82f5ecfed51494ffbc42125847cb0c6dda28453 [file] [log] [blame]
Lang Hames373f4622018-05-21 23:45:40 +00001//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
11
Lang Hamesfd0c1e712018-07-20 18:31:50 +000012namespace {
13
14using namespace llvm;
15using namespace llvm::orc;
16
Lang Hamesd5f56c52018-08-17 21:18:18 +000017class JITDylibSearchOrderResolver : public JITSymbolResolver {
Lang Hamesfd0c1e712018-07-20 18:31:50 +000018public:
Lang Hamesd5f56c52018-08-17 21:18:18 +000019 JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
Lang Hamesfd0c1e712018-07-20 18:31:50 +000020
21 Expected<LookupResult> lookup(const LookupSet &Symbols) {
Lang Hamesd5f56c52018-08-17 21:18:18 +000022 auto &ES = MR.getTargetJITDylib().getExecutionSession();
Lang Hamesfd0c1e712018-07-20 18:31:50 +000023 SymbolNameSet InternedSymbols;
24
25 for (auto &S : Symbols)
26 InternedSymbols.insert(ES.getSymbolStringPool().intern(S));
27
Lang Hamesa48d1082018-07-20 22:22:19 +000028 auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
Lang Hames960246d2018-07-21 00:12:05 +000029 MR.addDependenciesForAll(Deps);
Lang Hamesfd0c1e712018-07-20 18:31:50 +000030 };
31
Lang Hamesa48d1082018-07-20 22:22:19 +000032 auto InternedResult =
Lang Hamesd5f56c52018-08-17 21:18:18 +000033 MR.getTargetJITDylib().withSearchOrderDo([&](const JITDylibList &JDs) {
34 return ES.lookup(JDs, InternedSymbols, RegisterDependencies, false);
Lang Hamesa48d1082018-07-20 22:22:19 +000035 });
Lang Hamesfd0c1e712018-07-20 18:31:50 +000036
37 if (!InternedResult)
38 return InternedResult.takeError();
39
40 LookupResult Result;
41 for (auto &KV : *InternedResult)
42 Result[*KV.first] = std::move(KV.second);
43
44 return Result;
45 }
46
Lang Hames6cadc7c2018-08-28 21:18:05 +000047 Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
48 LookupSet Result;
Lang Hamesa48d1082018-07-20 22:22:19 +000049
Lang Hames6cadc7c2018-08-28 21:18:05 +000050 for (auto &KV : MR.getSymbols()) {
51 if (Symbols.count(*KV.first))
52 Result.insert(*KV.first);
53 }
Lang Hamesfd0c1e712018-07-20 18:31:50 +000054
55 return Result;
56 }
57
58private:
Lang Hamesfd0c1e712018-07-20 18:31:50 +000059 MaterializationResponsibility &MR;
60};
61
62} // end anonymous namespace
63
Lang Hames373f4622018-05-21 23:45:40 +000064namespace llvm {
65namespace orc {
66
67RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2(
Lang Hamesfd0c1e712018-07-20 18:31:50 +000068 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
Lang Hames76e21c92018-08-18 02:06:18 +000069 NotifyLoadedFunction NotifyLoaded, NotifyEmittedFunction NotifyEmitted)
Lang Hamesfd0c1e712018-07-20 18:31:50 +000070 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager),
Lang Hames373f4622018-05-21 23:45:40 +000071 NotifyLoaded(std::move(NotifyLoaded)),
Lang Hames76e21c92018-08-18 02:06:18 +000072 NotifyEmitted(std::move(NotifyEmitted)), ProcessAllSections(false) {}
Lang Hames373f4622018-05-21 23:45:40 +000073
74void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R,
75 VModuleKey K,
76 std::unique_ptr<MemoryBuffer> O) {
Lang Hames4caa2f72018-05-23 21:27:01 +000077 assert(O && "Object must not be null");
Lang Hames373f4622018-05-21 23:45:40 +000078
79 auto &ES = getExecutionSession();
80
81 auto ObjFile = object::ObjectFile::createObjectFile(*O);
82 if (!ObjFile) {
83 getExecutionSession().reportError(ObjFile.takeError());
84 R.failMaterialization();
85 }
86
Lang Hamesfd0c1e712018-07-20 18:31:50 +000087 auto MemoryManager = GetMemoryManager(K);
Lang Hames373f4622018-05-21 23:45:40 +000088
Lang Hamesd5f56c52018-08-17 21:18:18 +000089 JITDylibSearchOrderResolver Resolver(R);
Lang Hamesfd0c1e712018-07-20 18:31:50 +000090 auto RTDyld = llvm::make_unique<RuntimeDyld>(*MemoryManager, Resolver);
Lang Hames373f4622018-05-21 23:45:40 +000091 RTDyld->setProcessAllSections(ProcessAllSections);
92
93 {
94 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
95
96 assert(!ActiveRTDylds.count(K) &&
97 "An active RTDyld already exists for this key?");
98 ActiveRTDylds[K] = RTDyld.get();
99
100 assert(!MemMgrs.count(K) &&
101 "A memory manager already exists for this key?");
Lang Hamesfd0c1e712018-07-20 18:31:50 +0000102 MemMgrs[K] = std::move(MemoryManager);
Lang Hames373f4622018-05-21 23:45:40 +0000103 }
104
105 auto Info = RTDyld->loadObject(**ObjFile);
106
107 {
Lang Hames68c9b8d2018-06-18 18:01:43 +0000108 std::set<StringRef> InternalSymbols;
109 for (auto &Sym : (*ObjFile)->symbols()) {
110 if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
111 if (auto SymName = Sym.getName())
112 InternalSymbols.insert(*SymName);
113 else {
114 ES.reportError(SymName.takeError());
115 R.failMaterialization();
116 return;
117 }
118 }
119 }
120
Lang Hames373f4622018-05-21 23:45:40 +0000121 SymbolMap Symbols;
122 for (auto &KV : RTDyld->getSymbolTable())
Lang Hames68c9b8d2018-06-18 18:01:43 +0000123 if (!InternalSymbols.count(KV.first))
124 Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second;
125
Lang Hames373f4622018-05-21 23:45:40 +0000126 R.resolve(Symbols);
127 }
128
129 if (NotifyLoaded)
130 NotifyLoaded(K, **ObjFile, *Info);
131
132 RTDyld->finalizeWithMemoryManagerLocking();
133
134 {
135 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
136 ActiveRTDylds.erase(K);
137 }
138
139 if (RTDyld->hasError()) {
140 ES.reportError(make_error<StringError>(RTDyld->getErrorString(),
141 inconvertibleErrorCode()));
142 R.failMaterialization();
Lang Hames68c9b8d2018-06-18 18:01:43 +0000143 return;
Lang Hames373f4622018-05-21 23:45:40 +0000144 }
145
Lang Hames76e21c92018-08-18 02:06:18 +0000146 R.emit();
Lang Hames373f4622018-05-21 23:45:40 +0000147
Lang Hames76e21c92018-08-18 02:06:18 +0000148 if (NotifyEmitted)
149 NotifyEmitted(K);
Lang Hames373f4622018-05-21 23:45:40 +0000150}
151
152void RTDyldObjectLinkingLayer2::mapSectionAddress(
153 VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const {
154 std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
155 auto ActiveRTDyldItr = ActiveRTDylds.find(K);
156
157 assert(ActiveRTDyldItr != ActiveRTDylds.end() &&
158 "No active RTDyld instance found for key");
159 ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr);
160}
161
162} // End namespace orc.
163} // End namespace llvm.