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