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(TM->createDataLayout(), std::move(M)), TM(std::move(TM)), 72 Ctx(nullptr), MemMgr(std::move(MemMgr)), 73 Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver), 74 ObjCache(nullptr) { 75 // FIXME: We are managing our modules, so we do not want the base class 76 // ExecutionEngine to manage them as well. To avoid double destruction 77 // of the first (and only) module added in ExecutionEngine constructor 78 // we remove it from EE and will destruct it ourselves. 79 // 80 // It may make sense to move our module manager (based on SmallStPtr) back 81 // into EE if the JIT and Interpreter can live with it. 82 // If so, additional functions: addModule, removeModule, FindFunctionNamed, 83 // runStaticConstructorsDestructors could be moved back to EE as well. 84 // 85 std::unique_ptr<Module> First = std::move(Modules[0]); 86 Modules.clear(); 87 88 OwnedModules.addModule(std::move(First)); 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 163 std::unique_ptr<MemoryBuffer> CompiledObjBuffer( 164 new ObjectMemoryBuffer(std::move(ObjBufferSV))); 165 166 // If we have an object cache, tell it about the new object. 167 // Note that we're using the compiled image, not the loaded image (as below). 168 if (ObjCache) { 169 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK 170 // to create a temporary object here and delete it after the call. 171 MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef(); 172 ObjCache->notifyObjectCompiled(M, MB); 173 } 174 175 return CompiledObjBuffer; 176 } 177 178 void MCJIT::generateCodeForModule(Module *M) { 179 // Get a thread lock to make sure we aren't trying to load multiple times 180 MutexGuard locked(lock); 181 182 // This must be a module which has already been added to this MCJIT instance. 183 assert(OwnedModules.ownsModule(M) && 184 "MCJIT::generateCodeForModule: Unknown module."); 185 186 // Re-compilation is not supported 187 if (OwnedModules.hasModuleBeenLoaded(M)) 188 return; 189 190 std::unique_ptr<MemoryBuffer> ObjectToLoad; 191 // Try to load the pre-compiled object from cache if possible 192 if (ObjCache) 193 ObjectToLoad = ObjCache->getObject(M); 194 195 if (M->getDataLayout().isDefault()) { 196 M->setDataLayout(getDataLayout()); 197 } else { 198 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch"); 199 } 200 201 // If the cache did not contain a suitable object, compile the object 202 if (!ObjectToLoad) { 203 ObjectToLoad = emitObject(M); 204 assert(ObjectToLoad && "Compilation did not produce an object."); 205 } 206 207 // Load the object into the dynamic linker. 208 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). 209 Expected<std::unique_ptr<object::ObjectFile>> LoadedObject = 210 object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef()); 211 if (!LoadedObject) { 212 std::string Buf; 213 raw_string_ostream OS(Buf); 214 logAllUnhandledErrors(LoadedObject.takeError(), OS, ""); 215 OS.flush(); 216 report_fatal_error(Buf); 217 } 218 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = 219 Dyld.loadObject(*LoadedObject.get()); 220 221 if (Dyld.hasError()) 222 report_fatal_error(Dyld.getErrorString()); 223 224 NotifyObjectEmitted(*LoadedObject.get(), *L); 225 226 Buffers.push_back(std::move(ObjectToLoad)); 227 LoadedObjects.push_back(std::move(*LoadedObject)); 228 229 OwnedModules.markModuleAsLoaded(M); 230 } 231 232 void MCJIT::finalizeLoadedModules() { 233 MutexGuard locked(lock); 234 235 // Resolve any outstanding relocations. 236 Dyld.resolveRelocations(); 237 238 OwnedModules.markAllLoadedModulesAsFinalized(); 239 240 // Register EH frame data for any module we own which has been loaded 241 Dyld.registerEHFrames(); 242 243 // Set page permissions. 244 MemMgr->finalizeMemory(); 245 } 246 247 // FIXME: Rename this. 248 void MCJIT::finalizeObject() { 249 MutexGuard locked(lock); 250 251 // Generate code for module is going to move objects out of the 'added' list, 252 // so we need to copy that out before using it: 253 SmallVector<Module*, 16> ModsToAdd; 254 for (auto M : OwnedModules.added()) 255 ModsToAdd.push_back(M); 256 257 for (auto M : ModsToAdd) 258 generateCodeForModule(M); 259 260 finalizeLoadedModules(); 261 } 262 263 void MCJIT::finalizeModule(Module *M) { 264 MutexGuard locked(lock); 265 266 // This must be a module which has already been added to this MCJIT instance. 267 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); 268 269 // If the module hasn't been compiled, just do that. 270 if (!OwnedModules.hasModuleBeenLoaded(M)) 271 generateCodeForModule(M); 272 273 finalizeLoadedModules(); 274 } 275 276 RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) { 277 SmallString<128> FullName; 278 Mangler::getNameWithPrefix(FullName, Name, getDataLayout()); 279 280 if (void *Addr = getPointerToGlobalIfAvailable(FullName)) 281 return RuntimeDyld::SymbolInfo(static_cast<uint64_t>( 282 reinterpret_cast<uintptr_t>(Addr)), 283 JITSymbolFlags::Exported); 284 285 return Dyld.getSymbol(FullName); 286 } 287 288 Module *MCJIT::findModuleForSymbol(const std::string &Name, 289 bool CheckFunctionsOnly) { 290 MutexGuard locked(lock); 291 292 // If it hasn't already been generated, see if it's in one of our modules. 293 for (ModulePtrSet::iterator I = OwnedModules.begin_added(), 294 E = OwnedModules.end_added(); 295 I != E; ++I) { 296 Module *M = *I; 297 Function *F = M->getFunction(Name); 298 if (F && !F->isDeclaration()) 299 return M; 300 if (!CheckFunctionsOnly) { 301 GlobalVariable *G = M->getGlobalVariable(Name); 302 if (G && !G->isDeclaration()) 303 return M; 304 // FIXME: Do we need to worry about global aliases? 305 } 306 } 307 // We didn't find the symbol in any of our modules. 308 return nullptr; 309 } 310 311 uint64_t MCJIT::getSymbolAddress(const std::string &Name, 312 bool CheckFunctionsOnly) { 313 return findSymbol(Name, CheckFunctionsOnly).getAddress(); 314 } 315 316 RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name, 317 bool CheckFunctionsOnly) { 318 MutexGuard locked(lock); 319 320 // First, check to see if we already have this symbol. 321 if (auto Sym = findExistingSymbol(Name)) 322 return Sym; 323 324 for (object::OwningBinary<object::Archive> &OB : Archives) { 325 object::Archive *A = OB.getBinary(); 326 // Look for our symbols in each Archive 327 object::Archive::child_iterator ChildIt = A->findSym(Name); 328 if (std::error_code EC = ChildIt->getError()) 329 report_fatal_error(EC.message()); 330 if (ChildIt != A->child_end()) { 331 // FIXME: Support nested archives? 332 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr = 333 (*ChildIt)->getAsBinary(); 334 if (ChildBinOrErr.getError()) 335 continue; 336 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); 337 if (ChildBin->isObject()) { 338 std::unique_ptr<object::ObjectFile> OF( 339 static_cast<object::ObjectFile *>(ChildBin.release())); 340 // This causes the object file to be loaded. 341 addObjectFile(std::move(OF)); 342 // The address should be here now. 343 if (auto Sym = findExistingSymbol(Name)) 344 return Sym; 345 } 346 } 347 } 348 349 // If it hasn't already been generated, see if it's in one of our modules. 350 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); 351 if (M) { 352 generateCodeForModule(M); 353 354 // Check the RuntimeDyld table again, it should be there now. 355 return findExistingSymbol(Name); 356 } 357 358 // If a LazyFunctionCreator is installed, use it to get/create the function. 359 // FIXME: Should we instead have a LazySymbolCreator callback? 360 if (LazyFunctionCreator) { 361 auto Addr = static_cast<uint64_t>( 362 reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name))); 363 return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported); 364 } 365 366 return nullptr; 367 } 368 369 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { 370 MutexGuard locked(lock); 371 uint64_t Result = getSymbolAddress(Name, false); 372 if (Result != 0) 373 finalizeLoadedModules(); 374 return Result; 375 } 376 377 uint64_t MCJIT::getFunctionAddress(const std::string &Name) { 378 MutexGuard locked(lock); 379 uint64_t Result = getSymbolAddress(Name, true); 380 if (Result != 0) 381 finalizeLoadedModules(); 382 return Result; 383 } 384 385 // Deprecated. Use getFunctionAddress instead. 386 void *MCJIT::getPointerToFunction(Function *F) { 387 MutexGuard locked(lock); 388 389 Mangler Mang; 390 SmallString<128> Name; 391 TM->getNameWithPrefix(Name, F, Mang); 392 393 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { 394 bool AbortOnFailure = !F->hasExternalWeakLinkage(); 395 void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); 396 updateGlobalMapping(F, Addr); 397 return Addr; 398 } 399 400 Module *M = F->getParent(); 401 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); 402 403 // Make sure the relevant module has been compiled and loaded. 404 if (HasBeenAddedButNotLoaded) 405 generateCodeForModule(M); 406 else if (!OwnedModules.hasModuleBeenLoaded(M)) { 407 // If this function doesn't belong to one of our modules, we're done. 408 // FIXME: Asking for the pointer to a function that hasn't been registered, 409 // and isn't a declaration (which is handled above) should probably 410 // be an assertion. 411 return nullptr; 412 } 413 414 // FIXME: Should the Dyld be retaining module information? Probably not. 415 // 416 // This is the accessor for the target address, so make sure to check the 417 // load address of the symbol, not the local address. 418 return (void*)Dyld.getSymbol(Name).getAddress(); 419 } 420 421 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( 422 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { 423 for (; I != E; ++I) { 424 ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors); 425 } 426 } 427 428 void MCJIT::runStaticConstructorsDestructors(bool isDtors) { 429 // Execute global ctors/dtors for each module in the program. 430 runStaticConstructorsDestructorsInModulePtrSet( 431 isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); 432 runStaticConstructorsDestructorsInModulePtrSet( 433 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); 434 runStaticConstructorsDestructorsInModulePtrSet( 435 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); 436 } 437 438 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, 439 ModulePtrSet::iterator I, 440 ModulePtrSet::iterator E) { 441 for (; I != E; ++I) { 442 Function *F = (*I)->getFunction(FnName); 443 if (F && !F->isDeclaration()) 444 return F; 445 } 446 return nullptr; 447 } 448 449 GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(const char *Name, 450 bool AllowInternal, 451 ModulePtrSet::iterator I, 452 ModulePtrSet::iterator E) { 453 for (; I != E; ++I) { 454 GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal); 455 if (GV && !GV->isDeclaration()) 456 return GV; 457 } 458 return nullptr; 459 } 460 461 462 Function *MCJIT::FindFunctionNamed(const char *FnName) { 463 Function *F = FindFunctionNamedInModulePtrSet( 464 FnName, OwnedModules.begin_added(), OwnedModules.end_added()); 465 if (!F) 466 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), 467 OwnedModules.end_loaded()); 468 if (!F) 469 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), 470 OwnedModules.end_finalized()); 471 return F; 472 } 473 474 GlobalVariable *MCJIT::FindGlobalVariableNamed(const char *Name, bool AllowInternal) { 475 GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet( 476 Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added()); 477 if (!GV) 478 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(), 479 OwnedModules.end_loaded()); 480 if (!GV) 481 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(), 482 OwnedModules.end_finalized()); 483 return GV; 484 } 485 486 GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) { 487 assert(F && "Function *F was null at entry to run()"); 488 489 void *FPtr = getPointerToFunction(F); 490 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); 491 FunctionType *FTy = F->getFunctionType(); 492 Type *RetTy = FTy->getReturnType(); 493 494 assert((FTy->getNumParams() == ArgValues.size() || 495 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && 496 "Wrong number of arguments passed into function!"); 497 assert(FTy->getNumParams() == ArgValues.size() && 498 "This doesn't support passing arguments through varargs (yet)!"); 499 500 // Handle some common cases first. These cases correspond to common `main' 501 // prototypes. 502 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { 503 switch (ArgValues.size()) { 504 case 3: 505 if (FTy->getParamType(0)->isIntegerTy(32) && 506 FTy->getParamType(1)->isPointerTy() && 507 FTy->getParamType(2)->isPointerTy()) { 508 int (*PF)(int, char **, const char **) = 509 (int(*)(int, char **, const char **))(intptr_t)FPtr; 510 511 // Call the function. 512 GenericValue rv; 513 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 514 (char **)GVTOP(ArgValues[1]), 515 (const char **)GVTOP(ArgValues[2]))); 516 return rv; 517 } 518 break; 519 case 2: 520 if (FTy->getParamType(0)->isIntegerTy(32) && 521 FTy->getParamType(1)->isPointerTy()) { 522 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; 523 524 // Call the function. 525 GenericValue rv; 526 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 527 (char **)GVTOP(ArgValues[1]))); 528 return rv; 529 } 530 break; 531 case 1: 532 if (FTy->getNumParams() == 1 && 533 FTy->getParamType(0)->isIntegerTy(32)) { 534 GenericValue rv; 535 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; 536 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); 537 return rv; 538 } 539 break; 540 } 541 } 542 543 // Handle cases where no arguments are passed first. 544 if (ArgValues.empty()) { 545 GenericValue rv; 546 switch (RetTy->getTypeID()) { 547 default: llvm_unreachable("Unknown return type for function call!"); 548 case Type::IntegerTyID: { 549 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); 550 if (BitWidth == 1) 551 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); 552 else if (BitWidth <= 8) 553 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); 554 else if (BitWidth <= 16) 555 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); 556 else if (BitWidth <= 32) 557 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); 558 else if (BitWidth <= 64) 559 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); 560 else 561 llvm_unreachable("Integer types > 64 bits not supported"); 562 return rv; 563 } 564 case Type::VoidTyID: 565 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); 566 return rv; 567 case Type::FloatTyID: 568 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); 569 return rv; 570 case Type::DoubleTyID: 571 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); 572 return rv; 573 case Type::X86_FP80TyID: 574 case Type::FP128TyID: 575 case Type::PPC_FP128TyID: 576 llvm_unreachable("long double not supported yet"); 577 case Type::PointerTyID: 578 return PTOGV(((void*(*)())(intptr_t)FPtr)()); 579 } 580 } 581 582 llvm_unreachable("Full-featured argument passing not supported yet!"); 583 } 584 585 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { 586 if (!isSymbolSearchingDisabled()) { 587 void *ptr = 588 reinterpret_cast<void*>( 589 static_cast<uintptr_t>(Resolver.findSymbol(Name).getAddress())); 590 if (ptr) 591 return ptr; 592 } 593 594 /// If a LazyFunctionCreator is installed, use it to get/create the function. 595 if (LazyFunctionCreator) 596 if (void *RP = LazyFunctionCreator(Name)) 597 return RP; 598 599 if (AbortOnFailure) { 600 report_fatal_error("Program used external function '"+Name+ 601 "' which could not be resolved!"); 602 } 603 return nullptr; 604 } 605 606 void MCJIT::RegisterJITEventListener(JITEventListener *L) { 607 if (!L) 608 return; 609 MutexGuard locked(lock); 610 EventListeners.push_back(L); 611 } 612 613 void MCJIT::UnregisterJITEventListener(JITEventListener *L) { 614 if (!L) 615 return; 616 MutexGuard locked(lock); 617 auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L); 618 if (I != EventListeners.rend()) { 619 std::swap(*I, EventListeners.back()); 620 EventListeners.pop_back(); 621 } 622 } 623 624 void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj, 625 const RuntimeDyld::LoadedObjectInfo &L) { 626 MutexGuard locked(lock); 627 MemMgr->notifyObjectLoaded(this, Obj); 628 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { 629 EventListeners[I]->NotifyObjectEmitted(Obj, L); 630 } 631 } 632 633 void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) { 634 MutexGuard locked(lock); 635 for (JITEventListener *L : EventListeners) 636 L->NotifyFreeingObject(Obj); 637 } 638 639 RuntimeDyld::SymbolInfo 640 LinkingSymbolResolver::findSymbol(const std::string &Name) { 641 auto Result = ParentEngine.findSymbol(Name, false); 642 // If the symbols wasn't found and it begins with an underscore, try again 643 // without the underscore. 644 if (!Result && Name[0] == '_') 645 Result = ParentEngine.findSymbol(Name.substr(1), false); 646 if (Result) 647 return Result; 648 if (ParentEngine.isSymbolSearchingDisabled()) 649 return nullptr; 650 return ClientResolver->findSymbol(Name); 651 } 652