LLVM  3.7.0
ObjectTransformLayer.h
Go to the documentation of this file.
1 //===- ObjectTransformLayer.h - Run all objects through functor -*- 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 // Run all objects passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
16 
17 #include "JITSymbol.h"
18 
19 namespace llvm {
20 namespace orc {
21 
22 /// @brief Object mutating layer.
23 ///
24 /// This layer accepts sets of ObjectFiles (via addObjectSet). It
25 /// immediately applies the user supplied functor to each object, then adds
26 /// the set of transformed objects to the layer below.
27 template <typename BaseLayerT, typename TransformFtor>
29 public:
30  /// @brief Handle to a set of added objects.
31  typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
32 
33  /// @brief Construct an ObjectTransformLayer with the given BaseLayer
34  ObjectTransformLayer(BaseLayerT &BaseLayer,
35  TransformFtor Transform = TransformFtor())
36  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
37 
38  /// @brief Apply the transform functor to each object in the object set, then
39  /// add the resulting set of objects to the base layer, along with the
40  /// memory manager and symbol resolver.
41  ///
42  /// @return A handle for the added objects.
43  template <typename ObjSetT, typename MemoryManagerPtrT,
44  typename SymbolResolverPtrT>
45  ObjSetHandleT addObjectSet(ObjSetT &Objects, MemoryManagerPtrT MemMgr,
46  SymbolResolverPtrT Resolver) {
47 
48  for (auto I = Objects.begin(), E = Objects.end(); I != E; ++I)
49  *I = Transform(std::move(*I));
50 
51  return BaseLayer.addObjectSet(Objects, std::move(MemMgr),
52  std::move(Resolver));
53  }
54 
55  /// @brief Remove the object set associated with the handle H.
56  void removeObjectSet(ObjSetHandleT H) { BaseLayer.removeObjectSet(H); }
57 
58  /// @brief Search for the given named symbol.
59  /// @param Name The name of the symbol to search for.
60  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
61  /// @return A handle for the given named symbol, if it exists.
62  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
63  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
64  }
65 
66  /// @brief Get the address of the given symbol in the context of the set of
67  /// objects represented by the handle H. This call is forwarded to the
68  /// base layer's implementation.
69  /// @param H The handle for the object set to search in.
70  /// @param Name The name of the symbol to search for.
71  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
72  /// @return A handle for the given named symbol, if it is found in the
73  /// given object set.
74  JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name,
75  bool ExportedSymbolsOnly) {
76  return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
77  }
78 
79  /// @brief Immediately emit and finalize the object set represented by the
80  /// given handle.
81  /// @param H Handle for object set to emit/finalize.
82  void emitAndFinalize(ObjSetHandleT H) { BaseLayer.emitAndFinalize(H); }
83 
84  /// @brief Map section addresses for the objects associated with the handle H.
85  void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
86  TargetAddress TargetAddr) {
87  BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr);
88  }
89 
90  // Ownership hack.
91  // FIXME: Remove this as soon as RuntimeDyldELF can apply relocations without
92  // referencing the original object.
93  template <typename OwningMBSet>
94  void takeOwnershipOfBuffers(ObjSetHandleT H, OwningMBSet MBs) {
95  BaseLayer.takeOwnershipOfBuffers(H, std::move(MBs));
96  }
97 
98  /// @brief Access the transform functor directly.
99  TransformFtor &getTransform() { return Transform; }
100 
101  /// @brief Access the mumate functor directly.
102  const TransformFtor &getTransform() const { return Transform; }
103 
104 private:
105  BaseLayerT &BaseLayer;
106  TransformFtor Transform;
107 };
108 
109 } // End namespace orc.
110 } // End namespace llvm.
111 
112 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
void takeOwnershipOfBuffers(ObjSetHandleT H, OwningMBSet MBs)
void removeObjectSet(ObjSetHandleT H)
Remove the object set associated with the handle H.
const TransformFtor & getTransform() const
Access the mumate functor directly.
ObjSetHandleT addObjectSet(ObjSetT &Objects, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver)
Apply the transform functor to each object in the object set, then add the resulting set of objects t...
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
#define H(x, y, z)
Definition: MD5.cpp:53
Represents a symbol in the JIT.
Definition: JITSymbol.h:29
BaseLayerT::ObjSetHandleT ObjSetHandleT
Handle to a set of added objects.
void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress, TargetAddress TargetAddr)
Map section addresses for the objects associated with the handle H.
ObjectTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform=TransformFtor())
Construct an ObjectTransformLayer with the given BaseLayer.
uint64_t TargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:26
#define I(x, y, z)
Definition: MD5.cpp:54
TransformFtor & getTransform()
Access the transform functor directly.
void emitAndFinalize(ObjSetHandleT H)
Immediately emit and finalize the object set represented by the given handle.
JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the set of objects represented by the handle H...