xref: /freebsd/contrib/llvm-project/libunwind/src/UnwindCursor.hpp (revision 1fd87a682ad7442327078e1eeb63edc4258f9815)
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 
28 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
29 // Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
30 // earlier) SDKs.
31 // MinGW-w64 has always provided this struct.
32   #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
33       !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
34 struct _DISPATCHER_CONTEXT {
35   ULONG64 ControlPc;
36   ULONG64 ImageBase;
37   PRUNTIME_FUNCTION FunctionEntry;
38   ULONG64 EstablisherFrame;
39   ULONG64 TargetIp;
40   PCONTEXT ContextRecord;
41   PEXCEPTION_ROUTINE LanguageHandler;
42   PVOID HandlerData;
43   PUNWIND_HISTORY_TABLE HistoryTable;
44   ULONG ScopeIndex;
45   ULONG Fill0;
46 };
47   #endif
48 
49 struct UNWIND_INFO {
50   uint8_t Version : 3;
51   uint8_t Flags : 5;
52   uint8_t SizeOfProlog;
53   uint8_t CountOfCodes;
54   uint8_t FrameRegister : 4;
55   uint8_t FrameOffset : 4;
56   uint16_t UnwindCodes[2];
57 };
58 
59 extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
60     int, _Unwind_Action, uint64_t, _Unwind_Exception *,
61     struct _Unwind_Context *);
62 
63 #endif
64 
65 #include "config.h"
66 
67 #include "AddressSpace.hpp"
68 #include "CompactUnwinder.hpp"
69 #include "config.h"
70 #include "DwarfInstructions.hpp"
71 #include "EHHeaderParser.hpp"
72 #include "libunwind.h"
73 #include "Registers.hpp"
74 #include "RWMutex.hpp"
75 #include "Unwind-EHABI.h"
76 
77 namespace libunwind {
78 
79 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
80 /// Cache of recently found FDEs.
81 template <typename A>
82 class _LIBUNWIND_HIDDEN DwarfFDECache {
83   typedef typename A::pint_t pint_t;
84 public:
85   static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
86   static pint_t findFDE(pint_t mh, pint_t pc);
87   static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
88   static void removeAllIn(pint_t mh);
89   static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
90                                                unw_word_t ip_end,
91                                                unw_word_t fde, unw_word_t mh));
92 
93 private:
94 
95   struct entry {
96     pint_t mh;
97     pint_t ip_start;
98     pint_t ip_end;
99     pint_t fde;
100   };
101 
102   // These fields are all static to avoid needing an initializer.
103   // There is only one instance of this class per process.
104   static RWMutex _lock;
105 #ifdef __APPLE__
106   static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
107   static bool _registeredForDyldUnloads;
108 #endif
109   static entry *_buffer;
110   static entry *_bufferUsed;
111   static entry *_bufferEnd;
112   static entry _initialBuffer[64];
113 };
114 
115 template <typename A>
116 typename DwarfFDECache<A>::entry *
117 DwarfFDECache<A>::_buffer = _initialBuffer;
118 
119 template <typename A>
120 typename DwarfFDECache<A>::entry *
121 DwarfFDECache<A>::_bufferUsed = _initialBuffer;
122 
123 template <typename A>
124 typename DwarfFDECache<A>::entry *
125 DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
126 
127 template <typename A>
128 typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
129 
130 template <typename A>
131 RWMutex DwarfFDECache<A>::_lock;
132 
133 #ifdef __APPLE__
134 template <typename A>
135 bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
136 #endif
137 
138 template <typename A>
139 typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
140   pint_t result = 0;
141   _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
142   for (entry *p = _buffer; p < _bufferUsed; ++p) {
143     if ((mh == p->mh) || (mh == kSearchAll)) {
144       if ((p->ip_start <= pc) && (pc < p->ip_end)) {
145         result = p->fde;
146         break;
147       }
148     }
149   }
150   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
151   return result;
152 }
153 
154 template <typename A>
155 void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
156                            pint_t fde) {
157 #if !defined(_LIBUNWIND_NO_HEAP)
158   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
159   if (_bufferUsed >= _bufferEnd) {
160     size_t oldSize = (size_t)(_bufferEnd - _buffer);
161     size_t newSize = oldSize * 4;
162     // Can't use operator new (we are below it).
163     entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
164     memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
165     if (_buffer != _initialBuffer)
166       free(_buffer);
167     _buffer = newBuffer;
168     _bufferUsed = &newBuffer[oldSize];
169     _bufferEnd = &newBuffer[newSize];
170   }
171   _bufferUsed->mh = mh;
172   _bufferUsed->ip_start = ip_start;
173   _bufferUsed->ip_end = ip_end;
174   _bufferUsed->fde = fde;
175   ++_bufferUsed;
176 #ifdef __APPLE__
177   if (!_registeredForDyldUnloads) {
178     _dyld_register_func_for_remove_image(&dyldUnloadHook);
179     _registeredForDyldUnloads = true;
180   }
181 #endif
182   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
183 #endif
184 }
185 
186 template <typename A>
187 void DwarfFDECache<A>::removeAllIn(pint_t mh) {
188   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
189   entry *d = _buffer;
190   for (const entry *s = _buffer; s < _bufferUsed; ++s) {
191     if (s->mh != mh) {
192       if (d != s)
193         *d = *s;
194       ++d;
195     }
196   }
197   _bufferUsed = d;
198   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
199 }
200 
201 #ifdef __APPLE__
202 template <typename A>
203 void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
204   removeAllIn((pint_t) mh);
205 }
206 #endif
207 
208 template <typename A>
209 void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
210     unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
211   _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
212   for (entry *p = _buffer; p < _bufferUsed; ++p) {
213     (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
214   }
215   _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
216 }
217 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
218 
219 
220 #define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
221 
222 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
223 template <typename A> class UnwindSectionHeader {
224 public:
225   UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
226       : _addressSpace(addressSpace), _addr(addr) {}
227 
228   uint32_t version() const {
229     return _addressSpace.get32(_addr +
230                                offsetof(unwind_info_section_header, version));
231   }
232   uint32_t commonEncodingsArraySectionOffset() const {
233     return _addressSpace.get32(_addr +
234                                offsetof(unwind_info_section_header,
235                                         commonEncodingsArraySectionOffset));
236   }
237   uint32_t commonEncodingsArrayCount() const {
238     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
239                                                 commonEncodingsArrayCount));
240   }
241   uint32_t personalityArraySectionOffset() const {
242     return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
243                                                 personalityArraySectionOffset));
244   }
245   uint32_t personalityArrayCount() const {
246     return _addressSpace.get32(
247         _addr + offsetof(unwind_info_section_header, personalityArrayCount));
248   }
249   uint32_t indexSectionOffset() const {
250     return _addressSpace.get32(
251         _addr + offsetof(unwind_info_section_header, indexSectionOffset));
252   }
253   uint32_t indexCount() const {
254     return _addressSpace.get32(
255         _addr + offsetof(unwind_info_section_header, indexCount));
256   }
257 
258 private:
259   A                     &_addressSpace;
260   typename A::pint_t     _addr;
261 };
262 
263 template <typename A> class UnwindSectionIndexArray {
264 public:
265   UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
266       : _addressSpace(addressSpace), _addr(addr) {}
267 
268   uint32_t functionOffset(uint32_t index) const {
269     return _addressSpace.get32(
270         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
271                               functionOffset));
272   }
273   uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
274     return _addressSpace.get32(
275         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
276                               secondLevelPagesSectionOffset));
277   }
278   uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
279     return _addressSpace.get32(
280         _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
281                               lsdaIndexArraySectionOffset));
282   }
283 
284 private:
285   A                   &_addressSpace;
286   typename A::pint_t   _addr;
287 };
288 
289 template <typename A> class UnwindSectionRegularPageHeader {
290 public:
291   UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
292       : _addressSpace(addressSpace), _addr(addr) {}
293 
294   uint32_t kind() const {
295     return _addressSpace.get32(
296         _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
297   }
298   uint16_t entryPageOffset() const {
299     return _addressSpace.get16(
300         _addr + offsetof(unwind_info_regular_second_level_page_header,
301                          entryPageOffset));
302   }
303   uint16_t entryCount() const {
304     return _addressSpace.get16(
305         _addr +
306         offsetof(unwind_info_regular_second_level_page_header, entryCount));
307   }
308 
309 private:
310   A &_addressSpace;
311   typename A::pint_t _addr;
312 };
313 
314 template <typename A> class UnwindSectionRegularArray {
315 public:
316   UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
317       : _addressSpace(addressSpace), _addr(addr) {}
318 
319   uint32_t functionOffset(uint32_t index) const {
320     return _addressSpace.get32(
321         _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
322                               functionOffset));
323   }
324   uint32_t encoding(uint32_t index) const {
325     return _addressSpace.get32(
326         _addr +
327         arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
328   }
329 
330 private:
331   A &_addressSpace;
332   typename A::pint_t _addr;
333 };
334 
335 template <typename A> class UnwindSectionCompressedPageHeader {
336 public:
337   UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
338       : _addressSpace(addressSpace), _addr(addr) {}
339 
340   uint32_t kind() const {
341     return _addressSpace.get32(
342         _addr +
343         offsetof(unwind_info_compressed_second_level_page_header, kind));
344   }
345   uint16_t entryPageOffset() const {
346     return _addressSpace.get16(
347         _addr + offsetof(unwind_info_compressed_second_level_page_header,
348                          entryPageOffset));
349   }
350   uint16_t entryCount() const {
351     return _addressSpace.get16(
352         _addr +
353         offsetof(unwind_info_compressed_second_level_page_header, entryCount));
354   }
355   uint16_t encodingsPageOffset() const {
356     return _addressSpace.get16(
357         _addr + offsetof(unwind_info_compressed_second_level_page_header,
358                          encodingsPageOffset));
359   }
360   uint16_t encodingsCount() const {
361     return _addressSpace.get16(
362         _addr + offsetof(unwind_info_compressed_second_level_page_header,
363                          encodingsCount));
364   }
365 
366 private:
367   A &_addressSpace;
368   typename A::pint_t _addr;
369 };
370 
371 template <typename A> class UnwindSectionCompressedArray {
372 public:
373   UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
374       : _addressSpace(addressSpace), _addr(addr) {}
375 
376   uint32_t functionOffset(uint32_t index) const {
377     return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
378         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
379   }
380   uint16_t encodingIndex(uint32_t index) const {
381     return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
382         _addressSpace.get32(_addr + index * sizeof(uint32_t)));
383   }
384 
385 private:
386   A &_addressSpace;
387   typename A::pint_t _addr;
388 };
389 
390 template <typename A> class UnwindSectionLsdaArray {
391 public:
392   UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
393       : _addressSpace(addressSpace), _addr(addr) {}
394 
395   uint32_t functionOffset(uint32_t index) const {
396     return _addressSpace.get32(
397         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
398                               index, functionOffset));
399   }
400   uint32_t lsdaOffset(uint32_t index) const {
401     return _addressSpace.get32(
402         _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
403                               index, lsdaOffset));
404   }
405 
406 private:
407   A                   &_addressSpace;
408   typename A::pint_t   _addr;
409 };
410 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
411 
412 class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
413 public:
414   // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
415   // This avoids an unnecessary dependency to libc++abi.
416   void operator delete(void *, size_t) {}
417 
418   virtual ~AbstractUnwindCursor() {}
419   virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
420   virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
421   virtual void setReg(int, unw_word_t) {
422     _LIBUNWIND_ABORT("setReg not implemented");
423   }
424   virtual bool validFloatReg(int) {
425     _LIBUNWIND_ABORT("validFloatReg not implemented");
426   }
427   virtual unw_fpreg_t getFloatReg(int) {
428     _LIBUNWIND_ABORT("getFloatReg not implemented");
429   }
430   virtual void setFloatReg(int, unw_fpreg_t) {
431     _LIBUNWIND_ABORT("setFloatReg not implemented");
432   }
433   virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
434   virtual void getInfo(unw_proc_info_t *) {
435     _LIBUNWIND_ABORT("getInfo not implemented");
436   }
437   virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
438   virtual bool isSignalFrame() {
439     _LIBUNWIND_ABORT("isSignalFrame not implemented");
440   }
441   virtual bool getFunctionName(char *, size_t, unw_word_t *) {
442     _LIBUNWIND_ABORT("getFunctionName not implemented");
443   }
444   virtual void setInfoBasedOnIPRegister(bool = false) {
445     _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
446   }
447   virtual const char *getRegisterName(int) {
448     _LIBUNWIND_ABORT("getRegisterName not implemented");
449   }
450 #ifdef __arm__
451   virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
452 #endif
453 
454 #if defined(_LIBUNWIND_USE_CET)
455   virtual void *get_registers() {
456     _LIBUNWIND_ABORT("get_registers not implemented");
457   }
458 #endif
459 };
460 
461 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
462 
463 /// \c UnwindCursor contains all state (including all register values) during
464 /// an unwind.  This is normally stack-allocated inside a unw_cursor_t.
465 template <typename A, typename R>
466 class UnwindCursor : public AbstractUnwindCursor {
467   typedef typename A::pint_t pint_t;
468 public:
469                       UnwindCursor(unw_context_t *context, A &as);
470                       UnwindCursor(CONTEXT *context, A &as);
471                       UnwindCursor(A &as, void *threadArg);
472   virtual             ~UnwindCursor() {}
473   virtual bool        validReg(int);
474   virtual unw_word_t  getReg(int);
475   virtual void        setReg(int, unw_word_t);
476   virtual bool        validFloatReg(int);
477   virtual unw_fpreg_t getFloatReg(int);
478   virtual void        setFloatReg(int, unw_fpreg_t);
479   virtual int         step();
480   virtual void        getInfo(unw_proc_info_t *);
481   virtual void        jumpto();
482   virtual bool        isSignalFrame();
483   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
484   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
485   virtual const char *getRegisterName(int num);
486 #ifdef __arm__
487   virtual void        saveVFPAsX();
488 #endif
489 
490   DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
491   void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
492 
493   // libunwind does not and should not depend on C++ library which means that we
494   // need our own defition of inline placement new.
495   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
496 
497 private:
498 
499   pint_t getLastPC() const { return _dispContext.ControlPc; }
500   void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
501   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
502     _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
503                                                         &_dispContext.ImageBase,
504                                                         _dispContext.HistoryTable);
505     *base = _dispContext.ImageBase;
506     return _dispContext.FunctionEntry;
507   }
508   bool getInfoFromSEH(pint_t pc);
509   int stepWithSEHData() {
510     _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
511                                                     _dispContext.ImageBase,
512                                                     _dispContext.ControlPc,
513                                                     _dispContext.FunctionEntry,
514                                                     _dispContext.ContextRecord,
515                                                     &_dispContext.HandlerData,
516                                                     &_dispContext.EstablisherFrame,
517                                                     NULL);
518     // Update some fields of the unwind info now, since we have them.
519     _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
520     if (_dispContext.LanguageHandler) {
521       _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
522     } else
523       _info.handler = 0;
524     return UNW_STEP_SUCCESS;
525   }
526 
527   A                   &_addressSpace;
528   unw_proc_info_t      _info;
529   DISPATCHER_CONTEXT   _dispContext;
530   CONTEXT              _msContext;
531   UNWIND_HISTORY_TABLE _histTable;
532   bool                 _unwindInfoMissing;
533 };
534 
535 
536 template <typename A, typename R>
537 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
538     : _addressSpace(as), _unwindInfoMissing(false) {
539   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
540                 "UnwindCursor<> does not fit in unw_cursor_t");
541   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
542                 "UnwindCursor<> requires more alignment than unw_cursor_t");
543   memset(&_info, 0, sizeof(_info));
544   memset(&_histTable, 0, sizeof(_histTable));
545   _dispContext.ContextRecord = &_msContext;
546   _dispContext.HistoryTable = &_histTable;
547   // Initialize MS context from ours.
548   R r(context);
549   _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
550 #if defined(_LIBUNWIND_TARGET_X86_64)
551   _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
552   _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
553   _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
554   _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
555   _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
556   _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
557   _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
558   _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
559   _msContext.R8 = r.getRegister(UNW_X86_64_R8);
560   _msContext.R9 = r.getRegister(UNW_X86_64_R9);
561   _msContext.R10 = r.getRegister(UNW_X86_64_R10);
562   _msContext.R11 = r.getRegister(UNW_X86_64_R11);
563   _msContext.R12 = r.getRegister(UNW_X86_64_R12);
564   _msContext.R13 = r.getRegister(UNW_X86_64_R13);
565   _msContext.R14 = r.getRegister(UNW_X86_64_R14);
566   _msContext.R15 = r.getRegister(UNW_X86_64_R15);
567   _msContext.Rip = r.getRegister(UNW_REG_IP);
568   union {
569     v128 v;
570     M128A m;
571   } t;
572   t.v = r.getVectorRegister(UNW_X86_64_XMM0);
573   _msContext.Xmm0 = t.m;
574   t.v = r.getVectorRegister(UNW_X86_64_XMM1);
575   _msContext.Xmm1 = t.m;
576   t.v = r.getVectorRegister(UNW_X86_64_XMM2);
577   _msContext.Xmm2 = t.m;
578   t.v = r.getVectorRegister(UNW_X86_64_XMM3);
579   _msContext.Xmm3 = t.m;
580   t.v = r.getVectorRegister(UNW_X86_64_XMM4);
581   _msContext.Xmm4 = t.m;
582   t.v = r.getVectorRegister(UNW_X86_64_XMM5);
583   _msContext.Xmm5 = t.m;
584   t.v = r.getVectorRegister(UNW_X86_64_XMM6);
585   _msContext.Xmm6 = t.m;
586   t.v = r.getVectorRegister(UNW_X86_64_XMM7);
587   _msContext.Xmm7 = t.m;
588   t.v = r.getVectorRegister(UNW_X86_64_XMM8);
589   _msContext.Xmm8 = t.m;
590   t.v = r.getVectorRegister(UNW_X86_64_XMM9);
591   _msContext.Xmm9 = t.m;
592   t.v = r.getVectorRegister(UNW_X86_64_XMM10);
593   _msContext.Xmm10 = t.m;
594   t.v = r.getVectorRegister(UNW_X86_64_XMM11);
595   _msContext.Xmm11 = t.m;
596   t.v = r.getVectorRegister(UNW_X86_64_XMM12);
597   _msContext.Xmm12 = t.m;
598   t.v = r.getVectorRegister(UNW_X86_64_XMM13);
599   _msContext.Xmm13 = t.m;
600   t.v = r.getVectorRegister(UNW_X86_64_XMM14);
601   _msContext.Xmm14 = t.m;
602   t.v = r.getVectorRegister(UNW_X86_64_XMM15);
603   _msContext.Xmm15 = t.m;
604 #elif defined(_LIBUNWIND_TARGET_ARM)
605   _msContext.R0 = r.getRegister(UNW_ARM_R0);
606   _msContext.R1 = r.getRegister(UNW_ARM_R1);
607   _msContext.R2 = r.getRegister(UNW_ARM_R2);
608   _msContext.R3 = r.getRegister(UNW_ARM_R3);
609   _msContext.R4 = r.getRegister(UNW_ARM_R4);
610   _msContext.R5 = r.getRegister(UNW_ARM_R5);
611   _msContext.R6 = r.getRegister(UNW_ARM_R6);
612   _msContext.R7 = r.getRegister(UNW_ARM_R7);
613   _msContext.R8 = r.getRegister(UNW_ARM_R8);
614   _msContext.R9 = r.getRegister(UNW_ARM_R9);
615   _msContext.R10 = r.getRegister(UNW_ARM_R10);
616   _msContext.R11 = r.getRegister(UNW_ARM_R11);
617   _msContext.R12 = r.getRegister(UNW_ARM_R12);
618   _msContext.Sp = r.getRegister(UNW_ARM_SP);
619   _msContext.Lr = r.getRegister(UNW_ARM_LR);
620   _msContext.Pc = r.getRegister(UNW_ARM_IP);
621   for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
622     union {
623       uint64_t w;
624       double d;
625     } d;
626     d.d = r.getFloatRegister(i);
627     _msContext.D[i - UNW_ARM_D0] = d.w;
628   }
629 #elif defined(_LIBUNWIND_TARGET_AARCH64)
630   for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
631     _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
632   _msContext.Sp = r.getRegister(UNW_REG_SP);
633   _msContext.Pc = r.getRegister(UNW_REG_IP);
634   for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
635     _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
636 #endif
637 }
638 
639 template <typename A, typename R>
640 UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
641     : _addressSpace(as), _unwindInfoMissing(false) {
642   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
643                 "UnwindCursor<> does not fit in unw_cursor_t");
644   memset(&_info, 0, sizeof(_info));
645   memset(&_histTable, 0, sizeof(_histTable));
646   _dispContext.ContextRecord = &_msContext;
647   _dispContext.HistoryTable = &_histTable;
648   _msContext = *context;
649 }
650 
651 
652 template <typename A, typename R>
653 bool UnwindCursor<A, R>::validReg(int regNum) {
654   if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
655 #if defined(_LIBUNWIND_TARGET_X86_64)
656   if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
657 #elif defined(_LIBUNWIND_TARGET_ARM)
658   if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
659       regNum == UNW_ARM_RA_AUTH_CODE)
660     return true;
661 #elif defined(_LIBUNWIND_TARGET_AARCH64)
662   if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
663 #endif
664   return false;
665 }
666 
667 template <typename A, typename R>
668 unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
669   switch (regNum) {
670 #if defined(_LIBUNWIND_TARGET_X86_64)
671   case UNW_REG_IP: return _msContext.Rip;
672   case UNW_X86_64_RAX: return _msContext.Rax;
673   case UNW_X86_64_RDX: return _msContext.Rdx;
674   case UNW_X86_64_RCX: return _msContext.Rcx;
675   case UNW_X86_64_RBX: return _msContext.Rbx;
676   case UNW_REG_SP:
677   case UNW_X86_64_RSP: return _msContext.Rsp;
678   case UNW_X86_64_RBP: return _msContext.Rbp;
679   case UNW_X86_64_RSI: return _msContext.Rsi;
680   case UNW_X86_64_RDI: return _msContext.Rdi;
681   case UNW_X86_64_R8: return _msContext.R8;
682   case UNW_X86_64_R9: return _msContext.R9;
683   case UNW_X86_64_R10: return _msContext.R10;
684   case UNW_X86_64_R11: return _msContext.R11;
685   case UNW_X86_64_R12: return _msContext.R12;
686   case UNW_X86_64_R13: return _msContext.R13;
687   case UNW_X86_64_R14: return _msContext.R14;
688   case UNW_X86_64_R15: return _msContext.R15;
689 #elif defined(_LIBUNWIND_TARGET_ARM)
690   case UNW_ARM_R0: return _msContext.R0;
691   case UNW_ARM_R1: return _msContext.R1;
692   case UNW_ARM_R2: return _msContext.R2;
693   case UNW_ARM_R3: return _msContext.R3;
694   case UNW_ARM_R4: return _msContext.R4;
695   case UNW_ARM_R5: return _msContext.R5;
696   case UNW_ARM_R6: return _msContext.R6;
697   case UNW_ARM_R7: return _msContext.R7;
698   case UNW_ARM_R8: return _msContext.R8;
699   case UNW_ARM_R9: return _msContext.R9;
700   case UNW_ARM_R10: return _msContext.R10;
701   case UNW_ARM_R11: return _msContext.R11;
702   case UNW_ARM_R12: return _msContext.R12;
703   case UNW_REG_SP:
704   case UNW_ARM_SP: return _msContext.Sp;
705   case UNW_ARM_LR: return _msContext.Lr;
706   case UNW_REG_IP:
707   case UNW_ARM_IP: return _msContext.Pc;
708 #elif defined(_LIBUNWIND_TARGET_AARCH64)
709   case UNW_REG_SP: return _msContext.Sp;
710   case UNW_REG_IP: return _msContext.Pc;
711   default: return _msContext.X[regNum - UNW_AARCH64_X0];
712 #endif
713   }
714   _LIBUNWIND_ABORT("unsupported register");
715 }
716 
717 template <typename A, typename R>
718 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
719   switch (regNum) {
720 #if defined(_LIBUNWIND_TARGET_X86_64)
721   case UNW_REG_IP: _msContext.Rip = value; break;
722   case UNW_X86_64_RAX: _msContext.Rax = value; break;
723   case UNW_X86_64_RDX: _msContext.Rdx = value; break;
724   case UNW_X86_64_RCX: _msContext.Rcx = value; break;
725   case UNW_X86_64_RBX: _msContext.Rbx = value; break;
726   case UNW_REG_SP:
727   case UNW_X86_64_RSP: _msContext.Rsp = value; break;
728   case UNW_X86_64_RBP: _msContext.Rbp = value; break;
729   case UNW_X86_64_RSI: _msContext.Rsi = value; break;
730   case UNW_X86_64_RDI: _msContext.Rdi = value; break;
731   case UNW_X86_64_R8: _msContext.R8 = value; break;
732   case UNW_X86_64_R9: _msContext.R9 = value; break;
733   case UNW_X86_64_R10: _msContext.R10 = value; break;
734   case UNW_X86_64_R11: _msContext.R11 = value; break;
735   case UNW_X86_64_R12: _msContext.R12 = value; break;
736   case UNW_X86_64_R13: _msContext.R13 = value; break;
737   case UNW_X86_64_R14: _msContext.R14 = value; break;
738   case UNW_X86_64_R15: _msContext.R15 = value; break;
739 #elif defined(_LIBUNWIND_TARGET_ARM)
740   case UNW_ARM_R0: _msContext.R0 = value; break;
741   case UNW_ARM_R1: _msContext.R1 = value; break;
742   case UNW_ARM_R2: _msContext.R2 = value; break;
743   case UNW_ARM_R3: _msContext.R3 = value; break;
744   case UNW_ARM_R4: _msContext.R4 = value; break;
745   case UNW_ARM_R5: _msContext.R5 = value; break;
746   case UNW_ARM_R6: _msContext.R6 = value; break;
747   case UNW_ARM_R7: _msContext.R7 = value; break;
748   case UNW_ARM_R8: _msContext.R8 = value; break;
749   case UNW_ARM_R9: _msContext.R9 = value; break;
750   case UNW_ARM_R10: _msContext.R10 = value; break;
751   case UNW_ARM_R11: _msContext.R11 = value; break;
752   case UNW_ARM_R12: _msContext.R12 = value; break;
753   case UNW_REG_SP:
754   case UNW_ARM_SP: _msContext.Sp = value; break;
755   case UNW_ARM_LR: _msContext.Lr = value; break;
756   case UNW_REG_IP:
757   case UNW_ARM_IP: _msContext.Pc = value; break;
758 #elif defined(_LIBUNWIND_TARGET_AARCH64)
759   case UNW_REG_SP: _msContext.Sp = value; break;
760   case UNW_REG_IP: _msContext.Pc = value; break;
761   case UNW_AARCH64_X0:
762   case UNW_AARCH64_X1:
763   case UNW_AARCH64_X2:
764   case UNW_AARCH64_X3:
765   case UNW_AARCH64_X4:
766   case UNW_AARCH64_X5:
767   case UNW_AARCH64_X6:
768   case UNW_AARCH64_X7:
769   case UNW_AARCH64_X8:
770   case UNW_AARCH64_X9:
771   case UNW_AARCH64_X10:
772   case UNW_AARCH64_X11:
773   case UNW_AARCH64_X12:
774   case UNW_AARCH64_X13:
775   case UNW_AARCH64_X14:
776   case UNW_AARCH64_X15:
777   case UNW_AARCH64_X16:
778   case UNW_AARCH64_X17:
779   case UNW_AARCH64_X18:
780   case UNW_AARCH64_X19:
781   case UNW_AARCH64_X20:
782   case UNW_AARCH64_X21:
783   case UNW_AARCH64_X22:
784   case UNW_AARCH64_X23:
785   case UNW_AARCH64_X24:
786   case UNW_AARCH64_X25:
787   case UNW_AARCH64_X26:
788   case UNW_AARCH64_X27:
789   case UNW_AARCH64_X28:
790   case UNW_AARCH64_FP:
791   case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
792 #endif
793   default:
794     _LIBUNWIND_ABORT("unsupported register");
795   }
796 }
797 
798 template <typename A, typename R>
799 bool UnwindCursor<A, R>::validFloatReg(int regNum) {
800 #if defined(_LIBUNWIND_TARGET_ARM)
801   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
802   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
803 #elif defined(_LIBUNWIND_TARGET_AARCH64)
804   if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
805 #else
806   (void)regNum;
807 #endif
808   return false;
809 }
810 
811 template <typename A, typename R>
812 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
813 #if defined(_LIBUNWIND_TARGET_ARM)
814   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
815     union {
816       uint32_t w;
817       float f;
818     } d;
819     d.w = _msContext.S[regNum - UNW_ARM_S0];
820     return d.f;
821   }
822   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
823     union {
824       uint64_t w;
825       double d;
826     } d;
827     d.w = _msContext.D[regNum - UNW_ARM_D0];
828     return d.d;
829   }
830   _LIBUNWIND_ABORT("unsupported float register");
831 #elif defined(_LIBUNWIND_TARGET_AARCH64)
832   return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
833 #else
834   (void)regNum;
835   _LIBUNWIND_ABORT("float registers unimplemented");
836 #endif
837 }
838 
839 template <typename A, typename R>
840 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
841 #if defined(_LIBUNWIND_TARGET_ARM)
842   if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
843     union {
844       uint32_t w;
845       float f;
846     } d;
847     d.f = value;
848     _msContext.S[regNum - UNW_ARM_S0] = d.w;
849   }
850   if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
851     union {
852       uint64_t w;
853       double d;
854     } d;
855     d.d = value;
856     _msContext.D[regNum - UNW_ARM_D0] = d.w;
857   }
858   _LIBUNWIND_ABORT("unsupported float register");
859 #elif defined(_LIBUNWIND_TARGET_AARCH64)
860   _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
861 #else
862   (void)regNum;
863   (void)value;
864   _LIBUNWIND_ABORT("float registers unimplemented");
865 #endif
866 }
867 
868 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
869   RtlRestoreContext(&_msContext, nullptr);
870 }
871 
872 #ifdef __arm__
873 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
874 #endif
875 
876 template <typename A, typename R>
877 const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
878   return R::getRegisterName(regNum);
879 }
880 
881 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
882   return false;
883 }
884 
885 #else  // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
886 
887 /// UnwindCursor contains all state (including all register values) during
888 /// an unwind.  This is normally stack allocated inside a unw_cursor_t.
889 template <typename A, typename R>
890 class UnwindCursor : public AbstractUnwindCursor{
891   typedef typename A::pint_t pint_t;
892 public:
893                       UnwindCursor(unw_context_t *context, A &as);
894                       UnwindCursor(A &as, void *threadArg);
895   virtual             ~UnwindCursor() {}
896   virtual bool        validReg(int);
897   virtual unw_word_t  getReg(int);
898   virtual void        setReg(int, unw_word_t);
899   virtual bool        validFloatReg(int);
900   virtual unw_fpreg_t getFloatReg(int);
901   virtual void        setFloatReg(int, unw_fpreg_t);
902   virtual int         step();
903   virtual void        getInfo(unw_proc_info_t *);
904   virtual void        jumpto();
905   virtual bool        isSignalFrame();
906   virtual bool        getFunctionName(char *buf, size_t len, unw_word_t *off);
907   virtual void        setInfoBasedOnIPRegister(bool isReturnAddress = false);
908   virtual const char *getRegisterName(int num);
909 #ifdef __arm__
910   virtual void        saveVFPAsX();
911 #endif
912 
913 #if defined(_LIBUNWIND_USE_CET)
914   virtual void *get_registers() { return &_registers; }
915 #endif
916   // libunwind does not and should not depend on C++ library which means that we
917   // need our own defition of inline placement new.
918   static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
919 
920 private:
921 
922 #if defined(_LIBUNWIND_ARM_EHABI)
923   bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
924 
925   int stepWithEHABI() {
926     size_t len = 0;
927     size_t off = 0;
928     // FIXME: Calling decode_eht_entry() here is violating the libunwind
929     // abstraction layer.
930     const uint32_t *ehtp =
931         decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
932                          &off, &len);
933     if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
934             _URC_CONTINUE_UNWIND)
935       return UNW_STEP_END;
936     return UNW_STEP_SUCCESS;
937   }
938 #endif
939 
940 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
941   bool setInfoForSigReturn() {
942     R dummy;
943     return setInfoForSigReturn(dummy);
944   }
945   int stepThroughSigReturn() {
946     R dummy;
947     return stepThroughSigReturn(dummy);
948   }
949   bool setInfoForSigReturn(Registers_arm64 &);
950   int stepThroughSigReturn(Registers_arm64 &);
951   template <typename Registers> bool setInfoForSigReturn(Registers &) {
952     return false;
953   }
954   template <typename Registers> int stepThroughSigReturn(Registers &) {
955     return UNW_STEP_END;
956   }
957 #endif
958 
959 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
960   bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
961                          const typename CFI_Parser<A>::CIE_Info &cieInfo,
962                          pint_t pc, uintptr_t dso_base);
963   bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
964                                             uint32_t fdeSectionOffsetHint=0);
965   int stepWithDwarfFDE() {
966     return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
967                                               (pint_t)this->getReg(UNW_REG_IP),
968                                               (pint_t)_info.unwind_info,
969                                               _registers, _isSignalFrame);
970   }
971 #endif
972 
973 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
974   bool getInfoFromCompactEncodingSection(pint_t pc,
975                                             const UnwindInfoSections &sects);
976   int stepWithCompactEncoding() {
977   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
978     if ( compactSaysUseDwarf() )
979       return stepWithDwarfFDE();
980   #endif
981     R dummy;
982     return stepWithCompactEncoding(dummy);
983   }
984 
985 #if defined(_LIBUNWIND_TARGET_X86_64)
986   int stepWithCompactEncoding(Registers_x86_64 &) {
987     return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
988         _info.format, _info.start_ip, _addressSpace, _registers);
989   }
990 #endif
991 
992 #if defined(_LIBUNWIND_TARGET_I386)
993   int stepWithCompactEncoding(Registers_x86 &) {
994     return CompactUnwinder_x86<A>::stepWithCompactEncoding(
995         _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
996   }
997 #endif
998 
999 #if defined(_LIBUNWIND_TARGET_PPC)
1000   int stepWithCompactEncoding(Registers_ppc &) {
1001     return UNW_EINVAL;
1002   }
1003 #endif
1004 
1005 #if defined(_LIBUNWIND_TARGET_PPC64)
1006   int stepWithCompactEncoding(Registers_ppc64 &) {
1007     return UNW_EINVAL;
1008   }
1009 #endif
1010 
1011 
1012 #if defined(_LIBUNWIND_TARGET_AARCH64)
1013   int stepWithCompactEncoding(Registers_arm64 &) {
1014     return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
1015         _info.format, _info.start_ip, _addressSpace, _registers);
1016   }
1017 #endif
1018 
1019 #if defined(_LIBUNWIND_TARGET_MIPS_O32)
1020   int stepWithCompactEncoding(Registers_mips_o32 &) {
1021     return UNW_EINVAL;
1022   }
1023 #endif
1024 
1025 #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1026   int stepWithCompactEncoding(Registers_mips_newabi &) {
1027     return UNW_EINVAL;
1028   }
1029 #endif
1030 
1031 #if defined(_LIBUNWIND_TARGET_SPARC)
1032   int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1033 #endif
1034 
1035 #if defined (_LIBUNWIND_TARGET_RISCV)
1036   int stepWithCompactEncoding(Registers_riscv &) {
1037     return UNW_EINVAL;
1038   }
1039 #endif
1040 
1041   bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1042     R dummy;
1043     return compactSaysUseDwarf(dummy, offset);
1044   }
1045 
1046 #if defined(_LIBUNWIND_TARGET_X86_64)
1047   bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1048     if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1049       if (offset)
1050         *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1051       return true;
1052     }
1053     return false;
1054   }
1055 #endif
1056 
1057 #if defined(_LIBUNWIND_TARGET_I386)
1058   bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1059     if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1060       if (offset)
1061         *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1062       return true;
1063     }
1064     return false;
1065   }
1066 #endif
1067 
1068 #if defined(_LIBUNWIND_TARGET_PPC)
1069   bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1070     return true;
1071   }
1072 #endif
1073 
1074 #if defined(_LIBUNWIND_TARGET_PPC64)
1075   bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1076     return true;
1077   }
1078 #endif
1079 
1080 #if defined(_LIBUNWIND_TARGET_AARCH64)
1081   bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1082     if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1083       if (offset)
1084         *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1085       return true;
1086     }
1087     return false;
1088   }
1089 #endif
1090 
1091 #if defined(_LIBUNWIND_TARGET_MIPS_O32)
1092   bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1093     return true;
1094   }
1095 #endif
1096 
1097 #if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1098   bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
1099     return true;
1100   }
1101 #endif
1102 
1103 #if defined(_LIBUNWIND_TARGET_SPARC)
1104   bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1105 #endif
1106 
1107 #if defined (_LIBUNWIND_TARGET_RISCV)
1108   bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1109     return true;
1110   }
1111 #endif
1112 
1113 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
1114 
1115 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1116   compact_unwind_encoding_t dwarfEncoding() const {
1117     R dummy;
1118     return dwarfEncoding(dummy);
1119   }
1120 
1121 #if defined(_LIBUNWIND_TARGET_X86_64)
1122   compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1123     return UNWIND_X86_64_MODE_DWARF;
1124   }
1125 #endif
1126 
1127 #if defined(_LIBUNWIND_TARGET_I386)
1128   compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1129     return UNWIND_X86_MODE_DWARF;
1130   }
1131 #endif
1132 
1133 #if defined(_LIBUNWIND_TARGET_PPC)
1134   compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1135     return 0;
1136   }
1137 #endif
1138 
1139 #if defined(_LIBUNWIND_TARGET_PPC64)
1140   compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1141     return 0;
1142   }
1143 #endif
1144 
1145 #if defined(_LIBUNWIND_TARGET_AARCH64)
1146   compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1147     return UNWIND_ARM64_MODE_DWARF;
1148   }
1149 #endif
1150 
1151 #if defined(_LIBUNWIND_TARGET_ARM)
1152   compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1153     return 0;
1154   }
1155 #endif
1156 
1157 #if defined (_LIBUNWIND_TARGET_OR1K)
1158   compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1159     return 0;
1160   }
1161 #endif
1162 
1163 #if defined (_LIBUNWIND_TARGET_HEXAGON)
1164   compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1165     return 0;
1166   }
1167 #endif
1168 
1169 #if defined (_LIBUNWIND_TARGET_MIPS_O32)
1170   compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1171     return 0;
1172   }
1173 #endif
1174 
1175 #if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1176   compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
1177     return 0;
1178   }
1179 #endif
1180 
1181 #if defined(_LIBUNWIND_TARGET_SPARC)
1182   compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1183 #endif
1184 
1185 #if defined (_LIBUNWIND_TARGET_RISCV)
1186   compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1187     return 0;
1188   }
1189 #endif
1190 
1191 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1192 
1193 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1194   // For runtime environments using SEH unwind data without Windows runtime
1195   // support.
1196   pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1197   void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1198   RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1199     /* FIXME: Implement */
1200     *base = 0;
1201     return nullptr;
1202   }
1203   bool getInfoFromSEH(pint_t pc);
1204   int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1205 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1206 
1207 
1208   A               &_addressSpace;
1209   R                _registers;
1210   unw_proc_info_t  _info;
1211   bool             _unwindInfoMissing;
1212   bool             _isSignalFrame;
1213 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1214   bool             _isSigReturn = false;
1215 #endif
1216 };
1217 
1218 
1219 template <typename A, typename R>
1220 UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1221     : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1222       _isSignalFrame(false) {
1223   static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
1224                 "UnwindCursor<> does not fit in unw_cursor_t");
1225   static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1226                 "UnwindCursor<> requires more alignment than unw_cursor_t");
1227   memset(&_info, 0, sizeof(_info));
1228 }
1229 
1230 template <typename A, typename R>
1231 UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1232     : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1233   memset(&_info, 0, sizeof(_info));
1234   // FIXME
1235   // fill in _registers from thread arg
1236 }
1237 
1238 
1239 template <typename A, typename R>
1240 bool UnwindCursor<A, R>::validReg(int regNum) {
1241   return _registers.validRegister(regNum);
1242 }
1243 
1244 template <typename A, typename R>
1245 unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1246   return _registers.getRegister(regNum);
1247 }
1248 
1249 template <typename A, typename R>
1250 void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1251   _registers.setRegister(regNum, (typename A::pint_t)value);
1252 }
1253 
1254 template <typename A, typename R>
1255 bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1256   return _registers.validFloatRegister(regNum);
1257 }
1258 
1259 template <typename A, typename R>
1260 unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1261   return _registers.getFloatRegister(regNum);
1262 }
1263 
1264 template <typename A, typename R>
1265 void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1266   _registers.setFloatRegister(regNum, value);
1267 }
1268 
1269 template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1270   _registers.jumpto();
1271 }
1272 
1273 #ifdef __arm__
1274 template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1275   _registers.saveVFPAsX();
1276 }
1277 #endif
1278 
1279 template <typename A, typename R>
1280 const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1281   return _registers.getRegisterName(regNum);
1282 }
1283 
1284 template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1285   return _isSignalFrame;
1286 }
1287 
1288 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1289 
1290 #if defined(_LIBUNWIND_ARM_EHABI)
1291 template<typename A>
1292 struct EHABISectionIterator {
1293   typedef EHABISectionIterator _Self;
1294 
1295   typedef typename A::pint_t value_type;
1296   typedef typename A::pint_t* pointer;
1297   typedef typename A::pint_t& reference;
1298   typedef size_t size_type;
1299   typedef size_t difference_type;
1300 
1301   static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1302     return _Self(addressSpace, sects, 0);
1303   }
1304   static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
1305     return _Self(addressSpace, sects,
1306                  sects.arm_section_length / sizeof(EHABIIndexEntry));
1307   }
1308 
1309   EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1310       : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1311 
1312   _Self& operator++() { ++_i; return *this; }
1313   _Self& operator+=(size_t a) { _i += a; return *this; }
1314   _Self& operator--() { assert(_i > 0); --_i; return *this; }
1315   _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1316 
1317   _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1318   _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1319 
1320   size_t operator-(const _Self& other) const { return _i - other._i; }
1321 
1322   bool operator==(const _Self& other) const {
1323     assert(_addressSpace == other._addressSpace);
1324     assert(_sects == other._sects);
1325     return _i == other._i;
1326   }
1327 
1328   bool operator!=(const _Self& other) const {
1329     assert(_addressSpace == other._addressSpace);
1330     assert(_sects == other._sects);
1331     return _i != other._i;
1332   }
1333 
1334   typename A::pint_t operator*() const { return functionAddress(); }
1335 
1336   typename A::pint_t functionAddress() const {
1337     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1338         EHABIIndexEntry, _i, functionOffset);
1339     return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1340   }
1341 
1342   typename A::pint_t dataAddress() {
1343     typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1344         EHABIIndexEntry, _i, data);
1345     return indexAddr;
1346   }
1347 
1348  private:
1349   size_t _i;
1350   A* _addressSpace;
1351   const UnwindInfoSections* _sects;
1352 };
1353 
1354 namespace {
1355 
1356 template <typename A>
1357 EHABISectionIterator<A> EHABISectionUpperBound(
1358     EHABISectionIterator<A> first,
1359     EHABISectionIterator<A> last,
1360     typename A::pint_t value) {
1361   size_t len = last - first;
1362   while (len > 0) {
1363     size_t l2 = len / 2;
1364     EHABISectionIterator<A> m = first + l2;
1365     if (value < *m) {
1366         len = l2;
1367     } else {
1368         first = ++m;
1369         len -= l2 + 1;
1370     }
1371   }
1372   return first;
1373 }
1374 
1375 }
1376 
1377 template <typename A, typename R>
1378 bool UnwindCursor<A, R>::getInfoFromEHABISection(
1379     pint_t pc,
1380     const UnwindInfoSections &sects) {
1381   EHABISectionIterator<A> begin =
1382       EHABISectionIterator<A>::begin(_addressSpace, sects);
1383   EHABISectionIterator<A> end =
1384       EHABISectionIterator<A>::end(_addressSpace, sects);
1385   if (begin == end)
1386     return false;
1387 
1388   EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
1389   if (itNextPC == begin)
1390     return false;
1391   EHABISectionIterator<A> itThisPC = itNextPC - 1;
1392 
1393   pint_t thisPC = itThisPC.functionAddress();
1394   // If an exception is thrown from a function, corresponding to the last entry
1395   // in the table, we don't really know the function extent and have to choose a
1396   // value for nextPC. Choosing max() will allow the range check during trace to
1397   // succeed.
1398   pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
1399   pint_t indexDataAddr = itThisPC.dataAddress();
1400 
1401   if (indexDataAddr == 0)
1402     return false;
1403 
1404   uint32_t indexData = _addressSpace.get32(indexDataAddr);
1405   if (indexData == UNW_EXIDX_CANTUNWIND)
1406     return false;
1407 
1408   // If the high bit is set, the exception handling table entry is inline inside
1409   // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1410   // the table points at an offset in the exception handling table (section 5
1411   // EHABI).
1412   pint_t exceptionTableAddr;
1413   uint32_t exceptionTableData;
1414   bool isSingleWordEHT;
1415   if (indexData & 0x80000000) {
1416     exceptionTableAddr = indexDataAddr;
1417     // TODO(ajwong): Should this data be 0?
1418     exceptionTableData = indexData;
1419     isSingleWordEHT = true;
1420   } else {
1421     exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1422     exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1423     isSingleWordEHT = false;
1424   }
1425 
1426   // Now we know the 3 things:
1427   //   exceptionTableAddr -- exception handler table entry.
1428   //   exceptionTableData -- the data inside the first word of the eht entry.
1429   //   isSingleWordEHT -- whether the entry is in the index.
1430   unw_word_t personalityRoutine = 0xbadf00d;
1431   bool scope32 = false;
1432   uintptr_t lsda;
1433 
1434   // If the high bit in the exception handling table entry is set, the entry is
1435   // in compact form (section 6.3 EHABI).
1436   if (exceptionTableData & 0x80000000) {
1437     // Grab the index of the personality routine from the compact form.
1438     uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1439     uint32_t extraWords = 0;
1440     switch (choice) {
1441       case 0:
1442         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1443         extraWords = 0;
1444         scope32 = false;
1445         lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
1446         break;
1447       case 1:
1448         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1449         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1450         scope32 = false;
1451         lsda = exceptionTableAddr + (extraWords + 1) * 4;
1452         break;
1453       case 2:
1454         personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1455         extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1456         scope32 = true;
1457         lsda = exceptionTableAddr + (extraWords + 1) * 4;
1458         break;
1459       default:
1460         _LIBUNWIND_ABORT("unknown personality routine");
1461         return false;
1462     }
1463 
1464     if (isSingleWordEHT) {
1465       if (extraWords != 0) {
1466         _LIBUNWIND_ABORT("index inlined table detected but pr function "
1467                          "requires extra words");
1468         return false;
1469       }
1470     }
1471   } else {
1472     pint_t personalityAddr =
1473         exceptionTableAddr + signExtendPrel31(exceptionTableData);
1474     personalityRoutine = personalityAddr;
1475 
1476     // ARM EHABI # 6.2, # 9.2
1477     //
1478     //  +---- ehtp
1479     //  v
1480     // +--------------------------------------+
1481     // | +--------+--------+--------+-------+ |
1482     // | |0| prel31 to personalityRoutine   | |
1483     // | +--------+--------+--------+-------+ |
1484     // | |      N |      unwind opcodes     | |  <-- UnwindData
1485     // | +--------+--------+--------+-------+ |
1486     // | | Word 2        unwind opcodes     | |
1487     // | +--------+--------+--------+-------+ |
1488     // | ...                                  |
1489     // | +--------+--------+--------+-------+ |
1490     // | | Word N        unwind opcodes     | |
1491     // | +--------+--------+--------+-------+ |
1492     // | | LSDA                             | |  <-- lsda
1493     // | | ...                              | |
1494     // | +--------+--------+--------+-------+ |
1495     // +--------------------------------------+
1496 
1497     uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1498     uint32_t FirstDataWord = *UnwindData;
1499     size_t N = ((FirstDataWord >> 24) & 0xff);
1500     size_t NDataWords = N + 1;
1501     lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1502   }
1503 
1504   _info.start_ip = thisPC;
1505   _info.end_ip = nextPC;
1506   _info.handler = personalityRoutine;
1507   _info.unwind_info = exceptionTableAddr;
1508   _info.lsda = lsda;
1509   // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1510   _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0);  // Use enum?
1511 
1512   return true;
1513 }
1514 #endif
1515 
1516 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1517 template <typename A, typename R>
1518 bool UnwindCursor<A, R>::getInfoFromFdeCie(
1519     const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1520     const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1521     uintptr_t dso_base) {
1522   typename CFI_Parser<A>::PrologInfo prolog;
1523   if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1524                                           R::getArch(), &prolog)) {
1525     // Save off parsed FDE info
1526     _info.start_ip          = fdeInfo.pcStart;
1527     _info.end_ip            = fdeInfo.pcEnd;
1528     _info.lsda              = fdeInfo.lsda;
1529     _info.handler           = cieInfo.personality;
1530     // Some frameless functions need SP altered when resuming in function, so
1531     // propagate spExtraArgSize.
1532     _info.gp                = prolog.spExtraArgSize;
1533     _info.flags             = 0;
1534     _info.format            = dwarfEncoding();
1535     _info.unwind_info       = fdeInfo.fdeStart;
1536     _info.unwind_info_size  = static_cast<uint32_t>(fdeInfo.fdeLength);
1537     _info.extra             = static_cast<unw_word_t>(dso_base);
1538     return true;
1539   }
1540   return false;
1541 }
1542 
1543 template <typename A, typename R>
1544 bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1545                                                 const UnwindInfoSections &sects,
1546                                                 uint32_t fdeSectionOffsetHint) {
1547   typename CFI_Parser<A>::FDE_Info fdeInfo;
1548   typename CFI_Parser<A>::CIE_Info cieInfo;
1549   bool foundFDE = false;
1550   bool foundInCache = false;
1551   // If compact encoding table gave offset into dwarf section, go directly there
1552   if (fdeSectionOffsetHint != 0) {
1553     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1554                                     sects.dwarf_section_length,
1555                                     sects.dwarf_section + fdeSectionOffsetHint,
1556                                     &fdeInfo, &cieInfo);
1557   }
1558 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
1559   if (!foundFDE && (sects.dwarf_index_section != 0)) {
1560     foundFDE = EHHeaderParser<A>::findFDE(
1561         _addressSpace, pc, sects.dwarf_index_section,
1562         (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1563   }
1564 #endif
1565   if (!foundFDE) {
1566     // otherwise, search cache of previously found FDEs.
1567     pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1568     if (cachedFDE != 0) {
1569       foundFDE =
1570           CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1571                                  sects.dwarf_section_length,
1572                                  cachedFDE, &fdeInfo, &cieInfo);
1573       foundInCache = foundFDE;
1574     }
1575   }
1576   if (!foundFDE) {
1577     // Still not found, do full scan of __eh_frame section.
1578     foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1579                                       sects.dwarf_section_length, 0,
1580                                       &fdeInfo, &cieInfo);
1581   }
1582   if (foundFDE) {
1583     if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
1584       // Add to cache (to make next lookup faster) if we had no hint
1585       // and there was no index.
1586       if (!foundInCache && (fdeSectionOffsetHint == 0)) {
1587   #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
1588         if (sects.dwarf_index_section == 0)
1589   #endif
1590         DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1591                               fdeInfo.fdeStart);
1592       }
1593       return true;
1594     }
1595   }
1596   //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
1597   return false;
1598 }
1599 #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1600 
1601 
1602 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
1603 template <typename A, typename R>
1604 bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1605                                               const UnwindInfoSections &sects) {
1606   const bool log = false;
1607   if (log)
1608     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1609             (uint64_t)pc, (uint64_t)sects.dso_base);
1610 
1611   const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1612                                                 sects.compact_unwind_section);
1613   if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1614     return false;
1615 
1616   // do a binary search of top level index to find page with unwind info
1617   pint_t targetFunctionOffset = pc - sects.dso_base;
1618   const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1619                                            sects.compact_unwind_section
1620                                          + sectionHeader.indexSectionOffset());
1621   uint32_t low = 0;
1622   uint32_t high = sectionHeader.indexCount();
1623   uint32_t last = high - 1;
1624   while (low < high) {
1625     uint32_t mid = (low + high) / 2;
1626     //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1627     //mid, low, high, topIndex.functionOffset(mid));
1628     if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1629       if ((mid == last) ||
1630           (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1631         low = mid;
1632         break;
1633       } else {
1634         low = mid + 1;
1635       }
1636     } else {
1637       high = mid;
1638     }
1639   }
1640   const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1641   const uint32_t firstLevelNextPageFunctionOffset =
1642       topIndex.functionOffset(low + 1);
1643   const pint_t secondLevelAddr =
1644       sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1645   const pint_t lsdaArrayStartAddr =
1646       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1647   const pint_t lsdaArrayEndAddr =
1648       sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1649   if (log)
1650     fprintf(stderr, "\tfirst level search for result index=%d "
1651                     "to secondLevelAddr=0x%llX\n",
1652                     low, (uint64_t) secondLevelAddr);
1653   // do a binary search of second level page index
1654   uint32_t encoding = 0;
1655   pint_t funcStart = 0;
1656   pint_t funcEnd = 0;
1657   pint_t lsda = 0;
1658   pint_t personality = 0;
1659   uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1660   if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1661     // regular page
1662     UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1663                                                  secondLevelAddr);
1664     UnwindSectionRegularArray<A> pageIndex(
1665         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1666     // binary search looks for entry with e where index[e].offset <= pc <
1667     // index[e+1].offset
1668     if (log)
1669       fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1670                       "regular page starting at secondLevelAddr=0x%llX\n",
1671               (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1672     low = 0;
1673     high = pageHeader.entryCount();
1674     while (low < high) {
1675       uint32_t mid = (low + high) / 2;
1676       if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1677         if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1678           // at end of table
1679           low = mid;
1680           funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1681           break;
1682         } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1683           // next is too big, so we found it
1684           low = mid;
1685           funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1686           break;
1687         } else {
1688           low = mid + 1;
1689         }
1690       } else {
1691         high = mid;
1692       }
1693     }
1694     encoding = pageIndex.encoding(low);
1695     funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1696     if (pc < funcStart) {
1697       if (log)
1698         fprintf(
1699             stderr,
1700             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1701             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1702       return false;
1703     }
1704     if (pc > funcEnd) {
1705       if (log)
1706         fprintf(
1707             stderr,
1708             "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1709             (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1710       return false;
1711     }
1712   } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1713     // compressed page
1714     UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1715                                                     secondLevelAddr);
1716     UnwindSectionCompressedArray<A> pageIndex(
1717         _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1718     const uint32_t targetFunctionPageOffset =
1719         (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1720     // binary search looks for entry with e where index[e].offset <= pc <
1721     // index[e+1].offset
1722     if (log)
1723       fprintf(stderr, "\tbinary search of compressed page starting at "
1724                       "secondLevelAddr=0x%llX\n",
1725               (uint64_t) secondLevelAddr);
1726     low = 0;
1727     last = pageHeader.entryCount() - 1;
1728     high = pageHeader.entryCount();
1729     while (low < high) {
1730       uint32_t mid = (low + high) / 2;
1731       if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1732         if ((mid == last) ||
1733             (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1734           low = mid;
1735           break;
1736         } else {
1737           low = mid + 1;
1738         }
1739       } else {
1740         high = mid;
1741       }
1742     }
1743     funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1744                                                               + sects.dso_base;
1745     if (low < last)
1746       funcEnd =
1747           pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1748                                                               + sects.dso_base;
1749     else
1750       funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1751     if (pc < funcStart) {
1752       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1753                            "not in second level compressed unwind table. "
1754                            "funcStart=0x%llX",
1755                             (uint64_t) pc, (uint64_t) funcStart);
1756       return false;
1757     }
1758     if (pc > funcEnd) {
1759       _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1760                            "not in second level compressed unwind table. "
1761                            "funcEnd=0x%llX",
1762                            (uint64_t) pc, (uint64_t) funcEnd);
1763       return false;
1764     }
1765     uint16_t encodingIndex = pageIndex.encodingIndex(low);
1766     if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1767       // encoding is in common table in section header
1768       encoding = _addressSpace.get32(
1769           sects.compact_unwind_section +
1770           sectionHeader.commonEncodingsArraySectionOffset() +
1771           encodingIndex * sizeof(uint32_t));
1772     } else {
1773       // encoding is in page specific table
1774       uint16_t pageEncodingIndex =
1775           encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1776       encoding = _addressSpace.get32(secondLevelAddr +
1777                                      pageHeader.encodingsPageOffset() +
1778                                      pageEncodingIndex * sizeof(uint32_t));
1779     }
1780   } else {
1781     _LIBUNWIND_DEBUG_LOG(
1782         "malformed __unwind_info at 0x%0llX bad second level page",
1783         (uint64_t)sects.compact_unwind_section);
1784     return false;
1785   }
1786 
1787   // look up LSDA, if encoding says function has one
1788   if (encoding & UNWIND_HAS_LSDA) {
1789     UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1790     uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1791     low = 0;
1792     high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1793                     sizeof(unwind_info_section_header_lsda_index_entry);
1794     // binary search looks for entry with exact match for functionOffset
1795     if (log)
1796       fprintf(stderr,
1797               "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1798               funcStartOffset);
1799     while (low < high) {
1800       uint32_t mid = (low + high) / 2;
1801       if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1802         lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1803         break;
1804       } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1805         low = mid + 1;
1806       } else {
1807         high = mid;
1808       }
1809     }
1810     if (lsda == 0) {
1811       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
1812                     "pc=0x%0llX, but lsda table has no entry",
1813                     encoding, (uint64_t) pc);
1814       return false;
1815     }
1816   }
1817 
1818   // extract personality routine, if encoding says function has one
1819   uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1820                               (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1821   if (personalityIndex != 0) {
1822     --personalityIndex; // change 1-based to zero-based index
1823     if (personalityIndex >= sectionHeader.personalityArrayCount()) {
1824       _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d,  "
1825                             "but personality table has only %d entries",
1826                             encoding, personalityIndex,
1827                             sectionHeader.personalityArrayCount());
1828       return false;
1829     }
1830     int32_t personalityDelta = (int32_t)_addressSpace.get32(
1831         sects.compact_unwind_section +
1832         sectionHeader.personalityArraySectionOffset() +
1833         personalityIndex * sizeof(uint32_t));
1834     pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1835     personality = _addressSpace.getP(personalityPointer);
1836     if (log)
1837       fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1838                       "personalityDelta=0x%08X, personality=0x%08llX\n",
1839               (uint64_t) pc, personalityDelta, (uint64_t) personality);
1840   }
1841 
1842   if (log)
1843     fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1844                     "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1845             (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1846   _info.start_ip = funcStart;
1847   _info.end_ip = funcEnd;
1848   _info.lsda = lsda;
1849   _info.handler = personality;
1850   _info.gp = 0;
1851   _info.flags = 0;
1852   _info.format = encoding;
1853   _info.unwind_info = 0;
1854   _info.unwind_info_size = 0;
1855   _info.extra = sects.dso_base;
1856   return true;
1857 }
1858 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
1859 
1860 
1861 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1862 template <typename A, typename R>
1863 bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1864   pint_t base;
1865   RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1866   if (!unwindEntry) {
1867     _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1868     return false;
1869   }
1870   _info.gp = 0;
1871   _info.flags = 0;
1872   _info.format = 0;
1873   _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1874   _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1875   _info.extra = base;
1876   _info.start_ip = base + unwindEntry->BeginAddress;
1877 #ifdef _LIBUNWIND_TARGET_X86_64
1878   _info.end_ip = base + unwindEntry->EndAddress;
1879   // Only fill in the handler and LSDA if they're stale.
1880   if (pc != getLastPC()) {
1881     UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1882     if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1883       // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1884       // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1885       // these structures.)
1886       // N.B. UNWIND_INFO structs are DWORD-aligned.
1887       uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1888       const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1889       _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1890       if (*handler) {
1891         _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1892       } else
1893         _info.handler = 0;
1894     } else {
1895       _info.lsda = 0;
1896       _info.handler = 0;
1897     }
1898   }
1899 #elif defined(_LIBUNWIND_TARGET_ARM)
1900   _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1901   _info.lsda = 0; // FIXME
1902   _info.handler = 0; // FIXME
1903 #endif
1904   setLastPC(pc);
1905   return true;
1906 }
1907 #endif
1908 
1909 
1910 template <typename A, typename R>
1911 void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1912 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1913   _isSigReturn = false;
1914 #endif
1915 
1916   pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
1917 #if defined(_LIBUNWIND_ARM_EHABI)
1918   // Remove the thumb bit so the IP represents the actual instruction address.
1919   // This matches the behaviour of _Unwind_GetIP on arm.
1920   pc &= (pint_t)~0x1;
1921 #endif
1922 
1923   // Exit early if at the top of the stack.
1924   if (pc == 0) {
1925     _unwindInfoMissing = true;
1926     return;
1927   }
1928 
1929   // If the last line of a function is a "throw" the compiler sometimes
1930   // emits no instructions after the call to __cxa_throw.  This means
1931   // the return address is actually the start of the next function.
1932   // To disambiguate this, back up the pc when we know it is a return
1933   // address.
1934   if (isReturnAddress)
1935     --pc;
1936 
1937   // Ask address space object to find unwind sections for this pc.
1938   UnwindInfoSections sects;
1939   if (_addressSpace.findUnwindSections(pc, sects)) {
1940 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
1941     // If there is a compact unwind encoding table, look there first.
1942     if (sects.compact_unwind_section != 0) {
1943       if (this->getInfoFromCompactEncodingSection(pc, sects)) {
1944   #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1945         // Found info in table, done unless encoding says to use dwarf.
1946         uint32_t dwarfOffset;
1947         if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1948           if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1949             // found info in dwarf, done
1950             return;
1951           }
1952         }
1953   #endif
1954         // If unwind table has entry, but entry says there is no unwind info,
1955         // record that we have no unwind info.
1956         if (_info.format == 0)
1957           _unwindInfoMissing = true;
1958         return;
1959       }
1960     }
1961 #endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
1962 
1963 #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1964     // If there is SEH unwind info, look there next.
1965     if (this->getInfoFromSEH(pc))
1966       return;
1967 #endif
1968 
1969 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1970     // If there is dwarf unwind info, look there next.
1971     if (sects.dwarf_section != 0) {
1972       if (this->getInfoFromDwarfSection(pc, sects)) {
1973         // found info in dwarf, done
1974         return;
1975       }
1976     }
1977 #endif
1978 
1979 #if defined(_LIBUNWIND_ARM_EHABI)
1980     // If there is ARM EHABI unwind info, look there next.
1981     if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1982       return;
1983 #endif
1984   }
1985 
1986 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
1987   // There is no static unwind info for this pc. Look to see if an FDE was
1988   // dynamically registered for it.
1989   pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
1990                                                pc);
1991   if (cachedFDE != 0) {
1992     typename CFI_Parser<A>::FDE_Info fdeInfo;
1993     typename CFI_Parser<A>::CIE_Info cieInfo;
1994     if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
1995       if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
1996         return;
1997   }
1998 
1999   // Lastly, ask AddressSpace object about platform specific ways to locate
2000   // other FDEs.
2001   pint_t fde;
2002   if (_addressSpace.findOtherFDE(pc, fde)) {
2003     typename CFI_Parser<A>::FDE_Info fdeInfo;
2004     typename CFI_Parser<A>::CIE_Info cieInfo;
2005     if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
2006       // Double check this FDE is for a function that includes the pc.
2007       if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2008         if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
2009           return;
2010     }
2011   }
2012 #endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2013 
2014 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2015   if (setInfoForSigReturn())
2016     return;
2017 #endif
2018 
2019   // no unwind info, flag that we can't reliably unwind
2020   _unwindInfoMissing = true;
2021 }
2022 
2023 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2024 template <typename A, typename R>
2025 bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2026   // Look for the sigreturn trampoline. The trampoline's body is two
2027   // specific instructions (see below). Typically the trampoline comes from the
2028   // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2029   // own restorer function, though, or user-mode QEMU might write a trampoline
2030   // onto the stack.
2031   //
2032   // This special code path is a fallback that is only used if the trampoline
2033   // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2034   // constant for the PC needs to be defined before DWARF can handle a signal
2035   // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2036   //  - The PC points at a function compiled without unwind info, and which is
2037   //    part of an execute-only mapping (e.g. using -Wl,--execute-only).
2038   //  - The PC is invalid and happens to point to unreadable or unmapped memory.
2039   //
2040   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2041   const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2042   // Look for instructions: mov x8, #0x8b; svc #0x0
2043   if (_addressSpace.get32(pc) == 0xd2801168 &&
2044       _addressSpace.get32(pc + 4) == 0xd4000001) {
2045     _info = {};
2046     _isSigReturn = true;
2047     return true;
2048   }
2049   return false;
2050 }
2051 
2052 template <typename A, typename R>
2053 int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2054   // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2055   //  - 128-byte siginfo struct
2056   //  - ucontext struct:
2057   //     - 8-byte long (uc_flags)
2058   //     - 8-byte pointer (uc_link)
2059   //     - 24-byte stack_t
2060   //     - 128-byte signal set
2061   //     - 8 bytes of padding because sigcontext has 16-byte alignment
2062   //     - sigcontext/mcontext_t
2063   // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2064   const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2065 
2066   // Offsets from sigcontext to each register.
2067   const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2068   const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2069   const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2070 
2071   pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2072 
2073   for (int i = 0; i <= 30; ++i) {
2074     uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2075                                          static_cast<pint_t>(i * 8));
2076     _registers.setRegister(UNW_AARCH64_X0 + i, value);
2077   }
2078   _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2079   _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2080   _isSignalFrame = true;
2081   return UNW_STEP_SUCCESS;
2082 }
2083 #endif // defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2084 
2085 template <typename A, typename R>
2086 int UnwindCursor<A, R>::step() {
2087   // Bottom of stack is defined is when unwind info cannot be found.
2088   if (_unwindInfoMissing)
2089     return UNW_STEP_END;
2090 
2091   // Use unwinding info to modify register set as if function returned.
2092   int result;
2093 #if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2094   if (_isSigReturn) {
2095     result = this->stepThroughSigReturn();
2096   } else
2097 #endif
2098   {
2099 #if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
2100     result = this->stepWithCompactEncoding();
2101 #elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
2102     result = this->stepWithSEHData();
2103 #elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
2104     result = this->stepWithDwarfFDE();
2105 #elif defined(_LIBUNWIND_ARM_EHABI)
2106     result = this->stepWithEHABI();
2107 #else
2108   #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
2109               _LIBUNWIND_SUPPORT_SEH_UNWIND or \
2110               _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
2111               _LIBUNWIND_ARM_EHABI
2112 #endif
2113   }
2114 
2115   // update info based on new PC
2116   if (result == UNW_STEP_SUCCESS) {
2117     this->setInfoBasedOnIPRegister(true);
2118     if (_unwindInfoMissing)
2119       return UNW_STEP_END;
2120   }
2121 
2122   return result;
2123 }
2124 
2125 template <typename A, typename R>
2126 void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
2127   if (_unwindInfoMissing)
2128     memset(info, 0, sizeof(*info));
2129   else
2130     *info = _info;
2131 }
2132 
2133 template <typename A, typename R>
2134 bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2135                                                            unw_word_t *offset) {
2136   return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2137                                          buf, bufLen, offset);
2138 }
2139 
2140 #if defined(_LIBUNWIND_USE_CET)
2141 extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2142   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2143   return co->get_registers();
2144 }
2145 #endif
2146 } // namespace libunwind
2147 
2148 #endif // __UNWINDCURSOR_HPP__
2149