SimpleExecutorDylibManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. Dylibs[NextId] = std::move(DL);
  30. return NextId++;
  31. }
  32. Expected<std::vector<ExecutorAddr>>
  33. SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,
  34. const RemoteSymbolLookupSet &L) {
  35. std::vector<ExecutorAddr> Result;
  36. std::lock_guard<std::mutex> Lock(M);
  37. auto I = Dylibs.find(H);
  38. if (I == Dylibs.end())
  39. return make_error<StringError>("No dylib for handle " + formatv("{0:x}", H),
  40. inconvertibleErrorCode());
  41. auto &DL = I->second;
  42. for (const auto &E : L) {
  43. if (E.Name.empty()) {
  44. if (E.Required)
  45. return make_error<StringError>("Required address for empty symbol \"\"",
  46. inconvertibleErrorCode());
  47. else
  48. Result.push_back(ExecutorAddr());
  49. } else {
  50. const char *DemangledSymName = E.Name.c_str();
  51. #ifdef __APPLE__
  52. if (E.Name.front() != '_')
  53. return make_error<StringError>(Twine("MachO symbol \"") + E.Name +
  54. "\" missing leading '_'",
  55. inconvertibleErrorCode());
  56. ++DemangledSymName;
  57. #endif
  58. void *Addr = DL.getAddressOfSymbol(DemangledSymName);
  59. if (!Addr && E.Required)
  60. return make_error<StringError>(Twine("Missing definition for ") +
  61. DemangledSymName,
  62. inconvertibleErrorCode());
  63. Result.push_back(ExecutorAddr::fromPtr(Addr));
  64. }
  65. }
  66. return Result;
  67. }
  68. Error SimpleExecutorDylibManager::shutdown() {
  69. DylibsMap DM;
  70. {
  71. std::lock_guard<std::mutex> Lock(M);
  72. std::swap(DM, Dylibs);
  73. }
  74. // There is no removal of dylibs at the moment, so nothing to do here.
  75. return Error::success();
  76. }
  77. void SimpleExecutorDylibManager::addBootstrapSymbols(
  78. StringMap<ExecutorAddr> &M) {
  79. M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(this);
  80. M[rt::SimpleExecutorDylibManagerOpenWrapperName] =
  81. ExecutorAddr::fromPtr(&openWrapper);
  82. M[rt::SimpleExecutorDylibManagerLookupWrapperName] =
  83. ExecutorAddr::fromPtr(&lookupWrapper);
  84. }
  85. llvm::orc::shared::CWrapperFunctionResult
  86. SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {
  87. return shared::
  88. WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(
  89. ArgData, ArgSize,
  90. shared::makeMethodWrapperHandler(
  91. &SimpleExecutorDylibManager::open))
  92. .release();
  93. }
  94. llvm::orc::shared::CWrapperFunctionResult
  95. SimpleExecutorDylibManager::lookupWrapper(const char *ArgData, size_t ArgSize) {
  96. return shared::
  97. WrapperFunction<rt::SPSSimpleExecutorDylibManagerLookupSignature>::handle(
  98. ArgData, ArgSize,
  99. shared::makeMethodWrapperHandler(
  100. &SimpleExecutorDylibManager::lookup))
  101. .release();
  102. }
  103. } // namespace rt_bootstrap
  104. } // end namespace orc
  105. } // end namespace llvm