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