blob: c400fdf33d8a9d2ff589546476559babf31ece4d [file] [log] [blame]
Louis Dionne7f068e52021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
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
gejin7f493162021-08-26 16:20:38 +080014#include "cet_unwind.h"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000015#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000018#include <unwind.h>
19
Charles Davisfa2e6202018-08-30 21:29:00 +000020#ifdef _WIN32
21 #include <windows.h>
22 #include <ntverp.h>
23#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000024#ifdef __APPLE__
25 #include <mach-o/dyld.h>
26#endif
27
Charles Davisfa2e6202018-08-30 21:29:00 +000028#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
29// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
30// earlier) SDKs.
31// MinGW-w64 has always provided this struct.
32 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
33 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
34struct _DISPATCHER_CONTEXT {
35 ULONG64 ControlPc;
36 ULONG64 ImageBase;
37 PRUNTIME_FUNCTION FunctionEntry;
38 ULONG64 EstablisherFrame;
39 ULONG64 TargetIp;
40 PCONTEXT ContextRecord;
41 PEXCEPTION_ROUTINE LanguageHandler;
42 PVOID HandlerData;
43 PUNWIND_HISTORY_TABLE HistoryTable;
44 ULONG ScopeIndex;
45 ULONG Fill0;
46};
47 #endif
48
49struct UNWIND_INFO {
50 uint8_t Version : 3;
51 uint8_t Flags : 5;
52 uint8_t SizeOfProlog;
53 uint8_t CountOfCodes;
54 uint8_t FrameRegister : 4;
55 uint8_t FrameOffset : 4;
56 uint16_t UnwindCodes[2];
57};
58
59extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
60 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
61 struct _Unwind_Context *);
62
63#endif
64
Saleem Abdulrasool17552662015-04-24 19:39:17 +000065#include "config.h"
66
67#include "AddressSpace.hpp"
68#include "CompactUnwinder.hpp"
69#include "config.h"
70#include "DwarfInstructions.hpp"
71#include "EHHeaderParser.hpp"
72#include "libunwind.h"
73#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000074#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000075#include "Unwind-EHABI.h"
76
77namespace libunwind {
78
Ranjeet Singh421231a2017-03-31 15:28:06 +000079#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000080/// Cache of recently found FDEs.
81template <typename A>
82class _LIBUNWIND_HIDDEN DwarfFDECache {
83 typedef typename A::pint_t pint_t;
84public:
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -070085 static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000086 static pint_t findFDE(pint_t mh, pint_t pc);
87 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
88 static void removeAllIn(pint_t mh);
89 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
90 unw_word_t ip_end,
91 unw_word_t fde, unw_word_t mh));
92
93private:
94
95 struct entry {
96 pint_t mh;
97 pint_t ip_start;
98 pint_t ip_end;
99 pint_t fde;
100 };
101
102 // These fields are all static to avoid needing an initializer.
103 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000104 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105#ifdef __APPLE__
106 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
107 static bool _registeredForDyldUnloads;
108#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000109 static entry *_buffer;
110 static entry *_bufferUsed;
111 static entry *_bufferEnd;
112 static entry _initialBuffer[64];
113};
114
115template <typename A>
116typename DwarfFDECache<A>::entry *
117DwarfFDECache<A>::_buffer = _initialBuffer;
118
119template <typename A>
120typename DwarfFDECache<A>::entry *
121DwarfFDECache<A>::_bufferUsed = _initialBuffer;
122
123template <typename A>
124typename DwarfFDECache<A>::entry *
125DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
126
127template <typename A>
128typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
129
130template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000131RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000132
133#ifdef __APPLE__
134template <typename A>
135bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
136#endif
137
138template <typename A>
139typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
140 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000141 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000142 for (entry *p = _buffer; p < _bufferUsed; ++p) {
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -0700143 if ((mh == p->mh) || (mh == kSearchAll)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
145 result = p->fde;
146 break;
147 }
148 }
149 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000150 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000151 return result;
152}
153
154template <typename A>
155void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
156 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000157#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000158 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000159 if (_bufferUsed >= _bufferEnd) {
160 size_t oldSize = (size_t)(_bufferEnd - _buffer);
161 size_t newSize = oldSize * 4;
162 // Can't use operator new (we are below it).
163 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
164 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
165 if (_buffer != _initialBuffer)
166 free(_buffer);
167 _buffer = newBuffer;
168 _bufferUsed = &newBuffer[oldSize];
169 _bufferEnd = &newBuffer[newSize];
170 }
171 _bufferUsed->mh = mh;
172 _bufferUsed->ip_start = ip_start;
173 _bufferUsed->ip_end = ip_end;
174 _bufferUsed->fde = fde;
175 ++_bufferUsed;
176#ifdef __APPLE__
177 if (!_registeredForDyldUnloads) {
178 _dyld_register_func_for_remove_image(&dyldUnloadHook);
179 _registeredForDyldUnloads = true;
180 }
181#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000182 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000183#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000184}
185
186template <typename A>
187void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000188 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000189 entry *d = _buffer;
190 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
191 if (s->mh != mh) {
192 if (d != s)
193 *d = *s;
194 ++d;
195 }
196 }
197 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000198 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199}
200
201#ifdef __APPLE__
202template <typename A>
203void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
204 removeAllIn((pint_t) mh);
205}
206#endif
207
208template <typename A>
209void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
210 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000211 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000212 for (entry *p = _buffer; p < _bufferUsed; ++p) {
213 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
214 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000215 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000217#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000218
219
220#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
221
Ranjeet Singh421231a2017-03-31 15:28:06 +0000222#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000223template <typename A> class UnwindSectionHeader {
224public:
225 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
226 : _addressSpace(addressSpace), _addr(addr) {}
227
228 uint32_t version() const {
229 return _addressSpace.get32(_addr +
230 offsetof(unwind_info_section_header, version));
231 }
232 uint32_t commonEncodingsArraySectionOffset() const {
233 return _addressSpace.get32(_addr +
234 offsetof(unwind_info_section_header,
235 commonEncodingsArraySectionOffset));
236 }
237 uint32_t commonEncodingsArrayCount() const {
238 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
239 commonEncodingsArrayCount));
240 }
241 uint32_t personalityArraySectionOffset() const {
242 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
243 personalityArraySectionOffset));
244 }
245 uint32_t personalityArrayCount() const {
246 return _addressSpace.get32(
247 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
248 }
249 uint32_t indexSectionOffset() const {
250 return _addressSpace.get32(
251 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
252 }
253 uint32_t indexCount() const {
254 return _addressSpace.get32(
255 _addr + offsetof(unwind_info_section_header, indexCount));
256 }
257
258private:
259 A &_addressSpace;
260 typename A::pint_t _addr;
261};
262
263template <typename A> class UnwindSectionIndexArray {
264public:
265 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
266 : _addressSpace(addressSpace), _addr(addr) {}
267
268 uint32_t functionOffset(uint32_t index) const {
269 return _addressSpace.get32(
270 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
271 functionOffset));
272 }
273 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
274 return _addressSpace.get32(
275 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
276 secondLevelPagesSectionOffset));
277 }
278 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
279 return _addressSpace.get32(
280 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
281 lsdaIndexArraySectionOffset));
282 }
283
284private:
285 A &_addressSpace;
286 typename A::pint_t _addr;
287};
288
289template <typename A> class UnwindSectionRegularPageHeader {
290public:
291 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
292 : _addressSpace(addressSpace), _addr(addr) {}
293
294 uint32_t kind() const {
295 return _addressSpace.get32(
296 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
297 }
298 uint16_t entryPageOffset() const {
299 return _addressSpace.get16(
300 _addr + offsetof(unwind_info_regular_second_level_page_header,
301 entryPageOffset));
302 }
303 uint16_t entryCount() const {
304 return _addressSpace.get16(
305 _addr +
306 offsetof(unwind_info_regular_second_level_page_header, entryCount));
307 }
308
309private:
310 A &_addressSpace;
311 typename A::pint_t _addr;
312};
313
314template <typename A> class UnwindSectionRegularArray {
315public:
316 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
317 : _addressSpace(addressSpace), _addr(addr) {}
318
319 uint32_t functionOffset(uint32_t index) const {
320 return _addressSpace.get32(
321 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
322 functionOffset));
323 }
324 uint32_t encoding(uint32_t index) const {
325 return _addressSpace.get32(
326 _addr +
327 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
328 }
329
330private:
331 A &_addressSpace;
332 typename A::pint_t _addr;
333};
334
335template <typename A> class UnwindSectionCompressedPageHeader {
336public:
337 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
338 : _addressSpace(addressSpace), _addr(addr) {}
339
340 uint32_t kind() const {
341 return _addressSpace.get32(
342 _addr +
343 offsetof(unwind_info_compressed_second_level_page_header, kind));
344 }
345 uint16_t entryPageOffset() const {
346 return _addressSpace.get16(
347 _addr + offsetof(unwind_info_compressed_second_level_page_header,
348 entryPageOffset));
349 }
350 uint16_t entryCount() const {
351 return _addressSpace.get16(
352 _addr +
353 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
354 }
355 uint16_t encodingsPageOffset() const {
356 return _addressSpace.get16(
357 _addr + offsetof(unwind_info_compressed_second_level_page_header,
358 encodingsPageOffset));
359 }
360 uint16_t encodingsCount() const {
361 return _addressSpace.get16(
362 _addr + offsetof(unwind_info_compressed_second_level_page_header,
363 encodingsCount));
364 }
365
366private:
367 A &_addressSpace;
368 typename A::pint_t _addr;
369};
370
371template <typename A> class UnwindSectionCompressedArray {
372public:
373 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
374 : _addressSpace(addressSpace), _addr(addr) {}
375
376 uint32_t functionOffset(uint32_t index) const {
377 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
378 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
379 }
380 uint16_t encodingIndex(uint32_t index) const {
381 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
382 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
383 }
384
385private:
386 A &_addressSpace;
387 typename A::pint_t _addr;
388};
389
390template <typename A> class UnwindSectionLsdaArray {
391public:
392 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
393 : _addressSpace(addressSpace), _addr(addr) {}
394
395 uint32_t functionOffset(uint32_t index) const {
396 return _addressSpace.get32(
397 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
398 index, functionOffset));
399 }
400 uint32_t lsdaOffset(uint32_t index) const {
401 return _addressSpace.get32(
402 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
403 index, lsdaOffset));
404 }
405
406private:
407 A &_addressSpace;
408 typename A::pint_t _addr;
409};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000410#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000411
412class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
413public:
414 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
415 // This avoids an unnecessary dependency to libc++abi.
416 void operator delete(void *, size_t) {}
417
418 virtual ~AbstractUnwindCursor() {}
419 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
420 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
421 virtual void setReg(int, unw_word_t) {
422 _LIBUNWIND_ABORT("setReg not implemented");
423 }
424 virtual bool validFloatReg(int) {
425 _LIBUNWIND_ABORT("validFloatReg not implemented");
426 }
427 virtual unw_fpreg_t getFloatReg(int) {
428 _LIBUNWIND_ABORT("getFloatReg not implemented");
429 }
430 virtual void setFloatReg(int, unw_fpreg_t) {
431 _LIBUNWIND_ABORT("setFloatReg not implemented");
432 }
433 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
434 virtual void getInfo(unw_proc_info_t *) {
435 _LIBUNWIND_ABORT("getInfo not implemented");
436 }
437 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
438 virtual bool isSignalFrame() {
439 _LIBUNWIND_ABORT("isSignalFrame not implemented");
440 }
441 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
442 _LIBUNWIND_ABORT("getFunctionName not implemented");
443 }
444 virtual void setInfoBasedOnIPRegister(bool = false) {
445 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
446 }
447 virtual const char *getRegisterName(int) {
448 _LIBUNWIND_ABORT("getRegisterName not implemented");
449 }
450#ifdef __arm__
451 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
452#endif
gejin7f493162021-08-26 16:20:38 +0800453
454#if defined(_LIBUNWIND_USE_CET)
455 virtual void *get_registers() {
456 _LIBUNWIND_ABORT("get_registers not implemented");
457 }
458#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000459};
460
Charles Davisfa2e6202018-08-30 21:29:00 +0000461#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
462
463/// \c UnwindCursor contains all state (including all register values) during
464/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
465template <typename A, typename R>
466class UnwindCursor : public AbstractUnwindCursor {
467 typedef typename A::pint_t pint_t;
468public:
469 UnwindCursor(unw_context_t *context, A &as);
470 UnwindCursor(CONTEXT *context, A &as);
471 UnwindCursor(A &as, void *threadArg);
472 virtual ~UnwindCursor() {}
473 virtual bool validReg(int);
474 virtual unw_word_t getReg(int);
475 virtual void setReg(int, unw_word_t);
476 virtual bool validFloatReg(int);
477 virtual unw_fpreg_t getFloatReg(int);
478 virtual void setFloatReg(int, unw_fpreg_t);
479 virtual int step();
480 virtual void getInfo(unw_proc_info_t *);
481 virtual void jumpto();
482 virtual bool isSignalFrame();
483 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
484 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
485 virtual const char *getRegisterName(int num);
486#ifdef __arm__
487 virtual void saveVFPAsX();
488#endif
489
490 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
491 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
492
Martin Storsjo5f5036e2019-02-03 22:16:53 +0000493 // libunwind does not and should not depend on C++ library which means that we
494 // need our own defition of inline placement new.
495 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
496
Charles Davisfa2e6202018-08-30 21:29:00 +0000497private:
498
499 pint_t getLastPC() const { return _dispContext.ControlPc; }
500 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
501 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
502 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
503 &_dispContext.ImageBase,
504 _dispContext.HistoryTable);
505 *base = _dispContext.ImageBase;
506 return _dispContext.FunctionEntry;
507 }
508 bool getInfoFromSEH(pint_t pc);
509 int stepWithSEHData() {
510 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
511 _dispContext.ImageBase,
512 _dispContext.ControlPc,
513 _dispContext.FunctionEntry,
514 _dispContext.ContextRecord,
515 &_dispContext.HandlerData,
516 &_dispContext.EstablisherFrame,
517 NULL);
518 // Update some fields of the unwind info now, since we have them.
519 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
520 if (_dispContext.LanguageHandler) {
521 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
522 } else
523 _info.handler = 0;
524 return UNW_STEP_SUCCESS;
525 }
526
527 A &_addressSpace;
528 unw_proc_info_t _info;
529 DISPATCHER_CONTEXT _dispContext;
530 CONTEXT _msContext;
531 UNWIND_HISTORY_TABLE _histTable;
532 bool _unwindInfoMissing;
533};
534
535
536template <typename A, typename R>
537UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
538 : _addressSpace(as), _unwindInfoMissing(false) {
539 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
540 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +0300541 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
542 "UnwindCursor<> requires more alignment than unw_cursor_t");
Charles Davisfa2e6202018-08-30 21:29:00 +0000543 memset(&_info, 0, sizeof(_info));
544 memset(&_histTable, 0, sizeof(_histTable));
545 _dispContext.ContextRecord = &_msContext;
546 _dispContext.HistoryTable = &_histTable;
547 // Initialize MS context from ours.
548 R r(context);
549 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
550#if defined(_LIBUNWIND_TARGET_X86_64)
551 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
552 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
553 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
554 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
555 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
556 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
557 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
558 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
559 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
560 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
561 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
562 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
563 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
564 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
565 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
566 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
567 _msContext.Rip = r.getRegister(UNW_REG_IP);
568 union {
569 v128 v;
570 M128A m;
571 } t;
572 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
573 _msContext.Xmm0 = t.m;
574 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
575 _msContext.Xmm1 = t.m;
576 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
577 _msContext.Xmm2 = t.m;
578 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
579 _msContext.Xmm3 = t.m;
580 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
581 _msContext.Xmm4 = t.m;
582 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
583 _msContext.Xmm5 = t.m;
584 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
585 _msContext.Xmm6 = t.m;
586 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
587 _msContext.Xmm7 = t.m;
588 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
589 _msContext.Xmm8 = t.m;
590 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
591 _msContext.Xmm9 = t.m;
592 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
593 _msContext.Xmm10 = t.m;
594 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
595 _msContext.Xmm11 = t.m;
596 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
597 _msContext.Xmm12 = t.m;
598 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
599 _msContext.Xmm13 = t.m;
600 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
601 _msContext.Xmm14 = t.m;
602 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
603 _msContext.Xmm15 = t.m;
604#elif defined(_LIBUNWIND_TARGET_ARM)
605 _msContext.R0 = r.getRegister(UNW_ARM_R0);
606 _msContext.R1 = r.getRegister(UNW_ARM_R1);
607 _msContext.R2 = r.getRegister(UNW_ARM_R2);
608 _msContext.R3 = r.getRegister(UNW_ARM_R3);
609 _msContext.R4 = r.getRegister(UNW_ARM_R4);
610 _msContext.R5 = r.getRegister(UNW_ARM_R5);
611 _msContext.R6 = r.getRegister(UNW_ARM_R6);
612 _msContext.R7 = r.getRegister(UNW_ARM_R7);
613 _msContext.R8 = r.getRegister(UNW_ARM_R8);
614 _msContext.R9 = r.getRegister(UNW_ARM_R9);
615 _msContext.R10 = r.getRegister(UNW_ARM_R10);
616 _msContext.R11 = r.getRegister(UNW_ARM_R11);
617 _msContext.R12 = r.getRegister(UNW_ARM_R12);
618 _msContext.Sp = r.getRegister(UNW_ARM_SP);
619 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000620 _msContext.Pc = r.getRegister(UNW_ARM_IP);
621 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000622 union {
623 uint64_t w;
624 double d;
625 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000626 d.d = r.getFloatRegister(i);
627 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000628 }
Martin Storsjoce150112018-12-18 20:05:59 +0000629#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700630 for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
631 _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
Martin Storsjoce150112018-12-18 20:05:59 +0000632 _msContext.Sp = r.getRegister(UNW_REG_SP);
633 _msContext.Pc = r.getRegister(UNW_REG_IP);
Fangrui Song5f263002021-08-20 14:26:27 -0700634 for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
635 _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000636#endif
637}
638
639template <typename A, typename R>
640UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
641 : _addressSpace(as), _unwindInfoMissing(false) {
642 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
643 "UnwindCursor<> does not fit in unw_cursor_t");
644 memset(&_info, 0, sizeof(_info));
645 memset(&_histTable, 0, sizeof(_histTable));
646 _dispContext.ContextRecord = &_msContext;
647 _dispContext.HistoryTable = &_histTable;
648 _msContext = *context;
649}
650
651
652template <typename A, typename R>
653bool UnwindCursor<A, R>::validReg(int regNum) {
654 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
655#if defined(_LIBUNWIND_TARGET_X86_64)
656 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
657#elif defined(_LIBUNWIND_TARGET_ARM)
Ties Stuijc8c0ec92021-12-08 09:44:45 +0000658 if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
659 regNum == UNW_ARM_RA_AUTH_CODE)
660 return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000661#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700662 if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000663#endif
664 return false;
665}
666
667template <typename A, typename R>
668unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
669 switch (regNum) {
670#if defined(_LIBUNWIND_TARGET_X86_64)
671 case UNW_REG_IP: return _msContext.Rip;
672 case UNW_X86_64_RAX: return _msContext.Rax;
673 case UNW_X86_64_RDX: return _msContext.Rdx;
674 case UNW_X86_64_RCX: return _msContext.Rcx;
675 case UNW_X86_64_RBX: return _msContext.Rbx;
676 case UNW_REG_SP:
677 case UNW_X86_64_RSP: return _msContext.Rsp;
678 case UNW_X86_64_RBP: return _msContext.Rbp;
679 case UNW_X86_64_RSI: return _msContext.Rsi;
680 case UNW_X86_64_RDI: return _msContext.Rdi;
681 case UNW_X86_64_R8: return _msContext.R8;
682 case UNW_X86_64_R9: return _msContext.R9;
683 case UNW_X86_64_R10: return _msContext.R10;
684 case UNW_X86_64_R11: return _msContext.R11;
685 case UNW_X86_64_R12: return _msContext.R12;
686 case UNW_X86_64_R13: return _msContext.R13;
687 case UNW_X86_64_R14: return _msContext.R14;
688 case UNW_X86_64_R15: return _msContext.R15;
689#elif defined(_LIBUNWIND_TARGET_ARM)
690 case UNW_ARM_R0: return _msContext.R0;
691 case UNW_ARM_R1: return _msContext.R1;
692 case UNW_ARM_R2: return _msContext.R2;
693 case UNW_ARM_R3: return _msContext.R3;
694 case UNW_ARM_R4: return _msContext.R4;
695 case UNW_ARM_R5: return _msContext.R5;
696 case UNW_ARM_R6: return _msContext.R6;
697 case UNW_ARM_R7: return _msContext.R7;
698 case UNW_ARM_R8: return _msContext.R8;
699 case UNW_ARM_R9: return _msContext.R9;
700 case UNW_ARM_R10: return _msContext.R10;
701 case UNW_ARM_R11: return _msContext.R11;
702 case UNW_ARM_R12: return _msContext.R12;
703 case UNW_REG_SP:
704 case UNW_ARM_SP: return _msContext.Sp;
705 case UNW_ARM_LR: return _msContext.Lr;
706 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000707 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000708#elif defined(_LIBUNWIND_TARGET_AARCH64)
709 case UNW_REG_SP: return _msContext.Sp;
710 case UNW_REG_IP: return _msContext.Pc;
Fangrui Song5f263002021-08-20 14:26:27 -0700711 default: return _msContext.X[regNum - UNW_AARCH64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000712#endif
713 }
714 _LIBUNWIND_ABORT("unsupported register");
715}
716
717template <typename A, typename R>
718void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
719 switch (regNum) {
720#if defined(_LIBUNWIND_TARGET_X86_64)
721 case UNW_REG_IP: _msContext.Rip = value; break;
722 case UNW_X86_64_RAX: _msContext.Rax = value; break;
723 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
724 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
725 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
726 case UNW_REG_SP:
727 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
728 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
729 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
730 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
731 case UNW_X86_64_R8: _msContext.R8 = value; break;
732 case UNW_X86_64_R9: _msContext.R9 = value; break;
733 case UNW_X86_64_R10: _msContext.R10 = value; break;
734 case UNW_X86_64_R11: _msContext.R11 = value; break;
735 case UNW_X86_64_R12: _msContext.R12 = value; break;
736 case UNW_X86_64_R13: _msContext.R13 = value; break;
737 case UNW_X86_64_R14: _msContext.R14 = value; break;
738 case UNW_X86_64_R15: _msContext.R15 = value; break;
739#elif defined(_LIBUNWIND_TARGET_ARM)
740 case UNW_ARM_R0: _msContext.R0 = value; break;
741 case UNW_ARM_R1: _msContext.R1 = value; break;
742 case UNW_ARM_R2: _msContext.R2 = value; break;
743 case UNW_ARM_R3: _msContext.R3 = value; break;
744 case UNW_ARM_R4: _msContext.R4 = value; break;
745 case UNW_ARM_R5: _msContext.R5 = value; break;
746 case UNW_ARM_R6: _msContext.R6 = value; break;
747 case UNW_ARM_R7: _msContext.R7 = value; break;
748 case UNW_ARM_R8: _msContext.R8 = value; break;
749 case UNW_ARM_R9: _msContext.R9 = value; break;
750 case UNW_ARM_R10: _msContext.R10 = value; break;
751 case UNW_ARM_R11: _msContext.R11 = value; break;
752 case UNW_ARM_R12: _msContext.R12 = value; break;
753 case UNW_REG_SP:
754 case UNW_ARM_SP: _msContext.Sp = value; break;
755 case UNW_ARM_LR: _msContext.Lr = value; break;
756 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000757 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000758#elif defined(_LIBUNWIND_TARGET_AARCH64)
759 case UNW_REG_SP: _msContext.Sp = value; break;
760 case UNW_REG_IP: _msContext.Pc = value; break;
Fangrui Song5f263002021-08-20 14:26:27 -0700761 case UNW_AARCH64_X0:
762 case UNW_AARCH64_X1:
763 case UNW_AARCH64_X2:
764 case UNW_AARCH64_X3:
765 case UNW_AARCH64_X4:
766 case UNW_AARCH64_X5:
767 case UNW_AARCH64_X6:
768 case UNW_AARCH64_X7:
769 case UNW_AARCH64_X8:
770 case UNW_AARCH64_X9:
771 case UNW_AARCH64_X10:
772 case UNW_AARCH64_X11:
773 case UNW_AARCH64_X12:
774 case UNW_AARCH64_X13:
775 case UNW_AARCH64_X14:
776 case UNW_AARCH64_X15:
777 case UNW_AARCH64_X16:
778 case UNW_AARCH64_X17:
779 case UNW_AARCH64_X18:
780 case UNW_AARCH64_X19:
781 case UNW_AARCH64_X20:
782 case UNW_AARCH64_X21:
783 case UNW_AARCH64_X22:
784 case UNW_AARCH64_X23:
785 case UNW_AARCH64_X24:
786 case UNW_AARCH64_X25:
787 case UNW_AARCH64_X26:
788 case UNW_AARCH64_X27:
789 case UNW_AARCH64_X28:
790 case UNW_AARCH64_FP:
791 case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000792#endif
793 default:
794 _LIBUNWIND_ABORT("unsupported register");
795 }
796}
797
798template <typename A, typename R>
799bool UnwindCursor<A, R>::validFloatReg(int regNum) {
800#if defined(_LIBUNWIND_TARGET_ARM)
801 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
802 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000803#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700804 if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000805#else
806 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000807#endif
808 return false;
809}
810
811template <typename A, typename R>
812unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
813#if defined(_LIBUNWIND_TARGET_ARM)
814 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
815 union {
816 uint32_t w;
817 float f;
818 } d;
819 d.w = _msContext.S[regNum - UNW_ARM_S0];
820 return d.f;
821 }
822 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
823 union {
824 uint64_t w;
825 double d;
826 } d;
827 d.w = _msContext.D[regNum - UNW_ARM_D0];
828 return d.d;
829 }
830 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000831#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700832 return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000833#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000834 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000835 _LIBUNWIND_ABORT("float registers unimplemented");
836#endif
837}
838
839template <typename A, typename R>
840void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
841#if defined(_LIBUNWIND_TARGET_ARM)
842 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
843 union {
844 uint32_t w;
845 float f;
846 } d;
847 d.f = value;
848 _msContext.S[regNum - UNW_ARM_S0] = d.w;
849 }
850 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
851 union {
852 uint64_t w;
853 double d;
854 } d;
855 d.d = value;
856 _msContext.D[regNum - UNW_ARM_D0] = d.w;
857 }
858 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000859#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700860 _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000861#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000862 (void)regNum;
863 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000864 _LIBUNWIND_ABORT("float registers unimplemented");
865#endif
866}
867
868template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
869 RtlRestoreContext(&_msContext, nullptr);
870}
871
872#ifdef __arm__
873template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
874#endif
875
876template <typename A, typename R>
877const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000878 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000879}
880
881template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
882 return false;
883}
884
885#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
886
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000887/// UnwindCursor contains all state (including all register values) during
888/// an unwind. This is normally stack allocated inside a unw_cursor_t.
889template <typename A, typename R>
890class UnwindCursor : public AbstractUnwindCursor{
891 typedef typename A::pint_t pint_t;
892public:
893 UnwindCursor(unw_context_t *context, A &as);
894 UnwindCursor(A &as, void *threadArg);
895 virtual ~UnwindCursor() {}
896 virtual bool validReg(int);
897 virtual unw_word_t getReg(int);
898 virtual void setReg(int, unw_word_t);
899 virtual bool validFloatReg(int);
900 virtual unw_fpreg_t getFloatReg(int);
901 virtual void setFloatReg(int, unw_fpreg_t);
902 virtual int step();
903 virtual void getInfo(unw_proc_info_t *);
904 virtual void jumpto();
905 virtual bool isSignalFrame();
906 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
907 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
908 virtual const char *getRegisterName(int num);
909#ifdef __arm__
910 virtual void saveVFPAsX();
911#endif
912
gejin7f493162021-08-26 16:20:38 +0800913#if defined(_LIBUNWIND_USE_CET)
914 virtual void *get_registers() { return &_registers; }
915#endif
Petr Hosek36f61542019-02-02 21:15:49 +0000916 // libunwind does not and should not depend on C++ library which means that we
917 // need our own defition of inline placement new.
918 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
919
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000920private:
921
Ranjeet Singh421231a2017-03-31 15:28:06 +0000922#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000923 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000924
925 int stepWithEHABI() {
926 size_t len = 0;
927 size_t off = 0;
928 // FIXME: Calling decode_eht_entry() here is violating the libunwind
929 // abstraction layer.
930 const uint32_t *ehtp =
931 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
932 &off, &len);
933 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
934 _URC_CONTINUE_UNWIND)
935 return UNW_STEP_END;
936 return UNW_STEP_SUCCESS;
937 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000938#endif
939
Ryan Pricharda684bed2021-01-13 16:38:36 -0800940#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
941 bool setInfoForSigReturn() {
942 R dummy;
943 return setInfoForSigReturn(dummy);
944 }
945 int stepThroughSigReturn() {
946 R dummy;
947 return stepThroughSigReturn(dummy);
948 }
949 bool setInfoForSigReturn(Registers_arm64 &);
950 int stepThroughSigReturn(Registers_arm64 &);
951 template <typename Registers> bool setInfoForSigReturn(Registers &) {
952 return false;
953 }
954 template <typename Registers> int stepThroughSigReturn(Registers &) {
955 return UNW_STEP_END;
956 }
957#endif
958
Ranjeet Singh421231a2017-03-31 15:28:06 +0000959#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Prichard1ae2b942020-08-18 02:31:38 -0700960 bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
961 const typename CFI_Parser<A>::CIE_Info &cieInfo,
962 pint_t pc, uintptr_t dso_base);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000963 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
964 uint32_t fdeSectionOffsetHint=0);
965 int stepWithDwarfFDE() {
966 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
967 (pint_t)this->getReg(UNW_REG_IP),
968 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -0700969 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000970 }
971#endif
972
Ranjeet Singh421231a2017-03-31 15:28:06 +0000973#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000974 bool getInfoFromCompactEncodingSection(pint_t pc,
975 const UnwindInfoSections &sects);
976 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000977 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000978 if ( compactSaysUseDwarf() )
979 return stepWithDwarfFDE();
980 #endif
981 R dummy;
982 return stepWithCompactEncoding(dummy);
983 }
984
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000985#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000986 int stepWithCompactEncoding(Registers_x86_64 &) {
987 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
988 _info.format, _info.start_ip, _addressSpace, _registers);
989 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000990#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000991
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000992#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000993 int stepWithCompactEncoding(Registers_x86 &) {
994 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
995 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
996 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000997#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000998
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000999#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001000 int stepWithCompactEncoding(Registers_ppc &) {
1001 return UNW_EINVAL;
1002 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001003#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001004
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001005#if defined(_LIBUNWIND_TARGET_PPC64)
1006 int stepWithCompactEncoding(Registers_ppc64 &) {
1007 return UNW_EINVAL;
1008 }
1009#endif
1010
1011
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001012#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001013 int stepWithCompactEncoding(Registers_arm64 &) {
1014 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
1015 _info.format, _info.start_ip, _addressSpace, _registers);
1016 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001017#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001018
John Baldwin56441d42017-12-12 21:43:36 +00001019#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1020 int stepWithCompactEncoding(Registers_mips_o32 &) {
1021 return UNW_EINVAL;
1022 }
1023#endif
1024
John Baldwin541a4352018-01-09 17:07:18 +00001025#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1026 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +00001027 return UNW_EINVAL;
1028 }
1029#endif
1030
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001031#if defined(_LIBUNWIND_TARGET_SPARC)
1032 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1033#endif
1034
Sam Elliott81f7e172019-12-16 16:35:17 +00001035#if defined (_LIBUNWIND_TARGET_RISCV)
1036 int stepWithCompactEncoding(Registers_riscv &) {
1037 return UNW_EINVAL;
1038 }
1039#endif
1040
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001041 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1042 R dummy;
1043 return compactSaysUseDwarf(dummy, offset);
1044 }
1045
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001046#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001047 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1048 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1049 if (offset)
1050 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1051 return true;
1052 }
1053 return false;
1054 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001055#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001056
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001057#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001058 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1059 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1060 if (offset)
1061 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1062 return true;
1063 }
1064 return false;
1065 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001066#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001067
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001068#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001069 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1070 return true;
1071 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001072#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001073
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001074#if defined(_LIBUNWIND_TARGET_PPC64)
1075 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1076 return true;
1077 }
1078#endif
1079
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001080#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001081 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1082 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1083 if (offset)
1084 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1085 return true;
1086 }
1087 return false;
1088 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001089#endif
John Baldwin56441d42017-12-12 21:43:36 +00001090
1091#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1092 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1093 return true;
1094 }
1095#endif
1096
John Baldwin541a4352018-01-09 17:07:18 +00001097#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1098 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001099 return true;
1100 }
1101#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001102
1103#if defined(_LIBUNWIND_TARGET_SPARC)
1104 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1105#endif
1106
Sam Elliott81f7e172019-12-16 16:35:17 +00001107#if defined (_LIBUNWIND_TARGET_RISCV)
1108 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1109 return true;
1110 }
1111#endif
1112
Ranjeet Singh421231a2017-03-31 15:28:06 +00001113#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001114
Ranjeet Singh421231a2017-03-31 15:28:06 +00001115#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001116 compact_unwind_encoding_t dwarfEncoding() const {
1117 R dummy;
1118 return dwarfEncoding(dummy);
1119 }
1120
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001121#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001122 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1123 return UNWIND_X86_64_MODE_DWARF;
1124 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001125#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001126
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001127#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001128 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1129 return UNWIND_X86_MODE_DWARF;
1130 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001131#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001132
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001133#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001134 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1135 return 0;
1136 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001137#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001138
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001139#if defined(_LIBUNWIND_TARGET_PPC64)
1140 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1141 return 0;
1142 }
1143#endif
1144
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001145#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001146 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1147 return UNWIND_ARM64_MODE_DWARF;
1148 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001149#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001150
Martin Storsjoa72285f2017-11-02 08:16:16 +00001151#if defined(_LIBUNWIND_TARGET_ARM)
1152 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1153 return 0;
1154 }
1155#endif
1156
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001157#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001158 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1159 return 0;
1160 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001161#endif
John Baldwin56441d42017-12-12 21:43:36 +00001162
Brian Cainb432c472020-04-09 00:14:02 -05001163#if defined (_LIBUNWIND_TARGET_HEXAGON)
1164 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1165 return 0;
1166 }
1167#endif
1168
John Baldwin56441d42017-12-12 21:43:36 +00001169#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1170 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1171 return 0;
1172 }
1173#endif
1174
John Baldwin541a4352018-01-09 17:07:18 +00001175#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1176 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001177 return 0;
1178 }
1179#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001180
1181#if defined(_LIBUNWIND_TARGET_SPARC)
1182 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1183#endif
1184
Sam Elliott81f7e172019-12-16 16:35:17 +00001185#if defined (_LIBUNWIND_TARGET_RISCV)
1186 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1187 return 0;
1188 }
1189#endif
1190
Ranjeet Singh421231a2017-03-31 15:28:06 +00001191#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001192
Charles Davisfa2e6202018-08-30 21:29:00 +00001193#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1194 // For runtime environments using SEH unwind data without Windows runtime
1195 // support.
1196 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1197 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1198 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1199 /* FIXME: Implement */
1200 *base = 0;
1201 return nullptr;
1202 }
1203 bool getInfoFromSEH(pint_t pc);
1204 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1205#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1206
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001207
1208 A &_addressSpace;
1209 R _registers;
1210 unw_proc_info_t _info;
1211 bool _unwindInfoMissing;
1212 bool _isSignalFrame;
Ryan Pricharda684bed2021-01-13 16:38:36 -08001213#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1214 bool _isSigReturn = false;
1215#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001216};
1217
1218
1219template <typename A, typename R>
1220UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1221 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1222 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001223 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001224 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001225 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1226 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001227 memset(&_info, 0, sizeof(_info));
1228}
1229
1230template <typename A, typename R>
1231UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1232 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1233 memset(&_info, 0, sizeof(_info));
1234 // FIXME
1235 // fill in _registers from thread arg
1236}
1237
1238
1239template <typename A, typename R>
1240bool UnwindCursor<A, R>::validReg(int regNum) {
1241 return _registers.validRegister(regNum);
1242}
1243
1244template <typename A, typename R>
1245unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1246 return _registers.getRegister(regNum);
1247}
1248
1249template <typename A, typename R>
1250void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1251 _registers.setRegister(regNum, (typename A::pint_t)value);
1252}
1253
1254template <typename A, typename R>
1255bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1256 return _registers.validFloatRegister(regNum);
1257}
1258
1259template <typename A, typename R>
1260unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1261 return _registers.getFloatRegister(regNum);
1262}
1263
1264template <typename A, typename R>
1265void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1266 _registers.setFloatRegister(regNum, value);
1267}
1268
1269template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1270 _registers.jumpto();
1271}
1272
1273#ifdef __arm__
1274template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1275 _registers.saveVFPAsX();
1276}
1277#endif
1278
1279template <typename A, typename R>
1280const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1281 return _registers.getRegisterName(regNum);
1282}
1283
1284template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1285 return _isSignalFrame;
1286}
1287
Charles Davisfa2e6202018-08-30 21:29:00 +00001288#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1289
Ranjeet Singh421231a2017-03-31 15:28:06 +00001290#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001291template<typename A>
1292struct EHABISectionIterator {
1293 typedef EHABISectionIterator _Self;
1294
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001295 typedef typename A::pint_t value_type;
1296 typedef typename A::pint_t* pointer;
1297 typedef typename A::pint_t& reference;
1298 typedef size_t size_type;
1299 typedef size_t difference_type;
1300
1301 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1302 return _Self(addressSpace, sects, 0);
1303 }
1304 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001305 return _Self(addressSpace, sects,
1306 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001307 }
1308
1309 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1310 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1311
1312 _Self& operator++() { ++_i; return *this; }
1313 _Self& operator+=(size_t a) { _i += a; return *this; }
1314 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1315 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1316
1317 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1318 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1319
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001320 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001321
1322 bool operator==(const _Self& other) const {
1323 assert(_addressSpace == other._addressSpace);
1324 assert(_sects == other._sects);
1325 return _i == other._i;
1326 }
1327
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001328 bool operator!=(const _Self& other) const {
1329 assert(_addressSpace == other._addressSpace);
1330 assert(_sects == other._sects);
1331 return _i != other._i;
1332 }
1333
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001334 typename A::pint_t operator*() const { return functionAddress(); }
1335
1336 typename A::pint_t functionAddress() const {
1337 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1338 EHABIIndexEntry, _i, functionOffset);
1339 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1340 }
1341
1342 typename A::pint_t dataAddress() {
1343 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1344 EHABIIndexEntry, _i, data);
1345 return indexAddr;
1346 }
1347
1348 private:
1349 size_t _i;
1350 A* _addressSpace;
1351 const UnwindInfoSections* _sects;
1352};
1353
Petr Hosekac0d9e02019-01-29 22:26:18 +00001354namespace {
1355
1356template <typename A>
1357EHABISectionIterator<A> EHABISectionUpperBound(
1358 EHABISectionIterator<A> first,
1359 EHABISectionIterator<A> last,
1360 typename A::pint_t value) {
1361 size_t len = last - first;
1362 while (len > 0) {
1363 size_t l2 = len / 2;
1364 EHABISectionIterator<A> m = first + l2;
1365 if (value < *m) {
1366 len = l2;
1367 } else {
1368 first = ++m;
1369 len -= l2 + 1;
1370 }
1371 }
1372 return first;
1373}
1374
1375}
1376
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001377template <typename A, typename R>
1378bool UnwindCursor<A, R>::getInfoFromEHABISection(
1379 pint_t pc,
1380 const UnwindInfoSections &sects) {
1381 EHABISectionIterator<A> begin =
1382 EHABISectionIterator<A>::begin(_addressSpace, sects);
1383 EHABISectionIterator<A> end =
1384 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001385 if (begin == end)
1386 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001387
Petr Hosekac0d9e02019-01-29 22:26:18 +00001388 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001389 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001390 return false;
1391 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1392
1393 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001394 // If an exception is thrown from a function, corresponding to the last entry
1395 // in the table, we don't really know the function extent and have to choose a
1396 // value for nextPC. Choosing max() will allow the range check during trace to
1397 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001398 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001399 pint_t indexDataAddr = itThisPC.dataAddress();
1400
1401 if (indexDataAddr == 0)
1402 return false;
1403
1404 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1405 if (indexData == UNW_EXIDX_CANTUNWIND)
1406 return false;
1407
1408 // If the high bit is set, the exception handling table entry is inline inside
1409 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001410 // the table points at an offset in the exception handling table (section 5
1411 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001412 pint_t exceptionTableAddr;
1413 uint32_t exceptionTableData;
1414 bool isSingleWordEHT;
1415 if (indexData & 0x80000000) {
1416 exceptionTableAddr = indexDataAddr;
1417 // TODO(ajwong): Should this data be 0?
1418 exceptionTableData = indexData;
1419 isSingleWordEHT = true;
1420 } else {
1421 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1422 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1423 isSingleWordEHT = false;
1424 }
1425
1426 // Now we know the 3 things:
1427 // exceptionTableAddr -- exception handler table entry.
1428 // exceptionTableData -- the data inside the first word of the eht entry.
1429 // isSingleWordEHT -- whether the entry is in the index.
1430 unw_word_t personalityRoutine = 0xbadf00d;
1431 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001432 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001433
1434 // If the high bit in the exception handling table entry is set, the entry is
1435 // in compact form (section 6.3 EHABI).
1436 if (exceptionTableData & 0x80000000) {
1437 // Grab the index of the personality routine from the compact form.
1438 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1439 uint32_t extraWords = 0;
1440 switch (choice) {
1441 case 0:
1442 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1443 extraWords = 0;
1444 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001445 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001446 break;
1447 case 1:
1448 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1449 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1450 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001451 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001452 break;
1453 case 2:
1454 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1455 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1456 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001457 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001458 break;
1459 default:
1460 _LIBUNWIND_ABORT("unknown personality routine");
1461 return false;
1462 }
1463
1464 if (isSingleWordEHT) {
1465 if (extraWords != 0) {
1466 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1467 "requires extra words");
1468 return false;
1469 }
1470 }
1471 } else {
1472 pint_t personalityAddr =
1473 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1474 personalityRoutine = personalityAddr;
1475
1476 // ARM EHABI # 6.2, # 9.2
1477 //
1478 // +---- ehtp
1479 // v
1480 // +--------------------------------------+
1481 // | +--------+--------+--------+-------+ |
1482 // | |0| prel31 to personalityRoutine | |
1483 // | +--------+--------+--------+-------+ |
1484 // | | N | unwind opcodes | | <-- UnwindData
1485 // | +--------+--------+--------+-------+ |
1486 // | | Word 2 unwind opcodes | |
1487 // | +--------+--------+--------+-------+ |
1488 // | ... |
1489 // | +--------+--------+--------+-------+ |
1490 // | | Word N unwind opcodes | |
1491 // | +--------+--------+--------+-------+ |
1492 // | | LSDA | | <-- lsda
1493 // | | ... | |
1494 // | +--------+--------+--------+-------+ |
1495 // +--------------------------------------+
1496
1497 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1498 uint32_t FirstDataWord = *UnwindData;
1499 size_t N = ((FirstDataWord >> 24) & 0xff);
1500 size_t NDataWords = N + 1;
1501 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1502 }
1503
1504 _info.start_ip = thisPC;
1505 _info.end_ip = nextPC;
1506 _info.handler = personalityRoutine;
1507 _info.unwind_info = exceptionTableAddr;
1508 _info.lsda = lsda;
1509 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001510 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001511
1512 return true;
1513}
1514#endif
1515
Ranjeet Singh421231a2017-03-31 15:28:06 +00001516#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001517template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001518bool UnwindCursor<A, R>::getInfoFromFdeCie(
1519 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1520 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1521 uintptr_t dso_base) {
1522 typename CFI_Parser<A>::PrologInfo prolog;
1523 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1524 R::getArch(), &prolog)) {
1525 // Save off parsed FDE info
1526 _info.start_ip = fdeInfo.pcStart;
1527 _info.end_ip = fdeInfo.pcEnd;
1528 _info.lsda = fdeInfo.lsda;
1529 _info.handler = cieInfo.personality;
1530 // Some frameless functions need SP altered when resuming in function, so
1531 // propagate spExtraArgSize.
1532 _info.gp = prolog.spExtraArgSize;
1533 _info.flags = 0;
1534 _info.format = dwarfEncoding();
1535 _info.unwind_info = fdeInfo.fdeStart;
1536 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1537 _info.extra = static_cast<unw_word_t>(dso_base);
1538 return true;
1539 }
1540 return false;
1541}
1542
1543template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001544bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1545 const UnwindInfoSections &sects,
1546 uint32_t fdeSectionOffsetHint) {
1547 typename CFI_Parser<A>::FDE_Info fdeInfo;
1548 typename CFI_Parser<A>::CIE_Info cieInfo;
1549 bool foundFDE = false;
1550 bool foundInCache = false;
1551 // If compact encoding table gave offset into dwarf section, go directly there
1552 if (fdeSectionOffsetHint != 0) {
1553 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001554 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001555 sects.dwarf_section + fdeSectionOffsetHint,
1556 &fdeInfo, &cieInfo);
1557 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001558#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001559 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1560 foundFDE = EHHeaderParser<A>::findFDE(
1561 _addressSpace, pc, sects.dwarf_index_section,
1562 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1563 }
1564#endif
1565 if (!foundFDE) {
1566 // otherwise, search cache of previously found FDEs.
1567 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1568 if (cachedFDE != 0) {
1569 foundFDE =
1570 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001571 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001572 cachedFDE, &fdeInfo, &cieInfo);
1573 foundInCache = foundFDE;
1574 }
1575 }
1576 if (!foundFDE) {
1577 // Still not found, do full scan of __eh_frame section.
1578 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001579 sects.dwarf_section_length, 0,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001580 &fdeInfo, &cieInfo);
1581 }
1582 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001583 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001584 // Add to cache (to make next lookup faster) if we had no hint
1585 // and there was no index.
1586 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001587 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001588 if (sects.dwarf_index_section == 0)
1589 #endif
1590 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1591 fdeInfo.fdeStart);
1592 }
1593 return true;
1594 }
1595 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001596 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001597 return false;
1598}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001599#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001600
1601
Ranjeet Singh421231a2017-03-31 15:28:06 +00001602#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001603template <typename A, typename R>
1604bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1605 const UnwindInfoSections &sects) {
1606 const bool log = false;
1607 if (log)
1608 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1609 (uint64_t)pc, (uint64_t)sects.dso_base);
1610
1611 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1612 sects.compact_unwind_section);
1613 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1614 return false;
1615
1616 // do a binary search of top level index to find page with unwind info
1617 pint_t targetFunctionOffset = pc - sects.dso_base;
1618 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1619 sects.compact_unwind_section
1620 + sectionHeader.indexSectionOffset());
1621 uint32_t low = 0;
1622 uint32_t high = sectionHeader.indexCount();
1623 uint32_t last = high - 1;
1624 while (low < high) {
1625 uint32_t mid = (low + high) / 2;
1626 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1627 //mid, low, high, topIndex.functionOffset(mid));
1628 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1629 if ((mid == last) ||
1630 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1631 low = mid;
1632 break;
1633 } else {
1634 low = mid + 1;
1635 }
1636 } else {
1637 high = mid;
1638 }
1639 }
1640 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1641 const uint32_t firstLevelNextPageFunctionOffset =
1642 topIndex.functionOffset(low + 1);
1643 const pint_t secondLevelAddr =
1644 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1645 const pint_t lsdaArrayStartAddr =
1646 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1647 const pint_t lsdaArrayEndAddr =
1648 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1649 if (log)
1650 fprintf(stderr, "\tfirst level search for result index=%d "
1651 "to secondLevelAddr=0x%llX\n",
1652 low, (uint64_t) secondLevelAddr);
1653 // do a binary search of second level page index
1654 uint32_t encoding = 0;
1655 pint_t funcStart = 0;
1656 pint_t funcEnd = 0;
1657 pint_t lsda = 0;
1658 pint_t personality = 0;
1659 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1660 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1661 // regular page
1662 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1663 secondLevelAddr);
1664 UnwindSectionRegularArray<A> pageIndex(
1665 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1666 // binary search looks for entry with e where index[e].offset <= pc <
1667 // index[e+1].offset
1668 if (log)
1669 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1670 "regular page starting at secondLevelAddr=0x%llX\n",
1671 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1672 low = 0;
1673 high = pageHeader.entryCount();
1674 while (low < high) {
1675 uint32_t mid = (low + high) / 2;
1676 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1677 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1678 // at end of table
1679 low = mid;
1680 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1681 break;
1682 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1683 // next is too big, so we found it
1684 low = mid;
1685 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1686 break;
1687 } else {
1688 low = mid + 1;
1689 }
1690 } else {
1691 high = mid;
1692 }
1693 }
1694 encoding = pageIndex.encoding(low);
1695 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1696 if (pc < funcStart) {
1697 if (log)
1698 fprintf(
1699 stderr,
1700 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1701 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1702 return false;
1703 }
1704 if (pc > funcEnd) {
1705 if (log)
1706 fprintf(
1707 stderr,
1708 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1709 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1710 return false;
1711 }
1712 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1713 // compressed page
1714 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1715 secondLevelAddr);
1716 UnwindSectionCompressedArray<A> pageIndex(
1717 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1718 const uint32_t targetFunctionPageOffset =
1719 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1720 // binary search looks for entry with e where index[e].offset <= pc <
1721 // index[e+1].offset
1722 if (log)
1723 fprintf(stderr, "\tbinary search of compressed page starting at "
1724 "secondLevelAddr=0x%llX\n",
1725 (uint64_t) secondLevelAddr);
1726 low = 0;
1727 last = pageHeader.entryCount() - 1;
1728 high = pageHeader.entryCount();
1729 while (low < high) {
1730 uint32_t mid = (low + high) / 2;
1731 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1732 if ((mid == last) ||
1733 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1734 low = mid;
1735 break;
1736 } else {
1737 low = mid + 1;
1738 }
1739 } else {
1740 high = mid;
1741 }
1742 }
1743 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1744 + sects.dso_base;
1745 if (low < last)
1746 funcEnd =
1747 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1748 + sects.dso_base;
1749 else
1750 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1751 if (pc < funcStart) {
Nico Weber5f424e32021-07-02 10:03:32 -04001752 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1753 "not in second level compressed unwind table. "
1754 "funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001755 (uint64_t) pc, (uint64_t) funcStart);
1756 return false;
1757 }
1758 if (pc > funcEnd) {
Nico Weber5f424e32021-07-02 10:03:32 -04001759 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1760 "not in second level compressed unwind table. "
1761 "funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001762 (uint64_t) pc, (uint64_t) funcEnd);
1763 return false;
1764 }
1765 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1766 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1767 // encoding is in common table in section header
1768 encoding = _addressSpace.get32(
1769 sects.compact_unwind_section +
1770 sectionHeader.commonEncodingsArraySectionOffset() +
1771 encodingIndex * sizeof(uint32_t));
1772 } else {
1773 // encoding is in page specific table
1774 uint16_t pageEncodingIndex =
1775 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1776 encoding = _addressSpace.get32(secondLevelAddr +
1777 pageHeader.encodingsPageOffset() +
1778 pageEncodingIndex * sizeof(uint32_t));
1779 }
1780 } else {
Nico Weber5f424e32021-07-02 10:03:32 -04001781 _LIBUNWIND_DEBUG_LOG(
1782 "malformed __unwind_info at 0x%0llX bad second level page",
1783 (uint64_t)sects.compact_unwind_section);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001784 return false;
1785 }
1786
1787 // look up LSDA, if encoding says function has one
1788 if (encoding & UNWIND_HAS_LSDA) {
1789 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1790 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1791 low = 0;
1792 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1793 sizeof(unwind_info_section_header_lsda_index_entry);
1794 // binary search looks for entry with exact match for functionOffset
1795 if (log)
1796 fprintf(stderr,
1797 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1798 funcStartOffset);
1799 while (low < high) {
1800 uint32_t mid = (low + high) / 2;
1801 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1802 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1803 break;
1804 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1805 low = mid + 1;
1806 } else {
1807 high = mid;
1808 }
1809 }
1810 if (lsda == 0) {
1811 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001812 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001813 encoding, (uint64_t) pc);
1814 return false;
1815 }
1816 }
1817
Louis Dionned3bbbc32020-08-11 15:24:21 -04001818 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001819 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1820 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1821 if (personalityIndex != 0) {
1822 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001823 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001824 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001825 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001826 encoding, personalityIndex,
1827 sectionHeader.personalityArrayCount());
1828 return false;
1829 }
1830 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1831 sects.compact_unwind_section +
1832 sectionHeader.personalityArraySectionOffset() +
1833 personalityIndex * sizeof(uint32_t));
1834 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1835 personality = _addressSpace.getP(personalityPointer);
1836 if (log)
1837 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1838 "personalityDelta=0x%08X, personality=0x%08llX\n",
1839 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1840 }
1841
1842 if (log)
1843 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1844 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1845 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1846 _info.start_ip = funcStart;
1847 _info.end_ip = funcEnd;
1848 _info.lsda = lsda;
1849 _info.handler = personality;
1850 _info.gp = 0;
1851 _info.flags = 0;
1852 _info.format = encoding;
1853 _info.unwind_info = 0;
1854 _info.unwind_info_size = 0;
1855 _info.extra = sects.dso_base;
1856 return true;
1857}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001858#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001859
1860
Charles Davisfa2e6202018-08-30 21:29:00 +00001861#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1862template <typename A, typename R>
1863bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1864 pint_t base;
1865 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1866 if (!unwindEntry) {
1867 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1868 return false;
1869 }
1870 _info.gp = 0;
1871 _info.flags = 0;
1872 _info.format = 0;
1873 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1874 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1875 _info.extra = base;
1876 _info.start_ip = base + unwindEntry->BeginAddress;
1877#ifdef _LIBUNWIND_TARGET_X86_64
1878 _info.end_ip = base + unwindEntry->EndAddress;
1879 // Only fill in the handler and LSDA if they're stale.
1880 if (pc != getLastPC()) {
1881 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1882 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1883 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1884 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1885 // these structures.)
1886 // N.B. UNWIND_INFO structs are DWORD-aligned.
1887 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1888 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1889 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1890 if (*handler) {
1891 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1892 } else
1893 _info.handler = 0;
1894 } else {
1895 _info.lsda = 0;
1896 _info.handler = 0;
1897 }
1898 }
1899#elif defined(_LIBUNWIND_TARGET_ARM)
1900 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1901 _info.lsda = 0; // FIXME
1902 _info.handler = 0; // FIXME
1903#endif
1904 setLastPC(pc);
1905 return true;
1906}
1907#endif
1908
1909
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001910template <typename A, typename R>
1911void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
Ryan Pricharda684bed2021-01-13 16:38:36 -08001912#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1913 _isSigReturn = false;
1914#endif
1915
1916 pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Ranjeet Singh421231a2017-03-31 15:28:06 +00001917#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001918 // Remove the thumb bit so the IP represents the actual instruction address.
1919 // This matches the behaviour of _Unwind_GetIP on arm.
1920 pc &= (pint_t)~0x1;
1921#endif
1922
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001923 // Exit early if at the top of the stack.
1924 if (pc == 0) {
1925 _unwindInfoMissing = true;
1926 return;
1927 }
1928
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001929 // If the last line of a function is a "throw" the compiler sometimes
1930 // emits no instructions after the call to __cxa_throw. This means
1931 // the return address is actually the start of the next function.
1932 // To disambiguate this, back up the pc when we know it is a return
1933 // address.
1934 if (isReturnAddress)
1935 --pc;
1936
1937 // Ask address space object to find unwind sections for this pc.
1938 UnwindInfoSections sects;
1939 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001940#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001941 // If there is a compact unwind encoding table, look there first.
1942 if (sects.compact_unwind_section != 0) {
1943 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001944 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001945 // Found info in table, done unless encoding says to use dwarf.
1946 uint32_t dwarfOffset;
1947 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1948 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1949 // found info in dwarf, done
1950 return;
1951 }
1952 }
1953 #endif
1954 // If unwind table has entry, but entry says there is no unwind info,
1955 // record that we have no unwind info.
1956 if (_info.format == 0)
1957 _unwindInfoMissing = true;
1958 return;
1959 }
1960 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001961#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001962
Charles Davisfa2e6202018-08-30 21:29:00 +00001963#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1964 // If there is SEH unwind info, look there next.
1965 if (this->getInfoFromSEH(pc))
1966 return;
1967#endif
1968
Ranjeet Singh421231a2017-03-31 15:28:06 +00001969#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001970 // If there is dwarf unwind info, look there next.
1971 if (sects.dwarf_section != 0) {
1972 if (this->getInfoFromDwarfSection(pc, sects)) {
1973 // found info in dwarf, done
1974 return;
1975 }
1976 }
1977#endif
1978
Ranjeet Singh421231a2017-03-31 15:28:06 +00001979#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001980 // If there is ARM EHABI unwind info, look there next.
1981 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1982 return;
1983#endif
1984 }
1985
Ranjeet Singh421231a2017-03-31 15:28:06 +00001986#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001987 // There is no static unwind info for this pc. Look to see if an FDE was
1988 // dynamically registered for it.
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -07001989 pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
1990 pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001991 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001992 typename CFI_Parser<A>::FDE_Info fdeInfo;
1993 typename CFI_Parser<A>::CIE_Info cieInfo;
1994 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
1995 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001996 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001997 }
1998
1999 // Lastly, ask AddressSpace object about platform specific ways to locate
2000 // other FDEs.
2001 pint_t fde;
2002 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002003 typename CFI_Parser<A>::FDE_Info fdeInfo;
2004 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002005 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
2006 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002007 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2008 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002009 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002010 }
2011 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00002012#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002013
Ryan Pricharda684bed2021-01-13 16:38:36 -08002014#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2015 if (setInfoForSigReturn())
2016 return;
2017#endif
2018
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002019 // no unwind info, flag that we can't reliably unwind
2020 _unwindInfoMissing = true;
2021}
2022
Ryan Pricharda684bed2021-01-13 16:38:36 -08002023#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2024template <typename A, typename R>
2025bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2026 // Look for the sigreturn trampoline. The trampoline's body is two
2027 // specific instructions (see below). Typically the trampoline comes from the
2028 // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2029 // own restorer function, though, or user-mode QEMU might write a trampoline
2030 // onto the stack.
2031 //
2032 // This special code path is a fallback that is only used if the trampoline
2033 // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2034 // constant for the PC needs to be defined before DWARF can handle a signal
2035 // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2036 // - The PC points at a function compiled without unwind info, and which is
2037 // part of an execute-only mapping (e.g. using -Wl,--execute-only).
2038 // - The PC is invalid and happens to point to unreadable or unmapped memory.
2039 //
2040 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2041 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2042 // Look for instructions: mov x8, #0x8b; svc #0x0
2043 if (_addressSpace.get32(pc) == 0xd2801168 &&
2044 _addressSpace.get32(pc + 4) == 0xd4000001) {
2045 _info = {};
2046 _isSigReturn = true;
2047 return true;
2048 }
2049 return false;
2050}
2051
2052template <typename A, typename R>
2053int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2054 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2055 // - 128-byte siginfo struct
2056 // - ucontext struct:
2057 // - 8-byte long (uc_flags)
2058 // - 8-byte pointer (uc_link)
2059 // - 24-byte stack_t
2060 // - 128-byte signal set
2061 // - 8 bytes of padding because sigcontext has 16-byte alignment
2062 // - sigcontext/mcontext_t
2063 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2064 const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2065
2066 // Offsets from sigcontext to each register.
2067 const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2068 const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2069 const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2070
2071 pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2072
2073 for (int i = 0; i <= 30; ++i) {
2074 uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2075 static_cast<pint_t>(i * 8));
Fangrui Song5f263002021-08-20 14:26:27 -07002076 _registers.setRegister(UNW_AARCH64_X0 + i, value);
Ryan Pricharda684bed2021-01-13 16:38:36 -08002077 }
2078 _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2079 _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2080 _isSignalFrame = true;
2081 return UNW_STEP_SUCCESS;
2082}
2083#endif // defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2084
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002085template <typename A, typename R>
2086int UnwindCursor<A, R>::step() {
2087 // Bottom of stack is defined is when unwind info cannot be found.
2088 if (_unwindInfoMissing)
2089 return UNW_STEP_END;
2090
2091 // Use unwinding info to modify register set as if function returned.
2092 int result;
Ryan Pricharda684bed2021-01-13 16:38:36 -08002093#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2094 if (_isSigReturn) {
2095 result = this->stepThroughSigReturn();
2096 } else
2097#endif
2098 {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002099#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002100 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00002101#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002102 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002103#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002104 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002105#elif defined(_LIBUNWIND_ARM_EHABI)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002106 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002107#else
2108 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00002109 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002110 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00002111 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002112#endif
Ryan Pricharda684bed2021-01-13 16:38:36 -08002113 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002114
2115 // update info based on new PC
2116 if (result == UNW_STEP_SUCCESS) {
2117 this->setInfoBasedOnIPRegister(true);
2118 if (_unwindInfoMissing)
2119 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002120 }
2121
2122 return result;
2123}
2124
2125template <typename A, typename R>
2126void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002127 if (_unwindInfoMissing)
2128 memset(info, 0, sizeof(*info));
2129 else
2130 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002131}
2132
2133template <typename A, typename R>
2134bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2135 unw_word_t *offset) {
2136 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2137 buf, bufLen, offset);
2138}
2139
gejin7f493162021-08-26 16:20:38 +08002140#if defined(_LIBUNWIND_USE_CET)
2141extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2142 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2143 return co->get_registers();
2144}
2145#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002146} // namespace libunwind
2147
2148#endif // __UNWINDCURSOR_HPP__