1 //===-- ObjectLinkingLayer.h - JITLink-based jit linking layer --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Contains the definition for an JITLink-based, in-process object linking
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
21 #include "llvm/ExecutionEngine/JITSymbol.h"
22 #include "llvm/ExecutionEngine/Orc/Core.h"
23 #include "llvm/ExecutionEngine/Orc/Layer.h"
24 #include "llvm/Support/Error.h"
37 class EHFrameRegistrar;
38 } // namespace jitlink
46 class ObjectLinkingLayerJITLinkContext;
48 /// An ObjectLayer implementation built on JITLink.
50 /// Clients can use this class to add relocatable object files to an
51 /// ExecutionSession, and it typically serves as the base layer (underneath
52 /// a compiling layer like IRCompileLayer) for the rest of the JIT.
53 class ObjectLinkingLayer : public ObjectLayer {
54 friend class ObjectLinkingLayerJITLinkContext;
57 /// Plugin instances can be added to the ObjectLinkingLayer to receive
58 /// callbacks when code is loaded or emitted, and when JITLink is being
63 virtual void modifyPassConfig(MaterializationResponsibility &MR,
65 jitlink::PassConfiguration &Config) {}
66 virtual void notifyLoaded(MaterializationResponsibility &MR) {}
67 virtual Error notifyEmitted(MaterializationResponsibility &MR) {
68 return Error::success();
70 virtual Error notifyRemovingModule(VModuleKey K) {
71 return Error::success();
73 virtual Error notifyRemovingAllModules() { return Error::success(); }
76 /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
77 /// and NotifyEmitted functors.
78 ObjectLinkingLayer(ExecutionSession &ES,
79 jitlink::JITLinkMemoryManager &MemMgr);
81 /// Destruct an ObjectLinkingLayer.
82 ~ObjectLinkingLayer();
84 /// Add a pass-config modifier.
85 ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
86 std::lock_guard<std::mutex> Lock(LayerMutex);
87 Plugins.push_back(std::move(P));
92 void emit(MaterializationResponsibility R,
93 std::unique_ptr<MemoryBuffer> O) override;
95 /// Instructs this ObjectLinkingLayer instance to override the symbol flags
96 /// found in the AtomGraph with the flags supplied by the
97 /// MaterializationResponsibility instance. This is a workaround to support
98 /// symbol visibility in COFF, which does not use the libObject's
99 /// SF_Exported flag. Use only when generating / adding COFF object files.
101 /// FIXME: We should be able to remove this if/when COFF properly tracks
102 /// exported symbols.
104 setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) {
105 this->OverrideObjectFlags = OverrideObjectFlags;
109 /// If set, this ObjectLinkingLayer instance will claim responsibility
110 /// for any symbols provided by a given object file that were not already in
111 /// the MaterializationResponsibility instance. Setting this flag allows
112 /// higher-level program representations (e.g. LLVM IR) to be added based on
113 /// only a subset of the symbols they provide, without having to write
114 /// intervening layers to scan and add the additional symbols. This trades
115 /// diagnostic quality for convenience however: If all symbols are enumerated
116 /// up-front then clashes can be detected and reported early (and usually
117 /// deterministically). If this option is set, clashes for the additional
118 /// symbols may not be detected until late, and detection may depend on
119 /// the flow of control through JIT'd code. Use with care.
121 setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
122 this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
127 using AllocPtr = std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>;
129 void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
130 jitlink::PassConfiguration &PassConfig);
131 void notifyLoaded(MaterializationResponsibility &MR);
132 Error notifyEmitted(MaterializationResponsibility &MR, AllocPtr Alloc);
134 Error removeModule(VModuleKey K);
135 Error removeAllModules();
137 mutable std::mutex LayerMutex;
138 jitlink::JITLinkMemoryManager &MemMgr;
139 bool OverrideObjectFlags = false;
140 bool AutoClaimObjectSymbols = false;
141 DenseMap<VModuleKey, AllocPtr> TrackedAllocs;
142 std::vector<AllocPtr> UntrackedAllocs;
143 std::vector<std::unique_ptr<Plugin>> Plugins;
146 class EHFrameRegistrationPlugin : public ObjectLinkingLayer::Plugin {
148 EHFrameRegistrationPlugin(jitlink::EHFrameRegistrar &Registrar);
149 Error notifyEmitted(MaterializationResponsibility &MR) override;
150 void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
151 jitlink::PassConfiguration &PassConfig) override;
152 Error notifyRemovingModule(VModuleKey K) override;
153 Error notifyRemovingAllModules() override;
156 jitlink::EHFrameRegistrar &Registrar;
157 DenseMap<MaterializationResponsibility *, JITTargetAddress> InProcessLinks;
158 DenseMap<VModuleKey, JITTargetAddress> TrackedEHFrameAddrs;
159 std::vector<JITTargetAddress> UntrackedEHFrameAddrs;
162 } // end namespace orc
163 } // end namespace llvm
165 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H