blob: 1ca842f33aa51d319c02b522059d75cc184f7e86 [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
Koakumaf2ef96e2022-02-05 13:08:26 -08001035#if defined(_LIBUNWIND_TARGET_SPARC64)
1036 int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; }
1037#endif
1038
Sam Elliott81f7e172019-12-16 16:35:17 +00001039#if defined (_LIBUNWIND_TARGET_RISCV)
1040 int stepWithCompactEncoding(Registers_riscv &) {
1041 return UNW_EINVAL;
1042 }
1043#endif
1044
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001045 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1046 R dummy;
1047 return compactSaysUseDwarf(dummy, offset);
1048 }
1049
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001050#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001051 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1052 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1053 if (offset)
1054 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1055 return true;
1056 }
1057 return false;
1058 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001059#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001060
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001061#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001062 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1063 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1064 if (offset)
1065 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1066 return true;
1067 }
1068 return false;
1069 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001070#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001071
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001072#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001073 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1074 return true;
1075 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001076#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001077
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001078#if defined(_LIBUNWIND_TARGET_PPC64)
1079 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1080 return true;
1081 }
1082#endif
1083
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001084#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001085 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1086 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1087 if (offset)
1088 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1089 return true;
1090 }
1091 return false;
1092 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001093#endif
John Baldwin56441d42017-12-12 21:43:36 +00001094
1095#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1096 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1097 return true;
1098 }
1099#endif
1100
John Baldwin541a4352018-01-09 17:07:18 +00001101#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1102 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001103 return true;
1104 }
1105#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001106
1107#if defined(_LIBUNWIND_TARGET_SPARC)
1108 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1109#endif
1110
Koakumaf2ef96e2022-02-05 13:08:26 -08001111#if defined(_LIBUNWIND_TARGET_SPARC64)
1112 bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const {
1113 return true;
1114 }
1115#endif
1116
Sam Elliott81f7e172019-12-16 16:35:17 +00001117#if defined (_LIBUNWIND_TARGET_RISCV)
1118 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1119 return true;
1120 }
1121#endif
1122
Ranjeet Singh421231a2017-03-31 15:28:06 +00001123#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001124
Ranjeet Singh421231a2017-03-31 15:28:06 +00001125#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001126 compact_unwind_encoding_t dwarfEncoding() const {
1127 R dummy;
1128 return dwarfEncoding(dummy);
1129 }
1130
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001131#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001132 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1133 return UNWIND_X86_64_MODE_DWARF;
1134 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001135#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001136
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001137#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001138 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1139 return UNWIND_X86_MODE_DWARF;
1140 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001141#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001142
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001143#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001144 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1145 return 0;
1146 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001147#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001148
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001149#if defined(_LIBUNWIND_TARGET_PPC64)
1150 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1151 return 0;
1152 }
1153#endif
1154
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001155#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001156 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1157 return UNWIND_ARM64_MODE_DWARF;
1158 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001159#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001160
Martin Storsjoa72285f2017-11-02 08:16:16 +00001161#if defined(_LIBUNWIND_TARGET_ARM)
1162 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1163 return 0;
1164 }
1165#endif
1166
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001167#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001168 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1169 return 0;
1170 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001171#endif
John Baldwin56441d42017-12-12 21:43:36 +00001172
Brian Cainb432c472020-04-09 00:14:02 -05001173#if defined (_LIBUNWIND_TARGET_HEXAGON)
1174 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1175 return 0;
1176 }
1177#endif
1178
John Baldwin56441d42017-12-12 21:43:36 +00001179#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1180 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1181 return 0;
1182 }
1183#endif
1184
John Baldwin541a4352018-01-09 17:07:18 +00001185#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1186 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001187 return 0;
1188 }
1189#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001190
1191#if defined(_LIBUNWIND_TARGET_SPARC)
1192 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1193#endif
1194
Koakumaf2ef96e2022-02-05 13:08:26 -08001195#if defined(_LIBUNWIND_TARGET_SPARC64)
1196 compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const {
1197 return 0;
1198 }
1199#endif
1200
Sam Elliott81f7e172019-12-16 16:35:17 +00001201#if defined (_LIBUNWIND_TARGET_RISCV)
1202 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1203 return 0;
1204 }
1205#endif
1206
Ranjeet Singh421231a2017-03-31 15:28:06 +00001207#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001208
Charles Davisfa2e6202018-08-30 21:29:00 +00001209#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1210 // For runtime environments using SEH unwind data without Windows runtime
1211 // support.
1212 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1213 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1214 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1215 /* FIXME: Implement */
1216 *base = 0;
1217 return nullptr;
1218 }
1219 bool getInfoFromSEH(pint_t pc);
1220 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1221#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1222
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001223
1224 A &_addressSpace;
1225 R _registers;
1226 unw_proc_info_t _info;
1227 bool _unwindInfoMissing;
1228 bool _isSignalFrame;
Ryan Pricharda684bed2021-01-13 16:38:36 -08001229#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1230 bool _isSigReturn = false;
1231#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001232};
1233
1234
1235template <typename A, typename R>
1236UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1237 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1238 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001239 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001240 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001241 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1242 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001243 memset(&_info, 0, sizeof(_info));
1244}
1245
1246template <typename A, typename R>
1247UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1248 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1249 memset(&_info, 0, sizeof(_info));
1250 // FIXME
1251 // fill in _registers from thread arg
1252}
1253
1254
1255template <typename A, typename R>
1256bool UnwindCursor<A, R>::validReg(int regNum) {
1257 return _registers.validRegister(regNum);
1258}
1259
1260template <typename A, typename R>
1261unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1262 return _registers.getRegister(regNum);
1263}
1264
1265template <typename A, typename R>
1266void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1267 _registers.setRegister(regNum, (typename A::pint_t)value);
1268}
1269
1270template <typename A, typename R>
1271bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1272 return _registers.validFloatRegister(regNum);
1273}
1274
1275template <typename A, typename R>
1276unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1277 return _registers.getFloatRegister(regNum);
1278}
1279
1280template <typename A, typename R>
1281void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1282 _registers.setFloatRegister(regNum, value);
1283}
1284
1285template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1286 _registers.jumpto();
1287}
1288
1289#ifdef __arm__
1290template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1291 _registers.saveVFPAsX();
1292}
1293#endif
1294
1295template <typename A, typename R>
1296const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1297 return _registers.getRegisterName(regNum);
1298}
1299
1300template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1301 return _isSignalFrame;
1302}
1303
Charles Davisfa2e6202018-08-30 21:29:00 +00001304#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1305
Ranjeet Singh421231a2017-03-31 15:28:06 +00001306#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001307template<typename A>
1308struct EHABISectionIterator {
1309 typedef EHABISectionIterator _Self;
1310
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001311 typedef typename A::pint_t value_type;
1312 typedef typename A::pint_t* pointer;
1313 typedef typename A::pint_t& reference;
1314 typedef size_t size_type;
1315 typedef size_t difference_type;
1316
1317 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1318 return _Self(addressSpace, sects, 0);
1319 }
1320 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001321 return _Self(addressSpace, sects,
1322 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001323 }
1324
1325 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1326 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1327
1328 _Self& operator++() { ++_i; return *this; }
1329 _Self& operator+=(size_t a) { _i += a; return *this; }
1330 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1331 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1332
1333 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1334 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1335
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001336 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001337
1338 bool operator==(const _Self& other) const {
1339 assert(_addressSpace == other._addressSpace);
1340 assert(_sects == other._sects);
1341 return _i == other._i;
1342 }
1343
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001344 bool operator!=(const _Self& other) const {
1345 assert(_addressSpace == other._addressSpace);
1346 assert(_sects == other._sects);
1347 return _i != other._i;
1348 }
1349
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001350 typename A::pint_t operator*() const { return functionAddress(); }
1351
1352 typename A::pint_t functionAddress() const {
1353 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1354 EHABIIndexEntry, _i, functionOffset);
1355 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1356 }
1357
1358 typename A::pint_t dataAddress() {
1359 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1360 EHABIIndexEntry, _i, data);
1361 return indexAddr;
1362 }
1363
1364 private:
1365 size_t _i;
1366 A* _addressSpace;
1367 const UnwindInfoSections* _sects;
1368};
1369
Petr Hosekac0d9e02019-01-29 22:26:18 +00001370namespace {
1371
1372template <typename A>
1373EHABISectionIterator<A> EHABISectionUpperBound(
1374 EHABISectionIterator<A> first,
1375 EHABISectionIterator<A> last,
1376 typename A::pint_t value) {
1377 size_t len = last - first;
1378 while (len > 0) {
1379 size_t l2 = len / 2;
1380 EHABISectionIterator<A> m = first + l2;
1381 if (value < *m) {
1382 len = l2;
1383 } else {
1384 first = ++m;
1385 len -= l2 + 1;
1386 }
1387 }
1388 return first;
1389}
1390
1391}
1392
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001393template <typename A, typename R>
1394bool UnwindCursor<A, R>::getInfoFromEHABISection(
1395 pint_t pc,
1396 const UnwindInfoSections &sects) {
1397 EHABISectionIterator<A> begin =
1398 EHABISectionIterator<A>::begin(_addressSpace, sects);
1399 EHABISectionIterator<A> end =
1400 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001401 if (begin == end)
1402 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001403
Petr Hosekac0d9e02019-01-29 22:26:18 +00001404 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001405 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001406 return false;
1407 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1408
1409 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001410 // If an exception is thrown from a function, corresponding to the last entry
1411 // in the table, we don't really know the function extent and have to choose a
1412 // value for nextPC. Choosing max() will allow the range check during trace to
1413 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001414 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001415 pint_t indexDataAddr = itThisPC.dataAddress();
1416
1417 if (indexDataAddr == 0)
1418 return false;
1419
1420 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1421 if (indexData == UNW_EXIDX_CANTUNWIND)
1422 return false;
1423
1424 // If the high bit is set, the exception handling table entry is inline inside
1425 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001426 // the table points at an offset in the exception handling table (section 5
1427 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001428 pint_t exceptionTableAddr;
1429 uint32_t exceptionTableData;
1430 bool isSingleWordEHT;
1431 if (indexData & 0x80000000) {
1432 exceptionTableAddr = indexDataAddr;
1433 // TODO(ajwong): Should this data be 0?
1434 exceptionTableData = indexData;
1435 isSingleWordEHT = true;
1436 } else {
1437 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1438 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1439 isSingleWordEHT = false;
1440 }
1441
1442 // Now we know the 3 things:
1443 // exceptionTableAddr -- exception handler table entry.
1444 // exceptionTableData -- the data inside the first word of the eht entry.
1445 // isSingleWordEHT -- whether the entry is in the index.
1446 unw_word_t personalityRoutine = 0xbadf00d;
1447 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001448 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001449
1450 // If the high bit in the exception handling table entry is set, the entry is
1451 // in compact form (section 6.3 EHABI).
1452 if (exceptionTableData & 0x80000000) {
1453 // Grab the index of the personality routine from the compact form.
1454 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1455 uint32_t extraWords = 0;
1456 switch (choice) {
1457 case 0:
1458 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1459 extraWords = 0;
1460 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001461 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001462 break;
1463 case 1:
1464 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1465 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1466 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001467 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001468 break;
1469 case 2:
1470 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1471 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1472 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001473 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001474 break;
1475 default:
1476 _LIBUNWIND_ABORT("unknown personality routine");
1477 return false;
1478 }
1479
1480 if (isSingleWordEHT) {
1481 if (extraWords != 0) {
1482 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1483 "requires extra words");
1484 return false;
1485 }
1486 }
1487 } else {
1488 pint_t personalityAddr =
1489 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1490 personalityRoutine = personalityAddr;
1491
1492 // ARM EHABI # 6.2, # 9.2
1493 //
1494 // +---- ehtp
1495 // v
1496 // +--------------------------------------+
1497 // | +--------+--------+--------+-------+ |
1498 // | |0| prel31 to personalityRoutine | |
1499 // | +--------+--------+--------+-------+ |
1500 // | | N | unwind opcodes | | <-- UnwindData
1501 // | +--------+--------+--------+-------+ |
1502 // | | Word 2 unwind opcodes | |
1503 // | +--------+--------+--------+-------+ |
1504 // | ... |
1505 // | +--------+--------+--------+-------+ |
1506 // | | Word N unwind opcodes | |
1507 // | +--------+--------+--------+-------+ |
1508 // | | LSDA | | <-- lsda
1509 // | | ... | |
1510 // | +--------+--------+--------+-------+ |
1511 // +--------------------------------------+
1512
1513 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1514 uint32_t FirstDataWord = *UnwindData;
1515 size_t N = ((FirstDataWord >> 24) & 0xff);
1516 size_t NDataWords = N + 1;
1517 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1518 }
1519
1520 _info.start_ip = thisPC;
1521 _info.end_ip = nextPC;
1522 _info.handler = personalityRoutine;
1523 _info.unwind_info = exceptionTableAddr;
1524 _info.lsda = lsda;
1525 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001526 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001527
1528 return true;
1529}
1530#endif
1531
Ranjeet Singh421231a2017-03-31 15:28:06 +00001532#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001533template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001534bool UnwindCursor<A, R>::getInfoFromFdeCie(
1535 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1536 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1537 uintptr_t dso_base) {
1538 typename CFI_Parser<A>::PrologInfo prolog;
1539 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1540 R::getArch(), &prolog)) {
1541 // Save off parsed FDE info
1542 _info.start_ip = fdeInfo.pcStart;
1543 _info.end_ip = fdeInfo.pcEnd;
1544 _info.lsda = fdeInfo.lsda;
1545 _info.handler = cieInfo.personality;
1546 // Some frameless functions need SP altered when resuming in function, so
1547 // propagate spExtraArgSize.
1548 _info.gp = prolog.spExtraArgSize;
1549 _info.flags = 0;
1550 _info.format = dwarfEncoding();
1551 _info.unwind_info = fdeInfo.fdeStart;
1552 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1553 _info.extra = static_cast<unw_word_t>(dso_base);
1554 return true;
1555 }
1556 return false;
1557}
1558
1559template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001560bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1561 const UnwindInfoSections &sects,
1562 uint32_t fdeSectionOffsetHint) {
1563 typename CFI_Parser<A>::FDE_Info fdeInfo;
1564 typename CFI_Parser<A>::CIE_Info cieInfo;
1565 bool foundFDE = false;
1566 bool foundInCache = false;
1567 // If compact encoding table gave offset into dwarf section, go directly there
1568 if (fdeSectionOffsetHint != 0) {
1569 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001570 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001571 sects.dwarf_section + fdeSectionOffsetHint,
1572 &fdeInfo, &cieInfo);
1573 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001574#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001575 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1576 foundFDE = EHHeaderParser<A>::findFDE(
1577 _addressSpace, pc, sects.dwarf_index_section,
1578 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1579 }
1580#endif
1581 if (!foundFDE) {
1582 // otherwise, search cache of previously found FDEs.
1583 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1584 if (cachedFDE != 0) {
1585 foundFDE =
1586 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001587 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001588 cachedFDE, &fdeInfo, &cieInfo);
1589 foundInCache = foundFDE;
1590 }
1591 }
1592 if (!foundFDE) {
1593 // Still not found, do full scan of __eh_frame section.
1594 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001595 sects.dwarf_section_length, 0,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001596 &fdeInfo, &cieInfo);
1597 }
1598 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001599 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001600 // Add to cache (to make next lookup faster) if we had no hint
1601 // and there was no index.
1602 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001603 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001604 if (sects.dwarf_index_section == 0)
1605 #endif
1606 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1607 fdeInfo.fdeStart);
1608 }
1609 return true;
1610 }
1611 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001612 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001613 return false;
1614}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001615#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001616
1617
Ranjeet Singh421231a2017-03-31 15:28:06 +00001618#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001619template <typename A, typename R>
1620bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1621 const UnwindInfoSections &sects) {
1622 const bool log = false;
1623 if (log)
1624 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1625 (uint64_t)pc, (uint64_t)sects.dso_base);
1626
1627 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1628 sects.compact_unwind_section);
1629 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1630 return false;
1631
1632 // do a binary search of top level index to find page with unwind info
1633 pint_t targetFunctionOffset = pc - sects.dso_base;
1634 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1635 sects.compact_unwind_section
1636 + sectionHeader.indexSectionOffset());
1637 uint32_t low = 0;
1638 uint32_t high = sectionHeader.indexCount();
1639 uint32_t last = high - 1;
1640 while (low < high) {
1641 uint32_t mid = (low + high) / 2;
1642 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1643 //mid, low, high, topIndex.functionOffset(mid));
1644 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1645 if ((mid == last) ||
1646 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1647 low = mid;
1648 break;
1649 } else {
1650 low = mid + 1;
1651 }
1652 } else {
1653 high = mid;
1654 }
1655 }
1656 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1657 const uint32_t firstLevelNextPageFunctionOffset =
1658 topIndex.functionOffset(low + 1);
1659 const pint_t secondLevelAddr =
1660 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1661 const pint_t lsdaArrayStartAddr =
1662 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1663 const pint_t lsdaArrayEndAddr =
1664 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1665 if (log)
1666 fprintf(stderr, "\tfirst level search for result index=%d "
1667 "to secondLevelAddr=0x%llX\n",
1668 low, (uint64_t) secondLevelAddr);
1669 // do a binary search of second level page index
1670 uint32_t encoding = 0;
1671 pint_t funcStart = 0;
1672 pint_t funcEnd = 0;
1673 pint_t lsda = 0;
1674 pint_t personality = 0;
1675 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1676 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1677 // regular page
1678 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1679 secondLevelAddr);
1680 UnwindSectionRegularArray<A> pageIndex(
1681 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1682 // binary search looks for entry with e where index[e].offset <= pc <
1683 // index[e+1].offset
1684 if (log)
1685 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1686 "regular page starting at secondLevelAddr=0x%llX\n",
1687 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1688 low = 0;
1689 high = pageHeader.entryCount();
1690 while (low < high) {
1691 uint32_t mid = (low + high) / 2;
1692 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1693 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1694 // at end of table
1695 low = mid;
1696 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1697 break;
1698 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1699 // next is too big, so we found it
1700 low = mid;
1701 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1702 break;
1703 } else {
1704 low = mid + 1;
1705 }
1706 } else {
1707 high = mid;
1708 }
1709 }
1710 encoding = pageIndex.encoding(low);
1711 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1712 if (pc < funcStart) {
1713 if (log)
1714 fprintf(
1715 stderr,
1716 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1717 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1718 return false;
1719 }
1720 if (pc > funcEnd) {
1721 if (log)
1722 fprintf(
1723 stderr,
1724 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1725 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1726 return false;
1727 }
1728 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1729 // compressed page
1730 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1731 secondLevelAddr);
1732 UnwindSectionCompressedArray<A> pageIndex(
1733 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1734 const uint32_t targetFunctionPageOffset =
1735 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1736 // binary search looks for entry with e where index[e].offset <= pc <
1737 // index[e+1].offset
1738 if (log)
1739 fprintf(stderr, "\tbinary search of compressed page starting at "
1740 "secondLevelAddr=0x%llX\n",
1741 (uint64_t) secondLevelAddr);
1742 low = 0;
1743 last = pageHeader.entryCount() - 1;
1744 high = pageHeader.entryCount();
1745 while (low < high) {
1746 uint32_t mid = (low + high) / 2;
1747 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1748 if ((mid == last) ||
1749 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1750 low = mid;
1751 break;
1752 } else {
1753 low = mid + 1;
1754 }
1755 } else {
1756 high = mid;
1757 }
1758 }
1759 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1760 + sects.dso_base;
1761 if (low < last)
1762 funcEnd =
1763 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1764 + sects.dso_base;
1765 else
1766 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1767 if (pc < funcStart) {
Nico Weber5f424e32021-07-02 10:03:32 -04001768 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1769 "not in second level compressed unwind table. "
1770 "funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001771 (uint64_t) pc, (uint64_t) funcStart);
1772 return false;
1773 }
1774 if (pc > funcEnd) {
Nico Weber5f424e32021-07-02 10:03:32 -04001775 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1776 "not in second level compressed unwind table. "
1777 "funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001778 (uint64_t) pc, (uint64_t) funcEnd);
1779 return false;
1780 }
1781 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1782 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1783 // encoding is in common table in section header
1784 encoding = _addressSpace.get32(
1785 sects.compact_unwind_section +
1786 sectionHeader.commonEncodingsArraySectionOffset() +
1787 encodingIndex * sizeof(uint32_t));
1788 } else {
1789 // encoding is in page specific table
1790 uint16_t pageEncodingIndex =
1791 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1792 encoding = _addressSpace.get32(secondLevelAddr +
1793 pageHeader.encodingsPageOffset() +
1794 pageEncodingIndex * sizeof(uint32_t));
1795 }
1796 } else {
Nico Weber5f424e32021-07-02 10:03:32 -04001797 _LIBUNWIND_DEBUG_LOG(
1798 "malformed __unwind_info at 0x%0llX bad second level page",
1799 (uint64_t)sects.compact_unwind_section);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001800 return false;
1801 }
1802
1803 // look up LSDA, if encoding says function has one
1804 if (encoding & UNWIND_HAS_LSDA) {
1805 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1806 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1807 low = 0;
1808 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1809 sizeof(unwind_info_section_header_lsda_index_entry);
1810 // binary search looks for entry with exact match for functionOffset
1811 if (log)
1812 fprintf(stderr,
1813 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1814 funcStartOffset);
1815 while (low < high) {
1816 uint32_t mid = (low + high) / 2;
1817 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1818 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1819 break;
1820 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1821 low = mid + 1;
1822 } else {
1823 high = mid;
1824 }
1825 }
1826 if (lsda == 0) {
1827 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001828 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001829 encoding, (uint64_t) pc);
1830 return false;
1831 }
1832 }
1833
Louis Dionned3bbbc32020-08-11 15:24:21 -04001834 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001835 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1836 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1837 if (personalityIndex != 0) {
1838 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001839 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001840 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001841 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001842 encoding, personalityIndex,
1843 sectionHeader.personalityArrayCount());
1844 return false;
1845 }
1846 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1847 sects.compact_unwind_section +
1848 sectionHeader.personalityArraySectionOffset() +
1849 personalityIndex * sizeof(uint32_t));
1850 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1851 personality = _addressSpace.getP(personalityPointer);
1852 if (log)
1853 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1854 "personalityDelta=0x%08X, personality=0x%08llX\n",
1855 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1856 }
1857
1858 if (log)
1859 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1860 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1861 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1862 _info.start_ip = funcStart;
1863 _info.end_ip = funcEnd;
1864 _info.lsda = lsda;
1865 _info.handler = personality;
1866 _info.gp = 0;
1867 _info.flags = 0;
1868 _info.format = encoding;
1869 _info.unwind_info = 0;
1870 _info.unwind_info_size = 0;
1871 _info.extra = sects.dso_base;
1872 return true;
1873}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001874#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001875
1876
Charles Davisfa2e6202018-08-30 21:29:00 +00001877#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1878template <typename A, typename R>
1879bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1880 pint_t base;
1881 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1882 if (!unwindEntry) {
1883 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1884 return false;
1885 }
1886 _info.gp = 0;
1887 _info.flags = 0;
1888 _info.format = 0;
1889 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1890 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1891 _info.extra = base;
1892 _info.start_ip = base + unwindEntry->BeginAddress;
1893#ifdef _LIBUNWIND_TARGET_X86_64
1894 _info.end_ip = base + unwindEntry->EndAddress;
1895 // Only fill in the handler and LSDA if they're stale.
1896 if (pc != getLastPC()) {
1897 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1898 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1899 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1900 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1901 // these structures.)
1902 // N.B. UNWIND_INFO structs are DWORD-aligned.
1903 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1904 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1905 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1906 if (*handler) {
1907 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1908 } else
1909 _info.handler = 0;
1910 } else {
1911 _info.lsda = 0;
1912 _info.handler = 0;
1913 }
1914 }
1915#elif defined(_LIBUNWIND_TARGET_ARM)
1916 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1917 _info.lsda = 0; // FIXME
1918 _info.handler = 0; // FIXME
1919#endif
1920 setLastPC(pc);
1921 return true;
1922}
1923#endif
1924
1925
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001926template <typename A, typename R>
1927void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
Ryan Pricharda684bed2021-01-13 16:38:36 -08001928#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1929 _isSigReturn = false;
1930#endif
1931
1932 pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Ranjeet Singh421231a2017-03-31 15:28:06 +00001933#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001934 // Remove the thumb bit so the IP represents the actual instruction address.
1935 // This matches the behaviour of _Unwind_GetIP on arm.
1936 pc &= (pint_t)~0x1;
1937#endif
1938
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001939 // Exit early if at the top of the stack.
1940 if (pc == 0) {
1941 _unwindInfoMissing = true;
1942 return;
1943 }
1944
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001945 // If the last line of a function is a "throw" the compiler sometimes
1946 // emits no instructions after the call to __cxa_throw. This means
1947 // the return address is actually the start of the next function.
1948 // To disambiguate this, back up the pc when we know it is a return
1949 // address.
1950 if (isReturnAddress)
1951 --pc;
1952
1953 // Ask address space object to find unwind sections for this pc.
1954 UnwindInfoSections sects;
1955 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001956#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001957 // If there is a compact unwind encoding table, look there first.
1958 if (sects.compact_unwind_section != 0) {
1959 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001960 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001961 // Found info in table, done unless encoding says to use dwarf.
1962 uint32_t dwarfOffset;
1963 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1964 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1965 // found info in dwarf, done
1966 return;
1967 }
1968 }
1969 #endif
1970 // If unwind table has entry, but entry says there is no unwind info,
1971 // record that we have no unwind info.
1972 if (_info.format == 0)
1973 _unwindInfoMissing = true;
1974 return;
1975 }
1976 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001977#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001978
Charles Davisfa2e6202018-08-30 21:29:00 +00001979#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1980 // If there is SEH unwind info, look there next.
1981 if (this->getInfoFromSEH(pc))
1982 return;
1983#endif
1984
Ranjeet Singh421231a2017-03-31 15:28:06 +00001985#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001986 // If there is dwarf unwind info, look there next.
1987 if (sects.dwarf_section != 0) {
1988 if (this->getInfoFromDwarfSection(pc, sects)) {
1989 // found info in dwarf, done
1990 return;
1991 }
1992 }
1993#endif
1994
Ranjeet Singh421231a2017-03-31 15:28:06 +00001995#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001996 // If there is ARM EHABI unwind info, look there next.
1997 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1998 return;
1999#endif
2000 }
2001
Ranjeet Singh421231a2017-03-31 15:28:06 +00002002#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002003 // There is no static unwind info for this pc. Look to see if an FDE was
2004 // dynamically registered for it.
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -07002005 pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
2006 pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002007 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002008 typename CFI_Parser<A>::FDE_Info fdeInfo;
2009 typename CFI_Parser<A>::CIE_Info cieInfo;
2010 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
2011 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002012 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002013 }
2014
2015 // Lastly, ask AddressSpace object about platform specific ways to locate
2016 // other FDEs.
2017 pint_t fde;
2018 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002019 typename CFI_Parser<A>::FDE_Info fdeInfo;
2020 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002021 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
2022 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002023 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2024 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002025 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002026 }
2027 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00002028#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002029
Ryan Pricharda684bed2021-01-13 16:38:36 -08002030#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2031 if (setInfoForSigReturn())
2032 return;
2033#endif
2034
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002035 // no unwind info, flag that we can't reliably unwind
2036 _unwindInfoMissing = true;
2037}
2038
Ryan Pricharda684bed2021-01-13 16:38:36 -08002039#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2040template <typename A, typename R>
2041bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2042 // Look for the sigreturn trampoline. The trampoline's body is two
2043 // specific instructions (see below). Typically the trampoline comes from the
2044 // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2045 // own restorer function, though, or user-mode QEMU might write a trampoline
2046 // onto the stack.
2047 //
2048 // This special code path is a fallback that is only used if the trampoline
2049 // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2050 // constant for the PC needs to be defined before DWARF can handle a signal
2051 // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2052 // - The PC points at a function compiled without unwind info, and which is
2053 // part of an execute-only mapping (e.g. using -Wl,--execute-only).
2054 // - The PC is invalid and happens to point to unreadable or unmapped memory.
2055 //
2056 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2057 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2058 // Look for instructions: mov x8, #0x8b; svc #0x0
2059 if (_addressSpace.get32(pc) == 0xd2801168 &&
2060 _addressSpace.get32(pc + 4) == 0xd4000001) {
2061 _info = {};
2062 _isSigReturn = true;
2063 return true;
2064 }
2065 return false;
2066}
2067
2068template <typename A, typename R>
2069int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2070 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2071 // - 128-byte siginfo struct
2072 // - ucontext struct:
2073 // - 8-byte long (uc_flags)
2074 // - 8-byte pointer (uc_link)
2075 // - 24-byte stack_t
2076 // - 128-byte signal set
2077 // - 8 bytes of padding because sigcontext has 16-byte alignment
2078 // - sigcontext/mcontext_t
2079 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2080 const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2081
2082 // Offsets from sigcontext to each register.
2083 const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2084 const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2085 const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2086
2087 pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2088
2089 for (int i = 0; i <= 30; ++i) {
2090 uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2091 static_cast<pint_t>(i * 8));
Fangrui Song5f263002021-08-20 14:26:27 -07002092 _registers.setRegister(UNW_AARCH64_X0 + i, value);
Ryan Pricharda684bed2021-01-13 16:38:36 -08002093 }
2094 _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2095 _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2096 _isSignalFrame = true;
2097 return UNW_STEP_SUCCESS;
2098}
2099#endif // defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2100
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002101template <typename A, typename R>
2102int UnwindCursor<A, R>::step() {
2103 // Bottom of stack is defined is when unwind info cannot be found.
2104 if (_unwindInfoMissing)
2105 return UNW_STEP_END;
2106
2107 // Use unwinding info to modify register set as if function returned.
2108 int result;
Ryan Pricharda684bed2021-01-13 16:38:36 -08002109#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2110 if (_isSigReturn) {
2111 result = this->stepThroughSigReturn();
2112 } else
2113#endif
2114 {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002115#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002116 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00002117#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002118 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002119#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002120 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002121#elif defined(_LIBUNWIND_ARM_EHABI)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002122 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002123#else
2124 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00002125 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002126 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00002127 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002128#endif
Ryan Pricharda684bed2021-01-13 16:38:36 -08002129 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002130
2131 // update info based on new PC
2132 if (result == UNW_STEP_SUCCESS) {
2133 this->setInfoBasedOnIPRegister(true);
2134 if (_unwindInfoMissing)
2135 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002136 }
2137
2138 return result;
2139}
2140
2141template <typename A, typename R>
2142void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002143 if (_unwindInfoMissing)
2144 memset(info, 0, sizeof(*info));
2145 else
2146 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002147}
2148
2149template <typename A, typename R>
2150bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2151 unw_word_t *offset) {
2152 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2153 buf, bufLen, offset);
2154}
2155
gejin7f493162021-08-26 16:20:38 +08002156#if defined(_LIBUNWIND_USE_CET)
2157extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2158 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2159 return co->get_registers();
2160}
2161#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002162} // namespace libunwind
2163
2164#endif // __UNWINDCURSOR_HPP__