blob: 6d3ef69775e919bcdb9f460979f32b158f060985 [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;
Charles Davisfa2e6202018-08-30 21:29:00 +0000791#endif
792 return false;
793}
794
795template <typename A, typename R>
796unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
797#if defined(_LIBUNWIND_TARGET_ARM)
798 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
799 union {
800 uint32_t w;
801 float f;
802 } d;
803 d.w = _msContext.S[regNum - UNW_ARM_S0];
804 return d.f;
805 }
806 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
807 union {
808 uint64_t w;
809 double d;
810 } d;
811 d.w = _msContext.D[regNum - UNW_ARM_D0];
812 return d.d;
813 }
814 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000815#elif defined(_LIBUNWIND_TARGET_AARCH64)
816 return _msContext.V[regNum - UNW_ARM64_D0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000817#else
818 _LIBUNWIND_ABORT("float registers unimplemented");
819#endif
820}
821
822template <typename A, typename R>
823void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
824#if defined(_LIBUNWIND_TARGET_ARM)
825 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
826 union {
827 uint32_t w;
828 float f;
829 } d;
830 d.f = value;
831 _msContext.S[regNum - UNW_ARM_S0] = d.w;
832 }
833 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
834 union {
835 uint64_t w;
836 double d;
837 } d;
838 d.d = value;
839 _msContext.D[regNum - UNW_ARM_D0] = d.w;
840 }
841 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000842#elif defined(_LIBUNWIND_TARGET_AARCH64)
843 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000844#else
845 _LIBUNWIND_ABORT("float registers unimplemented");
846#endif
847}
848
849template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
850 RtlRestoreContext(&_msContext, nullptr);
851}
852
853#ifdef __arm__
854template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
855#endif
856
857template <typename A, typename R>
858const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000859 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000860}
861
862template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
863 return false;
864}
865
866#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
867
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000868/// UnwindCursor contains all state (including all register values) during
869/// an unwind. This is normally stack allocated inside a unw_cursor_t.
870template <typename A, typename R>
871class UnwindCursor : public AbstractUnwindCursor{
872 typedef typename A::pint_t pint_t;
873public:
874 UnwindCursor(unw_context_t *context, A &as);
875 UnwindCursor(A &as, void *threadArg);
876 virtual ~UnwindCursor() {}
877 virtual bool validReg(int);
878 virtual unw_word_t getReg(int);
879 virtual void setReg(int, unw_word_t);
880 virtual bool validFloatReg(int);
881 virtual unw_fpreg_t getFloatReg(int);
882 virtual void setFloatReg(int, unw_fpreg_t);
883 virtual int step();
884 virtual void getInfo(unw_proc_info_t *);
885 virtual void jumpto();
886 virtual bool isSignalFrame();
887 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
888 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
889 virtual const char *getRegisterName(int num);
890#ifdef __arm__
891 virtual void saveVFPAsX();
892#endif
893
894private:
895
Ranjeet Singh421231a2017-03-31 15:28:06 +0000896#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000897 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000898
899 int stepWithEHABI() {
900 size_t len = 0;
901 size_t off = 0;
902 // FIXME: Calling decode_eht_entry() here is violating the libunwind
903 // abstraction layer.
904 const uint32_t *ehtp =
905 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
906 &off, &len);
907 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
908 _URC_CONTINUE_UNWIND)
909 return UNW_STEP_END;
910 return UNW_STEP_SUCCESS;
911 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000912#endif
913
Ranjeet Singh421231a2017-03-31 15:28:06 +0000914#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000915 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
916 uint32_t fdeSectionOffsetHint=0);
917 int stepWithDwarfFDE() {
918 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
919 (pint_t)this->getReg(UNW_REG_IP),
920 (pint_t)_info.unwind_info,
921 _registers);
922 }
923#endif
924
Ranjeet Singh421231a2017-03-31 15:28:06 +0000925#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000926 bool getInfoFromCompactEncodingSection(pint_t pc,
927 const UnwindInfoSections &sects);
928 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000929 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000930 if ( compactSaysUseDwarf() )
931 return stepWithDwarfFDE();
932 #endif
933 R dummy;
934 return stepWithCompactEncoding(dummy);
935 }
936
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000937#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000938 int stepWithCompactEncoding(Registers_x86_64 &) {
939 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
940 _info.format, _info.start_ip, _addressSpace, _registers);
941 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000942#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000943
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000944#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000945 int stepWithCompactEncoding(Registers_x86 &) {
946 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
947 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
948 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000949#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000950
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000951#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000952 int stepWithCompactEncoding(Registers_ppc &) {
953 return UNW_EINVAL;
954 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000955#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000956
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000957#if defined(_LIBUNWIND_TARGET_PPC64)
958 int stepWithCompactEncoding(Registers_ppc64 &) {
959 return UNW_EINVAL;
960 }
961#endif
962
963
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000964#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000965 int stepWithCompactEncoding(Registers_arm64 &) {
966 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
967 _info.format, _info.start_ip, _addressSpace, _registers);
968 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000969#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000970
John Baldwin56441d42017-12-12 21:43:36 +0000971#if defined(_LIBUNWIND_TARGET_MIPS_O32)
972 int stepWithCompactEncoding(Registers_mips_o32 &) {
973 return UNW_EINVAL;
974 }
975#endif
976
John Baldwin541a4352018-01-09 17:07:18 +0000977#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
978 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000979 return UNW_EINVAL;
980 }
981#endif
982
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000983#if defined(_LIBUNWIND_TARGET_SPARC)
984 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
985#endif
986
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000987 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
988 R dummy;
989 return compactSaysUseDwarf(dummy, offset);
990 }
991
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000992#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000993 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
994 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
995 if (offset)
996 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
997 return true;
998 }
999 return false;
1000 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001001#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001002
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001003#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001004 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1005 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1006 if (offset)
1007 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1008 return true;
1009 }
1010 return false;
1011 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001012#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001013
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001014#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001015 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1016 return true;
1017 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001018#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001019
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001020#if defined(_LIBUNWIND_TARGET_PPC64)
1021 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1022 return true;
1023 }
1024#endif
1025
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001026#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001027 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1028 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1029 if (offset)
1030 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1031 return true;
1032 }
1033 return false;
1034 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001035#endif
John Baldwin56441d42017-12-12 21:43:36 +00001036
1037#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1038 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1039 return true;
1040 }
1041#endif
1042
John Baldwin541a4352018-01-09 17:07:18 +00001043#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1044 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001045 return true;
1046 }
1047#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001048
1049#if defined(_LIBUNWIND_TARGET_SPARC)
1050 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1051#endif
1052
Ranjeet Singh421231a2017-03-31 15:28:06 +00001053#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001054
Ranjeet Singh421231a2017-03-31 15:28:06 +00001055#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001056 compact_unwind_encoding_t dwarfEncoding() const {
1057 R dummy;
1058 return dwarfEncoding(dummy);
1059 }
1060
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001061#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001062 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1063 return UNWIND_X86_64_MODE_DWARF;
1064 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001065#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001066
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001067#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001068 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1069 return UNWIND_X86_MODE_DWARF;
1070 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001071#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001072
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001073#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001074 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1075 return 0;
1076 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001077#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001078
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001079#if defined(_LIBUNWIND_TARGET_PPC64)
1080 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1081 return 0;
1082 }
1083#endif
1084
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001085#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001086 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1087 return UNWIND_ARM64_MODE_DWARF;
1088 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001089#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001090
Martin Storsjoa72285f2017-11-02 08:16:16 +00001091#if defined(_LIBUNWIND_TARGET_ARM)
1092 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1093 return 0;
1094 }
1095#endif
1096
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001097#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001098 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1099 return 0;
1100 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001101#endif
John Baldwin56441d42017-12-12 21:43:36 +00001102
1103#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1104 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1105 return 0;
1106 }
1107#endif
1108
John Baldwin541a4352018-01-09 17:07:18 +00001109#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1110 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001111 return 0;
1112 }
1113#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001114
1115#if defined(_LIBUNWIND_TARGET_SPARC)
1116 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1117#endif
1118
Ranjeet Singh421231a2017-03-31 15:28:06 +00001119#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001120
Charles Davisfa2e6202018-08-30 21:29:00 +00001121#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1122 // For runtime environments using SEH unwind data without Windows runtime
1123 // support.
1124 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1125 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1126 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1127 /* FIXME: Implement */
1128 *base = 0;
1129 return nullptr;
1130 }
1131 bool getInfoFromSEH(pint_t pc);
1132 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1133#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1134
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001135
1136 A &_addressSpace;
1137 R _registers;
1138 unw_proc_info_t _info;
1139 bool _unwindInfoMissing;
1140 bool _isSignalFrame;
1141};
1142
1143
1144template <typename A, typename R>
1145UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1146 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1147 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001148 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001149 "UnwindCursor<> does not fit in unw_cursor_t");
1150 memset(&_info, 0, sizeof(_info));
1151}
1152
1153template <typename A, typename R>
1154UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1155 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1156 memset(&_info, 0, sizeof(_info));
1157 // FIXME
1158 // fill in _registers from thread arg
1159}
1160
1161
1162template <typename A, typename R>
1163bool UnwindCursor<A, R>::validReg(int regNum) {
1164 return _registers.validRegister(regNum);
1165}
1166
1167template <typename A, typename R>
1168unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1169 return _registers.getRegister(regNum);
1170}
1171
1172template <typename A, typename R>
1173void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1174 _registers.setRegister(regNum, (typename A::pint_t)value);
1175}
1176
1177template <typename A, typename R>
1178bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1179 return _registers.validFloatRegister(regNum);
1180}
1181
1182template <typename A, typename R>
1183unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1184 return _registers.getFloatRegister(regNum);
1185}
1186
1187template <typename A, typename R>
1188void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1189 _registers.setFloatRegister(regNum, value);
1190}
1191
1192template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1193 _registers.jumpto();
1194}
1195
1196#ifdef __arm__
1197template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1198 _registers.saveVFPAsX();
1199}
1200#endif
1201
1202template <typename A, typename R>
1203const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1204 return _registers.getRegisterName(regNum);
1205}
1206
1207template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1208 return _isSignalFrame;
1209}
1210
Charles Davisfa2e6202018-08-30 21:29:00 +00001211#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1212
Ranjeet Singh421231a2017-03-31 15:28:06 +00001213#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001214struct EHABIIndexEntry {
1215 uint32_t functionOffset;
1216 uint32_t data;
1217};
1218
1219template<typename A>
1220struct EHABISectionIterator {
1221 typedef EHABISectionIterator _Self;
1222
1223 typedef std::random_access_iterator_tag iterator_category;
1224 typedef typename A::pint_t value_type;
1225 typedef typename A::pint_t* pointer;
1226 typedef typename A::pint_t& reference;
1227 typedef size_t size_type;
1228 typedef size_t difference_type;
1229
1230 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1231 return _Self(addressSpace, sects, 0);
1232 }
1233 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001234 return _Self(addressSpace, sects,
1235 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001236 }
1237
1238 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1239 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1240
1241 _Self& operator++() { ++_i; return *this; }
1242 _Self& operator+=(size_t a) { _i += a; return *this; }
1243 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1244 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1245
1246 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1247 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1248
1249 size_t operator-(const _Self& other) { return _i - other._i; }
1250
1251 bool operator==(const _Self& other) const {
1252 assert(_addressSpace == other._addressSpace);
1253 assert(_sects == other._sects);
1254 return _i == other._i;
1255 }
1256
1257 typename A::pint_t operator*() const { return functionAddress(); }
1258
1259 typename A::pint_t functionAddress() const {
1260 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1261 EHABIIndexEntry, _i, functionOffset);
1262 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1263 }
1264
1265 typename A::pint_t dataAddress() {
1266 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1267 EHABIIndexEntry, _i, data);
1268 return indexAddr;
1269 }
1270
1271 private:
1272 size_t _i;
1273 A* _addressSpace;
1274 const UnwindInfoSections* _sects;
1275};
1276
1277template <typename A, typename R>
1278bool UnwindCursor<A, R>::getInfoFromEHABISection(
1279 pint_t pc,
1280 const UnwindInfoSections &sects) {
1281 EHABISectionIterator<A> begin =
1282 EHABISectionIterator<A>::begin(_addressSpace, sects);
1283 EHABISectionIterator<A> end =
1284 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001285 if (begin == end)
1286 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001287
1288 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001289 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001290 return false;
1291 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1292
1293 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001294 // If an exception is thrown from a function, corresponding to the last entry
1295 // in the table, we don't really know the function extent and have to choose a
1296 // value for nextPC. Choosing max() will allow the range check during trace to
1297 // succeed.
1298 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
1299 : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001300 pint_t indexDataAddr = itThisPC.dataAddress();
1301
1302 if (indexDataAddr == 0)
1303 return false;
1304
1305 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1306 if (indexData == UNW_EXIDX_CANTUNWIND)
1307 return false;
1308
1309 // If the high bit is set, the exception handling table entry is inline inside
1310 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1311 // the table points at an offset in the exception handling table (section 5 EHABI).
1312 pint_t exceptionTableAddr;
1313 uint32_t exceptionTableData;
1314 bool isSingleWordEHT;
1315 if (indexData & 0x80000000) {
1316 exceptionTableAddr = indexDataAddr;
1317 // TODO(ajwong): Should this data be 0?
1318 exceptionTableData = indexData;
1319 isSingleWordEHT = true;
1320 } else {
1321 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1322 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1323 isSingleWordEHT = false;
1324 }
1325
1326 // Now we know the 3 things:
1327 // exceptionTableAddr -- exception handler table entry.
1328 // exceptionTableData -- the data inside the first word of the eht entry.
1329 // isSingleWordEHT -- whether the entry is in the index.
1330 unw_word_t personalityRoutine = 0xbadf00d;
1331 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001332 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001333
1334 // If the high bit in the exception handling table entry is set, the entry is
1335 // in compact form (section 6.3 EHABI).
1336 if (exceptionTableData & 0x80000000) {
1337 // Grab the index of the personality routine from the compact form.
1338 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1339 uint32_t extraWords = 0;
1340 switch (choice) {
1341 case 0:
1342 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1343 extraWords = 0;
1344 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001345 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001346 break;
1347 case 1:
1348 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1349 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1350 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001351 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001352 break;
1353 case 2:
1354 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1355 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1356 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001357 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001358 break;
1359 default:
1360 _LIBUNWIND_ABORT("unknown personality routine");
1361 return false;
1362 }
1363
1364 if (isSingleWordEHT) {
1365 if (extraWords != 0) {
1366 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1367 "requires extra words");
1368 return false;
1369 }
1370 }
1371 } else {
1372 pint_t personalityAddr =
1373 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1374 personalityRoutine = personalityAddr;
1375
1376 // ARM EHABI # 6.2, # 9.2
1377 //
1378 // +---- ehtp
1379 // v
1380 // +--------------------------------------+
1381 // | +--------+--------+--------+-------+ |
1382 // | |0| prel31 to personalityRoutine | |
1383 // | +--------+--------+--------+-------+ |
1384 // | | N | unwind opcodes | | <-- UnwindData
1385 // | +--------+--------+--------+-------+ |
1386 // | | Word 2 unwind opcodes | |
1387 // | +--------+--------+--------+-------+ |
1388 // | ... |
1389 // | +--------+--------+--------+-------+ |
1390 // | | Word N unwind opcodes | |
1391 // | +--------+--------+--------+-------+ |
1392 // | | LSDA | | <-- lsda
1393 // | | ... | |
1394 // | +--------+--------+--------+-------+ |
1395 // +--------------------------------------+
1396
1397 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1398 uint32_t FirstDataWord = *UnwindData;
1399 size_t N = ((FirstDataWord >> 24) & 0xff);
1400 size_t NDataWords = N + 1;
1401 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1402 }
1403
1404 _info.start_ip = thisPC;
1405 _info.end_ip = nextPC;
1406 _info.handler = personalityRoutine;
1407 _info.unwind_info = exceptionTableAddr;
1408 _info.lsda = lsda;
1409 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1410 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1411
1412 return true;
1413}
1414#endif
1415
Ranjeet Singh421231a2017-03-31 15:28:06 +00001416#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001417template <typename A, typename R>
1418bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1419 const UnwindInfoSections &sects,
1420 uint32_t fdeSectionOffsetHint) {
1421 typename CFI_Parser<A>::FDE_Info fdeInfo;
1422 typename CFI_Parser<A>::CIE_Info cieInfo;
1423 bool foundFDE = false;
1424 bool foundInCache = false;
1425 // If compact encoding table gave offset into dwarf section, go directly there
1426 if (fdeSectionOffsetHint != 0) {
1427 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1428 (uint32_t)sects.dwarf_section_length,
1429 sects.dwarf_section + fdeSectionOffsetHint,
1430 &fdeInfo, &cieInfo);
1431 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001432#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001433 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1434 foundFDE = EHHeaderParser<A>::findFDE(
1435 _addressSpace, pc, sects.dwarf_index_section,
1436 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1437 }
1438#endif
1439 if (!foundFDE) {
1440 // otherwise, search cache of previously found FDEs.
1441 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1442 if (cachedFDE != 0) {
1443 foundFDE =
1444 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1445 (uint32_t)sects.dwarf_section_length,
1446 cachedFDE, &fdeInfo, &cieInfo);
1447 foundInCache = foundFDE;
1448 }
1449 }
1450 if (!foundFDE) {
1451 // Still not found, do full scan of __eh_frame section.
1452 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1453 (uint32_t)sects.dwarf_section_length, 0,
1454 &fdeInfo, &cieInfo);
1455 }
1456 if (foundFDE) {
1457 typename CFI_Parser<A>::PrologInfo prolog;
1458 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001459 R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001460 // Save off parsed FDE info
1461 _info.start_ip = fdeInfo.pcStart;
1462 _info.end_ip = fdeInfo.pcEnd;
1463 _info.lsda = fdeInfo.lsda;
1464 _info.handler = cieInfo.personality;
1465 _info.gp = prolog.spExtraArgSize;
1466 _info.flags = 0;
1467 _info.format = dwarfEncoding();
1468 _info.unwind_info = fdeInfo.fdeStart;
1469 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1470 _info.extra = (unw_word_t) sects.dso_base;
1471
1472 // Add to cache (to make next lookup faster) if we had no hint
1473 // and there was no index.
1474 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001475 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001476 if (sects.dwarf_index_section == 0)
1477 #endif
1478 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1479 fdeInfo.fdeStart);
1480 }
1481 return true;
1482 }
1483 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001484 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001485 return false;
1486}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001487#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001488
1489
Ranjeet Singh421231a2017-03-31 15:28:06 +00001490#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001491template <typename A, typename R>
1492bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1493 const UnwindInfoSections &sects) {
1494 const bool log = false;
1495 if (log)
1496 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1497 (uint64_t)pc, (uint64_t)sects.dso_base);
1498
1499 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1500 sects.compact_unwind_section);
1501 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1502 return false;
1503
1504 // do a binary search of top level index to find page with unwind info
1505 pint_t targetFunctionOffset = pc - sects.dso_base;
1506 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1507 sects.compact_unwind_section
1508 + sectionHeader.indexSectionOffset());
1509 uint32_t low = 0;
1510 uint32_t high = sectionHeader.indexCount();
1511 uint32_t last = high - 1;
1512 while (low < high) {
1513 uint32_t mid = (low + high) / 2;
1514 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1515 //mid, low, high, topIndex.functionOffset(mid));
1516 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1517 if ((mid == last) ||
1518 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1519 low = mid;
1520 break;
1521 } else {
1522 low = mid + 1;
1523 }
1524 } else {
1525 high = mid;
1526 }
1527 }
1528 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1529 const uint32_t firstLevelNextPageFunctionOffset =
1530 topIndex.functionOffset(low + 1);
1531 const pint_t secondLevelAddr =
1532 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1533 const pint_t lsdaArrayStartAddr =
1534 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1535 const pint_t lsdaArrayEndAddr =
1536 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1537 if (log)
1538 fprintf(stderr, "\tfirst level search for result index=%d "
1539 "to secondLevelAddr=0x%llX\n",
1540 low, (uint64_t) secondLevelAddr);
1541 // do a binary search of second level page index
1542 uint32_t encoding = 0;
1543 pint_t funcStart = 0;
1544 pint_t funcEnd = 0;
1545 pint_t lsda = 0;
1546 pint_t personality = 0;
1547 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1548 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1549 // regular page
1550 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1551 secondLevelAddr);
1552 UnwindSectionRegularArray<A> pageIndex(
1553 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1554 // binary search looks for entry with e where index[e].offset <= pc <
1555 // index[e+1].offset
1556 if (log)
1557 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1558 "regular page starting at secondLevelAddr=0x%llX\n",
1559 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1560 low = 0;
1561 high = pageHeader.entryCount();
1562 while (low < high) {
1563 uint32_t mid = (low + high) / 2;
1564 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1565 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1566 // at end of table
1567 low = mid;
1568 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1569 break;
1570 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1571 // next is too big, so we found it
1572 low = mid;
1573 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1574 break;
1575 } else {
1576 low = mid + 1;
1577 }
1578 } else {
1579 high = mid;
1580 }
1581 }
1582 encoding = pageIndex.encoding(low);
1583 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1584 if (pc < funcStart) {
1585 if (log)
1586 fprintf(
1587 stderr,
1588 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1589 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1590 return false;
1591 }
1592 if (pc > funcEnd) {
1593 if (log)
1594 fprintf(
1595 stderr,
1596 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1597 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1598 return false;
1599 }
1600 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1601 // compressed page
1602 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1603 secondLevelAddr);
1604 UnwindSectionCompressedArray<A> pageIndex(
1605 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1606 const uint32_t targetFunctionPageOffset =
1607 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1608 // binary search looks for entry with e where index[e].offset <= pc <
1609 // index[e+1].offset
1610 if (log)
1611 fprintf(stderr, "\tbinary search of compressed page starting at "
1612 "secondLevelAddr=0x%llX\n",
1613 (uint64_t) secondLevelAddr);
1614 low = 0;
1615 last = pageHeader.entryCount() - 1;
1616 high = pageHeader.entryCount();
1617 while (low < high) {
1618 uint32_t mid = (low + high) / 2;
1619 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1620 if ((mid == last) ||
1621 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1622 low = mid;
1623 break;
1624 } else {
1625 low = mid + 1;
1626 }
1627 } else {
1628 high = mid;
1629 }
1630 }
1631 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1632 + sects.dso_base;
1633 if (low < last)
1634 funcEnd =
1635 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1636 + sects.dso_base;
1637 else
1638 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1639 if (pc < funcStart) {
1640 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001641 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001642 (uint64_t) pc, (uint64_t) funcStart);
1643 return false;
1644 }
1645 if (pc > funcEnd) {
1646 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001647 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001648 (uint64_t) pc, (uint64_t) funcEnd);
1649 return false;
1650 }
1651 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1652 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1653 // encoding is in common table in section header
1654 encoding = _addressSpace.get32(
1655 sects.compact_unwind_section +
1656 sectionHeader.commonEncodingsArraySectionOffset() +
1657 encodingIndex * sizeof(uint32_t));
1658 } else {
1659 // encoding is in page specific table
1660 uint16_t pageEncodingIndex =
1661 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1662 encoding = _addressSpace.get32(secondLevelAddr +
1663 pageHeader.encodingsPageOffset() +
1664 pageEncodingIndex * sizeof(uint32_t));
1665 }
1666 } else {
1667 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001668 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001669 (uint64_t) sects.compact_unwind_section);
1670 return false;
1671 }
1672
1673 // look up LSDA, if encoding says function has one
1674 if (encoding & UNWIND_HAS_LSDA) {
1675 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1676 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1677 low = 0;
1678 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1679 sizeof(unwind_info_section_header_lsda_index_entry);
1680 // binary search looks for entry with exact match for functionOffset
1681 if (log)
1682 fprintf(stderr,
1683 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1684 funcStartOffset);
1685 while (low < high) {
1686 uint32_t mid = (low + high) / 2;
1687 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1688 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1689 break;
1690 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1691 low = mid + 1;
1692 } else {
1693 high = mid;
1694 }
1695 }
1696 if (lsda == 0) {
1697 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001698 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001699 encoding, (uint64_t) pc);
1700 return false;
1701 }
1702 }
1703
1704 // extact personality routine, if encoding says function has one
1705 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1706 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1707 if (personalityIndex != 0) {
1708 --personalityIndex; // change 1-based to zero-based index
1709 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1710 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001711 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001712 encoding, personalityIndex,
1713 sectionHeader.personalityArrayCount());
1714 return false;
1715 }
1716 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1717 sects.compact_unwind_section +
1718 sectionHeader.personalityArraySectionOffset() +
1719 personalityIndex * sizeof(uint32_t));
1720 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1721 personality = _addressSpace.getP(personalityPointer);
1722 if (log)
1723 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1724 "personalityDelta=0x%08X, personality=0x%08llX\n",
1725 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1726 }
1727
1728 if (log)
1729 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1730 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1731 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1732 _info.start_ip = funcStart;
1733 _info.end_ip = funcEnd;
1734 _info.lsda = lsda;
1735 _info.handler = personality;
1736 _info.gp = 0;
1737 _info.flags = 0;
1738 _info.format = encoding;
1739 _info.unwind_info = 0;
1740 _info.unwind_info_size = 0;
1741 _info.extra = sects.dso_base;
1742 return true;
1743}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001744#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001745
1746
Charles Davisfa2e6202018-08-30 21:29:00 +00001747#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1748template <typename A, typename R>
1749bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1750 pint_t base;
1751 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1752 if (!unwindEntry) {
1753 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1754 return false;
1755 }
1756 _info.gp = 0;
1757 _info.flags = 0;
1758 _info.format = 0;
1759 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1760 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1761 _info.extra = base;
1762 _info.start_ip = base + unwindEntry->BeginAddress;
1763#ifdef _LIBUNWIND_TARGET_X86_64
1764 _info.end_ip = base + unwindEntry->EndAddress;
1765 // Only fill in the handler and LSDA if they're stale.
1766 if (pc != getLastPC()) {
1767 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1768 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1769 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1770 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1771 // these structures.)
1772 // N.B. UNWIND_INFO structs are DWORD-aligned.
1773 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1774 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1775 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1776 if (*handler) {
1777 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1778 } else
1779 _info.handler = 0;
1780 } else {
1781 _info.lsda = 0;
1782 _info.handler = 0;
1783 }
1784 }
1785#elif defined(_LIBUNWIND_TARGET_ARM)
1786 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1787 _info.lsda = 0; // FIXME
1788 _info.handler = 0; // FIXME
1789#endif
1790 setLastPC(pc);
1791 return true;
1792}
1793#endif
1794
1795
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001796template <typename A, typename R>
1797void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1798 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001799#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001800 // Remove the thumb bit so the IP represents the actual instruction address.
1801 // This matches the behaviour of _Unwind_GetIP on arm.
1802 pc &= (pint_t)~0x1;
1803#endif
1804
1805 // If the last line of a function is a "throw" the compiler sometimes
1806 // emits no instructions after the call to __cxa_throw. This means
1807 // the return address is actually the start of the next function.
1808 // To disambiguate this, back up the pc when we know it is a return
1809 // address.
1810 if (isReturnAddress)
1811 --pc;
1812
1813 // Ask address space object to find unwind sections for this pc.
1814 UnwindInfoSections sects;
1815 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001816#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001817 // If there is a compact unwind encoding table, look there first.
1818 if (sects.compact_unwind_section != 0) {
1819 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001820 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001821 // Found info in table, done unless encoding says to use dwarf.
1822 uint32_t dwarfOffset;
1823 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1824 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1825 // found info in dwarf, done
1826 return;
1827 }
1828 }
1829 #endif
1830 // If unwind table has entry, but entry says there is no unwind info,
1831 // record that we have no unwind info.
1832 if (_info.format == 0)
1833 _unwindInfoMissing = true;
1834 return;
1835 }
1836 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001837#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001838
Charles Davisfa2e6202018-08-30 21:29:00 +00001839#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1840 // If there is SEH unwind info, look there next.
1841 if (this->getInfoFromSEH(pc))
1842 return;
1843#endif
1844
Ranjeet Singh421231a2017-03-31 15:28:06 +00001845#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001846 // If there is dwarf unwind info, look there next.
1847 if (sects.dwarf_section != 0) {
1848 if (this->getInfoFromDwarfSection(pc, sects)) {
1849 // found info in dwarf, done
1850 return;
1851 }
1852 }
1853#endif
1854
Ranjeet Singh421231a2017-03-31 15:28:06 +00001855#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001856 // If there is ARM EHABI unwind info, look there next.
1857 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1858 return;
1859#endif
1860 }
1861
Ranjeet Singh421231a2017-03-31 15:28:06 +00001862#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001863 // There is no static unwind info for this pc. Look to see if an FDE was
1864 // dynamically registered for it.
1865 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1866 if (cachedFDE != 0) {
1867 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1868 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1869 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1870 cachedFDE, &fdeInfo, &cieInfo);
1871 if (msg == NULL) {
1872 typename CFI_Parser<A>::PrologInfo prolog;
1873 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001874 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001875 // save off parsed FDE info
1876 _info.start_ip = fdeInfo.pcStart;
1877 _info.end_ip = fdeInfo.pcEnd;
1878 _info.lsda = fdeInfo.lsda;
1879 _info.handler = cieInfo.personality;
1880 _info.gp = prolog.spExtraArgSize;
1881 // Some frameless functions need SP
1882 // altered when resuming in function.
1883 _info.flags = 0;
1884 _info.format = dwarfEncoding();
1885 _info.unwind_info = fdeInfo.fdeStart;
1886 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1887 _info.extra = 0;
1888 return;
1889 }
1890 }
1891 }
1892
1893 // Lastly, ask AddressSpace object about platform specific ways to locate
1894 // other FDEs.
1895 pint_t fde;
1896 if (_addressSpace.findOtherFDE(pc, fde)) {
1897 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1898 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1899 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1900 // Double check this FDE is for a function that includes the pc.
1901 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1902 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001903 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1904 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001905 // save off parsed FDE info
1906 _info.start_ip = fdeInfo.pcStart;
1907 _info.end_ip = fdeInfo.pcEnd;
1908 _info.lsda = fdeInfo.lsda;
1909 _info.handler = cieInfo.personality;
1910 _info.gp = prolog.spExtraArgSize;
1911 _info.flags = 0;
1912 _info.format = dwarfEncoding();
1913 _info.unwind_info = fdeInfo.fdeStart;
1914 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1915 _info.extra = 0;
1916 return;
1917 }
1918 }
1919 }
1920 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001921#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001922
1923 // no unwind info, flag that we can't reliably unwind
1924 _unwindInfoMissing = true;
1925}
1926
1927template <typename A, typename R>
1928int UnwindCursor<A, R>::step() {
1929 // Bottom of stack is defined is when unwind info cannot be found.
1930 if (_unwindInfoMissing)
1931 return UNW_STEP_END;
1932
1933 // Use unwinding info to modify register set as if function returned.
1934 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001935#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001936 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001937#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1938 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001939#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001940 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001941#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001942 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001943#else
1944 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001945 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001946 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001947 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001948#endif
1949
1950 // update info based on new PC
1951 if (result == UNW_STEP_SUCCESS) {
1952 this->setInfoBasedOnIPRegister(true);
1953 if (_unwindInfoMissing)
1954 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001955 }
1956
1957 return result;
1958}
1959
1960template <typename A, typename R>
1961void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1962 *info = _info;
1963}
1964
1965template <typename A, typename R>
1966bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1967 unw_word_t *offset) {
1968 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1969 buf, bufLen, offset);
1970}
1971
1972} // namespace libunwind
1973
1974#endif // __UNWINDCURSOR_HPP__