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