LLVM  10.0.0svn
ObjectLinkingLayer.h
Go to the documentation of this file.
1 //===-- ObjectLinkingLayer.h - JITLink-based jit linking layer --*- 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 JITLink-based, in-process object linking
10 // layer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/Error.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <functional>
28 #include <list>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace llvm {
35 
36 namespace jitlink {
37 class EHFrameRegistrar;
38 } // namespace jitlink
39 
40 namespace object {
41 class ObjectFile;
42 } // namespace object
43 
44 namespace orc {
45 
46 class ObjectLinkingLayerJITLinkContext;
47 
48 /// An ObjectLayer implementation built on JITLink.
49 ///
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.
55 
56 public:
57  /// Plugin instances can be added to the ObjectLinkingLayer to receive
58  /// callbacks when code is loaded or emitted, and when JITLink is being
59  /// configured.
60  class Plugin {
61  public:
62  virtual ~Plugin();
64  const Triple &TT,
65  jitlink::PassConfiguration &Config) {}
68  return Error::success();
69  }
71  return Error::success();
72  }
74  };
75 
77  std::function<void(std::unique_ptr<MemoryBuffer>)>;
78 
79  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
80  /// and NotifyEmitted functors.
83 
84  /// Destruct an ObjectLinkingLayer.
86 
87  /// Set an object buffer return function. By default object buffers are
88  /// deleted once the JIT has linked them. If a return function is set then
89  /// it will be called to transfer ownership of the buffer instead.
91  this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
92  }
93 
94  /// Add a pass-config modifier.
95  ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
96  std::lock_guard<std::mutex> Lock(LayerMutex);
97  Plugins.push_back(std::move(P));
98  return *this;
99  }
100 
101  /// Emit the object.
102  void emit(MaterializationResponsibility R,
103  std::unique_ptr<MemoryBuffer> O) override;
104 
105  /// Instructs this ObjectLinkingLayer instance to override the symbol flags
106  /// found in the AtomGraph with the flags supplied by the
107  /// MaterializationResponsibility instance. This is a workaround to support
108  /// symbol visibility in COFF, which does not use the libObject's
109  /// SF_Exported flag. Use only when generating / adding COFF object files.
110  ///
111  /// FIXME: We should be able to remove this if/when COFF properly tracks
112  /// exported symbols.
115  this->OverrideObjectFlags = OverrideObjectFlags;
116  return *this;
117  }
118 
119  /// If set, this ObjectLinkingLayer instance will claim responsibility
120  /// for any symbols provided by a given object file that were not already in
121  /// the MaterializationResponsibility instance. Setting this flag allows
122  /// higher-level program representations (e.g. LLVM IR) to be added based on
123  /// only a subset of the symbols they provide, without having to write
124  /// intervening layers to scan and add the additional symbols. This trades
125  /// diagnostic quality for convenience however: If all symbols are enumerated
126  /// up-front then clashes can be detected and reported early (and usually
127  /// deterministically). If this option is set, clashes for the additional
128  /// symbols may not be detected until late, and detection may depend on
129  /// the flow of control through JIT'd code. Use with care.
131  setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
132  this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
133  return *this;
134  }
135 
136 private:
137  using AllocPtr = std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>;
138 
139  void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
140  jitlink::PassConfiguration &PassConfig);
141  void notifyLoaded(MaterializationResponsibility &MR);
142  Error notifyEmitted(MaterializationResponsibility &MR, AllocPtr Alloc);
143 
144  Error removeModule(VModuleKey K);
145  Error removeAllModules();
146 
147  mutable std::mutex LayerMutex;
149  bool OverrideObjectFlags = false;
150  bool AutoClaimObjectSymbols = false;
151  ReturnObjectBufferFunction ReturnObjectBuffer;
152  DenseMap<VModuleKey, AllocPtr> TrackedAllocs;
153  std::vector<AllocPtr> UntrackedAllocs;
154  std::vector<std::unique_ptr<Plugin>> Plugins;
155 };
156 
158 public:
160  Error notifyEmitted(MaterializationResponsibility &MR) override;
161  void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
162  jitlink::PassConfiguration &PassConfig) override;
163  Error notifyRemovingModule(VModuleKey K) override;
164  Error notifyRemovingAllModules() override;
165 
166 private:
167 
168  struct EHFrameRange {
169  JITTargetAddress Addr = 0;
170  size_t Size;
171  };
172 
173  jitlink::EHFrameRegistrar &Registrar;
175  DenseMap<VModuleKey, EHFrameRange> TrackedEHFrameRanges;
176  std::vector<EHFrameRange> UntrackedEHFrameRanges;
177 };
178 
179 } // end namespace orc
180 } // end namespace llvm
181 
182 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::function< void(std::unique_ptr< MemoryBuffer >)> ReturnObjectBufferFunction
static sys::Mutex Lock
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
virtual Error notifyEmitted(MaterializationResponsibility &MR)
ObjectLinkingLayer & setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags)
Instructs this ObjectLinkingLayer instance to override the symbol flags found in the AtomGraph with t...
An ObjectLayer implementation built on JITLink.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:171
void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer)
Set an object buffer return function.
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:41
virtual void notifyLoaded(MaterializationResponsibility &MR)
static ManagedStatic< std::vector< std::string > > Plugins
#define P(N)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
An ExecutionSession represents a running JIT program.
Definition: Core.h:761
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:42
ObjectLinkingLayer & addPlugin(std::unique_ptr< Plugin > P)
Add a pass-config modifier.
virtual void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT, jitlink::PassConfiguration &Config)
uint32_t Size
Definition: Profile.cpp:46
virtual Error notifyRemovingModule(VModuleKey K)
ObjectLinkingLayer & setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols)
If set, this ObjectLinkingLayer instance will claim responsibility for any symbols provided by a give...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:113