LLVM  mainline
MCJIT.cpp
Go to the documentation of this file.
00001 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "MCJIT.h"
00011 #include "llvm/ADT/STLExtras.h"
00012 #include "llvm/ExecutionEngine/GenericValue.h"
00013 #include "llvm/ExecutionEngine/JITEventListener.h"
00014 #include "llvm/ExecutionEngine/MCJIT.h"
00015 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
00016 #include "llvm/IR/DataLayout.h"
00017 #include "llvm/IR/DerivedTypes.h"
00018 #include "llvm/IR/Function.h"
00019 #include "llvm/IR/LegacyPassManager.h"
00020 #include "llvm/IR/Mangler.h"
00021 #include "llvm/IR/Module.h"
00022 #include "llvm/MC/MCAsmInfo.h"
00023 #include "llvm/Object/Archive.h"
00024 #include "llvm/Object/ObjectFile.h"
00025 #include "llvm/Support/DynamicLibrary.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Support/MemoryBuffer.h"
00028 #include "llvm/Support/MutexGuard.h"
00029 
00030 using namespace llvm;
00031 
00032 void ObjectCache::anchor() {}
00033 
00034 namespace {
00035 
00036 static struct RegisterJIT {
00037   RegisterJIT() { MCJIT::Register(); }
00038 } JITRegistrator;
00039 
00040 }
00041 
00042 extern "C" void LLVMLinkInMCJIT() {
00043 }
00044 
00045 ExecutionEngine*
00046 MCJIT::createJIT(std::unique_ptr<Module> M,
00047                  std::string *ErrorStr,
00048                  std::shared_ptr<MCJITMemoryManager> MemMgr,
00049                  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
00050                  std::unique_ptr<TargetMachine> TM) {
00051   // Try to register the program as a source of symbols to resolve against.
00052   //
00053   // FIXME: Don't do this here.
00054   sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
00055 
00056   if (!MemMgr || !Resolver) {
00057     auto RTDyldMM = std::make_shared<SectionMemoryManager>();
00058     if (!MemMgr)
00059       MemMgr = RTDyldMM;
00060     if (!Resolver)
00061       Resolver = RTDyldMM;
00062   }
00063 
00064   return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
00065                    std::move(Resolver));
00066 }
00067 
00068 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
00069              std::shared_ptr<MCJITMemoryManager> MemMgr,
00070              std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
00071     : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
00072       MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
00073       Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
00074   // FIXME: We are managing our modules, so we do not want the base class
00075   // ExecutionEngine to manage them as well. To avoid double destruction
00076   // of the first (and only) module added in ExecutionEngine constructor
00077   // we remove it from EE and will destruct it ourselves.
00078   //
00079   // It may make sense to move our module manager (based on SmallStPtr) back
00080   // into EE if the JIT and Interpreter can live with it.
00081   // If so, additional functions: addModule, removeModule, FindFunctionNamed,
00082   // runStaticConstructorsDestructors could be moved back to EE as well.
00083   //
00084   std::unique_ptr<Module> First = std::move(Modules[0]);
00085   Modules.clear();
00086 
00087   OwnedModules.addModule(std::move(First));
00088   setDataLayout(TM->getDataLayout());
00089   RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
00090 }
00091 
00092 MCJIT::~MCJIT() {
00093   MutexGuard locked(lock);
00094 
00095   Dyld.deregisterEHFrames();
00096 
00097   for (auto &Obj : LoadedObjects)
00098     if (Obj)
00099       NotifyFreeingObject(*Obj);
00100 
00101   Archives.clear();
00102 }
00103 
00104 void MCJIT::addModule(std::unique_ptr<Module> M) {
00105   MutexGuard locked(lock);
00106   OwnedModules.addModule(std::move(M));
00107 }
00108 
00109 bool MCJIT::removeModule(Module *M) {
00110   MutexGuard locked(lock);
00111   return OwnedModules.removeModule(M);
00112 }
00113 
00114 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
00115   std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
00116   if (Dyld.hasError())
00117     report_fatal_error(Dyld.getErrorString());
00118 
00119   NotifyObjectEmitted(*Obj, *L);
00120 
00121   LoadedObjects.push_back(std::move(Obj));
00122 }
00123 
00124 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
00125   std::unique_ptr<object::ObjectFile> ObjFile;
00126   std::unique_ptr<MemoryBuffer> MemBuf;
00127   std::tie(ObjFile, MemBuf) = Obj.takeBinary();
00128   addObjectFile(std::move(ObjFile));
00129   Buffers.push_back(std::move(MemBuf));
00130 }
00131 
00132 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
00133   Archives.push_back(std::move(A));
00134 }
00135 
00136 void MCJIT::setObjectCache(ObjectCache* NewCache) {
00137   MutexGuard locked(lock);
00138   ObjCache = NewCache;
00139 }
00140 
00141 std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
00142   MutexGuard locked(lock);
00143 
00144   // This must be a module which has already been added but not loaded to this
00145   // MCJIT instance, since these conditions are tested by our caller,
00146   // generateCodeForModule.
00147 
00148   legacy::PassManager PM;
00149 
00150   M->setDataLayout(*TM->getDataLayout());
00151 
00152   // The RuntimeDyld will take ownership of this shortly
00153   SmallVector<char, 4096> ObjBufferSV;
00154   raw_svector_ostream ObjStream(ObjBufferSV);
00155 
00156   // Turn the machine code intermediate representation into bytes in memory
00157   // that may be executed.
00158   if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
00159     report_fatal_error("Target does not support MC emission!");
00160 
00161   // Initialize passes.
00162   PM.run(*M);
00163   // Flush the output buffer to get the generated code into memory
00164   ObjStream.flush();
00165 
00166   std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
00167                                 new ObjectMemoryBuffer(std::move(ObjBufferSV)));
00168 
00169   // If we have an object cache, tell it about the new object.
00170   // Note that we're using the compiled image, not the loaded image (as below).
00171   if (ObjCache) {
00172     // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
00173     // to create a temporary object here and delete it after the call.
00174     MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
00175     ObjCache->notifyObjectCompiled(M, MB);
00176   }
00177 
00178   return CompiledObjBuffer;
00179 }
00180 
00181 void MCJIT::generateCodeForModule(Module *M) {
00182   // Get a thread lock to make sure we aren't trying to load multiple times
00183   MutexGuard locked(lock);
00184 
00185   // This must be a module which has already been added to this MCJIT instance.
00186   assert(OwnedModules.ownsModule(M) &&
00187          "MCJIT::generateCodeForModule: Unknown module.");
00188 
00189   // Re-compilation is not supported
00190   if (OwnedModules.hasModuleBeenLoaded(M))
00191     return;
00192 
00193   std::unique_ptr<MemoryBuffer> ObjectToLoad;
00194   // Try to load the pre-compiled object from cache if possible
00195   if (ObjCache)
00196     ObjectToLoad = ObjCache->getObject(M);
00197 
00198   // If the cache did not contain a suitable object, compile the object
00199   if (!ObjectToLoad) {
00200     ObjectToLoad = emitObject(M);
00201     assert(ObjectToLoad && "Compilation did not produce an object.");
00202   }
00203 
00204   // Load the object into the dynamic linker.
00205   // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
00206   ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =
00207     object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
00208   std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
00209     Dyld.loadObject(*LoadedObject.get());
00210 
00211   if (Dyld.hasError())
00212     report_fatal_error(Dyld.getErrorString());
00213 
00214   NotifyObjectEmitted(*LoadedObject.get(), *L);
00215 
00216   Buffers.push_back(std::move(ObjectToLoad));
00217   LoadedObjects.push_back(std::move(*LoadedObject));
00218 
00219   OwnedModules.markModuleAsLoaded(M);
00220 }
00221 
00222 void MCJIT::finalizeLoadedModules() {
00223   MutexGuard locked(lock);
00224 
00225   // Resolve any outstanding relocations.
00226   Dyld.resolveRelocations();
00227 
00228   OwnedModules.markAllLoadedModulesAsFinalized();
00229 
00230   // Register EH frame data for any module we own which has been loaded
00231   Dyld.registerEHFrames();
00232 
00233   // Set page permissions.
00234   MemMgr->finalizeMemory();
00235 }
00236 
00237 // FIXME: Rename this.
00238 void MCJIT::finalizeObject() {
00239   MutexGuard locked(lock);
00240 
00241   // Generate code for module is going to move objects out of the 'added' list,
00242   // so we need to copy that out before using it:
00243   SmallVector<Module*, 16> ModsToAdd;
00244   for (auto M : OwnedModules.added())
00245     ModsToAdd.push_back(M);
00246 
00247   for (auto M : ModsToAdd)
00248     generateCodeForModule(M);
00249 
00250   finalizeLoadedModules();
00251 }
00252 
00253 void MCJIT::finalizeModule(Module *M) {
00254   MutexGuard locked(lock);
00255 
00256   // This must be a module which has already been added to this MCJIT instance.
00257   assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
00258 
00259   // If the module hasn't been compiled, just do that.
00260   if (!OwnedModules.hasModuleBeenLoaded(M))
00261     generateCodeForModule(M);
00262 
00263   finalizeLoadedModules();
00264 }
00265 
00266 RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
00267   Mangler Mang(TM->getDataLayout());
00268   SmallString<128> FullName;
00269   Mang.getNameWithPrefix(FullName, Name);
00270   return Dyld.getSymbol(FullName);
00271 }
00272 
00273 Module *MCJIT::findModuleForSymbol(const std::string &Name,
00274                                    bool CheckFunctionsOnly) {
00275   MutexGuard locked(lock);
00276 
00277   // If it hasn't already been generated, see if it's in one of our modules.
00278   for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
00279                               E = OwnedModules.end_added();
00280        I != E; ++I) {
00281     Module *M = *I;
00282     Function *F = M->getFunction(Name);
00283     if (F && !F->isDeclaration())
00284       return M;
00285     if (!CheckFunctionsOnly) {
00286       GlobalVariable *G = M->getGlobalVariable(Name);
00287       if (G && !G->isDeclaration())
00288         return M;
00289       // FIXME: Do we need to worry about global aliases?
00290     }
00291   }
00292   // We didn't find the symbol in any of our modules.
00293   return nullptr;
00294 }
00295 
00296 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
00297                                  bool CheckFunctionsOnly) {
00298   return findSymbol(Name, CheckFunctionsOnly).getAddress();
00299 }
00300 
00301 RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name,
00302                                           bool CheckFunctionsOnly) {
00303   MutexGuard locked(lock);
00304 
00305   // First, check to see if we already have this symbol.
00306   if (auto Sym = findExistingSymbol(Name))
00307     return Sym;
00308 
00309   for (object::OwningBinary<object::Archive> &OB : Archives) {
00310     object::Archive *A = OB.getBinary();
00311     // Look for our symbols in each Archive
00312     object::Archive::child_iterator ChildIt = A->findSym(Name);
00313     if (ChildIt != A->child_end()) {
00314       // FIXME: Support nested archives?
00315       ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
00316           ChildIt->getAsBinary();
00317       if (ChildBinOrErr.getError())
00318         continue;
00319       std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
00320       if (ChildBin->isObject()) {
00321         std::unique_ptr<object::ObjectFile> OF(
00322             static_cast<object::ObjectFile *>(ChildBin.release()));
00323         // This causes the object file to be loaded.
00324         addObjectFile(std::move(OF));
00325         // The address should be here now.
00326         if (auto Sym = findExistingSymbol(Name))
00327           return Sym;
00328       }
00329     }
00330   }
00331 
00332   // If it hasn't already been generated, see if it's in one of our modules.
00333   Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
00334   if (M) {
00335     generateCodeForModule(M);
00336 
00337     // Check the RuntimeDyld table again, it should be there now.
00338     return findExistingSymbol(Name);
00339   }
00340 
00341   // If a LazyFunctionCreator is installed, use it to get/create the function.
00342   // FIXME: Should we instead have a LazySymbolCreator callback?
00343   if (LazyFunctionCreator) {
00344     auto Addr = static_cast<uint64_t>(
00345                   reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
00346     return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
00347   }
00348 
00349   return nullptr;
00350 }
00351 
00352 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
00353   MutexGuard locked(lock);
00354   uint64_t Result = getSymbolAddress(Name, false);
00355   if (Result != 0)
00356     finalizeLoadedModules();
00357   return Result;
00358 }
00359 
00360 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
00361   MutexGuard locked(lock);
00362   uint64_t Result = getSymbolAddress(Name, true);
00363   if (Result != 0)
00364     finalizeLoadedModules();
00365   return Result;
00366 }
00367 
00368 // Deprecated.  Use getFunctionAddress instead.
00369 void *MCJIT::getPointerToFunction(Function *F) {
00370   MutexGuard locked(lock);
00371 
00372   Mangler Mang(TM->getDataLayout());
00373   SmallString<128> Name;
00374   TM->getNameWithPrefix(Name, F, Mang);
00375 
00376   if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
00377     bool AbortOnFailure = !F->hasExternalWeakLinkage();
00378     void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
00379     updateGlobalMapping(F, Addr);
00380     return Addr;
00381   }
00382 
00383   Module *M = F->getParent();
00384   bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
00385 
00386   // Make sure the relevant module has been compiled and loaded.
00387   if (HasBeenAddedButNotLoaded)
00388     generateCodeForModule(M);
00389   else if (!OwnedModules.hasModuleBeenLoaded(M)) {
00390     // If this function doesn't belong to one of our modules, we're done.
00391     // FIXME: Asking for the pointer to a function that hasn't been registered,
00392     //        and isn't a declaration (which is handled above) should probably
00393     //        be an assertion.
00394     return nullptr;
00395   }
00396 
00397   // FIXME: Should the Dyld be retaining module information? Probably not.
00398   //
00399   // This is the accessor for the target address, so make sure to check the
00400   // load address of the symbol, not the local address.
00401   return (void*)Dyld.getSymbol(Name).getAddress();
00402 }
00403 
00404 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
00405     bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
00406   for (; I != E; ++I) {
00407     ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
00408   }
00409 }
00410 
00411 void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
00412   // Execute global ctors/dtors for each module in the program.
00413   runStaticConstructorsDestructorsInModulePtrSet(
00414       isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
00415   runStaticConstructorsDestructorsInModulePtrSet(
00416       isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
00417   runStaticConstructorsDestructorsInModulePtrSet(
00418       isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
00419 }
00420 
00421 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
00422                                                  ModulePtrSet::iterator I,
00423                                                  ModulePtrSet::iterator E) {
00424   for (; I != E; ++I) {
00425     Function *F = (*I)->getFunction(FnName);
00426     if (F && !F->isDeclaration())
00427       return F;
00428   }
00429   return nullptr;
00430 }
00431 
00432 Function *MCJIT::FindFunctionNamed(const char *FnName) {
00433   Function *F = FindFunctionNamedInModulePtrSet(
00434       FnName, OwnedModules.begin_added(), OwnedModules.end_added());
00435   if (!F)
00436     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
00437                                         OwnedModules.end_loaded());
00438   if (!F)
00439     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
00440                                         OwnedModules.end_finalized());
00441   return F;
00442 }
00443 
00444 GenericValue MCJIT::runFunction(Function *F,
00445                                 const std::vector<GenericValue> &ArgValues) {
00446   assert(F && "Function *F was null at entry to run()");
00447 
00448   void *FPtr = getPointerToFunction(F);
00449   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
00450   FunctionType *FTy = F->getFunctionType();
00451   Type *RetTy = FTy->getReturnType();
00452 
00453   assert((FTy->getNumParams() == ArgValues.size() ||
00454           (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
00455          "Wrong number of arguments passed into function!");
00456   assert(FTy->getNumParams() == ArgValues.size() &&
00457          "This doesn't support passing arguments through varargs (yet)!");
00458 
00459   // Handle some common cases first.  These cases correspond to common `main'
00460   // prototypes.
00461   if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
00462     switch (ArgValues.size()) {
00463     case 3:
00464       if (FTy->getParamType(0)->isIntegerTy(32) &&
00465           FTy->getParamType(1)->isPointerTy() &&
00466           FTy->getParamType(2)->isPointerTy()) {
00467         int (*PF)(int, char **, const char **) =
00468           (int(*)(int, char **, const char **))(intptr_t)FPtr;
00469 
00470         // Call the function.
00471         GenericValue rv;
00472         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
00473                                  (char **)GVTOP(ArgValues[1]),
00474                                  (const char **)GVTOP(ArgValues[2])));
00475         return rv;
00476       }
00477       break;
00478     case 2:
00479       if (FTy->getParamType(0)->isIntegerTy(32) &&
00480           FTy->getParamType(1)->isPointerTy()) {
00481         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
00482 
00483         // Call the function.
00484         GenericValue rv;
00485         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
00486                                  (char **)GVTOP(ArgValues[1])));
00487         return rv;
00488       }
00489       break;
00490     case 1:
00491       if (FTy->getNumParams() == 1 &&
00492           FTy->getParamType(0)->isIntegerTy(32)) {
00493         GenericValue rv;
00494         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
00495         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
00496         return rv;
00497       }
00498       break;
00499     }
00500   }
00501 
00502   // Handle cases where no arguments are passed first.
00503   if (ArgValues.empty()) {
00504     GenericValue rv;
00505     switch (RetTy->getTypeID()) {
00506     default: llvm_unreachable("Unknown return type for function call!");
00507     case Type::IntegerTyID: {
00508       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
00509       if (BitWidth == 1)
00510         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
00511       else if (BitWidth <= 8)
00512         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
00513       else if (BitWidth <= 16)
00514         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
00515       else if (BitWidth <= 32)
00516         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
00517       else if (BitWidth <= 64)
00518         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
00519       else
00520         llvm_unreachable("Integer types > 64 bits not supported");
00521       return rv;
00522     }
00523     case Type::VoidTyID:
00524       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
00525       return rv;
00526     case Type::FloatTyID:
00527       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
00528       return rv;
00529     case Type::DoubleTyID:
00530       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
00531       return rv;
00532     case Type::X86_FP80TyID:
00533     case Type::FP128TyID:
00534     case Type::PPC_FP128TyID:
00535       llvm_unreachable("long double not supported yet");
00536     case Type::PointerTyID:
00537       return PTOGV(((void*(*)())(intptr_t)FPtr)());
00538     }
00539   }
00540 
00541   llvm_unreachable("Full-featured argument passing not supported yet!");
00542 }
00543 
00544 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
00545   if (!isSymbolSearchingDisabled()) {
00546     void *ptr =
00547       reinterpret_cast<void*>(
00548         static_cast<uintptr_t>(Resolver.findSymbol(Name).getAddress()));
00549     if (ptr)
00550       return ptr;
00551   }
00552 
00553   /// If a LazyFunctionCreator is installed, use it to get/create the function.
00554   if (LazyFunctionCreator)
00555     if (void *RP = LazyFunctionCreator(Name))
00556       return RP;
00557 
00558   if (AbortOnFailure) {
00559     report_fatal_error("Program used external function '"+Name+
00560                        "' which could not be resolved!");
00561   }
00562   return nullptr;
00563 }
00564 
00565 void MCJIT::RegisterJITEventListener(JITEventListener *L) {
00566   if (!L)
00567     return;
00568   MutexGuard locked(lock);
00569   EventListeners.push_back(L);
00570 }
00571 
00572 void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
00573   if (!L)
00574     return;
00575   MutexGuard locked(lock);
00576   auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
00577   if (I != EventListeners.rend()) {
00578     std::swap(*I, EventListeners.back());
00579     EventListeners.pop_back();
00580   }
00581 }
00582 
00583 void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj,
00584                                 const RuntimeDyld::LoadedObjectInfo &L) {
00585   MutexGuard locked(lock);
00586   MemMgr->notifyObjectLoaded(this, Obj);
00587   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
00588     EventListeners[I]->NotifyObjectEmitted(Obj, L);
00589   }
00590 }
00591 
00592 void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) {
00593   MutexGuard locked(lock);
00594   for (JITEventListener *L : EventListeners)
00595     L->NotifyFreeingObject(Obj);
00596 }
00597 
00598 RuntimeDyld::SymbolInfo
00599 LinkingSymbolResolver::findSymbol(const std::string &Name) {
00600   auto Result = ParentEngine.findSymbol(Name, false);
00601   // If the symbols wasn't found and it begins with an underscore, try again
00602   // without the underscore.
00603   if (!Result && Name[0] == '_')
00604     Result = ParentEngine.findSymbol(Name.substr(1), false);
00605   if (Result)
00606     return Result;
00607   if (ParentEngine.isSymbolSearchingDisabled())
00608     return nullptr;
00609   return ClientResolver->findSymbol(Name);
00610 }