Eric Christopher | 5c896f7 | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 7e5d8a7 | 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" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/GenericValue.h" |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/MCJIT.h" |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/Function.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Mangler.h" |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 22 | #include "llvm/Object/Archive.h" |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MutexGuard.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | static struct RegisterJIT { |
| 34 | RegisterJIT() { MCJIT::Register(); } |
| 35 | } JITRegistrator; |
| 36 | |
| 37 | } |
| 38 | |
| 39 | extern "C" void LLVMLinkInMCJIT() { |
| 40 | } |
| 41 | |
Lang Hames | b72f484 | 2018-01-19 22:24:13 +0000 | [diff] [blame] | 42 | ExecutionEngine * |
| 43 | MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 44 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | b72f484 | 2018-01-19 22:24:13 +0000 | [diff] [blame] | 45 | std::shared_ptr<LegacyJITSymbolResolver> Resolver, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 46 | std::unique_ptr<TargetMachine> TM) { |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 47 | // Try to register the program as a source of symbols to resolve against. |
| 48 | // |
| 49 | // FIXME: Don't do this here. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 50 | sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 51 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 52 | if (!MemMgr || !Resolver) { |
| 53 | auto RTDyldMM = std::make_shared<SectionMemoryManager>(); |
| 54 | if (!MemMgr) |
| 55 | MemMgr = RTDyldMM; |
| 56 | if (!Resolver) |
| 57 | Resolver = RTDyldMM; |
| 58 | } |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 59 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 60 | return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr), |
| 61 | std::move(Resolver)); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 64 | MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 65 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | b72f484 | 2018-01-19 22:24:13 +0000 | [diff] [blame] | 66 | std::shared_ptr<LegacyJITSymbolResolver> Resolver) |
Mehdi Amini | 26d4813 | 2015-07-24 16:04:22 +0000 | [diff] [blame] | 67 | : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)), |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 68 | Ctx(nullptr), MemMgr(std::move(MemMgr)), |
| 69 | Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver), |
| 70 | ObjCache(nullptr) { |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 71 | // FIXME: We are managing our modules, so we do not want the base class |
| 72 | // ExecutionEngine to manage them as well. To avoid double destruction |
| 73 | // of the first (and only) module added in ExecutionEngine constructor |
| 74 | // we remove it from EE and will destruct it ourselves. |
| 75 | // |
| 76 | // It may make sense to move our module manager (based on SmallStPtr) back |
| 77 | // into EE if the JIT and Interpreter can live with it. |
| 78 | // If so, additional functions: addModule, removeModule, FindFunctionNamed, |
| 79 | // runStaticConstructorsDestructors could be moved back to EE as well. |
| 80 | // |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 81 | std::unique_ptr<Module> First = std::move(Modules[0]); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 82 | Modules.clear(); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 83 | |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 84 | if (First->getDataLayout().isDefault()) |
| 85 | First->setDataLayout(getDataLayout()); |
| 86 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 87 | OwnedModules.addModule(std::move(First)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 88 | RegisterJITEventListener(JITEventListener::createGDBRegistrationListener()); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | MCJIT::~MCJIT() { |
| 92 | MutexGuard locked(lock); |
| 93 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 94 | Dyld.deregisterEHFrames(); |
Chandler Carruth | d55d159 | 2013-10-24 09:52:56 +0000 | [diff] [blame] | 95 | |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 96 | for (auto &Obj : LoadedObjects) |
| 97 | if (Obj) |
Chandler Carruth | d55d159 | 2013-10-24 09:52:56 +0000 | [diff] [blame] | 98 | NotifyFreeingObject(*Obj); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 99 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 100 | Archives.clear(); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 103 | void MCJIT::addModule(std::unique_ptr<Module> M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 104 | MutexGuard locked(lock); |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 105 | |
| 106 | if (M->getDataLayout().isDefault()) |
| 107 | M->setDataLayout(getDataLayout()); |
| 108 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 109 | OwnedModules.addModule(std::move(M)); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 112 | bool MCJIT::removeModule(Module *M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 113 | MutexGuard locked(lock); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 114 | return OwnedModules.removeModule(M); |
| 115 | } |
| 116 | |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 117 | void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 118 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj); |
| 119 | if (Dyld.hasError()) |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 120 | report_fatal_error(Dyld.getErrorString()); |
| 121 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 122 | NotifyObjectEmitted(*Obj, *L); |
David Blaikie | 168861a | 2014-09-04 18:37:31 +0000 | [diff] [blame] | 123 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 124 | LoadedObjects.push_back(std::move(Obj)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 127 | void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) { |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 128 | std::unique_ptr<object::ObjectFile> ObjFile; |
| 129 | std::unique_ptr<MemoryBuffer> MemBuf; |
| 130 | std::tie(ObjFile, MemBuf) = Obj.takeBinary(); |
| 131 | addObjectFile(std::move(ObjFile)); |
| 132 | Buffers.push_back(std::move(MemBuf)); |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 135 | void MCJIT::addArchive(object::OwningBinary<object::Archive> A) { |
Rafael Espindola | ce47a05 | 2014-08-01 18:09:32 +0000 | [diff] [blame] | 136 | Archives.push_back(std::move(A)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 139 | void MCJIT::setObjectCache(ObjectCache* NewCache) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 140 | MutexGuard locked(lock); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 141 | ObjCache = NewCache; |
| 142 | } |
| 143 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 144 | std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) { |
Lang Hames | e7989ef | 2018-06-12 20:43:15 +0000 | [diff] [blame^] | 145 | assert(M && "Can not emit a null module"); |
| 146 | |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 147 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 148 | |
Lang Hames | e7989ef | 2018-06-12 20:43:15 +0000 | [diff] [blame^] | 149 | // Materialize all globals in the module if they have not been |
| 150 | // materialized already. |
| 151 | cantFail(M->materializeAll()); |
| 152 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 153 | // This must be a module which has already been added but not loaded to this |
| 154 | // MCJIT instance, since these conditions are tested by our caller, |
| 155 | // generateCodeForModule. |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 156 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 157 | legacy::PassManager PM; |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 158 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 159 | // The RuntimeDyld will take ownership of this shortly |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 160 | SmallVector<char, 4096> ObjBufferSV; |
| 161 | raw_svector_ostream ObjStream(ObjBufferSV); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 162 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 163 | // Turn the machine code intermediate representation into bytes in memory |
| 164 | // that may be executed. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 165 | if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules())) |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 166 | report_fatal_error("Target does not support MC emission!"); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 167 | |
| 168 | // Initialize passes. |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 169 | PM.run(*M); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 170 | // Flush the output buffer to get the generated code into memory |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 171 | |
| 172 | std::unique_ptr<MemoryBuffer> CompiledObjBuffer( |
Weiming Zhao | 79f2d09 | 2018-04-16 03:44:03 +0000 | [diff] [blame] | 173 | new SmallVectorMemoryBuffer(std::move(ObjBufferSV))); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 174 | |
| 175 | // If we have an object cache, tell it about the new object. |
| 176 | // Note that we're using the compiled image, not the loaded image (as below). |
| 177 | if (ObjCache) { |
| 178 | // MemoryBuffer is a thin wrapper around the actual memory, so it's OK |
| 179 | // to create a temporary object here and delete it after the call. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 180 | MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef(); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 181 | ObjCache->notifyObjectCompiled(M, MB); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 184 | return CompiledObjBuffer; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 187 | void MCJIT::generateCodeForModule(Module *M) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 188 | // Get a thread lock to make sure we aren't trying to load multiple times |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 189 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 190 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 191 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 192 | assert(OwnedModules.ownsModule(M) && |
| 193 | "MCJIT::generateCodeForModule: Unknown module."); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 194 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 195 | // Re-compilation is not supported |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 196 | if (OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 197 | return; |
| 198 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 199 | std::unique_ptr<MemoryBuffer> ObjectToLoad; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 200 | // Try to load the pre-compiled object from cache if possible |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 201 | if (ObjCache) |
| 202 | ObjectToLoad = ObjCache->getObject(M); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 203 | |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 204 | assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch"); |
Rafael Espindola | 6763f5a | 2015-06-23 14:42:34 +0000 | [diff] [blame] | 205 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 206 | // If the cache did not contain a suitable object, compile the object |
| 207 | if (!ObjectToLoad) { |
David Blaikie | d110157 | 2014-09-03 19:57:35 +0000 | [diff] [blame] | 208 | ObjectToLoad = emitObject(M); |
| 209 | assert(ObjectToLoad && "Compilation did not produce an object."); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 210 | } |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 211 | |
| 212 | // Load the object into the dynamic linker. |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 213 | // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 214 | Expected<std::unique_ptr<object::ObjectFile>> LoadedObject = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 215 | object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef()); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 216 | if (!LoadedObject) { |
| 217 | std::string Buf; |
| 218 | raw_string_ostream OS(Buf); |
| 219 | logAllUnhandledErrors(LoadedObject.takeError(), OS, ""); |
| 220 | OS.flush(); |
| 221 | report_fatal_error(Buf); |
| 222 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 223 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = |
| 224 | Dyld.loadObject(*LoadedObject.get()); |
| 225 | |
| 226 | if (Dyld.hasError()) |
Jim Grosbach | c114d89 | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 227 | report_fatal_error(Dyld.getErrorString()); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 228 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 229 | NotifyObjectEmitted(*LoadedObject.get(), *L); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 230 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 231 | Buffers.push_back(std::move(ObjectToLoad)); |
| 232 | LoadedObjects.push_back(std::move(*LoadedObject)); |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 233 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 234 | OwnedModules.markModuleAsLoaded(M); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 237 | void MCJIT::finalizeLoadedModules() { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 238 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 239 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 240 | // Resolve any outstanding relocations. |
| 241 | Dyld.resolveRelocations(); |
| 242 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 243 | OwnedModules.markAllLoadedModulesAsFinalized(); |
| 244 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 245 | // Register EH frame data for any module we own which has been loaded |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 246 | Dyld.registerEHFrames(); |
| 247 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 248 | // Set page permissions. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 249 | MemMgr->finalizeMemory(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // FIXME: Rename this. |
| 253 | void MCJIT::finalizeObject() { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 254 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 255 | |
Lang Hames | 018452e | 2014-09-05 23:38:35 +0000 | [diff] [blame] | 256 | // Generate code for module is going to move objects out of the 'added' list, |
| 257 | // so we need to copy that out before using it: |
| 258 | SmallVector<Module*, 16> ModsToAdd; |
| 259 | for (auto M : OwnedModules.added()) |
| 260 | ModsToAdd.push_back(M); |
| 261 | |
| 262 | for (auto M : ModsToAdd) |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 263 | generateCodeForModule(M); |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 264 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 265 | finalizeLoadedModules(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void MCJIT::finalizeModule(Module *M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 269 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 270 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 271 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 272 | assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 273 | |
| 274 | // If the module hasn't been compiled, just do that. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 275 | if (!OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 276 | generateCodeForModule(M); |
| 277 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 278 | finalizeLoadedModules(); |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 281 | JITSymbol MCJIT::findExistingSymbol(const std::string &Name) { |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 282 | if (void *Addr = getPointerToGlobalIfAvailable(Name)) |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 283 | return JITSymbol(static_cast<uint64_t>( |
| 284 | reinterpret_cast<uintptr_t>(Addr)), |
| 285 | JITSymbolFlags::Exported); |
Lang Hames | 3393cfd | 2015-07-29 23:12:33 +0000 | [diff] [blame] | 286 | |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 287 | return Dyld.getSymbol(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 288 | } |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 289 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 290 | Module *MCJIT::findModuleForSymbol(const std::string &Name, |
| 291 | bool CheckFunctionsOnly) { |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 292 | StringRef DemangledName = Name; |
| 293 | if (DemangledName[0] == getDataLayout().getGlobalPrefix()) |
| 294 | DemangledName = DemangledName.substr(1); |
| 295 | |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 296 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 297 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 298 | // If it hasn't already been generated, see if it's in one of our modules. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 299 | for (ModulePtrSet::iterator I = OwnedModules.begin_added(), |
| 300 | E = OwnedModules.end_added(); |
| 301 | I != E; ++I) { |
| 302 | Module *M = *I; |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 303 | Function *F = M->getFunction(DemangledName); |
Andrew Kaylor | 515b1da | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 304 | if (F && !F->isDeclaration()) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 305 | return M; |
| 306 | if (!CheckFunctionsOnly) { |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 307 | GlobalVariable *G = M->getGlobalVariable(DemangledName); |
Andrew Kaylor | 515b1da | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 308 | if (G && !G->isDeclaration()) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 309 | return M; |
| 310 | // FIXME: Do we need to worry about global aliases? |
| 311 | } |
| 312 | } |
| 313 | // We didn't find the symbol in any of our modules. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 314 | return nullptr; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | uint64_t MCJIT::getSymbolAddress(const std::string &Name, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 318 | bool CheckFunctionsOnly) { |
Lang Hames | 8d4be3a | 2016-09-12 17:19:24 +0000 | [diff] [blame] | 319 | std::string MangledName; |
| 320 | { |
| 321 | raw_string_ostream MangledNameStream(MangledName); |
| 322 | Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout()); |
| 323 | } |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 324 | if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) { |
| 325 | if (auto AddrOrErr = Sym.getAddress()) |
| 326 | return *AddrOrErr; |
| 327 | else |
| 328 | report_fatal_error(AddrOrErr.takeError()); |
| 329 | } else |
| 330 | report_fatal_error(Sym.takeError()); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 333 | JITSymbol MCJIT::findSymbol(const std::string &Name, |
| 334 | bool CheckFunctionsOnly) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 335 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 336 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 337 | // First, check to see if we already have this symbol. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 338 | if (auto Sym = findExistingSymbol(Name)) |
| 339 | return Sym; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 340 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 341 | for (object::OwningBinary<object::Archive> &OB : Archives) { |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 342 | object::Archive *A = OB.getBinary(); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 343 | // Look for our symbols in each Archive |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 344 | auto OptionalChildOrErr = A->findSym(Name); |
| 345 | if (!OptionalChildOrErr) |
| 346 | report_fatal_error(OptionalChildOrErr.takeError()); |
| 347 | auto &OptionalChild = *OptionalChildOrErr; |
| 348 | if (OptionalChild) { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 349 | // FIXME: Support nested archives? |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 350 | Expected<std::unique_ptr<object::Binary>> ChildBinOrErr = |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 351 | OptionalChild->getAsBinary(); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 352 | if (!ChildBinOrErr) { |
| 353 | // TODO: Actually report errors helpfully. |
| 354 | consumeError(ChildBinOrErr.takeError()); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 355 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 356 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 357 | std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 358 | if (ChildBin->isObject()) { |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 359 | std::unique_ptr<object::ObjectFile> OF( |
| 360 | static_cast<object::ObjectFile *>(ChildBin.release())); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 361 | // This causes the object file to be loaded. |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 362 | addObjectFile(std::move(OF)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 363 | // The address should be here now. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 364 | if (auto Sym = findExistingSymbol(Name)) |
| 365 | return Sym; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 370 | // If it hasn't already been generated, see if it's in one of our modules. |
| 371 | Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 372 | if (M) { |
| 373 | generateCodeForModule(M); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 374 | |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 375 | // Check the RuntimeDyld table again, it should be there now. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 376 | return findExistingSymbol(Name); |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 377 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 378 | |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 379 | // If a LazyFunctionCreator is installed, use it to get/create the function. |
| 380 | // FIXME: Should we instead have a LazySymbolCreator callback? |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 381 | if (LazyFunctionCreator) { |
| 382 | auto Addr = static_cast<uint64_t>( |
| 383 | reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name))); |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 384 | return JITSymbol(Addr, JITSymbolFlags::Exported); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 385 | } |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 386 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 387 | return nullptr; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 391 | MutexGuard locked(lock); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 392 | uint64_t Result = getSymbolAddress(Name, false); |
| 393 | if (Result != 0) |
| 394 | finalizeLoadedModules(); |
| 395 | return Result; |
| 396 | } |
| 397 | |
| 398 | uint64_t MCJIT::getFunctionAddress(const std::string &Name) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 399 | MutexGuard locked(lock); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 400 | uint64_t Result = getSymbolAddress(Name, true); |
| 401 | if (Result != 0) |
| 402 | finalizeLoadedModules(); |
| 403 | return Result; |
| 404 | } |
| 405 | |
| 406 | // Deprecated. Use getFunctionAddress instead. |
| 407 | void *MCJIT::getPointerToFunction(Function *F) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 408 | MutexGuard locked(lock); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 409 | |
Rafael Espindola | c233f74 | 2015-06-23 13:59:29 +0000 | [diff] [blame] | 410 | Mangler Mang; |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 411 | SmallString<128> Name; |
| 412 | TM->getNameWithPrefix(Name, F, Mang); |
| 413 | |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 414 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 415 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 416 | void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); |
Lang Hames | efe7e22 | 2014-10-22 23:18:42 +0000 | [diff] [blame] | 417 | updateGlobalMapping(F, Addr); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 418 | return Addr; |
| 419 | } |
| 420 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 421 | Module *M = F->getParent(); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 422 | bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 423 | |
| 424 | // Make sure the relevant module has been compiled and loaded. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 425 | if (HasBeenAddedButNotLoaded) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 426 | generateCodeForModule(M); |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 427 | else if (!OwnedModules.hasModuleBeenLoaded(M)) { |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 428 | // If this function doesn't belong to one of our modules, we're done. |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 429 | // FIXME: Asking for the pointer to a function that hasn't been registered, |
| 430 | // and isn't a declaration (which is handled above) should probably |
| 431 | // be an assertion. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 432 | return nullptr; |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 433 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 434 | |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 435 | // FIXME: Should the Dyld be retaining module information? Probably not. |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 436 | // |
| 437 | // This is the accessor for the target address, so make sure to check the |
| 438 | // load address of the symbol, not the local address. |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 439 | return (void*)Dyld.getSymbol(Name).getAddress(); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 442 | void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( |
| 443 | bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { |
| 444 | for (; I != E; ++I) { |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 445 | ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | void MCJIT::runStaticConstructorsDestructors(bool isDtors) { |
| 450 | // Execute global ctors/dtors for each module in the program. |
| 451 | runStaticConstructorsDestructorsInModulePtrSet( |
| 452 | isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 453 | runStaticConstructorsDestructorsInModulePtrSet( |
| 454 | isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); |
| 455 | runStaticConstructorsDestructorsInModulePtrSet( |
| 456 | isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); |
| 457 | } |
| 458 | |
Mehdi Amini | 7419e94 | 2016-10-01 06:22:04 +0000 | [diff] [blame] | 459 | Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName, |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 460 | ModulePtrSet::iterator I, |
| 461 | ModulePtrSet::iterator E) { |
| 462 | for (; I != E; ++I) { |
Keno Fischer | 5f92a08 | 2015-01-27 19:29:00 +0000 | [diff] [blame] | 463 | Function *F = (*I)->getFunction(FnName); |
| 464 | if (F && !F->isDeclaration()) |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 465 | return F; |
| 466 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 467 | return nullptr; |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Mehdi Amini | 7419e94 | 2016-10-01 06:22:04 +0000 | [diff] [blame] | 470 | GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name, |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 471 | bool AllowInternal, |
| 472 | ModulePtrSet::iterator I, |
| 473 | ModulePtrSet::iterator E) { |
| 474 | for (; I != E; ++I) { |
| 475 | GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal); |
| 476 | if (GV && !GV->isDeclaration()) |
| 477 | return GV; |
| 478 | } |
| 479 | return nullptr; |
| 480 | } |
| 481 | |
| 482 | |
Mehdi Amini | 7419e94 | 2016-10-01 06:22:04 +0000 | [diff] [blame] | 483 | Function *MCJIT::FindFunctionNamed(StringRef FnName) { |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 484 | Function *F = FindFunctionNamedInModulePtrSet( |
| 485 | FnName, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 486 | if (!F) |
| 487 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), |
| 488 | OwnedModules.end_loaded()); |
| 489 | if (!F) |
| 490 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), |
| 491 | OwnedModules.end_finalized()); |
| 492 | return F; |
| 493 | } |
| 494 | |
Mehdi Amini | 7419e94 | 2016-10-01 06:22:04 +0000 | [diff] [blame] | 495 | GlobalVariable *MCJIT::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) { |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 496 | GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet( |
| 497 | Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 498 | if (!GV) |
| 499 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(), |
| 500 | OwnedModules.end_loaded()); |
| 501 | if (!GV) |
| 502 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(), |
| 503 | OwnedModules.end_finalized()); |
| 504 | return GV; |
| 505 | } |
| 506 | |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 507 | GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) { |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 508 | assert(F && "Function *F was null at entry to run()"); |
| 509 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 510 | void *FPtr = getPointerToFunction(F); |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 511 | finalizeModule(F->getParent()); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 512 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 513 | FunctionType *FTy = F->getFunctionType(); |
| 514 | Type *RetTy = FTy->getReturnType(); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 515 | |
| 516 | assert((FTy->getNumParams() == ArgValues.size() || |
| 517 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 518 | "Wrong number of arguments passed into function!"); |
| 519 | assert(FTy->getNumParams() == ArgValues.size() && |
| 520 | "This doesn't support passing arguments through varargs (yet)!"); |
| 521 | |
| 522 | // Handle some common cases first. These cases correspond to common `main' |
| 523 | // prototypes. |
| 524 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 525 | switch (ArgValues.size()) { |
| 526 | case 3: |
| 527 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 528 | FTy->getParamType(1)->isPointerTy() && |
| 529 | FTy->getParamType(2)->isPointerTy()) { |
| 530 | int (*PF)(int, char **, const char **) = |
| 531 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 532 | |
| 533 | // Call the function. |
| 534 | GenericValue rv; |
| 535 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 536 | (char **)GVTOP(ArgValues[1]), |
| 537 | (const char **)GVTOP(ArgValues[2]))); |
| 538 | return rv; |
| 539 | } |
| 540 | break; |
| 541 | case 2: |
| 542 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 543 | FTy->getParamType(1)->isPointerTy()) { |
| 544 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 545 | |
| 546 | // Call the function. |
| 547 | GenericValue rv; |
| 548 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 549 | (char **)GVTOP(ArgValues[1]))); |
| 550 | return rv; |
| 551 | } |
| 552 | break; |
| 553 | case 1: |
| 554 | if (FTy->getNumParams() == 1 && |
| 555 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 556 | GenericValue rv; |
| 557 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 558 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 559 | return rv; |
| 560 | } |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // Handle cases where no arguments are passed first. |
| 566 | if (ArgValues.empty()) { |
| 567 | GenericValue rv; |
| 568 | switch (RetTy->getTypeID()) { |
| 569 | default: llvm_unreachable("Unknown return type for function call!"); |
| 570 | case Type::IntegerTyID: { |
| 571 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 572 | if (BitWidth == 1) |
| 573 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 574 | else if (BitWidth <= 8) |
| 575 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 576 | else if (BitWidth <= 16) |
| 577 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 578 | else if (BitWidth <= 32) |
| 579 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 580 | else if (BitWidth <= 64) |
| 581 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 582 | else |
| 583 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 584 | return rv; |
| 585 | } |
| 586 | case Type::VoidTyID: |
| 587 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 588 | return rv; |
| 589 | case Type::FloatTyID: |
| 590 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 591 | return rv; |
| 592 | case Type::DoubleTyID: |
| 593 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 594 | return rv; |
| 595 | case Type::X86_FP80TyID: |
| 596 | case Type::FP128TyID: |
| 597 | case Type::PPC_FP128TyID: |
| 598 | llvm_unreachable("long double not supported yet"); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 599 | case Type::PointerTyID: |
| 600 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 601 | } |
| 602 | } |
| 603 | |
Lang Hames | 3052607 | 2016-08-11 15:56:23 +0000 | [diff] [blame] | 604 | report_fatal_error("MCJIT::runFunction does not support full-featured " |
| 605 | "argument passing. Please use " |
| 606 | "ExecutionEngine::getFunctionAddress and cast the result " |
| 607 | "to the desired function pointer type."); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 608 | } |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 609 | |
Lang Hames | 9a78334 | 2014-09-15 17:50:22 +0000 | [diff] [blame] | 610 | void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 611 | if (!isSymbolSearchingDisabled()) { |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 612 | if (auto Sym = Resolver.findSymbol(Name)) { |
| 613 | if (auto AddrOrErr = Sym.getAddress()) |
| 614 | return reinterpret_cast<void*>( |
| 615 | static_cast<uintptr_t>(*AddrOrErr)); |
| 616 | } else if (auto Err = Sym.takeError()) |
| 617 | report_fatal_error(std::move(Err)); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 621 | if (LazyFunctionCreator) |
| 622 | if (void *RP = LazyFunctionCreator(Name)) |
| 623 | return RP; |
| 624 | |
| 625 | if (AbortOnFailure) { |
| 626 | report_fatal_error("Program used external function '"+Name+ |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 627 | "' which could not be resolved!"); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 628 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 629 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 630 | } |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 631 | |
| 632 | void MCJIT::RegisterJITEventListener(JITEventListener *L) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 633 | if (!L) |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 634 | return; |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 635 | MutexGuard locked(lock); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 636 | EventListeners.push_back(L); |
| 637 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 638 | |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 639 | void MCJIT::UnregisterJITEventListener(JITEventListener *L) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 640 | if (!L) |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 641 | return; |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 642 | MutexGuard locked(lock); |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 643 | auto I = find(reverse(EventListeners), L); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 644 | if (I != EventListeners.rend()) { |
| 645 | std::swap(*I, EventListeners.back()); |
| 646 | EventListeners.pop_back(); |
| 647 | } |
| 648 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 649 | |
| 650 | void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj, |
| 651 | const RuntimeDyld::LoadedObjectInfo &L) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 652 | MutexGuard locked(lock); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 653 | MemMgr->notifyObjectLoaded(this, Obj); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 654 | for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 655 | EventListeners[I]->NotifyObjectEmitted(Obj, L); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 658 | |
| 659 | void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 660 | MutexGuard locked(lock); |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 661 | for (JITEventListener *L : EventListeners) |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 662 | L->NotifyFreeingObject(Obj); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 663 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 664 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 665 | JITSymbol |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 666 | LinkingSymbolResolver::findSymbol(const std::string &Name) { |
| 667 | auto Result = ParentEngine.findSymbol(Name, false); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 668 | if (Result) |
| 669 | return Result; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 670 | if (ParentEngine.isSymbolSearchingDisabled()) |
| 671 | return nullptr; |
| 672 | return ClientResolver->findSymbol(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 673 | } |
Weiming Zhao | 1bd4000 | 2018-04-11 23:09:20 +0000 | [diff] [blame] | 674 | |
| 675 | void LinkingSymbolResolver::anchor() {} |