1a2c1cf09SLang Hames //===--- SimpleExecutorDylibManager.cpp - Executor-side dylib management --===// 2a2c1cf09SLang Hames // 3a2c1cf09SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4a2c1cf09SLang Hames // See https://llvm.org/LICENSE.txt for license information. 5a2c1cf09SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a2c1cf09SLang Hames // 7a2c1cf09SLang Hames //===----------------------------------------------------------------------===// 8a2c1cf09SLang Hames 9a2c1cf09SLang Hames #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h" 10a2c1cf09SLang Hames 11a2c1cf09SLang Hames #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h" 12a2c1cf09SLang Hames 13a2c1cf09SLang Hames #define DEBUG_TYPE "orc" 14a2c1cf09SLang Hames 15a2c1cf09SLang Hames namespace llvm { 16a2c1cf09SLang Hames namespace orc { 17a2c1cf09SLang Hames namespace rt_bootstrap { 18a2c1cf09SLang Hames 19a2c1cf09SLang Hames SimpleExecutorDylibManager::~SimpleExecutorDylibManager() { 20a2c1cf09SLang Hames assert(Dylibs.empty() && "shutdown not called?"); 21a2c1cf09SLang Hames } 22a2c1cf09SLang Hames 23a2c1cf09SLang Hames Expected<tpctypes::DylibHandle> 24a2c1cf09SLang Hames SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) { 25a2c1cf09SLang Hames if (Mode != 0) 26a2c1cf09SLang Hames return make_error<StringError>("open: non-zero mode bits not yet supported", 27a2c1cf09SLang Hames inconvertibleErrorCode()); 28a2c1cf09SLang Hames 29a2c1cf09SLang Hames const char *PathCStr = Path.empty() ? nullptr : Path.c_str(); 30a2c1cf09SLang Hames std::string ErrMsg; 31a2c1cf09SLang Hames 32a2c1cf09SLang Hames auto DL = sys::DynamicLibrary::getPermanentLibrary(PathCStr, &ErrMsg); 33a2c1cf09SLang Hames if (!DL.isValid()) 34a2c1cf09SLang Hames return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()); 35a2c1cf09SLang Hames 36a2c1cf09SLang Hames std::lock_guard<std::mutex> Lock(M); 376613f4afSLang Hames auto H = ExecutorAddr::fromPtr(DL.getOSSpecificHandle()); 386613f4afSLang Hames Dylibs.insert(DL.getOSSpecificHandle()); 396613f4afSLang Hames return H; 40a2c1cf09SLang Hames } 41a2c1cf09SLang Hames 42*40b4ac27SBen Langmuir Expected<std::vector<ExecutorSymbolDef>> 43a2c1cf09SLang Hames SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H, 44a2c1cf09SLang Hames const RemoteSymbolLookupSet &L) { 45*40b4ac27SBen Langmuir std::vector<ExecutorSymbolDef> Result; 466613f4afSLang Hames auto DL = sys::DynamicLibrary(H.toPtr<void *>()); 47a2c1cf09SLang Hames 48a2c1cf09SLang Hames for (const auto &E : L) { 49a2c1cf09SLang Hames if (E.Name.empty()) { 50a2c1cf09SLang Hames if (E.Required) 51a2c1cf09SLang Hames return make_error<StringError>("Required address for empty symbol \"\"", 52a2c1cf09SLang Hames inconvertibleErrorCode()); 53a2c1cf09SLang Hames else 54*40b4ac27SBen Langmuir Result.push_back(ExecutorSymbolDef()); 55a2c1cf09SLang Hames } else { 56a2c1cf09SLang Hames 57a2c1cf09SLang Hames const char *DemangledSymName = E.Name.c_str(); 58a2c1cf09SLang Hames #ifdef __APPLE__ 59a2c1cf09SLang Hames if (E.Name.front() != '_') 60a2c1cf09SLang Hames return make_error<StringError>(Twine("MachO symbol \"") + E.Name + 61a2c1cf09SLang Hames "\" missing leading '_'", 62a2c1cf09SLang Hames inconvertibleErrorCode()); 63a2c1cf09SLang Hames ++DemangledSymName; 64a2c1cf09SLang Hames #endif 65a2c1cf09SLang Hames 66a2c1cf09SLang Hames void *Addr = DL.getAddressOfSymbol(DemangledSymName); 67a2c1cf09SLang Hames if (!Addr && E.Required) 68a2c1cf09SLang Hames return make_error<StringError>(Twine("Missing definition for ") + 69a2c1cf09SLang Hames DemangledSymName, 70a2c1cf09SLang Hames inconvertibleErrorCode()); 71a2c1cf09SLang Hames 72*40b4ac27SBen Langmuir // FIXME: determine accurate JITSymbolFlags. 73*40b4ac27SBen Langmuir Result.push_back({ExecutorAddr::fromPtr(Addr), JITSymbolFlags::Exported}); 74a2c1cf09SLang Hames } 75a2c1cf09SLang Hames } 76a2c1cf09SLang Hames 77a2c1cf09SLang Hames return Result; 78a2c1cf09SLang Hames } 79a2c1cf09SLang Hames 80a2c1cf09SLang Hames Error SimpleExecutorDylibManager::shutdown() { 81a2c1cf09SLang Hames 826613f4afSLang Hames DylibSet DS; 83a2c1cf09SLang Hames { 84a2c1cf09SLang Hames std::lock_guard<std::mutex> Lock(M); 856613f4afSLang Hames std::swap(DS, Dylibs); 86a2c1cf09SLang Hames } 87a2c1cf09SLang Hames 88a2c1cf09SLang Hames // There is no removal of dylibs at the moment, so nothing to do here. 89a2c1cf09SLang Hames return Error::success(); 90a2c1cf09SLang Hames } 91a2c1cf09SLang Hames 92a2c1cf09SLang Hames void SimpleExecutorDylibManager::addBootstrapSymbols( 93ef391df2SLang Hames StringMap<ExecutorAddr> &M) { 94ef391df2SLang Hames M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(this); 95a2c1cf09SLang Hames M[rt::SimpleExecutorDylibManagerOpenWrapperName] = 96ef391df2SLang Hames ExecutorAddr::fromPtr(&openWrapper); 97a2c1cf09SLang Hames M[rt::SimpleExecutorDylibManagerLookupWrapperName] = 98ef391df2SLang Hames ExecutorAddr::fromPtr(&lookupWrapper); 99a2c1cf09SLang Hames } 100a2c1cf09SLang Hames 101213666f8SLang Hames llvm::orc::shared::CWrapperFunctionResult 102a2c1cf09SLang Hames SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) { 103a2c1cf09SLang Hames return shared:: 104a2c1cf09SLang Hames WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle( 105a2c1cf09SLang Hames ArgData, ArgSize, 106a2c1cf09SLang Hames shared::makeMethodWrapperHandler( 107a2c1cf09SLang Hames &SimpleExecutorDylibManager::open)) 108a2c1cf09SLang Hames .release(); 109a2c1cf09SLang Hames } 110a2c1cf09SLang Hames 111213666f8SLang Hames llvm::orc::shared::CWrapperFunctionResult 112a2c1cf09SLang Hames SimpleExecutorDylibManager::lookupWrapper(const char *ArgData, size_t ArgSize) { 113a2c1cf09SLang Hames return shared:: 114a2c1cf09SLang Hames WrapperFunction<rt::SPSSimpleExecutorDylibManagerLookupSignature>::handle( 115a2c1cf09SLang Hames ArgData, ArgSize, 116a2c1cf09SLang Hames shared::makeMethodWrapperHandler( 117a2c1cf09SLang Hames &SimpleExecutorDylibManager::lookup)) 118a2c1cf09SLang Hames .release(); 119a2c1cf09SLang Hames } 120a2c1cf09SLang Hames 121a2c1cf09SLang Hames } // namespace rt_bootstrap 122a2c1cf09SLang Hames } // end namespace orc 123a2c1cf09SLang Hames } // end namespace llvm 124