diff --git a/llvm/tools/lli/OrcLazyJIT.cpp b/llvm/tools/lli/OrcLazyJIT.cpp index 0e4e2a579b68..d08008da6e05 100644 --- a/llvm/tools/lli/OrcLazyJIT.cpp +++ b/llvm/tools/lli/OrcLazyJIT.cpp @@ -9,6 +9,7 @@ #include "OrcLazyJIT.h" #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h" +#include "llvm/Support/DynamicLibrary.h" using namespace llvm; @@ -29,19 +30,31 @@ OrcLazyJIT::createCallbackManagerBuilder(Triple T) { } int llvm::runOrcLazyJIT(std::unique_ptr M, int ArgC, char* ArgV[]) { + // Add the program's symbols into the JIT's search space. + if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) { + errs() << "Error loading program symbols.\n"; + return 1; + } + + // Grab a target machine and try to build a factory function for the + // target-specific Orc callback manager. auto TM = std::unique_ptr(EngineBuilder().selectTarget()); auto &Context = getGlobalContext(); auto CallbackMgrBuilder = OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple())); + // If we couldn't build the factory function then there must not be a callback + // manager for this target. Bail out. if (!CallbackMgrBuilder) { errs() << "No callback manager available for target '" << TM->getTargetTriple() << "'.\n"; return 1; } + // Everything looks good. Build the JIT. OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder); + // Add the module, look up main and run it. auto MainHandle = J.addModule(std::move(M)); auto MainSym = J.findSymbolIn(MainHandle, "main"); diff --git a/llvm/tools/lli/OrcLazyJIT.h b/llvm/tools/lli/OrcLazyJIT.h index 89dfa6ba9279..e323714646b7 100644 --- a/llvm/tools/lli/OrcLazyJIT.h +++ b/llvm/tools/lli/OrcLazyJIT.h @@ -21,6 +21,7 @@ #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h" #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/RTDyldMemoryManager.h" #include "llvm/IR/LLVMContext.h" namespace llvm { @@ -61,7 +62,13 @@ public: std::vector> S; S.push_back(std::move(M)); - return CODLayer.addModuleSet(std::move(S)); + auto FallbackLookup = + [](const std::string &Name) { + if (auto Addr = RTDyldMemoryManager::getSymbolAddressInProcess(Name)) + return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported); + return RuntimeDyld::SymbolInfo(nullptr); + }; + return CODLayer.addModuleSet(std::move(S), std::move(FallbackLookup)); } orc::JITSymbol findSymbol(const std::string &Name) {