blob: 406f229ce62e037a5361dbd924f23e221395a416 [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
Petr Hosek36f61542019-02-02 21:15:49 +0000897 // libunwind does not and should not depend on C++ library which means that we
898 // need our own defition of inline placement new.
899 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
900
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000901private:
902
Ranjeet Singh421231a2017-03-31 15:28:06 +0000903#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000904 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000905
906 int stepWithEHABI() {
907 size_t len = 0;
908 size_t off = 0;
909 // FIXME: Calling decode_eht_entry() here is violating the libunwind
910 // abstraction layer.
911 const uint32_t *ehtp =
912 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
913 &off, &len);
914 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
915 _URC_CONTINUE_UNWIND)
916 return UNW_STEP_END;
917 return UNW_STEP_SUCCESS;
918 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000919#endif
920
Ranjeet Singh421231a2017-03-31 15:28:06 +0000921#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000922 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
923 uint32_t fdeSectionOffsetHint=0);
924 int stepWithDwarfFDE() {
925 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
926 (pint_t)this->getReg(UNW_REG_IP),
927 (pint_t)_info.unwind_info,
928 _registers);
929 }
930#endif
931
Ranjeet Singh421231a2017-03-31 15:28:06 +0000932#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000933 bool getInfoFromCompactEncodingSection(pint_t pc,
934 const UnwindInfoSections &sects);
935 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000936 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000937 if ( compactSaysUseDwarf() )
938 return stepWithDwarfFDE();
939 #endif
940 R dummy;
941 return stepWithCompactEncoding(dummy);
942 }
943
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000944#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000945 int stepWithCompactEncoding(Registers_x86_64 &) {
946 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
947 _info.format, _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_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000952 int stepWithCompactEncoding(Registers_x86 &) {
953 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
954 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
955 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000956#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000957
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000958#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000959 int stepWithCompactEncoding(Registers_ppc &) {
960 return UNW_EINVAL;
961 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000962#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000963
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000964#if defined(_LIBUNWIND_TARGET_PPC64)
965 int stepWithCompactEncoding(Registers_ppc64 &) {
966 return UNW_EINVAL;
967 }
968#endif
969
970
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000971#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000972 int stepWithCompactEncoding(Registers_arm64 &) {
973 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
974 _info.format, _info.start_ip, _addressSpace, _registers);
975 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000976#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000977
John Baldwin56441d42017-12-12 21:43:36 +0000978#if defined(_LIBUNWIND_TARGET_MIPS_O32)
979 int stepWithCompactEncoding(Registers_mips_o32 &) {
980 return UNW_EINVAL;
981 }
982#endif
983
John Baldwin541a4352018-01-09 17:07:18 +0000984#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
985 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000986 return UNW_EINVAL;
987 }
988#endif
989
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000990#if defined(_LIBUNWIND_TARGET_SPARC)
991 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
992#endif
993
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000994 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
995 R dummy;
996 return compactSaysUseDwarf(dummy, offset);
997 }
998
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000999#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001000 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1001 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1002 if (offset)
1003 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1004 return true;
1005 }
1006 return false;
1007 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001008#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001009
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001010#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001011 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1012 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1013 if (offset)
1014 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1015 return true;
1016 }
1017 return false;
1018 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001019#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001020
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001021#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001022 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1023 return true;
1024 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001025#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001026
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001027#if defined(_LIBUNWIND_TARGET_PPC64)
1028 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1029 return true;
1030 }
1031#endif
1032
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001033#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001034 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1035 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1036 if (offset)
1037 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1038 return true;
1039 }
1040 return false;
1041 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001042#endif
John Baldwin56441d42017-12-12 21:43:36 +00001043
1044#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1045 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1046 return true;
1047 }
1048#endif
1049
John Baldwin541a4352018-01-09 17:07:18 +00001050#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1051 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001052 return true;
1053 }
1054#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001055
1056#if defined(_LIBUNWIND_TARGET_SPARC)
1057 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1058#endif
1059
Ranjeet Singh421231a2017-03-31 15:28:06 +00001060#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001061
Ranjeet Singh421231a2017-03-31 15:28:06 +00001062#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001063 compact_unwind_encoding_t dwarfEncoding() const {
1064 R dummy;
1065 return dwarfEncoding(dummy);
1066 }
1067
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001068#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001069 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1070 return UNWIND_X86_64_MODE_DWARF;
1071 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001072#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001073
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001074#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001075 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1076 return UNWIND_X86_MODE_DWARF;
1077 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001078#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001079
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001080#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001081 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1082 return 0;
1083 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001084#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001085
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001086#if defined(_LIBUNWIND_TARGET_PPC64)
1087 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1088 return 0;
1089 }
1090#endif
1091
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001092#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001093 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1094 return UNWIND_ARM64_MODE_DWARF;
1095 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001096#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001097
Martin Storsjoa72285f2017-11-02 08:16:16 +00001098#if defined(_LIBUNWIND_TARGET_ARM)
1099 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1100 return 0;
1101 }
1102#endif
1103
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001104#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001105 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1106 return 0;
1107 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001108#endif
John Baldwin56441d42017-12-12 21:43:36 +00001109
1110#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1111 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1112 return 0;
1113 }
1114#endif
1115
John Baldwin541a4352018-01-09 17:07:18 +00001116#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1117 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001118 return 0;
1119 }
1120#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001121
1122#if defined(_LIBUNWIND_TARGET_SPARC)
1123 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1124#endif
1125
Ranjeet Singh421231a2017-03-31 15:28:06 +00001126#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001127
Charles Davisfa2e6202018-08-30 21:29:00 +00001128#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1129 // For runtime environments using SEH unwind data without Windows runtime
1130 // support.
1131 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1132 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1133 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1134 /* FIXME: Implement */
1135 *base = 0;
1136 return nullptr;
1137 }
1138 bool getInfoFromSEH(pint_t pc);
1139 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1140#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1141
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001142
1143 A &_addressSpace;
1144 R _registers;
1145 unw_proc_info_t _info;
1146 bool _unwindInfoMissing;
1147 bool _isSignalFrame;
1148};
1149
1150
1151template <typename A, typename R>
1152UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1153 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1154 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001155 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001156 "UnwindCursor<> does not fit in unw_cursor_t");
1157 memset(&_info, 0, sizeof(_info));
1158}
1159
1160template <typename A, typename R>
1161UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1162 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1163 memset(&_info, 0, sizeof(_info));
1164 // FIXME
1165 // fill in _registers from thread arg
1166}
1167
1168
1169template <typename A, typename R>
1170bool UnwindCursor<A, R>::validReg(int regNum) {
1171 return _registers.validRegister(regNum);
1172}
1173
1174template <typename A, typename R>
1175unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1176 return _registers.getRegister(regNum);
1177}
1178
1179template <typename A, typename R>
1180void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1181 _registers.setRegister(regNum, (typename A::pint_t)value);
1182}
1183
1184template <typename A, typename R>
1185bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1186 return _registers.validFloatRegister(regNum);
1187}
1188
1189template <typename A, typename R>
1190unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1191 return _registers.getFloatRegister(regNum);
1192}
1193
1194template <typename A, typename R>
1195void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1196 _registers.setFloatRegister(regNum, value);
1197}
1198
1199template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1200 _registers.jumpto();
1201}
1202
1203#ifdef __arm__
1204template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1205 _registers.saveVFPAsX();
1206}
1207#endif
1208
1209template <typename A, typename R>
1210const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1211 return _registers.getRegisterName(regNum);
1212}
1213
1214template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1215 return _isSignalFrame;
1216}
1217
Charles Davisfa2e6202018-08-30 21:29:00 +00001218#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1219
Ranjeet Singh421231a2017-03-31 15:28:06 +00001220#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001221struct EHABIIndexEntry {
1222 uint32_t functionOffset;
1223 uint32_t data;
1224};
1225
1226template<typename A>
1227struct EHABISectionIterator {
1228 typedef EHABISectionIterator _Self;
1229
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001230 typedef typename A::pint_t value_type;
1231 typedef typename A::pint_t* pointer;
1232 typedef typename A::pint_t& reference;
1233 typedef size_t size_type;
1234 typedef size_t difference_type;
1235
1236 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1237 return _Self(addressSpace, sects, 0);
1238 }
1239 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001240 return _Self(addressSpace, sects,
1241 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001242 }
1243
1244 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1245 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1246
1247 _Self& operator++() { ++_i; return *this; }
1248 _Self& operator+=(size_t a) { _i += a; return *this; }
1249 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1250 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1251
1252 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1253 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1254
1255 size_t operator-(const _Self& other) { return _i - other._i; }
1256
1257 bool operator==(const _Self& other) const {
1258 assert(_addressSpace == other._addressSpace);
1259 assert(_sects == other._sects);
1260 return _i == other._i;
1261 }
1262
1263 typename A::pint_t operator*() const { return functionAddress(); }
1264
1265 typename A::pint_t functionAddress() const {
1266 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1267 EHABIIndexEntry, _i, functionOffset);
1268 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1269 }
1270
1271 typename A::pint_t dataAddress() {
1272 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1273 EHABIIndexEntry, _i, data);
1274 return indexAddr;
1275 }
1276
1277 private:
1278 size_t _i;
1279 A* _addressSpace;
1280 const UnwindInfoSections* _sects;
1281};
1282
Petr Hosekac0d9e02019-01-29 22:26:18 +00001283namespace {
1284
1285template <typename A>
1286EHABISectionIterator<A> EHABISectionUpperBound(
1287 EHABISectionIterator<A> first,
1288 EHABISectionIterator<A> last,
1289 typename A::pint_t value) {
1290 size_t len = last - first;
1291 while (len > 0) {
1292 size_t l2 = len / 2;
1293 EHABISectionIterator<A> m = first + l2;
1294 if (value < *m) {
1295 len = l2;
1296 } else {
1297 first = ++m;
1298 len -= l2 + 1;
1299 }
1300 }
1301 return first;
1302}
1303
1304}
1305
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001306template <typename A, typename R>
1307bool UnwindCursor<A, R>::getInfoFromEHABISection(
1308 pint_t pc,
1309 const UnwindInfoSections &sects) {
1310 EHABISectionIterator<A> begin =
1311 EHABISectionIterator<A>::begin(_addressSpace, sects);
1312 EHABISectionIterator<A> end =
1313 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001314 if (begin == end)
1315 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001316
Petr Hosekac0d9e02019-01-29 22:26:18 +00001317 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001318 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001319 return false;
1320 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1321
1322 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001323 // If an exception is thrown from a function, corresponding to the last entry
1324 // in the table, we don't really know the function extent and have to choose a
1325 // value for nextPC. Choosing max() will allow the range check during trace to
1326 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001327 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001328 pint_t indexDataAddr = itThisPC.dataAddress();
1329
1330 if (indexDataAddr == 0)
1331 return false;
1332
1333 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1334 if (indexData == UNW_EXIDX_CANTUNWIND)
1335 return false;
1336
1337 // If the high bit is set, the exception handling table entry is inline inside
1338 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1339 // the table points at an offset in the exception handling table (section 5 EHABI).
1340 pint_t exceptionTableAddr;
1341 uint32_t exceptionTableData;
1342 bool isSingleWordEHT;
1343 if (indexData & 0x80000000) {
1344 exceptionTableAddr = indexDataAddr;
1345 // TODO(ajwong): Should this data be 0?
1346 exceptionTableData = indexData;
1347 isSingleWordEHT = true;
1348 } else {
1349 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1350 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1351 isSingleWordEHT = false;
1352 }
1353
1354 // Now we know the 3 things:
1355 // exceptionTableAddr -- exception handler table entry.
1356 // exceptionTableData -- the data inside the first word of the eht entry.
1357 // isSingleWordEHT -- whether the entry is in the index.
1358 unw_word_t personalityRoutine = 0xbadf00d;
1359 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001360 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001361
1362 // If the high bit in the exception handling table entry is set, the entry is
1363 // in compact form (section 6.3 EHABI).
1364 if (exceptionTableData & 0x80000000) {
1365 // Grab the index of the personality routine from the compact form.
1366 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1367 uint32_t extraWords = 0;
1368 switch (choice) {
1369 case 0:
1370 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1371 extraWords = 0;
1372 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001373 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001374 break;
1375 case 1:
1376 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1377 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1378 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001379 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001380 break;
1381 case 2:
1382 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1383 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1384 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001385 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001386 break;
1387 default:
1388 _LIBUNWIND_ABORT("unknown personality routine");
1389 return false;
1390 }
1391
1392 if (isSingleWordEHT) {
1393 if (extraWords != 0) {
1394 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1395 "requires extra words");
1396 return false;
1397 }
1398 }
1399 } else {
1400 pint_t personalityAddr =
1401 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1402 personalityRoutine = personalityAddr;
1403
1404 // ARM EHABI # 6.2, # 9.2
1405 //
1406 // +---- ehtp
1407 // v
1408 // +--------------------------------------+
1409 // | +--------+--------+--------+-------+ |
1410 // | |0| prel31 to personalityRoutine | |
1411 // | +--------+--------+--------+-------+ |
1412 // | | N | unwind opcodes | | <-- UnwindData
1413 // | +--------+--------+--------+-------+ |
1414 // | | Word 2 unwind opcodes | |
1415 // | +--------+--------+--------+-------+ |
1416 // | ... |
1417 // | +--------+--------+--------+-------+ |
1418 // | | Word N unwind opcodes | |
1419 // | +--------+--------+--------+-------+ |
1420 // | | LSDA | | <-- lsda
1421 // | | ... | |
1422 // | +--------+--------+--------+-------+ |
1423 // +--------------------------------------+
1424
1425 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1426 uint32_t FirstDataWord = *UnwindData;
1427 size_t N = ((FirstDataWord >> 24) & 0xff);
1428 size_t NDataWords = N + 1;
1429 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1430 }
1431
1432 _info.start_ip = thisPC;
1433 _info.end_ip = nextPC;
1434 _info.handler = personalityRoutine;
1435 _info.unwind_info = exceptionTableAddr;
1436 _info.lsda = lsda;
1437 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1438 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1439
1440 return true;
1441}
1442#endif
1443
Ranjeet Singh421231a2017-03-31 15:28:06 +00001444#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001445template <typename A, typename R>
1446bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1447 const UnwindInfoSections &sects,
1448 uint32_t fdeSectionOffsetHint) {
1449 typename CFI_Parser<A>::FDE_Info fdeInfo;
1450 typename CFI_Parser<A>::CIE_Info cieInfo;
1451 bool foundFDE = false;
1452 bool foundInCache = false;
1453 // If compact encoding table gave offset into dwarf section, go directly there
1454 if (fdeSectionOffsetHint != 0) {
1455 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1456 (uint32_t)sects.dwarf_section_length,
1457 sects.dwarf_section + fdeSectionOffsetHint,
1458 &fdeInfo, &cieInfo);
1459 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001460#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001461 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1462 foundFDE = EHHeaderParser<A>::findFDE(
1463 _addressSpace, pc, sects.dwarf_index_section,
1464 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1465 }
1466#endif
1467 if (!foundFDE) {
1468 // otherwise, search cache of previously found FDEs.
1469 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1470 if (cachedFDE != 0) {
1471 foundFDE =
1472 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1473 (uint32_t)sects.dwarf_section_length,
1474 cachedFDE, &fdeInfo, &cieInfo);
1475 foundInCache = foundFDE;
1476 }
1477 }
1478 if (!foundFDE) {
1479 // Still not found, do full scan of __eh_frame section.
1480 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1481 (uint32_t)sects.dwarf_section_length, 0,
1482 &fdeInfo, &cieInfo);
1483 }
1484 if (foundFDE) {
1485 typename CFI_Parser<A>::PrologInfo prolog;
1486 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001487 R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001488 // Save off parsed FDE info
1489 _info.start_ip = fdeInfo.pcStart;
1490 _info.end_ip = fdeInfo.pcEnd;
1491 _info.lsda = fdeInfo.lsda;
1492 _info.handler = cieInfo.personality;
1493 _info.gp = prolog.spExtraArgSize;
1494 _info.flags = 0;
1495 _info.format = dwarfEncoding();
1496 _info.unwind_info = fdeInfo.fdeStart;
1497 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1498 _info.extra = (unw_word_t) sects.dso_base;
1499
1500 // Add to cache (to make next lookup faster) if we had no hint
1501 // and there was no index.
1502 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001503 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001504 if (sects.dwarf_index_section == 0)
1505 #endif
1506 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1507 fdeInfo.fdeStart);
1508 }
1509 return true;
1510 }
1511 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001512 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001513 return false;
1514}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001515#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001516
1517
Ranjeet Singh421231a2017-03-31 15:28:06 +00001518#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001519template <typename A, typename R>
1520bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1521 const UnwindInfoSections &sects) {
1522 const bool log = false;
1523 if (log)
1524 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1525 (uint64_t)pc, (uint64_t)sects.dso_base);
1526
1527 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1528 sects.compact_unwind_section);
1529 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1530 return false;
1531
1532 // do a binary search of top level index to find page with unwind info
1533 pint_t targetFunctionOffset = pc - sects.dso_base;
1534 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1535 sects.compact_unwind_section
1536 + sectionHeader.indexSectionOffset());
1537 uint32_t low = 0;
1538 uint32_t high = sectionHeader.indexCount();
1539 uint32_t last = high - 1;
1540 while (low < high) {
1541 uint32_t mid = (low + high) / 2;
1542 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1543 //mid, low, high, topIndex.functionOffset(mid));
1544 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1545 if ((mid == last) ||
1546 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1547 low = mid;
1548 break;
1549 } else {
1550 low = mid + 1;
1551 }
1552 } else {
1553 high = mid;
1554 }
1555 }
1556 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1557 const uint32_t firstLevelNextPageFunctionOffset =
1558 topIndex.functionOffset(low + 1);
1559 const pint_t secondLevelAddr =
1560 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1561 const pint_t lsdaArrayStartAddr =
1562 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1563 const pint_t lsdaArrayEndAddr =
1564 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1565 if (log)
1566 fprintf(stderr, "\tfirst level search for result index=%d "
1567 "to secondLevelAddr=0x%llX\n",
1568 low, (uint64_t) secondLevelAddr);
1569 // do a binary search of second level page index
1570 uint32_t encoding = 0;
1571 pint_t funcStart = 0;
1572 pint_t funcEnd = 0;
1573 pint_t lsda = 0;
1574 pint_t personality = 0;
1575 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1576 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1577 // regular page
1578 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1579 secondLevelAddr);
1580 UnwindSectionRegularArray<A> pageIndex(
1581 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1582 // binary search looks for entry with e where index[e].offset <= pc <
1583 // index[e+1].offset
1584 if (log)
1585 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1586 "regular page starting at secondLevelAddr=0x%llX\n",
1587 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1588 low = 0;
1589 high = pageHeader.entryCount();
1590 while (low < high) {
1591 uint32_t mid = (low + high) / 2;
1592 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1593 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1594 // at end of table
1595 low = mid;
1596 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1597 break;
1598 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1599 // next is too big, so we found it
1600 low = mid;
1601 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1602 break;
1603 } else {
1604 low = mid + 1;
1605 }
1606 } else {
1607 high = mid;
1608 }
1609 }
1610 encoding = pageIndex.encoding(low);
1611 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1612 if (pc < funcStart) {
1613 if (log)
1614 fprintf(
1615 stderr,
1616 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1617 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1618 return false;
1619 }
1620 if (pc > funcEnd) {
1621 if (log)
1622 fprintf(
1623 stderr,
1624 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1625 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1626 return false;
1627 }
1628 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1629 // compressed page
1630 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1631 secondLevelAddr);
1632 UnwindSectionCompressedArray<A> pageIndex(
1633 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1634 const uint32_t targetFunctionPageOffset =
1635 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1636 // binary search looks for entry with e where index[e].offset <= pc <
1637 // index[e+1].offset
1638 if (log)
1639 fprintf(stderr, "\tbinary search of compressed page starting at "
1640 "secondLevelAddr=0x%llX\n",
1641 (uint64_t) secondLevelAddr);
1642 low = 0;
1643 last = pageHeader.entryCount() - 1;
1644 high = pageHeader.entryCount();
1645 while (low < high) {
1646 uint32_t mid = (low + high) / 2;
1647 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1648 if ((mid == last) ||
1649 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1650 low = mid;
1651 break;
1652 } else {
1653 low = mid + 1;
1654 }
1655 } else {
1656 high = mid;
1657 }
1658 }
1659 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1660 + sects.dso_base;
1661 if (low < last)
1662 funcEnd =
1663 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1664 + sects.dso_base;
1665 else
1666 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1667 if (pc < funcStart) {
1668 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001669 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001670 (uint64_t) pc, (uint64_t) funcStart);
1671 return false;
1672 }
1673 if (pc > funcEnd) {
1674 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001675 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001676 (uint64_t) pc, (uint64_t) funcEnd);
1677 return false;
1678 }
1679 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1680 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1681 // encoding is in common table in section header
1682 encoding = _addressSpace.get32(
1683 sects.compact_unwind_section +
1684 sectionHeader.commonEncodingsArraySectionOffset() +
1685 encodingIndex * sizeof(uint32_t));
1686 } else {
1687 // encoding is in page specific table
1688 uint16_t pageEncodingIndex =
1689 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1690 encoding = _addressSpace.get32(secondLevelAddr +
1691 pageHeader.encodingsPageOffset() +
1692 pageEncodingIndex * sizeof(uint32_t));
1693 }
1694 } else {
1695 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001696 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001697 (uint64_t) sects.compact_unwind_section);
1698 return false;
1699 }
1700
1701 // look up LSDA, if encoding says function has one
1702 if (encoding & UNWIND_HAS_LSDA) {
1703 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1704 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1705 low = 0;
1706 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1707 sizeof(unwind_info_section_header_lsda_index_entry);
1708 // binary search looks for entry with exact match for functionOffset
1709 if (log)
1710 fprintf(stderr,
1711 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1712 funcStartOffset);
1713 while (low < high) {
1714 uint32_t mid = (low + high) / 2;
1715 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1716 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1717 break;
1718 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1719 low = mid + 1;
1720 } else {
1721 high = mid;
1722 }
1723 }
1724 if (lsda == 0) {
1725 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001726 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001727 encoding, (uint64_t) pc);
1728 return false;
1729 }
1730 }
1731
1732 // extact personality routine, if encoding says function has one
1733 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1734 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1735 if (personalityIndex != 0) {
1736 --personalityIndex; // change 1-based to zero-based index
1737 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1738 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001739 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001740 encoding, personalityIndex,
1741 sectionHeader.personalityArrayCount());
1742 return false;
1743 }
1744 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1745 sects.compact_unwind_section +
1746 sectionHeader.personalityArraySectionOffset() +
1747 personalityIndex * sizeof(uint32_t));
1748 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1749 personality = _addressSpace.getP(personalityPointer);
1750 if (log)
1751 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1752 "personalityDelta=0x%08X, personality=0x%08llX\n",
1753 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1754 }
1755
1756 if (log)
1757 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1758 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1759 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1760 _info.start_ip = funcStart;
1761 _info.end_ip = funcEnd;
1762 _info.lsda = lsda;
1763 _info.handler = personality;
1764 _info.gp = 0;
1765 _info.flags = 0;
1766 _info.format = encoding;
1767 _info.unwind_info = 0;
1768 _info.unwind_info_size = 0;
1769 _info.extra = sects.dso_base;
1770 return true;
1771}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001772#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001773
1774
Charles Davisfa2e6202018-08-30 21:29:00 +00001775#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1776template <typename A, typename R>
1777bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1778 pint_t base;
1779 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1780 if (!unwindEntry) {
1781 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1782 return false;
1783 }
1784 _info.gp = 0;
1785 _info.flags = 0;
1786 _info.format = 0;
1787 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1788 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1789 _info.extra = base;
1790 _info.start_ip = base + unwindEntry->BeginAddress;
1791#ifdef _LIBUNWIND_TARGET_X86_64
1792 _info.end_ip = base + unwindEntry->EndAddress;
1793 // Only fill in the handler and LSDA if they're stale.
1794 if (pc != getLastPC()) {
1795 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1796 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1797 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1798 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1799 // these structures.)
1800 // N.B. UNWIND_INFO structs are DWORD-aligned.
1801 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1802 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1803 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1804 if (*handler) {
1805 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1806 } else
1807 _info.handler = 0;
1808 } else {
1809 _info.lsda = 0;
1810 _info.handler = 0;
1811 }
1812 }
1813#elif defined(_LIBUNWIND_TARGET_ARM)
1814 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1815 _info.lsda = 0; // FIXME
1816 _info.handler = 0; // FIXME
1817#endif
1818 setLastPC(pc);
1819 return true;
1820}
1821#endif
1822
1823
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001824template <typename A, typename R>
1825void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1826 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001827#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001828 // Remove the thumb bit so the IP represents the actual instruction address.
1829 // This matches the behaviour of _Unwind_GetIP on arm.
1830 pc &= (pint_t)~0x1;
1831#endif
1832
1833 // If the last line of a function is a "throw" the compiler sometimes
1834 // emits no instructions after the call to __cxa_throw. This means
1835 // the return address is actually the start of the next function.
1836 // To disambiguate this, back up the pc when we know it is a return
1837 // address.
1838 if (isReturnAddress)
1839 --pc;
1840
1841 // Ask address space object to find unwind sections for this pc.
1842 UnwindInfoSections sects;
1843 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001844#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001845 // If there is a compact unwind encoding table, look there first.
1846 if (sects.compact_unwind_section != 0) {
1847 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001848 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001849 // Found info in table, done unless encoding says to use dwarf.
1850 uint32_t dwarfOffset;
1851 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1852 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1853 // found info in dwarf, done
1854 return;
1855 }
1856 }
1857 #endif
1858 // If unwind table has entry, but entry says there is no unwind info,
1859 // record that we have no unwind info.
1860 if (_info.format == 0)
1861 _unwindInfoMissing = true;
1862 return;
1863 }
1864 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001865#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001866
Charles Davisfa2e6202018-08-30 21:29:00 +00001867#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1868 // If there is SEH unwind info, look there next.
1869 if (this->getInfoFromSEH(pc))
1870 return;
1871#endif
1872
Ranjeet Singh421231a2017-03-31 15:28:06 +00001873#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001874 // If there is dwarf unwind info, look there next.
1875 if (sects.dwarf_section != 0) {
1876 if (this->getInfoFromDwarfSection(pc, sects)) {
1877 // found info in dwarf, done
1878 return;
1879 }
1880 }
1881#endif
1882
Ranjeet Singh421231a2017-03-31 15:28:06 +00001883#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001884 // If there is ARM EHABI unwind info, look there next.
1885 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1886 return;
1887#endif
1888 }
1889
Ranjeet Singh421231a2017-03-31 15:28:06 +00001890#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001891 // There is no static unwind info for this pc. Look to see if an FDE was
1892 // dynamically registered for it.
1893 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1894 if (cachedFDE != 0) {
1895 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1896 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1897 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1898 cachedFDE, &fdeInfo, &cieInfo);
1899 if (msg == NULL) {
1900 typename CFI_Parser<A>::PrologInfo prolog;
1901 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001902 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001903 // save off parsed FDE info
1904 _info.start_ip = fdeInfo.pcStart;
1905 _info.end_ip = fdeInfo.pcEnd;
1906 _info.lsda = fdeInfo.lsda;
1907 _info.handler = cieInfo.personality;
1908 _info.gp = prolog.spExtraArgSize;
1909 // Some frameless functions need SP
1910 // altered when resuming in function.
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
1921 // Lastly, ask AddressSpace object about platform specific ways to locate
1922 // other FDEs.
1923 pint_t fde;
1924 if (_addressSpace.findOtherFDE(pc, fde)) {
1925 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1926 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1927 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1928 // Double check this FDE is for a function that includes the pc.
1929 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1930 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001931 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1932 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001933 // save off parsed FDE info
1934 _info.start_ip = fdeInfo.pcStart;
1935 _info.end_ip = fdeInfo.pcEnd;
1936 _info.lsda = fdeInfo.lsda;
1937 _info.handler = cieInfo.personality;
1938 _info.gp = prolog.spExtraArgSize;
1939 _info.flags = 0;
1940 _info.format = dwarfEncoding();
1941 _info.unwind_info = fdeInfo.fdeStart;
1942 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1943 _info.extra = 0;
1944 return;
1945 }
1946 }
1947 }
1948 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001949#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001950
1951 // no unwind info, flag that we can't reliably unwind
1952 _unwindInfoMissing = true;
1953}
1954
1955template <typename A, typename R>
1956int UnwindCursor<A, R>::step() {
1957 // Bottom of stack is defined is when unwind info cannot be found.
1958 if (_unwindInfoMissing)
1959 return UNW_STEP_END;
1960
1961 // Use unwinding info to modify register set as if function returned.
1962 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001963#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001964 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001965#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1966 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001967#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001968 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001969#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001970 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001971#else
1972 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001973 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001974 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001975 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001976#endif
1977
1978 // update info based on new PC
1979 if (result == UNW_STEP_SUCCESS) {
1980 this->setInfoBasedOnIPRegister(true);
1981 if (_unwindInfoMissing)
1982 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001983 }
1984
1985 return result;
1986}
1987
1988template <typename A, typename R>
1989void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1990 *info = _info;
1991}
1992
1993template <typename A, typename R>
1994bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1995 unw_word_t *offset) {
1996 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1997 buf, bufLen, offset);
1998}
1999
2000} // namespace libunwind
2001
2002#endif // __UNWINDCURSOR_HPP__