LLVM  12.0.0git
RTDyldObjectLinkingLayer.cpp
Go to the documentation of this file.
1 //===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/Object/COFF.h"
11 
12 namespace {
13 
14 using namespace llvm;
15 using namespace llvm::orc;
16 
17 class JITDylibSearchOrderResolver : public JITSymbolResolver {
18 public:
19  JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20 
21  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) {
22  auto &ES = MR.getTargetJITDylib().getExecutionSession();
23  SymbolLookupSet InternedSymbols;
24 
25  // Intern the requested symbols: lookup takes interned strings.
26  for (auto &S : Symbols)
27  InternedSymbols.add(ES.intern(S));
28 
29  // Build an OnResolve callback to unwrap the interned strings and pass them
30  // to the OnResolved callback.
31  auto OnResolvedWithUnwrap =
32  [OnResolved = std::move(OnResolved)](
33  Expected<SymbolMap> InternedResult) mutable {
34  if (!InternedResult) {
35  OnResolved(InternedResult.takeError());
36  return;
37  }
38 
39  LookupResult Result;
40  for (auto &KV : *InternedResult)
41  Result[*KV.first] = std::move(KV.second);
42  OnResolved(Result);
43  };
44 
45  // Register dependencies for all symbols contained in this set.
46  auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
47  MR.addDependenciesForAll(Deps);
48  };
49 
50  JITDylibSearchOrder LinkOrder;
51  MR.getTargetJITDylib().withLinkOrderDo(
52  [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
53  ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
54  SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
55  RegisterDependencies);
56  }
57 
58  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
59  LookupSet Result;
60 
61  for (auto &KV : MR.getSymbols()) {
62  if (Symbols.count(*KV.first))
63  Result.insert(*KV.first);
64  }
65 
66  return Result;
67  }
68 
69 private:
71 };
72 
73 } // end anonymous namespace
74 
75 namespace llvm {
76 namespace orc {
77 
79  ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80  : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
81 
83  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
84  for (auto &MemMgr : MemMgrs) {
85  for (auto *L : EventListeners)
86  L->notifyFreeingObject(
87  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr.get())));
88  MemMgr->deregisterEHFrames();
89  }
90 }
91 
93  std::unique_ptr<MemoryBuffer> O) {
94  assert(O && "Object must not be null");
95 
96  // This method launches an asynchronous link step that will fulfill our
97  // materialization responsibility. We need to switch R to be heap
98  // allocated before that happens so it can live as long as the asynchronous
99  // link needs it to (i.e. it must be able to outlive this method).
100  auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R));
101 
102  auto &ES = getExecutionSession();
103 
105 
106  if (!Obj) {
107  getExecutionSession().reportError(Obj.takeError());
108  SharedR->failMaterialization();
109  return;
110  }
111 
112  // Collect the internal symbols from the object file: We will need to
113  // filter these later.
114  auto InternalSymbols = std::make_shared<std::set<StringRef>>();
115  {
116  for (auto &Sym : (*Obj)->symbols()) {
117 
118  // Skip file symbols.
119  if (auto SymType = Sym.getType()) {
120  if (*SymType == object::SymbolRef::ST_File)
121  continue;
122  } else {
123  ES.reportError(SymType.takeError());
125  return;
126  }
127 
128  Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
129  if (!SymFlagsOrErr) {
130  // TODO: Test this error.
131  ES.reportError(SymFlagsOrErr.takeError());
133  return;
134  }
135 
136  // Don't include symbols that aren't global.
137  if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
138  if (auto SymName = Sym.getName())
139  InternalSymbols->insert(*SymName);
140  else {
141  ES.reportError(SymName.takeError());
143  return;
144  }
145  }
146  }
147  }
148 
149  auto K = R.getVModuleKey();
150  RuntimeDyld::MemoryManager *MemMgr = nullptr;
151 
152  // Create a record a memory manager for this object.
153  {
154  auto Tmp = GetMemoryManager();
155  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
156  MemMgrs.push_back(std::move(Tmp));
157  MemMgr = MemMgrs.back().get();
158  }
159 
160  JITDylibSearchOrderResolver Resolver(*SharedR);
161 
163  object::OwningBinary<object::ObjectFile>(std::move(*Obj), std::move(O)),
164  *MemMgr, Resolver, ProcessAllSections,
165  [this, K, SharedR, MemMgr, InternalSymbols](
166  const object::ObjectFile &Obj,
167  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
168  std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
169  return onObjLoad(K, *SharedR, Obj, MemMgr, std::move(LoadedObjInfo),
170  ResolvedSymbols, *InternalSymbols);
171  },
172  [this, K, SharedR, MemMgr](object::OwningBinary<object::ObjectFile> Obj,
173  Error Err) mutable {
174  onObjEmit(K, *SharedR, std::move(Obj), MemMgr, std::move(Err));
175  });
176 }
177 
179  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
180  assert(llvm::none_of(EventListeners,
181  [&](JITEventListener *O) { return O == &L; }) &&
182  "Listener has already been registered");
183  EventListeners.push_back(&L);
184 }
185 
187  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
188  auto I = llvm::find(EventListeners, &L);
189  assert(I != EventListeners.end() && "Listener not registered");
190  EventListeners.erase(I);
191 }
192 
193 Error RTDyldObjectLinkingLayer::onObjLoad(
196  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
197  std::map<StringRef, JITEvaluatedSymbol> Resolved,
198  std::set<StringRef> &InternalSymbols) {
199  SymbolFlagsMap ExtraSymbolsToClaim;
200  SymbolMap Symbols;
201 
202  // Hack to support COFF constant pool comdats introduced during compilation:
203  // (See http://llvm.org/PR40074)
204  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
205  auto &ES = getExecutionSession();
206 
207  // For all resolved symbols that are not already in the responsibilty set:
208  // check whether the symbol is in a comdat section and if so mark it as
209  // weak.
210  for (auto &Sym : COFFObj->symbols()) {
211  // getFlags() on COFF symbols can't fail.
212  uint32_t SymFlags = cantFail(Sym.getFlags());
214  continue;
215  auto Name = Sym.getName();
216  if (!Name)
217  return Name.takeError();
218  auto I = Resolved.find(*Name);
219 
220  // Skip unresolved symbols, internal symbols, and symbols that are
221  // already in the responsibility set.
222  if (I == Resolved.end() || InternalSymbols.count(*Name) ||
223  R.getSymbols().count(ES.intern(*Name)))
224  continue;
225  auto Sec = Sym.getSection();
226  if (!Sec)
227  return Sec.takeError();
228  if (*Sec == COFFObj->section_end())
229  continue;
230  auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
231  if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
232  I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
233  }
234  }
235 
236  for (auto &KV : Resolved) {
237  // Scan the symbols and add them to the Symbols map for resolution.
238 
239  // We never claim internal symbols.
240  if (InternalSymbols.count(KV.first))
241  continue;
242 
243  auto InternedName = getExecutionSession().intern(KV.first);
244  auto Flags = KV.second.getFlags();
245 
246  // Override object flags and claim responsibility for symbols if
247  // requested.
248  if (OverrideObjectFlags || AutoClaimObjectSymbols) {
249  auto I = R.getSymbols().find(InternedName);
250 
251  if (OverrideObjectFlags && I != R.getSymbols().end())
252  Flags = I->second;
253  else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
254  ExtraSymbolsToClaim[InternedName] = Flags;
255  }
256 
257  Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
258  }
259 
260  if (!ExtraSymbolsToClaim.empty()) {
261  if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
262  return Err;
263 
264  // If we claimed responsibility for any weak symbols but were rejected then
265  // we need to remove them from the resolved set.
266  for (auto &KV : ExtraSymbolsToClaim)
267  if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
268  Symbols.erase(KV.first);
269  }
270 
271  if (auto Err = R.notifyResolved(Symbols)) {
273  return Err;
274  }
275 
276  if (NotifyLoaded)
277  NotifyLoaded(K, Obj, *LoadedObjInfo);
278 
279  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
280  assert(!LoadedObjInfos.count(MemMgr) && "Duplicate loaded info for MemMgr");
281  LoadedObjInfos[MemMgr] = std::move(LoadedObjInfo);
282 
283  return Error::success();
284 }
285 
286 void RTDyldObjectLinkingLayer::onObjEmit(
289  RuntimeDyld::MemoryManager *MemMgr, Error Err) {
290  if (Err) {
291  getExecutionSession().reportError(std::move(Err));
293  return;
294  }
295 
296  if (auto Err = R.notifyEmitted()) {
297  getExecutionSession().reportError(std::move(Err));
299  return;
300  }
301 
302  std::unique_ptr<object::ObjectFile> Obj;
303  std::unique_ptr<MemoryBuffer> ObjBuffer;
304  std::tie(Obj, ObjBuffer) = O.takeBinary();
305 
306  // Run EventListener notifyLoaded callbacks.
307  {
308  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
309  auto LOIItr = LoadedObjInfos.find(MemMgr);
310  assert(LOIItr != LoadedObjInfos.end() && "LoadedObjInfo missing");
311  for (auto *L : EventListeners)
312  L->notifyObjectLoaded(
313  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(MemMgr)), *Obj,
314  *LOIItr->second);
315  LoadedObjInfos.erase(MemMgr);
316  }
317 
318  if (NotifyEmitted)
319  NotifyEmitted(K, std::move(ObjBuffer));
320 }
321 
323  ExecutionSession &ES, ResourcesGetter GetResources,
324  NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
325  NotifyFreedFtor NotifyFreed)
326  : ES(ES), GetResources(std::move(GetResources)),
327  NotifyLoaded(std::move(NotifyLoaded)),
328  NotifyFinalized(std::move(NotifyFinalized)),
329  NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
330 
331 } // End namespace orc.
332 } // End namespace llvm.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:185
Queried, materialization begun.
Error notifyEmitted()
Notifies the target JITDylib (and any pending queries on that JITDylib) that all symbols covered by t...
Definition: Core.cpp:213
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Definition: InlineInfo.cpp:108
static sys::Mutex Lock
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup...
Definition: Core.h:92
This class is the base class for all object file types.
Definition: ObjectFile.h:225
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
void jitLinkForORC(object::OwningBinary< object::ObjectFile > O, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(const object::ObjectFile &Obj, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(object::OwningBinary< object::ObjectFile >, Error)> OnEmitted)
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1107
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1505
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:410
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:134
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
LegacyRTDyldObjectLinkingLayer(ORCv1DeprecationAcknowledgement, ExecutionSession &ES, ResourcesGetter GetResources, NotifyLoadedFtor NotifyLoaded=NotifyLoadedFtor(), NotifyFinalizedFtor NotifyFinalized=NotifyFinalizedFtor(), NotifyFreedFtor NotifyFreed=NotifyFreedFtor())
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
Definition: Core.h:427
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1870
Symbol resolution interface.
Definition: JITSymbol.h:371
void add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:161
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
Error notifyResolved(const SymbolMap &Symbols)
Notifies the target JITDylib that the given symbols have been resolved.
Definition: Core.cpp:193
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:219
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
std::function< std::unique_ptr< RuntimeDyld::MemoryManager >()> GetMemoryManagerFunction
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1511
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:113
An ExecutionSession represents a running JIT program.
Definition: Core.h:1088
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
Definition: Core.h:433
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1170
#define I(x, y, z)
Definition: MD5.cpp:59
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
Definition: Core.cpp:226
iterator end()
Definition: DenseMap.h:83
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:97
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void failMaterialization()
Notify all not-yet-emitted covered by this MaterializationResponsibility instance that an error has o...
Definition: Core.cpp:242
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:128
void emit(MaterializationResponsibility R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.