LLVM  7.0.0
RTDyldObjectLinkingLayer.h
Go to the documentation of this file.
1 //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===//
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 // Contains the definition for an RTDyld-based, in-process object linking layer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/Error.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <functional>
30 #include <list>
31 #include <memory>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace llvm {
37 namespace orc {
38 
40 public:
41  /// Functor for receiving object-loaded notifications.
42  using NotifyLoadedFunction =
45 
46  /// Functor for receiving finalization notifications.
47  using NotifyFinalizedFunction = std::function<void(VModuleKey)>;
48 
50  std::function<std::shared_ptr<RuntimeDyld::MemoryManager>(VModuleKey)>;
51 
52  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
53  /// and NotifyFinalized functors.
55  ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
58 
59  /// Emit the object.
61  std::unique_ptr<MemoryBuffer> O) override;
62 
63  /// Map section addresses for the object associated with the
64  /// VModuleKey K.
65  void mapSectionAddress(VModuleKey K, const void *LocalAddress,
66  JITTargetAddress TargetAddr) const;
67 
68  /// Set the 'ProcessAllSections' flag.
69  ///
70  /// If set to true, all sections in each object file will be allocated using
71  /// the memory manager, rather than just the sections required for execution.
72  ///
73  /// This is kludgy, and may be removed in the future.
74  void setProcessAllSections(bool ProcessAllSections) {
75  this->ProcessAllSections = ProcessAllSections;
76  }
77 
78 private:
79  mutable std::mutex RTDyldLayerMutex;
80  GetMemoryManagerFunction GetMemoryManager;
81  NotifyLoadedFunction NotifyLoaded;
82  NotifyFinalizedFunction NotifyFinalized;
83  bool ProcessAllSections;
84  std::map<VModuleKey, RuntimeDyld *> ActiveRTDylds;
85  std::map<VModuleKey, std::shared_ptr<RuntimeDyld::MemoryManager>> MemMgrs;
86 };
87 
89 public:
90  using ObjectPtr = std::unique_ptr<MemoryBuffer>;
91 
92 protected:
93 
94  /// Holds an object to be allocated/linked as a unit in the JIT.
95  ///
96  /// An instance of this class will be created for each object added
97  /// via JITObjectLayer::addObject. Deleting the instance (via
98  /// removeObject) frees its memory, removing all symbol definitions that
99  /// had been provided by this instance. Higher level layers are responsible
100  /// for taking any action required to handle the missing symbols.
101  class LinkedObject {
102  public:
103  LinkedObject() = default;
104  LinkedObject(const LinkedObject&) = delete;
105  void operator=(const LinkedObject&) = delete;
106  virtual ~LinkedObject() = default;
107 
108  virtual Error finalize() = 0;
109 
111  getSymbolMaterializer(std::string Name) = 0;
112 
113  virtual void mapSectionAddress(const void *LocalAddress,
114  JITTargetAddress TargetAddr) const = 0;
115 
116  JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly) {
117  auto SymEntry = SymbolTable.find(Name);
118  if (SymEntry == SymbolTable.end())
119  return nullptr;
120  if (!SymEntry->second.getFlags().isExported() && ExportedSymbolsOnly)
121  return nullptr;
122  if (!Finalized)
123  return JITSymbol(getSymbolMaterializer(Name),
124  SymEntry->second.getFlags());
125  return JITSymbol(SymEntry->second);
126  }
127 
128  protected:
130  bool Finalized = false;
131  };
132 };
133 
134 /// Bare bones object linking layer.
135 ///
136 /// This class is intended to be used as the base layer for a JIT. It allows
137 /// object files to be loaded into memory, linked, and the addresses of their
138 /// symbols queried. All objects added to this layer can see each other's
139 /// symbols.
141 public:
142 
144 
145  /// Functor for receiving object-loaded notifications.
146  using NotifyLoadedFtor =
149 
150  /// Functor for receiving finalization notifications.
151  using NotifyFinalizedFtor =
154 
155  /// Functor for receiving deallocation notifications.
156  using NotifyFreedFtor = std::function<void(VModuleKey, const object::ObjectFile &Obj)>;
157 
158 private:
160 
161  template <typename MemoryManagerPtrT>
162  class ConcreteLinkedObject : public LinkedObject {
163  public:
164  ConcreteLinkedObject(RTDyldObjectLinkingLayer &Parent, VModuleKey K,
165  OwnedObject Obj, MemoryManagerPtrT MemMgr,
166  std::shared_ptr<SymbolResolver> Resolver,
167  bool ProcessAllSections)
168  : K(std::move(K)),
169  Parent(Parent),
170  MemMgr(std::move(MemMgr)),
171  PFC(llvm::make_unique<PreFinalizeContents>(
172  std::move(Obj), std::move(Resolver),
173  ProcessAllSections)) {
174  buildInitialSymbolTable(PFC->Obj);
175  }
176 
177  ~ConcreteLinkedObject() override {
178  if (this->Parent.NotifyFreed)
179  this->Parent.NotifyFreed(K, *ObjForNotify.getBinary());
180 
181  MemMgr->deregisterEHFrames();
182  }
183 
184  Error finalize() override {
185  assert(PFC && "mapSectionAddress called on finalized LinkedObject");
186 
187  JITSymbolResolverAdapter ResolverAdapter(Parent.ES, *PFC->Resolver,
188  nullptr);
189  PFC->RTDyld = llvm::make_unique<RuntimeDyld>(*MemMgr, ResolverAdapter);
190  PFC->RTDyld->setProcessAllSections(PFC->ProcessAllSections);
191 
192  Finalized = true;
193 
194  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info =
195  PFC->RTDyld->loadObject(*PFC->Obj.getBinary());
196 
197  // Copy the symbol table out of the RuntimeDyld instance.
198  {
199  auto SymTab = PFC->RTDyld->getSymbolTable();
200  for (auto &KV : SymTab)
201  SymbolTable[KV.first] = KV.second;
202  }
203 
204  if (Parent.NotifyLoaded)
205  Parent.NotifyLoaded(K, *PFC->Obj.getBinary(), *Info);
206 
207  PFC->RTDyld->finalizeWithMemoryManagerLocking();
208 
209  if (PFC->RTDyld->hasError())
210  return make_error<StringError>(PFC->RTDyld->getErrorString(),
212 
213  if (Parent.NotifyFinalized)
214  Parent.NotifyFinalized(K, *PFC->Obj.getBinary(), *Info);
215 
216  // Release resources.
217  if (this->Parent.NotifyFreed)
218  ObjForNotify = std::move(PFC->Obj); // needed for callback
219  PFC = nullptr;
220  return Error::success();
221  }
222 
223  JITSymbol::GetAddressFtor getSymbolMaterializer(std::string Name) override {
224  return [this, Name]() -> Expected<JITTargetAddress> {
225  // The symbol may be materialized between the creation of this lambda
226  // and its execution, so we need to double check.
227  if (!this->Finalized)
228  if (auto Err = this->finalize())
229  return std::move(Err);
230  return this->getSymbol(Name, false).getAddress();
231  };
232  }
233 
234  void mapSectionAddress(const void *LocalAddress,
235  JITTargetAddress TargetAddr) const override {
236  assert(PFC && "mapSectionAddress called on finalized LinkedObject");
237  assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObject");
238  PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr);
239  }
240 
241  private:
242  void buildInitialSymbolTable(const OwnedObject &Obj) {
243  for (auto &Symbol : Obj.getBinary()->symbols()) {
244  if (Symbol.getFlags() & object::SymbolRef::SF_Undefined)
245  continue;
247  // FIXME: Raise an error for bad symbols.
248  if (!SymbolName) {
249  consumeError(SymbolName.takeError());
250  continue;
251  }
253  SymbolTable.insert(
254  std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags)));
255  }
256  }
257 
258  // Contains the information needed prior to finalization: the object files,
259  // memory manager, resolver, and flags needed for RuntimeDyld.
260  struct PreFinalizeContents {
261  PreFinalizeContents(OwnedObject Obj,
262  std::shared_ptr<SymbolResolver> Resolver,
263  bool ProcessAllSections)
264  : Obj(std::move(Obj)),
265  Resolver(std::move(Resolver)),
266  ProcessAllSections(ProcessAllSections) {}
267 
268  OwnedObject Obj;
269  std::shared_ptr<SymbolResolver> Resolver;
270  bool ProcessAllSections;
271  std::unique_ptr<RuntimeDyld> RTDyld;
272  };
273 
274  VModuleKey K;
275  RTDyldObjectLinkingLayer &Parent;
276  MemoryManagerPtrT MemMgr;
277  OwnedObject ObjForNotify;
278  std::unique_ptr<PreFinalizeContents> PFC;
279  };
280 
281  template <typename MemoryManagerPtrT>
282  std::unique_ptr<ConcreteLinkedObject<MemoryManagerPtrT>>
283  createLinkedObject(RTDyldObjectLinkingLayer &Parent, VModuleKey K,
284  OwnedObject Obj, MemoryManagerPtrT MemMgr,
285  std::shared_ptr<SymbolResolver> Resolver,
286  bool ProcessAllSections) {
287  using LOS = ConcreteLinkedObject<MemoryManagerPtrT>;
288  return llvm::make_unique<LOS>(Parent, std::move(K), std::move(Obj),
289  std::move(MemMgr), std::move(Resolver),
290  ProcessAllSections);
291  }
292 
293 public:
294  struct Resources {
295  std::shared_ptr<RuntimeDyld::MemoryManager> MemMgr;
296  std::shared_ptr<SymbolResolver> Resolver;
297  };
298 
299  using ResourcesGetter = std::function<Resources(VModuleKey)>;
300 
301  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
302  /// and NotifyFinalized functors.
304  ExecutionSession &ES, ResourcesGetter GetResources,
305  NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(),
306  NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor(),
307  NotifyFreedFtor NotifyFreed = NotifyFreedFtor())
308  : ES(ES), GetResources(std::move(GetResources)),
309  NotifyLoaded(std::move(NotifyLoaded)),
310  NotifyFinalized(std::move(NotifyFinalized)),
311  NotifyFreed(std::move(NotifyFreed)),
312  ProcessAllSections(false) {
313  }
314 
315  /// Set the 'ProcessAllSections' flag.
316  ///
317  /// If set to true, all sections in each object file will be allocated using
318  /// the memory manager, rather than just the sections required for execution.
319  ///
320  /// This is kludgy, and may be removed in the future.
321  void setProcessAllSections(bool ProcessAllSections) {
322  this->ProcessAllSections = ProcessAllSections;
323  }
324 
325  /// Add an object to the JIT.
327 
328  auto Obj =
329  object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
330  if (!Obj)
331  return Obj.takeError();
332 
333  assert(!LinkedObjects.count(K) && "VModuleKey already in use");
334 
335  auto R = GetResources(K);
336 
337  LinkedObjects[K] = createLinkedObject(
338  *this, K, OwnedObject(std::move(*Obj), std::move(ObjBuffer)),
339  std::move(R.MemMgr), std::move(R.Resolver), ProcessAllSections);
340 
341  return Error::success();
342  }
343 
344  /// Remove the object associated with VModuleKey K.
345  ///
346  /// All memory allocated for the object will be freed, and the sections and
347  /// symbols it provided will no longer be available. No attempt is made to
348  /// re-emit the missing symbols, and any use of these symbols (directly or
349  /// indirectly) will result in undefined behavior. If dependence tracking is
350  /// required to detect or resolve such issues it should be added at a higher
351  /// layer.
353  assert(LinkedObjects.count(K) && "VModuleKey not associated with object");
354  // How do we invalidate the symbols in H?
355  LinkedObjects.erase(K);
356  return Error::success();
357  }
358 
359  /// Search for the given named symbol.
360  /// @param Name The name of the symbol to search for.
361  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
362  /// @return A handle for the given named symbol, if it exists.
363  JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
364  for (auto &KV : LinkedObjects)
365  if (auto Sym = KV.second->getSymbol(Name, ExportedSymbolsOnly))
366  return Sym;
367  else if (auto Err = Sym.takeError())
368  return std::move(Err);
369 
370  return nullptr;
371  }
372 
373  /// Search for the given named symbol in the context of the loaded
374  /// object represented by the VModuleKey K.
375  /// @param K The VModuleKey for the object to search in.
376  /// @param Name The name of the symbol to search for.
377  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
378  /// @return A handle for the given named symbol, if it is found in the
379  /// given object.
381  bool ExportedSymbolsOnly) {
382  assert(LinkedObjects.count(K) && "VModuleKey not associated with object");
383  return LinkedObjects[K]->getSymbol(Name, ExportedSymbolsOnly);
384  }
385 
386  /// Map section addresses for the object associated with the
387  /// VModuleKey K.
388  void mapSectionAddress(VModuleKey K, const void *LocalAddress,
389  JITTargetAddress TargetAddr) {
390  assert(LinkedObjects.count(K) && "VModuleKey not associated with object");
391  LinkedObjects[K]->mapSectionAddress(LocalAddress, TargetAddr);
392  }
393 
394  /// Immediately emit and finalize the object represented by the given
395  /// VModuleKey.
396  /// @param K VModuleKey for object to emit/finalize.
398  assert(LinkedObjects.count(K) && "VModuleKey not associated with object");
399  return LinkedObjects[K]->finalize();
400  }
401 
402 private:
403  ExecutionSession &ES;
404 
405  std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects;
406  ResourcesGetter GetResources;
407  NotifyLoadedFtor NotifyLoaded;
408  NotifyFinalizedFtor NotifyFinalized;
409  NotifyFreedFtor NotifyFreed;
410  bool ProcessAllSections = false;
411 };
412 
413 } // end namespace orc
414 } // end namespace llvm
415 
416 #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:153
Information about the loaded object.
Definition: RuntimeDyld.h:69
Represents a symbol in the JIT.
Definition: JITSymbol.h:186
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
arc branch finalize
JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly)
Holds an object to be allocated/linked as a unit in the JIT.
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the object represented by the given VModuleKey.
Bare bones object linking layer.
void setProcessAllSections(bool ProcessAllSections)
Set the &#39;ProcessAllSections&#39; flag.
This class is the base class for all object file types.
Definition: ObjectFile.h:190
Error takeError()
Take ownership of the stored error.
Definition: Error.h:546
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Definition: BitVector.h:921
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
std::function< void(VModuleKey, const object::ObjectFile &Obj)> NotifyFreedFtor
Functor for receiving deallocation notifications.
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr)
Map section addresses for the object associated with the VModuleKey K.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:124
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFtor
Functor for receiving object-loaded notifications.
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:40
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:337
RTDyldObjectLinkingLayer2(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager, NotifyLoadedFunction NotifyLoaded=NotifyLoadedFunction(), NotifyFinalizedFunction NotifyFinalized=NotifyFinalizedFunction())
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyFinalized functors.
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const
Map section addresses for the object associated with the VModuleKey K.
void emit(MaterializationResponsibility R, VModuleKey K, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
RTDyldObjectLinkingLayer(ExecutionSession &ES, ResourcesGetter GetResources, NotifyLoadedFtor NotifyLoaded=NotifyLoadedFtor(), NotifyFinalizedFtor NotifyFinalized=NotifyFinalizedFtor(), NotifyFreedFtor NotifyFreed=NotifyFreedFtor())
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyFinalized functors.
static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:32
std::function< std::shared_ptr< RuntimeDyld::MemoryManager >(VModuleKey)> GetMemoryManagerFunction
std::function< Resources(VModuleKey)> ResourcesGetter
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:971
std::function< void(VModuleKey)> NotifyFinalizedFunction
Functor for receiving finalization notifications.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:322
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyFinalizedFtor
Functor for receiving finalization notifications.
An ExecutionSession represents a running JIT program.
Definition: Core.h:735
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
void setProcessAllSections(bool ProcessAllSections)
Set the &#39;ProcessAllSections&#39; flag.
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:157
JITSymbol findSymbolIn(VModuleKey K, StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol in the context of the loaded object represented by the VModuleKey K...
std::unique_ptr< MemoryBuffer > ObjectPtr
std::function< Expected< JITTargetAddress >()> GetAddressFtor
Definition: JITSymbol.h:188
symbol_iterator_range symbols() const
Definition: ObjectFile.h:270
std::shared_ptr< RuntimeDyld::MemoryManager > MemMgr
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
Error addObject(VModuleKey K, ObjectPtr ObjBuffer)
Add an object to the JIT.
Error removeObject(VModuleKey K)
Remove the object associated with VModuleKey K.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:86
std::function< void(VModuleKey, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFunction
Functor for receiving object-loaded notifications.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73