blob: 54913360ca2976134797bc4a65902d40a971e8a2 [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
Martin Storsjo5f5036e2019-02-03 22:16:53 +0000485 // libunwind does not and should not depend on C++ library which means that we
486 // need our own defition of inline placement new.
487 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
488
Charles Davisfa2e6202018-08-30 21:29:00 +0000489private:
490
491 pint_t getLastPC() const { return _dispContext.ControlPc; }
492 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
493 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
494 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
495 &_dispContext.ImageBase,
496 _dispContext.HistoryTable);
497 *base = _dispContext.ImageBase;
498 return _dispContext.FunctionEntry;
499 }
500 bool getInfoFromSEH(pint_t pc);
501 int stepWithSEHData() {
502 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
503 _dispContext.ImageBase,
504 _dispContext.ControlPc,
505 _dispContext.FunctionEntry,
506 _dispContext.ContextRecord,
507 &_dispContext.HandlerData,
508 &_dispContext.EstablisherFrame,
509 NULL);
510 // Update some fields of the unwind info now, since we have them.
511 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
512 if (_dispContext.LanguageHandler) {
513 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
514 } else
515 _info.handler = 0;
516 return UNW_STEP_SUCCESS;
517 }
518
519 A &_addressSpace;
520 unw_proc_info_t _info;
521 DISPATCHER_CONTEXT _dispContext;
522 CONTEXT _msContext;
523 UNWIND_HISTORY_TABLE _histTable;
524 bool _unwindInfoMissing;
525};
526
527
528template <typename A, typename R>
529UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
530 : _addressSpace(as), _unwindInfoMissing(false) {
531 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
532 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +0300533 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
534 "UnwindCursor<> requires more alignment than unw_cursor_t");
Charles Davisfa2e6202018-08-30 21:29:00 +0000535 memset(&_info, 0, sizeof(_info));
536 memset(&_histTable, 0, sizeof(_histTable));
537 _dispContext.ContextRecord = &_msContext;
538 _dispContext.HistoryTable = &_histTable;
539 // Initialize MS context from ours.
540 R r(context);
541 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
542#if defined(_LIBUNWIND_TARGET_X86_64)
543 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
544 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
545 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
546 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
547 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
548 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
549 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
550 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
551 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
552 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
553 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
554 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
555 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
556 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
557 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
558 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
559 _msContext.Rip = r.getRegister(UNW_REG_IP);
560 union {
561 v128 v;
562 M128A m;
563 } t;
564 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
565 _msContext.Xmm0 = t.m;
566 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
567 _msContext.Xmm1 = t.m;
568 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
569 _msContext.Xmm2 = t.m;
570 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
571 _msContext.Xmm3 = t.m;
572 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
573 _msContext.Xmm4 = t.m;
574 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
575 _msContext.Xmm5 = t.m;
576 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
577 _msContext.Xmm6 = t.m;
578 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
579 _msContext.Xmm7 = t.m;
580 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
581 _msContext.Xmm8 = t.m;
582 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
583 _msContext.Xmm9 = t.m;
584 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
585 _msContext.Xmm10 = t.m;
586 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
587 _msContext.Xmm11 = t.m;
588 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
589 _msContext.Xmm12 = t.m;
590 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
591 _msContext.Xmm13 = t.m;
592 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
593 _msContext.Xmm14 = t.m;
594 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
595 _msContext.Xmm15 = t.m;
596#elif defined(_LIBUNWIND_TARGET_ARM)
597 _msContext.R0 = r.getRegister(UNW_ARM_R0);
598 _msContext.R1 = r.getRegister(UNW_ARM_R1);
599 _msContext.R2 = r.getRegister(UNW_ARM_R2);
600 _msContext.R3 = r.getRegister(UNW_ARM_R3);
601 _msContext.R4 = r.getRegister(UNW_ARM_R4);
602 _msContext.R5 = r.getRegister(UNW_ARM_R5);
603 _msContext.R6 = r.getRegister(UNW_ARM_R6);
604 _msContext.R7 = r.getRegister(UNW_ARM_R7);
605 _msContext.R8 = r.getRegister(UNW_ARM_R8);
606 _msContext.R9 = r.getRegister(UNW_ARM_R9);
607 _msContext.R10 = r.getRegister(UNW_ARM_R10);
608 _msContext.R11 = r.getRegister(UNW_ARM_R11);
609 _msContext.R12 = r.getRegister(UNW_ARM_R12);
610 _msContext.Sp = r.getRegister(UNW_ARM_SP);
611 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000612 _msContext.Pc = r.getRegister(UNW_ARM_IP);
613 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000614 union {
615 uint64_t w;
616 double d;
617 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000618 d.d = r.getFloatRegister(i);
619 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000620 }
Martin Storsjoce150112018-12-18 20:05:59 +0000621#elif defined(_LIBUNWIND_TARGET_AARCH64)
622 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
623 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
624 _msContext.Sp = r.getRegister(UNW_REG_SP);
625 _msContext.Pc = r.getRegister(UNW_REG_IP);
626 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
627 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000628#endif
629}
630
631template <typename A, typename R>
632UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
633 : _addressSpace(as), _unwindInfoMissing(false) {
634 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
635 "UnwindCursor<> does not fit in unw_cursor_t");
636 memset(&_info, 0, sizeof(_info));
637 memset(&_histTable, 0, sizeof(_histTable));
638 _dispContext.ContextRecord = &_msContext;
639 _dispContext.HistoryTable = &_histTable;
640 _msContext = *context;
641}
642
643
644template <typename A, typename R>
645bool UnwindCursor<A, R>::validReg(int regNum) {
646 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
647#if defined(_LIBUNWIND_TARGET_X86_64)
648 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
649#elif defined(_LIBUNWIND_TARGET_ARM)
650 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000651#elif defined(_LIBUNWIND_TARGET_AARCH64)
652 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000653#endif
654 return false;
655}
656
657template <typename A, typename R>
658unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
659 switch (regNum) {
660#if defined(_LIBUNWIND_TARGET_X86_64)
661 case UNW_REG_IP: return _msContext.Rip;
662 case UNW_X86_64_RAX: return _msContext.Rax;
663 case UNW_X86_64_RDX: return _msContext.Rdx;
664 case UNW_X86_64_RCX: return _msContext.Rcx;
665 case UNW_X86_64_RBX: return _msContext.Rbx;
666 case UNW_REG_SP:
667 case UNW_X86_64_RSP: return _msContext.Rsp;
668 case UNW_X86_64_RBP: return _msContext.Rbp;
669 case UNW_X86_64_RSI: return _msContext.Rsi;
670 case UNW_X86_64_RDI: return _msContext.Rdi;
671 case UNW_X86_64_R8: return _msContext.R8;
672 case UNW_X86_64_R9: return _msContext.R9;
673 case UNW_X86_64_R10: return _msContext.R10;
674 case UNW_X86_64_R11: return _msContext.R11;
675 case UNW_X86_64_R12: return _msContext.R12;
676 case UNW_X86_64_R13: return _msContext.R13;
677 case UNW_X86_64_R14: return _msContext.R14;
678 case UNW_X86_64_R15: return _msContext.R15;
679#elif defined(_LIBUNWIND_TARGET_ARM)
680 case UNW_ARM_R0: return _msContext.R0;
681 case UNW_ARM_R1: return _msContext.R1;
682 case UNW_ARM_R2: return _msContext.R2;
683 case UNW_ARM_R3: return _msContext.R3;
684 case UNW_ARM_R4: return _msContext.R4;
685 case UNW_ARM_R5: return _msContext.R5;
686 case UNW_ARM_R6: return _msContext.R6;
687 case UNW_ARM_R7: return _msContext.R7;
688 case UNW_ARM_R8: return _msContext.R8;
689 case UNW_ARM_R9: return _msContext.R9;
690 case UNW_ARM_R10: return _msContext.R10;
691 case UNW_ARM_R11: return _msContext.R11;
692 case UNW_ARM_R12: return _msContext.R12;
693 case UNW_REG_SP:
694 case UNW_ARM_SP: return _msContext.Sp;
695 case UNW_ARM_LR: return _msContext.Lr;
696 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000697 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000698#elif defined(_LIBUNWIND_TARGET_AARCH64)
699 case UNW_REG_SP: return _msContext.Sp;
700 case UNW_REG_IP: return _msContext.Pc;
701 default: return _msContext.X[regNum - UNW_ARM64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000702#endif
703 }
704 _LIBUNWIND_ABORT("unsupported register");
705}
706
707template <typename A, typename R>
708void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
709 switch (regNum) {
710#if defined(_LIBUNWIND_TARGET_X86_64)
711 case UNW_REG_IP: _msContext.Rip = value; break;
712 case UNW_X86_64_RAX: _msContext.Rax = value; break;
713 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
714 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
715 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
716 case UNW_REG_SP:
717 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
718 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
719 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
720 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
721 case UNW_X86_64_R8: _msContext.R8 = value; break;
722 case UNW_X86_64_R9: _msContext.R9 = value; break;
723 case UNW_X86_64_R10: _msContext.R10 = value; break;
724 case UNW_X86_64_R11: _msContext.R11 = value; break;
725 case UNW_X86_64_R12: _msContext.R12 = value; break;
726 case UNW_X86_64_R13: _msContext.R13 = value; break;
727 case UNW_X86_64_R14: _msContext.R14 = value; break;
728 case UNW_X86_64_R15: _msContext.R15 = value; break;
729#elif defined(_LIBUNWIND_TARGET_ARM)
730 case UNW_ARM_R0: _msContext.R0 = value; break;
731 case UNW_ARM_R1: _msContext.R1 = value; break;
732 case UNW_ARM_R2: _msContext.R2 = value; break;
733 case UNW_ARM_R3: _msContext.R3 = value; break;
734 case UNW_ARM_R4: _msContext.R4 = value; break;
735 case UNW_ARM_R5: _msContext.R5 = value; break;
736 case UNW_ARM_R6: _msContext.R6 = value; break;
737 case UNW_ARM_R7: _msContext.R7 = value; break;
738 case UNW_ARM_R8: _msContext.R8 = value; break;
739 case UNW_ARM_R9: _msContext.R9 = value; break;
740 case UNW_ARM_R10: _msContext.R10 = value; break;
741 case UNW_ARM_R11: _msContext.R11 = value; break;
742 case UNW_ARM_R12: _msContext.R12 = value; break;
743 case UNW_REG_SP:
744 case UNW_ARM_SP: _msContext.Sp = value; break;
745 case UNW_ARM_LR: _msContext.Lr = value; break;
746 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000747 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000748#elif defined(_LIBUNWIND_TARGET_AARCH64)
749 case UNW_REG_SP: _msContext.Sp = value; break;
750 case UNW_REG_IP: _msContext.Pc = value; break;
751 case UNW_ARM64_X0:
752 case UNW_ARM64_X1:
753 case UNW_ARM64_X2:
754 case UNW_ARM64_X3:
755 case UNW_ARM64_X4:
756 case UNW_ARM64_X5:
757 case UNW_ARM64_X6:
758 case UNW_ARM64_X7:
759 case UNW_ARM64_X8:
760 case UNW_ARM64_X9:
761 case UNW_ARM64_X10:
762 case UNW_ARM64_X11:
763 case UNW_ARM64_X12:
764 case UNW_ARM64_X13:
765 case UNW_ARM64_X14:
766 case UNW_ARM64_X15:
767 case UNW_ARM64_X16:
768 case UNW_ARM64_X17:
769 case UNW_ARM64_X18:
770 case UNW_ARM64_X19:
771 case UNW_ARM64_X20:
772 case UNW_ARM64_X21:
773 case UNW_ARM64_X22:
774 case UNW_ARM64_X23:
775 case UNW_ARM64_X24:
776 case UNW_ARM64_X25:
777 case UNW_ARM64_X26:
778 case UNW_ARM64_X27:
779 case UNW_ARM64_X28:
780 case UNW_ARM64_FP:
781 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000782#endif
783 default:
784 _LIBUNWIND_ABORT("unsupported register");
785 }
786}
787
788template <typename A, typename R>
789bool UnwindCursor<A, R>::validFloatReg(int regNum) {
790#if defined(_LIBUNWIND_TARGET_ARM)
791 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
792 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000793#elif defined(_LIBUNWIND_TARGET_AARCH64)
794 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000795#else
796 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000797#endif
798 return false;
799}
800
801template <typename A, typename R>
802unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
803#if defined(_LIBUNWIND_TARGET_ARM)
804 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
805 union {
806 uint32_t w;
807 float f;
808 } d;
809 d.w = _msContext.S[regNum - UNW_ARM_S0];
810 return d.f;
811 }
812 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
813 union {
814 uint64_t w;
815 double d;
816 } d;
817 d.w = _msContext.D[regNum - UNW_ARM_D0];
818 return d.d;
819 }
820 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000821#elif defined(_LIBUNWIND_TARGET_AARCH64)
822 return _msContext.V[regNum - UNW_ARM64_D0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000823#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000824 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000825 _LIBUNWIND_ABORT("float registers unimplemented");
826#endif
827}
828
829template <typename A, typename R>
830void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
831#if defined(_LIBUNWIND_TARGET_ARM)
832 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
833 union {
834 uint32_t w;
835 float f;
836 } d;
837 d.f = value;
838 _msContext.S[regNum - UNW_ARM_S0] = d.w;
839 }
840 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
841 union {
842 uint64_t w;
843 double d;
844 } d;
845 d.d = value;
846 _msContext.D[regNum - UNW_ARM_D0] = d.w;
847 }
848 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000849#elif defined(_LIBUNWIND_TARGET_AARCH64)
850 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000851#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000852 (void)regNum;
853 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000854 _LIBUNWIND_ABORT("float registers unimplemented");
855#endif
856}
857
858template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
859 RtlRestoreContext(&_msContext, nullptr);
860}
861
862#ifdef __arm__
863template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
864#endif
865
866template <typename A, typename R>
867const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000868 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000869}
870
871template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
872 return false;
873}
874
875#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
876
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000877/// UnwindCursor contains all state (including all register values) during
878/// an unwind. This is normally stack allocated inside a unw_cursor_t.
879template <typename A, typename R>
880class UnwindCursor : public AbstractUnwindCursor{
881 typedef typename A::pint_t pint_t;
882public:
883 UnwindCursor(unw_context_t *context, A &as);
884 UnwindCursor(A &as, void *threadArg);
885 virtual ~UnwindCursor() {}
886 virtual bool validReg(int);
887 virtual unw_word_t getReg(int);
888 virtual void setReg(int, unw_word_t);
889 virtual bool validFloatReg(int);
890 virtual unw_fpreg_t getFloatReg(int);
891 virtual void setFloatReg(int, unw_fpreg_t);
892 virtual int step();
893 virtual void getInfo(unw_proc_info_t *);
894 virtual void jumpto();
895 virtual bool isSignalFrame();
896 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
897 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
898 virtual const char *getRegisterName(int num);
899#ifdef __arm__
900 virtual void saveVFPAsX();
901#endif
902
Petr Hosek36f61542019-02-02 21:15:49 +0000903 // libunwind does not and should not depend on C++ library which means that we
904 // need our own defition of inline placement new.
905 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
906
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000907private:
908
Ranjeet Singh421231a2017-03-31 15:28:06 +0000909#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000910 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000911
912 int stepWithEHABI() {
913 size_t len = 0;
914 size_t off = 0;
915 // FIXME: Calling decode_eht_entry() here is violating the libunwind
916 // abstraction layer.
917 const uint32_t *ehtp =
918 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
919 &off, &len);
920 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
921 _URC_CONTINUE_UNWIND)
922 return UNW_STEP_END;
923 return UNW_STEP_SUCCESS;
924 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000925#endif
926
Ranjeet Singh421231a2017-03-31 15:28:06 +0000927#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000928 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
929 uint32_t fdeSectionOffsetHint=0);
930 int stepWithDwarfFDE() {
931 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
932 (pint_t)this->getReg(UNW_REG_IP),
933 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -0700934 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000935 }
936#endif
937
Ranjeet Singh421231a2017-03-31 15:28:06 +0000938#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000939 bool getInfoFromCompactEncodingSection(pint_t pc,
940 const UnwindInfoSections &sects);
941 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000942 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000943 if ( compactSaysUseDwarf() )
944 return stepWithDwarfFDE();
945 #endif
946 R dummy;
947 return stepWithCompactEncoding(dummy);
948 }
949
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000950#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000951 int stepWithCompactEncoding(Registers_x86_64 &) {
952 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
953 _info.format, _info.start_ip, _addressSpace, _registers);
954 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000955#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000956
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000957#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000958 int stepWithCompactEncoding(Registers_x86 &) {
959 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
960 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
961 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000962#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000963
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000964#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000965 int stepWithCompactEncoding(Registers_ppc &) {
966 return UNW_EINVAL;
967 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000968#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000969
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000970#if defined(_LIBUNWIND_TARGET_PPC64)
971 int stepWithCompactEncoding(Registers_ppc64 &) {
972 return UNW_EINVAL;
973 }
974#endif
975
976
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000977#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000978 int stepWithCompactEncoding(Registers_arm64 &) {
979 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
980 _info.format, _info.start_ip, _addressSpace, _registers);
981 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000982#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000983
John Baldwin56441d42017-12-12 21:43:36 +0000984#if defined(_LIBUNWIND_TARGET_MIPS_O32)
985 int stepWithCompactEncoding(Registers_mips_o32 &) {
986 return UNW_EINVAL;
987 }
988#endif
989
John Baldwin541a4352018-01-09 17:07:18 +0000990#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
991 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000992 return UNW_EINVAL;
993 }
994#endif
995
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000996#if defined(_LIBUNWIND_TARGET_SPARC)
997 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
998#endif
999
Sam Elliott81f7e172019-12-16 16:35:17 +00001000#if defined (_LIBUNWIND_TARGET_RISCV)
1001 int stepWithCompactEncoding(Registers_riscv &) {
1002 return UNW_EINVAL;
1003 }
1004#endif
1005
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001006 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1007 R dummy;
1008 return compactSaysUseDwarf(dummy, offset);
1009 }
1010
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001011#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001012 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1013 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1014 if (offset)
1015 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1016 return true;
1017 }
1018 return false;
1019 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001020#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001021
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001022#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001023 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1024 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1025 if (offset)
1026 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1027 return true;
1028 }
1029 return false;
1030 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001031#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001032
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001033#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001034 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1035 return true;
1036 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001037#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001038
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001039#if defined(_LIBUNWIND_TARGET_PPC64)
1040 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1041 return true;
1042 }
1043#endif
1044
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001045#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001046 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1047 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1048 if (offset)
1049 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1050 return true;
1051 }
1052 return false;
1053 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001054#endif
John Baldwin56441d42017-12-12 21:43:36 +00001055
1056#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1057 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1058 return true;
1059 }
1060#endif
1061
John Baldwin541a4352018-01-09 17:07:18 +00001062#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1063 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001064 return true;
1065 }
1066#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001067
1068#if defined(_LIBUNWIND_TARGET_SPARC)
1069 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1070#endif
1071
Sam Elliott81f7e172019-12-16 16:35:17 +00001072#if defined (_LIBUNWIND_TARGET_RISCV)
1073 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1074 return true;
1075 }
1076#endif
1077
Ranjeet Singh421231a2017-03-31 15:28:06 +00001078#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001079
Ranjeet Singh421231a2017-03-31 15:28:06 +00001080#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001081 compact_unwind_encoding_t dwarfEncoding() const {
1082 R dummy;
1083 return dwarfEncoding(dummy);
1084 }
1085
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001086#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001087 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1088 return UNWIND_X86_64_MODE_DWARF;
1089 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001090#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001091
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001092#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001093 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1094 return UNWIND_X86_MODE_DWARF;
1095 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001096#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001097
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001098#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001099 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1100 return 0;
1101 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001102#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001103
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001104#if defined(_LIBUNWIND_TARGET_PPC64)
1105 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1106 return 0;
1107 }
1108#endif
1109
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001110#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001111 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1112 return UNWIND_ARM64_MODE_DWARF;
1113 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001114#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001115
Martin Storsjoa72285f2017-11-02 08:16:16 +00001116#if defined(_LIBUNWIND_TARGET_ARM)
1117 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1118 return 0;
1119 }
1120#endif
1121
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001122#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001123 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1124 return 0;
1125 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001126#endif
John Baldwin56441d42017-12-12 21:43:36 +00001127
Brian Cainb432c472020-04-09 00:14:02 -05001128#if defined (_LIBUNWIND_TARGET_HEXAGON)
1129 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1130 return 0;
1131 }
1132#endif
1133
John Baldwin56441d42017-12-12 21:43:36 +00001134#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1135 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1136 return 0;
1137 }
1138#endif
1139
John Baldwin541a4352018-01-09 17:07:18 +00001140#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1141 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001142 return 0;
1143 }
1144#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001145
1146#if defined(_LIBUNWIND_TARGET_SPARC)
1147 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1148#endif
1149
Sam Elliott81f7e172019-12-16 16:35:17 +00001150#if defined (_LIBUNWIND_TARGET_RISCV)
1151 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1152 return 0;
1153 }
1154#endif
1155
Ranjeet Singh421231a2017-03-31 15:28:06 +00001156#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001157
Charles Davisfa2e6202018-08-30 21:29:00 +00001158#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1159 // For runtime environments using SEH unwind data without Windows runtime
1160 // support.
1161 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1162 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1163 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1164 /* FIXME: Implement */
1165 *base = 0;
1166 return nullptr;
1167 }
1168 bool getInfoFromSEH(pint_t pc);
1169 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1170#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1171
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001172
1173 A &_addressSpace;
1174 R _registers;
1175 unw_proc_info_t _info;
1176 bool _unwindInfoMissing;
1177 bool _isSignalFrame;
1178};
1179
1180
1181template <typename A, typename R>
1182UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1183 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1184 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001185 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001186 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001187 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1188 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001189 memset(&_info, 0, sizeof(_info));
1190}
1191
1192template <typename A, typename R>
1193UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1194 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1195 memset(&_info, 0, sizeof(_info));
1196 // FIXME
1197 // fill in _registers from thread arg
1198}
1199
1200
1201template <typename A, typename R>
1202bool UnwindCursor<A, R>::validReg(int regNum) {
1203 return _registers.validRegister(regNum);
1204}
1205
1206template <typename A, typename R>
1207unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1208 return _registers.getRegister(regNum);
1209}
1210
1211template <typename A, typename R>
1212void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1213 _registers.setRegister(regNum, (typename A::pint_t)value);
1214}
1215
1216template <typename A, typename R>
1217bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1218 return _registers.validFloatRegister(regNum);
1219}
1220
1221template <typename A, typename R>
1222unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1223 return _registers.getFloatRegister(regNum);
1224}
1225
1226template <typename A, typename R>
1227void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1228 _registers.setFloatRegister(regNum, value);
1229}
1230
1231template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1232 _registers.jumpto();
1233}
1234
1235#ifdef __arm__
1236template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1237 _registers.saveVFPAsX();
1238}
1239#endif
1240
1241template <typename A, typename R>
1242const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1243 return _registers.getRegisterName(regNum);
1244}
1245
1246template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1247 return _isSignalFrame;
1248}
1249
Charles Davisfa2e6202018-08-30 21:29:00 +00001250#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1251
Ranjeet Singh421231a2017-03-31 15:28:06 +00001252#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001253template<typename A>
1254struct EHABISectionIterator {
1255 typedef EHABISectionIterator _Self;
1256
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001257 typedef typename A::pint_t value_type;
1258 typedef typename A::pint_t* pointer;
1259 typedef typename A::pint_t& reference;
1260 typedef size_t size_type;
1261 typedef size_t difference_type;
1262
1263 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1264 return _Self(addressSpace, sects, 0);
1265 }
1266 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001267 return _Self(addressSpace, sects,
1268 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001269 }
1270
1271 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1272 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1273
1274 _Self& operator++() { ++_i; return *this; }
1275 _Self& operator+=(size_t a) { _i += a; return *this; }
1276 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1277 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1278
1279 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1280 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1281
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001282 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001283
1284 bool operator==(const _Self& other) const {
1285 assert(_addressSpace == other._addressSpace);
1286 assert(_sects == other._sects);
1287 return _i == other._i;
1288 }
1289
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001290 bool operator!=(const _Self& other) const {
1291 assert(_addressSpace == other._addressSpace);
1292 assert(_sects == other._sects);
1293 return _i != other._i;
1294 }
1295
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001296 typename A::pint_t operator*() const { return functionAddress(); }
1297
1298 typename A::pint_t functionAddress() const {
1299 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1300 EHABIIndexEntry, _i, functionOffset);
1301 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1302 }
1303
1304 typename A::pint_t dataAddress() {
1305 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1306 EHABIIndexEntry, _i, data);
1307 return indexAddr;
1308 }
1309
1310 private:
1311 size_t _i;
1312 A* _addressSpace;
1313 const UnwindInfoSections* _sects;
1314};
1315
Petr Hosekac0d9e02019-01-29 22:26:18 +00001316namespace {
1317
1318template <typename A>
1319EHABISectionIterator<A> EHABISectionUpperBound(
1320 EHABISectionIterator<A> first,
1321 EHABISectionIterator<A> last,
1322 typename A::pint_t value) {
1323 size_t len = last - first;
1324 while (len > 0) {
1325 size_t l2 = len / 2;
1326 EHABISectionIterator<A> m = first + l2;
1327 if (value < *m) {
1328 len = l2;
1329 } else {
1330 first = ++m;
1331 len -= l2 + 1;
1332 }
1333 }
1334 return first;
1335}
1336
1337}
1338
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001339template <typename A, typename R>
1340bool UnwindCursor<A, R>::getInfoFromEHABISection(
1341 pint_t pc,
1342 const UnwindInfoSections &sects) {
1343 EHABISectionIterator<A> begin =
1344 EHABISectionIterator<A>::begin(_addressSpace, sects);
1345 EHABISectionIterator<A> end =
1346 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001347 if (begin == end)
1348 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001349
Petr Hosekac0d9e02019-01-29 22:26:18 +00001350 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001351 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001352 return false;
1353 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1354
1355 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001356 // If an exception is thrown from a function, corresponding to the last entry
1357 // in the table, we don't really know the function extent and have to choose a
1358 // value for nextPC. Choosing max() will allow the range check during trace to
1359 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001360 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001361 pint_t indexDataAddr = itThisPC.dataAddress();
1362
1363 if (indexDataAddr == 0)
1364 return false;
1365
1366 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1367 if (indexData == UNW_EXIDX_CANTUNWIND)
1368 return false;
1369
1370 // If the high bit is set, the exception handling table entry is inline inside
1371 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001372 // the table points at an offset in the exception handling table (section 5
1373 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001374 pint_t exceptionTableAddr;
1375 uint32_t exceptionTableData;
1376 bool isSingleWordEHT;
1377 if (indexData & 0x80000000) {
1378 exceptionTableAddr = indexDataAddr;
1379 // TODO(ajwong): Should this data be 0?
1380 exceptionTableData = indexData;
1381 isSingleWordEHT = true;
1382 } else {
1383 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1384 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1385 isSingleWordEHT = false;
1386 }
1387
1388 // Now we know the 3 things:
1389 // exceptionTableAddr -- exception handler table entry.
1390 // exceptionTableData -- the data inside the first word of the eht entry.
1391 // isSingleWordEHT -- whether the entry is in the index.
1392 unw_word_t personalityRoutine = 0xbadf00d;
1393 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001394 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001395
1396 // If the high bit in the exception handling table entry is set, the entry is
1397 // in compact form (section 6.3 EHABI).
1398 if (exceptionTableData & 0x80000000) {
1399 // Grab the index of the personality routine from the compact form.
1400 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1401 uint32_t extraWords = 0;
1402 switch (choice) {
1403 case 0:
1404 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1405 extraWords = 0;
1406 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001407 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001408 break;
1409 case 1:
1410 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1411 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1412 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001413 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001414 break;
1415 case 2:
1416 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1417 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1418 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001419 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001420 break;
1421 default:
1422 _LIBUNWIND_ABORT("unknown personality routine");
1423 return false;
1424 }
1425
1426 if (isSingleWordEHT) {
1427 if (extraWords != 0) {
1428 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1429 "requires extra words");
1430 return false;
1431 }
1432 }
1433 } else {
1434 pint_t personalityAddr =
1435 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1436 personalityRoutine = personalityAddr;
1437
1438 // ARM EHABI # 6.2, # 9.2
1439 //
1440 // +---- ehtp
1441 // v
1442 // +--------------------------------------+
1443 // | +--------+--------+--------+-------+ |
1444 // | |0| prel31 to personalityRoutine | |
1445 // | +--------+--------+--------+-------+ |
1446 // | | N | unwind opcodes | | <-- UnwindData
1447 // | +--------+--------+--------+-------+ |
1448 // | | Word 2 unwind opcodes | |
1449 // | +--------+--------+--------+-------+ |
1450 // | ... |
1451 // | +--------+--------+--------+-------+ |
1452 // | | Word N unwind opcodes | |
1453 // | +--------+--------+--------+-------+ |
1454 // | | LSDA | | <-- lsda
1455 // | | ... | |
1456 // | +--------+--------+--------+-------+ |
1457 // +--------------------------------------+
1458
1459 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1460 uint32_t FirstDataWord = *UnwindData;
1461 size_t N = ((FirstDataWord >> 24) & 0xff);
1462 size_t NDataWords = N + 1;
1463 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1464 }
1465
1466 _info.start_ip = thisPC;
1467 _info.end_ip = nextPC;
1468 _info.handler = personalityRoutine;
1469 _info.unwind_info = exceptionTableAddr;
1470 _info.lsda = lsda;
1471 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001472 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001473
1474 return true;
1475}
1476#endif
1477
Ranjeet Singh421231a2017-03-31 15:28:06 +00001478#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001479template <typename A, typename R>
1480bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1481 const UnwindInfoSections &sects,
1482 uint32_t fdeSectionOffsetHint) {
1483 typename CFI_Parser<A>::FDE_Info fdeInfo;
1484 typename CFI_Parser<A>::CIE_Info cieInfo;
1485 bool foundFDE = false;
1486 bool foundInCache = false;
1487 // If compact encoding table gave offset into dwarf section, go directly there
1488 if (fdeSectionOffsetHint != 0) {
1489 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1490 (uint32_t)sects.dwarf_section_length,
1491 sects.dwarf_section + fdeSectionOffsetHint,
1492 &fdeInfo, &cieInfo);
1493 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001494#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001495 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1496 foundFDE = EHHeaderParser<A>::findFDE(
1497 _addressSpace, pc, sects.dwarf_index_section,
1498 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1499 }
1500#endif
1501 if (!foundFDE) {
1502 // otherwise, search cache of previously found FDEs.
1503 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1504 if (cachedFDE != 0) {
1505 foundFDE =
1506 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1507 (uint32_t)sects.dwarf_section_length,
1508 cachedFDE, &fdeInfo, &cieInfo);
1509 foundInCache = foundFDE;
1510 }
1511 }
1512 if (!foundFDE) {
1513 // Still not found, do full scan of __eh_frame section.
1514 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1515 (uint32_t)sects.dwarf_section_length, 0,
1516 &fdeInfo, &cieInfo);
1517 }
1518 if (foundFDE) {
1519 typename CFI_Parser<A>::PrologInfo prolog;
1520 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001521 R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001522 // Save off parsed FDE info
1523 _info.start_ip = fdeInfo.pcStart;
1524 _info.end_ip = fdeInfo.pcEnd;
1525 _info.lsda = fdeInfo.lsda;
1526 _info.handler = cieInfo.personality;
1527 _info.gp = prolog.spExtraArgSize;
1528 _info.flags = 0;
1529 _info.format = dwarfEncoding();
1530 _info.unwind_info = fdeInfo.fdeStart;
1531 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1532 _info.extra = (unw_word_t) sects.dso_base;
1533
1534 // Add to cache (to make next lookup faster) if we had no hint
1535 // and there was no index.
1536 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001537 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001538 if (sects.dwarf_index_section == 0)
1539 #endif
1540 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1541 fdeInfo.fdeStart);
1542 }
1543 return true;
1544 }
1545 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001546 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001547 return false;
1548}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001549#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001550
1551
Ranjeet Singh421231a2017-03-31 15:28:06 +00001552#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001553template <typename A, typename R>
1554bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1555 const UnwindInfoSections &sects) {
1556 const bool log = false;
1557 if (log)
1558 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1559 (uint64_t)pc, (uint64_t)sects.dso_base);
1560
1561 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1562 sects.compact_unwind_section);
1563 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1564 return false;
1565
1566 // do a binary search of top level index to find page with unwind info
1567 pint_t targetFunctionOffset = pc - sects.dso_base;
1568 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1569 sects.compact_unwind_section
1570 + sectionHeader.indexSectionOffset());
1571 uint32_t low = 0;
1572 uint32_t high = sectionHeader.indexCount();
1573 uint32_t last = high - 1;
1574 while (low < high) {
1575 uint32_t mid = (low + high) / 2;
1576 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1577 //mid, low, high, topIndex.functionOffset(mid));
1578 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1579 if ((mid == last) ||
1580 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1581 low = mid;
1582 break;
1583 } else {
1584 low = mid + 1;
1585 }
1586 } else {
1587 high = mid;
1588 }
1589 }
1590 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1591 const uint32_t firstLevelNextPageFunctionOffset =
1592 topIndex.functionOffset(low + 1);
1593 const pint_t secondLevelAddr =
1594 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1595 const pint_t lsdaArrayStartAddr =
1596 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1597 const pint_t lsdaArrayEndAddr =
1598 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1599 if (log)
1600 fprintf(stderr, "\tfirst level search for result index=%d "
1601 "to secondLevelAddr=0x%llX\n",
1602 low, (uint64_t) secondLevelAddr);
1603 // do a binary search of second level page index
1604 uint32_t encoding = 0;
1605 pint_t funcStart = 0;
1606 pint_t funcEnd = 0;
1607 pint_t lsda = 0;
1608 pint_t personality = 0;
1609 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1610 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1611 // regular page
1612 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1613 secondLevelAddr);
1614 UnwindSectionRegularArray<A> pageIndex(
1615 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1616 // binary search looks for entry with e where index[e].offset <= pc <
1617 // index[e+1].offset
1618 if (log)
1619 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1620 "regular page starting at secondLevelAddr=0x%llX\n",
1621 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1622 low = 0;
1623 high = pageHeader.entryCount();
1624 while (low < high) {
1625 uint32_t mid = (low + high) / 2;
1626 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1627 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1628 // at end of table
1629 low = mid;
1630 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1631 break;
1632 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1633 // next is too big, so we found it
1634 low = mid;
1635 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1636 break;
1637 } else {
1638 low = mid + 1;
1639 }
1640 } else {
1641 high = mid;
1642 }
1643 }
1644 encoding = pageIndex.encoding(low);
1645 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1646 if (pc < funcStart) {
1647 if (log)
1648 fprintf(
1649 stderr,
1650 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1651 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1652 return false;
1653 }
1654 if (pc > funcEnd) {
1655 if (log)
1656 fprintf(
1657 stderr,
1658 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1659 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1660 return false;
1661 }
1662 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1663 // compressed page
1664 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1665 secondLevelAddr);
1666 UnwindSectionCompressedArray<A> pageIndex(
1667 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1668 const uint32_t targetFunctionPageOffset =
1669 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1670 // binary search looks for entry with e where index[e].offset <= pc <
1671 // index[e+1].offset
1672 if (log)
1673 fprintf(stderr, "\tbinary search of compressed page starting at "
1674 "secondLevelAddr=0x%llX\n",
1675 (uint64_t) secondLevelAddr);
1676 low = 0;
1677 last = pageHeader.entryCount() - 1;
1678 high = pageHeader.entryCount();
1679 while (low < high) {
1680 uint32_t mid = (low + high) / 2;
1681 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1682 if ((mid == last) ||
1683 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1684 low = mid;
1685 break;
1686 } else {
1687 low = mid + 1;
1688 }
1689 } else {
1690 high = mid;
1691 }
1692 }
1693 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1694 + sects.dso_base;
1695 if (low < last)
1696 funcEnd =
1697 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1698 + sects.dso_base;
1699 else
1700 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1701 if (pc < funcStart) {
1702 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001703 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001704 (uint64_t) pc, (uint64_t) funcStart);
1705 return false;
1706 }
1707 if (pc > funcEnd) {
1708 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001709 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001710 (uint64_t) pc, (uint64_t) funcEnd);
1711 return false;
1712 }
1713 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1714 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1715 // encoding is in common table in section header
1716 encoding = _addressSpace.get32(
1717 sects.compact_unwind_section +
1718 sectionHeader.commonEncodingsArraySectionOffset() +
1719 encodingIndex * sizeof(uint32_t));
1720 } else {
1721 // encoding is in page specific table
1722 uint16_t pageEncodingIndex =
1723 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1724 encoding = _addressSpace.get32(secondLevelAddr +
1725 pageHeader.encodingsPageOffset() +
1726 pageEncodingIndex * sizeof(uint32_t));
1727 }
1728 } else {
1729 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001730 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001731 (uint64_t) sects.compact_unwind_section);
1732 return false;
1733 }
1734
1735 // look up LSDA, if encoding says function has one
1736 if (encoding & UNWIND_HAS_LSDA) {
1737 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1738 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1739 low = 0;
1740 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1741 sizeof(unwind_info_section_header_lsda_index_entry);
1742 // binary search looks for entry with exact match for functionOffset
1743 if (log)
1744 fprintf(stderr,
1745 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1746 funcStartOffset);
1747 while (low < high) {
1748 uint32_t mid = (low + high) / 2;
1749 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1750 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1751 break;
1752 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1753 low = mid + 1;
1754 } else {
1755 high = mid;
1756 }
1757 }
1758 if (lsda == 0) {
1759 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001760 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001761 encoding, (uint64_t) pc);
1762 return false;
1763 }
1764 }
1765
Louis Dionned3bbbc32020-08-11 15:24:21 -04001766 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001767 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1768 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1769 if (personalityIndex != 0) {
1770 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001771 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001772 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001773 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001774 encoding, personalityIndex,
1775 sectionHeader.personalityArrayCount());
1776 return false;
1777 }
1778 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1779 sects.compact_unwind_section +
1780 sectionHeader.personalityArraySectionOffset() +
1781 personalityIndex * sizeof(uint32_t));
1782 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1783 personality = _addressSpace.getP(personalityPointer);
1784 if (log)
1785 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1786 "personalityDelta=0x%08X, personality=0x%08llX\n",
1787 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1788 }
1789
1790 if (log)
1791 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1792 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1793 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1794 _info.start_ip = funcStart;
1795 _info.end_ip = funcEnd;
1796 _info.lsda = lsda;
1797 _info.handler = personality;
1798 _info.gp = 0;
1799 _info.flags = 0;
1800 _info.format = encoding;
1801 _info.unwind_info = 0;
1802 _info.unwind_info_size = 0;
1803 _info.extra = sects.dso_base;
1804 return true;
1805}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001806#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001807
1808
Charles Davisfa2e6202018-08-30 21:29:00 +00001809#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1810template <typename A, typename R>
1811bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1812 pint_t base;
1813 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1814 if (!unwindEntry) {
1815 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1816 return false;
1817 }
1818 _info.gp = 0;
1819 _info.flags = 0;
1820 _info.format = 0;
1821 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1822 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1823 _info.extra = base;
1824 _info.start_ip = base + unwindEntry->BeginAddress;
1825#ifdef _LIBUNWIND_TARGET_X86_64
1826 _info.end_ip = base + unwindEntry->EndAddress;
1827 // Only fill in the handler and LSDA if they're stale.
1828 if (pc != getLastPC()) {
1829 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1830 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1831 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1832 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1833 // these structures.)
1834 // N.B. UNWIND_INFO structs are DWORD-aligned.
1835 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1836 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1837 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1838 if (*handler) {
1839 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1840 } else
1841 _info.handler = 0;
1842 } else {
1843 _info.lsda = 0;
1844 _info.handler = 0;
1845 }
1846 }
1847#elif defined(_LIBUNWIND_TARGET_ARM)
1848 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1849 _info.lsda = 0; // FIXME
1850 _info.handler = 0; // FIXME
1851#endif
1852 setLastPC(pc);
1853 return true;
1854}
1855#endif
1856
1857
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001858template <typename A, typename R>
1859void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1860 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001861#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001862 // Remove the thumb bit so the IP represents the actual instruction address.
1863 // This matches the behaviour of _Unwind_GetIP on arm.
1864 pc &= (pint_t)~0x1;
1865#endif
1866
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001867 // Exit early if at the top of the stack.
1868 if (pc == 0) {
1869 _unwindInfoMissing = true;
1870 return;
1871 }
1872
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001873 // If the last line of a function is a "throw" the compiler sometimes
1874 // emits no instructions after the call to __cxa_throw. This means
1875 // the return address is actually the start of the next function.
1876 // To disambiguate this, back up the pc when we know it is a return
1877 // address.
1878 if (isReturnAddress)
1879 --pc;
1880
1881 // Ask address space object to find unwind sections for this pc.
1882 UnwindInfoSections sects;
1883 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001884#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001885 // If there is a compact unwind encoding table, look there first.
1886 if (sects.compact_unwind_section != 0) {
1887 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001888 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001889 // Found info in table, done unless encoding says to use dwarf.
1890 uint32_t dwarfOffset;
1891 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1892 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1893 // found info in dwarf, done
1894 return;
1895 }
1896 }
1897 #endif
1898 // If unwind table has entry, but entry says there is no unwind info,
1899 // record that we have no unwind info.
1900 if (_info.format == 0)
1901 _unwindInfoMissing = true;
1902 return;
1903 }
1904 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001905#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001906
Charles Davisfa2e6202018-08-30 21:29:00 +00001907#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1908 // If there is SEH unwind info, look there next.
1909 if (this->getInfoFromSEH(pc))
1910 return;
1911#endif
1912
Ranjeet Singh421231a2017-03-31 15:28:06 +00001913#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001914 // If there is dwarf unwind info, look there next.
1915 if (sects.dwarf_section != 0) {
1916 if (this->getInfoFromDwarfSection(pc, sects)) {
1917 // found info in dwarf, done
1918 return;
1919 }
1920 }
1921#endif
1922
Ranjeet Singh421231a2017-03-31 15:28:06 +00001923#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001924 // If there is ARM EHABI unwind info, look there next.
1925 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1926 return;
1927#endif
1928 }
1929
Ranjeet Singh421231a2017-03-31 15:28:06 +00001930#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001931 // There is no static unwind info for this pc. Look to see if an FDE was
1932 // dynamically registered for it.
1933 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1934 if (cachedFDE != 0) {
1935 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1936 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1937 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1938 cachedFDE, &fdeInfo, &cieInfo);
1939 if (msg == NULL) {
1940 typename CFI_Parser<A>::PrologInfo prolog;
1941 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001942 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001943 // save off parsed FDE info
1944 _info.start_ip = fdeInfo.pcStart;
1945 _info.end_ip = fdeInfo.pcEnd;
1946 _info.lsda = fdeInfo.lsda;
1947 _info.handler = cieInfo.personality;
1948 _info.gp = prolog.spExtraArgSize;
1949 // Some frameless functions need SP
1950 // altered when resuming in function.
1951 _info.flags = 0;
1952 _info.format = dwarfEncoding();
1953 _info.unwind_info = fdeInfo.fdeStart;
1954 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1955 _info.extra = 0;
1956 return;
1957 }
1958 }
1959 }
1960
1961 // Lastly, ask AddressSpace object about platform specific ways to locate
1962 // other FDEs.
1963 pint_t fde;
1964 if (_addressSpace.findOtherFDE(pc, fde)) {
1965 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1966 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1967 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1968 // Double check this FDE is for a function that includes the pc.
1969 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1970 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001971 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1972 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001973 // save off parsed FDE info
1974 _info.start_ip = fdeInfo.pcStart;
1975 _info.end_ip = fdeInfo.pcEnd;
1976 _info.lsda = fdeInfo.lsda;
1977 _info.handler = cieInfo.personality;
1978 _info.gp = prolog.spExtraArgSize;
1979 _info.flags = 0;
1980 _info.format = dwarfEncoding();
1981 _info.unwind_info = fdeInfo.fdeStart;
1982 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1983 _info.extra = 0;
1984 return;
1985 }
1986 }
1987 }
1988 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001989#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001990
1991 // no unwind info, flag that we can't reliably unwind
1992 _unwindInfoMissing = true;
1993}
1994
1995template <typename A, typename R>
1996int UnwindCursor<A, R>::step() {
1997 // Bottom of stack is defined is when unwind info cannot be found.
1998 if (_unwindInfoMissing)
1999 return UNW_STEP_END;
2000
2001 // Use unwinding info to modify register set as if function returned.
2002 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00002003#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002004 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00002005#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
2006 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002007#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002008 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002009#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00002010 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002011#else
2012 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00002013 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002014 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00002015 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002016#endif
2017
2018 // update info based on new PC
2019 if (result == UNW_STEP_SUCCESS) {
2020 this->setInfoBasedOnIPRegister(true);
2021 if (_unwindInfoMissing)
2022 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002023 }
2024
2025 return result;
2026}
2027
2028template <typename A, typename R>
2029void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002030 if (_unwindInfoMissing)
2031 memset(info, 0, sizeof(*info));
2032 else
2033 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002034}
2035
2036template <typename A, typename R>
2037bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2038 unw_word_t *offset) {
2039 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2040 buf, bufLen, offset);
2041}
2042
2043} // namespace libunwind
2044
2045#endif // __UNWINDCURSOR_HPP__