1 //===- ObjectLinkingLayer.h - Add object files to a JIT process -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains the definition for the object layer of the JIT.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
17 #include "LookasideRTDyldMM.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
25 class ObjectLinkingLayerBase {
27 /// @brief Holds a set of objects to be allocated/linked as a unit in the JIT.
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 {
36 LinkedObjectSet(std::unique_ptr<RTDyldMemoryManager> MM)
37 : MM(std::move(MM)), RTDyld(llvm::make_unique<RuntimeDyld>(&*this->MM)),
40 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
41 addObject(const object::ObjectFile &Obj) {
42 return RTDyld->loadObject(Obj);
45 uint64_t getSymbolAddress(StringRef Name, bool ExportedSymbolsOnly) {
46 if (ExportedSymbolsOnly)
47 return RTDyld->getExportedSymbolLoadAddress(Name);
48 return RTDyld->getSymbolLoadAddress(Name);
51 bool NeedsFinalization() const { return (State == Raw); }
55 RTDyld->resolveRelocations();
56 RTDyld->registerEHFrames();
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);
68 std::unique_ptr<RTDyldMemoryManager> MM;
69 std::unique_ptr<RuntimeDyld> RTDyld;
70 enum { Raw, Finalizing, Finalized } State;
73 typedef std::list<LinkedObjectSet> LinkedObjectSetListT;
76 /// @brief Handle to a set of loaded objects.
77 typedef typename LinkedObjectSetListT::iterator ObjSetHandleT;
80 /// @brief Default (no-op) action to perform when loading objects.
81 class DoNothingOnNotifyLoaded {
83 template <typename ObjSetT, typename LoadResult>
84 void operator()(ObjectLinkingLayerBase::ObjSetHandleT, const ObjSetT &,
85 const LoadResult &) {}
88 /// @brief Bare bones object linking layer.
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
94 template <typename NotifyLoadedFtor = DoNothingOnNotifyLoaded>
95 class ObjectLinkingLayer : public ObjectLinkingLayerBase {
98 /// @brief LoadedObjectInfo list. Contains a list of owning pointers to
99 /// RuntimeDyld::LoadedObjectInfo instances.
100 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
103 /// @brief Functor to create RTDyldMemoryManager instances.
104 typedef std::function<std::unique_ptr<RTDyldMemoryManager>()> CreateRTDyldMMFtor;
106 /// @brief Functor for receiving finalization notifications.
107 typedef std::function<void(ObjSetHandleT)> NotifyFinalizedFtor;
109 /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
110 /// NotifyFinalized and CreateMemoryManager functors.
112 CreateRTDyldMMFtor CreateMemoryManager,
113 NotifyLoadedFtor NotifyLoaded,
114 NotifyFinalizedFtor NotifyFinalized)
115 : NotifyLoaded(std::move(NotifyLoaded)),
116 NotifyFinalized(std::move(NotifyFinalized)),
117 CreateMemoryManager(std::move(CreateMemoryManager)) {}
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.
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.
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) {
135 assert(CreateMemoryManager &&
136 "No memory manager or memory manager creator provided.");
137 MM = CreateMemoryManager();
140 ObjSetHandleT Handle = LinkedObjSetList.insert(
141 LinkedObjSetList.end(), LinkedObjectSet(std::move(MM)));
142 LinkedObjectSet &LOS = *Handle;
143 LoadedObjInfoList LoadedObjInfos;
145 for (auto &Obj : Objects)
146 LoadedObjInfos.push_back(LOS.addObject(*Obj));
148 NotifyLoaded(Handle, Objects, LoadedObjInfos);
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);
159 /// @brief Remove the set of objects associated with handle H.
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
167 void removeObjectSet(ObjSetHandleT H) {
168 // How do we invalidate the symbols in H?
169 LinkedObjSetList.erase(H);
172 /// @brief Get the address of a loaded symbol.
174 /// @return The address in the target process's address space of the named
175 /// symbol. Null if no such symbol is known.
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;
182 if (uint64_t Addr = lookupSymbolAddressIn(I, Name, ExportedSymbolsOnly))
188 /// @brief Search for a given symbol in the context of the set of loaded
189 /// objects represented by the handle H.
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
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()) {
211 LinkedObjectSetListT LinkedObjSetList;
212 NotifyLoadedFtor NotifyLoaded;
213 NotifyFinalizedFtor NotifyFinalized;
214 CreateRTDyldMMFtor CreateMemoryManager;
217 } // end namespace llvm
219 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H