blob: 497ff84552de2d6d3fad43f3099fce4b2431faa0 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.hpp ---------------------------===//
2//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// 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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
Ed Maste6f723382016-08-30 13:08:21 +00008// C++ interface to lower levels of libunwind
Saleem Abdulrasool17552662015-04-24 19:39:17 +00009//===----------------------------------------------------------------------===//
10
11#ifndef __UNWINDCURSOR_HPP__
12#define __UNWINDCURSOR_HPP__
13
14#include <algorithm>
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000018#include <unwind.h>
19
Charles Davisfa2e6202018-08-30 21:29:00 +000020#ifdef _WIN32
21 #include <windows.h>
22 #include <ntverp.h>
23#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000024#ifdef __APPLE__
25 #include <mach-o/dyld.h>
26#endif
27
Charles Davisfa2e6202018-08-30 21:29:00 +000028#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
34struct _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
49struct 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
59extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
60 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
61 struct _Unwind_Context *);
62
63#endif
64
Saleem Abdulrasool17552662015-04-24 19:39:17 +000065#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"
Martin Storsjo590ffef2017-10-23 19:29:36 +000074#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000075#include "Unwind-EHABI.h"
76
77namespace libunwind {
78
Ranjeet Singh421231a2017-03-31 15:28:06 +000079#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000080/// Cache of recently found FDEs.
81template <typename A>
82class _LIBUNWIND_HIDDEN DwarfFDECache {
83 typedef typename A::pint_t pint_t;
84public:
85 static pint_t findFDE(pint_t mh, pint_t pc);
86 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
87 static void removeAllIn(pint_t mh);
88 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
89 unw_word_t ip_end,
90 unw_word_t fde, unw_word_t mh));
91
92private:
93
94 struct entry {
95 pint_t mh;
96 pint_t ip_start;
97 pint_t ip_end;
98 pint_t fde;
99 };
100
101 // These fields are all static to avoid needing an initializer.
102 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000103 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000104#ifdef __APPLE__
105 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
106 static bool _registeredForDyldUnloads;
107#endif
108 // Can't use std::vector<> here because this code is below libc++.
109 static entry *_buffer;
110 static entry *_bufferUsed;
111 static entry *_bufferEnd;
112 static entry _initialBuffer[64];
113};
114
115template <typename A>
116typename DwarfFDECache<A>::entry *
117DwarfFDECache<A>::_buffer = _initialBuffer;
118
119template <typename A>
120typename DwarfFDECache<A>::entry *
121DwarfFDECache<A>::_bufferUsed = _initialBuffer;
122
123template <typename A>
124typename DwarfFDECache<A>::entry *
125DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
126
127template <typename A>
128typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
129
130template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000131RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000132
133#ifdef __APPLE__
134template <typename A>
135bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
136#endif
137
138template <typename A>
139typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
140 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000141 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000142 for (entry *p = _buffer; p < _bufferUsed; ++p) {
143 if ((mh == p->mh) || (mh == 0)) {
144 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
145 result = p->fde;
146 break;
147 }
148 }
149 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000150 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000151 return result;
152}
153
154template <typename A>
155void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
156 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000157#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000158 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000159 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
Martin Storsjo590ffef2017-10-23 19:29:36 +0000182 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000183#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000184}
185
186template <typename A>
187void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000188 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000189 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;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000198 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199}
200
201#ifdef __APPLE__
202template <typename A>
203void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
204 removeAllIn((pint_t) mh);
205}
206#endif
207
208template <typename A>
209void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
210 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000211 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000212 for (entry *p = _buffer; p < _bufferUsed; ++p) {
213 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
214 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000215 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000217#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000218
219
220#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
221
Ranjeet Singh421231a2017-03-31 15:28:06 +0000222#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000223template <typename A> class UnwindSectionHeader {
224public:
225 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
226 : _addressSpace(addressSpace), _addr(addr) {}
227
228 uint32_t version() const {
229 return _addressSpace.get32(_addr +
230 offsetof(unwind_info_section_header, version));
231 }
232 uint32_t commonEncodingsArraySectionOffset() const {
233 return _addressSpace.get32(_addr +
234 offsetof(unwind_info_section_header,
235 commonEncodingsArraySectionOffset));
236 }
237 uint32_t commonEncodingsArrayCount() const {
238 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
239 commonEncodingsArrayCount));
240 }
241 uint32_t personalityArraySectionOffset() const {
242 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
243 personalityArraySectionOffset));
244 }
245 uint32_t personalityArrayCount() const {
246 return _addressSpace.get32(
247 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
248 }
249 uint32_t indexSectionOffset() const {
250 return _addressSpace.get32(
251 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
252 }
253 uint32_t indexCount() const {
254 return _addressSpace.get32(
255 _addr + offsetof(unwind_info_section_header, indexCount));
256 }
257
258private:
259 A &_addressSpace;
260 typename A::pint_t _addr;
261};
262
263template <typename A> class UnwindSectionIndexArray {
264public:
265 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
266 : _addressSpace(addressSpace), _addr(addr) {}
267
268 uint32_t functionOffset(uint32_t index) const {
269 return _addressSpace.get32(
270 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
271 functionOffset));
272 }
273 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
274 return _addressSpace.get32(
275 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
276 secondLevelPagesSectionOffset));
277 }
278 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
279 return _addressSpace.get32(
280 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
281 lsdaIndexArraySectionOffset));
282 }
283
284private:
285 A &_addressSpace;
286 typename A::pint_t _addr;
287};
288
289template <typename A> class UnwindSectionRegularPageHeader {
290public:
291 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
292 : _addressSpace(addressSpace), _addr(addr) {}
293
294 uint32_t kind() const {
295 return _addressSpace.get32(
296 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
297 }
298 uint16_t entryPageOffset() const {
299 return _addressSpace.get16(
300 _addr + offsetof(unwind_info_regular_second_level_page_header,
301 entryPageOffset));
302 }
303 uint16_t entryCount() const {
304 return _addressSpace.get16(
305 _addr +
306 offsetof(unwind_info_regular_second_level_page_header, entryCount));
307 }
308
309private:
310 A &_addressSpace;
311 typename A::pint_t _addr;
312};
313
314template <typename A> class UnwindSectionRegularArray {
315public:
316 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
317 : _addressSpace(addressSpace), _addr(addr) {}
318
319 uint32_t functionOffset(uint32_t index) const {
320 return _addressSpace.get32(
321 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
322 functionOffset));
323 }
324 uint32_t encoding(uint32_t index) const {
325 return _addressSpace.get32(
326 _addr +
327 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
328 }
329
330private:
331 A &_addressSpace;
332 typename A::pint_t _addr;
333};
334
335template <typename A> class UnwindSectionCompressedPageHeader {
336public:
337 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
338 : _addressSpace(addressSpace), _addr(addr) {}
339
340 uint32_t kind() const {
341 return _addressSpace.get32(
342 _addr +
343 offsetof(unwind_info_compressed_second_level_page_header, kind));
344 }
345 uint16_t entryPageOffset() const {
346 return _addressSpace.get16(
347 _addr + offsetof(unwind_info_compressed_second_level_page_header,
348 entryPageOffset));
349 }
350 uint16_t entryCount() const {
351 return _addressSpace.get16(
352 _addr +
353 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
354 }
355 uint16_t encodingsPageOffset() const {
356 return _addressSpace.get16(
357 _addr + offsetof(unwind_info_compressed_second_level_page_header,
358 encodingsPageOffset));
359 }
360 uint16_t encodingsCount() const {
361 return _addressSpace.get16(
362 _addr + offsetof(unwind_info_compressed_second_level_page_header,
363 encodingsCount));
364 }
365
366private:
367 A &_addressSpace;
368 typename A::pint_t _addr;
369};
370
371template <typename A> class UnwindSectionCompressedArray {
372public:
373 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
374 : _addressSpace(addressSpace), _addr(addr) {}
375
376 uint32_t functionOffset(uint32_t index) const {
377 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
378 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
379 }
380 uint16_t encodingIndex(uint32_t index) const {
381 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
382 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
383 }
384
385private:
386 A &_addressSpace;
387 typename A::pint_t _addr;
388};
389
390template <typename A> class UnwindSectionLsdaArray {
391public:
392 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
393 : _addressSpace(addressSpace), _addr(addr) {}
394
395 uint32_t functionOffset(uint32_t index) const {
396 return _addressSpace.get32(
397 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
398 index, functionOffset));
399 }
400 uint32_t lsdaOffset(uint32_t index) const {
401 return _addressSpace.get32(
402 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
403 index, lsdaOffset));
404 }
405
406private:
407 A &_addressSpace;
408 typename A::pint_t _addr;
409};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000410#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000411
412class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
413public:
414 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
415 // This avoids an unnecessary dependency to libc++abi.
416 void operator delete(void *, size_t) {}
417
418 virtual ~AbstractUnwindCursor() {}
419 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
420 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
421 virtual void setReg(int, unw_word_t) {
422 _LIBUNWIND_ABORT("setReg not implemented");
423 }
424 virtual bool validFloatReg(int) {
425 _LIBUNWIND_ABORT("validFloatReg not implemented");
426 }
427 virtual unw_fpreg_t getFloatReg(int) {
428 _LIBUNWIND_ABORT("getFloatReg not implemented");
429 }
430 virtual void setFloatReg(int, unw_fpreg_t) {
431 _LIBUNWIND_ABORT("setFloatReg not implemented");
432 }
433 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
434 virtual void getInfo(unw_proc_info_t *) {
435 _LIBUNWIND_ABORT("getInfo not implemented");
436 }
437 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
438 virtual bool isSignalFrame() {
439 _LIBUNWIND_ABORT("isSignalFrame not implemented");
440 }
441 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
442 _LIBUNWIND_ABORT("getFunctionName not implemented");
443 }
444 virtual void setInfoBasedOnIPRegister(bool = false) {
445 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
446 }
447 virtual const char *getRegisterName(int) {
448 _LIBUNWIND_ABORT("getRegisterName not implemented");
449 }
450#ifdef __arm__
451 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
452#endif
453};
454
Charles Davisfa2e6202018-08-30 21:29:00 +0000455#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
456
457/// \c UnwindCursor contains all state (including all register values) during
458/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
459template <typename A, typename R>
460class UnwindCursor : public AbstractUnwindCursor {
461 typedef typename A::pint_t pint_t;
462public:
463 UnwindCursor(unw_context_t *context, A &as);
464 UnwindCursor(CONTEXT *context, A &as);
465 UnwindCursor(A &as, void *threadArg);
466 virtual ~UnwindCursor() {}
467 virtual bool validReg(int);
468 virtual unw_word_t getReg(int);
469 virtual void setReg(int, unw_word_t);
470 virtual bool validFloatReg(int);
471 virtual unw_fpreg_t getFloatReg(int);
472 virtual void setFloatReg(int, unw_fpreg_t);
473 virtual int step();
474 virtual void getInfo(unw_proc_info_t *);
475 virtual void jumpto();
476 virtual bool isSignalFrame();
477 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
478 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
479 virtual const char *getRegisterName(int num);
480#ifdef __arm__
481 virtual void saveVFPAsX();
482#endif
483
484 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
485 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
486
487private:
488
489 pint_t getLastPC() const { return _dispContext.ControlPc; }
490 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
491 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
492 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
493 &_dispContext.ImageBase,
494 _dispContext.HistoryTable);
495 *base = _dispContext.ImageBase;
496 return _dispContext.FunctionEntry;
497 }
498 bool getInfoFromSEH(pint_t pc);
499 int stepWithSEHData() {
500 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
501 _dispContext.ImageBase,
502 _dispContext.ControlPc,
503 _dispContext.FunctionEntry,
504 _dispContext.ContextRecord,
505 &_dispContext.HandlerData,
506 &_dispContext.EstablisherFrame,
507 NULL);
508 // Update some fields of the unwind info now, since we have them.
509 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
510 if (_dispContext.LanguageHandler) {
511 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
512 } else
513 _info.handler = 0;
514 return UNW_STEP_SUCCESS;
515 }
516
517 A &_addressSpace;
518 unw_proc_info_t _info;
519 DISPATCHER_CONTEXT _dispContext;
520 CONTEXT _msContext;
521 UNWIND_HISTORY_TABLE _histTable;
522 bool _unwindInfoMissing;
523};
524
525
526template <typename A, typename R>
527UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
528 : _addressSpace(as), _unwindInfoMissing(false) {
529 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
530 "UnwindCursor<> does not fit in unw_cursor_t");
531 memset(&_info, 0, sizeof(_info));
532 memset(&_histTable, 0, sizeof(_histTable));
533 _dispContext.ContextRecord = &_msContext;
534 _dispContext.HistoryTable = &_histTable;
535 // Initialize MS context from ours.
536 R r(context);
537 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
538#if defined(_LIBUNWIND_TARGET_X86_64)
539 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
540 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
541 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
542 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
543 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
544 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
545 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
546 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
547 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
548 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
549 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
550 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
551 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
552 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
553 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
554 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
555 _msContext.Rip = r.getRegister(UNW_REG_IP);
556 union {
557 v128 v;
558 M128A m;
559 } t;
560 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
561 _msContext.Xmm0 = t.m;
562 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
563 _msContext.Xmm1 = t.m;
564 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
565 _msContext.Xmm2 = t.m;
566 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
567 _msContext.Xmm3 = t.m;
568 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
569 _msContext.Xmm4 = t.m;
570 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
571 _msContext.Xmm5 = t.m;
572 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
573 _msContext.Xmm6 = t.m;
574 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
575 _msContext.Xmm7 = t.m;
576 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
577 _msContext.Xmm8 = t.m;
578 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
579 _msContext.Xmm9 = t.m;
580 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
581 _msContext.Xmm10 = t.m;
582 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
583 _msContext.Xmm11 = t.m;
584 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
585 _msContext.Xmm12 = t.m;
586 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
587 _msContext.Xmm13 = t.m;
588 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
589 _msContext.Xmm14 = t.m;
590 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
591 _msContext.Xmm15 = t.m;
592#elif defined(_LIBUNWIND_TARGET_ARM)
593 _msContext.R0 = r.getRegister(UNW_ARM_R0);
594 _msContext.R1 = r.getRegister(UNW_ARM_R1);
595 _msContext.R2 = r.getRegister(UNW_ARM_R2);
596 _msContext.R3 = r.getRegister(UNW_ARM_R3);
597 _msContext.R4 = r.getRegister(UNW_ARM_R4);
598 _msContext.R5 = r.getRegister(UNW_ARM_R5);
599 _msContext.R6 = r.getRegister(UNW_ARM_R6);
600 _msContext.R7 = r.getRegister(UNW_ARM_R7);
601 _msContext.R8 = r.getRegister(UNW_ARM_R8);
602 _msContext.R9 = r.getRegister(UNW_ARM_R9);
603 _msContext.R10 = r.getRegister(UNW_ARM_R10);
604 _msContext.R11 = r.getRegister(UNW_ARM_R11);
605 _msContext.R12 = r.getRegister(UNW_ARM_R12);
606 _msContext.Sp = r.getRegister(UNW_ARM_SP);
607 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000608 _msContext.Pc = r.getRegister(UNW_ARM_IP);
609 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000610 union {
611 uint64_t w;
612 double d;
613 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000614 d.d = r.getFloatRegister(i);
615 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000616 }
Martin Storsjoce150112018-12-18 20:05:59 +0000617#elif defined(_LIBUNWIND_TARGET_AARCH64)
618 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
619 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
620 _msContext.Sp = r.getRegister(UNW_REG_SP);
621 _msContext.Pc = r.getRegister(UNW_REG_IP);
622 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
623 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000624#endif
625}
626
627template <typename A, typename R>
628UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
629 : _addressSpace(as), _unwindInfoMissing(false) {
630 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
631 "UnwindCursor<> does not fit in unw_cursor_t");
632 memset(&_info, 0, sizeof(_info));
633 memset(&_histTable, 0, sizeof(_histTable));
634 _dispContext.ContextRecord = &_msContext;
635 _dispContext.HistoryTable = &_histTable;
636 _msContext = *context;
637}
638
639
640template <typename A, typename R>
641bool UnwindCursor<A, R>::validReg(int regNum) {
642 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
643#if defined(_LIBUNWIND_TARGET_X86_64)
644 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
645#elif defined(_LIBUNWIND_TARGET_ARM)
646 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000647#elif defined(_LIBUNWIND_TARGET_AARCH64)
648 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000649#endif
650 return false;
651}
652
653template <typename A, typename R>
654unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
655 switch (regNum) {
656#if defined(_LIBUNWIND_TARGET_X86_64)
657 case UNW_REG_IP: return _msContext.Rip;
658 case UNW_X86_64_RAX: return _msContext.Rax;
659 case UNW_X86_64_RDX: return _msContext.Rdx;
660 case UNW_X86_64_RCX: return _msContext.Rcx;
661 case UNW_X86_64_RBX: return _msContext.Rbx;
662 case UNW_REG_SP:
663 case UNW_X86_64_RSP: return _msContext.Rsp;
664 case UNW_X86_64_RBP: return _msContext.Rbp;
665 case UNW_X86_64_RSI: return _msContext.Rsi;
666 case UNW_X86_64_RDI: return _msContext.Rdi;
667 case UNW_X86_64_R8: return _msContext.R8;
668 case UNW_X86_64_R9: return _msContext.R9;
669 case UNW_X86_64_R10: return _msContext.R10;
670 case UNW_X86_64_R11: return _msContext.R11;
671 case UNW_X86_64_R12: return _msContext.R12;
672 case UNW_X86_64_R13: return _msContext.R13;
673 case UNW_X86_64_R14: return _msContext.R14;
674 case UNW_X86_64_R15: return _msContext.R15;
675#elif defined(_LIBUNWIND_TARGET_ARM)
676 case UNW_ARM_R0: return _msContext.R0;
677 case UNW_ARM_R1: return _msContext.R1;
678 case UNW_ARM_R2: return _msContext.R2;
679 case UNW_ARM_R3: return _msContext.R3;
680 case UNW_ARM_R4: return _msContext.R4;
681 case UNW_ARM_R5: return _msContext.R5;
682 case UNW_ARM_R6: return _msContext.R6;
683 case UNW_ARM_R7: return _msContext.R7;
684 case UNW_ARM_R8: return _msContext.R8;
685 case UNW_ARM_R9: return _msContext.R9;
686 case UNW_ARM_R10: return _msContext.R10;
687 case UNW_ARM_R11: return _msContext.R11;
688 case UNW_ARM_R12: return _msContext.R12;
689 case UNW_REG_SP:
690 case UNW_ARM_SP: return _msContext.Sp;
691 case UNW_ARM_LR: return _msContext.Lr;
692 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000693 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000694#elif defined(_LIBUNWIND_TARGET_AARCH64)
695 case UNW_REG_SP: return _msContext.Sp;
696 case UNW_REG_IP: return _msContext.Pc;
697 default: return _msContext.X[regNum - UNW_ARM64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000698#endif
699 }
700 _LIBUNWIND_ABORT("unsupported register");
701}
702
703template <typename A, typename R>
704void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
705 switch (regNum) {
706#if defined(_LIBUNWIND_TARGET_X86_64)
707 case UNW_REG_IP: _msContext.Rip = value; break;
708 case UNW_X86_64_RAX: _msContext.Rax = value; break;
709 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
710 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
711 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
712 case UNW_REG_SP:
713 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
714 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
715 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
716 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
717 case UNW_X86_64_R8: _msContext.R8 = value; break;
718 case UNW_X86_64_R9: _msContext.R9 = value; break;
719 case UNW_X86_64_R10: _msContext.R10 = value; break;
720 case UNW_X86_64_R11: _msContext.R11 = value; break;
721 case UNW_X86_64_R12: _msContext.R12 = value; break;
722 case UNW_X86_64_R13: _msContext.R13 = value; break;
723 case UNW_X86_64_R14: _msContext.R14 = value; break;
724 case UNW_X86_64_R15: _msContext.R15 = value; break;
725#elif defined(_LIBUNWIND_TARGET_ARM)
726 case UNW_ARM_R0: _msContext.R0 = value; break;
727 case UNW_ARM_R1: _msContext.R1 = value; break;
728 case UNW_ARM_R2: _msContext.R2 = value; break;
729 case UNW_ARM_R3: _msContext.R3 = value; break;
730 case UNW_ARM_R4: _msContext.R4 = value; break;
731 case UNW_ARM_R5: _msContext.R5 = value; break;
732 case UNW_ARM_R6: _msContext.R6 = value; break;
733 case UNW_ARM_R7: _msContext.R7 = value; break;
734 case UNW_ARM_R8: _msContext.R8 = value; break;
735 case UNW_ARM_R9: _msContext.R9 = value; break;
736 case UNW_ARM_R10: _msContext.R10 = value; break;
737 case UNW_ARM_R11: _msContext.R11 = value; break;
738 case UNW_ARM_R12: _msContext.R12 = value; break;
739 case UNW_REG_SP:
740 case UNW_ARM_SP: _msContext.Sp = value; break;
741 case UNW_ARM_LR: _msContext.Lr = value; break;
742 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000743 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000744#elif defined(_LIBUNWIND_TARGET_AARCH64)
745 case UNW_REG_SP: _msContext.Sp = value; break;
746 case UNW_REG_IP: _msContext.Pc = value; break;
747 case UNW_ARM64_X0:
748 case UNW_ARM64_X1:
749 case UNW_ARM64_X2:
750 case UNW_ARM64_X3:
751 case UNW_ARM64_X4:
752 case UNW_ARM64_X5:
753 case UNW_ARM64_X6:
754 case UNW_ARM64_X7:
755 case UNW_ARM64_X8:
756 case UNW_ARM64_X9:
757 case UNW_ARM64_X10:
758 case UNW_ARM64_X11:
759 case UNW_ARM64_X12:
760 case UNW_ARM64_X13:
761 case UNW_ARM64_X14:
762 case UNW_ARM64_X15:
763 case UNW_ARM64_X16:
764 case UNW_ARM64_X17:
765 case UNW_ARM64_X18:
766 case UNW_ARM64_X19:
767 case UNW_ARM64_X20:
768 case UNW_ARM64_X21:
769 case UNW_ARM64_X22:
770 case UNW_ARM64_X23:
771 case UNW_ARM64_X24:
772 case UNW_ARM64_X25:
773 case UNW_ARM64_X26:
774 case UNW_ARM64_X27:
775 case UNW_ARM64_X28:
776 case UNW_ARM64_FP:
777 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000778#endif
779 default:
780 _LIBUNWIND_ABORT("unsupported register");
781 }
782}
783
784template <typename A, typename R>
785bool UnwindCursor<A, R>::validFloatReg(int regNum) {
786#if defined(_LIBUNWIND_TARGET_ARM)
787 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
788 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000789#elif defined(_LIBUNWIND_TARGET_AARCH64)
790 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000791#else
792 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000793#endif
794 return false;
795}
796
797template <typename A, typename R>
798unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
799#if defined(_LIBUNWIND_TARGET_ARM)
800 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
801 union {
802 uint32_t w;
803 float f;
804 } d;
805 d.w = _msContext.S[regNum - UNW_ARM_S0];
806 return d.f;
807 }
808 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
809 union {
810 uint64_t w;
811 double d;
812 } d;
813 d.w = _msContext.D[regNum - UNW_ARM_D0];
814 return d.d;
815 }
816 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000817#elif defined(_LIBUNWIND_TARGET_AARCH64)
818 return _msContext.V[regNum - UNW_ARM64_D0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000819#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000820 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000821 _LIBUNWIND_ABORT("float registers unimplemented");
822#endif
823}
824
825template <typename A, typename R>
826void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
827#if defined(_LIBUNWIND_TARGET_ARM)
828 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
829 union {
830 uint32_t w;
831 float f;
832 } d;
833 d.f = value;
834 _msContext.S[regNum - UNW_ARM_S0] = d.w;
835 }
836 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
837 union {
838 uint64_t w;
839 double d;
840 } d;
841 d.d = value;
842 _msContext.D[regNum - UNW_ARM_D0] = d.w;
843 }
844 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000845#elif defined(_LIBUNWIND_TARGET_AARCH64)
846 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000847#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000848 (void)regNum;
849 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000850 _LIBUNWIND_ABORT("float registers unimplemented");
851#endif
852}
853
854template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
855 RtlRestoreContext(&_msContext, nullptr);
856}
857
858#ifdef __arm__
859template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
860#endif
861
862template <typename A, typename R>
863const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000864 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000865}
866
867template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
868 return false;
869}
870
871#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
872
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000873/// UnwindCursor contains all state (including all register values) during
874/// an unwind. This is normally stack allocated inside a unw_cursor_t.
875template <typename A, typename R>
876class UnwindCursor : public AbstractUnwindCursor{
877 typedef typename A::pint_t pint_t;
878public:
879 UnwindCursor(unw_context_t *context, A &as);
880 UnwindCursor(A &as, void *threadArg);
881 virtual ~UnwindCursor() {}
882 virtual bool validReg(int);
883 virtual unw_word_t getReg(int);
884 virtual void setReg(int, unw_word_t);
885 virtual bool validFloatReg(int);
886 virtual unw_fpreg_t getFloatReg(int);
887 virtual void setFloatReg(int, unw_fpreg_t);
888 virtual int step();
889 virtual void getInfo(unw_proc_info_t *);
890 virtual void jumpto();
891 virtual bool isSignalFrame();
892 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
893 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
894 virtual const char *getRegisterName(int num);
895#ifdef __arm__
896 virtual void saveVFPAsX();
897#endif
898
899private:
900
Ranjeet Singh421231a2017-03-31 15:28:06 +0000901#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000902 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000903
904 int stepWithEHABI() {
905 size_t len = 0;
906 size_t off = 0;
907 // FIXME: Calling decode_eht_entry() here is violating the libunwind
908 // abstraction layer.
909 const uint32_t *ehtp =
910 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
911 &off, &len);
912 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
913 _URC_CONTINUE_UNWIND)
914 return UNW_STEP_END;
915 return UNW_STEP_SUCCESS;
916 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000917#endif
918
Ranjeet Singh421231a2017-03-31 15:28:06 +0000919#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000920 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
921 uint32_t fdeSectionOffsetHint=0);
922 int stepWithDwarfFDE() {
923 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
924 (pint_t)this->getReg(UNW_REG_IP),
925 (pint_t)_info.unwind_info,
926 _registers);
927 }
928#endif
929
Ranjeet Singh421231a2017-03-31 15:28:06 +0000930#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000931 bool getInfoFromCompactEncodingSection(pint_t pc,
932 const UnwindInfoSections &sects);
933 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000934 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000935 if ( compactSaysUseDwarf() )
936 return stepWithDwarfFDE();
937 #endif
938 R dummy;
939 return stepWithCompactEncoding(dummy);
940 }
941
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000942#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000943 int stepWithCompactEncoding(Registers_x86_64 &) {
944 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
945 _info.format, _info.start_ip, _addressSpace, _registers);
946 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000947#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000948
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000949#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000950 int stepWithCompactEncoding(Registers_x86 &) {
951 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
952 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
953 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000954#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000955
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000956#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000957 int stepWithCompactEncoding(Registers_ppc &) {
958 return UNW_EINVAL;
959 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000960#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000961
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000962#if defined(_LIBUNWIND_TARGET_PPC64)
963 int stepWithCompactEncoding(Registers_ppc64 &) {
964 return UNW_EINVAL;
965 }
966#endif
967
968
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000969#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000970 int stepWithCompactEncoding(Registers_arm64 &) {
971 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
972 _info.format, _info.start_ip, _addressSpace, _registers);
973 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000974#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000975
John Baldwin56441d42017-12-12 21:43:36 +0000976#if defined(_LIBUNWIND_TARGET_MIPS_O32)
977 int stepWithCompactEncoding(Registers_mips_o32 &) {
978 return UNW_EINVAL;
979 }
980#endif
981
John Baldwin541a4352018-01-09 17:07:18 +0000982#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
983 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000984 return UNW_EINVAL;
985 }
986#endif
987
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000988#if defined(_LIBUNWIND_TARGET_SPARC)
989 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
990#endif
991
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000992 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
993 R dummy;
994 return compactSaysUseDwarf(dummy, offset);
995 }
996
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000997#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000998 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
999 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1000 if (offset)
1001 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1002 return true;
1003 }
1004 return false;
1005 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001006#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001007
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001008#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001009 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1010 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1011 if (offset)
1012 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1013 return true;
1014 }
1015 return false;
1016 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001017#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001018
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001019#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001020 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1021 return true;
1022 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001023#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001024
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001025#if defined(_LIBUNWIND_TARGET_PPC64)
1026 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1027 return true;
1028 }
1029#endif
1030
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001031#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001032 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1033 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1034 if (offset)
1035 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1036 return true;
1037 }
1038 return false;
1039 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001040#endif
John Baldwin56441d42017-12-12 21:43:36 +00001041
1042#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1043 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1044 return true;
1045 }
1046#endif
1047
John Baldwin541a4352018-01-09 17:07:18 +00001048#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1049 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001050 return true;
1051 }
1052#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001053
1054#if defined(_LIBUNWIND_TARGET_SPARC)
1055 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1056#endif
1057
Ranjeet Singh421231a2017-03-31 15:28:06 +00001058#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001059
Ranjeet Singh421231a2017-03-31 15:28:06 +00001060#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001061 compact_unwind_encoding_t dwarfEncoding() const {
1062 R dummy;
1063 return dwarfEncoding(dummy);
1064 }
1065
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001066#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001067 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1068 return UNWIND_X86_64_MODE_DWARF;
1069 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001070#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001071
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001072#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001073 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1074 return UNWIND_X86_MODE_DWARF;
1075 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001076#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001077
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001078#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001079 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1080 return 0;
1081 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001082#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001083
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001084#if defined(_LIBUNWIND_TARGET_PPC64)
1085 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1086 return 0;
1087 }
1088#endif
1089
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001090#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001091 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1092 return UNWIND_ARM64_MODE_DWARF;
1093 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001094#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001095
Martin Storsjoa72285f2017-11-02 08:16:16 +00001096#if defined(_LIBUNWIND_TARGET_ARM)
1097 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1098 return 0;
1099 }
1100#endif
1101
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001102#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001103 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1104 return 0;
1105 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001106#endif
John Baldwin56441d42017-12-12 21:43:36 +00001107
1108#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1109 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1110 return 0;
1111 }
1112#endif
1113
John Baldwin541a4352018-01-09 17:07:18 +00001114#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1115 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001116 return 0;
1117 }
1118#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001119
1120#if defined(_LIBUNWIND_TARGET_SPARC)
1121 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1122#endif
1123
Ranjeet Singh421231a2017-03-31 15:28:06 +00001124#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001125
Charles Davisfa2e6202018-08-30 21:29:00 +00001126#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1127 // For runtime environments using SEH unwind data without Windows runtime
1128 // support.
1129 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1130 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1131 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1132 /* FIXME: Implement */
1133 *base = 0;
1134 return nullptr;
1135 }
1136 bool getInfoFromSEH(pint_t pc);
1137 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1138#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1139
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001140
1141 A &_addressSpace;
1142 R _registers;
1143 unw_proc_info_t _info;
1144 bool _unwindInfoMissing;
1145 bool _isSignalFrame;
1146};
1147
1148
1149template <typename A, typename R>
1150UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1151 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1152 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001153 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001154 "UnwindCursor<> does not fit in unw_cursor_t");
1155 memset(&_info, 0, sizeof(_info));
1156}
1157
1158template <typename A, typename R>
1159UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1160 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1161 memset(&_info, 0, sizeof(_info));
1162 // FIXME
1163 // fill in _registers from thread arg
1164}
1165
1166
1167template <typename A, typename R>
1168bool UnwindCursor<A, R>::validReg(int regNum) {
1169 return _registers.validRegister(regNum);
1170}
1171
1172template <typename A, typename R>
1173unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1174 return _registers.getRegister(regNum);
1175}
1176
1177template <typename A, typename R>
1178void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1179 _registers.setRegister(regNum, (typename A::pint_t)value);
1180}
1181
1182template <typename A, typename R>
1183bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1184 return _registers.validFloatRegister(regNum);
1185}
1186
1187template <typename A, typename R>
1188unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1189 return _registers.getFloatRegister(regNum);
1190}
1191
1192template <typename A, typename R>
1193void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1194 _registers.setFloatRegister(regNum, value);
1195}
1196
1197template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1198 _registers.jumpto();
1199}
1200
1201#ifdef __arm__
1202template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1203 _registers.saveVFPAsX();
1204}
1205#endif
1206
1207template <typename A, typename R>
1208const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1209 return _registers.getRegisterName(regNum);
1210}
1211
1212template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1213 return _isSignalFrame;
1214}
1215
Charles Davisfa2e6202018-08-30 21:29:00 +00001216#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1217
Ranjeet Singh421231a2017-03-31 15:28:06 +00001218#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001219struct EHABIIndexEntry {
1220 uint32_t functionOffset;
1221 uint32_t data;
1222};
1223
1224template<typename A>
1225struct EHABISectionIterator {
1226 typedef EHABISectionIterator _Self;
1227
1228 typedef std::random_access_iterator_tag iterator_category;
1229 typedef typename A::pint_t value_type;
1230 typedef typename A::pint_t* pointer;
1231 typedef typename A::pint_t& reference;
1232 typedef size_t size_type;
1233 typedef size_t difference_type;
1234
1235 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1236 return _Self(addressSpace, sects, 0);
1237 }
1238 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001239 return _Self(addressSpace, sects,
1240 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001241 }
1242
1243 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1244 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1245
1246 _Self& operator++() { ++_i; return *this; }
1247 _Self& operator+=(size_t a) { _i += a; return *this; }
1248 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1249 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1250
1251 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1252 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1253
1254 size_t operator-(const _Self& other) { return _i - other._i; }
1255
1256 bool operator==(const _Self& other) const {
1257 assert(_addressSpace == other._addressSpace);
1258 assert(_sects == other._sects);
1259 return _i == other._i;
1260 }
1261
1262 typename A::pint_t operator*() const { return functionAddress(); }
1263
1264 typename A::pint_t functionAddress() const {
1265 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1266 EHABIIndexEntry, _i, functionOffset);
1267 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1268 }
1269
1270 typename A::pint_t dataAddress() {
1271 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1272 EHABIIndexEntry, _i, data);
1273 return indexAddr;
1274 }
1275
1276 private:
1277 size_t _i;
1278 A* _addressSpace;
1279 const UnwindInfoSections* _sects;
1280};
1281
1282template <typename A, typename R>
1283bool UnwindCursor<A, R>::getInfoFromEHABISection(
1284 pint_t pc,
1285 const UnwindInfoSections &sects) {
1286 EHABISectionIterator<A> begin =
1287 EHABISectionIterator<A>::begin(_addressSpace, sects);
1288 EHABISectionIterator<A> end =
1289 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001290 if (begin == end)
1291 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001292
1293 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001294 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001295 return false;
1296 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1297
1298 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001299 // If an exception is thrown from a function, corresponding to the last entry
1300 // in the table, we don't really know the function extent and have to choose a
1301 // value for nextPC. Choosing max() will allow the range check during trace to
1302 // succeed.
1303 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
1304 : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001305 pint_t indexDataAddr = itThisPC.dataAddress();
1306
1307 if (indexDataAddr == 0)
1308 return false;
1309
1310 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1311 if (indexData == UNW_EXIDX_CANTUNWIND)
1312 return false;
1313
1314 // If the high bit is set, the exception handling table entry is inline inside
1315 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1316 // the table points at an offset in the exception handling table (section 5 EHABI).
1317 pint_t exceptionTableAddr;
1318 uint32_t exceptionTableData;
1319 bool isSingleWordEHT;
1320 if (indexData & 0x80000000) {
1321 exceptionTableAddr = indexDataAddr;
1322 // TODO(ajwong): Should this data be 0?
1323 exceptionTableData = indexData;
1324 isSingleWordEHT = true;
1325 } else {
1326 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1327 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1328 isSingleWordEHT = false;
1329 }
1330
1331 // Now we know the 3 things:
1332 // exceptionTableAddr -- exception handler table entry.
1333 // exceptionTableData -- the data inside the first word of the eht entry.
1334 // isSingleWordEHT -- whether the entry is in the index.
1335 unw_word_t personalityRoutine = 0xbadf00d;
1336 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001337 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001338
1339 // If the high bit in the exception handling table entry is set, the entry is
1340 // in compact form (section 6.3 EHABI).
1341 if (exceptionTableData & 0x80000000) {
1342 // Grab the index of the personality routine from the compact form.
1343 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1344 uint32_t extraWords = 0;
1345 switch (choice) {
1346 case 0:
1347 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1348 extraWords = 0;
1349 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001350 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001351 break;
1352 case 1:
1353 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1354 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1355 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001356 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001357 break;
1358 case 2:
1359 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1360 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1361 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001362 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001363 break;
1364 default:
1365 _LIBUNWIND_ABORT("unknown personality routine");
1366 return false;
1367 }
1368
1369 if (isSingleWordEHT) {
1370 if (extraWords != 0) {
1371 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1372 "requires extra words");
1373 return false;
1374 }
1375 }
1376 } else {
1377 pint_t personalityAddr =
1378 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1379 personalityRoutine = personalityAddr;
1380
1381 // ARM EHABI # 6.2, # 9.2
1382 //
1383 // +---- ehtp
1384 // v
1385 // +--------------------------------------+
1386 // | +--------+--------+--------+-------+ |
1387 // | |0| prel31 to personalityRoutine | |
1388 // | +--------+--------+--------+-------+ |
1389 // | | N | unwind opcodes | | <-- UnwindData
1390 // | +--------+--------+--------+-------+ |
1391 // | | Word 2 unwind opcodes | |
1392 // | +--------+--------+--------+-------+ |
1393 // | ... |
1394 // | +--------+--------+--------+-------+ |
1395 // | | Word N unwind opcodes | |
1396 // | +--------+--------+--------+-------+ |
1397 // | | LSDA | | <-- lsda
1398 // | | ... | |
1399 // | +--------+--------+--------+-------+ |
1400 // +--------------------------------------+
1401
1402 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1403 uint32_t FirstDataWord = *UnwindData;
1404 size_t N = ((FirstDataWord >> 24) & 0xff);
1405 size_t NDataWords = N + 1;
1406 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1407 }
1408
1409 _info.start_ip = thisPC;
1410 _info.end_ip = nextPC;
1411 _info.handler = personalityRoutine;
1412 _info.unwind_info = exceptionTableAddr;
1413 _info.lsda = lsda;
1414 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1415 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1416
1417 return true;
1418}
1419#endif
1420
Ranjeet Singh421231a2017-03-31 15:28:06 +00001421#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001422template <typename A, typename R>
1423bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1424 const UnwindInfoSections &sects,
1425 uint32_t fdeSectionOffsetHint) {
1426 typename CFI_Parser<A>::FDE_Info fdeInfo;
1427 typename CFI_Parser<A>::CIE_Info cieInfo;
1428 bool foundFDE = false;
1429 bool foundInCache = false;
1430 // If compact encoding table gave offset into dwarf section, go directly there
1431 if (fdeSectionOffsetHint != 0) {
1432 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1433 (uint32_t)sects.dwarf_section_length,
1434 sects.dwarf_section + fdeSectionOffsetHint,
1435 &fdeInfo, &cieInfo);
1436 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001437#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001438 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1439 foundFDE = EHHeaderParser<A>::findFDE(
1440 _addressSpace, pc, sects.dwarf_index_section,
1441 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1442 }
1443#endif
1444 if (!foundFDE) {
1445 // otherwise, search cache of previously found FDEs.
1446 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1447 if (cachedFDE != 0) {
1448 foundFDE =
1449 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1450 (uint32_t)sects.dwarf_section_length,
1451 cachedFDE, &fdeInfo, &cieInfo);
1452 foundInCache = foundFDE;
1453 }
1454 }
1455 if (!foundFDE) {
1456 // Still not found, do full scan of __eh_frame section.
1457 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1458 (uint32_t)sects.dwarf_section_length, 0,
1459 &fdeInfo, &cieInfo);
1460 }
1461 if (foundFDE) {
1462 typename CFI_Parser<A>::PrologInfo prolog;
1463 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001464 R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001465 // Save off parsed FDE info
1466 _info.start_ip = fdeInfo.pcStart;
1467 _info.end_ip = fdeInfo.pcEnd;
1468 _info.lsda = fdeInfo.lsda;
1469 _info.handler = cieInfo.personality;
1470 _info.gp = prolog.spExtraArgSize;
1471 _info.flags = 0;
1472 _info.format = dwarfEncoding();
1473 _info.unwind_info = fdeInfo.fdeStart;
1474 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1475 _info.extra = (unw_word_t) sects.dso_base;
1476
1477 // Add to cache (to make next lookup faster) if we had no hint
1478 // and there was no index.
1479 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001480 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001481 if (sects.dwarf_index_section == 0)
1482 #endif
1483 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1484 fdeInfo.fdeStart);
1485 }
1486 return true;
1487 }
1488 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001489 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001490 return false;
1491}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001492#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001493
1494
Ranjeet Singh421231a2017-03-31 15:28:06 +00001495#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001496template <typename A, typename R>
1497bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1498 const UnwindInfoSections &sects) {
1499 const bool log = false;
1500 if (log)
1501 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1502 (uint64_t)pc, (uint64_t)sects.dso_base);
1503
1504 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1505 sects.compact_unwind_section);
1506 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1507 return false;
1508
1509 // do a binary search of top level index to find page with unwind info
1510 pint_t targetFunctionOffset = pc - sects.dso_base;
1511 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1512 sects.compact_unwind_section
1513 + sectionHeader.indexSectionOffset());
1514 uint32_t low = 0;
1515 uint32_t high = sectionHeader.indexCount();
1516 uint32_t last = high - 1;
1517 while (low < high) {
1518 uint32_t mid = (low + high) / 2;
1519 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1520 //mid, low, high, topIndex.functionOffset(mid));
1521 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1522 if ((mid == last) ||
1523 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1524 low = mid;
1525 break;
1526 } else {
1527 low = mid + 1;
1528 }
1529 } else {
1530 high = mid;
1531 }
1532 }
1533 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1534 const uint32_t firstLevelNextPageFunctionOffset =
1535 topIndex.functionOffset(low + 1);
1536 const pint_t secondLevelAddr =
1537 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1538 const pint_t lsdaArrayStartAddr =
1539 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1540 const pint_t lsdaArrayEndAddr =
1541 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1542 if (log)
1543 fprintf(stderr, "\tfirst level search for result index=%d "
1544 "to secondLevelAddr=0x%llX\n",
1545 low, (uint64_t) secondLevelAddr);
1546 // do a binary search of second level page index
1547 uint32_t encoding = 0;
1548 pint_t funcStart = 0;
1549 pint_t funcEnd = 0;
1550 pint_t lsda = 0;
1551 pint_t personality = 0;
1552 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1553 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1554 // regular page
1555 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1556 secondLevelAddr);
1557 UnwindSectionRegularArray<A> pageIndex(
1558 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1559 // binary search looks for entry with e where index[e].offset <= pc <
1560 // index[e+1].offset
1561 if (log)
1562 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1563 "regular page starting at secondLevelAddr=0x%llX\n",
1564 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1565 low = 0;
1566 high = pageHeader.entryCount();
1567 while (low < high) {
1568 uint32_t mid = (low + high) / 2;
1569 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1570 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1571 // at end of table
1572 low = mid;
1573 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1574 break;
1575 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1576 // next is too big, so we found it
1577 low = mid;
1578 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1579 break;
1580 } else {
1581 low = mid + 1;
1582 }
1583 } else {
1584 high = mid;
1585 }
1586 }
1587 encoding = pageIndex.encoding(low);
1588 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1589 if (pc < funcStart) {
1590 if (log)
1591 fprintf(
1592 stderr,
1593 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1594 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1595 return false;
1596 }
1597 if (pc > funcEnd) {
1598 if (log)
1599 fprintf(
1600 stderr,
1601 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1602 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1603 return false;
1604 }
1605 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1606 // compressed page
1607 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1608 secondLevelAddr);
1609 UnwindSectionCompressedArray<A> pageIndex(
1610 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1611 const uint32_t targetFunctionPageOffset =
1612 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1613 // binary search looks for entry with e where index[e].offset <= pc <
1614 // index[e+1].offset
1615 if (log)
1616 fprintf(stderr, "\tbinary search of compressed page starting at "
1617 "secondLevelAddr=0x%llX\n",
1618 (uint64_t) secondLevelAddr);
1619 low = 0;
1620 last = pageHeader.entryCount() - 1;
1621 high = pageHeader.entryCount();
1622 while (low < high) {
1623 uint32_t mid = (low + high) / 2;
1624 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1625 if ((mid == last) ||
1626 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1627 low = mid;
1628 break;
1629 } else {
1630 low = mid + 1;
1631 }
1632 } else {
1633 high = mid;
1634 }
1635 }
1636 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1637 + sects.dso_base;
1638 if (low < last)
1639 funcEnd =
1640 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1641 + sects.dso_base;
1642 else
1643 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1644 if (pc < funcStart) {
1645 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001646 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001647 (uint64_t) pc, (uint64_t) funcStart);
1648 return false;
1649 }
1650 if (pc > funcEnd) {
1651 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001652 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001653 (uint64_t) pc, (uint64_t) funcEnd);
1654 return false;
1655 }
1656 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1657 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1658 // encoding is in common table in section header
1659 encoding = _addressSpace.get32(
1660 sects.compact_unwind_section +
1661 sectionHeader.commonEncodingsArraySectionOffset() +
1662 encodingIndex * sizeof(uint32_t));
1663 } else {
1664 // encoding is in page specific table
1665 uint16_t pageEncodingIndex =
1666 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1667 encoding = _addressSpace.get32(secondLevelAddr +
1668 pageHeader.encodingsPageOffset() +
1669 pageEncodingIndex * sizeof(uint32_t));
1670 }
1671 } else {
1672 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001673 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001674 (uint64_t) sects.compact_unwind_section);
1675 return false;
1676 }
1677
1678 // look up LSDA, if encoding says function has one
1679 if (encoding & UNWIND_HAS_LSDA) {
1680 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1681 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1682 low = 0;
1683 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1684 sizeof(unwind_info_section_header_lsda_index_entry);
1685 // binary search looks for entry with exact match for functionOffset
1686 if (log)
1687 fprintf(stderr,
1688 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1689 funcStartOffset);
1690 while (low < high) {
1691 uint32_t mid = (low + high) / 2;
1692 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1693 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1694 break;
1695 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1696 low = mid + 1;
1697 } else {
1698 high = mid;
1699 }
1700 }
1701 if (lsda == 0) {
1702 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001703 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001704 encoding, (uint64_t) pc);
1705 return false;
1706 }
1707 }
1708
1709 // extact personality routine, if encoding says function has one
1710 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1711 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1712 if (personalityIndex != 0) {
1713 --personalityIndex; // change 1-based to zero-based index
1714 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1715 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001716 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001717 encoding, personalityIndex,
1718 sectionHeader.personalityArrayCount());
1719 return false;
1720 }
1721 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1722 sects.compact_unwind_section +
1723 sectionHeader.personalityArraySectionOffset() +
1724 personalityIndex * sizeof(uint32_t));
1725 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1726 personality = _addressSpace.getP(personalityPointer);
1727 if (log)
1728 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1729 "personalityDelta=0x%08X, personality=0x%08llX\n",
1730 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1731 }
1732
1733 if (log)
1734 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1735 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1736 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1737 _info.start_ip = funcStart;
1738 _info.end_ip = funcEnd;
1739 _info.lsda = lsda;
1740 _info.handler = personality;
1741 _info.gp = 0;
1742 _info.flags = 0;
1743 _info.format = encoding;
1744 _info.unwind_info = 0;
1745 _info.unwind_info_size = 0;
1746 _info.extra = sects.dso_base;
1747 return true;
1748}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001749#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001750
1751
Charles Davisfa2e6202018-08-30 21:29:00 +00001752#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1753template <typename A, typename R>
1754bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1755 pint_t base;
1756 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1757 if (!unwindEntry) {
1758 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1759 return false;
1760 }
1761 _info.gp = 0;
1762 _info.flags = 0;
1763 _info.format = 0;
1764 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1765 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1766 _info.extra = base;
1767 _info.start_ip = base + unwindEntry->BeginAddress;
1768#ifdef _LIBUNWIND_TARGET_X86_64
1769 _info.end_ip = base + unwindEntry->EndAddress;
1770 // Only fill in the handler and LSDA if they're stale.
1771 if (pc != getLastPC()) {
1772 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1773 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1774 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1775 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1776 // these structures.)
1777 // N.B. UNWIND_INFO structs are DWORD-aligned.
1778 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1779 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1780 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1781 if (*handler) {
1782 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1783 } else
1784 _info.handler = 0;
1785 } else {
1786 _info.lsda = 0;
1787 _info.handler = 0;
1788 }
1789 }
1790#elif defined(_LIBUNWIND_TARGET_ARM)
1791 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1792 _info.lsda = 0; // FIXME
1793 _info.handler = 0; // FIXME
1794#endif
1795 setLastPC(pc);
1796 return true;
1797}
1798#endif
1799
1800
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001801template <typename A, typename R>
1802void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1803 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001804#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001805 // Remove the thumb bit so the IP represents the actual instruction address.
1806 // This matches the behaviour of _Unwind_GetIP on arm.
1807 pc &= (pint_t)~0x1;
1808#endif
1809
1810 // If the last line of a function is a "throw" the compiler sometimes
1811 // emits no instructions after the call to __cxa_throw. This means
1812 // the return address is actually the start of the next function.
1813 // To disambiguate this, back up the pc when we know it is a return
1814 // address.
1815 if (isReturnAddress)
1816 --pc;
1817
1818 // Ask address space object to find unwind sections for this pc.
1819 UnwindInfoSections sects;
1820 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001821#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001822 // If there is a compact unwind encoding table, look there first.
1823 if (sects.compact_unwind_section != 0) {
1824 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001825 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001826 // Found info in table, done unless encoding says to use dwarf.
1827 uint32_t dwarfOffset;
1828 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1829 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1830 // found info in dwarf, done
1831 return;
1832 }
1833 }
1834 #endif
1835 // If unwind table has entry, but entry says there is no unwind info,
1836 // record that we have no unwind info.
1837 if (_info.format == 0)
1838 _unwindInfoMissing = true;
1839 return;
1840 }
1841 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001842#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001843
Charles Davisfa2e6202018-08-30 21:29:00 +00001844#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1845 // If there is SEH unwind info, look there next.
1846 if (this->getInfoFromSEH(pc))
1847 return;
1848#endif
1849
Ranjeet Singh421231a2017-03-31 15:28:06 +00001850#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001851 // If there is dwarf unwind info, look there next.
1852 if (sects.dwarf_section != 0) {
1853 if (this->getInfoFromDwarfSection(pc, sects)) {
1854 // found info in dwarf, done
1855 return;
1856 }
1857 }
1858#endif
1859
Ranjeet Singh421231a2017-03-31 15:28:06 +00001860#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001861 // If there is ARM EHABI unwind info, look there next.
1862 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1863 return;
1864#endif
1865 }
1866
Ranjeet Singh421231a2017-03-31 15:28:06 +00001867#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001868 // There is no static unwind info for this pc. Look to see if an FDE was
1869 // dynamically registered for it.
1870 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1871 if (cachedFDE != 0) {
1872 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1873 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1874 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1875 cachedFDE, &fdeInfo, &cieInfo);
1876 if (msg == NULL) {
1877 typename CFI_Parser<A>::PrologInfo prolog;
1878 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001879 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001880 // save off parsed FDE info
1881 _info.start_ip = fdeInfo.pcStart;
1882 _info.end_ip = fdeInfo.pcEnd;
1883 _info.lsda = fdeInfo.lsda;
1884 _info.handler = cieInfo.personality;
1885 _info.gp = prolog.spExtraArgSize;
1886 // Some frameless functions need SP
1887 // altered when resuming in function.
1888 _info.flags = 0;
1889 _info.format = dwarfEncoding();
1890 _info.unwind_info = fdeInfo.fdeStart;
1891 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1892 _info.extra = 0;
1893 return;
1894 }
1895 }
1896 }
1897
1898 // Lastly, ask AddressSpace object about platform specific ways to locate
1899 // other FDEs.
1900 pint_t fde;
1901 if (_addressSpace.findOtherFDE(pc, fde)) {
1902 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1903 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1904 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1905 // Double check this FDE is for a function that includes the pc.
1906 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1907 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001908 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1909 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001910 // save off parsed FDE info
1911 _info.start_ip = fdeInfo.pcStart;
1912 _info.end_ip = fdeInfo.pcEnd;
1913 _info.lsda = fdeInfo.lsda;
1914 _info.handler = cieInfo.personality;
1915 _info.gp = prolog.spExtraArgSize;
1916 _info.flags = 0;
1917 _info.format = dwarfEncoding();
1918 _info.unwind_info = fdeInfo.fdeStart;
1919 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1920 _info.extra = 0;
1921 return;
1922 }
1923 }
1924 }
1925 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001926#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001927
1928 // no unwind info, flag that we can't reliably unwind
1929 _unwindInfoMissing = true;
1930}
1931
1932template <typename A, typename R>
1933int UnwindCursor<A, R>::step() {
1934 // Bottom of stack is defined is when unwind info cannot be found.
1935 if (_unwindInfoMissing)
1936 return UNW_STEP_END;
1937
1938 // Use unwinding info to modify register set as if function returned.
1939 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001940#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001941 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001942#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1943 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001944#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001945 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001946#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001947 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001948#else
1949 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001950 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001951 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001952 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001953#endif
1954
1955 // update info based on new PC
1956 if (result == UNW_STEP_SUCCESS) {
1957 this->setInfoBasedOnIPRegister(true);
1958 if (_unwindInfoMissing)
1959 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001960 }
1961
1962 return result;
1963}
1964
1965template <typename A, typename R>
1966void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1967 *info = _info;
1968}
1969
1970template <typename A, typename R>
1971bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1972 unw_word_t *offset) {
1973 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1974 buf, bufLen, offset);
1975}
1976
1977} // namespace libunwind
1978
1979#endif // __UNWINDCURSOR_HPP__