Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 03b8701

Browse files
Allow symbol resolution from the host process
1 parent 8cf690b commit 03b8701

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ target_compile_definitions(JitFromScratch PRIVATE ${LLVM_DEFINITIONS})
9393
target_link_libraries(JitFromScratch PRIVATE ${llvm_libs})
9494
target_compile_options(JitFromScratch PRIVATE ${rtti_flags})
9595

96+
# Allow dlopen() to access exported symbols.
97+
# This will add linker flags like -Wl,--export-dynamic -rdynamic
98+
export_executable_symbols(JitFromScratch)
99+
96100
function(dump_target_properties target property)
97101
get_target_property(values ${target} ${property})
98102
if(values)

JitFromScratch.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ JitFromScratch::JitFromScratch(ExitOnError ExitOnErr) {
1313
ExitOnErr(Builder.prepareForConstruction());
1414
TT = Builder.JTMB->getTargetTriple();
1515
LLJIT = ExitOnErr(Builder.create());
16+
17+
if (auto R = createHostProcessResolver(LLJIT->getDataLayout()))
18+
LLJIT->getMainJITDylib().setGenerator(std::move(R));
19+
}
20+
21+
JITDylib::GeneratorFunction
22+
JitFromScratch::createHostProcessResolver(DataLayout DL) {
23+
char Prefix = DL.getGlobalPrefix();
24+
Expected<JITDylib::GeneratorFunction> R =
25+
DynamicLibrarySearchGenerator::GetForCurrentProcess(Prefix);
26+
27+
if (!R) {
28+
LLJIT->getExecutionSession().reportError(R.takeError());
29+
return nullptr;
30+
}
31+
32+
if (!*R) {
33+
LLJIT->getExecutionSession().reportError(createStringError(
34+
inconvertibleErrorCode(),
35+
"Generator function for host process symbols must not be null"));
36+
return nullptr;
37+
}
38+
39+
return *R;
1640
}
1741

1842
Error JitFromScratch::submitModule(std::unique_ptr<Module> M,

JitFromScratch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@ class JitFromScratch {
4646
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
4747
llvm::Triple TT;
4848

49+
llvm::orc::JITDylib::GeneratorFunction
50+
createHostProcessResolver(llvm::DataLayout DL);
51+
4952
llvm::Expected<llvm::JITTargetAddress> getFunctionAddr(llvm::StringRef Name);
5053
};

0 commit comments

Comments
 (0)