1//===------- EPCGenericDylibManager.cpp -- Dylib management via EPC -------===//
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/EPCGenericDylibManager.h"
10
11#include "llvm/ExecutionEngine/Orc/Core.h"
12#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
13#include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
14
15namespace llvm {
16namespace orc {
17namespace shared {
18
19template <>
20class SPSSerializationTraits<SPSRemoteSymbolLookupSetElement,
21 SymbolLookupSet::value_type> {
22public:
23 static size_t size(const SymbolLookupSet::value_type &V) {
24 return SPSArgList<SPSString, bool>::size(
25 Arg: *V.first, Args: V.second == SymbolLookupFlags::RequiredSymbol);
26 }
27
28 static bool serialize(SPSOutputBuffer &OB,
29 const SymbolLookupSet::value_type &V) {
30 return SPSArgList<SPSString, bool>::serialize(
31 OB, Arg: *V.first, Args: V.second == SymbolLookupFlags::RequiredSymbol);
32 }
33};
34
35template <>
36class TrivialSPSSequenceSerialization<SPSRemoteSymbolLookupSetElement,
37 SymbolLookupSet> {
38public:
39 static constexpr bool available = true;
40};
41
42template <>
43class SPSSerializationTraits<SPSRemoteSymbolLookup,
44 ExecutorProcessControl::LookupRequest> {
45 using MemberSerialization =
46 SPSArgList<SPSExecutorAddr, SPSRemoteSymbolLookupSet>;
47
48public:
49 static size_t size(const ExecutorProcessControl::LookupRequest &LR) {
50 return MemberSerialization::size(Arg: ExecutorAddr(LR.Handle), Args: LR.Symbols);
51 }
52
53 static bool serialize(SPSOutputBuffer &OB,
54 const ExecutorProcessControl::LookupRequest &LR) {
55 return MemberSerialization::serialize(OB, Arg: ExecutorAddr(LR.Handle),
56 Args: LR.Symbols);
57 }
58};
59
60} // end namespace shared
61
62Expected<EPCGenericDylibManager>
63EPCGenericDylibManager::CreateWithDefaultBootstrapSymbols(
64 ExecutorProcessControl &EPC) {
65 SymbolAddrs SAs;
66 if (auto Err = EPC.getBootstrapSymbols(
67 Pairs: {{SAs.Instance, rt::SimpleExecutorDylibManagerInstanceName},
68 {SAs.Open, rt::SimpleExecutorDylibManagerOpenWrapperName},
69 {SAs.Lookup, rt::SimpleExecutorDylibManagerLookupWrapperName}}))
70 return std::move(Err);
71 return EPCGenericDylibManager(EPC, std::move(SAs));
72}
73
74Expected<tpctypes::DylibHandle> EPCGenericDylibManager::open(StringRef Path,
75 uint64_t Mode) {
76 Expected<tpctypes::DylibHandle> H((ExecutorAddr()));
77 if (auto Err =
78 EPC.callSPSWrapper<rt::SPSSimpleExecutorDylibManagerOpenSignature>(
79 WrapperFnAddr: SAs.Open, WrapperCallArgs&: H, WrapperCallArgs&: SAs.Instance, WrapperCallArgs&: Path, WrapperCallArgs&: Mode))
80 return std::move(Err);
81 return H;
82}
83
84void EPCGenericDylibManager::lookupAsync(tpctypes::DylibHandle H,
85 const SymbolLookupSet &Lookup,
86 SymbolLookupCompleteFn Complete) {
87 EPC.callSPSWrapperAsync<rt::SPSSimpleExecutorDylibManagerLookupSignature>(
88 WrapperFnAddr: SAs.Lookup,
89 SendResult: [Complete = std::move(Complete)](
90 Error SerializationErr,
91 Expected<std::vector<ExecutorSymbolDef>> Result) mutable {
92 if (SerializationErr) {
93 cantFail(Err: Result.takeError());
94 Complete(std::move(SerializationErr));
95 return;
96 }
97 Complete(std::move(Result));
98 },
99 Args: SAs.Instance, Args: H, Args: Lookup);
100}
101
102void EPCGenericDylibManager::lookupAsync(tpctypes::DylibHandle H,
103 const RemoteSymbolLookupSet &Lookup,
104 SymbolLookupCompleteFn Complete) {
105 EPC.callSPSWrapperAsync<rt::SPSSimpleExecutorDylibManagerLookupSignature>(
106 WrapperFnAddr: SAs.Lookup,
107 SendResult: [Complete = std::move(Complete)](
108 Error SerializationErr,
109 Expected<std::vector<ExecutorSymbolDef>> Result) mutable {
110 if (SerializationErr) {
111 cantFail(Err: Result.takeError());
112 Complete(std::move(SerializationErr));
113 return;
114 }
115 Complete(std::move(Result));
116 },
117 Args: SAs.Instance, Args: H, Args: Lookup);
118}
119
120} // end namespace orc
121} // end namespace llvm
122