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