Eric Christopher | bb498ca | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 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 | #include "MCJIT.h" |
| 11 | #include "llvm/ExecutionEngine/GenericValue.h" |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 14 | #include "llvm/ExecutionEngine/ObjectBuffer.h" |
| 15 | #include "llvm/ExecutionEngine/ObjectImage.h" |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Function.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "llvm/IR/Mangler.h" |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmInfo.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 23 | #include "llvm/Object/Archive.h" |
| 24 | #include "llvm/PassManager.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MutexGuard.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 29 | #include "llvm/Target/TargetLowering.h" |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 30 | #include "llvm/Target/TargetSubtargetInfo.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | static struct RegisterJIT { |
| 37 | RegisterJIT() { MCJIT::Register(); } |
| 38 | } JITRegistrator; |
| 39 | |
| 40 | } |
| 41 | |
| 42 | extern "C" void LLVMLinkInMCJIT() { |
| 43 | } |
| 44 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 45 | ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 46 | std::string *ErrorStr, |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 47 | RTDyldMemoryManager *MemMgr, |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 48 | std::unique_ptr<TargetMachine> TM) { |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 49 | // Try to register the program as a source of symbols to resolve against. |
| 50 | // |
| 51 | // FIXME: Don't do this here. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 52 | sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 53 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 54 | return new MCJIT(std::move(M), std::move(TM), |
| 55 | MemMgr ? MemMgr : new SectionMemoryManager()); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 58 | MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, |
| 59 | RTDyldMemoryManager *MM) |
| 60 | : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr), |
| 61 | MemMgr(this, MM), Dyld(&MemMgr), ObjCache(nullptr) { |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 62 | // FIXME: We are managing our modules, so we do not want the base class |
| 63 | // ExecutionEngine to manage them as well. To avoid double destruction |
| 64 | // of the first (and only) module added in ExecutionEngine constructor |
| 65 | // we remove it from EE and will destruct it ourselves. |
| 66 | // |
| 67 | // It may make sense to move our module manager (based on SmallStPtr) back |
| 68 | // into EE if the JIT and Interpreter can live with it. |
| 69 | // If so, additional functions: addModule, removeModule, FindFunctionNamed, |
| 70 | // runStaticConstructorsDestructors could be moved back to EE as well. |
| 71 | // |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 72 | std::unique_ptr<Module> First = std::move(Modules[0]); |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 73 | Modules.clear(); |
Chandler Carruth | 8179873 | 2013-10-24 09:52:56 +0000 | [diff] [blame] | 74 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 75 | OwnedModules.addModule(std::move(First)); |
| 76 | setDataLayout(TM->getSubtargetImpl()->getDataLayout()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 79 | MCJIT::~MCJIT() { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 80 | MutexGuard locked(lock); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 81 | |
| 82 | Dyld.deregisterEHFrames(); |
| 83 | |
| 84 | for (auto &Obj : LoadedObjects) |
| 85 | if (Obj) |
| 86 | NotifyFreeingObject(*Obj); |
| 87 | |
| 88 | Archives.clear(); |
| 89 | } |
| 90 | |
| 91 | void MCJIT::addModule(std::unique_ptr<Module> M) { |
| 92 | MutexGuard locked(lock); |
| 93 | OwnedModules.addModule(std::move(M)); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 96 | bool MCJIT::removeModule(Module *M) { |
| 97 | MutexGuard locked(lock); |
| 98 | return OwnedModules.removeModule(M); |
| 99 | } |
| 100 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 101 | void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 102 | std::unique_ptr<ObjectImage> LoadedObject = Dyld.loadObject(std::move(Obj)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 103 | if (!LoadedObject || Dyld.hasError()) |
| 104 | report_fatal_error(Dyld.getErrorString()); |
| 105 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 106 | NotifyObjectEmitted(*LoadedObject); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 107 | |
| 108 | LoadedObjects.push_back(std::move(LoadedObject)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 111 | void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) { |
| 112 | std::unique_ptr<object::ObjectFile> ObjFile; |
| 113 | std::unique_ptr<MemoryBuffer> MemBuf; |
| 114 | std::tie(ObjFile, MemBuf) = Obj.takeBinary(); |
| 115 | addObjectFile(std::move(ObjFile)); |
| 116 | Buffers.push_back(std::move(MemBuf)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 119 | void MCJIT::addArchive(object::OwningBinary<object::Archive> A) { |
| 120 | Archives.push_back(std::move(A)); |
| 121 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 122 | |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 123 | void MCJIT::setObjectCache(ObjectCache* NewCache) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 124 | MutexGuard locked(lock); |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 125 | ObjCache = NewCache; |
| 126 | } |
| 127 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 128 | std::unique_ptr<ObjectBufferStream> MCJIT::emitObject(Module *M) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 129 | MutexGuard locked(lock); |
| 130 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 131 | // This must be a module which has already been added but not loaded to this |
| 132 | // MCJIT instance, since these conditions are tested by our caller, |
| 133 | // generateCodeForModule. |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 134 | |
| 135 | PassManager PM; |
| 136 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 137 | M->setDataLayout(TM->getSubtargetImpl()->getDataLayout()); |
| 138 | PM.add(new DataLayoutPass()); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 139 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 140 | // The RuntimeDyld will take ownership of this shortly |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 141 | std::unique_ptr<ObjectBufferStream> CompiledObject(new ObjectBufferStream()); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 142 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 143 | // Turn the machine code intermediate representation into bytes in memory |
| 144 | // that may be executed. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 145 | if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), |
| 146 | !getVerifyModules())) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 147 | report_fatal_error("Target does not support MC emission!"); |
| 148 | } |
| 149 | |
| 150 | // Initialize passes. |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 151 | PM.run(*M); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 152 | // Flush the output buffer to get the generated code into memory |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 153 | CompiledObject->flush(); |
| 154 | |
| 155 | // If we have an object cache, tell it about the new object. |
| 156 | // Note that we're using the compiled image, not the loaded image (as below). |
| 157 | if (ObjCache) { |
| 158 | // MemoryBuffer is a thin wrapper around the actual memory, so it's OK |
| 159 | // to create a temporary object here and delete it after the call. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 160 | MemoryBufferRef MB = CompiledObject->getMemBuffer(); |
| 161 | ObjCache->notifyObjectCompiled(M, MB); |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 164 | return CompiledObject; |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 167 | void MCJIT::generateCodeForModule(Module *M) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 168 | // Get a thread lock to make sure we aren't trying to load multiple times |
| 169 | MutexGuard locked(lock); |
| 170 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 171 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 172 | assert(OwnedModules.ownsModule(M) && |
| 173 | "MCJIT::generateCodeForModule: Unknown module."); |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 174 | |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 175 | // Re-compilation is not supported |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 176 | if (OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 177 | return; |
| 178 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 179 | std::unique_ptr<ObjectBuffer> ObjectToLoad; |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 180 | // Try to load the pre-compiled object from cache if possible |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 181 | if (ObjCache) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 182 | if (std::unique_ptr<MemoryBuffer> PreCompiledObject = |
| 183 | ObjCache->getObject(M)) |
| 184 | ObjectToLoad = |
| 185 | llvm::make_unique<ObjectBuffer>(std::move(PreCompiledObject)); |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // If the cache did not contain a suitable object, compile the object |
| 189 | if (!ObjectToLoad) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 190 | ObjectToLoad = emitObject(M); |
| 191 | assert(ObjectToLoad && "Compilation did not produce an object."); |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 192 | } |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 193 | |
| 194 | // Load the object into the dynamic linker. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 195 | // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 196 | std::unique_ptr<ObjectImage> LoadedObject = |
| 197 | Dyld.loadObject(std::move(ObjectToLoad)); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 198 | if (!LoadedObject) |
Jim Grosbach | 8086f3b | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 199 | report_fatal_error(Dyld.getErrorString()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 200 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 201 | // FIXME: Make this optional, maybe even move it to a JIT event listener |
| 202 | LoadedObject->registerWithDebugger(); |
| 203 | |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 204 | NotifyObjectEmitted(*LoadedObject); |
| 205 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 206 | LoadedObjects.push_back(std::move(LoadedObject)); |
| 207 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 208 | OwnedModules.markModuleAsLoaded(M); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 211 | void MCJIT::finalizeLoadedModules() { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 212 | MutexGuard locked(lock); |
| 213 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 214 | // Resolve any outstanding relocations. |
| 215 | Dyld.resolveRelocations(); |
| 216 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 217 | OwnedModules.markAllLoadedModulesAsFinalized(); |
| 218 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 219 | // Register EH frame data for any module we own which has been loaded |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 220 | Dyld.registerEHFrames(); |
| 221 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 222 | // Set page permissions. |
| 223 | MemMgr.finalizeMemory(); |
| 224 | } |
| 225 | |
| 226 | // FIXME: Rename this. |
| 227 | void MCJIT::finalizeObject() { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 228 | MutexGuard locked(lock); |
| 229 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 230 | // Generate code for module is going to move objects out of the 'added' list, |
| 231 | // so we need to copy that out before using it: |
| 232 | SmallVector<Module*, 16> ModsToAdd; |
| 233 | for (auto M : OwnedModules.added()) |
| 234 | ModsToAdd.push_back(M); |
| 235 | |
| 236 | for (auto M : ModsToAdd) |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 237 | generateCodeForModule(M); |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 238 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 239 | finalizeLoadedModules(); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void MCJIT::finalizeModule(Module *M) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 243 | MutexGuard locked(lock); |
| 244 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 245 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 246 | assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 247 | |
| 248 | // If the module hasn't been compiled, just do that. |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 249 | if (!OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 250 | generateCodeForModule(M); |
| 251 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 252 | finalizeLoadedModules(); |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 255 | uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 256 | Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 257 | SmallString<128> FullName; |
| 258 | Mang.getNameWithPrefix(FullName, Name); |
| 259 | return Dyld.getSymbolLoadAddress(FullName); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 260 | } |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 261 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 262 | Module *MCJIT::findModuleForSymbol(const std::string &Name, |
| 263 | bool CheckFunctionsOnly) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 264 | MutexGuard locked(lock); |
| 265 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 266 | // If it hasn't already been generated, see if it's in one of our modules. |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 267 | for (ModulePtrSet::iterator I = OwnedModules.begin_added(), |
| 268 | E = OwnedModules.end_added(); |
| 269 | I != E; ++I) { |
| 270 | Module *M = *I; |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 271 | Function *F = M->getFunction(Name); |
Andrew Kaylor | 59bbf5a | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 272 | if (F && !F->isDeclaration()) |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 273 | return M; |
| 274 | if (!CheckFunctionsOnly) { |
| 275 | GlobalVariable *G = M->getGlobalVariable(Name); |
Andrew Kaylor | 59bbf5a | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 276 | if (G && !G->isDeclaration()) |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 277 | return M; |
| 278 | // FIXME: Do we need to worry about global aliases? |
| 279 | } |
| 280 | } |
| 281 | // We didn't find the symbol in any of our modules. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 282 | return nullptr; |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | uint64_t MCJIT::getSymbolAddress(const std::string &Name, |
| 286 | bool CheckFunctionsOnly) |
| 287 | { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 288 | MutexGuard locked(lock); |
| 289 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 290 | // First, check to see if we already have this symbol. |
| 291 | uint64_t Addr = getExistingSymbolAddress(Name); |
| 292 | if (Addr) |
| 293 | return Addr; |
| 294 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 295 | for (object::OwningBinary<object::Archive> &OB : Archives) { |
| 296 | object::Archive *A = OB.getBinary(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 297 | // Look for our symbols in each Archive |
| 298 | object::Archive::child_iterator ChildIt = A->findSym(Name); |
| 299 | if (ChildIt != A->child_end()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 300 | // FIXME: Support nested archives? |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 301 | ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr = |
| 302 | ChildIt->getAsBinary(); |
| 303 | if (ChildBinOrErr.getError()) |
| 304 | continue; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 305 | std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 306 | if (ChildBin->isObject()) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 307 | std::unique_ptr<object::ObjectFile> OF( |
| 308 | static_cast<object::ObjectFile *>(ChildBin.release())); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 309 | // This causes the object file to be loaded. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 310 | addObjectFile(std::move(OF)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 311 | // The address should be here now. |
| 312 | Addr = getExistingSymbolAddress(Name); |
| 313 | if (Addr) |
| 314 | return Addr; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 319 | // If it hasn't already been generated, see if it's in one of our modules. |
| 320 | Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 321 | if (M) { |
| 322 | generateCodeForModule(M); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 323 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 324 | // Check the RuntimeDyld table again, it should be there now. |
| 325 | return getExistingSymbolAddress(Name); |
| 326 | } |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 327 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 328 | // If a LazyFunctionCreator is installed, use it to get/create the function. |
| 329 | // FIXME: Should we instead have a LazySymbolCreator callback? |
| 330 | if (LazyFunctionCreator) |
| 331 | Addr = (uint64_t)LazyFunctionCreator(Name); |
| 332 | |
| 333 | return Addr; |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 337 | MutexGuard locked(lock); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 338 | uint64_t Result = getSymbolAddress(Name, false); |
| 339 | if (Result != 0) |
| 340 | finalizeLoadedModules(); |
| 341 | return Result; |
| 342 | } |
| 343 | |
| 344 | uint64_t MCJIT::getFunctionAddress(const std::string &Name) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 345 | MutexGuard locked(lock); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 346 | uint64_t Result = getSymbolAddress(Name, true); |
| 347 | if (Result != 0) |
| 348 | finalizeLoadedModules(); |
| 349 | return Result; |
| 350 | } |
| 351 | |
| 352 | // Deprecated. Use getFunctionAddress instead. |
| 353 | void *MCJIT::getPointerToFunction(Function *F) { |
Andrew Kaylor | 6169453 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 354 | MutexGuard locked(lock); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 355 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 356 | Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); |
| 357 | SmallString<128> Name; |
| 358 | TM->getNameWithPrefix(Name, F, Mang); |
| 359 | |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 360 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 361 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 362 | void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); |
| 363 | updateGlobalMapping(F, Addr); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 364 | return Addr; |
| 365 | } |
| 366 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 367 | Module *M = F->getParent(); |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 368 | bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 369 | |
| 370 | // Make sure the relevant module has been compiled and loaded. |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 371 | if (HasBeenAddedButNotLoaded) |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 372 | generateCodeForModule(M); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 373 | else if (!OwnedModules.hasModuleBeenLoaded(M)) { |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 374 | // If this function doesn't belong to one of our modules, we're done. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 375 | // FIXME: Asking for the pointer to a function that hasn't been registered, |
| 376 | // and isn't a declaration (which is handled above) should probably |
| 377 | // be an assertion. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 378 | return nullptr; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 379 | } |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 380 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 381 | // FIXME: Should the Dyld be retaining module information? Probably not. |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 382 | // |
| 383 | // This is the accessor for the target address, so make sure to check the |
| 384 | // load address of the symbol, not the local address. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 385 | return (void*)Dyld.getSymbolLoadAddress(Name); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 388 | void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( |
| 389 | bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { |
| 390 | for (; I != E; ++I) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 391 | ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors); |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | void MCJIT::runStaticConstructorsDestructors(bool isDtors) { |
| 396 | // Execute global ctors/dtors for each module in the program. |
| 397 | runStaticConstructorsDestructorsInModulePtrSet( |
| 398 | isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 399 | runStaticConstructorsDestructorsInModulePtrSet( |
| 400 | isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); |
| 401 | runStaticConstructorsDestructorsInModulePtrSet( |
| 402 | isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); |
| 403 | } |
| 404 | |
| 405 | Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, |
| 406 | ModulePtrSet::iterator I, |
| 407 | ModulePtrSet::iterator E) { |
| 408 | for (; I != E; ++I) { |
| 409 | if (Function *F = (*I)->getFunction(FnName)) |
| 410 | return F; |
| 411 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 412 | return nullptr; |
Andrew Kaylor | 2ad18ef | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | Function *MCJIT::FindFunctionNamed(const char *FnName) { |
| 416 | Function *F = FindFunctionNamedInModulePtrSet( |
| 417 | FnName, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 418 | if (!F) |
| 419 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), |
| 420 | OwnedModules.end_loaded()); |
| 421 | if (!F) |
| 422 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), |
| 423 | OwnedModules.end_finalized()); |
| 424 | return F; |
| 425 | } |
| 426 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 427 | GenericValue MCJIT::runFunction(Function *F, |
| 428 | const std::vector<GenericValue> &ArgValues) { |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 429 | assert(F && "Function *F was null at entry to run()"); |
| 430 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 431 | void *FPtr = getPointerToFunction(F); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 432 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 433 | FunctionType *FTy = F->getFunctionType(); |
| 434 | Type *RetTy = FTy->getReturnType(); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 435 | |
| 436 | assert((FTy->getNumParams() == ArgValues.size() || |
| 437 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 438 | "Wrong number of arguments passed into function!"); |
| 439 | assert(FTy->getNumParams() == ArgValues.size() && |
| 440 | "This doesn't support passing arguments through varargs (yet)!"); |
| 441 | |
| 442 | // Handle some common cases first. These cases correspond to common `main' |
| 443 | // prototypes. |
| 444 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 445 | switch (ArgValues.size()) { |
| 446 | case 3: |
| 447 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 448 | FTy->getParamType(1)->isPointerTy() && |
| 449 | FTy->getParamType(2)->isPointerTy()) { |
| 450 | int (*PF)(int, char **, const char **) = |
| 451 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 452 | |
| 453 | // Call the function. |
| 454 | GenericValue rv; |
| 455 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 456 | (char **)GVTOP(ArgValues[1]), |
| 457 | (const char **)GVTOP(ArgValues[2]))); |
| 458 | return rv; |
| 459 | } |
| 460 | break; |
| 461 | case 2: |
| 462 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 463 | FTy->getParamType(1)->isPointerTy()) { |
| 464 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 465 | |
| 466 | // Call the function. |
| 467 | GenericValue rv; |
| 468 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 469 | (char **)GVTOP(ArgValues[1]))); |
| 470 | return rv; |
| 471 | } |
| 472 | break; |
| 473 | case 1: |
| 474 | if (FTy->getNumParams() == 1 && |
| 475 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 476 | GenericValue rv; |
| 477 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 478 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 479 | return rv; |
| 480 | } |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Handle cases where no arguments are passed first. |
| 486 | if (ArgValues.empty()) { |
| 487 | GenericValue rv; |
| 488 | switch (RetTy->getTypeID()) { |
| 489 | default: llvm_unreachable("Unknown return type for function call!"); |
| 490 | case Type::IntegerTyID: { |
| 491 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 492 | if (BitWidth == 1) |
| 493 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 494 | else if (BitWidth <= 8) |
| 495 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 496 | else if (BitWidth <= 16) |
| 497 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 498 | else if (BitWidth <= 32) |
| 499 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 500 | else if (BitWidth <= 64) |
| 501 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 502 | else |
| 503 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 504 | return rv; |
| 505 | } |
| 506 | case Type::VoidTyID: |
| 507 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 508 | return rv; |
| 509 | case Type::FloatTyID: |
| 510 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 511 | return rv; |
| 512 | case Type::DoubleTyID: |
| 513 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 514 | return rv; |
| 515 | case Type::X86_FP80TyID: |
| 516 | case Type::FP128TyID: |
| 517 | case Type::PPC_FP128TyID: |
| 518 | llvm_unreachable("long double not supported yet"); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 519 | case Type::PointerTyID: |
| 520 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 521 | } |
| 522 | } |
| 523 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 524 | llvm_unreachable("Full-featured argument passing not supported yet!"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 525 | } |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 526 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 527 | void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 528 | if (!isSymbolSearchingDisabled()) { |
| 529 | void *ptr = MemMgr.getPointerToNamedFunction(Name, false); |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 530 | if (ptr) |
| 531 | return ptr; |
| 532 | } |
| 533 | |
| 534 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 535 | if (LazyFunctionCreator) |
| 536 | if (void *RP = LazyFunctionCreator(Name)) |
| 537 | return RP; |
| 538 | |
| 539 | if (AbortOnFailure) { |
| 540 | report_fatal_error("Program used external function '"+Name+ |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 541 | "' which could not be resolved!"); |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 542 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 543 | return nullptr; |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 544 | } |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 545 | |
| 546 | void MCJIT::RegisterJITEventListener(JITEventListener *L) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 547 | if (!L) |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 548 | return; |
| 549 | MutexGuard locked(lock); |
| 550 | EventListeners.push_back(L); |
| 551 | } |
| 552 | void MCJIT::UnregisterJITEventListener(JITEventListener *L) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 553 | if (!L) |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 554 | return; |
| 555 | MutexGuard locked(lock); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 556 | auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L); |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 557 | if (I != EventListeners.rend()) { |
| 558 | std::swap(*I, EventListeners.back()); |
| 559 | EventListeners.pop_back(); |
| 560 | } |
| 561 | } |
| 562 | void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { |
| 563 | MutexGuard locked(lock); |
Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 564 | MemMgr.notifyObjectLoaded(this, &Obj); |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 565 | for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { |
| 566 | EventListeners[I]->NotifyObjectEmitted(Obj); |
| 567 | } |
| 568 | } |
| 569 | void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { |
| 570 | MutexGuard locked(lock); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 571 | for (JITEventListener *L : EventListeners) |
| 572 | L->NotifyFreeingObject(Obj); |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 573 | } |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 574 | |
| 575 | uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) { |
| 576 | uint64_t Result = ParentEngine->getSymbolAddress(Name, false); |
Andrew Kaylor | 52c9016 | 2013-10-01 16:42:50 +0000 | [diff] [blame] | 577 | // If the symbols wasn't found and it begins with an underscore, try again |
| 578 | // without the underscore. |
| 579 | if (!Result && Name[0] == '_') |
| 580 | Result = ParentEngine->getSymbolAddress(Name.substr(1), false); |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 581 | if (Result) |
| 582 | return Result; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 583 | if (ParentEngine->isSymbolSearchingDisabled()) |
| 584 | return 0; |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 585 | return ClientMM->getSymbolAddress(Name); |
| 586 | } |