[clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cas… · llvm/llvm-project@8c2dc1b (original) (raw)
4 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -57,7 +57,7 @@ class IncrementalExecutor { | ||
57 | 57 | virtual llvm::Error removeModule(PartialTranslationUnit &PTU); |
58 | 58 | virtual llvm::Error runCtors() const; |
59 | 59 | virtual llvm::Error cleanUp(); |
60 | - llvm::Expectedllvm::orc::ExecutorAddr | |
60 | +virtual llvm::Expectedllvm::orc::ExecutorAddr | |
61 | 61 | getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; |
62 | 62 | |
63 | 63 | llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -18,6 +18,7 @@ | ||
18 | 18 | #include "llvm/Support/VirtualFileSystem.h" |
19 | 19 | #ifdef __EMSCRIPTEN__ |
20 | 20 | #include "Wasm.h" |
21 | +#include <dlfcn.h> | |
21 | 22 | #endif // __EMSCRIPTEN__ |
22 | 23 | |
23 | 24 | #include "clang/AST/ASTConsumer.h" |
@@ -737,6 +738,14 @@ llvm::Error Interpreter::Undo(unsigned N) { | ||
737 | 738 | } |
738 | 739 | |
739 | 740 | llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { |
741 | +#ifdef __EMSCRIPTEN__ | |
742 | +void *handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL); | |
743 | +if (!handle) { | |
744 | +llvm::errs() << dlerror() << '\n'; | |
745 | +return llvm::make_errorllvm::StringError("Failed to load dynamic library", | |
746 | +llvm::inconvertibleErrorCode()); | |
747 | + } | |
748 | +#else | |
740 | 749 | auto EE = getExecutionEngine(); |
741 | 750 | if (!EE) |
742 | 751 | return EE.takeError(); |
@@ -748,6 +757,7 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { | ||
748 | 757 | EE->getMainJITDylib().addGenerator(std::move(*DLSG)); |
749 | 758 | else |
750 | 759 | return DLSG.takeError(); |
760 | +#endif | |
751 | 761 | |
752 | 762 | return llvm::Error::success(); |
753 | 763 | } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -144,6 +144,19 @@ llvm::Error WasmIncrementalExecutor::cleanUp() { | ||
144 | 144 | return llvm::Error::success(); |
145 | 145 | } |
146 | 146 | |
147 | +llvm::Expectedllvm::orc::ExecutorAddr | |
148 | +WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name, | |
149 | + SymbolNameKind NameKind) const { | |
150 | +void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str()); | |
151 | +if (!Sym) { | |
152 | +return llvm::make_errorllvm::StringError("dlsym failed for symbol: " + | |
153 | + Name.str(), | |
154 | +llvm::inconvertibleErrorCode()); | |
155 | + } | |
156 | + | |
157 | +return llvm::orc::ExecutorAddr::fromPtr(Sym); | |
158 | +} | |
159 | + | |
147 | 160 | WasmIncrementalExecutor::~WasmIncrementalExecutor() = default; |
148 | 161 | |
149 | 162 | } // namespace clang |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -29,6 +29,9 @@ class WasmIncrementalExecutor : public IncrementalExecutor { | ||
29 | 29 | llvm::Error removeModule(PartialTranslationUnit &PTU) override; |
30 | 30 | llvm::Error runCtors() const override; |
31 | 31 | llvm::Error cleanUp() override; |
32 | + llvm::Expectedllvm::orc::ExecutorAddr | |
33 | +getSymbolAddress(llvm::StringRef Name, | |
34 | + SymbolNameKind NameKind) const override; | |
32 | 35 | |
33 | 36 | ~WasmIncrementalExecutor() override; |
34 | 37 | }; |