Index: lib/ExecutionEngine/MCJIT/MCJIT.cpp =================================================================== --- lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -371,9 +371,11 @@ Mangler Mang(TM->getDataLayout()); SmallString<128> Name; - TM->getNameWithPrefix(Name, F, Mang); if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { + if (F->hasPrivateLinkage()) + return nullptr; + TM->getNameWithPrefix(Name, F, Mang); bool AbortOnFailure = !F->hasExternalWeakLinkage(); void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); updateGlobalMapping(F, Addr); @@ -394,6 +396,10 @@ return nullptr; } + // Do not move this earlier - this function may rely on the target machine + // having been initialized by code generation. + TM->getNameWithPrefix(Name, F, Mang); + // FIXME: Should the Dyld be retaining module information? Probably not. // // This is the accessor for the target address, so make sure to check the Index: unittests/ExecutionEngine/MCJIT/MCJITTest.cpp =================================================================== --- unittests/ExecutionEngine/MCJIT/MCJITTest.cpp +++ unittests/ExecutionEngine/MCJIT/MCJITTest.cpp @@ -199,4 +199,16 @@ EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail."; } +TEST_F(MCJITTest, private_function_lookup) { + SKIP_UNSUPPORTED_PLATFORM; + + Function *Foo = insertMainFunction(M.get(), 0, GlobalValue::PrivateLinkage); + createJIT(std::move(M)); + void *A = TheJIT->getPointerToFunction(Foo); + void *B = TheJIT->getPointerToFunction(Foo); + + EXPECT_TRUE(A != 0) << "Failed lookup - test not correctly configured."; + EXPECT_EQ(A, B) + << "Repeat calls to getPointerToFunction fail for private function."; +} } Index: unittests/ExecutionEngine/MCJIT/MCJITTestBase.h =================================================================== --- unittests/ExecutionEngine/MCJIT/MCJITTestBase.h +++ unittests/ExecutionEngine/MCJIT/MCJITTestBase.h @@ -46,11 +46,12 @@ return M; } - template - Function *startFunction(Module *M, StringRef Name) { + template + Function *startFunction( + Module *M, StringRef Name, + GlobalValue::LinkageTypes linkage = GlobalValue::ExternalLinkage) { Function *Result = Function::Create( - TypeBuilder::get(Context), - GlobalValue::ExternalLinkage, Name, M); + TypeBuilder::get(Context), linkage, Name, M); BasicBlock *BB = BasicBlock::Create(Context, Name, Result); Builder.SetInsertPoint(BB); @@ -82,8 +83,10 @@ // Inserts a function named 'main' that returns a uint32_t: // int32_t main() { return X; } // where X is given by returnCode - Function *insertMainFunction(Module *M, uint32_t returnCode) { - Function *Result = startFunction(M, "main"); + Function *insertMainFunction( + Module *M, uint32_t returnCode, + GlobalValue::LinkageTypes linkage = GlobalValue::ExternalLinkage) { + Function *Result = startFunction(M, "main", linkage); Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode)); endFunctionWithRet(Result, ReturnVal);