101 lines · cpp
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-exception6//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>("open: non-zero mode bits not yet supported",31 inconvertibleErrorCode());32 33 const char *PathCStr = Path.empty() ? nullptr : Path.c_str();34 std::string ErrMsg;35 36 auto DL = sys::DynamicLibrary::getPermanentLibrary(PathCStr, &ErrMsg);37 if (!DL.isValid())38 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());39 40 std::lock_guard<std::mutex> Lock(M);41 auto H = ExecutorAddr::fromPtr(DL.getOSSpecificHandle());42 Resolvers.push_back(std::make_unique<DylibSymbolResolver>(H));43 Dylibs.insert(DL.getOSSpecificHandle());44 return ExecutorAddr::fromPtr(Resolvers.back().get());45}46 47Error SimpleExecutorDylibManager::shutdown() {48 49 DylibSet DS;50 {51 std::lock_guard<std::mutex> Lock(M);52 std::swap(DS, 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(this);62 M[rt::SimpleExecutorDylibManagerOpenWrapperName] =63 ExecutorAddr::fromPtr(&openWrapper);64 M[rt::SimpleExecutorDylibManagerResolveWrapperName] =65 ExecutorAddr::fromPtr(&resolveWrapper);66}67 68llvm::orc::shared::CWrapperFunctionResult69SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {70 return shared::71 WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(72 ArgData, ArgSize,73 shared::makeMethodWrapperHandler(74 &SimpleExecutorDylibManager::open))75 .release();76}77 78llvm::orc::shared::CWrapperFunctionResult79SimpleExecutorDylibManager::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 [](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 std::move(L),92 [&](TmpResult R) { P.set_value(std::move(R)); });93 return F.get();94 })95 .release();96}97 98} // namespace rt_bootstrap99} // end namespace orc100} // end namespace llvm101