[Orc] Remove a bunch of constructors from ObjectLinkingLayer.
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / ObjectLinkingLayer.h
1 //===- ObjectLinkingLayer.h - Add object files to a JIT process -*- 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 // Contains the definition for the object layer of the JIT.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
16
17 #include "LookasideRTDyldMM.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
20 #include <list>
21 #include <memory>
22
23 namespace llvm {
24
25 class ObjectLinkingLayerBase {
26 protected:
27   /// @brief Holds a set of objects to be allocated/linked as a unit in the JIT.
28   ///
29   /// An instance of this class will be created for each set of objects added
30   /// via JITObjectLayer::addObjectSet. Deleting the instance (via
31   /// removeObjectSet) frees its memory, removing all symbol definitions that
32   /// had been provided by this instance. Higher level layers are responsible
33   /// for taking any action required to handle the missing symbols.
34   class LinkedObjectSet {
35   public:
36     LinkedObjectSet(std::unique_ptr<RTDyldMemoryManager> MM)
37         : MM(std::move(MM)), RTDyld(llvm::make_unique<RuntimeDyld>(&*this->MM)),
38           State(Raw) {}
39
40     std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
41     addObject(const object::ObjectFile &Obj) {
42       return RTDyld->loadObject(Obj);
43     }
44
45     uint64_t getSymbolAddress(StringRef Name, bool ExportedSymbolsOnly) {
46       if (ExportedSymbolsOnly)
47         return RTDyld->getExportedSymbolLoadAddress(Name);
48       return RTDyld->getSymbolLoadAddress(Name);
49     }
50
51     bool NeedsFinalization() const { return (State == Raw); }
52
53     void Finalize() {
54       State = Finalizing;
55       RTDyld->resolveRelocations();
56       RTDyld->registerEHFrames();
57       MM->finalizeMemory();
58       State = Finalized;
59     }
60
61     void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) {
62       assert((State != Finalized) &&
63              "Attempting to remap sections for finalized objects.");
64       RTDyld->mapSectionAddress(LocalAddress, TargetAddress);
65     }
66
67   private:
68     std::unique_ptr<RTDyldMemoryManager> MM;
69     std::unique_ptr<RuntimeDyld> RTDyld;
70     enum { Raw, Finalizing, Finalized } State;
71   };
72
73   typedef std::list<LinkedObjectSet> LinkedObjectSetListT;
74
75 public:
76   /// @brief Handle to a set of loaded objects.
77   typedef typename LinkedObjectSetListT::iterator ObjSetHandleT;
78 };
79
80 /// @brief Default (no-op) action to perform when loading objects.
81 class DoNothingOnNotifyLoaded {
82 public:
83   template <typename ObjSetT, typename LoadResult>
84   void operator()(ObjectLinkingLayerBase::ObjSetHandleT, const ObjSetT &,
85                   const LoadResult &) {}
86 };
87
88 /// @brief Bare bones object linking layer.
89 ///
90 ///   This class is intended to be used as the base layer for a JIT. It allows
91 /// object files to be loaded into memory, linked, and the addresses of their
92 /// symbols queried. All objects added to this layer can see each other's
93 /// symbols.
94 template <typename NotifyLoadedFtor = DoNothingOnNotifyLoaded>
95 class ObjectLinkingLayer : public ObjectLinkingLayerBase {
96 public:
97
98   /// @brief LoadedObjectInfo list. Contains a list of owning pointers to
99   ///        RuntimeDyld::LoadedObjectInfo instances.
100   typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
101       LoadedObjInfoList;
102
103   /// @brief Functor to create RTDyldMemoryManager instances.
104   typedef std::function<std::unique_ptr<RTDyldMemoryManager>()> CreateRTDyldMMFtor;
105
106   /// @brief Functor for receiving finalization notifications.
107   typedef std::function<void(ObjSetHandleT)> NotifyFinalizedFtor;
108
109   /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
110   ///        NotifyFinalized and CreateMemoryManager functors.
111   ObjectLinkingLayer(
112       CreateRTDyldMMFtor CreateMemoryManager,
113       NotifyLoadedFtor NotifyLoaded,
114       NotifyFinalizedFtor NotifyFinalized)
115       : NotifyLoaded(std::move(NotifyLoaded)),
116         NotifyFinalized(std::move(NotifyFinalized)),
117         CreateMemoryManager(std::move(CreateMemoryManager)) {}
118
119   /// @brief Add a set of objects (or archives) that will be treated as a unit
120   ///        for the purposes of symbol lookup and memory management.
121   ///
122   /// @return A pair containing (1) A handle that can be used to free the memory
123   ///         allocated for the objects, and (2) a LoadedObjInfoList containing
124   ///         one LoadedObjInfo instance for each object at the corresponding
125   ///         index in the Objects list.
126   ///
127   ///   This version of this method allows the client to pass in an
128   /// RTDyldMemoryManager instance that will be used to allocate memory and look
129   /// up external symbol addresses for the given objects.
130   template <typename ObjSetT>
131   ObjSetHandleT addObjectSet(const ObjSetT &Objects,
132                              std::unique_ptr<RTDyldMemoryManager> MM) {
133
134     if (!MM) {
135       assert(CreateMemoryManager &&
136              "No memory manager or memory manager creator provided.");
137       MM = CreateMemoryManager();
138     }
139
140     ObjSetHandleT Handle = LinkedObjSetList.insert(
141         LinkedObjSetList.end(), LinkedObjectSet(std::move(MM)));
142     LinkedObjectSet &LOS = *Handle;
143     LoadedObjInfoList LoadedObjInfos;
144
145     for (auto &Obj : Objects)
146       LoadedObjInfos.push_back(LOS.addObject(*Obj));
147
148     NotifyLoaded(Handle, Objects, LoadedObjInfos);
149
150     return Handle;
151   }
152
153   /// @brief Map section addresses for the objects associated with the handle H.
154   void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
155                          uint64_t TargetAddress) {
156     H->mapSectionAddress(LocalAddress, TargetAddress);
157   }
158
159   /// @brief Remove the set of objects associated with handle H.
160   ///
161   ///   All memory allocated for the objects will be freed, and the sections and
162   /// symbols they provided will no longer be available. No attempt is made to
163   /// re-emit the missing symbols, and any use of these symbols (directly or
164   /// indirectly) will result in undefined behavior. If dependence tracking is
165   /// required to detect or resolve such issues it should be added at a higher
166   /// layer.
167   void removeObjectSet(ObjSetHandleT H) {
168     // How do we invalidate the symbols in H?
169     LinkedObjSetList.erase(H);
170   }
171
172   /// @brief Get the address of a loaded symbol.
173   ///
174   /// @return The address in the target process's address space of the named
175   ///         symbol. Null if no such symbol is known.
176   ///
177   ///   This method will trigger the finalization of the linked object set
178   /// containing the definition of the given symbol, if it is found.
179   uint64_t getSymbolAddress(StringRef Name, bool ExportedSymbolsOnly) {
180     for (auto I = LinkedObjSetList.begin(), E = LinkedObjSetList.end(); I != E;
181          ++I)
182       if (uint64_t Addr = lookupSymbolAddressIn(I, Name, ExportedSymbolsOnly))
183         return Addr;
184
185     return 0;
186   }
187
188   /// @brief Search for a given symbol in the context of the set of loaded
189   ///        objects represented by the handle H.
190   ///
191   /// @return The address in the target process's address space of the named
192   ///         symbol. Null if the given object set does not contain a definition
193   ///         of this symbol.
194   ///
195   ///   This method will trigger the finalization of the linked object set
196   /// represented by the handle H if that set contains the requested symbol.
197   uint64_t lookupSymbolAddressIn(ObjSetHandleT H, StringRef Name,
198                                  bool ExportedSymbolsOnly) {
199     if (uint64_t Addr = H->getSymbolAddress(Name, ExportedSymbolsOnly)) {
200       if (H->NeedsFinalization()) {
201         H->Finalize();
202         if (NotifyFinalized)
203           NotifyFinalized(H);
204       }
205       return Addr;
206     }
207     return 0;
208   }
209
210 private:
211   LinkedObjectSetListT LinkedObjSetList;
212   NotifyLoadedFtor NotifyLoaded;
213   NotifyFinalizedFtor NotifyFinalized;
214   CreateRTDyldMMFtor CreateMemoryManager;
215 };
216
217 } // end namespace llvm
218
219 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H