1 //===--- SimpleExecutorDylibManager.cpp - Executor-side dylib management --===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h"
11 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12 #include "llvm/Support/FormatVariadic.h"
14 #define DEBUG_TYPE "orc"
18 namespace rt_bootstrap
{
20 SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {
21 assert(Dylibs
.empty() && "shutdown not called?");
24 Expected
<tpctypes::DylibHandle
>
25 SimpleExecutorDylibManager::open(const std::string
&Path
, uint64_t Mode
) {
27 return make_error
<StringError
>("open: non-zero mode bits not yet supported",
28 inconvertibleErrorCode());
30 const char *PathCStr
= Path
.empty() ? nullptr : Path
.c_str();
33 auto DL
= sys::DynamicLibrary::getPermanentLibrary(PathCStr
, &ErrMsg
);
35 return make_error
<StringError
>(std::move(ErrMsg
), inconvertibleErrorCode());
37 std::lock_guard
<std::mutex
> Lock(M
);
38 Dylibs
[NextId
] = std::move(DL
);
42 Expected
<std::vector
<ExecutorAddr
>>
43 SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H
,
44 const RemoteSymbolLookupSet
&L
) {
45 std::vector
<ExecutorAddr
> Result
;
47 std::lock_guard
<std::mutex
> Lock(M
);
48 auto I
= Dylibs
.find(H
);
49 if (I
== Dylibs
.end())
50 return make_error
<StringError
>("No dylib for handle " + formatv("{0:x}", H
),
51 inconvertibleErrorCode());
54 for (const auto &E
: L
) {
58 return make_error
<StringError
>("Required address for empty symbol \"\"",
59 inconvertibleErrorCode());
61 Result
.push_back(ExecutorAddr());
64 const char *DemangledSymName
= E
.Name
.c_str();
66 if (E
.Name
.front() != '_')
67 return make_error
<StringError
>(Twine("MachO symbol \"") + E
.Name
+
68 "\" missing leading '_'",
69 inconvertibleErrorCode());
73 void *Addr
= DL
.getAddressOfSymbol(DemangledSymName
);
74 if (!Addr
&& E
.Required
)
75 return make_error
<StringError
>(Twine("Missing definition for ") +
77 inconvertibleErrorCode());
79 Result
.push_back(ExecutorAddr::fromPtr(Addr
));
86 Error
SimpleExecutorDylibManager::shutdown() {
90 std::lock_guard
<std::mutex
> Lock(M
);
91 std::swap(DM
, Dylibs
);
94 // There is no removal of dylibs at the moment, so nothing to do here.
95 return Error::success();
98 void SimpleExecutorDylibManager::addBootstrapSymbols(
99 StringMap
<ExecutorAddr
> &M
) {
100 M
[rt::SimpleExecutorDylibManagerInstanceName
] = ExecutorAddr::fromPtr(this);
101 M
[rt::SimpleExecutorDylibManagerOpenWrapperName
] =
102 ExecutorAddr::fromPtr(&openWrapper
);
103 M
[rt::SimpleExecutorDylibManagerLookupWrapperName
] =
104 ExecutorAddr::fromPtr(&lookupWrapper
);
107 llvm::orc::shared::CWrapperFunctionResult
108 SimpleExecutorDylibManager::openWrapper(const char *ArgData
, size_t ArgSize
) {
110 WrapperFunction
<rt::SPSSimpleExecutorDylibManagerOpenSignature
>::handle(
112 shared::makeMethodWrapperHandler(
113 &SimpleExecutorDylibManager::open
))
117 llvm::orc::shared::CWrapperFunctionResult
118 SimpleExecutorDylibManager::lookupWrapper(const char *ArgData
, size_t ArgSize
) {
120 WrapperFunction
<rt::SPSSimpleExecutorDylibManagerLookupSignature
>::handle(
122 shared::makeMethodWrapperHandler(
123 &SimpleExecutorDylibManager::lookup
))
127 } // namespace rt_bootstrap
128 } // end namespace orc
129 } // end namespace llvm