Louis Dionne | 7f068e5 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 8 | // C++ interface to lower levels of libunwind |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef __UNWINDCURSOR_HPP__ |
| 12 | #define __UNWINDCURSOR_HPP__ |
| 13 | |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 14 | #include "cet_unwind.h" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 18 | #include <unwind.h> |
| 19 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 20 | #ifdef _WIN32 |
| 21 | #include <windows.h> |
| 22 | #include <ntverp.h> |
| 23 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 24 | #ifdef __APPLE__ |
| 25 | #include <mach-o/dyld.h> |
| 26 | #endif |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 27 | #ifdef _AIX |
| 28 | #include <dlfcn.h> |
| 29 | #include <sys/debug.h> |
| 30 | #include <sys/pseg.h> |
| 31 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 32 | |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 33 | #if defined(_LIBUNWIND_TARGET_LINUX) && \ |
| 34 | (defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_S390X)) |
Shoaib Meenai | 8c606b0 | 2022-05-25 21:39:12 -0700 | [diff] [blame] | 35 | #include <sys/syscall.h> |
| 36 | #include <sys/uio.h> |
| 37 | #include <unistd.h> |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 38 | #define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1 |
| 39 | #endif |
| 40 | |
Ryan Prichard | 60a480e | 2022-09-07 17:27:57 -0400 | [diff] [blame^] | 41 | #include "AddressSpace.hpp" |
| 42 | #include "CompactUnwinder.hpp" |
| 43 | #include "config.h" |
| 44 | #include "DwarfInstructions.hpp" |
| 45 | #include "EHHeaderParser.hpp" |
| 46 | #include "libunwind.h" |
| 47 | #include "libunwind_ext.h" |
| 48 | #include "Registers.hpp" |
| 49 | #include "RWMutex.hpp" |
| 50 | #include "Unwind-EHABI.h" |
| 51 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 52 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 53 | // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and |
| 54 | // earlier) SDKs. |
| 55 | // MinGW-w64 has always provided this struct. |
| 56 | #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \ |
| 57 | !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000 |
| 58 | struct _DISPATCHER_CONTEXT { |
| 59 | ULONG64 ControlPc; |
| 60 | ULONG64 ImageBase; |
| 61 | PRUNTIME_FUNCTION FunctionEntry; |
| 62 | ULONG64 EstablisherFrame; |
| 63 | ULONG64 TargetIp; |
| 64 | PCONTEXT ContextRecord; |
| 65 | PEXCEPTION_ROUTINE LanguageHandler; |
| 66 | PVOID HandlerData; |
| 67 | PUNWIND_HISTORY_TABLE HistoryTable; |
| 68 | ULONG ScopeIndex; |
| 69 | ULONG Fill0; |
| 70 | }; |
| 71 | #endif |
| 72 | |
| 73 | struct UNWIND_INFO { |
| 74 | uint8_t Version : 3; |
| 75 | uint8_t Flags : 5; |
| 76 | uint8_t SizeOfProlog; |
| 77 | uint8_t CountOfCodes; |
| 78 | uint8_t FrameRegister : 4; |
| 79 | uint8_t FrameOffset : 4; |
| 80 | uint16_t UnwindCodes[2]; |
| 81 | }; |
| 82 | |
| 83 | extern "C" _Unwind_Reason_Code __libunwind_seh_personality( |
| 84 | int, _Unwind_Action, uint64_t, _Unwind_Exception *, |
| 85 | struct _Unwind_Context *); |
| 86 | |
| 87 | #endif |
| 88 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 89 | namespace libunwind { |
| 90 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 91 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 92 | /// Cache of recently found FDEs. |
| 93 | template <typename A> |
| 94 | class _LIBUNWIND_HIDDEN DwarfFDECache { |
| 95 | typedef typename A::pint_t pint_t; |
| 96 | public: |
Ryan Prichard | 2cfcb8c | 2020-09-09 15:43:35 -0700 | [diff] [blame] | 97 | static constexpr pint_t kSearchAll = static_cast<pint_t>(-1); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 98 | static pint_t findFDE(pint_t mh, pint_t pc); |
| 99 | static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde); |
| 100 | static void removeAllIn(pint_t mh); |
| 101 | static void iterateCacheEntries(void (*func)(unw_word_t ip_start, |
| 102 | unw_word_t ip_end, |
| 103 | unw_word_t fde, unw_word_t mh)); |
| 104 | |
| 105 | private: |
| 106 | |
| 107 | struct entry { |
| 108 | pint_t mh; |
| 109 | pint_t ip_start; |
| 110 | pint_t ip_end; |
| 111 | pint_t fde; |
| 112 | }; |
| 113 | |
| 114 | // These fields are all static to avoid needing an initializer. |
| 115 | // There is only one instance of this class per process. |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 116 | static RWMutex _lock; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 117 | #ifdef __APPLE__ |
| 118 | static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide); |
| 119 | static bool _registeredForDyldUnloads; |
| 120 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 121 | static entry *_buffer; |
| 122 | static entry *_bufferUsed; |
| 123 | static entry *_bufferEnd; |
| 124 | static entry _initialBuffer[64]; |
| 125 | }; |
| 126 | |
| 127 | template <typename A> |
| 128 | typename DwarfFDECache<A>::entry * |
| 129 | DwarfFDECache<A>::_buffer = _initialBuffer; |
| 130 | |
| 131 | template <typename A> |
| 132 | typename DwarfFDECache<A>::entry * |
| 133 | DwarfFDECache<A>::_bufferUsed = _initialBuffer; |
| 134 | |
| 135 | template <typename A> |
| 136 | typename DwarfFDECache<A>::entry * |
| 137 | DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64]; |
| 138 | |
| 139 | template <typename A> |
| 140 | typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64]; |
| 141 | |
| 142 | template <typename A> |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 143 | RWMutex DwarfFDECache<A>::_lock; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 144 | |
| 145 | #ifdef __APPLE__ |
| 146 | template <typename A> |
| 147 | bool DwarfFDECache<A>::_registeredForDyldUnloads = false; |
| 148 | #endif |
| 149 | |
| 150 | template <typename A> |
| 151 | typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) { |
| 152 | pint_t result = 0; |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 153 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 154 | for (entry *p = _buffer; p < _bufferUsed; ++p) { |
Ryan Prichard | 2cfcb8c | 2020-09-09 15:43:35 -0700 | [diff] [blame] | 155 | if ((mh == p->mh) || (mh == kSearchAll)) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 156 | if ((p->ip_start <= pc) && (pc < p->ip_end)) { |
| 157 | result = p->fde; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 162 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 163 | return result; |
| 164 | } |
| 165 | |
| 166 | template <typename A> |
| 167 | void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end, |
| 168 | pint_t fde) { |
Peter Zotov | 0717a2e | 2015-11-09 06:57:29 +0000 | [diff] [blame] | 169 | #if !defined(_LIBUNWIND_NO_HEAP) |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 170 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 171 | if (_bufferUsed >= _bufferEnd) { |
| 172 | size_t oldSize = (size_t)(_bufferEnd - _buffer); |
| 173 | size_t newSize = oldSize * 4; |
| 174 | // Can't use operator new (we are below it). |
| 175 | entry *newBuffer = (entry *)malloc(newSize * sizeof(entry)); |
| 176 | memcpy(newBuffer, _buffer, oldSize * sizeof(entry)); |
| 177 | if (_buffer != _initialBuffer) |
| 178 | free(_buffer); |
| 179 | _buffer = newBuffer; |
| 180 | _bufferUsed = &newBuffer[oldSize]; |
| 181 | _bufferEnd = &newBuffer[newSize]; |
| 182 | } |
| 183 | _bufferUsed->mh = mh; |
| 184 | _bufferUsed->ip_start = ip_start; |
| 185 | _bufferUsed->ip_end = ip_end; |
| 186 | _bufferUsed->fde = fde; |
| 187 | ++_bufferUsed; |
| 188 | #ifdef __APPLE__ |
| 189 | if (!_registeredForDyldUnloads) { |
| 190 | _dyld_register_func_for_remove_image(&dyldUnloadHook); |
| 191 | _registeredForDyldUnloads = true; |
| 192 | } |
| 193 | #endif |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 194 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Peter Zotov | 0717a2e | 2015-11-09 06:57:29 +0000 | [diff] [blame] | 195 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template <typename A> |
| 199 | void DwarfFDECache<A>::removeAllIn(pint_t mh) { |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 200 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 201 | entry *d = _buffer; |
| 202 | for (const entry *s = _buffer; s < _bufferUsed; ++s) { |
| 203 | if (s->mh != mh) { |
| 204 | if (d != s) |
| 205 | *d = *s; |
| 206 | ++d; |
| 207 | } |
| 208 | } |
| 209 | _bufferUsed = d; |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 210 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #ifdef __APPLE__ |
| 214 | template <typename A> |
| 215 | void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) { |
| 216 | removeAllIn((pint_t) mh); |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | template <typename A> |
| 221 | void DwarfFDECache<A>::iterateCacheEntries(void (*func)( |
| 222 | unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) { |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 223 | _LIBUNWIND_LOG_IF_FALSE(_lock.lock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 224 | for (entry *p = _buffer; p < _bufferUsed; ++p) { |
| 225 | (*func)(p->ip_start, p->ip_end, p->fde, p->mh); |
| 226 | } |
Martin Storsjo | 590ffef | 2017-10-23 19:29:36 +0000 | [diff] [blame] | 227 | _LIBUNWIND_LOG_IF_FALSE(_lock.unlock()); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 228 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 229 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 230 | |
| 231 | |
| 232 | #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field)) |
| 233 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 234 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 235 | template <typename A> class UnwindSectionHeader { |
| 236 | public: |
| 237 | UnwindSectionHeader(A &addressSpace, typename A::pint_t addr) |
| 238 | : _addressSpace(addressSpace), _addr(addr) {} |
| 239 | |
| 240 | uint32_t version() const { |
| 241 | return _addressSpace.get32(_addr + |
| 242 | offsetof(unwind_info_section_header, version)); |
| 243 | } |
| 244 | uint32_t commonEncodingsArraySectionOffset() const { |
| 245 | return _addressSpace.get32(_addr + |
| 246 | offsetof(unwind_info_section_header, |
| 247 | commonEncodingsArraySectionOffset)); |
| 248 | } |
| 249 | uint32_t commonEncodingsArrayCount() const { |
| 250 | return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, |
| 251 | commonEncodingsArrayCount)); |
| 252 | } |
| 253 | uint32_t personalityArraySectionOffset() const { |
| 254 | return _addressSpace.get32(_addr + offsetof(unwind_info_section_header, |
| 255 | personalityArraySectionOffset)); |
| 256 | } |
| 257 | uint32_t personalityArrayCount() const { |
| 258 | return _addressSpace.get32( |
| 259 | _addr + offsetof(unwind_info_section_header, personalityArrayCount)); |
| 260 | } |
| 261 | uint32_t indexSectionOffset() const { |
| 262 | return _addressSpace.get32( |
| 263 | _addr + offsetof(unwind_info_section_header, indexSectionOffset)); |
| 264 | } |
| 265 | uint32_t indexCount() const { |
| 266 | return _addressSpace.get32( |
| 267 | _addr + offsetof(unwind_info_section_header, indexCount)); |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | A &_addressSpace; |
| 272 | typename A::pint_t _addr; |
| 273 | }; |
| 274 | |
| 275 | template <typename A> class UnwindSectionIndexArray { |
| 276 | public: |
| 277 | UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr) |
| 278 | : _addressSpace(addressSpace), _addr(addr) {} |
| 279 | |
| 280 | uint32_t functionOffset(uint32_t index) const { |
| 281 | return _addressSpace.get32( |
| 282 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 283 | functionOffset)); |
| 284 | } |
| 285 | uint32_t secondLevelPagesSectionOffset(uint32_t index) const { |
| 286 | return _addressSpace.get32( |
| 287 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 288 | secondLevelPagesSectionOffset)); |
| 289 | } |
| 290 | uint32_t lsdaIndexArraySectionOffset(uint32_t index) const { |
| 291 | return _addressSpace.get32( |
| 292 | _addr + arrayoffsetof(unwind_info_section_header_index_entry, index, |
| 293 | lsdaIndexArraySectionOffset)); |
| 294 | } |
| 295 | |
| 296 | private: |
| 297 | A &_addressSpace; |
| 298 | typename A::pint_t _addr; |
| 299 | }; |
| 300 | |
| 301 | template <typename A> class UnwindSectionRegularPageHeader { |
| 302 | public: |
| 303 | UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr) |
| 304 | : _addressSpace(addressSpace), _addr(addr) {} |
| 305 | |
| 306 | uint32_t kind() const { |
| 307 | return _addressSpace.get32( |
| 308 | _addr + offsetof(unwind_info_regular_second_level_page_header, kind)); |
| 309 | } |
| 310 | uint16_t entryPageOffset() const { |
| 311 | return _addressSpace.get16( |
| 312 | _addr + offsetof(unwind_info_regular_second_level_page_header, |
| 313 | entryPageOffset)); |
| 314 | } |
| 315 | uint16_t entryCount() const { |
| 316 | return _addressSpace.get16( |
| 317 | _addr + |
| 318 | offsetof(unwind_info_regular_second_level_page_header, entryCount)); |
| 319 | } |
| 320 | |
| 321 | private: |
| 322 | A &_addressSpace; |
| 323 | typename A::pint_t _addr; |
| 324 | }; |
| 325 | |
| 326 | template <typename A> class UnwindSectionRegularArray { |
| 327 | public: |
| 328 | UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr) |
| 329 | : _addressSpace(addressSpace), _addr(addr) {} |
| 330 | |
| 331 | uint32_t functionOffset(uint32_t index) const { |
| 332 | return _addressSpace.get32( |
| 333 | _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index, |
| 334 | functionOffset)); |
| 335 | } |
| 336 | uint32_t encoding(uint32_t index) const { |
| 337 | return _addressSpace.get32( |
| 338 | _addr + |
| 339 | arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding)); |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | A &_addressSpace; |
| 344 | typename A::pint_t _addr; |
| 345 | }; |
| 346 | |
| 347 | template <typename A> class UnwindSectionCompressedPageHeader { |
| 348 | public: |
| 349 | UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr) |
| 350 | : _addressSpace(addressSpace), _addr(addr) {} |
| 351 | |
| 352 | uint32_t kind() const { |
| 353 | return _addressSpace.get32( |
| 354 | _addr + |
| 355 | offsetof(unwind_info_compressed_second_level_page_header, kind)); |
| 356 | } |
| 357 | uint16_t entryPageOffset() const { |
| 358 | return _addressSpace.get16( |
| 359 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 360 | entryPageOffset)); |
| 361 | } |
| 362 | uint16_t entryCount() const { |
| 363 | return _addressSpace.get16( |
| 364 | _addr + |
| 365 | offsetof(unwind_info_compressed_second_level_page_header, entryCount)); |
| 366 | } |
| 367 | uint16_t encodingsPageOffset() const { |
| 368 | return _addressSpace.get16( |
| 369 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 370 | encodingsPageOffset)); |
| 371 | } |
| 372 | uint16_t encodingsCount() const { |
| 373 | return _addressSpace.get16( |
| 374 | _addr + offsetof(unwind_info_compressed_second_level_page_header, |
| 375 | encodingsCount)); |
| 376 | } |
| 377 | |
| 378 | private: |
| 379 | A &_addressSpace; |
| 380 | typename A::pint_t _addr; |
| 381 | }; |
| 382 | |
| 383 | template <typename A> class UnwindSectionCompressedArray { |
| 384 | public: |
| 385 | UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr) |
| 386 | : _addressSpace(addressSpace), _addr(addr) {} |
| 387 | |
| 388 | uint32_t functionOffset(uint32_t index) const { |
| 389 | return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET( |
| 390 | _addressSpace.get32(_addr + index * sizeof(uint32_t))); |
| 391 | } |
| 392 | uint16_t encodingIndex(uint32_t index) const { |
| 393 | return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX( |
| 394 | _addressSpace.get32(_addr + index * sizeof(uint32_t))); |
| 395 | } |
| 396 | |
| 397 | private: |
| 398 | A &_addressSpace; |
| 399 | typename A::pint_t _addr; |
| 400 | }; |
| 401 | |
| 402 | template <typename A> class UnwindSectionLsdaArray { |
| 403 | public: |
| 404 | UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr) |
| 405 | : _addressSpace(addressSpace), _addr(addr) {} |
| 406 | |
| 407 | uint32_t functionOffset(uint32_t index) const { |
| 408 | return _addressSpace.get32( |
| 409 | _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, |
| 410 | index, functionOffset)); |
| 411 | } |
| 412 | uint32_t lsdaOffset(uint32_t index) const { |
| 413 | return _addressSpace.get32( |
| 414 | _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry, |
| 415 | index, lsdaOffset)); |
| 416 | } |
| 417 | |
| 418 | private: |
| 419 | A &_addressSpace; |
| 420 | typename A::pint_t _addr; |
| 421 | }; |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 422 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 423 | |
| 424 | class _LIBUNWIND_HIDDEN AbstractUnwindCursor { |
| 425 | public: |
| 426 | // NOTE: provide a class specific placement deallocation function (S5.3.4 p20) |
| 427 | // This avoids an unnecessary dependency to libc++abi. |
| 428 | void operator delete(void *, size_t) {} |
| 429 | |
| 430 | virtual ~AbstractUnwindCursor() {} |
| 431 | virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); } |
| 432 | virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); } |
| 433 | virtual void setReg(int, unw_word_t) { |
| 434 | _LIBUNWIND_ABORT("setReg not implemented"); |
| 435 | } |
| 436 | virtual bool validFloatReg(int) { |
| 437 | _LIBUNWIND_ABORT("validFloatReg not implemented"); |
| 438 | } |
| 439 | virtual unw_fpreg_t getFloatReg(int) { |
| 440 | _LIBUNWIND_ABORT("getFloatReg not implemented"); |
| 441 | } |
| 442 | virtual void setFloatReg(int, unw_fpreg_t) { |
| 443 | _LIBUNWIND_ABORT("setFloatReg not implemented"); |
| 444 | } |
| 445 | virtual int step() { _LIBUNWIND_ABORT("step not implemented"); } |
| 446 | virtual void getInfo(unw_proc_info_t *) { |
| 447 | _LIBUNWIND_ABORT("getInfo not implemented"); |
| 448 | } |
| 449 | virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); } |
| 450 | virtual bool isSignalFrame() { |
| 451 | _LIBUNWIND_ABORT("isSignalFrame not implemented"); |
| 452 | } |
| 453 | virtual bool getFunctionName(char *, size_t, unw_word_t *) { |
| 454 | _LIBUNWIND_ABORT("getFunctionName not implemented"); |
| 455 | } |
| 456 | virtual void setInfoBasedOnIPRegister(bool = false) { |
| 457 | _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented"); |
| 458 | } |
| 459 | virtual const char *getRegisterName(int) { |
| 460 | _LIBUNWIND_ABORT("getRegisterName not implemented"); |
| 461 | } |
| 462 | #ifdef __arm__ |
| 463 | virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); } |
| 464 | #endif |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 465 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 466 | #ifdef _AIX |
| 467 | virtual uintptr_t getDataRelBase() { |
| 468 | _LIBUNWIND_ABORT("getDataRelBase not implemented"); |
| 469 | } |
| 470 | #endif |
| 471 | |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 472 | #if defined(_LIBUNWIND_USE_CET) |
| 473 | virtual void *get_registers() { |
| 474 | _LIBUNWIND_ABORT("get_registers not implemented"); |
| 475 | } |
| 476 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 479 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32) |
| 480 | |
| 481 | /// \c UnwindCursor contains all state (including all register values) during |
| 482 | /// an unwind. This is normally stack-allocated inside a unw_cursor_t. |
| 483 | template <typename A, typename R> |
| 484 | class UnwindCursor : public AbstractUnwindCursor { |
| 485 | typedef typename A::pint_t pint_t; |
| 486 | public: |
| 487 | UnwindCursor(unw_context_t *context, A &as); |
| 488 | UnwindCursor(CONTEXT *context, A &as); |
| 489 | UnwindCursor(A &as, void *threadArg); |
| 490 | virtual ~UnwindCursor() {} |
| 491 | virtual bool validReg(int); |
| 492 | virtual unw_word_t getReg(int); |
| 493 | virtual void setReg(int, unw_word_t); |
| 494 | virtual bool validFloatReg(int); |
| 495 | virtual unw_fpreg_t getFloatReg(int); |
| 496 | virtual void setFloatReg(int, unw_fpreg_t); |
| 497 | virtual int step(); |
| 498 | virtual void getInfo(unw_proc_info_t *); |
| 499 | virtual void jumpto(); |
| 500 | virtual bool isSignalFrame(); |
| 501 | virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); |
| 502 | virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); |
| 503 | virtual const char *getRegisterName(int num); |
| 504 | #ifdef __arm__ |
| 505 | virtual void saveVFPAsX(); |
| 506 | #endif |
| 507 | |
| 508 | DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; } |
| 509 | void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; } |
| 510 | |
Martin Storsjo | 5f5036e | 2019-02-03 22:16:53 +0000 | [diff] [blame] | 511 | // libunwind does not and should not depend on C++ library which means that we |
Gabriel Ravier | 42aa6de | 2022-08-20 18:09:03 -0700 | [diff] [blame] | 512 | // need our own definition of inline placement new. |
Martin Storsjo | 5f5036e | 2019-02-03 22:16:53 +0000 | [diff] [blame] | 513 | static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } |
| 514 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 515 | private: |
| 516 | |
| 517 | pint_t getLastPC() const { return _dispContext.ControlPc; } |
| 518 | void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; } |
| 519 | RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { |
Martin Storsjö | 1644d07 | 2022-05-11 11:20:38 +0300 | [diff] [blame] | 520 | #ifdef __arm__ |
| 521 | // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit. |
| 522 | pc &= ~1U; |
| 523 | #endif |
| 524 | // If pc points exactly at the end of the range, we might resolve the |
| 525 | // next function instead. Decrement pc by 1 to fit inside the current |
| 526 | // function. |
| 527 | pc -= 1; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 528 | _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc, |
| 529 | &_dispContext.ImageBase, |
| 530 | _dispContext.HistoryTable); |
| 531 | *base = _dispContext.ImageBase; |
| 532 | return _dispContext.FunctionEntry; |
| 533 | } |
| 534 | bool getInfoFromSEH(pint_t pc); |
| 535 | int stepWithSEHData() { |
| 536 | _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER, |
| 537 | _dispContext.ImageBase, |
| 538 | _dispContext.ControlPc, |
| 539 | _dispContext.FunctionEntry, |
| 540 | _dispContext.ContextRecord, |
| 541 | &_dispContext.HandlerData, |
| 542 | &_dispContext.EstablisherFrame, |
| 543 | NULL); |
| 544 | // Update some fields of the unwind info now, since we have them. |
| 545 | _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData); |
| 546 | if (_dispContext.LanguageHandler) { |
| 547 | _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); |
| 548 | } else |
| 549 | _info.handler = 0; |
| 550 | return UNW_STEP_SUCCESS; |
| 551 | } |
| 552 | |
| 553 | A &_addressSpace; |
| 554 | unw_proc_info_t _info; |
| 555 | DISPATCHER_CONTEXT _dispContext; |
| 556 | CONTEXT _msContext; |
| 557 | UNWIND_HISTORY_TABLE _histTable; |
| 558 | bool _unwindInfoMissing; |
| 559 | }; |
| 560 | |
| 561 | |
| 562 | template <typename A, typename R> |
| 563 | UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) |
| 564 | : _addressSpace(as), _unwindInfoMissing(false) { |
| 565 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
| 566 | "UnwindCursor<> does not fit in unw_cursor_t"); |
Martin Storsjö | 1c0575e | 2020-08-17 22:41:58 +0300 | [diff] [blame] | 567 | static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), |
| 568 | "UnwindCursor<> requires more alignment than unw_cursor_t"); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 569 | memset(&_info, 0, sizeof(_info)); |
| 570 | memset(&_histTable, 0, sizeof(_histTable)); |
| 571 | _dispContext.ContextRecord = &_msContext; |
| 572 | _dispContext.HistoryTable = &_histTable; |
| 573 | // Initialize MS context from ours. |
| 574 | R r(context); |
| 575 | _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT; |
| 576 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 577 | _msContext.Rax = r.getRegister(UNW_X86_64_RAX); |
| 578 | _msContext.Rcx = r.getRegister(UNW_X86_64_RCX); |
| 579 | _msContext.Rdx = r.getRegister(UNW_X86_64_RDX); |
| 580 | _msContext.Rbx = r.getRegister(UNW_X86_64_RBX); |
| 581 | _msContext.Rsp = r.getRegister(UNW_X86_64_RSP); |
| 582 | _msContext.Rbp = r.getRegister(UNW_X86_64_RBP); |
| 583 | _msContext.Rsi = r.getRegister(UNW_X86_64_RSI); |
| 584 | _msContext.Rdi = r.getRegister(UNW_X86_64_RDI); |
| 585 | _msContext.R8 = r.getRegister(UNW_X86_64_R8); |
| 586 | _msContext.R9 = r.getRegister(UNW_X86_64_R9); |
| 587 | _msContext.R10 = r.getRegister(UNW_X86_64_R10); |
| 588 | _msContext.R11 = r.getRegister(UNW_X86_64_R11); |
| 589 | _msContext.R12 = r.getRegister(UNW_X86_64_R12); |
| 590 | _msContext.R13 = r.getRegister(UNW_X86_64_R13); |
| 591 | _msContext.R14 = r.getRegister(UNW_X86_64_R14); |
| 592 | _msContext.R15 = r.getRegister(UNW_X86_64_R15); |
| 593 | _msContext.Rip = r.getRegister(UNW_REG_IP); |
| 594 | union { |
| 595 | v128 v; |
| 596 | M128A m; |
| 597 | } t; |
| 598 | t.v = r.getVectorRegister(UNW_X86_64_XMM0); |
| 599 | _msContext.Xmm0 = t.m; |
| 600 | t.v = r.getVectorRegister(UNW_X86_64_XMM1); |
| 601 | _msContext.Xmm1 = t.m; |
| 602 | t.v = r.getVectorRegister(UNW_X86_64_XMM2); |
| 603 | _msContext.Xmm2 = t.m; |
| 604 | t.v = r.getVectorRegister(UNW_X86_64_XMM3); |
| 605 | _msContext.Xmm3 = t.m; |
| 606 | t.v = r.getVectorRegister(UNW_X86_64_XMM4); |
| 607 | _msContext.Xmm4 = t.m; |
| 608 | t.v = r.getVectorRegister(UNW_X86_64_XMM5); |
| 609 | _msContext.Xmm5 = t.m; |
| 610 | t.v = r.getVectorRegister(UNW_X86_64_XMM6); |
| 611 | _msContext.Xmm6 = t.m; |
| 612 | t.v = r.getVectorRegister(UNW_X86_64_XMM7); |
| 613 | _msContext.Xmm7 = t.m; |
| 614 | t.v = r.getVectorRegister(UNW_X86_64_XMM8); |
| 615 | _msContext.Xmm8 = t.m; |
| 616 | t.v = r.getVectorRegister(UNW_X86_64_XMM9); |
| 617 | _msContext.Xmm9 = t.m; |
| 618 | t.v = r.getVectorRegister(UNW_X86_64_XMM10); |
| 619 | _msContext.Xmm10 = t.m; |
| 620 | t.v = r.getVectorRegister(UNW_X86_64_XMM11); |
| 621 | _msContext.Xmm11 = t.m; |
| 622 | t.v = r.getVectorRegister(UNW_X86_64_XMM12); |
| 623 | _msContext.Xmm12 = t.m; |
| 624 | t.v = r.getVectorRegister(UNW_X86_64_XMM13); |
| 625 | _msContext.Xmm13 = t.m; |
| 626 | t.v = r.getVectorRegister(UNW_X86_64_XMM14); |
| 627 | _msContext.Xmm14 = t.m; |
| 628 | t.v = r.getVectorRegister(UNW_X86_64_XMM15); |
| 629 | _msContext.Xmm15 = t.m; |
| 630 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 631 | _msContext.R0 = r.getRegister(UNW_ARM_R0); |
| 632 | _msContext.R1 = r.getRegister(UNW_ARM_R1); |
| 633 | _msContext.R2 = r.getRegister(UNW_ARM_R2); |
| 634 | _msContext.R3 = r.getRegister(UNW_ARM_R3); |
| 635 | _msContext.R4 = r.getRegister(UNW_ARM_R4); |
| 636 | _msContext.R5 = r.getRegister(UNW_ARM_R5); |
| 637 | _msContext.R6 = r.getRegister(UNW_ARM_R6); |
| 638 | _msContext.R7 = r.getRegister(UNW_ARM_R7); |
| 639 | _msContext.R8 = r.getRegister(UNW_ARM_R8); |
| 640 | _msContext.R9 = r.getRegister(UNW_ARM_R9); |
| 641 | _msContext.R10 = r.getRegister(UNW_ARM_R10); |
| 642 | _msContext.R11 = r.getRegister(UNW_ARM_R11); |
| 643 | _msContext.R12 = r.getRegister(UNW_ARM_R12); |
| 644 | _msContext.Sp = r.getRegister(UNW_ARM_SP); |
| 645 | _msContext.Lr = r.getRegister(UNW_ARM_LR); |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 646 | _msContext.Pc = r.getRegister(UNW_ARM_IP); |
| 647 | for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) { |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 648 | union { |
| 649 | uint64_t w; |
| 650 | double d; |
| 651 | } d; |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 652 | d.d = r.getFloatRegister(i); |
| 653 | _msContext.D[i - UNW_ARM_D0] = d.w; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 654 | } |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 655 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 656 | for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i) |
| 657 | _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i); |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 658 | _msContext.Sp = r.getRegister(UNW_REG_SP); |
| 659 | _msContext.Pc = r.getRegister(UNW_REG_IP); |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 660 | for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i) |
| 661 | _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 662 | #endif |
| 663 | } |
| 664 | |
| 665 | template <typename A, typename R> |
| 666 | UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as) |
| 667 | : _addressSpace(as), _unwindInfoMissing(false) { |
| 668 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
| 669 | "UnwindCursor<> does not fit in unw_cursor_t"); |
| 670 | memset(&_info, 0, sizeof(_info)); |
| 671 | memset(&_histTable, 0, sizeof(_histTable)); |
| 672 | _dispContext.ContextRecord = &_msContext; |
| 673 | _dispContext.HistoryTable = &_histTable; |
| 674 | _msContext = *context; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | template <typename A, typename R> |
| 679 | bool UnwindCursor<A, R>::validReg(int regNum) { |
| 680 | if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true; |
| 681 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 682 | if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true; |
| 683 | #elif defined(_LIBUNWIND_TARGET_ARM) |
Ties Stuij | c8c0ec9 | 2021-12-08 09:44:45 +0000 | [diff] [blame] | 684 | if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) || |
| 685 | regNum == UNW_ARM_RA_AUTH_CODE) |
| 686 | return true; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 687 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 688 | if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 689 | #endif |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | template <typename A, typename R> |
| 694 | unw_word_t UnwindCursor<A, R>::getReg(int regNum) { |
| 695 | switch (regNum) { |
| 696 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 697 | case UNW_REG_IP: return _msContext.Rip; |
| 698 | case UNW_X86_64_RAX: return _msContext.Rax; |
| 699 | case UNW_X86_64_RDX: return _msContext.Rdx; |
| 700 | case UNW_X86_64_RCX: return _msContext.Rcx; |
| 701 | case UNW_X86_64_RBX: return _msContext.Rbx; |
| 702 | case UNW_REG_SP: |
| 703 | case UNW_X86_64_RSP: return _msContext.Rsp; |
| 704 | case UNW_X86_64_RBP: return _msContext.Rbp; |
| 705 | case UNW_X86_64_RSI: return _msContext.Rsi; |
| 706 | case UNW_X86_64_RDI: return _msContext.Rdi; |
| 707 | case UNW_X86_64_R8: return _msContext.R8; |
| 708 | case UNW_X86_64_R9: return _msContext.R9; |
| 709 | case UNW_X86_64_R10: return _msContext.R10; |
| 710 | case UNW_X86_64_R11: return _msContext.R11; |
| 711 | case UNW_X86_64_R12: return _msContext.R12; |
| 712 | case UNW_X86_64_R13: return _msContext.R13; |
| 713 | case UNW_X86_64_R14: return _msContext.R14; |
| 714 | case UNW_X86_64_R15: return _msContext.R15; |
| 715 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 716 | case UNW_ARM_R0: return _msContext.R0; |
| 717 | case UNW_ARM_R1: return _msContext.R1; |
| 718 | case UNW_ARM_R2: return _msContext.R2; |
| 719 | case UNW_ARM_R3: return _msContext.R3; |
| 720 | case UNW_ARM_R4: return _msContext.R4; |
| 721 | case UNW_ARM_R5: return _msContext.R5; |
| 722 | case UNW_ARM_R6: return _msContext.R6; |
| 723 | case UNW_ARM_R7: return _msContext.R7; |
| 724 | case UNW_ARM_R8: return _msContext.R8; |
| 725 | case UNW_ARM_R9: return _msContext.R9; |
| 726 | case UNW_ARM_R10: return _msContext.R10; |
| 727 | case UNW_ARM_R11: return _msContext.R11; |
| 728 | case UNW_ARM_R12: return _msContext.R12; |
| 729 | case UNW_REG_SP: |
| 730 | case UNW_ARM_SP: return _msContext.Sp; |
| 731 | case UNW_ARM_LR: return _msContext.Lr; |
| 732 | case UNW_REG_IP: |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 733 | case UNW_ARM_IP: return _msContext.Pc; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 734 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 735 | case UNW_REG_SP: return _msContext.Sp; |
| 736 | case UNW_REG_IP: return _msContext.Pc; |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 737 | default: return _msContext.X[regNum - UNW_AARCH64_X0]; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 738 | #endif |
| 739 | } |
| 740 | _LIBUNWIND_ABORT("unsupported register"); |
| 741 | } |
| 742 | |
| 743 | template <typename A, typename R> |
| 744 | void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { |
| 745 | switch (regNum) { |
| 746 | #if defined(_LIBUNWIND_TARGET_X86_64) |
| 747 | case UNW_REG_IP: _msContext.Rip = value; break; |
| 748 | case UNW_X86_64_RAX: _msContext.Rax = value; break; |
| 749 | case UNW_X86_64_RDX: _msContext.Rdx = value; break; |
| 750 | case UNW_X86_64_RCX: _msContext.Rcx = value; break; |
| 751 | case UNW_X86_64_RBX: _msContext.Rbx = value; break; |
| 752 | case UNW_REG_SP: |
| 753 | case UNW_X86_64_RSP: _msContext.Rsp = value; break; |
| 754 | case UNW_X86_64_RBP: _msContext.Rbp = value; break; |
| 755 | case UNW_X86_64_RSI: _msContext.Rsi = value; break; |
| 756 | case UNW_X86_64_RDI: _msContext.Rdi = value; break; |
| 757 | case UNW_X86_64_R8: _msContext.R8 = value; break; |
| 758 | case UNW_X86_64_R9: _msContext.R9 = value; break; |
| 759 | case UNW_X86_64_R10: _msContext.R10 = value; break; |
| 760 | case UNW_X86_64_R11: _msContext.R11 = value; break; |
| 761 | case UNW_X86_64_R12: _msContext.R12 = value; break; |
| 762 | case UNW_X86_64_R13: _msContext.R13 = value; break; |
| 763 | case UNW_X86_64_R14: _msContext.R14 = value; break; |
| 764 | case UNW_X86_64_R15: _msContext.R15 = value; break; |
| 765 | #elif defined(_LIBUNWIND_TARGET_ARM) |
| 766 | case UNW_ARM_R0: _msContext.R0 = value; break; |
| 767 | case UNW_ARM_R1: _msContext.R1 = value; break; |
| 768 | case UNW_ARM_R2: _msContext.R2 = value; break; |
| 769 | case UNW_ARM_R3: _msContext.R3 = value; break; |
| 770 | case UNW_ARM_R4: _msContext.R4 = value; break; |
| 771 | case UNW_ARM_R5: _msContext.R5 = value; break; |
| 772 | case UNW_ARM_R6: _msContext.R6 = value; break; |
| 773 | case UNW_ARM_R7: _msContext.R7 = value; break; |
| 774 | case UNW_ARM_R8: _msContext.R8 = value; break; |
| 775 | case UNW_ARM_R9: _msContext.R9 = value; break; |
| 776 | case UNW_ARM_R10: _msContext.R10 = value; break; |
| 777 | case UNW_ARM_R11: _msContext.R11 = value; break; |
| 778 | case UNW_ARM_R12: _msContext.R12 = value; break; |
| 779 | case UNW_REG_SP: |
| 780 | case UNW_ARM_SP: _msContext.Sp = value; break; |
| 781 | case UNW_ARM_LR: _msContext.Lr = value; break; |
| 782 | case UNW_REG_IP: |
Martin Storsjo | e5dbce2 | 2018-08-31 14:56:55 +0000 | [diff] [blame] | 783 | case UNW_ARM_IP: _msContext.Pc = value; break; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 784 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
| 785 | case UNW_REG_SP: _msContext.Sp = value; break; |
| 786 | case UNW_REG_IP: _msContext.Pc = value; break; |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 787 | case UNW_AARCH64_X0: |
| 788 | case UNW_AARCH64_X1: |
| 789 | case UNW_AARCH64_X2: |
| 790 | case UNW_AARCH64_X3: |
| 791 | case UNW_AARCH64_X4: |
| 792 | case UNW_AARCH64_X5: |
| 793 | case UNW_AARCH64_X6: |
| 794 | case UNW_AARCH64_X7: |
| 795 | case UNW_AARCH64_X8: |
| 796 | case UNW_AARCH64_X9: |
| 797 | case UNW_AARCH64_X10: |
| 798 | case UNW_AARCH64_X11: |
| 799 | case UNW_AARCH64_X12: |
| 800 | case UNW_AARCH64_X13: |
| 801 | case UNW_AARCH64_X14: |
| 802 | case UNW_AARCH64_X15: |
| 803 | case UNW_AARCH64_X16: |
| 804 | case UNW_AARCH64_X17: |
| 805 | case UNW_AARCH64_X18: |
| 806 | case UNW_AARCH64_X19: |
| 807 | case UNW_AARCH64_X20: |
| 808 | case UNW_AARCH64_X21: |
| 809 | case UNW_AARCH64_X22: |
| 810 | case UNW_AARCH64_X23: |
| 811 | case UNW_AARCH64_X24: |
| 812 | case UNW_AARCH64_X25: |
| 813 | case UNW_AARCH64_X26: |
| 814 | case UNW_AARCH64_X27: |
| 815 | case UNW_AARCH64_X28: |
| 816 | case UNW_AARCH64_FP: |
| 817 | case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 818 | #endif |
| 819 | default: |
| 820 | _LIBUNWIND_ABORT("unsupported register"); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | template <typename A, typename R> |
| 825 | bool UnwindCursor<A, R>::validFloatReg(int regNum) { |
| 826 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 827 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true; |
| 828 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true; |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 829 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 830 | if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true; |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 831 | #else |
| 832 | (void)regNum; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 833 | #endif |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | template <typename A, typename R> |
| 838 | unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { |
| 839 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 840 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { |
| 841 | union { |
| 842 | uint32_t w; |
| 843 | float f; |
| 844 | } d; |
| 845 | d.w = _msContext.S[regNum - UNW_ARM_S0]; |
| 846 | return d.f; |
| 847 | } |
| 848 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { |
| 849 | union { |
| 850 | uint64_t w; |
| 851 | double d; |
| 852 | } d; |
| 853 | d.w = _msContext.D[regNum - UNW_ARM_D0]; |
| 854 | return d.d; |
| 855 | } |
| 856 | _LIBUNWIND_ABORT("unsupported float register"); |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 857 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 858 | return _msContext.V[regNum - UNW_AARCH64_V0].D[0]; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 859 | #else |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 860 | (void)regNum; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 861 | _LIBUNWIND_ABORT("float registers unimplemented"); |
| 862 | #endif |
| 863 | } |
| 864 | |
| 865 | template <typename A, typename R> |
| 866 | void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { |
| 867 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 868 | if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) { |
| 869 | union { |
| 870 | uint32_t w; |
| 871 | float f; |
| 872 | } d; |
Martin Storsjö | 1c11cfe | 2022-05-07 00:26:45 +0300 | [diff] [blame] | 873 | d.f = (float)value; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 874 | _msContext.S[regNum - UNW_ARM_S0] = d.w; |
| 875 | } |
| 876 | if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) { |
| 877 | union { |
| 878 | uint64_t w; |
| 879 | double d; |
| 880 | } d; |
| 881 | d.d = value; |
| 882 | _msContext.D[regNum - UNW_ARM_D0] = d.w; |
| 883 | } |
| 884 | _LIBUNWIND_ABORT("unsupported float register"); |
Martin Storsjo | ce15011 | 2018-12-18 20:05:59 +0000 | [diff] [blame] | 885 | #elif defined(_LIBUNWIND_TARGET_AARCH64) |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 886 | _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 887 | #else |
Martin Storsjo | 059a163 | 2019-01-22 22:12:23 +0000 | [diff] [blame] | 888 | (void)regNum; |
| 889 | (void)value; |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 890 | _LIBUNWIND_ABORT("float registers unimplemented"); |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { |
| 895 | RtlRestoreContext(&_msContext, nullptr); |
| 896 | } |
| 897 | |
| 898 | #ifdef __arm__ |
| 899 | template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {} |
| 900 | #endif |
| 901 | |
| 902 | template <typename A, typename R> |
| 903 | const char *UnwindCursor<A, R>::getRegisterName(int regNum) { |
Martin Storsjo | 43bb9f8 | 2018-12-12 22:24:42 +0000 | [diff] [blame] | 904 | return R::getRegisterName(regNum); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | #else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32) |
| 912 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 913 | /// UnwindCursor contains all state (including all register values) during |
| 914 | /// an unwind. This is normally stack allocated inside a unw_cursor_t. |
| 915 | template <typename A, typename R> |
| 916 | class UnwindCursor : public AbstractUnwindCursor{ |
| 917 | typedef typename A::pint_t pint_t; |
| 918 | public: |
| 919 | UnwindCursor(unw_context_t *context, A &as); |
| 920 | UnwindCursor(A &as, void *threadArg); |
| 921 | virtual ~UnwindCursor() {} |
| 922 | virtual bool validReg(int); |
| 923 | virtual unw_word_t getReg(int); |
| 924 | virtual void setReg(int, unw_word_t); |
| 925 | virtual bool validFloatReg(int); |
| 926 | virtual unw_fpreg_t getFloatReg(int); |
| 927 | virtual void setFloatReg(int, unw_fpreg_t); |
| 928 | virtual int step(); |
| 929 | virtual void getInfo(unw_proc_info_t *); |
| 930 | virtual void jumpto(); |
| 931 | virtual bool isSignalFrame(); |
| 932 | virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off); |
| 933 | virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false); |
| 934 | virtual const char *getRegisterName(int num); |
| 935 | #ifdef __arm__ |
| 936 | virtual void saveVFPAsX(); |
| 937 | #endif |
| 938 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 939 | #ifdef _AIX |
| 940 | virtual uintptr_t getDataRelBase(); |
| 941 | #endif |
| 942 | |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 943 | #if defined(_LIBUNWIND_USE_CET) |
| 944 | virtual void *get_registers() { return &_registers; } |
| 945 | #endif |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 946 | |
Petr Hosek | 36f6154 | 2019-02-02 21:15:49 +0000 | [diff] [blame] | 947 | // libunwind does not and should not depend on C++ library which means that we |
Gabriel Ravier | 42aa6de | 2022-08-20 18:09:03 -0700 | [diff] [blame] | 948 | // need our own definition of inline placement new. |
Petr Hosek | 36f6154 | 2019-02-02 21:15:49 +0000 | [diff] [blame] | 949 | static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; } |
| 950 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 951 | private: |
| 952 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 953 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 954 | bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s); |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 955 | |
| 956 | int stepWithEHABI() { |
| 957 | size_t len = 0; |
| 958 | size_t off = 0; |
| 959 | // FIXME: Calling decode_eht_entry() here is violating the libunwind |
| 960 | // abstraction layer. |
| 961 | const uint32_t *ehtp = |
| 962 | decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info), |
| 963 | &off, &len); |
| 964 | if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) != |
| 965 | _URC_CONTINUE_UNWIND) |
| 966 | return UNW_STEP_END; |
| 967 | return UNW_STEP_SUCCESS; |
| 968 | } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 969 | #endif |
| 970 | |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 971 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 972 | bool setInfoForSigReturn() { |
| 973 | R dummy; |
| 974 | return setInfoForSigReturn(dummy); |
| 975 | } |
| 976 | int stepThroughSigReturn() { |
| 977 | R dummy; |
| 978 | return stepThroughSigReturn(dummy); |
| 979 | } |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 980 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 981 | bool setInfoForSigReturn(Registers_arm64 &); |
| 982 | int stepThroughSigReturn(Registers_arm64 &); |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 983 | #endif |
| 984 | #if defined(_LIBUNWIND_TARGET_S390X) |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 985 | bool setInfoForSigReturn(Registers_s390x &); |
| 986 | int stepThroughSigReturn(Registers_s390x &); |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 987 | #endif |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 988 | template <typename Registers> bool setInfoForSigReturn(Registers &) { |
| 989 | return false; |
| 990 | } |
| 991 | template <typename Registers> int stepThroughSigReturn(Registers &) { |
| 992 | return UNW_STEP_END; |
| 993 | } |
| 994 | #endif |
| 995 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 996 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 997 | bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo, |
| 998 | const typename CFI_Parser<A>::CIE_Info &cieInfo, |
| 999 | pint_t pc, uintptr_t dso_base); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1000 | bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s, |
| 1001 | uint32_t fdeSectionOffsetHint=0); |
| 1002 | int stepWithDwarfFDE() { |
| 1003 | return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace, |
| 1004 | (pint_t)this->getReg(UNW_REG_IP), |
| 1005 | (pint_t)_info.unwind_info, |
Sterling Augustine | b6a6639 | 2019-10-31 12:45:20 -0700 | [diff] [blame] | 1006 | _registers, _isSignalFrame); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1007 | } |
| 1008 | #endif |
| 1009 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1010 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1011 | bool getInfoFromCompactEncodingSection(pint_t pc, |
| 1012 | const UnwindInfoSections §s); |
| 1013 | int stepWithCompactEncoding() { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1014 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1015 | if ( compactSaysUseDwarf() ) |
| 1016 | return stepWithDwarfFDE(); |
| 1017 | #endif |
| 1018 | R dummy; |
| 1019 | return stepWithCompactEncoding(dummy); |
| 1020 | } |
| 1021 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1022 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1023 | int stepWithCompactEncoding(Registers_x86_64 &) { |
| 1024 | return CompactUnwinder_x86_64<A>::stepWithCompactEncoding( |
| 1025 | _info.format, _info.start_ip, _addressSpace, _registers); |
| 1026 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1027 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1028 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1029 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1030 | int stepWithCompactEncoding(Registers_x86 &) { |
| 1031 | return CompactUnwinder_x86<A>::stepWithCompactEncoding( |
| 1032 | _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers); |
| 1033 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1034 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1035 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1036 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1037 | int stepWithCompactEncoding(Registers_ppc &) { |
| 1038 | return UNW_EINVAL; |
| 1039 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1040 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1041 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 1042 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 1043 | int stepWithCompactEncoding(Registers_ppc64 &) { |
| 1044 | return UNW_EINVAL; |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
| 1048 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1049 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1050 | int stepWithCompactEncoding(Registers_arm64 &) { |
| 1051 | return CompactUnwinder_arm64<A>::stepWithCompactEncoding( |
| 1052 | _info.format, _info.start_ip, _addressSpace, _registers); |
| 1053 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1054 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1055 | |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1056 | #if defined(_LIBUNWIND_TARGET_MIPS_O32) |
| 1057 | int stepWithCompactEncoding(Registers_mips_o32 &) { |
| 1058 | return UNW_EINVAL; |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 1062 | #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 1063 | int stepWithCompactEncoding(Registers_mips_newabi &) { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1064 | return UNW_EINVAL; |
| 1065 | } |
| 1066 | #endif |
| 1067 | |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 1068 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1069 | int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; } |
| 1070 | #endif |
| 1071 | |
Koakuma | f2ef96e | 2022-02-05 13:08:26 -0800 | [diff] [blame] | 1072 | #if defined(_LIBUNWIND_TARGET_SPARC64) |
| 1073 | int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; } |
| 1074 | #endif |
| 1075 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1076 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1077 | int stepWithCompactEncoding(Registers_riscv &) { |
| 1078 | return UNW_EINVAL; |
| 1079 | } |
| 1080 | #endif |
| 1081 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1082 | bool compactSaysUseDwarf(uint32_t *offset=NULL) const { |
| 1083 | R dummy; |
| 1084 | return compactSaysUseDwarf(dummy, offset); |
| 1085 | } |
| 1086 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1087 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1088 | bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const { |
| 1089 | if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) { |
| 1090 | if (offset) |
| 1091 | *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET); |
| 1092 | return true; |
| 1093 | } |
| 1094 | return false; |
| 1095 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1096 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1097 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1098 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1099 | bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const { |
| 1100 | if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) { |
| 1101 | if (offset) |
| 1102 | *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET); |
| 1103 | return true; |
| 1104 | } |
| 1105 | return false; |
| 1106 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1107 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1108 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1109 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1110 | bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const { |
| 1111 | return true; |
| 1112 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1113 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1114 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 1115 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 1116 | bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const { |
| 1117 | return true; |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1121 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1122 | bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const { |
| 1123 | if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) { |
| 1124 | if (offset) |
| 1125 | *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET); |
| 1126 | return true; |
| 1127 | } |
| 1128 | return false; |
| 1129 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1130 | #endif |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1131 | |
| 1132 | #if defined(_LIBUNWIND_TARGET_MIPS_O32) |
| 1133 | bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const { |
| 1134 | return true; |
| 1135 | } |
| 1136 | #endif |
| 1137 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 1138 | #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 1139 | bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1140 | return true; |
| 1141 | } |
| 1142 | #endif |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 1143 | |
| 1144 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1145 | bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; } |
| 1146 | #endif |
| 1147 | |
Koakuma | f2ef96e | 2022-02-05 13:08:26 -0800 | [diff] [blame] | 1148 | #if defined(_LIBUNWIND_TARGET_SPARC64) |
| 1149 | bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const { |
| 1150 | return true; |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1154 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1155 | bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const { |
| 1156 | return true; |
| 1157 | } |
| 1158 | #endif |
| 1159 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1160 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1161 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1162 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1163 | compact_unwind_encoding_t dwarfEncoding() const { |
| 1164 | R dummy; |
| 1165 | return dwarfEncoding(dummy); |
| 1166 | } |
| 1167 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1168 | #if defined(_LIBUNWIND_TARGET_X86_64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1169 | compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const { |
| 1170 | return UNWIND_X86_64_MODE_DWARF; |
| 1171 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1172 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1173 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1174 | #if defined(_LIBUNWIND_TARGET_I386) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1175 | compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const { |
| 1176 | return UNWIND_X86_MODE_DWARF; |
| 1177 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1178 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1179 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1180 | #if defined(_LIBUNWIND_TARGET_PPC) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1181 | compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const { |
| 1182 | return 0; |
| 1183 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1184 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1185 | |
Martin Storsjo | 8338b0a | 2018-01-02 22:11:30 +0000 | [diff] [blame] | 1186 | #if defined(_LIBUNWIND_TARGET_PPC64) |
| 1187 | compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const { |
| 1188 | return 0; |
| 1189 | } |
| 1190 | #endif |
| 1191 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1192 | #if defined(_LIBUNWIND_TARGET_AARCH64) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1193 | compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const { |
| 1194 | return UNWIND_ARM64_MODE_DWARF; |
| 1195 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1196 | #endif |
Peter Zotov | 8d63999 | 2015-08-31 05:26:37 +0000 | [diff] [blame] | 1197 | |
Martin Storsjo | a72285f | 2017-11-02 08:16:16 +0000 | [diff] [blame] | 1198 | #if defined(_LIBUNWIND_TARGET_ARM) |
| 1199 | compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const { |
| 1200 | return 0; |
| 1201 | } |
| 1202 | #endif |
| 1203 | |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1204 | #if defined (_LIBUNWIND_TARGET_OR1K) |
Peter Zotov | 8d63999 | 2015-08-31 05:26:37 +0000 | [diff] [blame] | 1205 | compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const { |
| 1206 | return 0; |
| 1207 | } |
Asiri Rathnayake | c00dcef | 2016-05-25 12:36:34 +0000 | [diff] [blame] | 1208 | #endif |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1209 | |
Brian Cain | b432c47 | 2020-04-09 00:14:02 -0500 | [diff] [blame] | 1210 | #if defined (_LIBUNWIND_TARGET_HEXAGON) |
| 1211 | compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const { |
| 1212 | return 0; |
| 1213 | } |
| 1214 | #endif |
| 1215 | |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1216 | #if defined (_LIBUNWIND_TARGET_MIPS_O32) |
| 1217 | compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const { |
| 1218 | return 0; |
| 1219 | } |
| 1220 | #endif |
| 1221 | |
John Baldwin | 541a435 | 2018-01-09 17:07:18 +0000 | [diff] [blame] | 1222 | #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI) |
| 1223 | compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const { |
John Baldwin | 56441d4 | 2017-12-12 21:43:36 +0000 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | #endif |
Daniel Cederman | 9f2f07a | 2019-01-14 10:15:20 +0000 | [diff] [blame] | 1227 | |
| 1228 | #if defined(_LIBUNWIND_TARGET_SPARC) |
| 1229 | compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; } |
| 1230 | #endif |
| 1231 | |
Koakuma | f2ef96e | 2022-02-05 13:08:26 -0800 | [diff] [blame] | 1232 | #if defined(_LIBUNWIND_TARGET_SPARC64) |
| 1233 | compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const { |
| 1234 | return 0; |
| 1235 | } |
| 1236 | #endif |
| 1237 | |
Sam Elliott | 81f7e17 | 2019-12-16 16:35:17 +0000 | [diff] [blame] | 1238 | #if defined (_LIBUNWIND_TARGET_RISCV) |
| 1239 | compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const { |
| 1240 | return 0; |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
Ulrich Weigand | 393e3ee | 2022-05-02 14:35:29 +0200 | [diff] [blame] | 1244 | #if defined (_LIBUNWIND_TARGET_S390X) |
| 1245 | compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const { |
| 1246 | return 0; |
| 1247 | } |
| 1248 | #endif |
| 1249 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1250 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1251 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1252 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1253 | // For runtime environments using SEH unwind data without Windows runtime |
| 1254 | // support. |
| 1255 | pint_t getLastPC() const { /* FIXME: Implement */ return 0; } |
| 1256 | void setLastPC(pint_t pc) { /* FIXME: Implement */ } |
| 1257 | RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) { |
| 1258 | /* FIXME: Implement */ |
| 1259 | *base = 0; |
| 1260 | return nullptr; |
| 1261 | } |
| 1262 | bool getInfoFromSEH(pint_t pc); |
| 1263 | int stepWithSEHData() { /* FIXME: Implement */ return 0; } |
| 1264 | #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1265 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 1266 | #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
| 1267 | bool getInfoFromTBTable(pint_t pc, R ®isters); |
| 1268 | int stepWithTBTable(pint_t pc, tbtable *TBTable, R ®isters, |
| 1269 | bool &isSignalFrame); |
| 1270 | int stepWithTBTableData() { |
| 1271 | return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)), |
| 1272 | reinterpret_cast<tbtable *>(_info.unwind_info), |
| 1273 | _registers, _isSignalFrame); |
| 1274 | } |
| 1275 | #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1276 | |
| 1277 | A &_addressSpace; |
| 1278 | R _registers; |
| 1279 | unw_proc_info_t _info; |
| 1280 | bool _unwindInfoMissing; |
| 1281 | bool _isSignalFrame; |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 1282 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 1283 | bool _isSigReturn = false; |
| 1284 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1285 | }; |
| 1286 | |
| 1287 | |
| 1288 | template <typename A, typename R> |
| 1289 | UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as) |
| 1290 | : _addressSpace(as), _registers(context), _unwindInfoMissing(false), |
| 1291 | _isSignalFrame(false) { |
Asiri Rathnayake | 74d3525 | 2016-05-26 21:45:54 +0000 | [diff] [blame] | 1292 | static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit), |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1293 | "UnwindCursor<> does not fit in unw_cursor_t"); |
Martin Storsjö | 1c0575e | 2020-08-17 22:41:58 +0300 | [diff] [blame] | 1294 | static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)), |
| 1295 | "UnwindCursor<> requires more alignment than unw_cursor_t"); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1296 | memset(&_info, 0, sizeof(_info)); |
| 1297 | } |
| 1298 | |
| 1299 | template <typename A, typename R> |
| 1300 | UnwindCursor<A, R>::UnwindCursor(A &as, void *) |
| 1301 | : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) { |
| 1302 | memset(&_info, 0, sizeof(_info)); |
| 1303 | // FIXME |
| 1304 | // fill in _registers from thread arg |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | template <typename A, typename R> |
| 1309 | bool UnwindCursor<A, R>::validReg(int regNum) { |
| 1310 | return _registers.validRegister(regNum); |
| 1311 | } |
| 1312 | |
| 1313 | template <typename A, typename R> |
| 1314 | unw_word_t UnwindCursor<A, R>::getReg(int regNum) { |
| 1315 | return _registers.getRegister(regNum); |
| 1316 | } |
| 1317 | |
| 1318 | template <typename A, typename R> |
| 1319 | void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) { |
| 1320 | _registers.setRegister(regNum, (typename A::pint_t)value); |
| 1321 | } |
| 1322 | |
| 1323 | template <typename A, typename R> |
| 1324 | bool UnwindCursor<A, R>::validFloatReg(int regNum) { |
| 1325 | return _registers.validFloatRegister(regNum); |
| 1326 | } |
| 1327 | |
| 1328 | template <typename A, typename R> |
| 1329 | unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) { |
| 1330 | return _registers.getFloatRegister(regNum); |
| 1331 | } |
| 1332 | |
| 1333 | template <typename A, typename R> |
| 1334 | void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) { |
| 1335 | _registers.setFloatRegister(regNum, value); |
| 1336 | } |
| 1337 | |
| 1338 | template <typename A, typename R> void UnwindCursor<A, R>::jumpto() { |
| 1339 | _registers.jumpto(); |
| 1340 | } |
| 1341 | |
| 1342 | #ifdef __arm__ |
| 1343 | template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() { |
| 1344 | _registers.saveVFPAsX(); |
| 1345 | } |
| 1346 | #endif |
| 1347 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 1348 | #ifdef _AIX |
| 1349 | template <typename A, typename R> |
| 1350 | uintptr_t UnwindCursor<A, R>::getDataRelBase() { |
| 1351 | return reinterpret_cast<uintptr_t>(_info.extra); |
| 1352 | } |
| 1353 | #endif |
| 1354 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1355 | template <typename A, typename R> |
| 1356 | const char *UnwindCursor<A, R>::getRegisterName(int regNum) { |
| 1357 | return _registers.getRegisterName(regNum); |
| 1358 | } |
| 1359 | |
| 1360 | template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() { |
| 1361 | return _isSignalFrame; |
| 1362 | } |
| 1363 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1364 | #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1365 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1366 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1367 | template<typename A> |
| 1368 | struct EHABISectionIterator { |
| 1369 | typedef EHABISectionIterator _Self; |
| 1370 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1371 | typedef typename A::pint_t value_type; |
| 1372 | typedef typename A::pint_t* pointer; |
| 1373 | typedef typename A::pint_t& reference; |
| 1374 | typedef size_t size_type; |
| 1375 | typedef size_t difference_type; |
| 1376 | |
| 1377 | static _Self begin(A& addressSpace, const UnwindInfoSections& sects) { |
| 1378 | return _Self(addressSpace, sects, 0); |
| 1379 | } |
| 1380 | static _Self end(A& addressSpace, const UnwindInfoSections& sects) { |
Ed Schouten | 5d3f35b | 2017-03-07 15:21:57 +0000 | [diff] [blame] | 1381 | return _Self(addressSpace, sects, |
| 1382 | sects.arm_section_length / sizeof(EHABIIndexEntry)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i) |
| 1386 | : _i(i), _addressSpace(&addressSpace), _sects(§s) {} |
| 1387 | |
| 1388 | _Self& operator++() { ++_i; return *this; } |
| 1389 | _Self& operator+=(size_t a) { _i += a; return *this; } |
| 1390 | _Self& operator--() { assert(_i > 0); --_i; return *this; } |
| 1391 | _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; } |
| 1392 | |
| 1393 | _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; } |
| 1394 | _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; } |
| 1395 | |
Saleem Abdulrasool | fbff6b1 | 2020-06-18 08:51:44 -0700 | [diff] [blame] | 1396 | size_t operator-(const _Self& other) const { return _i - other._i; } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1397 | |
| 1398 | bool operator==(const _Self& other) const { |
| 1399 | assert(_addressSpace == other._addressSpace); |
| 1400 | assert(_sects == other._sects); |
| 1401 | return _i == other._i; |
| 1402 | } |
| 1403 | |
Saleem Abdulrasool | fbff6b1 | 2020-06-18 08:51:44 -0700 | [diff] [blame] | 1404 | bool operator!=(const _Self& other) const { |
| 1405 | assert(_addressSpace == other._addressSpace); |
| 1406 | assert(_sects == other._sects); |
| 1407 | return _i != other._i; |
| 1408 | } |
| 1409 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1410 | typename A::pint_t operator*() const { return functionAddress(); } |
| 1411 | |
| 1412 | typename A::pint_t functionAddress() const { |
| 1413 | typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( |
| 1414 | EHABIIndexEntry, _i, functionOffset); |
| 1415 | return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr)); |
| 1416 | } |
| 1417 | |
| 1418 | typename A::pint_t dataAddress() { |
| 1419 | typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof( |
| 1420 | EHABIIndexEntry, _i, data); |
| 1421 | return indexAddr; |
| 1422 | } |
| 1423 | |
| 1424 | private: |
| 1425 | size_t _i; |
| 1426 | A* _addressSpace; |
| 1427 | const UnwindInfoSections* _sects; |
| 1428 | }; |
| 1429 | |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1430 | namespace { |
| 1431 | |
| 1432 | template <typename A> |
| 1433 | EHABISectionIterator<A> EHABISectionUpperBound( |
| 1434 | EHABISectionIterator<A> first, |
| 1435 | EHABISectionIterator<A> last, |
| 1436 | typename A::pint_t value) { |
| 1437 | size_t len = last - first; |
| 1438 | while (len > 0) { |
| 1439 | size_t l2 = len / 2; |
| 1440 | EHABISectionIterator<A> m = first + l2; |
| 1441 | if (value < *m) { |
| 1442 | len = l2; |
| 1443 | } else { |
| 1444 | first = ++m; |
| 1445 | len -= l2 + 1; |
| 1446 | } |
| 1447 | } |
| 1448 | return first; |
| 1449 | } |
| 1450 | |
| 1451 | } |
| 1452 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1453 | template <typename A, typename R> |
| 1454 | bool UnwindCursor<A, R>::getInfoFromEHABISection( |
| 1455 | pint_t pc, |
| 1456 | const UnwindInfoSections §s) { |
| 1457 | EHABISectionIterator<A> begin = |
| 1458 | EHABISectionIterator<A>::begin(_addressSpace, sects); |
| 1459 | EHABISectionIterator<A> end = |
| 1460 | EHABISectionIterator<A>::end(_addressSpace, sects); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1461 | if (begin == end) |
| 1462 | return false; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1463 | |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1464 | EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1465 | if (itNextPC == begin) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1466 | return false; |
| 1467 | EHABISectionIterator<A> itThisPC = itNextPC - 1; |
| 1468 | |
| 1469 | pint_t thisPC = itThisPC.functionAddress(); |
Momchil Velikov | 064d69a | 2017-07-24 09:19:32 +0000 | [diff] [blame] | 1470 | // If an exception is thrown from a function, corresponding to the last entry |
| 1471 | // in the table, we don't really know the function extent and have to choose a |
| 1472 | // value for nextPC. Choosing max() will allow the range check during trace to |
| 1473 | // succeed. |
Petr Hosek | ac0d9e0 | 2019-01-29 22:26:18 +0000 | [diff] [blame] | 1474 | pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress(); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1475 | pint_t indexDataAddr = itThisPC.dataAddress(); |
| 1476 | |
| 1477 | if (indexDataAddr == 0) |
| 1478 | return false; |
| 1479 | |
| 1480 | uint32_t indexData = _addressSpace.get32(indexDataAddr); |
| 1481 | if (indexData == UNW_EXIDX_CANTUNWIND) |
| 1482 | return false; |
| 1483 | |
| 1484 | // If the high bit is set, the exception handling table entry is inline inside |
| 1485 | // the index table entry on the second word (aka |indexDataAddr|). Otherwise, |
Nico Weber | d999d54 | 2020-02-03 14:16:52 -0500 | [diff] [blame] | 1486 | // the table points at an offset in the exception handling table (section 5 |
| 1487 | // EHABI). |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1488 | pint_t exceptionTableAddr; |
| 1489 | uint32_t exceptionTableData; |
| 1490 | bool isSingleWordEHT; |
| 1491 | if (indexData & 0x80000000) { |
| 1492 | exceptionTableAddr = indexDataAddr; |
| 1493 | // TODO(ajwong): Should this data be 0? |
| 1494 | exceptionTableData = indexData; |
| 1495 | isSingleWordEHT = true; |
| 1496 | } else { |
| 1497 | exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData); |
| 1498 | exceptionTableData = _addressSpace.get32(exceptionTableAddr); |
| 1499 | isSingleWordEHT = false; |
| 1500 | } |
| 1501 | |
| 1502 | // Now we know the 3 things: |
| 1503 | // exceptionTableAddr -- exception handler table entry. |
| 1504 | // exceptionTableData -- the data inside the first word of the eht entry. |
| 1505 | // isSingleWordEHT -- whether the entry is in the index. |
| 1506 | unw_word_t personalityRoutine = 0xbadf00d; |
| 1507 | bool scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1508 | uintptr_t lsda; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1509 | |
| 1510 | // If the high bit in the exception handling table entry is set, the entry is |
| 1511 | // in compact form (section 6.3 EHABI). |
| 1512 | if (exceptionTableData & 0x80000000) { |
| 1513 | // Grab the index of the personality routine from the compact form. |
| 1514 | uint32_t choice = (exceptionTableData & 0x0f000000) >> 24; |
| 1515 | uint32_t extraWords = 0; |
| 1516 | switch (choice) { |
| 1517 | case 0: |
| 1518 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0; |
| 1519 | extraWords = 0; |
| 1520 | scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1521 | lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | case 1: |
| 1524 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1; |
| 1525 | extraWords = (exceptionTableData & 0x00ff0000) >> 16; |
| 1526 | scope32 = false; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1527 | lsda = exceptionTableAddr + (extraWords + 1) * 4; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | case 2: |
| 1530 | personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2; |
| 1531 | extraWords = (exceptionTableData & 0x00ff0000) >> 16; |
| 1532 | scope32 = true; |
Logan Chien | a54f096 | 2015-05-29 15:33:38 +0000 | [diff] [blame] | 1533 | lsda = exceptionTableAddr + (extraWords + 1) * 4; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1534 | break; |
| 1535 | default: |
| 1536 | _LIBUNWIND_ABORT("unknown personality routine"); |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | if (isSingleWordEHT) { |
| 1541 | if (extraWords != 0) { |
| 1542 | _LIBUNWIND_ABORT("index inlined table detected but pr function " |
| 1543 | "requires extra words"); |
| 1544 | return false; |
| 1545 | } |
| 1546 | } |
| 1547 | } else { |
| 1548 | pint_t personalityAddr = |
| 1549 | exceptionTableAddr + signExtendPrel31(exceptionTableData); |
| 1550 | personalityRoutine = personalityAddr; |
| 1551 | |
| 1552 | // ARM EHABI # 6.2, # 9.2 |
| 1553 | // |
| 1554 | // +---- ehtp |
| 1555 | // v |
| 1556 | // +--------------------------------------+ |
| 1557 | // | +--------+--------+--------+-------+ | |
| 1558 | // | |0| prel31 to personalityRoutine | | |
| 1559 | // | +--------+--------+--------+-------+ | |
| 1560 | // | | N | unwind opcodes | | <-- UnwindData |
| 1561 | // | +--------+--------+--------+-------+ | |
| 1562 | // | | Word 2 unwind opcodes | | |
| 1563 | // | +--------+--------+--------+-------+ | |
| 1564 | // | ... | |
| 1565 | // | +--------+--------+--------+-------+ | |
| 1566 | // | | Word N unwind opcodes | | |
| 1567 | // | +--------+--------+--------+-------+ | |
| 1568 | // | | LSDA | | <-- lsda |
| 1569 | // | | ... | | |
| 1570 | // | +--------+--------+--------+-------+ | |
| 1571 | // +--------------------------------------+ |
| 1572 | |
| 1573 | uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1; |
| 1574 | uint32_t FirstDataWord = *UnwindData; |
| 1575 | size_t N = ((FirstDataWord >> 24) & 0xff); |
| 1576 | size_t NDataWords = N + 1; |
| 1577 | lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords); |
| 1578 | } |
| 1579 | |
| 1580 | _info.start_ip = thisPC; |
| 1581 | _info.end_ip = nextPC; |
| 1582 | _info.handler = personalityRoutine; |
| 1583 | _info.unwind_info = exceptionTableAddr; |
| 1584 | _info.lsda = lsda; |
| 1585 | // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0. |
Nico Weber | d999d54 | 2020-02-03 14:16:52 -0500 | [diff] [blame] | 1586 | _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum? |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1587 | |
| 1588 | return true; |
| 1589 | } |
| 1590 | #endif |
| 1591 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1592 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1593 | template <typename A, typename R> |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1594 | bool UnwindCursor<A, R>::getInfoFromFdeCie( |
| 1595 | const typename CFI_Parser<A>::FDE_Info &fdeInfo, |
| 1596 | const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc, |
| 1597 | uintptr_t dso_base) { |
| 1598 | typename CFI_Parser<A>::PrologInfo prolog; |
| 1599 | if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc, |
| 1600 | R::getArch(), &prolog)) { |
| 1601 | // Save off parsed FDE info |
| 1602 | _info.start_ip = fdeInfo.pcStart; |
| 1603 | _info.end_ip = fdeInfo.pcEnd; |
| 1604 | _info.lsda = fdeInfo.lsda; |
| 1605 | _info.handler = cieInfo.personality; |
| 1606 | // Some frameless functions need SP altered when resuming in function, so |
| 1607 | // propagate spExtraArgSize. |
| 1608 | _info.gp = prolog.spExtraArgSize; |
| 1609 | _info.flags = 0; |
| 1610 | _info.format = dwarfEncoding(); |
| 1611 | _info.unwind_info = fdeInfo.fdeStart; |
| 1612 | _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength); |
| 1613 | _info.extra = static_cast<unw_word_t>(dso_base); |
| 1614 | return true; |
| 1615 | } |
| 1616 | return false; |
| 1617 | } |
| 1618 | |
| 1619 | template <typename A, typename R> |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1620 | bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc, |
| 1621 | const UnwindInfoSections §s, |
| 1622 | uint32_t fdeSectionOffsetHint) { |
| 1623 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 1624 | typename CFI_Parser<A>::CIE_Info cieInfo; |
| 1625 | bool foundFDE = false; |
| 1626 | bool foundInCache = false; |
| 1627 | // If compact encoding table gave offset into dwarf section, go directly there |
| 1628 | if (fdeSectionOffsetHint != 0) { |
| 1629 | foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
Ryan Prichard | 0cff857 | 2020-09-16 01:22:55 -0700 | [diff] [blame] | 1630 | sects.dwarf_section_length, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1631 | sects.dwarf_section + fdeSectionOffsetHint, |
| 1632 | &fdeInfo, &cieInfo); |
| 1633 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1634 | #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1635 | if (!foundFDE && (sects.dwarf_index_section != 0)) { |
| 1636 | foundFDE = EHHeaderParser<A>::findFDE( |
| 1637 | _addressSpace, pc, sects.dwarf_index_section, |
| 1638 | (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo); |
| 1639 | } |
| 1640 | #endif |
| 1641 | if (!foundFDE) { |
| 1642 | // otherwise, search cache of previously found FDEs. |
| 1643 | pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc); |
| 1644 | if (cachedFDE != 0) { |
| 1645 | foundFDE = |
| 1646 | CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
Ryan Prichard | 0cff857 | 2020-09-16 01:22:55 -0700 | [diff] [blame] | 1647 | sects.dwarf_section_length, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1648 | cachedFDE, &fdeInfo, &cieInfo); |
| 1649 | foundInCache = foundFDE; |
| 1650 | } |
| 1651 | } |
| 1652 | if (!foundFDE) { |
| 1653 | // Still not found, do full scan of __eh_frame section. |
| 1654 | foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section, |
Ryan Prichard | 0cff857 | 2020-09-16 01:22:55 -0700 | [diff] [blame] | 1655 | sects.dwarf_section_length, 0, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1656 | &fdeInfo, &cieInfo); |
| 1657 | } |
| 1658 | if (foundFDE) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 1659 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1660 | // Add to cache (to make next lookup faster) if we had no hint |
| 1661 | // and there was no index. |
| 1662 | if (!foundInCache && (fdeSectionOffsetHint == 0)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1663 | #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1664 | if (sects.dwarf_index_section == 0) |
| 1665 | #endif |
| 1666 | DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd, |
| 1667 | fdeInfo.fdeStart); |
| 1668 | } |
| 1669 | return true; |
| 1670 | } |
| 1671 | } |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1672 | //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1675 | #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1676 | |
| 1677 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1678 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1679 | template <typename A, typename R> |
| 1680 | bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc, |
| 1681 | const UnwindInfoSections §s) { |
| 1682 | const bool log = false; |
| 1683 | if (log) |
| 1684 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n", |
| 1685 | (uint64_t)pc, (uint64_t)sects.dso_base); |
| 1686 | |
| 1687 | const UnwindSectionHeader<A> sectionHeader(_addressSpace, |
| 1688 | sects.compact_unwind_section); |
| 1689 | if (sectionHeader.version() != UNWIND_SECTION_VERSION) |
| 1690 | return false; |
| 1691 | |
| 1692 | // do a binary search of top level index to find page with unwind info |
| 1693 | pint_t targetFunctionOffset = pc - sects.dso_base; |
| 1694 | const UnwindSectionIndexArray<A> topIndex(_addressSpace, |
| 1695 | sects.compact_unwind_section |
| 1696 | + sectionHeader.indexSectionOffset()); |
| 1697 | uint32_t low = 0; |
| 1698 | uint32_t high = sectionHeader.indexCount(); |
| 1699 | uint32_t last = high - 1; |
| 1700 | while (low < high) { |
| 1701 | uint32_t mid = (low + high) / 2; |
| 1702 | //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n", |
| 1703 | //mid, low, high, topIndex.functionOffset(mid)); |
| 1704 | if (topIndex.functionOffset(mid) <= targetFunctionOffset) { |
| 1705 | if ((mid == last) || |
| 1706 | (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) { |
| 1707 | low = mid; |
| 1708 | break; |
| 1709 | } else { |
| 1710 | low = mid + 1; |
| 1711 | } |
| 1712 | } else { |
| 1713 | high = mid; |
| 1714 | } |
| 1715 | } |
| 1716 | const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low); |
| 1717 | const uint32_t firstLevelNextPageFunctionOffset = |
| 1718 | topIndex.functionOffset(low + 1); |
| 1719 | const pint_t secondLevelAddr = |
| 1720 | sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low); |
| 1721 | const pint_t lsdaArrayStartAddr = |
| 1722 | sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low); |
| 1723 | const pint_t lsdaArrayEndAddr = |
| 1724 | sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1); |
| 1725 | if (log) |
| 1726 | fprintf(stderr, "\tfirst level search for result index=%d " |
| 1727 | "to secondLevelAddr=0x%llX\n", |
| 1728 | low, (uint64_t) secondLevelAddr); |
| 1729 | // do a binary search of second level page index |
| 1730 | uint32_t encoding = 0; |
| 1731 | pint_t funcStart = 0; |
| 1732 | pint_t funcEnd = 0; |
| 1733 | pint_t lsda = 0; |
| 1734 | pint_t personality = 0; |
| 1735 | uint32_t pageKind = _addressSpace.get32(secondLevelAddr); |
| 1736 | if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) { |
| 1737 | // regular page |
| 1738 | UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace, |
| 1739 | secondLevelAddr); |
| 1740 | UnwindSectionRegularArray<A> pageIndex( |
| 1741 | _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); |
| 1742 | // binary search looks for entry with e where index[e].offset <= pc < |
| 1743 | // index[e+1].offset |
| 1744 | if (log) |
| 1745 | fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in " |
| 1746 | "regular page starting at secondLevelAddr=0x%llX\n", |
| 1747 | (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr); |
| 1748 | low = 0; |
| 1749 | high = pageHeader.entryCount(); |
| 1750 | while (low < high) { |
| 1751 | uint32_t mid = (low + high) / 2; |
| 1752 | if (pageIndex.functionOffset(mid) <= targetFunctionOffset) { |
| 1753 | if (mid == (uint32_t)(pageHeader.entryCount() - 1)) { |
| 1754 | // at end of table |
| 1755 | low = mid; |
| 1756 | funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; |
| 1757 | break; |
| 1758 | } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) { |
| 1759 | // next is too big, so we found it |
| 1760 | low = mid; |
| 1761 | funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base; |
| 1762 | break; |
| 1763 | } else { |
| 1764 | low = mid + 1; |
| 1765 | } |
| 1766 | } else { |
| 1767 | high = mid; |
| 1768 | } |
| 1769 | } |
| 1770 | encoding = pageIndex.encoding(low); |
| 1771 | funcStart = pageIndex.functionOffset(low) + sects.dso_base; |
| 1772 | if (pc < funcStart) { |
| 1773 | if (log) |
| 1774 | fprintf( |
| 1775 | stderr, |
| 1776 | "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", |
| 1777 | (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); |
| 1778 | return false; |
| 1779 | } |
| 1780 | if (pc > funcEnd) { |
| 1781 | if (log) |
| 1782 | fprintf( |
| 1783 | stderr, |
| 1784 | "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n", |
| 1785 | (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd); |
| 1786 | return false; |
| 1787 | } |
| 1788 | } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) { |
| 1789 | // compressed page |
| 1790 | UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace, |
| 1791 | secondLevelAddr); |
| 1792 | UnwindSectionCompressedArray<A> pageIndex( |
| 1793 | _addressSpace, secondLevelAddr + pageHeader.entryPageOffset()); |
| 1794 | const uint32_t targetFunctionPageOffset = |
| 1795 | (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset); |
| 1796 | // binary search looks for entry with e where index[e].offset <= pc < |
| 1797 | // index[e+1].offset |
| 1798 | if (log) |
| 1799 | fprintf(stderr, "\tbinary search of compressed page starting at " |
| 1800 | "secondLevelAddr=0x%llX\n", |
| 1801 | (uint64_t) secondLevelAddr); |
| 1802 | low = 0; |
| 1803 | last = pageHeader.entryCount() - 1; |
| 1804 | high = pageHeader.entryCount(); |
| 1805 | while (low < high) { |
| 1806 | uint32_t mid = (low + high) / 2; |
| 1807 | if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) { |
| 1808 | if ((mid == last) || |
| 1809 | (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) { |
| 1810 | low = mid; |
| 1811 | break; |
| 1812 | } else { |
| 1813 | low = mid + 1; |
| 1814 | } |
| 1815 | } else { |
| 1816 | high = mid; |
| 1817 | } |
| 1818 | } |
| 1819 | funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset |
| 1820 | + sects.dso_base; |
| 1821 | if (low < last) |
| 1822 | funcEnd = |
| 1823 | pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset |
| 1824 | + sects.dso_base; |
| 1825 | else |
| 1826 | funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base; |
| 1827 | if (pc < funcStart) { |
Nico Weber | 5f424e3 | 2021-07-02 10:03:32 -0400 | [diff] [blame] | 1828 | _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " |
| 1829 | "not in second level compressed unwind table. " |
| 1830 | "funcStart=0x%llX", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1831 | (uint64_t) pc, (uint64_t) funcStart); |
| 1832 | return false; |
| 1833 | } |
| 1834 | if (pc > funcEnd) { |
Nico Weber | 5f424e3 | 2021-07-02 10:03:32 -0400 | [diff] [blame] | 1835 | _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX " |
| 1836 | "not in second level compressed unwind table. " |
| 1837 | "funcEnd=0x%llX", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1838 | (uint64_t) pc, (uint64_t) funcEnd); |
| 1839 | return false; |
| 1840 | } |
| 1841 | uint16_t encodingIndex = pageIndex.encodingIndex(low); |
| 1842 | if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) { |
| 1843 | // encoding is in common table in section header |
| 1844 | encoding = _addressSpace.get32( |
| 1845 | sects.compact_unwind_section + |
| 1846 | sectionHeader.commonEncodingsArraySectionOffset() + |
| 1847 | encodingIndex * sizeof(uint32_t)); |
| 1848 | } else { |
| 1849 | // encoding is in page specific table |
| 1850 | uint16_t pageEncodingIndex = |
| 1851 | encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount(); |
| 1852 | encoding = _addressSpace.get32(secondLevelAddr + |
| 1853 | pageHeader.encodingsPageOffset() + |
| 1854 | pageEncodingIndex * sizeof(uint32_t)); |
| 1855 | } |
| 1856 | } else { |
Nico Weber | 5f424e3 | 2021-07-02 10:03:32 -0400 | [diff] [blame] | 1857 | _LIBUNWIND_DEBUG_LOG( |
| 1858 | "malformed __unwind_info at 0x%0llX bad second level page", |
| 1859 | (uint64_t)sects.compact_unwind_section); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1860 | return false; |
| 1861 | } |
| 1862 | |
| 1863 | // look up LSDA, if encoding says function has one |
| 1864 | if (encoding & UNWIND_HAS_LSDA) { |
| 1865 | UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr); |
| 1866 | uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base); |
| 1867 | low = 0; |
| 1868 | high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) / |
| 1869 | sizeof(unwind_info_section_header_lsda_index_entry); |
| 1870 | // binary search looks for entry with exact match for functionOffset |
| 1871 | if (log) |
| 1872 | fprintf(stderr, |
| 1873 | "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n", |
| 1874 | funcStartOffset); |
| 1875 | while (low < high) { |
| 1876 | uint32_t mid = (low + high) / 2; |
| 1877 | if (lsdaIndex.functionOffset(mid) == funcStartOffset) { |
| 1878 | lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base; |
| 1879 | break; |
| 1880 | } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) { |
| 1881 | low = mid + 1; |
| 1882 | } else { |
| 1883 | high = mid; |
| 1884 | } |
| 1885 | } |
| 1886 | if (lsda == 0) { |
| 1887 | _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 1888 | "pc=0x%0llX, but lsda table has no entry", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1889 | encoding, (uint64_t) pc); |
| 1890 | return false; |
| 1891 | } |
| 1892 | } |
| 1893 | |
Louis Dionne | d3bbbc3 | 2020-08-11 15:24:21 -0400 | [diff] [blame] | 1894 | // extract personality routine, if encoding says function has one |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1895 | uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >> |
| 1896 | (__builtin_ctz(UNWIND_PERSONALITY_MASK)); |
| 1897 | if (personalityIndex != 0) { |
| 1898 | --personalityIndex; // change 1-based to zero-based index |
Louis Dionne | 8c360cb | 2020-08-11 15:29:00 -0400 | [diff] [blame] | 1899 | if (personalityIndex >= sectionHeader.personalityArrayCount()) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1900 | _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, " |
Louis Dionne | 103afa4 | 2019-04-22 15:40:50 +0000 | [diff] [blame] | 1901 | "but personality table has only %d entries", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1902 | encoding, personalityIndex, |
| 1903 | sectionHeader.personalityArrayCount()); |
| 1904 | return false; |
| 1905 | } |
| 1906 | int32_t personalityDelta = (int32_t)_addressSpace.get32( |
| 1907 | sects.compact_unwind_section + |
| 1908 | sectionHeader.personalityArraySectionOffset() + |
| 1909 | personalityIndex * sizeof(uint32_t)); |
| 1910 | pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta; |
| 1911 | personality = _addressSpace.getP(personalityPointer); |
| 1912 | if (log) |
| 1913 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " |
| 1914 | "personalityDelta=0x%08X, personality=0x%08llX\n", |
| 1915 | (uint64_t) pc, personalityDelta, (uint64_t) personality); |
| 1916 | } |
| 1917 | |
| 1918 | if (log) |
| 1919 | fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), " |
| 1920 | "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n", |
| 1921 | (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart); |
| 1922 | _info.start_ip = funcStart; |
| 1923 | _info.end_ip = funcEnd; |
| 1924 | _info.lsda = lsda; |
| 1925 | _info.handler = personality; |
| 1926 | _info.gp = 0; |
| 1927 | _info.flags = 0; |
| 1928 | _info.format = encoding; |
| 1929 | _info.unwind_info = 0; |
| 1930 | _info.unwind_info_size = 0; |
| 1931 | _info.extra = sects.dso_base; |
| 1932 | return true; |
| 1933 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 1934 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1935 | |
| 1936 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1937 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 1938 | template <typename A, typename R> |
| 1939 | bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) { |
| 1940 | pint_t base; |
| 1941 | RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base); |
| 1942 | if (!unwindEntry) { |
| 1943 | _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc); |
| 1944 | return false; |
| 1945 | } |
| 1946 | _info.gp = 0; |
| 1947 | _info.flags = 0; |
| 1948 | _info.format = 0; |
| 1949 | _info.unwind_info_size = sizeof(RUNTIME_FUNCTION); |
| 1950 | _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry); |
| 1951 | _info.extra = base; |
| 1952 | _info.start_ip = base + unwindEntry->BeginAddress; |
| 1953 | #ifdef _LIBUNWIND_TARGET_X86_64 |
| 1954 | _info.end_ip = base + unwindEntry->EndAddress; |
| 1955 | // Only fill in the handler and LSDA if they're stale. |
| 1956 | if (pc != getLastPC()) { |
| 1957 | UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData); |
| 1958 | if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) { |
| 1959 | // The personality is given in the UNWIND_INFO itself. The LSDA immediately |
| 1960 | // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit |
| 1961 | // these structures.) |
| 1962 | // N.B. UNWIND_INFO structs are DWORD-aligned. |
| 1963 | uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1; |
| 1964 | const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]); |
| 1965 | _info.lsda = reinterpret_cast<unw_word_t>(handler+1); |
| 1966 | if (*handler) { |
| 1967 | _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality); |
| 1968 | } else |
| 1969 | _info.handler = 0; |
| 1970 | } else { |
| 1971 | _info.lsda = 0; |
| 1972 | _info.handler = 0; |
| 1973 | } |
| 1974 | } |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 1975 | #endif |
| 1976 | setLastPC(pc); |
| 1977 | return true; |
| 1978 | } |
| 1979 | #endif |
| 1980 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 1981 | #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
| 1982 | // Masks for traceback table field xtbtable. |
| 1983 | enum xTBTableMask : uint8_t { |
| 1984 | reservedBit = 0x02, // The traceback table was incorrectly generated if set |
| 1985 | // (see comments in function getInfoFromTBTable(). |
| 1986 | ehInfoBit = 0x08 // Exception handling info is present if set |
| 1987 | }; |
| 1988 | |
| 1989 | enum frameType : unw_word_t { |
| 1990 | frameWithXLEHStateTable = 0, |
| 1991 | frameWithEHInfo = 1 |
| 1992 | }; |
| 1993 | |
| 1994 | extern "C" { |
| 1995 | typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action, |
| 1996 | uint64_t, |
| 1997 | _Unwind_Exception *, |
| 1998 | struct _Unwind_Context *); |
| 1999 | __attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0; |
| 2000 | } |
| 2001 | |
| 2002 | static __xlcxx_personality_v0_t *xlcPersonalityV0; |
| 2003 | static RWMutex xlcPersonalityV0InitLock; |
| 2004 | |
| 2005 | template <typename A, typename R> |
| 2006 | bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R ®isters) { |
| 2007 | uint32_t *p = reinterpret_cast<uint32_t *>(pc); |
| 2008 | |
| 2009 | // Keep looking forward until a word of 0 is found. The traceback |
| 2010 | // table starts at the following word. |
| 2011 | while (*p) |
| 2012 | ++p; |
| 2013 | tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1); |
| 2014 | |
| 2015 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 2016 | char functionBuf[512]; |
| 2017 | const char *functionName = functionBuf; |
| 2018 | unw_word_t offset; |
| 2019 | if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) { |
| 2020 | functionName = ".anonymous."; |
| 2021 | } |
| 2022 | _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p", |
| 2023 | __func__, functionName, |
| 2024 | reinterpret_cast<void *>(TBTable)); |
| 2025 | } |
| 2026 | |
| 2027 | // If the traceback table does not contain necessary info, bypass this frame. |
| 2028 | if (!TBTable->tb.has_tboff) |
| 2029 | return false; |
| 2030 | |
| 2031 | // Structure tbtable_ext contains important data we are looking for. |
| 2032 | p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext); |
| 2033 | |
| 2034 | // Skip field parminfo if it exists. |
| 2035 | if (TBTable->tb.fixedparms || TBTable->tb.floatparms) |
| 2036 | ++p; |
| 2037 | |
| 2038 | // p now points to tb_offset, the offset from start of function to TB table. |
| 2039 | unw_word_t start_ip = |
| 2040 | reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t); |
| 2041 | unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable); |
| 2042 | ++p; |
| 2043 | |
| 2044 | _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n", |
| 2045 | reinterpret_cast<void *>(start_ip), |
| 2046 | reinterpret_cast<void *>(end_ip)); |
| 2047 | |
| 2048 | // Skip field hand_mask if it exists. |
| 2049 | if (TBTable->tb.int_hndl) |
| 2050 | ++p; |
| 2051 | |
| 2052 | unw_word_t lsda = 0; |
| 2053 | unw_word_t handler = 0; |
| 2054 | unw_word_t flags = frameType::frameWithXLEHStateTable; |
| 2055 | |
| 2056 | if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) { |
| 2057 | // State table info is available. The ctl_info field indicates the |
| 2058 | // number of CTL anchors. There should be only one entry for the C++ |
| 2059 | // state table. |
| 2060 | assert(*p == 1 && "libunwind: there must be only one ctl_info entry"); |
| 2061 | ++p; |
| 2062 | // p points to the offset of the state table into the stack. |
| 2063 | pint_t stateTableOffset = *p++; |
| 2064 | |
| 2065 | int framePointerReg; |
| 2066 | |
| 2067 | // Skip fields name_len and name if exist. |
| 2068 | if (TBTable->tb.name_present) { |
| 2069 | const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p)); |
| 2070 | p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len + |
| 2071 | sizeof(uint16_t)); |
| 2072 | } |
| 2073 | |
| 2074 | if (TBTable->tb.uses_alloca) |
| 2075 | framePointerReg = *(reinterpret_cast<char *>(p)); |
| 2076 | else |
| 2077 | framePointerReg = 1; // default frame pointer == SP |
| 2078 | |
| 2079 | _LIBUNWIND_TRACE_UNWINDING( |
| 2080 | "framePointerReg=%d, framePointer=%p, " |
| 2081 | "stateTableOffset=%#lx\n", |
| 2082 | framePointerReg, |
| 2083 | reinterpret_cast<void *>(_registers.getRegister(framePointerReg)), |
| 2084 | stateTableOffset); |
| 2085 | lsda = _registers.getRegister(framePointerReg) + stateTableOffset; |
| 2086 | |
| 2087 | // Since the traceback table generated by the legacy XLC++ does not |
| 2088 | // provide the location of the personality for the state table, |
| 2089 | // function __xlcxx_personality_v0(), which is the personality for the state |
| 2090 | // table and is exported from libc++abi, is directly assigned as the |
| 2091 | // handler here. When a legacy XLC++ frame is encountered, the symbol |
| 2092 | // is resolved dynamically using dlopen() to avoid hard dependency from |
| 2093 | // libunwind on libc++abi. |
| 2094 | |
| 2095 | // Resolve the function pointer to the state table personality if it has |
| 2096 | // not already. |
| 2097 | if (xlcPersonalityV0 == NULL) { |
| 2098 | xlcPersonalityV0InitLock.lock(); |
| 2099 | if (xlcPersonalityV0 == NULL) { |
| 2100 | // If libc++abi is statically linked in, symbol __xlcxx_personality_v0 |
| 2101 | // has been resolved at the link time. |
| 2102 | xlcPersonalityV0 = &__xlcxx_personality_v0; |
| 2103 | if (xlcPersonalityV0 == NULL) { |
| 2104 | // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0 |
| 2105 | // using dlopen(). |
| 2106 | const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)"; |
| 2107 | void *libHandle; |
Xing Xue | c38cbd4 | 2022-08-08 17:21:30 -0400 | [diff] [blame] | 2108 | // The AIX dlopen() sets errno to 0 when it is successful, which |
| 2109 | // clobbers the value of errno from the user code. This is an AIX |
| 2110 | // bug because according to POSIX it should not set errno to 0. To |
| 2111 | // workaround before AIX fixes the bug, errno is saved and restored. |
| 2112 | int saveErrno = errno; |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2113 | libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW); |
| 2114 | if (libHandle == NULL) { |
| 2115 | _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n", |
| 2116 | errno); |
| 2117 | assert(0 && "dlopen() failed"); |
| 2118 | } |
| 2119 | xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>( |
| 2120 | dlsym(libHandle, "__xlcxx_personality_v0")); |
| 2121 | if (xlcPersonalityV0 == NULL) { |
| 2122 | _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno); |
| 2123 | assert(0 && "dlsym() failed"); |
| 2124 | } |
| 2125 | dlclose(libHandle); |
Xing Xue | c38cbd4 | 2022-08-08 17:21:30 -0400 | [diff] [blame] | 2126 | errno = saveErrno; |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | xlcPersonalityV0InitLock.unlock(); |
| 2130 | } |
| 2131 | handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0); |
| 2132 | _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n", |
| 2133 | reinterpret_cast<void *>(lsda), |
| 2134 | reinterpret_cast<void *>(handler)); |
| 2135 | } else if (TBTable->tb.longtbtable) { |
| 2136 | // This frame has the traceback table extension. Possible cases are |
| 2137 | // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that |
| 2138 | // is not EH aware; or, 3) a frame of other languages. We need to figure out |
| 2139 | // if the traceback table extension contains the 'eh_info' structure. |
| 2140 | // |
| 2141 | // We also need to deal with the complexity arising from some XL compiler |
| 2142 | // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits |
| 2143 | // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice |
| 2144 | // versa. For frames of code generated by those compilers, the 'longtbtable' |
| 2145 | // bit may be set but there isn't really a traceback table extension. |
| 2146 | // |
| 2147 | // In </usr/include/sys/debug.h>, there is the following definition of |
| 2148 | // 'struct tbtable_ext'. It is not really a structure but a dummy to |
| 2149 | // collect the description of optional parts of the traceback table. |
| 2150 | // |
| 2151 | // struct tbtable_ext { |
| 2152 | // ... |
| 2153 | // char alloca_reg; /* Register for alloca automatic storage */ |
| 2154 | // struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */ |
| 2155 | // unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/ |
| 2156 | // }; |
| 2157 | // |
| 2158 | // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data |
| 2159 | // following 'alloca_reg' can be treated either as 'struct vec_ext' or |
| 2160 | // 'unsigned char xtbtable'. 'xtbtable' bits are defined in |
| 2161 | // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently |
| 2162 | // unused and should not be set. 'struct vec_ext' is defined in |
| 2163 | // </usr/include/sys/debug.h> as follows: |
| 2164 | // |
| 2165 | // struct vec_ext { |
| 2166 | // unsigned vr_saved:6; /* Number of non-volatile vector regs saved |
| 2167 | // */ |
| 2168 | // /* first register saved is assumed to be */ |
| 2169 | // /* 32 - vr_saved */ |
| 2170 | // unsigned saves_vrsave:1; /* Set if vrsave is saved on the stack */ |
| 2171 | // unsigned has_varargs:1; |
| 2172 | // ... |
| 2173 | // }; |
| 2174 | // |
| 2175 | // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it |
| 2176 | // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg', |
| 2177 | // we checks if the 7th bit is set or not because 'xtbtable' should |
| 2178 | // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved |
| 2179 | // in the future to make sure the mitigation works. This mitigation |
| 2180 | // is not 100% bullet proof because 'struct vec_ext' may not always have |
| 2181 | // 'saves_vrsave' bit set. |
| 2182 | // |
| 2183 | // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for |
| 2184 | // checking the 7th bit. |
| 2185 | |
| 2186 | // p points to field name len. |
| 2187 | uint8_t *charPtr = reinterpret_cast<uint8_t *>(p); |
| 2188 | |
| 2189 | // Skip fields name_len and name if they exist. |
| 2190 | if (TBTable->tb.name_present) { |
| 2191 | const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr)); |
| 2192 | charPtr = charPtr + name_len + sizeof(uint16_t); |
| 2193 | } |
| 2194 | |
| 2195 | // Skip field alloc_reg if it exists. |
| 2196 | if (TBTable->tb.uses_alloca) |
| 2197 | ++charPtr; |
| 2198 | |
| 2199 | // Check traceback table bit has_vec. Skip struct vec_ext if it exists. |
| 2200 | if (TBTable->tb.has_vec) |
| 2201 | // Note struct vec_ext does exist at this point because whether the |
| 2202 | // ordering of longtbtable and has_vec bits is correct or not, both |
| 2203 | // are set. |
| 2204 | charPtr += sizeof(struct vec_ext); |
| 2205 | |
| 2206 | // charPtr points to field 'xtbtable'. Check if the EH info is available. |
| 2207 | // Also check if the reserved bit of the extended traceback table field |
| 2208 | // 'xtbtable' is set. If it is, the traceback table was incorrectly |
| 2209 | // generated by an XL compiler that uses the wrong ordering of 'longtbtable' |
| 2210 | // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the |
| 2211 | // frame. |
| 2212 | if ((*charPtr & xTBTableMask::ehInfoBit) && |
| 2213 | !(*charPtr & xTBTableMask::reservedBit)) { |
| 2214 | // Mark this frame has the new EH info. |
| 2215 | flags = frameType::frameWithEHInfo; |
| 2216 | |
| 2217 | // eh_info is available. |
| 2218 | charPtr++; |
| 2219 | // The pointer is 4-byte aligned. |
| 2220 | if (reinterpret_cast<uintptr_t>(charPtr) % 4) |
| 2221 | charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4; |
| 2222 | uintptr_t *ehInfo = |
| 2223 | reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>( |
| 2224 | registers.getRegister(2) + |
| 2225 | *(reinterpret_cast<uintptr_t *>(charPtr))))); |
| 2226 | |
| 2227 | // ehInfo points to structure en_info. The first member is version. |
| 2228 | // Only version 0 is currently supported. |
| 2229 | assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 && |
| 2230 | "libunwind: ehInfo version other than 0 is not supported"); |
| 2231 | |
| 2232 | // Increment ehInfo to point to member lsda. |
| 2233 | ++ehInfo; |
| 2234 | lsda = *ehInfo++; |
| 2235 | |
| 2236 | // enInfo now points to member personality. |
| 2237 | handler = *ehInfo; |
| 2238 | |
| 2239 | _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n", |
| 2240 | lsda, handler); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | _info.start_ip = start_ip; |
| 2245 | _info.end_ip = end_ip; |
| 2246 | _info.lsda = lsda; |
| 2247 | _info.handler = handler; |
| 2248 | _info.gp = 0; |
| 2249 | _info.flags = flags; |
| 2250 | _info.format = 0; |
| 2251 | _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable); |
| 2252 | _info.unwind_info_size = 0; |
| 2253 | _info.extra = registers.getRegister(2); |
| 2254 | |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
| 2258 | // Step back up the stack following the frame back link. |
| 2259 | template <typename A, typename R> |
| 2260 | int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable, |
| 2261 | R ®isters, bool &isSignalFrame) { |
| 2262 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 2263 | char functionBuf[512]; |
| 2264 | const char *functionName = functionBuf; |
| 2265 | unw_word_t offset; |
| 2266 | if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) { |
| 2267 | functionName = ".anonymous."; |
| 2268 | } |
| 2269 | _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p", |
| 2270 | __func__, functionName, |
| 2271 | reinterpret_cast<void *>(TBTable)); |
| 2272 | } |
| 2273 | |
| 2274 | #if defined(__powerpc64__) |
| 2275 | // Instruction to reload TOC register "l r2,40(r1)" |
| 2276 | const uint32_t loadTOCRegInst = 0xe8410028; |
| 2277 | const int32_t unwPPCF0Index = UNW_PPC64_F0; |
| 2278 | const int32_t unwPPCV0Index = UNW_PPC64_V0; |
| 2279 | #else |
| 2280 | // Instruction to reload TOC register "l r2,20(r1)" |
| 2281 | const uint32_t loadTOCRegInst = 0x80410014; |
| 2282 | const int32_t unwPPCF0Index = UNW_PPC_F0; |
| 2283 | const int32_t unwPPCV0Index = UNW_PPC_V0; |
| 2284 | #endif |
| 2285 | |
| 2286 | R newRegisters = registers; |
| 2287 | |
| 2288 | // lastStack points to the stack frame of the next routine up. |
| 2289 | pint_t lastStack = *(reinterpret_cast<pint_t *>(registers.getSP())); |
| 2290 | |
| 2291 | // Return address is the address after call site instruction. |
| 2292 | pint_t returnAddress; |
| 2293 | |
| 2294 | if (isSignalFrame) { |
| 2295 | _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p", |
| 2296 | reinterpret_cast<void *>(lastStack)); |
| 2297 | |
| 2298 | sigcontext *sigContext = reinterpret_cast<sigcontext *>( |
| 2299 | reinterpret_cast<char *>(lastStack) + STKMIN); |
| 2300 | returnAddress = sigContext->sc_jmpbuf.jmp_context.iar; |
| 2301 | |
| 2302 | _LIBUNWIND_TRACE_UNWINDING("From sigContext=%p, returnAddress=%p\n", |
| 2303 | reinterpret_cast<void *>(sigContext), |
| 2304 | reinterpret_cast<void *>(returnAddress)); |
| 2305 | |
| 2306 | if (returnAddress < 0x10000000) { |
| 2307 | // Try again using STKMINALIGN |
| 2308 | sigContext = reinterpret_cast<sigcontext *>( |
| 2309 | reinterpret_cast<char *>(lastStack) + STKMINALIGN); |
| 2310 | returnAddress = sigContext->sc_jmpbuf.jmp_context.iar; |
| 2311 | if (returnAddress < 0x10000000) { |
| 2312 | _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p\n", |
| 2313 | reinterpret_cast<void *>(returnAddress)); |
| 2314 | return UNW_EBADFRAME; |
| 2315 | } else { |
| 2316 | _LIBUNWIND_TRACE_UNWINDING("Tried again using STKMINALIGN: " |
| 2317 | "sigContext=%p, returnAddress=%p. " |
| 2318 | "Seems to be a valid address\n", |
| 2319 | reinterpret_cast<void *>(sigContext), |
| 2320 | reinterpret_cast<void *>(returnAddress)); |
| 2321 | } |
| 2322 | } |
| 2323 | // Restore the condition register from sigcontext. |
| 2324 | newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr); |
| 2325 | |
| 2326 | // Restore GPRs from sigcontext. |
| 2327 | for (int i = 0; i < 32; ++i) |
| 2328 | newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]); |
| 2329 | |
| 2330 | // Restore FPRs from sigcontext. |
| 2331 | for (int i = 0; i < 32; ++i) |
| 2332 | newRegisters.setFloatRegister(i + unwPPCF0Index, |
| 2333 | sigContext->sc_jmpbuf.jmp_context.fpr[i]); |
| 2334 | |
| 2335 | // Restore vector registers if there is an associated extended context |
| 2336 | // structure. |
| 2337 | if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) { |
| 2338 | ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext); |
| 2339 | if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) { |
| 2340 | for (int i = 0; i < 32; ++i) |
| 2341 | newRegisters.setVectorRegister( |
| 2342 | i + unwPPCV0Index, *(reinterpret_cast<v128 *>( |
| 2343 | &(uContext->__extctx->__vmx.__vr[i])))); |
| 2344 | } |
| 2345 | } |
| 2346 | } else { |
| 2347 | // Step up a normal frame. |
| 2348 | returnAddress = reinterpret_cast<pint_t *>(lastStack)[2]; |
| 2349 | |
| 2350 | _LIBUNWIND_TRACE_UNWINDING("Extract info from lastStack=%p, " |
| 2351 | "returnAddress=%p\n", |
| 2352 | reinterpret_cast<void *>(lastStack), |
| 2353 | reinterpret_cast<void *>(returnAddress)); |
| 2354 | _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d\n", |
| 2355 | TBTable->tb.fpr_saved, TBTable->tb.gpr_saved, |
| 2356 | TBTable->tb.saves_cr); |
| 2357 | |
| 2358 | // Restore FP registers. |
| 2359 | char *ptrToRegs = reinterpret_cast<char *>(lastStack); |
| 2360 | double *FPRegs = reinterpret_cast<double *>( |
| 2361 | ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double))); |
| 2362 | for (int i = 0; i < TBTable->tb.fpr_saved; ++i) |
| 2363 | newRegisters.setFloatRegister( |
| 2364 | 32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]); |
| 2365 | |
| 2366 | // Restore GP registers. |
| 2367 | ptrToRegs = reinterpret_cast<char *>(FPRegs); |
| 2368 | uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>( |
| 2369 | ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t))); |
| 2370 | for (int i = 0; i < TBTable->tb.gpr_saved; ++i) |
| 2371 | newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]); |
| 2372 | |
| 2373 | // Restore Vector registers. |
| 2374 | ptrToRegs = reinterpret_cast<char *>(GPRegs); |
| 2375 | |
| 2376 | // Restore vector registers only if this is a Clang frame. Also |
| 2377 | // check if traceback table bit has_vec is set. If it is, structure |
| 2378 | // vec_ext is available. |
| 2379 | if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) { |
| 2380 | |
| 2381 | // Get to the vec_ext structure to check if vector registers are saved. |
| 2382 | uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext); |
| 2383 | |
| 2384 | // Skip field parminfo if exists. |
| 2385 | if (TBTable->tb.fixedparms || TBTable->tb.floatparms) |
| 2386 | ++p; |
| 2387 | |
| 2388 | // Skip field tb_offset if exists. |
| 2389 | if (TBTable->tb.has_tboff) |
| 2390 | ++p; |
| 2391 | |
| 2392 | // Skip field hand_mask if exists. |
| 2393 | if (TBTable->tb.int_hndl) |
| 2394 | ++p; |
| 2395 | |
| 2396 | // Skip fields ctl_info and ctl_info_disp if exist. |
| 2397 | if (TBTable->tb.has_ctl) { |
| 2398 | // Skip field ctl_info. |
| 2399 | ++p; |
| 2400 | // Skip field ctl_info_disp. |
| 2401 | ++p; |
| 2402 | } |
| 2403 | |
| 2404 | // Skip fields name_len and name if exist. |
| 2405 | // p is supposed to point to field name_len now. |
| 2406 | uint8_t *charPtr = reinterpret_cast<uint8_t *>(p); |
| 2407 | if (TBTable->tb.name_present) { |
| 2408 | const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr)); |
| 2409 | charPtr = charPtr + name_len + sizeof(uint16_t); |
| 2410 | } |
| 2411 | |
| 2412 | // Skip field alloc_reg if it exists. |
| 2413 | if (TBTable->tb.uses_alloca) |
| 2414 | ++charPtr; |
| 2415 | |
| 2416 | struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr); |
| 2417 | |
| 2418 | _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d\n", vec_ext->vr_saved); |
| 2419 | |
| 2420 | // Restore vector register(s) if saved on the stack. |
| 2421 | if (vec_ext->vr_saved) { |
| 2422 | // Saved vector registers are 16-byte aligned. |
| 2423 | if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16) |
| 2424 | ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16; |
| 2425 | v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved * |
| 2426 | sizeof(v128)); |
| 2427 | for (int i = 0; i < vec_ext->vr_saved; ++i) { |
| 2428 | newRegisters.setVectorRegister( |
| 2429 | 32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]); |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | if (TBTable->tb.saves_cr) { |
| 2434 | // Get the saved condition register. The condition register is only |
| 2435 | // a single word. |
| 2436 | newRegisters.setCR( |
| 2437 | *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t)))); |
| 2438 | } |
| 2439 | |
| 2440 | // Restore the SP. |
| 2441 | newRegisters.setSP(lastStack); |
| 2442 | |
| 2443 | // The first instruction after return. |
| 2444 | uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress)); |
| 2445 | |
| 2446 | // Do we need to set the TOC register? |
| 2447 | _LIBUNWIND_TRACE_UNWINDING( |
| 2448 | "Current gpr2=%p\n", |
| 2449 | reinterpret_cast<void *>(newRegisters.getRegister(2))); |
| 2450 | if (firstInstruction == loadTOCRegInst) { |
| 2451 | _LIBUNWIND_TRACE_UNWINDING( |
| 2452 | "Set gpr2=%p from frame\n", |
| 2453 | reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5])); |
| 2454 | newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]); |
| 2455 | } |
| 2456 | } |
| 2457 | _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n", |
| 2458 | reinterpret_cast<void *>(lastStack), |
| 2459 | reinterpret_cast<void *>(returnAddress), |
| 2460 | reinterpret_cast<void *>(pc)); |
| 2461 | |
| 2462 | // The return address is the address after call site instruction, so |
Gabriel Ravier | 42aa6de | 2022-08-20 18:09:03 -0700 | [diff] [blame] | 2463 | // setting IP to that simulates a return. |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2464 | newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress)); |
| 2465 | |
| 2466 | // Simulate the step by replacing the register set with the new ones. |
| 2467 | registers = newRegisters; |
| 2468 | |
| 2469 | // Check if the next frame is a signal frame. |
| 2470 | pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP())); |
| 2471 | |
| 2472 | // Return address is the address after call site instruction. |
| 2473 | pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2]; |
| 2474 | |
| 2475 | if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) { |
| 2476 | _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: " |
| 2477 | "nextStack=%p, next return address=%p\n", |
| 2478 | reinterpret_cast<void *>(nextStack), |
| 2479 | reinterpret_cast<void *>(nextReturnAddress)); |
| 2480 | isSignalFrame = true; |
| 2481 | } else { |
| 2482 | isSignalFrame = false; |
| 2483 | } |
| 2484 | |
| 2485 | return UNW_STEP_SUCCESS; |
| 2486 | } |
| 2487 | #endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 2488 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2489 | template <typename A, typename R> |
| 2490 | void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) { |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2491 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2492 | _isSigReturn = false; |
| 2493 | #endif |
| 2494 | |
| 2495 | pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2496 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2497 | // Remove the thumb bit so the IP represents the actual instruction address. |
| 2498 | // This matches the behaviour of _Unwind_GetIP on arm. |
| 2499 | pc &= (pint_t)~0x1; |
| 2500 | #endif |
| 2501 | |
Sterling Augustine | c100c4d | 2020-03-30 15:20:26 -0700 | [diff] [blame] | 2502 | // Exit early if at the top of the stack. |
| 2503 | if (pc == 0) { |
| 2504 | _unwindInfoMissing = true; |
| 2505 | return; |
| 2506 | } |
| 2507 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2508 | // If the last line of a function is a "throw" the compiler sometimes |
| 2509 | // emits no instructions after the call to __cxa_throw. This means |
| 2510 | // the return address is actually the start of the next function. |
| 2511 | // To disambiguate this, back up the pc when we know it is a return |
| 2512 | // address. |
| 2513 | if (isReturnAddress) |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2514 | #if defined(_AIX) |
| 2515 | // PC needs to be a 4-byte aligned address to be able to look for a |
| 2516 | // word of 0 that indicates the start of the traceback table at the end |
| 2517 | // of a function on AIX. |
| 2518 | pc -= 4; |
| 2519 | #else |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2520 | --pc; |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2521 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2522 | |
| 2523 | // Ask address space object to find unwind sections for this pc. |
| 2524 | UnwindInfoSections sects; |
| 2525 | if (_addressSpace.findUnwindSections(pc, sects)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2526 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2527 | // If there is a compact unwind encoding table, look there first. |
| 2528 | if (sects.compact_unwind_section != 0) { |
| 2529 | if (this->getInfoFromCompactEncodingSection(pc, sects)) { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2530 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2531 | // Found info in table, done unless encoding says to use dwarf. |
| 2532 | uint32_t dwarfOffset; |
| 2533 | if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) { |
| 2534 | if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) { |
| 2535 | // found info in dwarf, done |
| 2536 | return; |
| 2537 | } |
| 2538 | } |
| 2539 | #endif |
| 2540 | // If unwind table has entry, but entry says there is no unwind info, |
| 2541 | // record that we have no unwind info. |
| 2542 | if (_info.format == 0) |
| 2543 | _unwindInfoMissing = true; |
| 2544 | return; |
| 2545 | } |
| 2546 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2547 | #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2548 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 2549 | #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
| 2550 | // If there is SEH unwind info, look there next. |
| 2551 | if (this->getInfoFromSEH(pc)) |
| 2552 | return; |
| 2553 | #endif |
| 2554 | |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2555 | #if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
| 2556 | // If there is unwind info in the traceback table, look there next. |
| 2557 | if (this->getInfoFromTBTable(pc, _registers)) |
| 2558 | return; |
| 2559 | #endif |
| 2560 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2561 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2562 | // If there is dwarf unwind info, look there next. |
| 2563 | if (sects.dwarf_section != 0) { |
| 2564 | if (this->getInfoFromDwarfSection(pc, sects)) { |
| 2565 | // found info in dwarf, done |
| 2566 | return; |
| 2567 | } |
| 2568 | } |
| 2569 | #endif |
| 2570 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2571 | #if defined(_LIBUNWIND_ARM_EHABI) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2572 | // If there is ARM EHABI unwind info, look there next. |
| 2573 | if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects)) |
| 2574 | return; |
| 2575 | #endif |
| 2576 | } |
| 2577 | |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2578 | #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2579 | // There is no static unwind info for this pc. Look to see if an FDE was |
| 2580 | // dynamically registered for it. |
Ryan Prichard | 2cfcb8c | 2020-09-09 15:43:35 -0700 | [diff] [blame] | 2581 | pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll, |
| 2582 | pc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2583 | if (cachedFDE != 0) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 2584 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 2585 | typename CFI_Parser<A>::CIE_Info cieInfo; |
| 2586 | if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo)) |
| 2587 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2588 | return; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | // Lastly, ask AddressSpace object about platform specific ways to locate |
| 2592 | // other FDEs. |
| 2593 | pint_t fde; |
| 2594 | if (_addressSpace.findOtherFDE(pc, fde)) { |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 2595 | typename CFI_Parser<A>::FDE_Info fdeInfo; |
| 2596 | typename CFI_Parser<A>::CIE_Info cieInfo; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2597 | if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) { |
| 2598 | // Double check this FDE is for a function that includes the pc. |
Ryan Prichard | 1ae2b94 | 2020-08-18 02:31:38 -0700 | [diff] [blame] | 2599 | if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) |
| 2600 | if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0)) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2601 | return; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2602 | } |
| 2603 | } |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2604 | #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2605 | |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2606 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2607 | if (setInfoForSigReturn()) |
| 2608 | return; |
| 2609 | #endif |
| 2610 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2611 | // no unwind info, flag that we can't reliably unwind |
| 2612 | _unwindInfoMissing = true; |
| 2613 | } |
| 2614 | |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2615 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \ |
| 2616 | defined(_LIBUNWIND_TARGET_AARCH64) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2617 | template <typename A, typename R> |
| 2618 | bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) { |
| 2619 | // Look for the sigreturn trampoline. The trampoline's body is two |
| 2620 | // specific instructions (see below). Typically the trampoline comes from the |
| 2621 | // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its |
| 2622 | // own restorer function, though, or user-mode QEMU might write a trampoline |
| 2623 | // onto the stack. |
| 2624 | // |
| 2625 | // This special code path is a fallback that is only used if the trampoline |
| 2626 | // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register |
| 2627 | // constant for the PC needs to be defined before DWARF can handle a signal |
| 2628 | // trampoline. This code may segfault if the target PC is unreadable, e.g.: |
| 2629 | // - The PC points at a function compiled without unwind info, and which is |
| 2630 | // part of an execute-only mapping (e.g. using -Wl,--execute-only). |
| 2631 | // - The PC is invalid and happens to point to unreadable or unmapped memory. |
| 2632 | // |
| 2633 | // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S |
| 2634 | const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); |
Shoaib Meenai | 8c606b0 | 2022-05-25 21:39:12 -0700 | [diff] [blame] | 2635 | // The PC might contain an invalid address if the unwind info is bad, so |
| 2636 | // directly accessing it could cause a segfault. Use process_vm_readv to read |
| 2637 | // the memory safely instead. process_vm_readv was added in Linux 3.2, and |
| 2638 | // AArch64 supported was added in Linux 3.7, so the syscall is guaranteed to |
| 2639 | // be present. Unfortunately, there are Linux AArch64 environments where the |
| 2640 | // libc wrapper for the syscall might not be present (e.g. Android 5), so call |
| 2641 | // the syscall directly instead. |
| 2642 | uint32_t instructions[2]; |
| 2643 | struct iovec local_iov = {&instructions, sizeof instructions}; |
| 2644 | struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions}; |
| 2645 | long bytesRead = |
| 2646 | syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0); |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2647 | // Look for instructions: mov x8, #0x8b; svc #0x0 |
Shoaib Meenai | 8c606b0 | 2022-05-25 21:39:12 -0700 | [diff] [blame] | 2648 | if (bytesRead != sizeof instructions || instructions[0] != 0xd2801168 || |
| 2649 | instructions[1] != 0xd4000001) |
| 2650 | return false; |
| 2651 | |
| 2652 | _info = {}; |
| 2653 | _info.start_ip = pc; |
| 2654 | _info.end_ip = pc + 4; |
| 2655 | _isSigReturn = true; |
| 2656 | return true; |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | template <typename A, typename R> |
| 2660 | int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) { |
| 2661 | // In the signal trampoline frame, sp points to an rt_sigframe[1], which is: |
| 2662 | // - 128-byte siginfo struct |
| 2663 | // - ucontext struct: |
| 2664 | // - 8-byte long (uc_flags) |
| 2665 | // - 8-byte pointer (uc_link) |
| 2666 | // - 24-byte stack_t |
| 2667 | // - 128-byte signal set |
| 2668 | // - 8 bytes of padding because sigcontext has 16-byte alignment |
| 2669 | // - sigcontext/mcontext_t |
| 2670 | // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c |
| 2671 | const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304 |
| 2672 | |
| 2673 | // Offsets from sigcontext to each register. |
| 2674 | const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field |
| 2675 | const pint_t kOffsetSp = 256; // offset to "__u64 sp" field |
| 2676 | const pint_t kOffsetPc = 264; // offset to "__u64 pc" field |
| 2677 | |
| 2678 | pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext; |
| 2679 | |
| 2680 | for (int i = 0; i <= 30; ++i) { |
| 2681 | uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs + |
| 2682 | static_cast<pint_t>(i * 8)); |
Fangrui Song | 5f26300 | 2021-08-20 14:26:27 -0700 | [diff] [blame] | 2683 | _registers.setRegister(UNW_AARCH64_X0 + i, value); |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2684 | } |
| 2685 | _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp)); |
| 2686 | _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc)); |
| 2687 | _isSignalFrame = true; |
| 2688 | return UNW_STEP_SUCCESS; |
| 2689 | } |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2690 | #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && |
| 2691 | // defined(_LIBUNWIND_TARGET_AARCH64) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2692 | |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2693 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \ |
| 2694 | defined(_LIBUNWIND_TARGET_S390X) |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 2695 | template <typename A, typename R> |
| 2696 | bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) { |
| 2697 | // Look for the sigreturn trampoline. The trampoline's body is a |
| 2698 | // specific instruction (see below). Typically the trampoline comes from the |
| 2699 | // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its |
| 2700 | // own restorer function, though, or user-mode QEMU might write a trampoline |
| 2701 | // onto the stack. |
| 2702 | const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); |
Ulrich Weigand | 955e2ff | 2022-07-18 16:54:48 +0200 | [diff] [blame] | 2703 | // The PC might contain an invalid address if the unwind info is bad, so |
| 2704 | // directly accessing it could cause a segfault. Use process_vm_readv to |
| 2705 | // read the memory safely instead. |
| 2706 | uint16_t inst; |
| 2707 | struct iovec local_iov = {&inst, sizeof inst}; |
| 2708 | struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof inst}; |
| 2709 | long bytesRead = process_vm_readv(getpid(), &local_iov, 1, &remote_iov, 1, 0); |
| 2710 | if (bytesRead == sizeof inst && (inst == 0x0a77 || inst == 0x0aad)) { |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 2711 | _info = {}; |
| 2712 | _info.start_ip = pc; |
| 2713 | _info.end_ip = pc + 2; |
| 2714 | _isSigReturn = true; |
| 2715 | return true; |
| 2716 | } |
| 2717 | return false; |
| 2718 | } |
| 2719 | |
| 2720 | template <typename A, typename R> |
| 2721 | int UnwindCursor<A, R>::stepThroughSigReturn(Registers_s390x &) { |
| 2722 | // Determine current SP. |
| 2723 | const pint_t sp = static_cast<pint_t>(this->getReg(UNW_REG_SP)); |
| 2724 | // According to the s390x ABI, the CFA is at (incoming) SP + 160. |
| 2725 | const pint_t cfa = sp + 160; |
| 2726 | |
| 2727 | // Determine current PC and instruction there (this must be either |
| 2728 | // a "svc __NR_sigreturn" or "svc __NR_rt_sigreturn"). |
| 2729 | const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP)); |
| 2730 | const uint16_t inst = _addressSpace.get16(pc); |
| 2731 | |
| 2732 | // Find the addresses of the signo and sigcontext in the frame. |
| 2733 | pint_t pSigctx = 0; |
| 2734 | pint_t pSigno = 0; |
| 2735 | |
| 2736 | // "svc __NR_sigreturn" uses a non-RT signal trampoline frame. |
| 2737 | if (inst == 0x0a77) { |
| 2738 | // Layout of a non-RT signal trampoline frame, starting at the CFA: |
| 2739 | // - 8-byte signal mask |
| 2740 | // - 8-byte pointer to sigcontext, followed by signo |
| 2741 | // - 4-byte signo |
| 2742 | pSigctx = _addressSpace.get64(cfa + 8); |
| 2743 | pSigno = pSigctx + 344; |
| 2744 | } |
| 2745 | |
| 2746 | // "svc __NR_rt_sigreturn" uses a RT signal trampoline frame. |
| 2747 | if (inst == 0x0aad) { |
| 2748 | // Layout of a RT signal trampoline frame, starting at the CFA: |
| 2749 | // - 8-byte retcode (+ alignment) |
| 2750 | // - 128-byte siginfo struct (starts with signo) |
| 2751 | // - ucontext struct: |
| 2752 | // - 8-byte long (uc_flags) |
| 2753 | // - 8-byte pointer (uc_link) |
| 2754 | // - 24-byte stack_t |
| 2755 | // - 8 bytes of padding because sigcontext has 16-byte alignment |
| 2756 | // - sigcontext/mcontext_t |
| 2757 | pSigctx = cfa + 8 + 128 + 8 + 8 + 24 + 8; |
| 2758 | pSigno = cfa + 8; |
| 2759 | } |
| 2760 | |
| 2761 | assert(pSigctx != 0); |
| 2762 | assert(pSigno != 0); |
| 2763 | |
| 2764 | // Offsets from sigcontext to each register. |
| 2765 | const pint_t kOffsetPc = 8; |
| 2766 | const pint_t kOffsetGprs = 16; |
| 2767 | const pint_t kOffsetFprs = 216; |
| 2768 | |
| 2769 | // Restore all registers. |
| 2770 | for (int i = 0; i < 16; ++i) { |
| 2771 | uint64_t value = _addressSpace.get64(pSigctx + kOffsetGprs + |
| 2772 | static_cast<pint_t>(i * 8)); |
| 2773 | _registers.setRegister(UNW_S390X_R0 + i, value); |
| 2774 | } |
| 2775 | for (int i = 0; i < 16; ++i) { |
| 2776 | static const int fpr[16] = { |
| 2777 | UNW_S390X_F0, UNW_S390X_F1, UNW_S390X_F2, UNW_S390X_F3, |
| 2778 | UNW_S390X_F4, UNW_S390X_F5, UNW_S390X_F6, UNW_S390X_F7, |
| 2779 | UNW_S390X_F8, UNW_S390X_F9, UNW_S390X_F10, UNW_S390X_F11, |
| 2780 | UNW_S390X_F12, UNW_S390X_F13, UNW_S390X_F14, UNW_S390X_F15 |
| 2781 | }; |
| 2782 | double value = _addressSpace.getDouble(pSigctx + kOffsetFprs + |
| 2783 | static_cast<pint_t>(i * 8)); |
| 2784 | _registers.setFloatRegister(fpr[i], value); |
| 2785 | } |
| 2786 | _registers.setIP(_addressSpace.get64(pSigctx + kOffsetPc)); |
| 2787 | |
| 2788 | // SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr |
| 2789 | // after the faulting instruction rather than before it. |
| 2790 | // Do not set _isSignalFrame in that case. |
| 2791 | uint32_t signo = _addressSpace.get32(pSigno); |
| 2792 | _isSignalFrame = (signo != 4 && signo != 5 && signo != 8); |
| 2793 | |
| 2794 | return UNW_STEP_SUCCESS; |
| 2795 | } |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2796 | #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && |
| 2797 | // defined(_LIBUNWIND_TARGET_S390X) |
Ulrich Weigand | f1108b6 | 2022-05-04 10:43:11 +0200 | [diff] [blame] | 2798 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2799 | template <typename A, typename R> |
| 2800 | int UnwindCursor<A, R>::step() { |
| 2801 | // Bottom of stack is defined is when unwind info cannot be found. |
| 2802 | if (_unwindInfoMissing) |
| 2803 | return UNW_STEP_END; |
| 2804 | |
| 2805 | // Use unwinding info to modify register set as if function returned. |
| 2806 | int result; |
Shoaib Meenai | 67bace7 | 2022-05-23 22:11:34 -0700 | [diff] [blame] | 2807 | #if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2808 | if (_isSigReturn) { |
| 2809 | result = this->stepThroughSigReturn(); |
| 2810 | } else |
| 2811 | #endif |
| 2812 | { |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2813 | #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2814 | result = this->stepWithCompactEncoding(); |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 2815 | #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2816 | result = this->stepWithSEHData(); |
Xing Xue | bbcbce9 | 2022-04-13 11:01:59 -0400 | [diff] [blame] | 2817 | #elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) |
| 2818 | result = this->stepWithTBTableData(); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2819 | #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2820 | result = this->stepWithDwarfFDE(); |
Ranjeet Singh | 421231a | 2017-03-31 15:28:06 +0000 | [diff] [blame] | 2821 | #elif defined(_LIBUNWIND_ARM_EHABI) |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2822 | result = this->stepWithEHABI(); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2823 | #else |
| 2824 | #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \ |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 2825 | _LIBUNWIND_SUPPORT_SEH_UNWIND or \ |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2826 | _LIBUNWIND_SUPPORT_DWARF_UNWIND or \ |
Logan Chien | 06b0c7a | 2015-07-19 15:23:10 +0000 | [diff] [blame] | 2827 | _LIBUNWIND_ARM_EHABI |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2828 | #endif |
Ryan Prichard | a684bed | 2021-01-13 16:38:36 -0800 | [diff] [blame] | 2829 | } |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2830 | |
| 2831 | // update info based on new PC |
| 2832 | if (result == UNW_STEP_SUCCESS) { |
| 2833 | this->setInfoBasedOnIPRegister(true); |
| 2834 | if (_unwindInfoMissing) |
| 2835 | return UNW_STEP_END; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | return result; |
| 2839 | } |
| 2840 | |
| 2841 | template <typename A, typename R> |
| 2842 | void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) { |
Saleem Abdulrasool | 78b42cc | 2019-09-20 15:53:42 +0000 | [diff] [blame] | 2843 | if (_unwindInfoMissing) |
| 2844 | memset(info, 0, sizeof(*info)); |
| 2845 | else |
| 2846 | *info = _info; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | template <typename A, typename R> |
| 2850 | bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen, |
| 2851 | unw_word_t *offset) { |
| 2852 | return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP), |
| 2853 | buf, bufLen, offset); |
| 2854 | } |
| 2855 | |
gejin | 7f49316 | 2021-08-26 16:20:38 +0800 | [diff] [blame] | 2856 | #if defined(_LIBUNWIND_USE_CET) |
| 2857 | extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) { |
| 2858 | AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor; |
| 2859 | return co->get_registers(); |
| 2860 | } |
| 2861 | #endif |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 2862 | } // namespace libunwind |
| 2863 | |
| 2864 | #endif // __UNWINDCURSOR_HPP__ |