Skip to content

Commit 4565a9d

Browse files
hahnjodevajithvs
authored andcommitted
[cling] Move generators to ProcessSymbols JITDylib
This makes them run after the <Platform> library: It provides the __cxa_atexit function, which must not be found in the process.
1 parent d919a69 commit 4565a9d

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

interpreter/cling/lib/Interpreter/IncrementalJIT.cpp

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,34 @@ IncrementalJIT::IncrementalJIT(
501501
return std::make_unique<SimpleCompiler>(*m_TM);
502502
});
503503

504+
char LinkerPrefix = this->m_TM->createDataLayout().getGlobalPrefix();
505+
506+
Builder.setProcessSymbolsJITDylibSetup([&](LLJIT& J) -> Expected<JITDylibSP> {
507+
auto& JD = J.getExecutionSession().createBareJITDylib("<Process Symbols>");
508+
// Process symbol resolution
509+
auto HostProcessLookup =
510+
RTDynamicLibrarySearchGenerator::GetForCurrentProcess(
511+
LinkerPrefix, [this] { return m_CurrentProcessRT; },
512+
[this](const SymbolStringPtr& Sym) {
513+
return !m_ForbidDlSymbols.contains(*Sym);
514+
});
515+
if (!HostProcessLookup) {
516+
return HostProcessLookup.takeError();
517+
}
518+
JD.addGenerator(std::move(*HostProcessLookup));
519+
520+
// This must come after process resolution, to consistently resolve global
521+
// symbols (e.g. std::cout) to the same address.
522+
auto LibLookup = std::make_unique<RTDynamicLibrarySearchGenerator>(
523+
llvm::sys::DynamicLibrary(ExtraLibHandle), LinkerPrefix,
524+
[this] { return m_CurrentProcessRT; },
525+
[this](const SymbolStringPtr& Sym) {
526+
return !m_ForbidDlSymbols.contains(*Sym);
527+
});
528+
JD.addGenerator(std::move(LibLookup));
529+
return &JD;
530+
});
531+
504532
if (Expected<std::unique_ptr<LLJIT>> JitInstance = Builder.create()) {
505533
Jit = std::move(*JitInstance);
506534
} else {
@@ -518,32 +546,9 @@ IncrementalJIT::IncrementalJIT(
518546
m_CompiledModules[Unsafe] = std::move(TSM);
519547
});
520548

