diff --git a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 4cf3e2044dff..efd30ba7580a 100644 --- a/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -351,9 +351,13 @@ uint64_t MCJIT::getFunctionAddress(const std::string &Name) { void *MCJIT::getPointerToFunction(Function *F) { MutexGuard locked(lock); + Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); + SmallString<128> Name; + TM->getNameWithPrefix(Name, F, Mang); + if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { bool AbortOnFailure = !F->hasExternalWeakLinkage(); - void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); + void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); addGlobalMapping(F, Addr); return Addr; } @@ -364,17 +368,18 @@ void *MCJIT::getPointerToFunction(Function *F) { // Make sure the relevant module has been compiled and loaded. if (HasBeenAddedButNotLoaded) generateCodeForModule(M); - else if (!OwnedModules.hasModuleBeenLoaded(M)) + else if (!OwnedModules.hasModuleBeenLoaded(M)) { // If this function doesn't belong to one of our modules, we're done. + // FIXME: Asking for the pointer to a function that hasn't been registered, + // and isn't a declaration (which is handled above) should probably + // be an assertion. return nullptr; + } // FIXME: Should the Dyld be retaining module information? Probably not. // // This is the accessor for the target address, so make sure to check the // load address of the symbol, not the local address. - Mangler Mang(TM->getSubtargetImpl()->getDataLayout()); - SmallString<128> Name; - TM->getNameWithPrefix(Name, F, Mang); return (void*)Dyld.getSymbolLoadAddress(Name); } diff --git a/llvm/lib/ExecutionEngine/RTDyldMemoryManager.cpp b/llvm/lib/ExecutionEngine/RTDyldMemoryManager.cpp index 1646937e816c..9cbcd2fa8142 100644 --- a/llvm/lib/ExecutionEngine/RTDyldMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/RTDyldMemoryManager.cpp @@ -253,19 +253,20 @@ uint64_t RTDyldMemoryManager::getSymbolAddress(const std::string &Name) { // is called before ExecutionEngine::runFunctionAsMain() is called. if (Name == "__main") return (uint64_t)&jit_noop; - const char *NameStr = Name.c_str(); - void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr); - if (Ptr) - return (uint64_t)Ptr; + // Try to demangle Name before looking it up in the process, otherwise symbol + // '_' (if present) will shadow '', and there will be no way to + // refer to the latter. - // If it wasn't found and if it starts with an underscore ('_') character, - // try again without the underscore. - if (NameStr[0] == '_') { - Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1); - if (Ptr) + const char *NameStr = Name.c_str(); + + if (NameStr[0] == '_') + if (void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr + 1)) return (uint64_t)Ptr; - } - return 0; + + // If we Name did not require demangling, or we failed to find the demangled + // name, try again without demangling. + if (void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr)) + return (uint64_t)Ptr; } void *RTDyldMemoryManager::getPointerToNamedFunction(const std::string &Name,