Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 1 | //===-- MCJIT.h - Class definition for the MCJIT ----------------*- 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 10 | #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |
| 11 | #define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 12 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallPtrSet.h" |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 19 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 23 | class MCJIT; |
| 24 | |
| 25 | // This is a helper class that the MCJIT execution engine uses for linking |
| 26 | // functions across modules that it owns. It aggregates the memory manager |
| 27 | // that is passed in to the MCJIT constructor and defers most functionality |
| 28 | // to that object. |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 29 | class LinkingSymbolResolver : public JITSymbolResolver { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 30 | public: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 31 | LinkingSymbolResolver(MCJIT &Parent, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 32 | std::shared_ptr<JITSymbolResolver> Resolver) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 33 | : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {} |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 34 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 35 | JITSymbol findSymbol(const std::string &Name) override; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 36 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 37 | // MCJIT doesn't support logical dylibs. |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 38 | JITSymbol findSymbolInLogicalDylib(const std::string &Name) override { |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 39 | return nullptr; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 43 | MCJIT &ParentEngine; |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 44 | std::shared_ptr<JITSymbolResolver> ClientResolver; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 45 | }; |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 46 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 47 | // About Module states: added->loaded->finalized. |
Yaron Keren | fb95582 | 2013-10-19 09:03:20 +0000 | [diff] [blame] | 48 | // |
| 49 | // The purpose of the "added" state is having modules in standby. (added=known |
| 50 | // but not compiled). The idea is that you can add a module to provide function |
| 51 | // definitions but if nothing in that module is referenced by a module in which |
Yaron Keren | 1ec9df3 | 2013-10-24 10:04:47 +0000 | [diff] [blame] | 52 | // a function is executed (note the wording here because it's not exactly the |
Yaron Keren | fb95582 | 2013-10-19 09:03:20 +0000 | [diff] [blame] | 53 | // ideal case) then the module never gets compiled. This is sort of lazy |
| 54 | // compilation. |
| 55 | // |
| 56 | // The purpose of the "loaded" state (loaded=compiled and required sections |
| 57 | // copied into local memory but not yet ready for execution) is to have an |
| 58 | // intermediate state wherein clients can remap the addresses of sections, using |
| 59 | // MCJIT::mapSectionAddress, (in preparation for later copying to a new location |
| 60 | // or an external process) before relocations and page permissions are applied. |
| 61 | // |
| 62 | // It might not be obvious at first glance, but the "remote-mcjit" case in the |
| 63 | // lli tool does this. In that case, the intermediate action is taken by the |
| 64 | // RemoteMemoryManager in response to the notifyObjectLoaded function being |
| 65 | // called. |
| 66 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 67 | class MCJIT : public ExecutionEngine { |
David Blaikie | 196e323 | 2014-09-02 22:41:07 +0000 | [diff] [blame] | 68 | MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 69 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 70 | std::shared_ptr<JITSymbolResolver> Resolver); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 71 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 72 | typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 73 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 74 | class OwningModuleContainer { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 75 | public: |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 76 | OwningModuleContainer() { |
| 77 | } |
| 78 | ~OwningModuleContainer() { |
| 79 | freeModulePtrSet(AddedModules); |
| 80 | freeModulePtrSet(LoadedModules); |
| 81 | freeModulePtrSet(FinalizedModules); |
| 82 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 83 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 84 | ModulePtrSet::iterator begin_added() { return AddedModules.begin(); } |
| 85 | ModulePtrSet::iterator end_added() { return AddedModules.end(); } |
Lang Hames | 018452e | 2014-09-05 23:38:35 +0000 | [diff] [blame] | 86 | iterator_range<ModulePtrSet::iterator> added() { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 87 | return make_range(begin_added(), end_added()); |
Lang Hames | 018452e | 2014-09-05 23:38:35 +0000 | [diff] [blame] | 88 | } |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 89 | |
| 90 | ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); } |
| 91 | ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); } |
| 92 | |
| 93 | ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); } |
| 94 | ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); } |
| 95 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 96 | void addModule(std::unique_ptr<Module> M) { |
| 97 | AddedModules.insert(M.release()); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool removeModule(Module *M) { |
| 101 | return AddedModules.erase(M) || LoadedModules.erase(M) || |
| 102 | FinalizedModules.erase(M); |
| 103 | } |
| 104 | |
| 105 | bool hasModuleBeenAddedButNotLoaded(Module *M) { |
| 106 | return AddedModules.count(M) != 0; |
| 107 | } |
| 108 | |
| 109 | bool hasModuleBeenLoaded(Module *M) { |
| 110 | // If the module is in either the "loaded" or "finalized" sections it |
| 111 | // has been loaded. |
| 112 | return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0); |
| 113 | } |
| 114 | |
| 115 | bool hasModuleBeenFinalized(Module *M) { |
| 116 | return FinalizedModules.count(M) != 0; |
| 117 | } |
| 118 | |
| 119 | bool ownsModule(Module* M) { |
| 120 | return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) || |
| 121 | (FinalizedModules.count(M) != 0); |
| 122 | } |
| 123 | |
| 124 | void markModuleAsLoaded(Module *M) { |
| 125 | // This checks against logic errors in the MCJIT implementation. |
| 126 | // This function should never be called with either a Module that MCJIT |
| 127 | // does not own or a Module that has already been loaded and/or finalized. |
| 128 | assert(AddedModules.count(M) && |
| 129 | "markModuleAsLoaded: Module not found in AddedModules"); |
| 130 | |
| 131 | // Remove the module from the "Added" set. |
| 132 | AddedModules.erase(M); |
| 133 | |
| 134 | // Add the Module to the "Loaded" set. |
| 135 | LoadedModules.insert(M); |
| 136 | } |
| 137 | |
| 138 | void markModuleAsFinalized(Module *M) { |
| 139 | // This checks against logic errors in the MCJIT implementation. |
| 140 | // This function should never be called with either a Module that MCJIT |
| 141 | // does not own, a Module that has not been loaded or a Module that has |
| 142 | // already been finalized. |
| 143 | assert(LoadedModules.count(M) && |
| 144 | "markModuleAsFinalized: Module not found in LoadedModules"); |
| 145 | |
| 146 | // Remove the module from the "Loaded" section of the list. |
| 147 | LoadedModules.erase(M); |
| 148 | |
| 149 | // Add the Module to the "Finalized" section of the list by inserting it |
| 150 | // before the 'end' iterator. |
| 151 | FinalizedModules.insert(M); |
| 152 | } |
| 153 | |
| 154 | void markAllLoadedModulesAsFinalized() { |
| 155 | for (ModulePtrSet::iterator I = LoadedModules.begin(), |
| 156 | E = LoadedModules.end(); |
| 157 | I != E; ++I) { |
| 158 | Module *M = *I; |
| 159 | FinalizedModules.insert(M); |
| 160 | } |
| 161 | LoadedModules.clear(); |
| 162 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 163 | |
| 164 | private: |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 165 | ModulePtrSet AddedModules; |
| 166 | ModulePtrSet LoadedModules; |
| 167 | ModulePtrSet FinalizedModules; |
| 168 | |
| 169 | void freeModulePtrSet(ModulePtrSet& MPS) { |
| 170 | // Go through the module set and delete everything. |
| 171 | for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) { |
| 172 | Module *M = *I; |
| 173 | delete M; |
| 174 | } |
| 175 | MPS.clear(); |
| 176 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
David Blaikie | 196e323 | 2014-09-02 22:41:07 +0000 | [diff] [blame] | 179 | std::unique_ptr<TargetMachine> TM; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 180 | MCContext *Ctx; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 181 | std::shared_ptr<MCJITMemoryManager> MemMgr; |
| 182 | LinkingSymbolResolver Resolver; |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 183 | RuntimeDyld Dyld; |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 184 | std::vector<JITEventListener*> EventListeners; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 185 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 186 | OwningModuleContainer OwnedModules; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 187 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 188 | SmallVector<object::OwningBinary<object::Archive>, 2> Archives; |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 189 | SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 190 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 191 | SmallVector<std::unique_ptr<object::ObjectFile>, 2> LoadedObjects; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 192 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 193 | // An optional ObjectCache to be notified of compiled objects and used to |
| 194 | // perform lookup of pre-compiled code to avoid re-compilation. |
| 195 | ObjectCache *ObjCache; |
| 196 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 197 | Function *FindFunctionNamedInModulePtrSet(const char *FnName, |
| 198 | ModulePtrSet::iterator I, |
| 199 | ModulePtrSet::iterator E); |
| 200 | |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 201 | GlobalVariable *FindGlobalVariableNamedInModulePtrSet(const char *Name, |
| 202 | bool AllowInternal, |
| 203 | ModulePtrSet::iterator I, |
| 204 | ModulePtrSet::iterator E); |
| 205 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 206 | void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors, |
| 207 | ModulePtrSet::iterator I, |
| 208 | ModulePtrSet::iterator E); |
| 209 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 210 | public: |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 211 | ~MCJIT() override; |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 212 | |
| 213 | /// @name ExecutionEngine interface implementation |
| 214 | /// @{ |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 215 | void addModule(std::unique_ptr<Module> M) override; |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 216 | void addObjectFile(std::unique_ptr<object::ObjectFile> O) override; |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 217 | void addObjectFile(object::OwningBinary<object::ObjectFile> O) override; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 218 | void addArchive(object::OwningBinary<object::Archive> O) override; |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 219 | bool removeModule(Module *M) override; |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 220 | |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 221 | /// FindFunctionNamed - Search all of the active modules to find the function that |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 222 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 223 | /// general code. |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 224 | Function *FindFunctionNamed(const char *FnName) override; |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 225 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 226 | /// FindGlobalVariableNamed - Search all of the active modules to find the |
| 227 | /// global variable that defines Name. This is very slow operation and |
| 228 | /// shouldn't be used for general code. |
| 229 | GlobalVariable *FindGlobalVariableNamed(const char *Name, |
| 230 | bool AllowInternal = false) override; |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 231 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 232 | /// Sets the object manager that MCJIT should use to avoid compilation. |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 233 | void setObjectCache(ObjectCache *manager) override; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 234 | |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 235 | void setProcessAllSections(bool ProcessAllSections) override { |
| 236 | Dyld.setProcessAllSections(ProcessAllSections); |
| 237 | } |
| 238 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 239 | void generateCodeForModule(Module *M) override; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 240 | |
David Tweed | 2e7efed | 2013-05-17 10:01:46 +0000 | [diff] [blame] | 241 | /// finalizeObject - ensure the module is fully processed and is usable. |
| 242 | /// |
| 243 | /// It is the user-level function for completing the process of making the |
| 244 | /// object usable for execution. It should be called after sections within an |
| 245 | /// object have been relocated using mapSectionAddress. When this method is |
| 246 | /// called the MCJIT execution engine will reapply relocations for a loaded |
| 247 | /// object. |
Yaron Keren | fb95582 | 2013-10-19 09:03:20 +0000 | [diff] [blame] | 248 | /// Is it OK to finalize a set of modules, add modules and finalize again. |
Andrew Kaylor | 2fb40ce | 2013-10-21 23:27:02 +0000 | [diff] [blame] | 249 | // FIXME: Do we really need both of these? |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 250 | void finalizeObject() override; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 251 | virtual void finalizeModule(Module *); |
| 252 | void finalizeLoadedModules(); |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 253 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 254 | /// runStaticConstructorsDestructors - This method is used to execute all of |
| 255 | /// the static constructors or destructors for a program. |
| 256 | /// |
| 257 | /// \param isDtors - Run the destructors instead of constructors. |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 258 | void runStaticConstructorsDestructors(bool isDtors) override; |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 259 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 260 | void *getPointerToFunction(Function *F) override; |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 261 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 262 | GenericValue runFunction(Function *F, |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 263 | ArrayRef<GenericValue> ArgValues) override; |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 264 | |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 265 | /// getPointerToNamedFunction - This method returns the address of the |
| 266 | /// specified function by using the dlsym function call. As such it is only |
| 267 | /// useful for resolving library symbols, not code generated symbols. |
| 268 | /// |
| 269 | /// If AbortOnFailure is false and no function with the given name is |
| 270 | /// found, this function silently returns a null pointer. Otherwise, |
| 271 | /// it prints a message to stderr and aborts. |
| 272 | /// |
Lang Hames | 9a78334 | 2014-09-15 17:50:22 +0000 | [diff] [blame] | 273 | void *getPointerToNamedFunction(StringRef Name, |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 274 | bool AbortOnFailure = true) override; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 275 | |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 276 | /// mapSectionAddress - map a section to its target address space value. |
| 277 | /// Map the address of a JIT section as returned from the memory manager |
| 278 | /// to the address in the target process as the running code will see it. |
| 279 | /// This is the address which will be used for relocation resolution. |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 280 | void mapSectionAddress(const void *LocalAddress, |
| 281 | uint64_t TargetAddress) override { |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 282 | Dyld.mapSectionAddress(LocalAddress, TargetAddress); |
| 283 | } |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 284 | void RegisterJITEventListener(JITEventListener *L) override; |
| 285 | void UnregisterJITEventListener(JITEventListener *L) override; |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 286 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 287 | // If successful, these function will implicitly finalize all loaded objects. |
| 288 | // To get a function address within MCJIT without causing a finalize, use |
| 289 | // getSymbolAddress. |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 290 | uint64_t getGlobalValueAddress(const std::string &Name) override; |
| 291 | uint64_t getFunctionAddress(const std::string &Name) override; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 292 | |
David Blaikie | 196e323 | 2014-09-02 22:41:07 +0000 | [diff] [blame] | 293 | TargetMachine *getTargetMachine() override { return TM.get(); } |
Juergen Ributzka | 5fe955c | 2014-01-23 19:23:28 +0000 | [diff] [blame] | 294 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 295 | /// @} |
| 296 | /// @name (Private) Registration Interfaces |
| 297 | /// @{ |
| 298 | |
| 299 | static void Register() { |
| 300 | MCJITCtor = createJIT; |
| 301 | } |
| 302 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 303 | static ExecutionEngine* |
| 304 | createJIT(std::unique_ptr<Module> M, |
| 305 | std::string *ErrorStr, |
| 306 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 307 | std::shared_ptr<JITSymbolResolver> Resolver, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 308 | std::unique_ptr<TargetMachine> TM); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 309 | |
| 310 | // @} |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 311 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 312 | JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 313 | // DEPRECATED - Please use findSymbol instead. |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 314 | // This is not directly exposed via the ExecutionEngine API, but it is |
| 315 | // used by the LinkingMemoryManager. |
| 316 | uint64_t getSymbolAddress(const std::string &Name, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 317 | bool CheckFunctionsOnly); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 318 | |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 319 | protected: |
| 320 | /// emitObject -- Generate a JITed object in memory from the specified module |
| 321 | /// Currently, MCJIT only supports a single module and the module passed to |
| 322 | /// this function call is expected to be the contained module. The module |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 323 | /// is passed as a parameter here to prepare for multiple module support in |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 324 | /// the future. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 325 | std::unique_ptr<MemoryBuffer> emitObject(Module *M); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 326 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 327 | void NotifyObjectEmitted(const object::ObjectFile& Obj, |
| 328 | const RuntimeDyld::LoadedObjectInfo &L); |
| 329 | void NotifyFreeingObject(const object::ObjectFile& Obj); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 330 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 331 | JITSymbol findExistingSymbol(const std::string &Name); |
| 332 | Module *findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 335 | } // end llvm namespace |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 336 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 337 | #endif // LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |