LLVM 22.0.0git
UnwindInfoManager.cpp
Go to the documentation of this file.
1//===------- UnwindInfoManager.cpp - Register unwind info sections --------===//
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
12
13#ifdef __APPLE__
14#include <dlfcn.h>
15#endif // __APPLE__
16
17#define DEBUG_TYPE "orc"
18
19using namespace llvm;
20using namespace llvm::orc;
21using namespace llvm::orc::shared;
22
25 size_t ArgSize) {
28
30 ArgData, ArgSize,
31 [](std::vector<ExecutorAddrRange> CodeRanges, ExecutorAddr DSOBase,
32 ExecutorAddrRange DWARFRange,
33 ExecutorAddrRange CompactUnwindRange) {
35 CodeRanges, DSOBase, DWARFRange, CompactUnwindRange);
36 })
37 .release();
38}
39
42 size_t ArgSize) {
43 using SPSSig = SPSError(SPSSequence<SPSExecutorAddrRange>);
44
46 ArgData, ArgSize,
47 [](std::vector<ExecutorAddrRange> CodeRanges) {
49 })
50 .release();
51}
52
53namespace llvm::orc {
54
55[[maybe_unused]] static const char *AddFnName =
56 "__unw_add_find_dynamic_unwind_sections";
57[[maybe_unused]] static const char *RemoveFnName =
58 "__unw_remove_find_dynamic_unwind_sections";
59static std::unique_ptr<UnwindInfoManager> Instance;
60static int (*RemoveFindDynamicUnwindSections)(void *) = nullptr;
61
63 if (int Err = RemoveFindDynamicUnwindSections((void *)&findSections)) {
64 (void)Err; // Silence unused variable warning in release builds.
66 dbgs() << "Failed call to " << RemoveFnName << ": error = " << Err
67 << "\n";
68 });
69 (void)Err;
70 }
71}
72
74#ifdef __APPLE__
75 static std::mutex M;
76 std::lock_guard<std::mutex> Lock(M);
77
78 if (Instance)
79 return true;
80
81 auto AddFn = (int (*)(void *))dlsym(RTLD_DEFAULT, AddFnName);
82 if (!AddFn)
83 return false;
84
85 auto RemoveFn = (int (*)(void *))dlsym(RTLD_DEFAULT, RemoveFnName);
86 if (!RemoveFn)
87 return false;
88
89 Instance.reset(new UnwindInfoManager());
90
91 if (auto Err = AddFn((void *)&findSections)) {
92 (void)Err; // Silence unused variable warning in release builds.
94 dbgs() << "Failed call to " << AddFnName << ": error = " << Err << "\n";
95 });
96 Instance = nullptr;
97 return false;
98 }
99
101 return true;
102
103#else
104 return false;
105#endif // __APPLE__
106}
107
113}
114
117 orc::ExecutorAddrRange DWARFEHFrame, orc::ExecutorAddrRange CompactUnwind) {
118 return Instance->registerSectionsImpl(CodeRanges, DSOBase, DWARFEHFrame,
119 CompactUnwind);
120}
121
124 return Instance->deregisterSectionsImpl(CodeRanges);
125}
126
127int UnwindInfoManager::findSectionsImpl(uintptr_t Addr, UnwindSections *Info) {
128 std::lock_guard<std::mutex> Lock(M);
129 auto I = UWSecs.upper_bound(Addr);
130 if (I == UWSecs.begin())
131 return 0;
132 --I;
133 *Info = I->second;
134 return 1;
135}
136
137int UnwindInfoManager::findSections(uintptr_t Addr, UnwindSections *Info) {
138 return Instance->findSectionsImpl(Addr, Info);
139}
140
141Error UnwindInfoManager::registerSectionsImpl(
142 ArrayRef<ExecutorAddrRange> CodeRanges, ExecutorAddr DSOBase,
143 ExecutorAddrRange DWARFEHFrame, ExecutorAddrRange CompactUnwind) {
144 std::lock_guard<std::mutex> Lock(M);
145 for (auto &R : CodeRanges)
146 UWSecs[R.Start.getValue()] =
147 UnwindSections{static_cast<uintptr_t>(DSOBase.getValue()),
148 static_cast<uintptr_t>(DWARFEHFrame.Start.getValue()),
149 static_cast<size_t>(DWARFEHFrame.size()),
150 static_cast<uintptr_t>(CompactUnwind.Start.getValue()),
151 static_cast<size_t>(CompactUnwind.size())};
152 return Error::success();
153}
154
155Error UnwindInfoManager::deregisterSectionsImpl(
156 ArrayRef<ExecutorAddrRange> CodeRanges) {
157 std::lock_guard<std::mutex> Lock(M);
158 for (auto &R : CodeRanges) {
159 auto I = UWSecs.find(R.Start.getValue());
160 if (I == UWSecs.end())
161 return make_error<StringError>(
162 "No unwind-info sections registered for range " +
163 formatv("{0:x} - {1:x}", R.Start, R.End),
165 UWSecs.erase(I);
166 }
167 return Error::success();
168}
169
170} // namespace llvm::orc
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
uint64_t Addr
#define I(x, y, z)
Definition: MD5.cpp:58
#define LLVM_DEBUG(...)
Definition: Debug.h:119
static orc::shared::CWrapperFunctionResult llvm_orc_rt_alt_UnwindInfoManager_register(const char *ArgData, size_t ArgSize)
static orc::shared::CWrapperFunctionResult llvm_orc_rt_alt_UnwindInfoManager_deregister(const char *ArgData, size_t ArgSize)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
Represents an address in the executor process.
uint64_t getValue() const
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
static LLVM_ABI bool TryEnable()
If the libunwind find-dynamic-unwind-info callback registration APIs are available then this method w...
static LLVM_ABI void addBootstrapSymbols(StringMap< ExecutorAddr > &M)
static LLVM_ABI Error deregisterSections(ArrayRef< orc::ExecutorAddrRange > CodeRanges)
static LLVM_ABI Error registerSections(ArrayRef< orc::ExecutorAddrRange > CodeRanges, orc::ExecutorAddr DSOBase, orc::ExecutorAddrRange DWARFEHFrame, orc::ExecutorAddrRange CompactUnwind)
LLVM_ABI const char * UnwindInfoManagerDeregisterActionName
Definition: OrcRTBridge.cpp:85
LLVM_ABI const char * UnwindInfoManagerRegisterActionName
Definition: OrcRTBridge.cpp:83
static std::unique_ptr< UnwindInfoManager > Instance
static const char * RemoveFnName
static int(* RemoveFindDynamicUnwindSections)(void *)
static const char * AddFnName
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
Represents an address range in the exceutor process.
ExecutorAddrDiff size() const