1//===--- SimpleExecutorDylibManager.cpp - Executor-side dylib management --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h"
10
11#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12
13#include "llvm/Support/MSVCErrorWorkarounds.h"
14
15#include <future>
16
17#define DEBUG_TYPE "orc"
18
19namespace llvm {
20namespace orc {
21namespace rt_bootstrap {
22
23SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {
24 assert(Dylibs.empty() && "shutdown not called?");
25}
26
27Expected<tpctypes::DylibHandle>
28SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {
29 if (Mode != 0)
30 return make_error<StringError>(Args: "open: non-zero mode bits not yet supported",
31 Args: inconvertibleErrorCode());
32
33 const char *PathCStr = Path.empty() ? nullptr : Path.c_str();
34 std::string ErrMsg;
35
36 auto DL = sys::DynamicLibrary::getPermanentLibrary(filename: PathCStr, errMsg: &ErrMsg);
37 if (!DL.isValid())
38 return make_error<StringError>(Args: std::move(ErrMsg), Args: inconvertibleErrorCode());
39
40 std::lock_guard<std::mutex> Lock(M);
41 auto H = ExecutorAddr::fromPtr(Ptr: DL.getOSSpecificHandle());
42 Resolvers.push_back(x: std::make_unique<DylibSymbolResolver>(args&: H));
43 Dylibs.insert(V: DL.getOSSpecificHandle());
44 return ExecutorAddr::fromPtr(Ptr: Resolvers.back().get());
45}
46
47Error SimpleExecutorDylibManager::shutdown() {
48
49 DylibSet DS;
50 {
51 std::lock_guard<std::mutex> Lock(M);
52 std::swap(a&: DS, b&: Dylibs);
53 }
54
55 // There is no removal of dylibs at the moment, so nothing to do here.
56 return Error::success();
57}
58
59void SimpleExecutorDylibManager::addBootstrapSymbols(
60 StringMap<ExecutorAddr> &M) {
61 M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(Ptr: this);
62 M[rt::SimpleExecutorDylibManagerOpenWrapperName] =
63 ExecutorAddr::fromPtr(Ptr: &openWrapper);
64 M[rt::SimpleExecutorDylibManagerResolveWrapperName] =
65 ExecutorAddr::fromPtr(Ptr: &resolveWrapper);
66}
67
68llvm::orc::shared::CWrapperFunctionBuffer
69SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {
70 return shared::
71 WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(
72 ArgData, ArgSize,
73 Handler: shared::makeMethodWrapperHandler(
74 Method: &SimpleExecutorDylibManager::open))
75 .release();
76}
77
78llvm::orc::shared::CWrapperFunctionBuffer
79SimpleExecutorDylibManager::resolveWrapper(const char *ArgData,
80 size_t ArgSize) {
81 using ResolveResult = ExecutorResolver::ResolveResult;
82 return shared::WrapperFunction<
83 rt::SPSSimpleExecutorDylibManagerResolveSignature>::
84 handle(ArgData, ArgSize,
85 Handler: [](ExecutorAddr Obj, RemoteSymbolLookupSet L) -> ResolveResult {
86 using TmpResult =
87 MSVCPExpected<std::vector<std::optional<ExecutorSymbolDef>>>;
88 std::promise<TmpResult> P;
89 auto F = P.get_future();
90 Obj.toPtr<ExecutorResolver *>()->resolveAsync(
91 L: std::move(L),
92 OnResolve: [&](TmpResult R) { P.set_value(std::move(R)); });
93 return F.get();
94 })
95 .release();
96}
97
98} // namespace rt_bootstrap
99} // end namespace orc
100} // end namespace llvm
101