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