@@ -2563,6 +2563,57 @@ namespace Cpp {
25632563 return res == compat::Interpreter::kSuccess ;
25642564 }
25652565
2566+ bool InsertOrReplaceJitSymbol (const char * linker_mangled_name,
2567+ uint64_t address) {
2568+ using namespace llvm ;
2569+ using namespace llvm ::orc;
2570+
2571+ auto & I = getInterp ();
2572+ auto Symbol = compat::getSymbolAddress (I, linker_mangled_name);
2573+ if (Error Err = Symbol.takeError ()) {
2574+ logAllUnhandledErrors (std::move (Err), errs (),
2575+ " [InsertOrReplaceJitSymbol] error: " );
2576+ return true ;
2577+ }
2578+
2579+ // Nothing to define, we are redefining the same function.
2580+ if (*Symbol && *Symbol == address) {
2581+ errs () << " [InsertOrReplaceJitSymbol] warning: redefining '"
2582+ << linker_mangled_name << " ' with the same address\n " ;
2583+ return true ;
2584+ }
2585+
2586+ // Let's inject it.
2587+ bool Inserted;
2588+ SymbolMap::iterator It;
2589+ llvm::orc::SymbolMap InjectedSymbols;
2590+
2591+ llvm::orc::LLJIT& Jit = *compat::getExecutionEngine (I);
2592+ JITDylib& DyLib = Jit.getMainJITDylib ();
2593+
2594+ std::tie (It, Inserted) = InjectedSymbols.try_emplace (
2595+ Jit.getExecutionSession ().intern (linker_mangled_name),
2596+ JITEvaluatedSymbol (address, JITSymbolFlags::Exported));
2597+ assert (Inserted && " Why wasn't this found in the initial Jit lookup?" );
2598+
2599+ // We want to replace a symbol with a custom provided one.
2600+ if (Symbol && address)
2601+ // The symbol be in the DyLib or in-process.
2602+ if (auto Err = DyLib.remove ({It->first })) {
2603+ logAllUnhandledErrors (std::move (Err), errs (),
2604+ " [InsertOrReplaceJitSymbol] error: " );
2605+ return true ;
2606+ }
2607+
2608+ if (Error Err = DyLib.define (absoluteSymbols ({*It}))) {
2609+ logAllUnhandledErrors (std::move (Err), errs (),
2610+ " [InsertOrReplaceJitSymbol] error: " );
2611+ return true ;
2612+ }
2613+
2614+ return false ;
2615+ }
2616+
25662617 std::string ObjToString (const char *type, void *obj) {
25672618 return getInterp ().toString (type, obj);
25682619 }
0 commit comments