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