From: Lang Hames Date: Wed, 29 Jul 2015 23:12:33 +0000 (+0000) Subject: [MCJIT] Fix PR20656 by teaching MCJIT to honor ExecutionEngine's global mapping. X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=7fe19725001547077aa737d1018a90207432acce [MCJIT] Fix PR20656 by teaching MCJIT to honor ExecutionEngine's global mapping. This is important for users of the C API who can't supply custom symbol resolvers yet. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243589 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 67b843737cd..1fc6bb5fe16 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -189,10 +189,17 @@ uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) { } std::string ExecutionEngine::getMangledName(const GlobalValue *GV) { + assert(GV->hasName() && "Global must have name."); + MutexGuard locked(lock); - Mangler Mang; SmallString<128> FullName; - Mang.getNameWithPrefix(FullName, GV, false); + + const DataLayout &DL = + GV->getParent()->getDataLayout().isDefault() + ? getDataLayout() + : GV->getParent()->getDataLayout(); + + Mangler::getNameWithPrefix(FullName, GV->getName(), DL); return FullName.str(); } diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 492478da89f..c201f39a375 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -270,6 +270,12 @@ void MCJIT::finalizeModule(Module *M) { RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) { SmallString<128> FullName; Mangler::getNameWithPrefix(FullName, Name, getDataLayout()); + + if (void *Addr = getPointerToGlobalIfAvailable(FullName)) + return RuntimeDyld::SymbolInfo(static_cast( + reinterpret_cast(Addr)), + JITSymbolFlags::Exported); + return Dyld.getSymbol(FullName); } diff --git a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp index c7d4dd757cf..16530293270 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp @@ -488,3 +488,36 @@ TEST_F(MCJITCAPITest, yield) { EXPECT_TRUE(didCallYield); } +static int localTestFunc() { + return 42; +} + +TEST_F(MCJITCAPITest, addGlobalMapping) { + SKIP_UNSUPPORTED_PLATFORM; + + Module = LLVMModuleCreateWithName("testModule"); + LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0); + LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType); + + Function = LLVMAddFunction(Module, "test_fn", FunctionType); + LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, ""); + LLVMBuilderRef Builder = LLVMCreateBuilder(); + LLVMPositionBuilderAtEnd(Builder, Entry); + LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, NULL, 0, ""); + LLVMBuildRet(Builder, RetVal); + + LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error); + LLVMDisposeMessage(Error); + + buildMCJITOptions(); + buildMCJITEngine(); + + LLVMAddGlobalMapping(Engine, MappedFn, reinterpret_cast(&localTestFunc)); + + buildAndRunPasses(); + + uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn"); + int (*usable)() = (int (*)()) raw; + + EXPECT_EQ(42, usable()); +}