10b57cec5SDimitry Andric //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "MCJIT.h"
100b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
110b57cec5SDimitry Andric #include "llvm/ExecutionEngine/GenericValue.h"
120b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITEventListener.h"
130b57cec5SDimitry Andric #include "llvm/ExecutionEngine/MCJIT.h"
145ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/ObjectCache.h"
150b57cec5SDimitry Andric #include "llvm/ExecutionEngine/SectionMemoryManager.h"
160b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
170b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
180b57cec5SDimitry Andric #include "llvm/IR/Function.h"
190b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h"
200b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
210b57cec5SDimitry Andric #include "llvm/IR/Module.h"
2281ad6265SDimitry Andric #include "llvm/MC/MCContext.h"
230b57cec5SDimitry Andric #include "llvm/Object/Archive.h"
240b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
250b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
260b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
270b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
285ffd83dbSDimitry Andric #include "llvm/Support/SmallVectorMemoryBuffer.h"
298bcb0991SDimitry Andric #include <mutex>
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric namespace {
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric static struct RegisterJIT {
RegisterJIT__anon8da5bdb80111::RegisterJIT360b57cec5SDimitry Andric RegisterJIT() { MCJIT::Register(); }
370b57cec5SDimitry Andric } JITRegistrator;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric
LLVMLinkInMCJIT()410b57cec5SDimitry Andric extern "C" void LLVMLinkInMCJIT() {
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric ExecutionEngine *
createJIT(std::unique_ptr<Module> M,std::string * ErrorStr,std::shared_ptr<MCJITMemoryManager> MemMgr,std::shared_ptr<LegacyJITSymbolResolver> Resolver,std::unique_ptr<TargetMachine> TM)450b57cec5SDimitry Andric MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr,
460b57cec5SDimitry Andric std::shared_ptr<MCJITMemoryManager> MemMgr,
470b57cec5SDimitry Andric std::shared_ptr<LegacyJITSymbolResolver> Resolver,
480b57cec5SDimitry Andric std::unique_ptr<TargetMachine> TM) {
490b57cec5SDimitry Andric // Try to register the program as a source of symbols to resolve against.
500b57cec5SDimitry Andric //
510b57cec5SDimitry Andric // FIXME: Don't do this here.
520b57cec5SDimitry Andric sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric if (!MemMgr || !Resolver) {
550b57cec5SDimitry Andric auto RTDyldMM = std::make_shared<SectionMemoryManager>();
560b57cec5SDimitry Andric if (!MemMgr)
570b57cec5SDimitry Andric MemMgr = RTDyldMM;
580b57cec5SDimitry Andric if (!Resolver)
590b57cec5SDimitry Andric Resolver = RTDyldMM;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
630b57cec5SDimitry Andric std::move(Resolver));
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric
MCJIT(std::unique_ptr<Module> M,std::unique_ptr<TargetMachine> TM,std::shared_ptr<MCJITMemoryManager> MemMgr,std::shared_ptr<LegacyJITSymbolResolver> Resolver)660b57cec5SDimitry Andric MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
670b57cec5SDimitry Andric std::shared_ptr<MCJITMemoryManager> MemMgr,
680b57cec5SDimitry Andric std::shared_ptr<LegacyJITSymbolResolver> Resolver)
690b57cec5SDimitry Andric : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
700b57cec5SDimitry Andric Ctx(nullptr), MemMgr(std::move(MemMgr)),
710b57cec5SDimitry Andric Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
720b57cec5SDimitry Andric ObjCache(nullptr) {
730b57cec5SDimitry Andric // FIXME: We are managing our modules, so we do not want the base class
740b57cec5SDimitry Andric // ExecutionEngine to manage them as well. To avoid double destruction
750b57cec5SDimitry Andric // of the first (and only) module added in ExecutionEngine constructor
760b57cec5SDimitry Andric // we remove it from EE and will destruct it ourselves.
770b57cec5SDimitry Andric //
780b57cec5SDimitry Andric // It may make sense to move our module manager (based on SmallStPtr) back
790b57cec5SDimitry Andric // into EE if the JIT and Interpreter can live with it.
800b57cec5SDimitry Andric // If so, additional functions: addModule, removeModule, FindFunctionNamed,
810b57cec5SDimitry Andric // runStaticConstructorsDestructors could be moved back to EE as well.
820b57cec5SDimitry Andric //
830b57cec5SDimitry Andric std::unique_ptr<Module> First = std::move(Modules[0]);
840b57cec5SDimitry Andric Modules.clear();
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric if (First->getDataLayout().isDefault())
870b57cec5SDimitry Andric First->setDataLayout(getDataLayout());
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric OwnedModules.addModule(std::move(First));
900b57cec5SDimitry Andric RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric
~MCJIT()930b57cec5SDimitry Andric MCJIT::~MCJIT() {
948bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric Dyld.deregisterEHFrames();
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric for (auto &Obj : LoadedObjects)
990b57cec5SDimitry Andric if (Obj)
1000b57cec5SDimitry Andric notifyFreeingObject(*Obj);
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric Archives.clear();
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
addModule(std::unique_ptr<Module> M)1050b57cec5SDimitry Andric void MCJIT::addModule(std::unique_ptr<Module> M) {
1068bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric if (M->getDataLayout().isDefault())
1090b57cec5SDimitry Andric M->setDataLayout(getDataLayout());
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric OwnedModules.addModule(std::move(M));
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
removeModule(Module * M)1140b57cec5SDimitry Andric bool MCJIT::removeModule(Module *M) {
1158bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
1160b57cec5SDimitry Andric return OwnedModules.removeModule(M);
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
addObjectFile(std::unique_ptr<object::ObjectFile> Obj)1190b57cec5SDimitry Andric void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
1200b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
1210b57cec5SDimitry Andric if (Dyld.hasError())
1220b57cec5SDimitry Andric report_fatal_error(Dyld.getErrorString());
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric notifyObjectLoaded(*Obj, *L);
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric LoadedObjects.push_back(std::move(Obj));
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
addObjectFile(object::OwningBinary<object::ObjectFile> Obj)1290b57cec5SDimitry Andric void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
1300b57cec5SDimitry Andric std::unique_ptr<object::ObjectFile> ObjFile;
1310b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> MemBuf;
1320b57cec5SDimitry Andric std::tie(ObjFile, MemBuf) = Obj.takeBinary();
1330b57cec5SDimitry Andric addObjectFile(std::move(ObjFile));
1340b57cec5SDimitry Andric Buffers.push_back(std::move(MemBuf));
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
addArchive(object::OwningBinary<object::Archive> A)1370b57cec5SDimitry Andric void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
1380b57cec5SDimitry Andric Archives.push_back(std::move(A));
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
setObjectCache(ObjectCache * NewCache)1410b57cec5SDimitry Andric void MCJIT::setObjectCache(ObjectCache* NewCache) {
1428bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
1430b57cec5SDimitry Andric ObjCache = NewCache;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric
emitObject(Module * M)1460b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
1470b57cec5SDimitry Andric assert(M && "Can not emit a null module");
1480b57cec5SDimitry Andric
1498bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric // Materialize all globals in the module if they have not been
1520b57cec5SDimitry Andric // materialized already.
1530b57cec5SDimitry Andric cantFail(M->materializeAll());
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric // This must be a module which has already been added but not loaded to this
1560b57cec5SDimitry Andric // MCJIT instance, since these conditions are tested by our caller,
1570b57cec5SDimitry Andric // generateCodeForModule.
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric legacy::PassManager PM;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric // The RuntimeDyld will take ownership of this shortly
1620b57cec5SDimitry Andric SmallVector<char, 4096> ObjBufferSV;
1630b57cec5SDimitry Andric raw_svector_ostream ObjStream(ObjBufferSV);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric // Turn the machine code intermediate representation into bytes in memory
1660b57cec5SDimitry Andric // that may be executed.
1670b57cec5SDimitry Andric if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
1680b57cec5SDimitry Andric report_fatal_error("Target does not support MC emission!");
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric // Initialize passes.
1710b57cec5SDimitry Andric PM.run(*M);
1720b57cec5SDimitry Andric // Flush the output buffer to get the generated code into memory
1730b57cec5SDimitry Andric
1740eae32dcSDimitry Andric auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
1750eae32dcSDimitry Andric std::move(ObjBufferSV), /*RequiresNullTerminator=*/false);
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric // If we have an object cache, tell it about the new object.
1780b57cec5SDimitry Andric // Note that we're using the compiled image, not the loaded image (as below).
1790b57cec5SDimitry Andric if (ObjCache) {
1800b57cec5SDimitry Andric // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
1810b57cec5SDimitry Andric // to create a temporary object here and delete it after the call.
1820b57cec5SDimitry Andric MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
1830b57cec5SDimitry Andric ObjCache->notifyObjectCompiled(M, MB);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric return CompiledObjBuffer;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
generateCodeForModule(Module * M)1890b57cec5SDimitry Andric void MCJIT::generateCodeForModule(Module *M) {
1900b57cec5SDimitry Andric // Get a thread lock to make sure we aren't trying to load multiple times
1918bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric // This must be a module which has already been added to this MCJIT instance.
1940b57cec5SDimitry Andric assert(OwnedModules.ownsModule(M) &&
1950b57cec5SDimitry Andric "MCJIT::generateCodeForModule: Unknown module.");
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric // Re-compilation is not supported
1980b57cec5SDimitry Andric if (OwnedModules.hasModuleBeenLoaded(M))
1990b57cec5SDimitry Andric return;
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> ObjectToLoad;
2020b57cec5SDimitry Andric // Try to load the pre-compiled object from cache if possible
2030b57cec5SDimitry Andric if (ObjCache)
2040b57cec5SDimitry Andric ObjectToLoad = ObjCache->getObject(M);
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // If the cache did not contain a suitable object, compile the object
2090b57cec5SDimitry Andric if (!ObjectToLoad) {
2100b57cec5SDimitry Andric ObjectToLoad = emitObject(M);
2110b57cec5SDimitry Andric assert(ObjectToLoad && "Compilation did not produce an object.");
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric // Load the object into the dynamic linker.
2150b57cec5SDimitry Andric // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
2160b57cec5SDimitry Andric Expected<std::unique_ptr<object::ObjectFile>> LoadedObject =
2170b57cec5SDimitry Andric object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
2180b57cec5SDimitry Andric if (!LoadedObject) {
2190b57cec5SDimitry Andric std::string Buf;
2200b57cec5SDimitry Andric raw_string_ostream OS(Buf);
2210b57cec5SDimitry Andric logAllUnhandledErrors(LoadedObject.takeError(), OS);
222349cc55cSDimitry Andric report_fatal_error(Twine(OS.str()));
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
2250b57cec5SDimitry Andric Dyld.loadObject(*LoadedObject.get());
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric if (Dyld.hasError())
2280b57cec5SDimitry Andric report_fatal_error(Dyld.getErrorString());
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric notifyObjectLoaded(*LoadedObject.get(), *L);
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric Buffers.push_back(std::move(ObjectToLoad));
2330b57cec5SDimitry Andric LoadedObjects.push_back(std::move(*LoadedObject));
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric OwnedModules.markModuleAsLoaded(M);
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric
finalizeLoadedModules()2380b57cec5SDimitry Andric void MCJIT::finalizeLoadedModules() {
2398bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric // Resolve any outstanding relocations.
2420b57cec5SDimitry Andric Dyld.resolveRelocations();
2430b57cec5SDimitry Andric
2445ffd83dbSDimitry Andric // Check for Dyld error.
2455ffd83dbSDimitry Andric if (Dyld.hasError())
2465ffd83dbSDimitry Andric ErrMsg = Dyld.getErrorString().str();
2475ffd83dbSDimitry Andric
2480b57cec5SDimitry Andric OwnedModules.markAllLoadedModulesAsFinalized();
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andric // Register EH frame data for any module we own which has been loaded
2510b57cec5SDimitry Andric Dyld.registerEHFrames();
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric // Set page permissions.
2540b57cec5SDimitry Andric MemMgr->finalizeMemory();
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // FIXME: Rename this.
finalizeObject()2580b57cec5SDimitry Andric void MCJIT::finalizeObject() {
2598bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric // Generate code for module is going to move objects out of the 'added' list,
2620b57cec5SDimitry Andric // so we need to copy that out before using it:
2630b57cec5SDimitry Andric SmallVector<Module*, 16> ModsToAdd;
264*bdd1243dSDimitry Andric for (auto *M : OwnedModules.added())
2650b57cec5SDimitry Andric ModsToAdd.push_back(M);
2660b57cec5SDimitry Andric
267*bdd1243dSDimitry Andric for (auto *M : ModsToAdd)
2680b57cec5SDimitry Andric generateCodeForModule(M);
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric finalizeLoadedModules();
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
finalizeModule(Module * M)2730b57cec5SDimitry Andric void MCJIT::finalizeModule(Module *M) {
2748bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric // This must be a module which has already been added to this MCJIT instance.
2770b57cec5SDimitry Andric assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric // If the module hasn't been compiled, just do that.
2800b57cec5SDimitry Andric if (!OwnedModules.hasModuleBeenLoaded(M))
2810b57cec5SDimitry Andric generateCodeForModule(M);
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric finalizeLoadedModules();
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric
findExistingSymbol(const std::string & Name)2860b57cec5SDimitry Andric JITSymbol MCJIT::findExistingSymbol(const std::string &Name) {
2870b57cec5SDimitry Andric if (void *Addr = getPointerToGlobalIfAvailable(Name))
2880b57cec5SDimitry Andric return JITSymbol(static_cast<uint64_t>(
2890b57cec5SDimitry Andric reinterpret_cast<uintptr_t>(Addr)),
2900b57cec5SDimitry Andric JITSymbolFlags::Exported);
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andric return Dyld.getSymbol(Name);
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric
findModuleForSymbol(const std::string & Name,bool CheckFunctionsOnly)2950b57cec5SDimitry Andric Module *MCJIT::findModuleForSymbol(const std::string &Name,
2960b57cec5SDimitry Andric bool CheckFunctionsOnly) {
2970b57cec5SDimitry Andric StringRef DemangledName = Name;
2980b57cec5SDimitry Andric if (DemangledName[0] == getDataLayout().getGlobalPrefix())
2990b57cec5SDimitry Andric DemangledName = DemangledName.substr(1);
3000b57cec5SDimitry Andric
3018bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric // If it hasn't already been generated, see if it's in one of our modules.
3040b57cec5SDimitry Andric for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
3050b57cec5SDimitry Andric E = OwnedModules.end_added();
3060b57cec5SDimitry Andric I != E; ++I) {
3070b57cec5SDimitry Andric Module *M = *I;
3080b57cec5SDimitry Andric Function *F = M->getFunction(DemangledName);
3090b57cec5SDimitry Andric if (F && !F->isDeclaration())
3100b57cec5SDimitry Andric return M;
3110b57cec5SDimitry Andric if (!CheckFunctionsOnly) {
3120b57cec5SDimitry Andric GlobalVariable *G = M->getGlobalVariable(DemangledName);
3130b57cec5SDimitry Andric if (G && !G->isDeclaration())
3140b57cec5SDimitry Andric return M;
3150b57cec5SDimitry Andric // FIXME: Do we need to worry about global aliases?
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric // We didn't find the symbol in any of our modules.
3190b57cec5SDimitry Andric return nullptr;
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric
getSymbolAddress(const std::string & Name,bool CheckFunctionsOnly)3220b57cec5SDimitry Andric uint64_t MCJIT::getSymbolAddress(const std::string &Name,
3230b57cec5SDimitry Andric bool CheckFunctionsOnly) {
3240b57cec5SDimitry Andric std::string MangledName;
3250b57cec5SDimitry Andric {
3260b57cec5SDimitry Andric raw_string_ostream MangledNameStream(MangledName);
3270b57cec5SDimitry Andric Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
3300b57cec5SDimitry Andric if (auto AddrOrErr = Sym.getAddress())
3310b57cec5SDimitry Andric return *AddrOrErr;
3320b57cec5SDimitry Andric else
3330b57cec5SDimitry Andric report_fatal_error(AddrOrErr.takeError());
3340b57cec5SDimitry Andric } else if (auto Err = Sym.takeError())
3350b57cec5SDimitry Andric report_fatal_error(Sym.takeError());
3360b57cec5SDimitry Andric return 0;
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric
findSymbol(const std::string & Name,bool CheckFunctionsOnly)3390b57cec5SDimitry Andric JITSymbol MCJIT::findSymbol(const std::string &Name,
3400b57cec5SDimitry Andric bool CheckFunctionsOnly) {
3418bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric // First, check to see if we already have this symbol.
3440b57cec5SDimitry Andric if (auto Sym = findExistingSymbol(Name))
3450b57cec5SDimitry Andric return Sym;
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric for (object::OwningBinary<object::Archive> &OB : Archives) {
3480b57cec5SDimitry Andric object::Archive *A = OB.getBinary();
3490b57cec5SDimitry Andric // Look for our symbols in each Archive
3500b57cec5SDimitry Andric auto OptionalChildOrErr = A->findSym(Name);
3510b57cec5SDimitry Andric if (!OptionalChildOrErr)
3520b57cec5SDimitry Andric report_fatal_error(OptionalChildOrErr.takeError());
3530b57cec5SDimitry Andric auto &OptionalChild = *OptionalChildOrErr;
3540b57cec5SDimitry Andric if (OptionalChild) {
3550b57cec5SDimitry Andric // FIXME: Support nested archives?
3560b57cec5SDimitry Andric Expected<std::unique_ptr<object::Binary>> ChildBinOrErr =
3570b57cec5SDimitry Andric OptionalChild->getAsBinary();
3580b57cec5SDimitry Andric if (!ChildBinOrErr) {
3590b57cec5SDimitry Andric // TODO: Actually report errors helpfully.
3600b57cec5SDimitry Andric consumeError(ChildBinOrErr.takeError());
3610b57cec5SDimitry Andric continue;
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
3640b57cec5SDimitry Andric if (ChildBin->isObject()) {
3650b57cec5SDimitry Andric std::unique_ptr<object::ObjectFile> OF(
3660b57cec5SDimitry Andric static_cast<object::ObjectFile *>(ChildBin.release()));
3670b57cec5SDimitry Andric // This causes the object file to be loaded.
3680b57cec5SDimitry Andric addObjectFile(std::move(OF));
3690b57cec5SDimitry Andric // The address should be here now.
3700b57cec5SDimitry Andric if (auto Sym = findExistingSymbol(Name))
3710b57cec5SDimitry Andric return Sym;
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric // If it hasn't already been generated, see if it's in one of our modules.
3770b57cec5SDimitry Andric Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
3780b57cec5SDimitry Andric if (M) {
3790b57cec5SDimitry Andric generateCodeForModule(M);
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andric // Check the RuntimeDyld table again, it should be there now.
3820b57cec5SDimitry Andric return findExistingSymbol(Name);
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric // If a LazyFunctionCreator is installed, use it to get/create the function.
3860b57cec5SDimitry Andric // FIXME: Should we instead have a LazySymbolCreator callback?
3870b57cec5SDimitry Andric if (LazyFunctionCreator) {
3880b57cec5SDimitry Andric auto Addr = static_cast<uint64_t>(
3890b57cec5SDimitry Andric reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
3900b57cec5SDimitry Andric return JITSymbol(Addr, JITSymbolFlags::Exported);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric return nullptr;
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric
getGlobalValueAddress(const std::string & Name)3960b57cec5SDimitry Andric uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
3978bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
3980b57cec5SDimitry Andric uint64_t Result = getSymbolAddress(Name, false);
3990b57cec5SDimitry Andric if (Result != 0)
4000b57cec5SDimitry Andric finalizeLoadedModules();
4010b57cec5SDimitry Andric return Result;
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric
getFunctionAddress(const std::string & Name)4040b57cec5SDimitry Andric uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
4058bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
4060b57cec5SDimitry Andric uint64_t Result = getSymbolAddress(Name, true);
4070b57cec5SDimitry Andric if (Result != 0)
4080b57cec5SDimitry Andric finalizeLoadedModules();
4090b57cec5SDimitry Andric return Result;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andric // Deprecated. Use getFunctionAddress instead.
getPointerToFunction(Function * F)4130b57cec5SDimitry Andric void *MCJIT::getPointerToFunction(Function *F) {
4148bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andric Mangler Mang;
4170b57cec5SDimitry Andric SmallString<128> Name;
4180b57cec5SDimitry Andric TM->getNameWithPrefix(Name, F, Mang);
4190b57cec5SDimitry Andric
4200b57cec5SDimitry Andric if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
4210b57cec5SDimitry Andric bool AbortOnFailure = !F->hasExternalWeakLinkage();
4220b57cec5SDimitry Andric void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
4230b57cec5SDimitry Andric updateGlobalMapping(F, Addr);
4240b57cec5SDimitry Andric return Addr;
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric Module *M = F->getParent();
4280b57cec5SDimitry Andric bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric // Make sure the relevant module has been compiled and loaded.
4310b57cec5SDimitry Andric if (HasBeenAddedButNotLoaded)
4320b57cec5SDimitry Andric generateCodeForModule(M);
4330b57cec5SDimitry Andric else if (!OwnedModules.hasModuleBeenLoaded(M)) {
4340b57cec5SDimitry Andric // If this function doesn't belong to one of our modules, we're done.
4350b57cec5SDimitry Andric // FIXME: Asking for the pointer to a function that hasn't been registered,
4360b57cec5SDimitry Andric // and isn't a declaration (which is handled above) should probably
4370b57cec5SDimitry Andric // be an assertion.
4380b57cec5SDimitry Andric return nullptr;
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric // FIXME: Should the Dyld be retaining module information? Probably not.
4420b57cec5SDimitry Andric //
4430b57cec5SDimitry Andric // This is the accessor for the target address, so make sure to check the
4440b57cec5SDimitry Andric // load address of the symbol, not the local address.
4450b57cec5SDimitry Andric return (void*)Dyld.getSymbol(Name).getAddress();
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric
runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,ModulePtrSet::iterator I,ModulePtrSet::iterator E)4480b57cec5SDimitry Andric void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
4490b57cec5SDimitry Andric bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
4500b57cec5SDimitry Andric for (; I != E; ++I) {
4510b57cec5SDimitry Andric ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric
runStaticConstructorsDestructors(bool isDtors)4550b57cec5SDimitry Andric void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
4560b57cec5SDimitry Andric // Execute global ctors/dtors for each module in the program.
4570b57cec5SDimitry Andric runStaticConstructorsDestructorsInModulePtrSet(
4580b57cec5SDimitry Andric isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
4590b57cec5SDimitry Andric runStaticConstructorsDestructorsInModulePtrSet(
4600b57cec5SDimitry Andric isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
4610b57cec5SDimitry Andric runStaticConstructorsDestructorsInModulePtrSet(
4620b57cec5SDimitry Andric isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric
FindFunctionNamedInModulePtrSet(StringRef FnName,ModulePtrSet::iterator I,ModulePtrSet::iterator E)4650b57cec5SDimitry Andric Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
4660b57cec5SDimitry Andric ModulePtrSet::iterator I,
4670b57cec5SDimitry Andric ModulePtrSet::iterator E) {
4680b57cec5SDimitry Andric for (; I != E; ++I) {
4690b57cec5SDimitry Andric Function *F = (*I)->getFunction(FnName);
4700b57cec5SDimitry Andric if (F && !F->isDeclaration())
4710b57cec5SDimitry Andric return F;
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric return nullptr;
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
FindGlobalVariableNamedInModulePtrSet(StringRef Name,bool AllowInternal,ModulePtrSet::iterator I,ModulePtrSet::iterator E)4760b57cec5SDimitry Andric GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
4770b57cec5SDimitry Andric bool AllowInternal,
4780b57cec5SDimitry Andric ModulePtrSet::iterator I,
4790b57cec5SDimitry Andric ModulePtrSet::iterator E) {
4800b57cec5SDimitry Andric for (; I != E; ++I) {
4810b57cec5SDimitry Andric GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
4820b57cec5SDimitry Andric if (GV && !GV->isDeclaration())
4830b57cec5SDimitry Andric return GV;
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric return nullptr;
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric
FindFunctionNamed(StringRef FnName)4890b57cec5SDimitry Andric Function *MCJIT::FindFunctionNamed(StringRef FnName) {
4900b57cec5SDimitry Andric Function *F = FindFunctionNamedInModulePtrSet(
4910b57cec5SDimitry Andric FnName, OwnedModules.begin_added(), OwnedModules.end_added());
4920b57cec5SDimitry Andric if (!F)
4930b57cec5SDimitry Andric F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
4940b57cec5SDimitry Andric OwnedModules.end_loaded());
4950b57cec5SDimitry Andric if (!F)
4960b57cec5SDimitry Andric F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
4970b57cec5SDimitry Andric OwnedModules.end_finalized());
4980b57cec5SDimitry Andric return F;
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric
FindGlobalVariableNamed(StringRef Name,bool AllowInternal)5010b57cec5SDimitry Andric GlobalVariable *MCJIT::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
5020b57cec5SDimitry Andric GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
5030b57cec5SDimitry Andric Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
5040b57cec5SDimitry Andric if (!GV)
5050b57cec5SDimitry Andric GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
5060b57cec5SDimitry Andric OwnedModules.end_loaded());
5070b57cec5SDimitry Andric if (!GV)
5080b57cec5SDimitry Andric GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
5090b57cec5SDimitry Andric OwnedModules.end_finalized());
5100b57cec5SDimitry Andric return GV;
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
runFunction(Function * F,ArrayRef<GenericValue> ArgValues)5130b57cec5SDimitry Andric GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) {
5140b57cec5SDimitry Andric assert(F && "Function *F was null at entry to run()");
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric void *FPtr = getPointerToFunction(F);
5170b57cec5SDimitry Andric finalizeModule(F->getParent());
5180b57cec5SDimitry Andric assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
5190b57cec5SDimitry Andric FunctionType *FTy = F->getFunctionType();
5200b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType();
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric assert((FTy->getNumParams() == ArgValues.size() ||
5230b57cec5SDimitry Andric (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
5240b57cec5SDimitry Andric "Wrong number of arguments passed into function!");
5250b57cec5SDimitry Andric assert(FTy->getNumParams() == ArgValues.size() &&
5260b57cec5SDimitry Andric "This doesn't support passing arguments through varargs (yet)!");
5270b57cec5SDimitry Andric
5280b57cec5SDimitry Andric // Handle some common cases first. These cases correspond to common `main'
5290b57cec5SDimitry Andric // prototypes.
5300b57cec5SDimitry Andric if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
5310b57cec5SDimitry Andric switch (ArgValues.size()) {
5320b57cec5SDimitry Andric case 3:
5330b57cec5SDimitry Andric if (FTy->getParamType(0)->isIntegerTy(32) &&
5340b57cec5SDimitry Andric FTy->getParamType(1)->isPointerTy() &&
5350b57cec5SDimitry Andric FTy->getParamType(2)->isPointerTy()) {
5360b57cec5SDimitry Andric int (*PF)(int, char **, const char **) =
5370b57cec5SDimitry Andric (int(*)(int, char **, const char **))(intptr_t)FPtr;
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // Call the function.
5400b57cec5SDimitry Andric GenericValue rv;
5410b57cec5SDimitry Andric rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
5420b57cec5SDimitry Andric (char **)GVTOP(ArgValues[1]),
5430b57cec5SDimitry Andric (const char **)GVTOP(ArgValues[2])));
5440b57cec5SDimitry Andric return rv;
5450b57cec5SDimitry Andric }
5460b57cec5SDimitry Andric break;
5470b57cec5SDimitry Andric case 2:
5480b57cec5SDimitry Andric if (FTy->getParamType(0)->isIntegerTy(32) &&
5490b57cec5SDimitry Andric FTy->getParamType(1)->isPointerTy()) {
5500b57cec5SDimitry Andric int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric // Call the function.
5530b57cec5SDimitry Andric GenericValue rv;
5540b57cec5SDimitry Andric rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
5550b57cec5SDimitry Andric (char **)GVTOP(ArgValues[1])));
5560b57cec5SDimitry Andric return rv;
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric break;
5590b57cec5SDimitry Andric case 1:
5600b57cec5SDimitry Andric if (FTy->getNumParams() == 1 &&
5610b57cec5SDimitry Andric FTy->getParamType(0)->isIntegerTy(32)) {
5620b57cec5SDimitry Andric GenericValue rv;
5630b57cec5SDimitry Andric int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
5640b57cec5SDimitry Andric rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
5650b57cec5SDimitry Andric return rv;
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric break;
5680b57cec5SDimitry Andric }
5690b57cec5SDimitry Andric }
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric // Handle cases where no arguments are passed first.
5720b57cec5SDimitry Andric if (ArgValues.empty()) {
5730b57cec5SDimitry Andric GenericValue rv;
5740b57cec5SDimitry Andric switch (RetTy->getTypeID()) {
5750b57cec5SDimitry Andric default: llvm_unreachable("Unknown return type for function call!");
5760b57cec5SDimitry Andric case Type::IntegerTyID: {
5770b57cec5SDimitry Andric unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
5780b57cec5SDimitry Andric if (BitWidth == 1)
5790b57cec5SDimitry Andric rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
5800b57cec5SDimitry Andric else if (BitWidth <= 8)
5810b57cec5SDimitry Andric rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
5820b57cec5SDimitry Andric else if (BitWidth <= 16)
5830b57cec5SDimitry Andric rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
5840b57cec5SDimitry Andric else if (BitWidth <= 32)
5850b57cec5SDimitry Andric rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
5860b57cec5SDimitry Andric else if (BitWidth <= 64)
5870b57cec5SDimitry Andric rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
5880b57cec5SDimitry Andric else
5890b57cec5SDimitry Andric llvm_unreachable("Integer types > 64 bits not supported");
5900b57cec5SDimitry Andric return rv;
5910b57cec5SDimitry Andric }
5920b57cec5SDimitry Andric case Type::VoidTyID:
5930b57cec5SDimitry Andric rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
5940b57cec5SDimitry Andric return rv;
5950b57cec5SDimitry Andric case Type::FloatTyID:
5960b57cec5SDimitry Andric rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
5970b57cec5SDimitry Andric return rv;
5980b57cec5SDimitry Andric case Type::DoubleTyID:
5990b57cec5SDimitry Andric rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
6000b57cec5SDimitry Andric return rv;
6010b57cec5SDimitry Andric case Type::X86_FP80TyID:
6020b57cec5SDimitry Andric case Type::FP128TyID:
6030b57cec5SDimitry Andric case Type::PPC_FP128TyID:
6040b57cec5SDimitry Andric llvm_unreachable("long double not supported yet");
6050b57cec5SDimitry Andric case Type::PointerTyID:
6060b57cec5SDimitry Andric return PTOGV(((void*(*)())(intptr_t)FPtr)());
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric
6100b57cec5SDimitry Andric report_fatal_error("MCJIT::runFunction does not support full-featured "
6110b57cec5SDimitry Andric "argument passing. Please use "
6120b57cec5SDimitry Andric "ExecutionEngine::getFunctionAddress and cast the result "
6130b57cec5SDimitry Andric "to the desired function pointer type.");
6140b57cec5SDimitry Andric }
6150b57cec5SDimitry Andric
getPointerToNamedFunction(StringRef Name,bool AbortOnFailure)6160b57cec5SDimitry Andric void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
6170b57cec5SDimitry Andric if (!isSymbolSearchingDisabled()) {
6185ffd83dbSDimitry Andric if (auto Sym = Resolver.findSymbol(std::string(Name))) {
6190b57cec5SDimitry Andric if (auto AddrOrErr = Sym.getAddress())
6200b57cec5SDimitry Andric return reinterpret_cast<void*>(
6210b57cec5SDimitry Andric static_cast<uintptr_t>(*AddrOrErr));
6220b57cec5SDimitry Andric } else if (auto Err = Sym.takeError())
6230b57cec5SDimitry Andric report_fatal_error(std::move(Err));
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric
6260b57cec5SDimitry Andric /// If a LazyFunctionCreator is installed, use it to get/create the function.
6270b57cec5SDimitry Andric if (LazyFunctionCreator)
6285ffd83dbSDimitry Andric if (void *RP = LazyFunctionCreator(std::string(Name)))
6290b57cec5SDimitry Andric return RP;
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andric if (AbortOnFailure) {
6320b57cec5SDimitry Andric report_fatal_error("Program used external function '"+Name+
6330b57cec5SDimitry Andric "' which could not be resolved!");
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric return nullptr;
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric
RegisterJITEventListener(JITEventListener * L)6380b57cec5SDimitry Andric void MCJIT::RegisterJITEventListener(JITEventListener *L) {
6390b57cec5SDimitry Andric if (!L)
6400b57cec5SDimitry Andric return;
6418bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
6420b57cec5SDimitry Andric EventListeners.push_back(L);
6430b57cec5SDimitry Andric }
6440b57cec5SDimitry Andric
UnregisterJITEventListener(JITEventListener * L)6450b57cec5SDimitry Andric void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
6460b57cec5SDimitry Andric if (!L)
6470b57cec5SDimitry Andric return;
6488bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
6490b57cec5SDimitry Andric auto I = find(reverse(EventListeners), L);
6500b57cec5SDimitry Andric if (I != EventListeners.rend()) {
6510b57cec5SDimitry Andric std::swap(*I, EventListeners.back());
6520b57cec5SDimitry Andric EventListeners.pop_back();
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric
notifyObjectLoaded(const object::ObjectFile & Obj,const RuntimeDyld::LoadedObjectInfo & L)6560b57cec5SDimitry Andric void MCJIT::notifyObjectLoaded(const object::ObjectFile &Obj,
6570b57cec5SDimitry Andric const RuntimeDyld::LoadedObjectInfo &L) {
6580b57cec5SDimitry Andric uint64_t Key =
6590b57cec5SDimitry Andric static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
6608bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
6610b57cec5SDimitry Andric MemMgr->notifyObjectLoaded(this, Obj);
662*bdd1243dSDimitry Andric for (JITEventListener *EL : EventListeners)
663*bdd1243dSDimitry Andric EL->notifyObjectLoaded(Key, Obj, L);
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric
notifyFreeingObject(const object::ObjectFile & Obj)6660b57cec5SDimitry Andric void MCJIT::notifyFreeingObject(const object::ObjectFile &Obj) {
6670b57cec5SDimitry Andric uint64_t Key =
6680b57cec5SDimitry Andric static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
6698bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock);
6700b57cec5SDimitry Andric for (JITEventListener *L : EventListeners)
6710b57cec5SDimitry Andric L->notifyFreeingObject(Key);
6720b57cec5SDimitry Andric }
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric JITSymbol
findSymbol(const std::string & Name)6750b57cec5SDimitry Andric LinkingSymbolResolver::findSymbol(const std::string &Name) {
6760b57cec5SDimitry Andric auto Result = ParentEngine.findSymbol(Name, false);
6770b57cec5SDimitry Andric if (Result)
6780b57cec5SDimitry Andric return Result;
6790b57cec5SDimitry Andric if (ParentEngine.isSymbolSearchingDisabled())
6800b57cec5SDimitry Andric return nullptr;
6810b57cec5SDimitry Andric return ClientResolver->findSymbol(Name);
6820b57cec5SDimitry Andric }
6830b57cec5SDimitry Andric
anchor()6840b57cec5SDimitry Andric void LinkingSymbolResolver::anchor() {}
685