LLVM  9.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 object {
37 class ObjectFile;
38 } // namespace object
39 
40 namespace orc {
41 
42 class ObjectLinkingLayerJITLinkContext;
43 
44 /// An ObjectLayer implementation built on JITLink.
45 ///
46 /// Clients can use this class to add relocatable object files to an
47 /// ExecutionSession, and it typically serves as the base layer (underneath
48 /// a compiling layer like IRCompileLayer) for the rest of the JIT.
51 
52 public:
53  /// Plugin instances can be added to the ObjectLinkingLayer to receive
54  /// callbacks when code is loaded or emitted, and when JITLink is being
55  /// configured.
56  class Plugin {
57  public:
58  virtual ~Plugin();
60  const Triple &TT,
61  jitlink::PassConfiguration &Config) {}
64  return Error::success();
65  }
67  return Error::success();
68  }
70  };
71 
72  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
73  /// and NotifyEmitted functors.
76 
77  /// Destruct an ObjectLinkingLayer.
79 
80  /// Add a pass-config modifier.
81  ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
82  std::lock_guard<std::mutex> Lock(LayerMutex);
83  Plugins.push_back(std::move(P));
84  return *this;
85  }
86 
87  /// Emit the object.
89  std::unique_ptr<MemoryBuffer> O) override;
90 
91  /// Instructs this ObjectLinkingLayer instance to override the symbol flags
92  /// found in the AtomGraph with the flags supplied by the
93  /// MaterializationResponsibility instance. This is a workaround to support
94  /// symbol visibility in COFF, which does not use the libObject's
95  /// SF_Exported flag. Use only when generating / adding COFF object files.
96  ///
97  /// FIXME: We should be able to remove this if/when COFF properly tracks
98  /// exported symbols.
101  this->OverrideObjectFlags = OverrideObjectFlags;
102  return *this;
103  }
104 
105  /// If set, this ObjectLinkingLayer instance will claim responsibility
106  /// for any symbols provided by a given object file that were not already in
107  /// the MaterializationResponsibility instance. Setting this flag allows
108  /// higher-level program representations (e.g. LLVM IR) to be added based on
109  /// only a subset of the symbols they provide, without having to write
110  /// intervening layers to scan and add the additional symbols. This trades
111  /// diagnostic quality for convenience however: If all symbols are enumerated
112  /// up-front then clashes can be detected and reported early (and usually
113  /// deterministically). If this option is set, clashes for the additional
114  /// symbols may not be detected until late, and detection may depend on
115  /// the flow of control through JIT'd code. Use with care.
117  setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
118  this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
119  return *this;
120  }
121 
122 private:
123  using AllocPtr = std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>;
124 
125  void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
126  jitlink::PassConfiguration &PassConfig);
127  void notifyLoaded(MaterializationResponsibility &MR);
128  Error notifyEmitted(MaterializationResponsibility &MR, AllocPtr Alloc);
129 
130  Error removeModule(VModuleKey K);
131  Error removeAllModules();
132 
133  mutable std::mutex LayerMutex;
135  bool OverrideObjectFlags = false;
136  bool AutoClaimObjectSymbols = false;
137  DenseMap<VModuleKey, AllocPtr> TrackedAllocs;
138  std::vector<AllocPtr> UntrackedAllocs;
139  std::vector<std::unique_ptr<Plugin>> Plugins;
140 };
141 
143 public:
144  Error notifyEmitted(MaterializationResponsibility &MR) override;
145  void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT,
146  jitlink::PassConfiguration &PassConfig) override;
147  Error notifyRemovingModule(VModuleKey K) override;
148  Error notifyRemovingAllModules() override;
149 
150 private:
152  DenseMap<VModuleKey, const void *> TrackedEHFrameAddrs;
153  std::vector<const void *> UntrackedEHFrameAddrs;
154 };
155 
156 } // end namespace orc
157 } // end namespace llvm
158 
159 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static sys::Mutex Lock
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:39
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:154
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:696
ObjectLinkingLayer & addPlugin(std::unique_ptr< Plugin > P)
Add a pass-config modifier.
virtual void modifyPassConfig(MaterializationResponsibility &MR, const Triple &TT, jitlink::PassConfiguration &Config)
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