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