[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / ExecutionEngine / Orc / TargetProcess / SimpleExecutorDylibManager.cpp
blob3c9dd21b083201ff0d01aafdb009fa98455850fe
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorDylibManager.h"
11 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
12 #include "llvm/Support/FormatVariadic.h"
14 #define DEBUG_TYPE "orc"
16 namespace llvm {
17 namespace orc {
18 namespace rt_bootstrap {
20 SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {
21 assert(Dylibs.empty() && "shutdown not called?");
24 Expected<tpctypes::DylibHandle>
25 SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {
26 if (Mode != 0)
27 return make_error<StringError>("open: non-zero mode bits not yet supported",
28 inconvertibleErrorCode());
30 const char *PathCStr = Path.empty() ? nullptr : Path.c_str();
31 std::string ErrMsg;
33 auto DL = sys::DynamicLibrary::getPermanentLibrary(PathCStr, &ErrMsg);
34 if (!DL.isValid())
35 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
37 std::lock_guard<std::mutex> Lock(M);
38 Dylibs[NextId] = std::move(DL);
39 return NextId++;
42 Expected<std::vector<ExecutorAddr>>
43 SimpleExecutorDylibManager::lookup(tpctypes::DylibHandle H,
44 const RemoteSymbolLookupSet &L) {
45 std::vector<ExecutorAddr> Result;
47 std::lock_guard<std::mutex> Lock(M);
48 auto I = Dylibs.find(H);
49 if (I == Dylibs.end())
50 return make_error<StringError>("No dylib for handle " + formatv("{0:x}", H),
51 inconvertibleErrorCode());
52 auto &DL = I->second;
54 for (const auto &E : L) {
56 if (E.Name.empty()) {
57 if (E.Required)
58 return make_error<StringError>("Required address for empty symbol \"\"",
59 inconvertibleErrorCode());
60 else
61 Result.push_back(ExecutorAddr());
62 } else {
64 const char *DemangledSymName = E.Name.c_str();
65 #ifdef __APPLE__
66 if (E.Name.front() != '_')
67 return make_error<StringError>(Twine("MachO symbol \"") + E.Name +
68 "\" missing leading '_'",
69 inconvertibleErrorCode());
70 ++DemangledSymName;
71 #endif
73 void *Addr = DL.getAddressOfSymbol(DemangledSymName);
74 if (!Addr && E.Required)
75 return make_error<StringError>(Twine("Missing definition for ") +
76 DemangledSymName,
77 inconvertibleErrorCode());
79 Result.push_back(ExecutorAddr::fromPtr(Addr));
83 return Result;
86 Error SimpleExecutorDylibManager::shutdown() {
88 DylibsMap DM;
90 std::lock_guard<std::mutex> Lock(M);
91 std::swap(DM, Dylibs);
94 // There is no removal of dylibs at the moment, so nothing to do here.
95 return Error::success();
98 void SimpleExecutorDylibManager::addBootstrapSymbols(
99 StringMap<ExecutorAddr> &M) {
100 M[rt::SimpleExecutorDylibManagerInstanceName] = ExecutorAddr::fromPtr(this);
101 M[rt::SimpleExecutorDylibManagerOpenWrapperName] =
102 ExecutorAddr::fromPtr(&openWrapper);
103 M[rt::SimpleExecutorDylibManagerLookupWrapperName] =
104 ExecutorAddr::fromPtr(&lookupWrapper);
107 llvm::orc::shared::CWrapperFunctionResult
108 SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {
109 return shared::
110 WrapperFunction<rt::SPSSimpleExecutorDylibManagerOpenSignature>::handle(
111 ArgData, ArgSize,
112 shared::makeMethodWrapperHandler(
113 &SimpleExecutorDylibManager::open))
114 .release();
117 llvm::orc::shared::CWrapperFunctionResult
118 SimpleExecutorDylibManager::lookupWrapper(const char *ArgData, size_t ArgSize) {
119 return shared::
120 WrapperFunction<rt::SPSSimpleExecutorDylibManagerLookupSignature>::handle(
121 ArgData, ArgSize,
122 shared::makeMethodWrapperHandler(
123 &SimpleExecutorDylibManager::lookup))
124 .release();
127 } // namespace rt_bootstrap
128 } // end namespace orc
129 } // end namespace llvm