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