Index: clang/lib/Interpreter/IncrementalExecutor.cpp =================================================================== --- clang/lib/Interpreter/IncrementalExecutor.cpp +++ clang/lib/Interpreter/IncrementalExecutor.cpp @@ -45,8 +45,11 @@ } const char Pref = Jit->getDataLayout().getGlobalPrefix(); + const bool DoNotMangleLeadingQuestionMark = + Jit->getDataLayout().doNotMangleLeadingQuestionMark(); // Discover symbols from the process as a fallback. - if (auto PSGOrErr = DynamicLibrarySearchGenerator::GetForCurrentProcess(Pref)) + if (auto PSGOrErr = DynamicLibrarySearchGenerator::GetForCurrentProcess( + Pref, DoNotMangleLeadingQuestionMark)) Jit->getMainJITDylib().addGenerator(std::move(*PSGOrErr)); else { Err = PSGOrErr.takeError(); Index: llvm/docs/ORCv2.rst =================================================================== --- llvm/docs/ORCv2.rst +++ llvm/docs/ORCv2.rst @@ -870,7 +870,7 @@ // Use GetForCurrentProcess with a predicate function that checks the // allowed list. JD.addGenerator(cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix(), + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark(), [&](const SymbolStringPtr &S) { return AllowList.count(S); }))); // IR added to JD can now link against any symbols exported by the process Index: llvm/docs/tutorial/BuildingAJIT1.rst =================================================================== --- llvm/docs/tutorial/BuildingAJIT1.rst +++ llvm/docs/tutorial/BuildingAJIT1.rst @@ -143,7 +143,8 @@ DL(std::move(DL)), Mangle(ES, this->DL), Ctx(std::make_unique()) { ES.getMainJITDylib().addGenerator( - cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL.getGlobalPrefix()))); + cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } Our class begins with six member variables: An ExecutionSession member, ``ES``, Index: llvm/docs/tutorial/BuildingAJIT2.rst =================================================================== --- llvm/docs/tutorial/BuildingAJIT2.rst +++ llvm/docs/tutorial/BuildingAJIT2.rst @@ -77,7 +77,8 @@ DL(std::move(DL)), Mangle(ES, this->DL), Ctx(std::make_unique()) { ES.getMainJITDylib().addGenerator( - cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL.getGlobalPrefix()))); + cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } Our extended KaleidoscopeJIT class starts out the same as it did in Chapter 1, Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h =================================================================== --- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h +++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h @@ -53,7 +53,7 @@ MainJD(this->ES->createBareJITDylib("
")) { MainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix()))); + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } ~KaleidoscopeJIT() { Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h =================================================================== --- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h +++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h @@ -60,7 +60,7 @@ MainJD(this->ES->createBareJITDylib("
")) { MainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix()))); + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } ~KaleidoscopeJIT() { Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h =================================================================== --- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h +++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h @@ -74,7 +74,7 @@ MainJD(this->ES->createBareJITDylib("
")) { MainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix()))); + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } ~KaleidoscopeJIT() { Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h =================================================================== --- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h +++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h @@ -159,7 +159,7 @@ MainJD(this->ES->createBareJITDylib("
")) { MainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix()))); + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); } ~KaleidoscopeJIT() { Index: llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h =================================================================== --- llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h +++ llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h @@ -53,7 +53,7 @@ MainJD(this->ES->createBareJITDylib("
")) { MainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL.getGlobalPrefix()))); + DL.getGlobalPrefix(), DL.doNotMangleLeadingQuestionMark()))); if (JTMB.getTargetTriple().isOSBinFormatCOFF()) { ObjectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); ObjectLayer.setAutoClaimResponsibilityForObjectSymbols(true); Index: llvm/examples/OrcV2Examples/LLJITWithGDBRegistrationListener/LLJITWithGDBRegistrationListener.cpp =================================================================== --- llvm/examples/OrcV2Examples/LLJITWithGDBRegistrationListener/LLJITWithGDBRegistrationListener.cpp +++ llvm/examples/OrcV2Examples/LLJITWithGDBRegistrationListener/LLJITWithGDBRegistrationListener.cpp @@ -88,6 +88,7 @@ J->getMainJITDylib().addGenerator( ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess( J->getDataLayout().getGlobalPrefix(), + J->getDataLayout().doNotMangleLeadingQuestionMark(), [MainName = Mangle("main")](const orc::SymbolStringPtr &Name) { return Name != MainName; }))); Index: llvm/examples/OrcV2Examples/LLJITWithObjectLinkingLayerPlugin/LLJITWithObjectLinkingLayerPlugin.cpp =================================================================== --- llvm/examples/OrcV2Examples/LLJITWithObjectLinkingLayerPlugin/LLJITWithObjectLinkingLayerPlugin.cpp +++ llvm/examples/OrcV2Examples/LLJITWithObjectLinkingLayerPlugin/LLJITWithObjectLinkingLayerPlugin.cpp @@ -223,7 +223,8 @@ // objects can do interesting things, like call printf. J->getMainJITDylib().addGenerator( ExitOnErr(DynamicLibrarySearchGenerator::GetForCurrentProcess( - J->getDataLayout().getGlobalPrefix()))); + J->getDataLayout().getGlobalPrefix(), + J->getDataLayout().doNotMangleLeadingQuestionMark()))); // Load the input objects. for (auto InputObject : InputObjects) { Index: llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp =================================================================== --- llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp +++ llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp @@ -69,7 +69,7 @@ auto ProcessSymbolsSearchGenerator = DynamicLibrarySearchGenerator::GetForCurrentProcess( - DL->getGlobalPrefix()); + DL->getGlobalPrefix(), DL->doNotMangleLeadingQuestionMark()); if (!ProcessSymbolsSearchGenerator) return ProcessSymbolsSearchGenerator.takeError(); Index: llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h =================================================================== --- llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -225,6 +225,7 @@ /// will be searched for. If the predicate is not given then all symbols will /// be searched for. DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, char GlobalPrefix, + bool DoNotMangleLeadingQuestionMark = false, SymbolPredicate Allow = SymbolPredicate()); /// Permanently loads the library at the given path and, on success, returns @@ -232,14 +233,17 @@ /// in the library. On failure returns the reason the library failed to load. static Expected> Load(const char *FileName, char GlobalPrefix, + bool DoNotMangleLeadingQuestionMark = false, SymbolPredicate Allow = SymbolPredicate()); /// Creates a DynamicLibrarySearchGenerator that searches for symbols in /// the current process. static Expected> GetForCurrentProcess(char GlobalPrefix, + bool DoNotMangleLeadingQuestionMark = false, SymbolPredicate Allow = SymbolPredicate()) { - return Load(nullptr, GlobalPrefix, std::move(Allow)); + return Load(nullptr, GlobalPrefix, DoNotMangleLeadingQuestionMark, + std::move(Allow)); } Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, @@ -250,6 +254,7 @@ sys::DynamicLibrary Dylib; SymbolPredicate Allow; char GlobalPrefix; + bool DoNotMangleLeadingQuestionMark; }; /// A utility class to expose symbols from a static library. Index: llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp =================================================================== --- llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp +++ llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp @@ -221,19 +221,23 @@ } DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator( - sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow) + sys::DynamicLibrary Dylib, char GlobalPrefix, + bool DoNotMangleLeadingQuestionMark, SymbolPredicate Allow) : Dylib(std::move(Dylib)), Allow(std::move(Allow)), - GlobalPrefix(GlobalPrefix) {} + GlobalPrefix(GlobalPrefix), + DoNotMangleLeadingQuestionMark(DoNotMangleLeadingQuestionMark) {} Expected> DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix, + bool DoNotMangleLeadingQuestionMark, SymbolPredicate Allow) { std::string ErrMsg; auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg); if (!Lib.isValid()) return make_error(std::move(ErrMsg), inconvertibleErrorCode()); return std::make_unique( - std::move(Lib), GlobalPrefix, std::move(Allow)); + std::move(Lib), GlobalPrefix, DoNotMangleLeadingQuestionMark, + std::move(Allow)); } Error DynamicLibrarySearchGenerator::tryToGenerate( @@ -252,11 +256,19 @@ if (Allow && !Allow(Name)) continue; - if (HasGlobalPrefix && (*Name).front() != GlobalPrefix) + bool StripFirstCharacter = HasGlobalPrefix; + char FirstCharacter = (*Name).front(); + if (FirstCharacter == '?' && DoNotMangleLeadingQuestionMark) { + // Continue without stripping the first character; it is ok that the + // mangled name does not start with GlobalPrefix (if there is one). + StripFirstCharacter = false; + } else if (HasGlobalPrefix && FirstCharacter != GlobalPrefix) { + // The mangled name does not start with GlobalPrefix, skip. continue; + } - std::string Tmp((*Name).data() + HasGlobalPrefix, - (*Name).size() - HasGlobalPrefix); + std::string Tmp((*Name).data() + StripFirstCharacter, + (*Name).size() - StripFirstCharacter); if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) { NewSymbols[Name] = JITEvaluatedSymbol( static_cast(reinterpret_cast(Addr)), Index: llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp =================================================================== --- llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp +++ llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp @@ -690,7 +690,8 @@ LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess( LLVMOrcDefinitionGeneratorRef *Result, char GlobalPrefix, - LLVMOrcSymbolPredicate Filter, void *FilterCtx) { + bool DoNotMangleLeadingQuestionMark, LLVMOrcSymbolPredicate Filter, + void *FilterCtx) { assert(Result && "Result can not be null"); assert((Filter || !FilterCtx) && "if Filter is null then FilterCtx must also be null"); @@ -702,7 +703,8 @@ }; auto ProcessSymsGenerator = - DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix, Pred); + DynamicLibrarySearchGenerator::GetForCurrentProcess( + GlobalPrefix, DoNotMangleLeadingQuestionMark, Pred); if (!ProcessSymsGenerator) { *Result = nullptr; @@ -715,7 +717,8 @@ LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForPath( LLVMOrcDefinitionGeneratorRef *Result, const char *FileName, - char GlobalPrefix, LLVMOrcSymbolPredicate Filter, void *FilterCtx) { + char GlobalPrefix, bool DoNotMangleLeadingQuestionMark, + LLVMOrcSymbolPredicate Filter, void *FilterCtx) { assert(Result && "Result can not be null"); assert(FileName && "FileName can not be null"); assert((Filter || !FilterCtx) && @@ -727,8 +730,8 @@ return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name))); }; - auto LibrarySymsGenerator = - DynamicLibrarySearchGenerator::Load(FileName, GlobalPrefix, Pred); + auto LibrarySymsGenerator = DynamicLibrarySearchGenerator::Load( + FileName, GlobalPrefix, DoNotMangleLeadingQuestionMark, Pred); if (!LibrarySymsGenerator) { *Result = nullptr; Index: llvm/tools/lli/lli.cpp =================================================================== --- llvm/tools/lli/lli.cpp +++ llvm/tools/lli/lli.cpp @@ -979,6 +979,7 @@ J->getMainJITDylib().addGenerator( ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess( J->getDataLayout().getGlobalPrefix(), + J->getDataLayout().doNotMangleLeadingQuestionMark(), [MainName = Mangle("main")](const orc::SymbolStringPtr &Name) { return Name != MainName; }))); Index: mlir/lib/ExecutionEngine/ExecutionEngine.cpp =================================================================== --- mlir/lib/ExecutionEngine/ExecutionEngine.cpp +++ mlir/lib/ExecutionEngine/ExecutionEngine.cpp @@ -354,7 +354,8 @@ llvm::orc::JITDylib &mainJD = engine->jit->getMainJITDylib(); mainJD.addGenerator( cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( - dataLayout.getGlobalPrefix()))); + dataLayout.getGlobalPrefix(), + dataLayout.doNotMangleLeadingQuestionMark()))); return std::move(engine); }