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