1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
//===------- UnwindInfoManager.cpp - Register unwind info sections --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/TargetProcess/UnwindInfoManager.h"
#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
#include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
#ifdef __APPLE__
#include <dlfcn.h>
#endif // __APPLE__
#define DEBUG_TYPE "orc"
using namespace llvm;
using namespace llvm::orc;
using namespace llvm::orc::shared;
static orc::shared::CWrapperFunctionResult
llvm_orc_rt_alt_UnwindInfoManager_register(const char *Data, uint64_t Size) {
using SPSSig = SPSError(SPSSequence<SPSExecutorAddrRange>, SPSExecutorAddr,
SPSExecutorAddrRange, SPSExecutorAddrRange);
return WrapperFunction<SPSSig>::handle(
Data, Size,
[](std::vector<ExecutorAddrRange> CodeRanges, ExecutorAddr DSOBase,
ExecutorAddrRange DWARFRange,
ExecutorAddrRange CompactUnwindRange) {
return UnwindInfoManager::registerSections(
CodeRanges, DSOBase, DWARFRange, CompactUnwindRange);
})
.release();
}
static orc::shared::CWrapperFunctionResult
llvm_orc_rt_alt_UnwindInfoManager_deregister(const char *Data, uint64_t Size) {
using SPSSig = SPSError(SPSSequence<SPSExecutorAddrRange>);
return WrapperFunction<SPSSig>::handle(
Data, Size,
[](std::vector<ExecutorAddrRange> CodeRanges) {
return UnwindInfoManager::deregisterSections(CodeRanges);
})
.release();
}
namespace llvm::orc {
static const char *AddFnName = "__unw_add_find_dynamic_unwind_sections";
[[maybe_unused]] static const char *RemoveFnName = "__unw_remove_find_dynamic_unwind_sections";
static std::unique_ptr<UnwindInfoManager> Instance;
static int (*RemoveFindDynamicUnwindSections)(void *) = nullptr;
UnwindInfoManager::~UnwindInfoManager() {
if (int Err = RemoveFindDynamicUnwindSections((void *)&findSections)) {
LLVM_DEBUG({
dbgs() << "Failed call to " << RemoveFnName << ": error = " << Err
<< "\n";
});
}
}
bool UnwindInfoManager::TryEnable() {
#ifdef __APPLE__
static std::mutex M;
std::lock_guard<std::mutex> Lock(M);
if (Instance)
return true;
auto AddFn = (int (*)(void *))dlsym(RTLD_DEFAULT, AddFnName);
if (!AddFn)
return false;
auto RemoveFn = (int (*)(void *))dlsym(RTLD_DEFAULT, RemoveFnName);
if (!RemoveFn)
return false;
Instance.reset(new UnwindInfoManager());
if (auto Err = AddFn((void *)&findSections)) {
LLVM_DEBUG({
dbgs() << "Failed call to " << AddFnName << ": error = " << Err << "\n";
});
Instance = nullptr;
return false;
}
RemoveFindDynamicUnwindSections = RemoveFn;
return true;
#else
return false;
#endif // __APPLE__
}
void UnwindInfoManager::addBootstrapSymbols(StringMap<ExecutorAddr> &M) {
M[rt_alt::UnwindInfoManagerRegisterActionName] =
ExecutorAddr::fromPtr(llvm_orc_rt_alt_UnwindInfoManager_register);
M[rt_alt::UnwindInfoManagerDeregisterActionName] =
ExecutorAddr::fromPtr(llvm_orc_rt_alt_UnwindInfoManager_deregister);
}
Error UnwindInfoManager::registerSections(
ArrayRef<orc::ExecutorAddrRange> CodeRanges, orc::ExecutorAddr DSOBase,
orc::ExecutorAddrRange DWARFEHFrame, orc::ExecutorAddrRange CompactUnwind) {
return Instance->registerSectionsImpl(CodeRanges, DSOBase, DWARFEHFrame,
CompactUnwind);
}
Error UnwindInfoManager::deregisterSections(
ArrayRef<orc::ExecutorAddrRange> CodeRanges) {
return Instance->deregisterSectionsImpl(CodeRanges);
}
int UnwindInfoManager::findSectionsImpl(uintptr_t Addr, UnwindSections *Info) {
std::lock_guard<std::mutex> Lock(M);
auto I = UWSecs.upper_bound(Addr);
if (I == UWSecs.begin())
return 0;
--I;
*Info = I->second;
return 1;
}
int UnwindInfoManager::findSections(uintptr_t Addr, UnwindSections *Info) {
return Instance->findSectionsImpl(Addr, Info);
}
Error UnwindInfoManager::registerSectionsImpl(
ArrayRef<ExecutorAddrRange> CodeRanges, ExecutorAddr DSOBase,
ExecutorAddrRange DWARFEHFrame, ExecutorAddrRange CompactUnwind) {
std::lock_guard<std::mutex> Lock(M);
for (auto &R : CodeRanges)
UWSecs[R.Start.getValue()] =
UnwindSections{static_cast<uintptr_t>(DSOBase.getValue()),
static_cast<uintptr_t>(DWARFEHFrame.Start.getValue()),
static_cast<size_t>(DWARFEHFrame.size()),
static_cast<uintptr_t>(CompactUnwind.Start.getValue()),
static_cast<size_t>(CompactUnwind.size())};
return Error::success();
}
Error UnwindInfoManager::deregisterSectionsImpl(
ArrayRef<ExecutorAddrRange> CodeRanges) {
std::lock_guard<std::mutex> Lock(M);
for (auto &R : CodeRanges) {
auto I = UWSecs.find(R.Start.getValue());
if (I == UWSecs.end())
return make_error<StringError>(
"No unwind-info sections registered for range " +
formatv("{0:x} - {1:x}", R.Start, R.End),
inconvertibleErrorCode());
UWSecs.erase(I);
}
return Error::success();
}
} // namespace llvm::orc
|