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