blob: e6a36764fc793ef3ef6c91add785aa9ed8922e85 [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)
Ryan Prichard1ae2b942020-08-18 02:31:38 -0700928 bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
929 const typename CFI_Parser<A>::CIE_Info &cieInfo,
930 pint_t pc, uintptr_t dso_base);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000931 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
932 uint32_t fdeSectionOffsetHint=0);
933 int stepWithDwarfFDE() {
934 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
935 (pint_t)this->getReg(UNW_REG_IP),
936 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -0700937 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000938 }
939#endif
940
Ranjeet Singh421231a2017-03-31 15:28:06 +0000941#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000942 bool getInfoFromCompactEncodingSection(pint_t pc,
943 const UnwindInfoSections &sects);
944 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000945 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000946 if ( compactSaysUseDwarf() )
947 return stepWithDwarfFDE();
948 #endif
949 R dummy;
950 return stepWithCompactEncoding(dummy);
951 }
952
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000953#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000954 int stepWithCompactEncoding(Registers_x86_64 &) {
955 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
956 _info.format, _info.start_ip, _addressSpace, _registers);
957 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000958#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000959
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000960#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000961 int stepWithCompactEncoding(Registers_x86 &) {
962 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
963 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
964 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000965#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000966
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000967#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000968 int stepWithCompactEncoding(Registers_ppc &) {
969 return UNW_EINVAL;
970 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000971#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000972
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000973#if defined(_LIBUNWIND_TARGET_PPC64)
974 int stepWithCompactEncoding(Registers_ppc64 &) {
975 return UNW_EINVAL;
976 }
977#endif
978
979
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000980#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000981 int stepWithCompactEncoding(Registers_arm64 &) {
982 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
983 _info.format, _info.start_ip, _addressSpace, _registers);
984 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000985#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000986
John Baldwin56441d42017-12-12 21:43:36 +0000987#if defined(_LIBUNWIND_TARGET_MIPS_O32)
988 int stepWithCompactEncoding(Registers_mips_o32 &) {
989 return UNW_EINVAL;
990 }
991#endif
992
John Baldwin541a4352018-01-09 17:07:18 +0000993#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
994 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000995 return UNW_EINVAL;
996 }
997#endif
998
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000999#if defined(_LIBUNWIND_TARGET_SPARC)
1000 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1001#endif
1002
Sam Elliott81f7e172019-12-16 16:35:17 +00001003#if defined (_LIBUNWIND_TARGET_RISCV)
1004 int stepWithCompactEncoding(Registers_riscv &) {
1005 return UNW_EINVAL;
1006 }
1007#endif
1008
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001009 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1010 R dummy;
1011 return compactSaysUseDwarf(dummy, offset);
1012 }
1013
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001014#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001015 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1016 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1017 if (offset)
1018 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1019 return true;
1020 }
1021 return false;
1022 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001023#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001024
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001025#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001026 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1027 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1028 if (offset)
1029 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1030 return true;
1031 }
1032 return false;
1033 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001034#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001035
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001036#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001037 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1038 return true;
1039 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001040#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001041
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001042#if defined(_LIBUNWIND_TARGET_PPC64)
1043 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1044 return true;
1045 }
1046#endif
1047
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001048#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001049 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1050 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1051 if (offset)
1052 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1053 return true;
1054 }
1055 return false;
1056 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001057#endif
John Baldwin56441d42017-12-12 21:43:36 +00001058
1059#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1060 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1061 return true;
1062 }
1063#endif
1064
John Baldwin541a4352018-01-09 17:07:18 +00001065#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1066 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001067 return true;
1068 }
1069#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001070
1071#if defined(_LIBUNWIND_TARGET_SPARC)
1072 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1073#endif
1074
Sam Elliott81f7e172019-12-16 16:35:17 +00001075#if defined (_LIBUNWIND_TARGET_RISCV)
1076 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1077 return true;
1078 }
1079#endif
1080
Ranjeet Singh421231a2017-03-31 15:28:06 +00001081#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001082
Ranjeet Singh421231a2017-03-31 15:28:06 +00001083#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001084 compact_unwind_encoding_t dwarfEncoding() const {
1085 R dummy;
1086 return dwarfEncoding(dummy);
1087 }
1088
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001089#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001090 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1091 return UNWIND_X86_64_MODE_DWARF;
1092 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001093#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001094
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001095#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001096 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1097 return UNWIND_X86_MODE_DWARF;
1098 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001099#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001100
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001101#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001102 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1103 return 0;
1104 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001105#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001106
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001107#if defined(_LIBUNWIND_TARGET_PPC64)
1108 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1109 return 0;
1110 }
1111#endif
1112
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001113#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001114 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1115 return UNWIND_ARM64_MODE_DWARF;
1116 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001117#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001118
Martin Storsjoa72285f2017-11-02 08:16:16 +00001119#if defined(_LIBUNWIND_TARGET_ARM)
1120 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1121 return 0;
1122 }
1123#endif
1124
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001125#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001126 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1127 return 0;
1128 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001129#endif
John Baldwin56441d42017-12-12 21:43:36 +00001130
Brian Cainb432c472020-04-09 00:14:02 -05001131#if defined (_LIBUNWIND_TARGET_HEXAGON)
1132 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1133 return 0;
1134 }
1135#endif
1136
John Baldwin56441d42017-12-12 21:43:36 +00001137#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1138 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1139 return 0;
1140 }
1141#endif
1142
John Baldwin541a4352018-01-09 17:07:18 +00001143#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1144 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001145 return 0;
1146 }
1147#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001148
1149#if defined(_LIBUNWIND_TARGET_SPARC)
1150 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1151#endif
1152
Sam Elliott81f7e172019-12-16 16:35:17 +00001153#if defined (_LIBUNWIND_TARGET_RISCV)
1154 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1155 return 0;
1156 }
1157#endif
1158
Ranjeet Singh421231a2017-03-31 15:28:06 +00001159#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001160
Charles Davisfa2e6202018-08-30 21:29:00 +00001161#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1162 // For runtime environments using SEH unwind data without Windows runtime
1163 // support.
1164 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1165 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1166 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1167 /* FIXME: Implement */
1168 *base = 0;
1169 return nullptr;
1170 }
1171 bool getInfoFromSEH(pint_t pc);
1172 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1173#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1174
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001175
1176 A &_addressSpace;
1177 R _registers;
1178 unw_proc_info_t _info;
1179 bool _unwindInfoMissing;
1180 bool _isSignalFrame;
1181};
1182
1183
1184template <typename A, typename R>
1185UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1186 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1187 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001188 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001189 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001190 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1191 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001192 memset(&_info, 0, sizeof(_info));
1193}
1194
1195template <typename A, typename R>
1196UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1197 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1198 memset(&_info, 0, sizeof(_info));
1199 // FIXME
1200 // fill in _registers from thread arg
1201}
1202
1203
1204template <typename A, typename R>
1205bool UnwindCursor<A, R>::validReg(int regNum) {
1206 return _registers.validRegister(regNum);
1207}
1208
1209template <typename A, typename R>
1210unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1211 return _registers.getRegister(regNum);
1212}
1213
1214template <typename A, typename R>
1215void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1216 _registers.setRegister(regNum, (typename A::pint_t)value);
1217}
1218
1219template <typename A, typename R>
1220bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1221 return _registers.validFloatRegister(regNum);
1222}
1223
1224template <typename A, typename R>
1225unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1226 return _registers.getFloatRegister(regNum);
1227}
1228
1229template <typename A, typename R>
1230void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1231 _registers.setFloatRegister(regNum, value);
1232}
1233
1234template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1235 _registers.jumpto();
1236}
1237
1238#ifdef __arm__
1239template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1240 _registers.saveVFPAsX();
1241}
1242#endif
1243
1244template <typename A, typename R>
1245const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1246 return _registers.getRegisterName(regNum);
1247}
1248
1249template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1250 return _isSignalFrame;
1251}
1252
Charles Davisfa2e6202018-08-30 21:29:00 +00001253#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1254
Ranjeet Singh421231a2017-03-31 15:28:06 +00001255#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001256template<typename A>
1257struct EHABISectionIterator {
1258 typedef EHABISectionIterator _Self;
1259
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001260 typedef typename A::pint_t value_type;
1261 typedef typename A::pint_t* pointer;
1262 typedef typename A::pint_t& reference;
1263 typedef size_t size_type;
1264 typedef size_t difference_type;
1265
1266 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1267 return _Self(addressSpace, sects, 0);
1268 }
1269 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001270 return _Self(addressSpace, sects,
1271 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001272 }
1273
1274 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1275 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1276
1277 _Self& operator++() { ++_i; return *this; }
1278 _Self& operator+=(size_t a) { _i += a; return *this; }
1279 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1280 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1281
1282 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1283 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1284
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001285 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001286
1287 bool operator==(const _Self& other) const {
1288 assert(_addressSpace == other._addressSpace);
1289 assert(_sects == other._sects);
1290 return _i == other._i;
1291 }
1292
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001293 bool operator!=(const _Self& other) const {
1294 assert(_addressSpace == other._addressSpace);
1295 assert(_sects == other._sects);
1296 return _i != other._i;
1297 }
1298
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001299 typename A::pint_t operator*() const { return functionAddress(); }
1300
1301 typename A::pint_t functionAddress() const {
1302 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1303 EHABIIndexEntry, _i, functionOffset);
1304 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1305 }
1306
1307 typename A::pint_t dataAddress() {
1308 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1309 EHABIIndexEntry, _i, data);
1310 return indexAddr;
1311 }
1312
1313 private:
1314 size_t _i;
1315 A* _addressSpace;
1316 const UnwindInfoSections* _sects;
1317};
1318
Petr Hosekac0d9e02019-01-29 22:26:18 +00001319namespace {
1320
1321template <typename A>
1322EHABISectionIterator<A> EHABISectionUpperBound(
1323 EHABISectionIterator<A> first,
1324 EHABISectionIterator<A> last,
1325 typename A::pint_t value) {
1326 size_t len = last - first;
1327 while (len > 0) {
1328 size_t l2 = len / 2;
1329 EHABISectionIterator<A> m = first + l2;
1330 if (value < *m) {
1331 len = l2;
1332 } else {
1333 first = ++m;
1334 len -= l2 + 1;
1335 }
1336 }
1337 return first;
1338}
1339
1340}
1341
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001342template <typename A, typename R>
1343bool UnwindCursor<A, R>::getInfoFromEHABISection(
1344 pint_t pc,
1345 const UnwindInfoSections &sects) {
1346 EHABISectionIterator<A> begin =
1347 EHABISectionIterator<A>::begin(_addressSpace, sects);
1348 EHABISectionIterator<A> end =
1349 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001350 if (begin == end)
1351 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001352
Petr Hosekac0d9e02019-01-29 22:26:18 +00001353 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001354 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001355 return false;
1356 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1357
1358 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001359 // If an exception is thrown from a function, corresponding to the last entry
1360 // in the table, we don't really know the function extent and have to choose a
1361 // value for nextPC. Choosing max() will allow the range check during trace to
1362 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001363 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001364 pint_t indexDataAddr = itThisPC.dataAddress();
1365
1366 if (indexDataAddr == 0)
1367 return false;
1368
1369 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1370 if (indexData == UNW_EXIDX_CANTUNWIND)
1371 return false;
1372
1373 // If the high bit is set, the exception handling table entry is inline inside
1374 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001375 // the table points at an offset in the exception handling table (section 5
1376 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001377 pint_t exceptionTableAddr;
1378 uint32_t exceptionTableData;
1379 bool isSingleWordEHT;
1380 if (indexData & 0x80000000) {
1381 exceptionTableAddr = indexDataAddr;
1382 // TODO(ajwong): Should this data be 0?
1383 exceptionTableData = indexData;
1384 isSingleWordEHT = true;
1385 } else {
1386 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1387 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1388 isSingleWordEHT = false;
1389 }
1390
1391 // Now we know the 3 things:
1392 // exceptionTableAddr -- exception handler table entry.
1393 // exceptionTableData -- the data inside the first word of the eht entry.
1394 // isSingleWordEHT -- whether the entry is in the index.
1395 unw_word_t personalityRoutine = 0xbadf00d;
1396 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001397 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001398
1399 // If the high bit in the exception handling table entry is set, the entry is
1400 // in compact form (section 6.3 EHABI).
1401 if (exceptionTableData & 0x80000000) {
1402 // Grab the index of the personality routine from the compact form.
1403 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1404 uint32_t extraWords = 0;
1405 switch (choice) {
1406 case 0:
1407 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1408 extraWords = 0;
1409 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001410 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001411 break;
1412 case 1:
1413 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1414 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1415 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001416 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001417 break;
1418 case 2:
1419 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1420 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1421 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001422 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001423 break;
1424 default:
1425 _LIBUNWIND_ABORT("unknown personality routine");
1426 return false;
1427 }
1428
1429 if (isSingleWordEHT) {
1430 if (extraWords != 0) {
1431 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1432 "requires extra words");
1433 return false;
1434 }
1435 }
1436 } else {
1437 pint_t personalityAddr =
1438 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1439 personalityRoutine = personalityAddr;
1440
1441 // ARM EHABI # 6.2, # 9.2
1442 //
1443 // +---- ehtp
1444 // v
1445 // +--------------------------------------+
1446 // | +--------+--------+--------+-------+ |
1447 // | |0| prel31 to personalityRoutine | |
1448 // | +--------+--------+--------+-------+ |
1449 // | | N | unwind opcodes | | <-- UnwindData
1450 // | +--------+--------+--------+-------+ |
1451 // | | Word 2 unwind opcodes | |
1452 // | +--------+--------+--------+-------+ |
1453 // | ... |
1454 // | +--------+--------+--------+-------+ |
1455 // | | Word N unwind opcodes | |
1456 // | +--------+--------+--------+-------+ |
1457 // | | LSDA | | <-- lsda
1458 // | | ... | |
1459 // | +--------+--------+--------+-------+ |
1460 // +--------------------------------------+
1461
1462 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1463 uint32_t FirstDataWord = *UnwindData;
1464 size_t N = ((FirstDataWord >> 24) & 0xff);
1465 size_t NDataWords = N + 1;
1466 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1467 }
1468
1469 _info.start_ip = thisPC;
1470 _info.end_ip = nextPC;
1471 _info.handler = personalityRoutine;
1472 _info.unwind_info = exceptionTableAddr;
1473 _info.lsda = lsda;
1474 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001475 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001476
1477 return true;
1478}
1479#endif
1480
Ranjeet Singh421231a2017-03-31 15:28:06 +00001481#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001482template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001483bool UnwindCursor<A, R>::getInfoFromFdeCie(
1484 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1485 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1486 uintptr_t dso_base) {
1487 typename CFI_Parser<A>::PrologInfo prolog;
1488 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1489 R::getArch(), &prolog)) {
1490 // Save off parsed FDE info
1491 _info.start_ip = fdeInfo.pcStart;
1492 _info.end_ip = fdeInfo.pcEnd;
1493 _info.lsda = fdeInfo.lsda;
1494 _info.handler = cieInfo.personality;
1495 // Some frameless functions need SP altered when resuming in function, so
1496 // propagate spExtraArgSize.
1497 _info.gp = prolog.spExtraArgSize;
1498 _info.flags = 0;
1499 _info.format = dwarfEncoding();
1500 _info.unwind_info = fdeInfo.fdeStart;
1501 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1502 _info.extra = static_cast<unw_word_t>(dso_base);
1503 return true;
1504 }
1505 return false;
1506}
1507
1508template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001509bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1510 const UnwindInfoSections &sects,
1511 uint32_t fdeSectionOffsetHint) {
1512 typename CFI_Parser<A>::FDE_Info fdeInfo;
1513 typename CFI_Parser<A>::CIE_Info cieInfo;
1514 bool foundFDE = false;
1515 bool foundInCache = false;
1516 // If compact encoding table gave offset into dwarf section, go directly there
1517 if (fdeSectionOffsetHint != 0) {
1518 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1519 (uint32_t)sects.dwarf_section_length,
1520 sects.dwarf_section + fdeSectionOffsetHint,
1521 &fdeInfo, &cieInfo);
1522 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001523#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001524 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1525 foundFDE = EHHeaderParser<A>::findFDE(
1526 _addressSpace, pc, sects.dwarf_index_section,
1527 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1528 }
1529#endif
1530 if (!foundFDE) {
1531 // otherwise, search cache of previously found FDEs.
1532 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1533 if (cachedFDE != 0) {
1534 foundFDE =
1535 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1536 (uint32_t)sects.dwarf_section_length,
1537 cachedFDE, &fdeInfo, &cieInfo);
1538 foundInCache = foundFDE;
1539 }
1540 }
1541 if (!foundFDE) {
1542 // Still not found, do full scan of __eh_frame section.
1543 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1544 (uint32_t)sects.dwarf_section_length, 0,
1545 &fdeInfo, &cieInfo);
1546 }
1547 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001548 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001549 // Add to cache (to make next lookup faster) if we had no hint
1550 // and there was no index.
1551 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001552 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001553 if (sects.dwarf_index_section == 0)
1554 #endif
1555 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1556 fdeInfo.fdeStart);
1557 }
1558 return true;
1559 }
1560 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001561 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001562 return false;
1563}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001564#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001565
1566
Ranjeet Singh421231a2017-03-31 15:28:06 +00001567#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001568template <typename A, typename R>
1569bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1570 const UnwindInfoSections &sects) {
1571 const bool log = false;
1572 if (log)
1573 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1574 (uint64_t)pc, (uint64_t)sects.dso_base);
1575
1576 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1577 sects.compact_unwind_section);
1578 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1579 return false;
1580
1581 // do a binary search of top level index to find page with unwind info
1582 pint_t targetFunctionOffset = pc - sects.dso_base;
1583 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1584 sects.compact_unwind_section
1585 + sectionHeader.indexSectionOffset());
1586 uint32_t low = 0;
1587 uint32_t high = sectionHeader.indexCount();
1588 uint32_t last = high - 1;
1589 while (low < high) {
1590 uint32_t mid = (low + high) / 2;
1591 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1592 //mid, low, high, topIndex.functionOffset(mid));
1593 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1594 if ((mid == last) ||
1595 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1596 low = mid;
1597 break;
1598 } else {
1599 low = mid + 1;
1600 }
1601 } else {
1602 high = mid;
1603 }
1604 }
1605 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1606 const uint32_t firstLevelNextPageFunctionOffset =
1607 topIndex.functionOffset(low + 1);
1608 const pint_t secondLevelAddr =
1609 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1610 const pint_t lsdaArrayStartAddr =
1611 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1612 const pint_t lsdaArrayEndAddr =
1613 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1614 if (log)
1615 fprintf(stderr, "\tfirst level search for result index=%d "
1616 "to secondLevelAddr=0x%llX\n",
1617 low, (uint64_t) secondLevelAddr);
1618 // do a binary search of second level page index
1619 uint32_t encoding = 0;
1620 pint_t funcStart = 0;
1621 pint_t funcEnd = 0;
1622 pint_t lsda = 0;
1623 pint_t personality = 0;
1624 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1625 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1626 // regular page
1627 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1628 secondLevelAddr);
1629 UnwindSectionRegularArray<A> pageIndex(
1630 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1631 // binary search looks for entry with e where index[e].offset <= pc <
1632 // index[e+1].offset
1633 if (log)
1634 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1635 "regular page starting at secondLevelAddr=0x%llX\n",
1636 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1637 low = 0;
1638 high = pageHeader.entryCount();
1639 while (low < high) {
1640 uint32_t mid = (low + high) / 2;
1641 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1642 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1643 // at end of table
1644 low = mid;
1645 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1646 break;
1647 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1648 // next is too big, so we found it
1649 low = mid;
1650 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1651 break;
1652 } else {
1653 low = mid + 1;
1654 }
1655 } else {
1656 high = mid;
1657 }
1658 }
1659 encoding = pageIndex.encoding(low);
1660 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1661 if (pc < funcStart) {
1662 if (log)
1663 fprintf(
1664 stderr,
1665 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1666 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1667 return false;
1668 }
1669 if (pc > funcEnd) {
1670 if (log)
1671 fprintf(
1672 stderr,
1673 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1674 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1675 return false;
1676 }
1677 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1678 // compressed page
1679 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1680 secondLevelAddr);
1681 UnwindSectionCompressedArray<A> pageIndex(
1682 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1683 const uint32_t targetFunctionPageOffset =
1684 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1685 // binary search looks for entry with e where index[e].offset <= pc <
1686 // index[e+1].offset
1687 if (log)
1688 fprintf(stderr, "\tbinary search of compressed page starting at "
1689 "secondLevelAddr=0x%llX\n",
1690 (uint64_t) secondLevelAddr);
1691 low = 0;
1692 last = pageHeader.entryCount() - 1;
1693 high = pageHeader.entryCount();
1694 while (low < high) {
1695 uint32_t mid = (low + high) / 2;
1696 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1697 if ((mid == last) ||
1698 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1699 low = mid;
1700 break;
1701 } else {
1702 low = mid + 1;
1703 }
1704 } else {
1705 high = mid;
1706 }
1707 }
1708 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1709 + sects.dso_base;
1710 if (low < last)
1711 funcEnd =
1712 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1713 + sects.dso_base;
1714 else
1715 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1716 if (pc < funcStart) {
1717 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001718 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001719 (uint64_t) pc, (uint64_t) funcStart);
1720 return false;
1721 }
1722 if (pc > funcEnd) {
1723 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001724 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001725 (uint64_t) pc, (uint64_t) funcEnd);
1726 return false;
1727 }
1728 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1729 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1730 // encoding is in common table in section header
1731 encoding = _addressSpace.get32(
1732 sects.compact_unwind_section +
1733 sectionHeader.commonEncodingsArraySectionOffset() +
1734 encodingIndex * sizeof(uint32_t));
1735 } else {
1736 // encoding is in page specific table
1737 uint16_t pageEncodingIndex =
1738 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1739 encoding = _addressSpace.get32(secondLevelAddr +
1740 pageHeader.encodingsPageOffset() +
1741 pageEncodingIndex * sizeof(uint32_t));
1742 }
1743 } else {
1744 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001745 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001746 (uint64_t) sects.compact_unwind_section);
1747 return false;
1748 }
1749
1750 // look up LSDA, if encoding says function has one
1751 if (encoding & UNWIND_HAS_LSDA) {
1752 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1753 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1754 low = 0;
1755 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1756 sizeof(unwind_info_section_header_lsda_index_entry);
1757 // binary search looks for entry with exact match for functionOffset
1758 if (log)
1759 fprintf(stderr,
1760 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1761 funcStartOffset);
1762 while (low < high) {
1763 uint32_t mid = (low + high) / 2;
1764 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1765 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1766 break;
1767 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1768 low = mid + 1;
1769 } else {
1770 high = mid;
1771 }
1772 }
1773 if (lsda == 0) {
1774 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001775 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001776 encoding, (uint64_t) pc);
1777 return false;
1778 }
1779 }
1780
Louis Dionned3bbbc32020-08-11 15:24:21 -04001781 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001782 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1783 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1784 if (personalityIndex != 0) {
1785 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001786 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001787 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001788 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001789 encoding, personalityIndex,
1790 sectionHeader.personalityArrayCount());
1791 return false;
1792 }
1793 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1794 sects.compact_unwind_section +
1795 sectionHeader.personalityArraySectionOffset() +
1796 personalityIndex * sizeof(uint32_t));
1797 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1798 personality = _addressSpace.getP(personalityPointer);
1799 if (log)
1800 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1801 "personalityDelta=0x%08X, personality=0x%08llX\n",
1802 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1803 }
1804
1805 if (log)
1806 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1807 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1808 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1809 _info.start_ip = funcStart;
1810 _info.end_ip = funcEnd;
1811 _info.lsda = lsda;
1812 _info.handler = personality;
1813 _info.gp = 0;
1814 _info.flags = 0;
1815 _info.format = encoding;
1816 _info.unwind_info = 0;
1817 _info.unwind_info_size = 0;
1818 _info.extra = sects.dso_base;
1819 return true;
1820}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001821#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001822
1823
Charles Davisfa2e6202018-08-30 21:29:00 +00001824#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1825template <typename A, typename R>
1826bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1827 pint_t base;
1828 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1829 if (!unwindEntry) {
1830 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1831 return false;
1832 }
1833 _info.gp = 0;
1834 _info.flags = 0;
1835 _info.format = 0;
1836 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1837 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1838 _info.extra = base;
1839 _info.start_ip = base + unwindEntry->BeginAddress;
1840#ifdef _LIBUNWIND_TARGET_X86_64
1841 _info.end_ip = base + unwindEntry->EndAddress;
1842 // Only fill in the handler and LSDA if they're stale.
1843 if (pc != getLastPC()) {
1844 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1845 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1846 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1847 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1848 // these structures.)
1849 // N.B. UNWIND_INFO structs are DWORD-aligned.
1850 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1851 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1852 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1853 if (*handler) {
1854 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1855 } else
1856 _info.handler = 0;
1857 } else {
1858 _info.lsda = 0;
1859 _info.handler = 0;
1860 }
1861 }
1862#elif defined(_LIBUNWIND_TARGET_ARM)
1863 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1864 _info.lsda = 0; // FIXME
1865 _info.handler = 0; // FIXME
1866#endif
1867 setLastPC(pc);
1868 return true;
1869}
1870#endif
1871
1872
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001873template <typename A, typename R>
1874void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1875 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001876#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001877 // Remove the thumb bit so the IP represents the actual instruction address.
1878 // This matches the behaviour of _Unwind_GetIP on arm.
1879 pc &= (pint_t)~0x1;
1880#endif
1881
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001882 // Exit early if at the top of the stack.
1883 if (pc == 0) {
1884 _unwindInfoMissing = true;
1885 return;
1886 }
1887
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001888 // If the last line of a function is a "throw" the compiler sometimes
1889 // emits no instructions after the call to __cxa_throw. This means
1890 // the return address is actually the start of the next function.
1891 // To disambiguate this, back up the pc when we know it is a return
1892 // address.
1893 if (isReturnAddress)
1894 --pc;
1895
1896 // Ask address space object to find unwind sections for this pc.
1897 UnwindInfoSections sects;
1898 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001899#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001900 // If there is a compact unwind encoding table, look there first.
1901 if (sects.compact_unwind_section != 0) {
1902 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001903 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001904 // Found info in table, done unless encoding says to use dwarf.
1905 uint32_t dwarfOffset;
1906 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1907 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1908 // found info in dwarf, done
1909 return;
1910 }
1911 }
1912 #endif
1913 // If unwind table has entry, but entry says there is no unwind info,
1914 // record that we have no unwind info.
1915 if (_info.format == 0)
1916 _unwindInfoMissing = true;
1917 return;
1918 }
1919 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001920#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001921
Charles Davisfa2e6202018-08-30 21:29:00 +00001922#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1923 // If there is SEH unwind info, look there next.
1924 if (this->getInfoFromSEH(pc))
1925 return;
1926#endif
1927
Ranjeet Singh421231a2017-03-31 15:28:06 +00001928#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001929 // If there is dwarf unwind info, look there next.
1930 if (sects.dwarf_section != 0) {
1931 if (this->getInfoFromDwarfSection(pc, sects)) {
1932 // found info in dwarf, done
1933 return;
1934 }
1935 }
1936#endif
1937
Ranjeet Singh421231a2017-03-31 15:28:06 +00001938#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001939 // If there is ARM EHABI unwind info, look there next.
1940 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1941 return;
1942#endif
1943 }
1944
Ranjeet Singh421231a2017-03-31 15:28:06 +00001945#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001946 // There is no static unwind info for this pc. Look to see if an FDE was
1947 // dynamically registered for it.
1948 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1949 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001950 typename CFI_Parser<A>::FDE_Info fdeInfo;
1951 typename CFI_Parser<A>::CIE_Info cieInfo;
1952 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
1953 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001954 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001955 }
1956
1957 // Lastly, ask AddressSpace object about platform specific ways to locate
1958 // other FDEs.
1959 pint_t fde;
1960 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001961 typename CFI_Parser<A>::FDE_Info fdeInfo;
1962 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001963 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1964 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001965 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
1966 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001967 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001968 }
1969 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001970#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001971
1972 // no unwind info, flag that we can't reliably unwind
1973 _unwindInfoMissing = true;
1974}
1975
1976template <typename A, typename R>
1977int UnwindCursor<A, R>::step() {
1978 // Bottom of stack is defined is when unwind info cannot be found.
1979 if (_unwindInfoMissing)
1980 return UNW_STEP_END;
1981
1982 // Use unwinding info to modify register set as if function returned.
1983 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001984#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001985 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001986#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1987 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001988#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001989 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001990#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001991 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001992#else
1993 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001994 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001995 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001996 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001997#endif
1998
1999 // update info based on new PC
2000 if (result == UNW_STEP_SUCCESS) {
2001 this->setInfoBasedOnIPRegister(true);
2002 if (_unwindInfoMissing)
2003 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002004 }
2005
2006 return result;
2007}
2008
2009template <typename A, typename R>
2010void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002011 if (_unwindInfoMissing)
2012 memset(info, 0, sizeof(*info));
2013 else
2014 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002015}
2016
2017template <typename A, typename R>
2018bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2019 unw_word_t *offset) {
2020 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2021 buf, bufLen, offset);
2022}
2023
2024} // namespace libunwind
2025
2026#endif // __UNWINDCURSOR_HPP__