blob: a830d5840af81fb862e06a939bc6bc2690752d84 [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
Saleem Abdulrasool17552662015-04-24 19:39:17 +000014#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000017#include <unwind.h>
18
Charles Davisfa2e6202018-08-30 21:29:00 +000019#ifdef _WIN32
20 #include <windows.h>
21 #include <ntverp.h>
22#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000023#ifdef __APPLE__
24 #include <mach-o/dyld.h>
25#endif
26
Charles Davisfa2e6202018-08-30 21:29:00 +000027#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
28// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
29// earlier) SDKs.
30// MinGW-w64 has always provided this struct.
31 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
32 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
33struct _DISPATCHER_CONTEXT {
34 ULONG64 ControlPc;
35 ULONG64 ImageBase;
36 PRUNTIME_FUNCTION FunctionEntry;
37 ULONG64 EstablisherFrame;
38 ULONG64 TargetIp;
39 PCONTEXT ContextRecord;
40 PEXCEPTION_ROUTINE LanguageHandler;
41 PVOID HandlerData;
42 PUNWIND_HISTORY_TABLE HistoryTable;
43 ULONG ScopeIndex;
44 ULONG Fill0;
45};
46 #endif
47
48struct UNWIND_INFO {
49 uint8_t Version : 3;
50 uint8_t Flags : 5;
51 uint8_t SizeOfProlog;
52 uint8_t CountOfCodes;
53 uint8_t FrameRegister : 4;
54 uint8_t FrameOffset : 4;
55 uint16_t UnwindCodes[2];
56};
57
58extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
59 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
60 struct _Unwind_Context *);
61
62#endif
63
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064#include "config.h"
65
66#include "AddressSpace.hpp"
67#include "CompactUnwinder.hpp"
68#include "config.h"
69#include "DwarfInstructions.hpp"
70#include "EHHeaderParser.hpp"
71#include "libunwind.h"
72#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000073#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000074#include "Unwind-EHABI.h"
75
76namespace libunwind {
77
Ranjeet Singh421231a2017-03-31 15:28:06 +000078#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079/// Cache of recently found FDEs.
80template <typename A>
81class _LIBUNWIND_HIDDEN DwarfFDECache {
82 typedef typename A::pint_t pint_t;
83public:
84 static pint_t findFDE(pint_t mh, pint_t pc);
85 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
86 static void removeAllIn(pint_t mh);
87 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
88 unw_word_t ip_end,
89 unw_word_t fde, unw_word_t mh));
90
91private:
92
93 struct entry {
94 pint_t mh;
95 pint_t ip_start;
96 pint_t ip_end;
97 pint_t fde;
98 };
99
100 // These fields are all static to avoid needing an initializer.
101 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000102 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000103#ifdef __APPLE__
104 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
105 static bool _registeredForDyldUnloads;
106#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000107 static entry *_buffer;
108 static entry *_bufferUsed;
109 static entry *_bufferEnd;
110 static entry _initialBuffer[64];
111};
112
113template <typename A>
114typename DwarfFDECache<A>::entry *
115DwarfFDECache<A>::_buffer = _initialBuffer;
116
117template <typename A>
118typename DwarfFDECache<A>::entry *
119DwarfFDECache<A>::_bufferUsed = _initialBuffer;
120
121template <typename A>
122typename DwarfFDECache<A>::entry *
123DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
124
125template <typename A>
126typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
127
128template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000129RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000130
131#ifdef __APPLE__
132template <typename A>
133bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
134#endif
135
136template <typename A>
137typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
138 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000139 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000140 for (entry *p = _buffer; p < _bufferUsed; ++p) {
141 if ((mh == p->mh) || (mh == 0)) {
142 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
143 result = p->fde;
144 break;
145 }
146 }
147 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000148 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 return result;
150}
151
152template <typename A>
153void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
154 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000155#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000156 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000157 if (_bufferUsed >= _bufferEnd) {
158 size_t oldSize = (size_t)(_bufferEnd - _buffer);
159 size_t newSize = oldSize * 4;
160 // Can't use operator new (we are below it).
161 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
162 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
163 if (_buffer != _initialBuffer)
164 free(_buffer);
165 _buffer = newBuffer;
166 _bufferUsed = &newBuffer[oldSize];
167 _bufferEnd = &newBuffer[newSize];
168 }
169 _bufferUsed->mh = mh;
170 _bufferUsed->ip_start = ip_start;
171 _bufferUsed->ip_end = ip_end;
172 _bufferUsed->fde = fde;
173 ++_bufferUsed;
174#ifdef __APPLE__
175 if (!_registeredForDyldUnloads) {
176 _dyld_register_func_for_remove_image(&dyldUnloadHook);
177 _registeredForDyldUnloads = true;
178 }
179#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000180 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000181#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000182}
183
184template <typename A>
185void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000186 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000187 entry *d = _buffer;
188 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
189 if (s->mh != mh) {
190 if (d != s)
191 *d = *s;
192 ++d;
193 }
194 }
195 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000196 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000197}
198
199#ifdef __APPLE__
200template <typename A>
201void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
202 removeAllIn((pint_t) mh);
203}
204#endif
205
206template <typename A>
207void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
208 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 +0000209 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000210 for (entry *p = _buffer; p < _bufferUsed; ++p) {
211 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
212 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000213 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000214}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000215#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216
217
218#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
219
Ranjeet Singh421231a2017-03-31 15:28:06 +0000220#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000221template <typename A> class UnwindSectionHeader {
222public:
223 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
224 : _addressSpace(addressSpace), _addr(addr) {}
225
226 uint32_t version() const {
227 return _addressSpace.get32(_addr +
228 offsetof(unwind_info_section_header, version));
229 }
230 uint32_t commonEncodingsArraySectionOffset() const {
231 return _addressSpace.get32(_addr +
232 offsetof(unwind_info_section_header,
233 commonEncodingsArraySectionOffset));
234 }
235 uint32_t commonEncodingsArrayCount() const {
236 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
237 commonEncodingsArrayCount));
238 }
239 uint32_t personalityArraySectionOffset() const {
240 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
241 personalityArraySectionOffset));
242 }
243 uint32_t personalityArrayCount() const {
244 return _addressSpace.get32(
245 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
246 }
247 uint32_t indexSectionOffset() const {
248 return _addressSpace.get32(
249 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
250 }
251 uint32_t indexCount() const {
252 return _addressSpace.get32(
253 _addr + offsetof(unwind_info_section_header, indexCount));
254 }
255
256private:
257 A &_addressSpace;
258 typename A::pint_t _addr;
259};
260
261template <typename A> class UnwindSectionIndexArray {
262public:
263 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
264 : _addressSpace(addressSpace), _addr(addr) {}
265
266 uint32_t functionOffset(uint32_t index) const {
267 return _addressSpace.get32(
268 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
269 functionOffset));
270 }
271 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
272 return _addressSpace.get32(
273 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
274 secondLevelPagesSectionOffset));
275 }
276 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
277 return _addressSpace.get32(
278 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
279 lsdaIndexArraySectionOffset));
280 }
281
282private:
283 A &_addressSpace;
284 typename A::pint_t _addr;
285};
286
287template <typename A> class UnwindSectionRegularPageHeader {
288public:
289 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
290 : _addressSpace(addressSpace), _addr(addr) {}
291
292 uint32_t kind() const {
293 return _addressSpace.get32(
294 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
295 }
296 uint16_t entryPageOffset() const {
297 return _addressSpace.get16(
298 _addr + offsetof(unwind_info_regular_second_level_page_header,
299 entryPageOffset));
300 }
301 uint16_t entryCount() const {
302 return _addressSpace.get16(
303 _addr +
304 offsetof(unwind_info_regular_second_level_page_header, entryCount));
305 }
306
307private:
308 A &_addressSpace;
309 typename A::pint_t _addr;
310};
311
312template <typename A> class UnwindSectionRegularArray {
313public:
314 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
315 : _addressSpace(addressSpace), _addr(addr) {}
316
317 uint32_t functionOffset(uint32_t index) const {
318 return _addressSpace.get32(
319 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
320 functionOffset));
321 }
322 uint32_t encoding(uint32_t index) const {
323 return _addressSpace.get32(
324 _addr +
325 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
326 }
327
328private:
329 A &_addressSpace;
330 typename A::pint_t _addr;
331};
332
333template <typename A> class UnwindSectionCompressedPageHeader {
334public:
335 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
336 : _addressSpace(addressSpace), _addr(addr) {}
337
338 uint32_t kind() const {
339 return _addressSpace.get32(
340 _addr +
341 offsetof(unwind_info_compressed_second_level_page_header, kind));
342 }
343 uint16_t entryPageOffset() const {
344 return _addressSpace.get16(
345 _addr + offsetof(unwind_info_compressed_second_level_page_header,
346 entryPageOffset));
347 }
348 uint16_t entryCount() const {
349 return _addressSpace.get16(
350 _addr +
351 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
352 }
353 uint16_t encodingsPageOffset() const {
354 return _addressSpace.get16(
355 _addr + offsetof(unwind_info_compressed_second_level_page_header,
356 encodingsPageOffset));
357 }
358 uint16_t encodingsCount() const {
359 return _addressSpace.get16(
360 _addr + offsetof(unwind_info_compressed_second_level_page_header,
361 encodingsCount));
362 }
363
364private:
365 A &_addressSpace;
366 typename A::pint_t _addr;
367};
368
369template <typename A> class UnwindSectionCompressedArray {
370public:
371 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
372 : _addressSpace(addressSpace), _addr(addr) {}
373
374 uint32_t functionOffset(uint32_t index) const {
375 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
376 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
377 }
378 uint16_t encodingIndex(uint32_t index) const {
379 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
380 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
381 }
382
383private:
384 A &_addressSpace;
385 typename A::pint_t _addr;
386};
387
388template <typename A> class UnwindSectionLsdaArray {
389public:
390 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
391 : _addressSpace(addressSpace), _addr(addr) {}
392
393 uint32_t functionOffset(uint32_t index) const {
394 return _addressSpace.get32(
395 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
396 index, functionOffset));
397 }
398 uint32_t lsdaOffset(uint32_t index) const {
399 return _addressSpace.get32(
400 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
401 index, lsdaOffset));
402 }
403
404private:
405 A &_addressSpace;
406 typename A::pint_t _addr;
407};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000408#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000409
410class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
411public:
412 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
413 // This avoids an unnecessary dependency to libc++abi.
414 void operator delete(void *, size_t) {}
415
416 virtual ~AbstractUnwindCursor() {}
417 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
418 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
419 virtual void setReg(int, unw_word_t) {
420 _LIBUNWIND_ABORT("setReg not implemented");
421 }
422 virtual bool validFloatReg(int) {
423 _LIBUNWIND_ABORT("validFloatReg not implemented");
424 }
425 virtual unw_fpreg_t getFloatReg(int) {
426 _LIBUNWIND_ABORT("getFloatReg not implemented");
427 }
428 virtual void setFloatReg(int, unw_fpreg_t) {
429 _LIBUNWIND_ABORT("setFloatReg not implemented");
430 }
431 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
432 virtual void getInfo(unw_proc_info_t *) {
433 _LIBUNWIND_ABORT("getInfo not implemented");
434 }
435 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
436 virtual bool isSignalFrame() {
437 _LIBUNWIND_ABORT("isSignalFrame not implemented");
438 }
439 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
440 _LIBUNWIND_ABORT("getFunctionName not implemented");
441 }
442 virtual void setInfoBasedOnIPRegister(bool = false) {
443 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
444 }
445 virtual const char *getRegisterName(int) {
446 _LIBUNWIND_ABORT("getRegisterName not implemented");
447 }
448#ifdef __arm__
449 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
450#endif
451};
452
Charles Davisfa2e6202018-08-30 21:29:00 +0000453#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
454
455/// \c UnwindCursor contains all state (including all register values) during
456/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
457template <typename A, typename R>
458class UnwindCursor : public AbstractUnwindCursor {
459 typedef typename A::pint_t pint_t;
460public:
461 UnwindCursor(unw_context_t *context, A &as);
462 UnwindCursor(CONTEXT *context, A &as);
463 UnwindCursor(A &as, void *threadArg);
464 virtual ~UnwindCursor() {}
465 virtual bool validReg(int);
466 virtual unw_word_t getReg(int);
467 virtual void setReg(int, unw_word_t);
468 virtual bool validFloatReg(int);
469 virtual unw_fpreg_t getFloatReg(int);
470 virtual void setFloatReg(int, unw_fpreg_t);
471 virtual int step();
472 virtual void getInfo(unw_proc_info_t *);
473 virtual void jumpto();
474 virtual bool isSignalFrame();
475 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
476 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
477 virtual const char *getRegisterName(int num);
478#ifdef __arm__
479 virtual void saveVFPAsX();
480#endif
481
482 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
483 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
484
485private:
486
487 pint_t getLastPC() const { return _dispContext.ControlPc; }
488 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
489 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
490 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
491 &_dispContext.ImageBase,
492 _dispContext.HistoryTable);
493 *base = _dispContext.ImageBase;
494 return _dispContext.FunctionEntry;
495 }
496 bool getInfoFromSEH(pint_t pc);
497 int stepWithSEHData() {
498 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
499 _dispContext.ImageBase,
500 _dispContext.ControlPc,
501 _dispContext.FunctionEntry,
502 _dispContext.ContextRecord,
503 &_dispContext.HandlerData,
504 &_dispContext.EstablisherFrame,
505 NULL);
506 // Update some fields of the unwind info now, since we have them.
507 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
508 if (_dispContext.LanguageHandler) {
509 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
510 } else
511 _info.handler = 0;
512 return UNW_STEP_SUCCESS;
513 }
514
515 A &_addressSpace;
516 unw_proc_info_t _info;
517 DISPATCHER_CONTEXT _dispContext;
518 CONTEXT _msContext;
519 UNWIND_HISTORY_TABLE _histTable;
520 bool _unwindInfoMissing;
521};
522
523
524template <typename A, typename R>
525UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
526 : _addressSpace(as), _unwindInfoMissing(false) {
527 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
528 "UnwindCursor<> does not fit in unw_cursor_t");
529 memset(&_info, 0, sizeof(_info));
530 memset(&_histTable, 0, sizeof(_histTable));
531 _dispContext.ContextRecord = &_msContext;
532 _dispContext.HistoryTable = &_histTable;
533 // Initialize MS context from ours.
534 R r(context);
535 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
536#if defined(_LIBUNWIND_TARGET_X86_64)
537 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
538 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
539 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
540 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
541 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
542 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
543 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
544 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
545 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
546 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
547 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
548 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
549 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
550 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
551 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
552 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
553 _msContext.Rip = r.getRegister(UNW_REG_IP);
554 union {
555 v128 v;
556 M128A m;
557 } t;
558 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
559 _msContext.Xmm0 = t.m;
560 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
561 _msContext.Xmm1 = t.m;
562 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
563 _msContext.Xmm2 = t.m;
564 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
565 _msContext.Xmm3 = t.m;
566 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
567 _msContext.Xmm4 = t.m;
568 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
569 _msContext.Xmm5 = t.m;
570 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
571 _msContext.Xmm6 = t.m;
572 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
573 _msContext.Xmm7 = t.m;
574 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
575 _msContext.Xmm8 = t.m;
576 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
577 _msContext.Xmm9 = t.m;
578 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
579 _msContext.Xmm10 = t.m;
580 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
581 _msContext.Xmm11 = t.m;
582 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
583 _msContext.Xmm12 = t.m;
584 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
585 _msContext.Xmm13 = t.m;
586 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
587 _msContext.Xmm14 = t.m;
588 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
589 _msContext.Xmm15 = t.m;
590#elif defined(_LIBUNWIND_TARGET_ARM)
591 _msContext.R0 = r.getRegister(UNW_ARM_R0);
592 _msContext.R1 = r.getRegister(UNW_ARM_R1);
593 _msContext.R2 = r.getRegister(UNW_ARM_R2);
594 _msContext.R3 = r.getRegister(UNW_ARM_R3);
595 _msContext.R4 = r.getRegister(UNW_ARM_R4);
596 _msContext.R5 = r.getRegister(UNW_ARM_R5);
597 _msContext.R6 = r.getRegister(UNW_ARM_R6);
598 _msContext.R7 = r.getRegister(UNW_ARM_R7);
599 _msContext.R8 = r.getRegister(UNW_ARM_R8);
600 _msContext.R9 = r.getRegister(UNW_ARM_R9);
601 _msContext.R10 = r.getRegister(UNW_ARM_R10);
602 _msContext.R11 = r.getRegister(UNW_ARM_R11);
603 _msContext.R12 = r.getRegister(UNW_ARM_R12);
604 _msContext.Sp = r.getRegister(UNW_ARM_SP);
605 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000606 _msContext.Pc = r.getRegister(UNW_ARM_IP);
607 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000608 union {
609 uint64_t w;
610 double d;
611 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000612 d.d = r.getFloatRegister(i);
613 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000614 }
Martin Storsjoce150112018-12-18 20:05:59 +0000615#elif defined(_LIBUNWIND_TARGET_AARCH64)
616 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
617 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
618 _msContext.Sp = r.getRegister(UNW_REG_SP);
619 _msContext.Pc = r.getRegister(UNW_REG_IP);
620 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
621 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000622#endif
623}
624
625template <typename A, typename R>
626UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
627 : _addressSpace(as), _unwindInfoMissing(false) {
628 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
629 "UnwindCursor<> does not fit in unw_cursor_t");
630 memset(&_info, 0, sizeof(_info));
631 memset(&_histTable, 0, sizeof(_histTable));
632 _dispContext.ContextRecord = &_msContext;
633 _dispContext.HistoryTable = &_histTable;
634 _msContext = *context;
635}
636
637
638template <typename A, typename R>
639bool UnwindCursor<A, R>::validReg(int regNum) {
640 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
641#if defined(_LIBUNWIND_TARGET_X86_64)
642 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
643#elif defined(_LIBUNWIND_TARGET_ARM)
644 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000645#elif defined(_LIBUNWIND_TARGET_AARCH64)
646 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000647#endif
648 return false;
649}
650
651template <typename A, typename R>
652unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
653 switch (regNum) {
654#if defined(_LIBUNWIND_TARGET_X86_64)
655 case UNW_REG_IP: return _msContext.Rip;
656 case UNW_X86_64_RAX: return _msContext.Rax;
657 case UNW_X86_64_RDX: return _msContext.Rdx;
658 case UNW_X86_64_RCX: return _msContext.Rcx;
659 case UNW_X86_64_RBX: return _msContext.Rbx;
660 case UNW_REG_SP:
661 case UNW_X86_64_RSP: return _msContext.Rsp;
662 case UNW_X86_64_RBP: return _msContext.Rbp;
663 case UNW_X86_64_RSI: return _msContext.Rsi;
664 case UNW_X86_64_RDI: return _msContext.Rdi;
665 case UNW_X86_64_R8: return _msContext.R8;
666 case UNW_X86_64_R9: return _msContext.R9;
667 case UNW_X86_64_R10: return _msContext.R10;
668 case UNW_X86_64_R11: return _msContext.R11;
669 case UNW_X86_64_R12: return _msContext.R12;
670 case UNW_X86_64_R13: return _msContext.R13;
671 case UNW_X86_64_R14: return _msContext.R14;
672 case UNW_X86_64_R15: return _msContext.R15;
673#elif defined(_LIBUNWIND_TARGET_ARM)
674 case UNW_ARM_R0: return _msContext.R0;
675 case UNW_ARM_R1: return _msContext.R1;
676 case UNW_ARM_R2: return _msContext.R2;
677 case UNW_ARM_R3: return _msContext.R3;
678 case UNW_ARM_R4: return _msContext.R4;
679 case UNW_ARM_R5: return _msContext.R5;
680 case UNW_ARM_R6: return _msContext.R6;
681 case UNW_ARM_R7: return _msContext.R7;
682 case UNW_ARM_R8: return _msContext.R8;
683 case UNW_ARM_R9: return _msContext.R9;
684 case UNW_ARM_R10: return _msContext.R10;
685 case UNW_ARM_R11: return _msContext.R11;
686 case UNW_ARM_R12: return _msContext.R12;
687 case UNW_REG_SP:
688 case UNW_ARM_SP: return _msContext.Sp;
689 case UNW_ARM_LR: return _msContext.Lr;
690 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000691 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000692#elif defined(_LIBUNWIND_TARGET_AARCH64)
693 case UNW_REG_SP: return _msContext.Sp;
694 case UNW_REG_IP: return _msContext.Pc;
695 default: return _msContext.X[regNum - UNW_ARM64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000696#endif
697 }
698 _LIBUNWIND_ABORT("unsupported register");
699}
700
701template <typename A, typename R>
702void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
703 switch (regNum) {
704#if defined(_LIBUNWIND_TARGET_X86_64)
705 case UNW_REG_IP: _msContext.Rip = value; break;
706 case UNW_X86_64_RAX: _msContext.Rax = value; break;
707 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
708 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
709 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
710 case UNW_REG_SP:
711 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
712 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
713 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
714 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
715 case UNW_X86_64_R8: _msContext.R8 = value; break;
716 case UNW_X86_64_R9: _msContext.R9 = value; break;
717 case UNW_X86_64_R10: _msContext.R10 = value; break;
718 case UNW_X86_64_R11: _msContext.R11 = value; break;
719 case UNW_X86_64_R12: _msContext.R12 = value; break;
720 case UNW_X86_64_R13: _msContext.R13 = value; break;
721 case UNW_X86_64_R14: _msContext.R14 = value; break;
722 case UNW_X86_64_R15: _msContext.R15 = value; break;
723#elif defined(_LIBUNWIND_TARGET_ARM)
724 case UNW_ARM_R0: _msContext.R0 = value; break;
725 case UNW_ARM_R1: _msContext.R1 = value; break;
726 case UNW_ARM_R2: _msContext.R2 = value; break;
727 case UNW_ARM_R3: _msContext.R3 = value; break;
728 case UNW_ARM_R4: _msContext.R4 = value; break;
729 case UNW_ARM_R5: _msContext.R5 = value; break;
730 case UNW_ARM_R6: _msContext.R6 = value; break;
731 case UNW_ARM_R7: _msContext.R7 = value; break;
732 case UNW_ARM_R8: _msContext.R8 = value; break;
733 case UNW_ARM_R9: _msContext.R9 = value; break;
734 case UNW_ARM_R10: _msContext.R10 = value; break;
735 case UNW_ARM_R11: _msContext.R11 = value; break;
736 case UNW_ARM_R12: _msContext.R12 = value; break;
737 case UNW_REG_SP:
738 case UNW_ARM_SP: _msContext.Sp = value; break;
739 case UNW_ARM_LR: _msContext.Lr = value; break;
740 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000741 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000742#elif defined(_LIBUNWIND_TARGET_AARCH64)
743 case UNW_REG_SP: _msContext.Sp = value; break;
744 case UNW_REG_IP: _msContext.Pc = value; break;
745 case UNW_ARM64_X0:
746 case UNW_ARM64_X1:
747 case UNW_ARM64_X2:
748 case UNW_ARM64_X3:
749 case UNW_ARM64_X4:
750 case UNW_ARM64_X5:
751 case UNW_ARM64_X6:
752 case UNW_ARM64_X7:
753 case UNW_ARM64_X8:
754 case UNW_ARM64_X9:
755 case UNW_ARM64_X10:
756 case UNW_ARM64_X11:
757 case UNW_ARM64_X12:
758 case UNW_ARM64_X13:
759 case UNW_ARM64_X14:
760 case UNW_ARM64_X15:
761 case UNW_ARM64_X16:
762 case UNW_ARM64_X17:
763 case UNW_ARM64_X18:
764 case UNW_ARM64_X19:
765 case UNW_ARM64_X20:
766 case UNW_ARM64_X21:
767 case UNW_ARM64_X22:
768 case UNW_ARM64_X23:
769 case UNW_ARM64_X24:
770 case UNW_ARM64_X25:
771 case UNW_ARM64_X26:
772 case UNW_ARM64_X27:
773 case UNW_ARM64_X28:
774 case UNW_ARM64_FP:
775 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000776#endif
777 default:
778 _LIBUNWIND_ABORT("unsupported register");
779 }
780}
781
782template <typename A, typename R>
783bool UnwindCursor<A, R>::validFloatReg(int regNum) {
784#if defined(_LIBUNWIND_TARGET_ARM)
785 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
786 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000787#elif defined(_LIBUNWIND_TARGET_AARCH64)
788 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000789#else
790 (void)regNum;
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
Martin Storsjo059a1632019-01-22 22:12:23 +0000818 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000819 _LIBUNWIND_ABORT("float registers unimplemented");
820#endif
821}
822
823template <typename A, typename R>
824void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
825#if defined(_LIBUNWIND_TARGET_ARM)
826 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
827 union {
828 uint32_t w;
829 float f;
830 } d;
831 d.f = value;
832 _msContext.S[regNum - UNW_ARM_S0] = d.w;
833 }
834 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
835 union {
836 uint64_t w;
837 double d;
838 } d;
839 d.d = value;
840 _msContext.D[regNum - UNW_ARM_D0] = d.w;
841 }
842 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000843#elif defined(_LIBUNWIND_TARGET_AARCH64)
844 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000845#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000846 (void)regNum;
847 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000848 _LIBUNWIND_ABORT("float registers unimplemented");
849#endif
850}
851
852template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
853 RtlRestoreContext(&_msContext, nullptr);
854}
855
856#ifdef __arm__
857template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
858#endif
859
860template <typename A, typename R>
861const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000862 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000863}
864
865template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
866 return false;
867}
868
869#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
870
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000871/// UnwindCursor contains all state (including all register values) during
872/// an unwind. This is normally stack allocated inside a unw_cursor_t.
873template <typename A, typename R>
874class UnwindCursor : public AbstractUnwindCursor{
875 typedef typename A::pint_t pint_t;
876public:
877 UnwindCursor(unw_context_t *context, A &as);
878 UnwindCursor(A &as, void *threadArg);
879 virtual ~UnwindCursor() {}
880 virtual bool validReg(int);
881 virtual unw_word_t getReg(int);
882 virtual void setReg(int, unw_word_t);
883 virtual bool validFloatReg(int);
884 virtual unw_fpreg_t getFloatReg(int);
885 virtual void setFloatReg(int, unw_fpreg_t);
886 virtual int step();
887 virtual void getInfo(unw_proc_info_t *);
888 virtual void jumpto();
889 virtual bool isSignalFrame();
890 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
891 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
892 virtual const char *getRegisterName(int num);
893#ifdef __arm__
894 virtual void saveVFPAsX();
895#endif
896
897private:
898
Ranjeet Singh421231a2017-03-31 15:28:06 +0000899#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000900 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000901
902 int stepWithEHABI() {
903 size_t len = 0;
904 size_t off = 0;
905 // FIXME: Calling decode_eht_entry() here is violating the libunwind
906 // abstraction layer.
907 const uint32_t *ehtp =
908 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
909 &off, &len);
910 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
911 _URC_CONTINUE_UNWIND)
912 return UNW_STEP_END;
913 return UNW_STEP_SUCCESS;
914 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000915#endif
916
Ranjeet Singh421231a2017-03-31 15:28:06 +0000917#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000918 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
919 uint32_t fdeSectionOffsetHint=0);
920 int stepWithDwarfFDE() {
921 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
922 (pint_t)this->getReg(UNW_REG_IP),
923 (pint_t)_info.unwind_info,
924 _registers);
925 }
926#endif
927
Ranjeet Singh421231a2017-03-31 15:28:06 +0000928#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000929 bool getInfoFromCompactEncodingSection(pint_t pc,
930 const UnwindInfoSections &sects);
931 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000932 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000933 if ( compactSaysUseDwarf() )
934 return stepWithDwarfFDE();
935 #endif
936 R dummy;
937 return stepWithCompactEncoding(dummy);
938 }
939
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000940#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000941 int stepWithCompactEncoding(Registers_x86_64 &) {
942 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
943 _info.format, _info.start_ip, _addressSpace, _registers);
944 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000945#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000946
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000947#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000948 int stepWithCompactEncoding(Registers_x86 &) {
949 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
950 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
951 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000952#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000953
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000954#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000955 int stepWithCompactEncoding(Registers_ppc &) {
956 return UNW_EINVAL;
957 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000958#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000959
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000960#if defined(_LIBUNWIND_TARGET_PPC64)
961 int stepWithCompactEncoding(Registers_ppc64 &) {
962 return UNW_EINVAL;
963 }
964#endif
965
966
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000967#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000968 int stepWithCompactEncoding(Registers_arm64 &) {
969 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
970 _info.format, _info.start_ip, _addressSpace, _registers);
971 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000972#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000973
John Baldwin56441d42017-12-12 21:43:36 +0000974#if defined(_LIBUNWIND_TARGET_MIPS_O32)
975 int stepWithCompactEncoding(Registers_mips_o32 &) {
976 return UNW_EINVAL;
977 }
978#endif
979
John Baldwin541a4352018-01-09 17:07:18 +0000980#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
981 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000982 return UNW_EINVAL;
983 }
984#endif
985
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000986#if defined(_LIBUNWIND_TARGET_SPARC)
987 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
988#endif
989
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000990 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
991 R dummy;
992 return compactSaysUseDwarf(dummy, offset);
993 }
994
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000995#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000996 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
997 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
998 if (offset)
999 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1000 return true;
1001 }
1002 return false;
1003 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001004#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001005
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001006#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001007 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1008 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1009 if (offset)
1010 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1011 return true;
1012 }
1013 return false;
1014 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001015#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001016
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001017#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001018 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1019 return true;
1020 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001021#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001022
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001023#if defined(_LIBUNWIND_TARGET_PPC64)
1024 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1025 return true;
1026 }
1027#endif
1028
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001029#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001030 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1031 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1032 if (offset)
1033 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1034 return true;
1035 }
1036 return false;
1037 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001038#endif
John Baldwin56441d42017-12-12 21:43:36 +00001039
1040#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1041 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1042 return true;
1043 }
1044#endif
1045
John Baldwin541a4352018-01-09 17:07:18 +00001046#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1047 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001048 return true;
1049 }
1050#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001051
1052#if defined(_LIBUNWIND_TARGET_SPARC)
1053 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1054#endif
1055
Ranjeet Singh421231a2017-03-31 15:28:06 +00001056#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001057
Ranjeet Singh421231a2017-03-31 15:28:06 +00001058#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001059 compact_unwind_encoding_t dwarfEncoding() const {
1060 R dummy;
1061 return dwarfEncoding(dummy);
1062 }
1063
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001064#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001065 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1066 return UNWIND_X86_64_MODE_DWARF;
1067 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001068#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001069
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001070#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001071 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1072 return UNWIND_X86_MODE_DWARF;
1073 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001074#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001075
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001076#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001077 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1078 return 0;
1079 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001080#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001081
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001082#if defined(_LIBUNWIND_TARGET_PPC64)
1083 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1084 return 0;
1085 }
1086#endif
1087
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001088#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001089 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1090 return UNWIND_ARM64_MODE_DWARF;
1091 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001092#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001093
Martin Storsjoa72285f2017-11-02 08:16:16 +00001094#if defined(_LIBUNWIND_TARGET_ARM)
1095 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1096 return 0;
1097 }
1098#endif
1099
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001100#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001101 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1102 return 0;
1103 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001104#endif
John Baldwin56441d42017-12-12 21:43:36 +00001105
1106#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1107 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1108 return 0;
1109 }
1110#endif
1111
John Baldwin541a4352018-01-09 17:07:18 +00001112#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1113 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001114 return 0;
1115 }
1116#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001117
1118#if defined(_LIBUNWIND_TARGET_SPARC)
1119 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1120#endif
1121
Ranjeet Singh421231a2017-03-31 15:28:06 +00001122#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001123
Charles Davisfa2e6202018-08-30 21:29:00 +00001124#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1125 // For runtime environments using SEH unwind data without Windows runtime
1126 // support.
1127 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1128 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1129 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1130 /* FIXME: Implement */
1131 *base = 0;
1132 return nullptr;
1133 }
1134 bool getInfoFromSEH(pint_t pc);
1135 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1136#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1137
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001138
1139 A &_addressSpace;
1140 R _registers;
1141 unw_proc_info_t _info;
1142 bool _unwindInfoMissing;
1143 bool _isSignalFrame;
1144};
1145
1146
1147template <typename A, typename R>
1148UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1149 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1150 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001151 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001152 "UnwindCursor<> does not fit in unw_cursor_t");
1153 memset(&_info, 0, sizeof(_info));
1154}
1155
1156template <typename A, typename R>
1157UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1158 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1159 memset(&_info, 0, sizeof(_info));
1160 // FIXME
1161 // fill in _registers from thread arg
1162}
1163
1164
1165template <typename A, typename R>
1166bool UnwindCursor<A, R>::validReg(int regNum) {
1167 return _registers.validRegister(regNum);
1168}
1169
1170template <typename A, typename R>
1171unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1172 return _registers.getRegister(regNum);
1173}
1174
1175template <typename A, typename R>
1176void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1177 _registers.setRegister(regNum, (typename A::pint_t)value);
1178}
1179
1180template <typename A, typename R>
1181bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1182 return _registers.validFloatRegister(regNum);
1183}
1184
1185template <typename A, typename R>
1186unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1187 return _registers.getFloatRegister(regNum);
1188}
1189
1190template <typename A, typename R>
1191void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1192 _registers.setFloatRegister(regNum, value);
1193}
1194
1195template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1196 _registers.jumpto();
1197}
1198
1199#ifdef __arm__
1200template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1201 _registers.saveVFPAsX();
1202}
1203#endif
1204
1205template <typename A, typename R>
1206const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1207 return _registers.getRegisterName(regNum);
1208}
1209
1210template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1211 return _isSignalFrame;
1212}
1213
Charles Davisfa2e6202018-08-30 21:29:00 +00001214#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1215
Ranjeet Singh421231a2017-03-31 15:28:06 +00001216#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001217struct EHABIIndexEntry {
1218 uint32_t functionOffset;
1219 uint32_t data;
1220};
1221
1222template<typename A>
1223struct EHABISectionIterator {
1224 typedef EHABISectionIterator _Self;
1225
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001226 typedef typename A::pint_t value_type;
1227 typedef typename A::pint_t* pointer;
1228 typedef typename A::pint_t& reference;
1229 typedef size_t size_type;
1230 typedef size_t difference_type;
1231
1232 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1233 return _Self(addressSpace, sects, 0);
1234 }
1235 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001236 return _Self(addressSpace, sects,
1237 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001238 }
1239
1240 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1241 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1242
1243 _Self& operator++() { ++_i; return *this; }
1244 _Self& operator+=(size_t a) { _i += a; return *this; }
1245 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1246 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1247
1248 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1249 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1250
1251 size_t operator-(const _Self& other) { return _i - other._i; }
1252
1253 bool operator==(const _Self& other) const {
1254 assert(_addressSpace == other._addressSpace);
1255 assert(_sects == other._sects);
1256 return _i == other._i;
1257 }
1258
1259 typename A::pint_t operator*() const { return functionAddress(); }
1260
1261 typename A::pint_t functionAddress() const {
1262 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1263 EHABIIndexEntry, _i, functionOffset);
1264 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1265 }
1266
1267 typename A::pint_t dataAddress() {
1268 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1269 EHABIIndexEntry, _i, data);
1270 return indexAddr;
1271 }
1272
1273 private:
1274 size_t _i;
1275 A* _addressSpace;
1276 const UnwindInfoSections* _sects;
1277};
1278
Petr Hosekac0d9e02019-01-29 22:26:18 +00001279namespace {
1280
1281template <typename A>
1282EHABISectionIterator<A> EHABISectionUpperBound(
1283 EHABISectionIterator<A> first,
1284 EHABISectionIterator<A> last,
1285 typename A::pint_t value) {
1286 size_t len = last - first;
1287 while (len > 0) {
1288 size_t l2 = len / 2;
1289 EHABISectionIterator<A> m = first + l2;
1290 if (value < *m) {
1291 len = l2;
1292 } else {
1293 first = ++m;
1294 len -= l2 + 1;
1295 }
1296 }
1297 return first;
1298}
1299
1300}
1301
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001302template <typename A, typename R>
1303bool UnwindCursor<A, R>::getInfoFromEHABISection(
1304 pint_t pc,
1305 const UnwindInfoSections &sects) {
1306 EHABISectionIterator<A> begin =
1307 EHABISectionIterator<A>::begin(_addressSpace, sects);
1308 EHABISectionIterator<A> end =
1309 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001310 if (begin == end)
1311 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001312
Petr Hosekac0d9e02019-01-29 22:26:18 +00001313 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001314 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001315 return false;
1316 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1317
1318 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001319 // If an exception is thrown from a function, corresponding to the last entry
1320 // in the table, we don't really know the function extent and have to choose a
1321 // value for nextPC. Choosing max() will allow the range check during trace to
1322 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001323 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001324 pint_t indexDataAddr = itThisPC.dataAddress();
1325
1326 if (indexDataAddr == 0)
1327 return false;
1328
1329 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1330 if (indexData == UNW_EXIDX_CANTUNWIND)
1331 return false;
1332
1333 // If the high bit is set, the exception handling table entry is inline inside
1334 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1335 // the table points at an offset in the exception handling table (section 5 EHABI).
1336 pint_t exceptionTableAddr;
1337 uint32_t exceptionTableData;
1338 bool isSingleWordEHT;
1339 if (indexData & 0x80000000) {
1340 exceptionTableAddr = indexDataAddr;
1341 // TODO(ajwong): Should this data be 0?
1342 exceptionTableData = indexData;
1343 isSingleWordEHT = true;
1344 } else {
1345 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1346 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1347 isSingleWordEHT = false;
1348 }
1349
1350 // Now we know the 3 things:
1351 // exceptionTableAddr -- exception handler table entry.
1352 // exceptionTableData -- the data inside the first word of the eht entry.
1353 // isSingleWordEHT -- whether the entry is in the index.
1354 unw_word_t personalityRoutine = 0xbadf00d;
1355 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001356 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001357
1358 // If the high bit in the exception handling table entry is set, the entry is
1359 // in compact form (section 6.3 EHABI).
1360 if (exceptionTableData & 0x80000000) {
1361 // Grab the index of the personality routine from the compact form.
1362 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1363 uint32_t extraWords = 0;
1364 switch (choice) {
1365 case 0:
1366 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1367 extraWords = 0;
1368 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001369 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001370 break;
1371 case 1:
1372 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1373 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1374 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001375 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001376 break;
1377 case 2:
1378 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1379 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1380 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001381 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001382 break;
1383 default:
1384 _LIBUNWIND_ABORT("unknown personality routine");
1385 return false;
1386 }
1387
1388 if (isSingleWordEHT) {
1389 if (extraWords != 0) {
1390 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1391 "requires extra words");
1392 return false;
1393 }
1394 }
1395 } else {
1396 pint_t personalityAddr =
1397 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1398 personalityRoutine = personalityAddr;
1399
1400 // ARM EHABI # 6.2, # 9.2
1401 //
1402 // +---- ehtp
1403 // v
1404 // +--------------------------------------+
1405 // | +--------+--------+--------+-------+ |
1406 // | |0| prel31 to personalityRoutine | |
1407 // | +--------+--------+--------+-------+ |
1408 // | | N | unwind opcodes | | <-- UnwindData
1409 // | +--------+--------+--------+-------+ |
1410 // | | Word 2 unwind opcodes | |
1411 // | +--------+--------+--------+-------+ |
1412 // | ... |
1413 // | +--------+--------+--------+-------+ |
1414 // | | Word N unwind opcodes | |
1415 // | +--------+--------+--------+-------+ |
1416 // | | LSDA | | <-- lsda
1417 // | | ... | |
1418 // | +--------+--------+--------+-------+ |
1419 // +--------------------------------------+
1420
1421 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1422 uint32_t FirstDataWord = *UnwindData;
1423 size_t N = ((FirstDataWord >> 24) & 0xff);
1424 size_t NDataWords = N + 1;
1425 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1426 }
1427
1428 _info.start_ip = thisPC;
1429 _info.end_ip = nextPC;
1430 _info.handler = personalityRoutine;
1431 _info.unwind_info = exceptionTableAddr;
1432 _info.lsda = lsda;
1433 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1434 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1435
1436 return true;
1437}
1438#endif
1439
Ranjeet Singh421231a2017-03-31 15:28:06 +00001440#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001441template <typename A, typename R>
1442bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1443 const UnwindInfoSections &sects,
1444 uint32_t fdeSectionOffsetHint) {
1445 typename CFI_Parser<A>::FDE_Info fdeInfo;
1446 typename CFI_Parser<A>::CIE_Info cieInfo;
1447 bool foundFDE = false;
1448 bool foundInCache = false;
1449 // If compact encoding table gave offset into dwarf section, go directly there
1450 if (fdeSectionOffsetHint != 0) {
1451 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1452 (uint32_t)sects.dwarf_section_length,
1453 sects.dwarf_section + fdeSectionOffsetHint,
1454 &fdeInfo, &cieInfo);
1455 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001456#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001457 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1458 foundFDE = EHHeaderParser<A>::findFDE(
1459 _addressSpace, pc, sects.dwarf_index_section,
1460 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1461 }
1462#endif
1463 if (!foundFDE) {
1464 // otherwise, search cache of previously found FDEs.
1465 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1466 if (cachedFDE != 0) {
1467 foundFDE =
1468 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1469 (uint32_t)sects.dwarf_section_length,
1470 cachedFDE, &fdeInfo, &cieInfo);
1471 foundInCache = foundFDE;
1472 }
1473 }
1474 if (!foundFDE) {
1475 // Still not found, do full scan of __eh_frame section.
1476 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1477 (uint32_t)sects.dwarf_section_length, 0,
1478 &fdeInfo, &cieInfo);
1479 }
1480 if (foundFDE) {
1481 typename CFI_Parser<A>::PrologInfo prolog;
1482 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001483 R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001484 // Save off parsed FDE info
1485 _info.start_ip = fdeInfo.pcStart;
1486 _info.end_ip = fdeInfo.pcEnd;
1487 _info.lsda = fdeInfo.lsda;
1488 _info.handler = cieInfo.personality;
1489 _info.gp = prolog.spExtraArgSize;
1490 _info.flags = 0;
1491 _info.format = dwarfEncoding();
1492 _info.unwind_info = fdeInfo.fdeStart;
1493 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1494 _info.extra = (unw_word_t) sects.dso_base;
1495
1496 // Add to cache (to make next lookup faster) if we had no hint
1497 // and there was no index.
1498 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001499 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001500 if (sects.dwarf_index_section == 0)
1501 #endif
1502 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1503 fdeInfo.fdeStart);
1504 }
1505 return true;
1506 }
1507 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001508 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001509 return false;
1510}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001511#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001512
1513
Ranjeet Singh421231a2017-03-31 15:28:06 +00001514#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001515template <typename A, typename R>
1516bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1517 const UnwindInfoSections &sects) {
1518 const bool log = false;
1519 if (log)
1520 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1521 (uint64_t)pc, (uint64_t)sects.dso_base);
1522
1523 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1524 sects.compact_unwind_section);
1525 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1526 return false;
1527
1528 // do a binary search of top level index to find page with unwind info
1529 pint_t targetFunctionOffset = pc - sects.dso_base;
1530 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1531 sects.compact_unwind_section
1532 + sectionHeader.indexSectionOffset());
1533 uint32_t low = 0;
1534 uint32_t high = sectionHeader.indexCount();
1535 uint32_t last = high - 1;
1536 while (low < high) {
1537 uint32_t mid = (low + high) / 2;
1538 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1539 //mid, low, high, topIndex.functionOffset(mid));
1540 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1541 if ((mid == last) ||
1542 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1543 low = mid;
1544 break;
1545 } else {
1546 low = mid + 1;
1547 }
1548 } else {
1549 high = mid;
1550 }
1551 }
1552 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1553 const uint32_t firstLevelNextPageFunctionOffset =
1554 topIndex.functionOffset(low + 1);
1555 const pint_t secondLevelAddr =
1556 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1557 const pint_t lsdaArrayStartAddr =
1558 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1559 const pint_t lsdaArrayEndAddr =
1560 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1561 if (log)
1562 fprintf(stderr, "\tfirst level search for result index=%d "
1563 "to secondLevelAddr=0x%llX\n",
1564 low, (uint64_t) secondLevelAddr);
1565 // do a binary search of second level page index
1566 uint32_t encoding = 0;
1567 pint_t funcStart = 0;
1568 pint_t funcEnd = 0;
1569 pint_t lsda = 0;
1570 pint_t personality = 0;
1571 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1572 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1573 // regular page
1574 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1575 secondLevelAddr);
1576 UnwindSectionRegularArray<A> pageIndex(
1577 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1578 // binary search looks for entry with e where index[e].offset <= pc <
1579 // index[e+1].offset
1580 if (log)
1581 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1582 "regular page starting at secondLevelAddr=0x%llX\n",
1583 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1584 low = 0;
1585 high = pageHeader.entryCount();
1586 while (low < high) {
1587 uint32_t mid = (low + high) / 2;
1588 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1589 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1590 // at end of table
1591 low = mid;
1592 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1593 break;
1594 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1595 // next is too big, so we found it
1596 low = mid;
1597 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1598 break;
1599 } else {
1600 low = mid + 1;
1601 }
1602 } else {
1603 high = mid;
1604 }
1605 }
1606 encoding = pageIndex.encoding(low);
1607 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1608 if (pc < funcStart) {
1609 if (log)
1610 fprintf(
1611 stderr,
1612 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1613 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1614 return false;
1615 }
1616 if (pc > funcEnd) {
1617 if (log)
1618 fprintf(
1619 stderr,
1620 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1621 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1622 return false;
1623 }
1624 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1625 // compressed page
1626 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1627 secondLevelAddr);
1628 UnwindSectionCompressedArray<A> pageIndex(
1629 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1630 const uint32_t targetFunctionPageOffset =
1631 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1632 // binary search looks for entry with e where index[e].offset <= pc <
1633 // index[e+1].offset
1634 if (log)
1635 fprintf(stderr, "\tbinary search of compressed page starting at "
1636 "secondLevelAddr=0x%llX\n",
1637 (uint64_t) secondLevelAddr);
1638 low = 0;
1639 last = pageHeader.entryCount() - 1;
1640 high = pageHeader.entryCount();
1641 while (low < high) {
1642 uint32_t mid = (low + high) / 2;
1643 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1644 if ((mid == last) ||
1645 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1646 low = mid;
1647 break;
1648 } else {
1649 low = mid + 1;
1650 }
1651 } else {
1652 high = mid;
1653 }
1654 }
1655 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1656 + sects.dso_base;
1657 if (low < last)
1658 funcEnd =
1659 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1660 + sects.dso_base;
1661 else
1662 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1663 if (pc < funcStart) {
1664 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001665 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001666 (uint64_t) pc, (uint64_t) funcStart);
1667 return false;
1668 }
1669 if (pc > funcEnd) {
1670 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001671 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001672 (uint64_t) pc, (uint64_t) funcEnd);
1673 return false;
1674 }
1675 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1676 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1677 // encoding is in common table in section header
1678 encoding = _addressSpace.get32(
1679 sects.compact_unwind_section +
1680 sectionHeader.commonEncodingsArraySectionOffset() +
1681 encodingIndex * sizeof(uint32_t));
1682 } else {
1683 // encoding is in page specific table
1684 uint16_t pageEncodingIndex =
1685 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1686 encoding = _addressSpace.get32(secondLevelAddr +
1687 pageHeader.encodingsPageOffset() +
1688 pageEncodingIndex * sizeof(uint32_t));
1689 }
1690 } else {
1691 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001692 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001693 (uint64_t) sects.compact_unwind_section);
1694 return false;
1695 }
1696
1697 // look up LSDA, if encoding says function has one
1698 if (encoding & UNWIND_HAS_LSDA) {
1699 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1700 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1701 low = 0;
1702 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1703 sizeof(unwind_info_section_header_lsda_index_entry);
1704 // binary search looks for entry with exact match for functionOffset
1705 if (log)
1706 fprintf(stderr,
1707 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1708 funcStartOffset);
1709 while (low < high) {
1710 uint32_t mid = (low + high) / 2;
1711 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1712 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1713 break;
1714 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1715 low = mid + 1;
1716 } else {
1717 high = mid;
1718 }
1719 }
1720 if (lsda == 0) {
1721 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001722 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001723 encoding, (uint64_t) pc);
1724 return false;
1725 }
1726 }
1727
1728 // extact personality routine, if encoding says function has one
1729 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1730 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1731 if (personalityIndex != 0) {
1732 --personalityIndex; // change 1-based to zero-based index
1733 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1734 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001735 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001736 encoding, personalityIndex,
1737 sectionHeader.personalityArrayCount());
1738 return false;
1739 }
1740 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1741 sects.compact_unwind_section +
1742 sectionHeader.personalityArraySectionOffset() +
1743 personalityIndex * sizeof(uint32_t));
1744 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1745 personality = _addressSpace.getP(personalityPointer);
1746 if (log)
1747 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1748 "personalityDelta=0x%08X, personality=0x%08llX\n",
1749 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1750 }
1751
1752 if (log)
1753 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1754 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1755 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1756 _info.start_ip = funcStart;
1757 _info.end_ip = funcEnd;
1758 _info.lsda = lsda;
1759 _info.handler = personality;
1760 _info.gp = 0;
1761 _info.flags = 0;
1762 _info.format = encoding;
1763 _info.unwind_info = 0;
1764 _info.unwind_info_size = 0;
1765 _info.extra = sects.dso_base;
1766 return true;
1767}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001768#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001769
1770
Charles Davisfa2e6202018-08-30 21:29:00 +00001771#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1772template <typename A, typename R>
1773bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1774 pint_t base;
1775 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1776 if (!unwindEntry) {
1777 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1778 return false;
1779 }
1780 _info.gp = 0;
1781 _info.flags = 0;
1782 _info.format = 0;
1783 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1784 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1785 _info.extra = base;
1786 _info.start_ip = base + unwindEntry->BeginAddress;
1787#ifdef _LIBUNWIND_TARGET_X86_64
1788 _info.end_ip = base + unwindEntry->EndAddress;
1789 // Only fill in the handler and LSDA if they're stale.
1790 if (pc != getLastPC()) {
1791 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1792 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1793 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1794 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1795 // these structures.)
1796 // N.B. UNWIND_INFO structs are DWORD-aligned.
1797 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1798 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1799 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1800 if (*handler) {
1801 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1802 } else
1803 _info.handler = 0;
1804 } else {
1805 _info.lsda = 0;
1806 _info.handler = 0;
1807 }
1808 }
1809#elif defined(_LIBUNWIND_TARGET_ARM)
1810 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1811 _info.lsda = 0; // FIXME
1812 _info.handler = 0; // FIXME
1813#endif
1814 setLastPC(pc);
1815 return true;
1816}
1817#endif
1818
1819
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001820template <typename A, typename R>
1821void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1822 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001823#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001824 // Remove the thumb bit so the IP represents the actual instruction address.
1825 // This matches the behaviour of _Unwind_GetIP on arm.
1826 pc &= (pint_t)~0x1;
1827#endif
1828
1829 // If the last line of a function is a "throw" the compiler sometimes
1830 // emits no instructions after the call to __cxa_throw. This means
1831 // the return address is actually the start of the next function.
1832 // To disambiguate this, back up the pc when we know it is a return
1833 // address.
1834 if (isReturnAddress)
1835 --pc;
1836
1837 // Ask address space object to find unwind sections for this pc.
1838 UnwindInfoSections sects;
1839 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001840#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001841 // If there is a compact unwind encoding table, look there first.
1842 if (sects.compact_unwind_section != 0) {
1843 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001844 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001845 // Found info in table, done unless encoding says to use dwarf.
1846 uint32_t dwarfOffset;
1847 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1848 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1849 // found info in dwarf, done
1850 return;
1851 }
1852 }
1853 #endif
1854 // If unwind table has entry, but entry says there is no unwind info,
1855 // record that we have no unwind info.
1856 if (_info.format == 0)
1857 _unwindInfoMissing = true;
1858 return;
1859 }
1860 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001861#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001862
Charles Davisfa2e6202018-08-30 21:29:00 +00001863#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1864 // If there is SEH unwind info, look there next.
1865 if (this->getInfoFromSEH(pc))
1866 return;
1867#endif
1868
Ranjeet Singh421231a2017-03-31 15:28:06 +00001869#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001870 // If there is dwarf unwind info, look there next.
1871 if (sects.dwarf_section != 0) {
1872 if (this->getInfoFromDwarfSection(pc, sects)) {
1873 // found info in dwarf, done
1874 return;
1875 }
1876 }
1877#endif
1878
Ranjeet Singh421231a2017-03-31 15:28:06 +00001879#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001880 // If there is ARM EHABI unwind info, look there next.
1881 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1882 return;
1883#endif
1884 }
1885
Ranjeet Singh421231a2017-03-31 15:28:06 +00001886#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001887 // There is no static unwind info for this pc. Look to see if an FDE was
1888 // dynamically registered for it.
1889 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1890 if (cachedFDE != 0) {
1891 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1892 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1893 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1894 cachedFDE, &fdeInfo, &cieInfo);
1895 if (msg == NULL) {
1896 typename CFI_Parser<A>::PrologInfo prolog;
1897 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001898 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001899 // save off parsed FDE info
1900 _info.start_ip = fdeInfo.pcStart;
1901 _info.end_ip = fdeInfo.pcEnd;
1902 _info.lsda = fdeInfo.lsda;
1903 _info.handler = cieInfo.personality;
1904 _info.gp = prolog.spExtraArgSize;
1905 // Some frameless functions need SP
1906 // altered when resuming in function.
1907 _info.flags = 0;
1908 _info.format = dwarfEncoding();
1909 _info.unwind_info = fdeInfo.fdeStart;
1910 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1911 _info.extra = 0;
1912 return;
1913 }
1914 }
1915 }
1916
1917 // Lastly, ask AddressSpace object about platform specific ways to locate
1918 // other FDEs.
1919 pint_t fde;
1920 if (_addressSpace.findOtherFDE(pc, fde)) {
1921 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1922 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1923 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1924 // Double check this FDE is for a function that includes the pc.
1925 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1926 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001927 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1928 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001929 // save off parsed FDE info
1930 _info.start_ip = fdeInfo.pcStart;
1931 _info.end_ip = fdeInfo.pcEnd;
1932 _info.lsda = fdeInfo.lsda;
1933 _info.handler = cieInfo.personality;
1934 _info.gp = prolog.spExtraArgSize;
1935 _info.flags = 0;
1936 _info.format = dwarfEncoding();
1937 _info.unwind_info = fdeInfo.fdeStart;
1938 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1939 _info.extra = 0;
1940 return;
1941 }
1942 }
1943 }
1944 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001945#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001946
1947 // no unwind info, flag that we can't reliably unwind
1948 _unwindInfoMissing = true;
1949}
1950
1951template <typename A, typename R>
1952int UnwindCursor<A, R>::step() {
1953 // Bottom of stack is defined is when unwind info cannot be found.
1954 if (_unwindInfoMissing)
1955 return UNW_STEP_END;
1956
1957 // Use unwinding info to modify register set as if function returned.
1958 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001959#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001960 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001961#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1962 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001963#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001964 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001965#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001966 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001967#else
1968 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001969 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001970 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001971 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001972#endif
1973
1974 // update info based on new PC
1975 if (result == UNW_STEP_SUCCESS) {
1976 this->setInfoBasedOnIPRegister(true);
1977 if (_unwindInfoMissing)
1978 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001979 }
1980
1981 return result;
1982}
1983
1984template <typename A, typename R>
1985void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1986 *info = _info;
1987}
1988
1989template <typename A, typename R>
1990bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1991 unw_word_t *offset) {
1992 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1993 buf, bufLen, offset);
1994}
1995
1996} // namespace libunwind
1997
1998#endif // __UNWINDCURSOR_HPP__