SimpleExecutorDylibManager.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h"
  9. #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
  10. #include "llvm/Support/FormatVariadic.h"
  11. #define DEBUG_TYPE "orc"
  12. namespace llvm {
  13. namespace orc {
  14. namespace rt_bootstrap {
  15. SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {
  16. assert(Dylibs.empty() && "shutdown not called?");
  17. }
  18. Expected<tpctypes::DylibHandle>
  19. SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {
  20. if (Mode != 0)
  21. return make_error<StringError>("open: non-zero mode bits not yet supported",
  22. inconvertibleErrorCode());
  23. const char *PathCStr = Path.empty() ? nullptr : Path.c_str();
  24. std::string ErrMsg;
  25. auto DL = sys::DynamicLibrary::getPermanentLibrary(PathCStr, &ErrMsg);
  26. if (!DL.isValid())
  27. return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
  28. std::lock_guard<std::mutex> Lock(M);
  29. auto H = ExecutorAddr::fromPtr(DL.getOSSpecificHandle());
  30. Dylibs.insert(DL.getOSSpecificHandle());
  31. return H;
  32. }
  33. Expected<std::vector<ExecutorAddr>>
  34. SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,
  35. const RemoteSymbolLookupSet &L) {
  36. std::vector<ExecutorAddr> Result;
  37. auto DL = sys::DynamicLibrary(H.toPtr<void *>());
  38. for (const auto &E : L) {
  39. if (E.Name.empty()) {
  40. if (E.Required)
  41. return make_error<StringError>("Required address for empty symbol \"\"",
  42. inconvertibleErrorCode());
  43. else
  44. Result.push_back(ExecutorAddr());
  45. } else {
  46. const char *DemangledSymName = E.Name.c_str();
  47. #ifdef __APPLE__
  48. if (E.Name.front() != '_')
  49. return make_error<StringError>(Twine("MachO symbol \"") + E.Name +
  50. "\" missing leading '_'",
  51. inconvertibleErrorCode());
  52. ++DemangledSymName;
  53. #endif
  54. void *Addr = DL.getAddressOfSymbol(DemangledSymName);
  55. if (!Addr && E.Required)
  56. return make_error<StringError>(Twine("Missing definition for ") +
  57. DemangledSymName,
  58. inconvertibleErrorCode());
  59. Result.push_back(ExecutorAddr::fromPtr(Addr));
  60. }
  61. }
  62. return Result;
  63. }
  64. Error SimpleExecutorDylibManager::shutdown() {
  65. DylibSet DS;
  66. {
  67. std::lock_guard<std::mutex> Lock(M);
  68. std::swap(DS, Dylibs);
  69. }
  70. // There is no removal of dylibs at the moment, so nothing to do here.
  71. return Error::success();
  72. }
  73. void SimpleExecutorDylibManager::addBootstrapSymbols(
  74. StringMap<ExecutorAddr> &M) {
  75. M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(this);
  76. M[rt::SimpleExecutorDylibManagerOpenWrapperName] =
  77. ExecutorAddr::fromPtr(&openWrapper);
  78. M[rt::SimpleExecutorDylibManagerLookupWrapperName] =
  79. ExecutorAddr::fromPtr(&lookupWrapper);
  80. }
  81. llvm::orc::shared::CWrapperFunctionResult
  82. SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {
  83. return shared::
  84. WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(
  85. ArgData, ArgSize,
  86. shared::makeMethodWrapperHandler(
  87. &SimpleExecutorDylibManager::open))
  88. .release();
  89. }
  90. llvm::orc::shared::CWrapperFunctionResult
  91. SimpleExecutorDylibManager::lookupWrapper(const char *ArgData, size_t ArgSize) {
  92. return shared::
  93. WrapperFunction<rt::SPSSimpleExecutorDylibManagerLookupSignature>::handle(
  94. ArgData, ArgSize,
  95. shared::makeMethodWrapperHandler(
  96. &SimpleExecutorDylibManager::lookup))
  97. .release();
  98. }
  99. } // namespace rt_bootstrap
  100. } // end namespace orc
  101. } // end namespace llvm