Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.cpp
35323 views
//===--- SimpleExecutorDylibManager.cpp - Executor-side dylib management --===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h"910#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"11#include "llvm/Support/FormatVariadic.h"1213#define DEBUG_TYPE "orc"1415namespace llvm {16namespace orc {17namespace rt_bootstrap {1819SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {20assert(Dylibs.empty() && "shutdown not called?");21}2223Expected<tpctypes::DylibHandle>24SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {25if (Mode != 0)26return make_error<StringError>("open: non-zero mode bits not yet supported",27inconvertibleErrorCode());2829const char *PathCStr = Path.empty() ? nullptr : Path.c_str();30std::string ErrMsg;3132auto DL = sys::DynamicLibrary::getPermanentLibrary(PathCStr, &ErrMsg);33if (!DL.isValid())34return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());3536std::lock_guard<std::mutex> Lock(M);37auto H = ExecutorAddr::fromPtr(DL.getOSSpecificHandle());38Dylibs.insert(DL.getOSSpecificHandle());39return H;40}4142Expected<std::vector<ExecutorSymbolDef>>43SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,44const RemoteSymbolLookupSet &L) {45std::vector<ExecutorSymbolDef> Result;46auto DL = sys::DynamicLibrary(H.toPtr<void *>());4748for (const auto &E : L) {49if (E.Name.empty()) {50if (E.Required)51return make_error<StringError>("Required address for empty symbol \"\"",52inconvertibleErrorCode());53else54Result.push_back(ExecutorSymbolDef());55} else {5657const char *DemangledSymName = E.Name.c_str();58#ifdef __APPLE__59if (E.Name.front() != '_')60return make_error<StringError>(Twine("MachO symbol \"") + E.Name +61"\" missing leading '_'",62inconvertibleErrorCode());63++DemangledSymName;64#endif6566void *Addr = DL.getAddressOfSymbol(DemangledSymName);67if (!Addr && E.Required)68return make_error<StringError>(Twine("Missing definition for ") +69DemangledSymName,70inconvertibleErrorCode());7172// FIXME: determine accurate JITSymbolFlags.73Result.push_back({ExecutorAddr::fromPtr(Addr), JITSymbolFlags::Exported});74}75}7677return Result;78}7980Error SimpleExecutorDylibManager::shutdown() {8182DylibSet DS;83{84std::lock_guard<std::mutex> Lock(M);85std::swap(DS, Dylibs);86}8788// There is no removal of dylibs at the moment, so nothing to do here.89return Error::success();90}9192void SimpleExecutorDylibManager::addBootstrapSymbols(93StringMap<ExecutorAddr> &M) {94M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(this);95M[rt::SimpleExecutorDylibManagerOpenWrapperName] =96ExecutorAddr::fromPtr(&openWrapper);97M[rt::SimpleExecutorDylibManagerLookupWrapperName] =98ExecutorAddr::fromPtr(&lookupWrapper);99}100101llvm::orc::shared::CWrapperFunctionResult102SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {103return shared::104WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(105ArgData, ArgSize,106shared::makeMethodWrapperHandler(107&SimpleExecutorDylibManager::open))108.release();109}110111llvm::orc::shared::CWrapperFunctionResult112SimpleExecutorDylibManager::lookupWrapper(const char *ArgData, size_t ArgSize) {113return shared::114WrapperFunction<rt::SPSSimpleExecutorDylibManagerLookupSignature>::handle(115ArgData, ArgSize,116shared::makeMethodWrapperHandler(117&SimpleExecutorDylibManager::lookup))118.release();119}120121} // namespace rt_bootstrap122} // end namespace orc123} // end namespace llvm124125126