LLVM  14.0.0git
RTDyldObjectLinkingLayer.h
Go to the documentation of this file.
1 //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===//
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 //
9 // Contains the definition for an RTDyld-based, in-process object linking layer.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
15 
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Object/ObjectFile.h"
25 #include "llvm/Support/Error.h"
26 #include <algorithm>
27 #include <cassert>
28 #include <functional>
29 #include <list>
30 #include <memory>
31 #include <utility>
32 #include <vector>
33 
34 namespace llvm {
35 namespace orc {
36 
38  : public RTTIExtends<RTDyldObjectLinkingLayer, ObjectLayer>,
39  private ResourceManager {
40 public:
41  static char ID;
42 
43  /// Functor for receiving object-loaded notifications.
47 
48  /// Functor for receiving finalization notifications.
50  MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>;
51 
53  std::function<std::unique_ptr<RuntimeDyld::MemoryManager>()>;
54 
55  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
56  /// and NotifyEmitted functors.
58  GetMemoryManagerFunction GetMemoryManager);
59 
61 
62  /// Emit the object.
63  void emit(std::unique_ptr<MaterializationResponsibility> R,
64  std::unique_ptr<MemoryBuffer> O) override;
65 
66  /// Set the NotifyLoaded callback.
68  this->NotifyLoaded = std::move(NotifyLoaded);
69  return *this;
70  }
71 
72  /// Set the NotifyEmitted callback.
75  this->NotifyEmitted = std::move(NotifyEmitted);
76  return *this;
77  }
78 
79  /// Set the 'ProcessAllSections' flag.
80  ///
81  /// If set to true, all sections in each object file will be allocated using
82  /// the memory manager, rather than just the sections required for execution.
83  ///
84  /// This is kludgy, and may be removed in the future.
85  RTDyldObjectLinkingLayer &setProcessAllSections(bool ProcessAllSections) {
86  this->ProcessAllSections = ProcessAllSections;
87  return *this;
88  }
89 
90  /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags
91  /// returned by RuntimeDyld for any given object file with the flags supplied
92  /// by the MaterializationResponsibility instance. This is a workaround to
93  /// support symbol visibility in COFF, which does not use the libObject's
94  /// SF_Exported flag. Use only when generating / adding COFF object files.
95  ///
96  /// FIXME: We should be able to remove this if/when COFF properly tracks
97  /// exported symbols.
100  this->OverrideObjectFlags = OverrideObjectFlags;
101  return *this;
102  }
103 
104  /// If set, this RTDyldObjectLinkingLayer instance will claim responsibility
105  /// for any symbols provided by a given object file that were not already in
106  /// the MaterializationResponsibility instance. Setting this flag allows
107  /// higher-level program representations (e.g. LLVM IR) to be added based on
108  /// only a subset of the symbols they provide, without having to write
109  /// intervening layers to scan and add the additional symbols. This trades
110  /// diagnostic quality for convenience however: If all symbols are enumerated
111  /// up-front then clashes can be detected and reported early (and usually
112  /// deterministically). If this option is set, clashes for the additional
113  /// symbols may not be detected until late, and detection may depend on
114  /// the flow of control through JIT'd code. Use with care.
116  setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
117  this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
118  return *this;
119  }
120 
121  /// Register a JITEventListener.
123 
124  /// Unregister a JITEventListener.
126 
127 private:
128  using MemoryManagerUP = std::unique_ptr<RuntimeDyld::MemoryManager>;
129 
131  const object::ObjectFile &Obj,
133  RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
134  std::map<StringRef, JITEvaluatedSymbol> Resolved,
135  std::set<StringRef> &InternalSymbols);
136 
137  void onObjEmit(MaterializationResponsibility &R,
139  std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
140  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
141  Error Err);
142 
143  Error handleRemoveResources(ResourceKey K) override;
144  void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override;
145 
146  mutable std::mutex RTDyldLayerMutex;
147  GetMemoryManagerFunction GetMemoryManager;
148  NotifyLoadedFunction NotifyLoaded;
149  NotifyEmittedFunction NotifyEmitted;
150  bool ProcessAllSections = false;
151  bool OverrideObjectFlags = false;
152  bool AutoClaimObjectSymbols = false;
154  std::vector<JITEventListener *> EventListeners;
155 };
156 
157 } // end namespace orc
158 } // end namespace llvm
159 
160 #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
llvm::orc::ResourceKey
uintptr_t ResourceKey
Definition: Core.h:50
llvm::orc::RTDyldObjectLinkingLayer::NotifyLoadedFunction
std::function< void(MaterializationResponsibility &R, const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &)> NotifyLoadedFunction
Functor for receiving object-loaded notifications.
Definition: RTDyldObjectLinkingLayer.h:46
llvm::orc::MaterializationResponsibility
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:520
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
JITSymbol.h
llvm::orc::RTDyldObjectLinkingLayer
Definition: RTDyldObjectLinkingLayer.h:37
StringRef.h
Error.h
llvm::orc::RTDyldObjectLinkingLayer::registerJITEventListener
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
Definition: RTDyldObjectLinkingLayer.cpp:172
STLExtras.h
llvm::orc::RTDyldObjectLinkingLayer::emit
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
Definition: RTDyldObjectLinkingLayer.cpp:92
RuntimeDyld.h
Layer.h
llvm::orc::RTDyldObjectLinkingLayer::setAutoClaimResponsibilityForObjectSymbols
RTDyldObjectLinkingLayer & setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols)
If set, this RTDyldObjectLinkingLayer instance will claim responsibility for any symbols provided by ...
Definition: RTDyldObjectLinkingLayer.h:116
llvm::orc::RTDyldObjectLinkingLayer::setOverrideObjectFlagsWithResponsibilityFlags
RTDyldObjectLinkingLayer & setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags)
Instructs this RTDyldLinkingLayer2 instance to override the symbol flags returned by RuntimeDyld for ...
Definition: RTDyldObjectLinkingLayer.h:99
StringMap.h
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:65
llvm::orc::RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
Definition: RTDyldObjectLinkingLayer.cpp:82
llvm::orc::RTDyldObjectLinkingLayer::unregisterJITEventListener
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
Definition: RTDyldObjectLinkingLayer.cpp:179
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::orc::RTDyldObjectLinkingLayer::GetMemoryManagerFunction
std::function< std::unique_ptr< RuntimeDyld::MemoryManager >()> GetMemoryManagerFunction
Definition: RTDyldObjectLinkingLayer.h:53
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
ObjectFile.h
llvm::orc::RTDyldObjectLinkingLayer::setNotifyLoaded
RTDyldObjectLinkingLayer & setNotifyLoaded(NotifyLoadedFunction NotifyLoaded)
Set the NotifyLoaded callback.
Definition: RTDyldObjectLinkingLayer.h:67
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::orc::RTDyldObjectLinkingLayer::setNotifyEmitted
RTDyldObjectLinkingLayer & setNotifyEmitted(NotifyEmittedFunction NotifyEmitted)
Set the NotifyEmitted callback.
Definition: RTDyldObjectLinkingLayer.h:74
llvm::JITEventListener
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Definition: JITEventListener.h:41
llvm::orc::RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer
~RTDyldObjectLinkingLayer()
Definition: RTDyldObjectLinkingLayer.cpp:88
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::orc::ResourceManager
Listens for ResourceTracker operations.
Definition: Core.h:97
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::RuntimeDyld::LoadedObjectInfo
Information about the loaded object.
Definition: RuntimeDyld.h:69
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1275
llvm::orc::RTDyldObjectLinkingLayer::ID
static char ID
Definition: RTDyldObjectLinkingLayer.h:41
Core.h
llvm::orc::RTDyldObjectLinkingLayer::NotifyEmittedFunction
std::function< void(MaterializationResponsibility &R, std::unique_ptr< MemoryBuffer >)> NotifyEmittedFunction
Functor for receiving finalization notifications.
Definition: RTDyldObjectLinkingLayer.h:50
llvm::orc::RTDyldObjectLinkingLayer::setProcessAllSections
RTDyldObjectLinkingLayer & setProcessAllSections(bool ProcessAllSections)
Set the 'ProcessAllSections' flag.
Definition: RTDyldObjectLinkingLayer.h:85
JITEventListener.h
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92