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