521-
char LinkerPrefix = this->m_TM->createDataLayout().getGlobalPrefix();
522-
523-
// Process symbol resolution
524-
auto HostProcessLookup
525-
= RTDynamicLibrarySearchGenerator::GetForCurrentProcess(LinkerPrefix,
526-
[&]{ return m_CurrentRT; },
527-
[&](const SymbolStringPtr &Sym) {
528-
return !m_ForbidDlSymbols.contains(*Sym); });
529-
if (!HostProcessLookup) {
530-
Err = HostProcessLookup.takeError();
531-
return;
532-
}
533-
Jit->getMainJITDylib().addGenerator(std::move(*HostProcessLookup));
534-
535-
// This must come after process resolution, to consistently resolve global
536-
// symbols (e.g. std::cout) to the same address.
537-
auto LibLookup = std::make_unique<RTDynamicLibrarySearchGenerator>(
538-
llvm::sys::DynamicLibrary(ExtraLibHandle), LinkerPrefix,
539-
[&]{ return m_CurrentRT; },
540-
[&](const SymbolStringPtr &Sym) {
541-
return !m_ForbidDlSymbols.contains(*Sym); });
542-
Jit->getMainJITDylib().addGenerator(std::move(LibLookup));
543-
544549
#if defined(__linux__) && defined(__GLIBC__)
545550
// See comment in ListOfLibcNonsharedSymbols.
546-
cantFail(Jit->getMainJITDylib().define(
551+
cantFail(Jit->getProcessSymbolsJITDylib()->define(
547552
absoluteSymbols(GetListOfLibcNonsharedSymbols(*Jit))));
548553
#endif
549554

@@ -585,8 +590,11 @@ std::unique_ptr<llvm::orc::DefinitionGenerator> IncrementalJIT::getGenerator() {
585590
}
586591

587592
void IncrementalJIT::addModule(Transaction& T) {
588-
ResourceTrackerSP RT = Jit->getMainJITDylib().createResourceTracker();
589-
m_ResourceTrackers[&T] = RT;
593+
ResourceTrackerSP MainRT = Jit->getMainJITDylib().createResourceTracker();
594+
m_MainResourceTrackers[&T] = MainRT;
595+
ResourceTrackerSP ProcessRT =
596+
Jit->getProcessSymbolsJITDylib()->createResourceTracker();
597+
m_ProcessResourceTrackers[&T] = ProcessRT;
590598

591599
std::unique_ptr<Module> module = T.takeModule();
592600

@@ -609,22 +617,26 @@ void IncrementalJIT::addModule(Transaction& T) {
609617

610618
const Module *Unsafe = TSM.getModuleUnlocked();
611619
T.m_CompiledModule = Unsafe;
612-
m_CurrentRT = RT;
620+
m_CurrentProcessRT = ProcessRT;
613621

614-
if (Error Err = Jit->addIRModule(RT, std::move(TSM))) {
622+
if (Error Err = Jit->addIRModule(MainRT, std::move(TSM))) {
615623
logAllUnhandledErrors(std::move(Err), errs(),
616624
"[IncrementalJIT] addModule() failed: ");
617625
return;
618626
}
619627
}
620628

621629
llvm::Error IncrementalJIT::removeModule(const Transaction& T) {
622-
ResourceTrackerSP RT = std::move(m_ResourceTrackers[&T]);
623-
if (!RT)
630+
ResourceTrackerSP MainRT = std::move(m_MainResourceTrackers[&T]);
631+
if (!MainRT)
624632
return llvm::Error::success();
633+
ResourceTrackerSP ProcessRT = std::move(m_ProcessResourceTrackers[&T]);
625634

626-
m_ResourceTrackers.erase(&T);
627-
if (Error Err = RT->remove())
635+
m_MainResourceTrackers.erase(&T);
636+
m_ProcessResourceTrackers.erase(&T);
637+
if (Error Err = MainRT->remove())
638+
return Err;
639+
if (Error Err = ProcessRT->remove())
628640
return Err;
629641
auto iMod = m_CompiledModules.find(T.m_CompiledModule);
630642
if (iMod != m_CompiledModules.end())
@@ -667,7 +679,14 @@ void* IncrementalJIT::getSymbolAddress(StringRef Name, bool IncludeHostSymbols){
667679
if (!IncludeHostSymbols)
668680
insertInfo = m_ForbidDlSymbols.insert(Name);
669681

670-
Expected<llvm::orc::ExecutorAddr> Symbol = Jit->lookup(Name);
682+
Expected<llvm::orc::ExecutorAddr> Symbol =
683+
Jit->lookup(Jit->getMainJITDylib(), Name);
684+
if (!Symbol) {
685+
// FIXME: We should take advantage of the fact that all process symbols
686+
// are now in a separate JITDylib; see also the comments and ideas in
687+
// IncrementalExecutor::getAddressOfGlobal().
688+
Symbol = Jit->lookup(*Jit->getProcessSymbolsJITDylib(), Name);
689+
}
671690

672691
// If m_ForbidDlSymbols already contained Name before we tried to insert it
673692
// then some calling frame has added it and will remove it later because its

interpreter/cling/lib/Interpreter/IncrementalJIT.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class IncrementalJIT {
6565
/// Register a DefinitionGenerator to dynamically provide symbols for
6666
/// generated code that are not already available within the process.
6767
void addGenerator(std::unique_ptr<llvm::orc::DefinitionGenerator> G) {
68-
Jit->getMainJITDylib().addGenerator(std::move(G));
68+
Jit->getProcessSymbolsJITDylib()->addGenerator(std::move(G));
6969
}
7070

7171
/// Return a `DefinitionGenerator` that can provide addresses for symbols
@@ -109,13 +109,14 @@ class IncrementalJIT {
109109
llvm::orc::SymbolMap m_InjectedSymbols;
110110
SharedAtomicFlag SkipHostProcessLookup;
111111
llvm::StringSet<> m_ForbidDlSymbols;
112-
llvm::orc::ResourceTrackerSP m_CurrentRT;
112+
llvm::orc::ResourceTrackerSP m_CurrentProcessRT;
113113

114114
/// FIXME: If the relation between modules and transactions is a bijection, the
115115
/// mapping via module pointers here is unnecessary. The transaction should
116116
/// store the resource tracker directly and pass it to `remove()` for
117117
/// unloading.
118-
std::map<const Transaction*, llvm::orc::ResourceTrackerSP> m_ResourceTrackers;
118+
std::map<const Transaction*, llvm::orc::ResourceTrackerSP> m_MainResourceTrackers;
119+
std::map<const Transaction*, llvm::orc::ResourceTrackerSP> m_ProcessResourceTrackers;
119120
std::map<const llvm::Module *, llvm::orc::ThreadSafeModule> m_CompiledModules;
120121

121122
bool m_JITLink;

0 commit comments

Comments
 (0)