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