blob: 27eca0819149560396af5504ba117f01d5921da8 [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
Xing Xuebbcbce92022-04-13 11:01:59 -040027#ifdef _AIX
28#include <dlfcn.h>
29#include <sys/debug.h>
30#include <sys/pseg.h>
31#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000032
Shoaib Meenai67bace72022-05-23 22:11:34 -070033#if defined(_LIBUNWIND_TARGET_LINUX) && \
34 (defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_S390X))
Shoaib Meenai8c606b02022-05-25 21:39:12 -070035#include <sys/syscall.h>
36#include <sys/uio.h>
37#include <unistd.h>
Shoaib Meenai67bace72022-05-23 22:11:34 -070038#define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
39#endif
40
Ryan Prichard60a480e2022-09-07 17:27:57 -040041#include "AddressSpace.hpp"
42#include "CompactUnwinder.hpp"
43#include "config.h"
44#include "DwarfInstructions.hpp"
45#include "EHHeaderParser.hpp"
46#include "libunwind.h"
47#include "libunwind_ext.h"
48#include "Registers.hpp"
49#include "RWMutex.hpp"
50#include "Unwind-EHABI.h"
51
Charles Davisfa2e6202018-08-30 21:29:00 +000052#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
53// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
54// earlier) SDKs.
55// MinGW-w64 has always provided this struct.
56 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
57 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
58struct _DISPATCHER_CONTEXT {
59 ULONG64 ControlPc;
60 ULONG64 ImageBase;
61 PRUNTIME_FUNCTION FunctionEntry;
62 ULONG64 EstablisherFrame;
63 ULONG64 TargetIp;
64 PCONTEXT ContextRecord;
65 PEXCEPTION_ROUTINE LanguageHandler;
66 PVOID HandlerData;
67 PUNWIND_HISTORY_TABLE HistoryTable;
68 ULONG ScopeIndex;
69 ULONG Fill0;
70};
71 #endif
72
73struct UNWIND_INFO {
74 uint8_t Version : 3;
75 uint8_t Flags : 5;
76 uint8_t SizeOfProlog;
77 uint8_t CountOfCodes;
78 uint8_t FrameRegister : 4;
79 uint8_t FrameOffset : 4;
80 uint16_t UnwindCodes[2];
81};
82
83extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
84 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
85 struct _Unwind_Context *);
86
87#endif
88
Saleem Abdulrasool17552662015-04-24 19:39:17 +000089namespace libunwind {
90
Ranjeet Singh421231a2017-03-31 15:28:06 +000091#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000092/// Cache of recently found FDEs.
93template <typename A>
94class _LIBUNWIND_HIDDEN DwarfFDECache {
95 typedef typename A::pint_t pint_t;
96public:
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -070097 static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000098 static pint_t findFDE(pint_t mh, pint_t pc);
99 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
100 static void removeAllIn(pint_t mh);
101 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
102 unw_word_t ip_end,
103 unw_word_t fde, unw_word_t mh));
104
105private:
106
107 struct entry {
108 pint_t mh;
109 pint_t ip_start;
110 pint_t ip_end;
111 pint_t fde;
112 };
113
114 // These fields are all static to avoid needing an initializer.
115 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000116 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000117#ifdef __APPLE__
118 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
119 static bool _registeredForDyldUnloads;
120#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000121 static entry *_buffer;
122 static entry *_bufferUsed;
123 static entry *_bufferEnd;
124 static entry _initialBuffer[64];
125};
126
127template <typename A>
128typename DwarfFDECache<A>::entry *
129DwarfFDECache<A>::_buffer = _initialBuffer;
130
131template <typename A>
132typename DwarfFDECache<A>::entry *
133DwarfFDECache<A>::_bufferUsed = _initialBuffer;
134
135template <typename A>
136typename DwarfFDECache<A>::entry *
137DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
138
139template <typename A>
140typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
141
142template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000143RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144
145#ifdef __APPLE__
146template <typename A>
147bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
148#endif
149
150template <typename A>
151typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
152 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000153 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000154 for (entry *p = _buffer; p < _bufferUsed; ++p) {
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -0700155 if ((mh == p->mh) || (mh == kSearchAll)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000156 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
157 result = p->fde;
158 break;
159 }
160 }
161 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000162 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000163 return result;
164}
165
166template <typename A>
167void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
168 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000169#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000170 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171 if (_bufferUsed >= _bufferEnd) {
172 size_t oldSize = (size_t)(_bufferEnd - _buffer);
173 size_t newSize = oldSize * 4;
174 // Can't use operator new (we are below it).
175 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
176 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
177 if (_buffer != _initialBuffer)
178 free(_buffer);
179 _buffer = newBuffer;
180 _bufferUsed = &newBuffer[oldSize];
181 _bufferEnd = &newBuffer[newSize];
182 }
183 _bufferUsed->mh = mh;
184 _bufferUsed->ip_start = ip_start;
185 _bufferUsed->ip_end = ip_end;
186 _bufferUsed->fde = fde;
187 ++_bufferUsed;
188#ifdef __APPLE__
189 if (!_registeredForDyldUnloads) {
190 _dyld_register_func_for_remove_image(&dyldUnloadHook);
191 _registeredForDyldUnloads = true;
192 }
193#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000194 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000195#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000196}
197
198template <typename A>
199void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000200 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000201 entry *d = _buffer;
202 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
203 if (s->mh != mh) {
204 if (d != s)
205 *d = *s;
206 ++d;
207 }
208 }
209 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000210 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000211}
212
213#ifdef __APPLE__
214template <typename A>
215void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
216 removeAllIn((pint_t) mh);
217}
218#endif
219
220template <typename A>
221void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
222 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 +0000223 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000224 for (entry *p = _buffer; p < _bufferUsed; ++p) {
225 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
226 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000227 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000228}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000229#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000230
231
232#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
233
Ranjeet Singh421231a2017-03-31 15:28:06 +0000234#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000235template <typename A> class UnwindSectionHeader {
236public:
237 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
238 : _addressSpace(addressSpace), _addr(addr) {}
239
240 uint32_t version() const {
241 return _addressSpace.get32(_addr +
242 offsetof(unwind_info_section_header, version));
243 }
244 uint32_t commonEncodingsArraySectionOffset() const {
245 return _addressSpace.get32(_addr +
246 offsetof(unwind_info_section_header,
247 commonEncodingsArraySectionOffset));
248 }
249 uint32_t commonEncodingsArrayCount() const {
250 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
251 commonEncodingsArrayCount));
252 }
253 uint32_t personalityArraySectionOffset() const {
254 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
255 personalityArraySectionOffset));
256 }
257 uint32_t personalityArrayCount() const {
258 return _addressSpace.get32(
259 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
260 }
261 uint32_t indexSectionOffset() const {
262 return _addressSpace.get32(
263 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
264 }
265 uint32_t indexCount() const {
266 return _addressSpace.get32(
267 _addr + offsetof(unwind_info_section_header, indexCount));
268 }
269
270private:
271 A &_addressSpace;
272 typename A::pint_t _addr;
273};
274
275template <typename A> class UnwindSectionIndexArray {
276public:
277 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
278 : _addressSpace(addressSpace), _addr(addr) {}
279
280 uint32_t functionOffset(uint32_t index) const {
281 return _addressSpace.get32(
282 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
283 functionOffset));
284 }
285 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
286 return _addressSpace.get32(
287 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
288 secondLevelPagesSectionOffset));
289 }
290 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
291 return _addressSpace.get32(
292 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
293 lsdaIndexArraySectionOffset));
294 }
295
296private:
297 A &_addressSpace;
298 typename A::pint_t _addr;
299};
300
301template <typename A> class UnwindSectionRegularPageHeader {
302public:
303 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
304 : _addressSpace(addressSpace), _addr(addr) {}
305
306 uint32_t kind() const {
307 return _addressSpace.get32(
308 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
309 }
310 uint16_t entryPageOffset() const {
311 return _addressSpace.get16(
312 _addr + offsetof(unwind_info_regular_second_level_page_header,
313 entryPageOffset));
314 }
315 uint16_t entryCount() const {
316 return _addressSpace.get16(
317 _addr +
318 offsetof(unwind_info_regular_second_level_page_header, entryCount));
319 }
320
321private:
322 A &_addressSpace;
323 typename A::pint_t _addr;
324};
325
326template <typename A> class UnwindSectionRegularArray {
327public:
328 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
329 : _addressSpace(addressSpace), _addr(addr) {}
330
331 uint32_t functionOffset(uint32_t index) const {
332 return _addressSpace.get32(
333 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
334 functionOffset));
335 }
336 uint32_t encoding(uint32_t index) const {
337 return _addressSpace.get32(
338 _addr +
339 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
340 }
341
342private:
343 A &_addressSpace;
344 typename A::pint_t _addr;
345};
346
347template <typename A> class UnwindSectionCompressedPageHeader {
348public:
349 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
350 : _addressSpace(addressSpace), _addr(addr) {}
351
352 uint32_t kind() const {
353 return _addressSpace.get32(
354 _addr +
355 offsetof(unwind_info_compressed_second_level_page_header, kind));
356 }
357 uint16_t entryPageOffset() const {
358 return _addressSpace.get16(
359 _addr + offsetof(unwind_info_compressed_second_level_page_header,
360 entryPageOffset));
361 }
362 uint16_t entryCount() const {
363 return _addressSpace.get16(
364 _addr +
365 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
366 }
367 uint16_t encodingsPageOffset() const {
368 return _addressSpace.get16(
369 _addr + offsetof(unwind_info_compressed_second_level_page_header,
370 encodingsPageOffset));
371 }
372 uint16_t encodingsCount() const {
373 return _addressSpace.get16(
374 _addr + offsetof(unwind_info_compressed_second_level_page_header,
375 encodingsCount));
376 }
377
378private:
379 A &_addressSpace;
380 typename A::pint_t _addr;
381};
382
383template <typename A> class UnwindSectionCompressedArray {
384public:
385 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
386 : _addressSpace(addressSpace), _addr(addr) {}
387
388 uint32_t functionOffset(uint32_t index) const {
389 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
390 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
391 }
392 uint16_t encodingIndex(uint32_t index) const {
393 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
394 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
395 }
396
397private:
398 A &_addressSpace;
399 typename A::pint_t _addr;
400};
401
402template <typename A> class UnwindSectionLsdaArray {
403public:
404 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
405 : _addressSpace(addressSpace), _addr(addr) {}
406
407 uint32_t functionOffset(uint32_t index) const {
408 return _addressSpace.get32(
409 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
410 index, functionOffset));
411 }
412 uint32_t lsdaOffset(uint32_t index) const {
413 return _addressSpace.get32(
414 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
415 index, lsdaOffset));
416 }
417
418private:
419 A &_addressSpace;
420 typename A::pint_t _addr;
421};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000422#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000423
424class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
425public:
426 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
427 // This avoids an unnecessary dependency to libc++abi.
428 void operator delete(void *, size_t) {}
429
430 virtual ~AbstractUnwindCursor() {}
431 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
432 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
433 virtual void setReg(int, unw_word_t) {
434 _LIBUNWIND_ABORT("setReg not implemented");
435 }
436 virtual bool validFloatReg(int) {
437 _LIBUNWIND_ABORT("validFloatReg not implemented");
438 }
439 virtual unw_fpreg_t getFloatReg(int) {
440 _LIBUNWIND_ABORT("getFloatReg not implemented");
441 }
442 virtual void setFloatReg(int, unw_fpreg_t) {
443 _LIBUNWIND_ABORT("setFloatReg not implemented");
444 }
445 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
446 virtual void getInfo(unw_proc_info_t *) {
447 _LIBUNWIND_ABORT("getInfo not implemented");
448 }
449 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
450 virtual bool isSignalFrame() {
451 _LIBUNWIND_ABORT("isSignalFrame not implemented");
452 }
453 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
454 _LIBUNWIND_ABORT("getFunctionName not implemented");
455 }
456 virtual void setInfoBasedOnIPRegister(bool = false) {
457 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
458 }
459 virtual const char *getRegisterName(int) {
460 _LIBUNWIND_ABORT("getRegisterName not implemented");
461 }
462#ifdef __arm__
463 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
464#endif
gejin7f493162021-08-26 16:20:38 +0800465
Xing Xuebbcbce92022-04-13 11:01:59 -0400466#ifdef _AIX
467 virtual uintptr_t getDataRelBase() {
468 _LIBUNWIND_ABORT("getDataRelBase not implemented");
469 }
470#endif
471
gejin7f493162021-08-26 16:20:38 +0800472#if defined(_LIBUNWIND_USE_CET)
473 virtual void *get_registers() {
474 _LIBUNWIND_ABORT("get_registers not implemented");
475 }
476#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000477};
478
Charles Davisfa2e6202018-08-30 21:29:00 +0000479#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
480
481/// \c UnwindCursor contains all state (including all register values) during
482/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
483template <typename A, typename R>
484class UnwindCursor : public AbstractUnwindCursor {
485 typedef typename A::pint_t pint_t;
486public:
487 UnwindCursor(unw_context_t *context, A &as);
488 UnwindCursor(CONTEXT *context, A &as);
489 UnwindCursor(A &as, void *threadArg);
490 virtual ~UnwindCursor() {}
491 virtual bool validReg(int);
492 virtual unw_word_t getReg(int);
493 virtual void setReg(int, unw_word_t);
494 virtual bool validFloatReg(int);
495 virtual unw_fpreg_t getFloatReg(int);
496 virtual void setFloatReg(int, unw_fpreg_t);
497 virtual int step();
498 virtual void getInfo(unw_proc_info_t *);
499 virtual void jumpto();
500 virtual bool isSignalFrame();
501 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
502 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
503 virtual const char *getRegisterName(int num);
504#ifdef __arm__
505 virtual void saveVFPAsX();
506#endif
507
508 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
509 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
510
Martin Storsjo5f5036e2019-02-03 22:16:53 +0000511 // libunwind does not and should not depend on C++ library which means that we
Gabriel Ravier42aa6de2022-08-20 18:09:03 -0700512 // need our own definition of inline placement new.
Martin Storsjo5f5036e2019-02-03 22:16:53 +0000513 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
514
Charles Davisfa2e6202018-08-30 21:29:00 +0000515private:
516
517 pint_t getLastPC() const { return _dispContext.ControlPc; }
518 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
519 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
Martin Storsjö1644d072022-05-11 11:20:38 +0300520#ifdef __arm__
521 // Remove the thumb bit; FunctionEntry ranges don't include the thumb bit.
522 pc &= ~1U;
523#endif
524 // If pc points exactly at the end of the range, we might resolve the
525 // next function instead. Decrement pc by 1 to fit inside the current
526 // function.
527 pc -= 1;
Charles Davisfa2e6202018-08-30 21:29:00 +0000528 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
529 &_dispContext.ImageBase,
530 _dispContext.HistoryTable);
531 *base = _dispContext.ImageBase;
532 return _dispContext.FunctionEntry;
533 }
534 bool getInfoFromSEH(pint_t pc);
535 int stepWithSEHData() {
536 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
537 _dispContext.ImageBase,
538 _dispContext.ControlPc,
539 _dispContext.FunctionEntry,
540 _dispContext.ContextRecord,
541 &_dispContext.HandlerData,
542 &_dispContext.EstablisherFrame,
543 NULL);
544 // Update some fields of the unwind info now, since we have them.
545 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
546 if (_dispContext.LanguageHandler) {
547 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
548 } else
549 _info.handler = 0;
550 return UNW_STEP_SUCCESS;
551 }
552
553 A &_addressSpace;
554 unw_proc_info_t _info;
555 DISPATCHER_CONTEXT _dispContext;
556 CONTEXT _msContext;
557 UNWIND_HISTORY_TABLE _histTable;
558 bool _unwindInfoMissing;
559};
560
561
562template <typename A, typename R>
563UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
564 : _addressSpace(as), _unwindInfoMissing(false) {
565 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
566 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +0300567 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
568 "UnwindCursor<> requires more alignment than unw_cursor_t");
Charles Davisfa2e6202018-08-30 21:29:00 +0000569 memset(&_info, 0, sizeof(_info));
570 memset(&_histTable, 0, sizeof(_histTable));
571 _dispContext.ContextRecord = &_msContext;
572 _dispContext.HistoryTable = &_histTable;
573 // Initialize MS context from ours.
574 R r(context);
575 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
576#if defined(_LIBUNWIND_TARGET_X86_64)
577 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
578 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
579 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
580 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
581 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
582 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
583 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
584 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
585 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
586 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
587 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
588 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
589 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
590 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
591 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
592 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
593 _msContext.Rip = r.getRegister(UNW_REG_IP);
594 union {
595 v128 v;
596 M128A m;
597 } t;
598 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
599 _msContext.Xmm0 = t.m;
600 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
601 _msContext.Xmm1 = t.m;
602 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
603 _msContext.Xmm2 = t.m;
604 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
605 _msContext.Xmm3 = t.m;
606 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
607 _msContext.Xmm4 = t.m;
608 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
609 _msContext.Xmm5 = t.m;
610 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
611 _msContext.Xmm6 = t.m;
612 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
613 _msContext.Xmm7 = t.m;
614 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
615 _msContext.Xmm8 = t.m;
616 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
617 _msContext.Xmm9 = t.m;
618 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
619 _msContext.Xmm10 = t.m;
620 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
621 _msContext.Xmm11 = t.m;
622 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
623 _msContext.Xmm12 = t.m;
624 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
625 _msContext.Xmm13 = t.m;
626 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
627 _msContext.Xmm14 = t.m;
628 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
629 _msContext.Xmm15 = t.m;
630#elif defined(_LIBUNWIND_TARGET_ARM)
631 _msContext.R0 = r.getRegister(UNW_ARM_R0);
632 _msContext.R1 = r.getRegister(UNW_ARM_R1);
633 _msContext.R2 = r.getRegister(UNW_ARM_R2);
634 _msContext.R3 = r.getRegister(UNW_ARM_R3);
635 _msContext.R4 = r.getRegister(UNW_ARM_R4);
636 _msContext.R5 = r.getRegister(UNW_ARM_R5);
637 _msContext.R6 = r.getRegister(UNW_ARM_R6);
638 _msContext.R7 = r.getRegister(UNW_ARM_R7);
639 _msContext.R8 = r.getRegister(UNW_ARM_R8);
640 _msContext.R9 = r.getRegister(UNW_ARM_R9);
641 _msContext.R10 = r.getRegister(UNW_ARM_R10);
642 _msContext.R11 = r.getRegister(UNW_ARM_R11);
643 _msContext.R12 = r.getRegister(UNW_ARM_R12);
644 _msContext.Sp = r.getRegister(UNW_ARM_SP);
645 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000646 _msContext.Pc = r.getRegister(UNW_ARM_IP);
647 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000648 union {
649 uint64_t w;
650 double d;
651 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000652 d.d = r.getFloatRegister(i);
653 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000654 }
Martin Storsjoce150112018-12-18 20:05:59 +0000655#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700656 for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
657 _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
Martin Storsjoce150112018-12-18 20:05:59 +0000658 _msContext.Sp = r.getRegister(UNW_REG_SP);
659 _msContext.Pc = r.getRegister(UNW_REG_IP);
Fangrui Song5f263002021-08-20 14:26:27 -0700660 for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
661 _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000662#endif
663}
664
665template <typename A, typename R>
666UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
667 : _addressSpace(as), _unwindInfoMissing(false) {
668 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
669 "UnwindCursor<> does not fit in unw_cursor_t");
670 memset(&_info, 0, sizeof(_info));
671 memset(&_histTable, 0, sizeof(_histTable));
672 _dispContext.ContextRecord = &_msContext;
673 _dispContext.HistoryTable = &_histTable;
674 _msContext = *context;
675}
676
677
678template <typename A, typename R>
679bool UnwindCursor<A, R>::validReg(int regNum) {
680 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
681#if defined(_LIBUNWIND_TARGET_X86_64)
682 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
683#elif defined(_LIBUNWIND_TARGET_ARM)
Ties Stuijc8c0ec92021-12-08 09:44:45 +0000684 if ((regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) ||
685 regNum == UNW_ARM_RA_AUTH_CODE)
686 return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000687#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700688 if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000689#endif
690 return false;
691}
692
693template <typename A, typename R>
694unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
695 switch (regNum) {
696#if defined(_LIBUNWIND_TARGET_X86_64)
697 case UNW_REG_IP: return _msContext.Rip;
698 case UNW_X86_64_RAX: return _msContext.Rax;
699 case UNW_X86_64_RDX: return _msContext.Rdx;
700 case UNW_X86_64_RCX: return _msContext.Rcx;
701 case UNW_X86_64_RBX: return _msContext.Rbx;
702 case UNW_REG_SP:
703 case UNW_X86_64_RSP: return _msContext.Rsp;
704 case UNW_X86_64_RBP: return _msContext.Rbp;
705 case UNW_X86_64_RSI: return _msContext.Rsi;
706 case UNW_X86_64_RDI: return _msContext.Rdi;
707 case UNW_X86_64_R8: return _msContext.R8;
708 case UNW_X86_64_R9: return _msContext.R9;
709 case UNW_X86_64_R10: return _msContext.R10;
710 case UNW_X86_64_R11: return _msContext.R11;
711 case UNW_X86_64_R12: return _msContext.R12;
712 case UNW_X86_64_R13: return _msContext.R13;
713 case UNW_X86_64_R14: return _msContext.R14;
714 case UNW_X86_64_R15: return _msContext.R15;
715#elif defined(_LIBUNWIND_TARGET_ARM)
716 case UNW_ARM_R0: return _msContext.R0;
717 case UNW_ARM_R1: return _msContext.R1;
718 case UNW_ARM_R2: return _msContext.R2;
719 case UNW_ARM_R3: return _msContext.R3;
720 case UNW_ARM_R4: return _msContext.R4;
721 case UNW_ARM_R5: return _msContext.R5;
722 case UNW_ARM_R6: return _msContext.R6;
723 case UNW_ARM_R7: return _msContext.R7;
724 case UNW_ARM_R8: return _msContext.R8;
725 case UNW_ARM_R9: return _msContext.R9;
726 case UNW_ARM_R10: return _msContext.R10;
727 case UNW_ARM_R11: return _msContext.R11;
728 case UNW_ARM_R12: return _msContext.R12;
729 case UNW_REG_SP:
730 case UNW_ARM_SP: return _msContext.Sp;
731 case UNW_ARM_LR: return _msContext.Lr;
732 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000733 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000734#elif defined(_LIBUNWIND_TARGET_AARCH64)
735 case UNW_REG_SP: return _msContext.Sp;
736 case UNW_REG_IP: return _msContext.Pc;
Fangrui Song5f263002021-08-20 14:26:27 -0700737 default: return _msContext.X[regNum - UNW_AARCH64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000738#endif
739 }
740 _LIBUNWIND_ABORT("unsupported register");
741}
742
743template <typename A, typename R>
744void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
745 switch (regNum) {
746#if defined(_LIBUNWIND_TARGET_X86_64)
747 case UNW_REG_IP: _msContext.Rip = value; break;
748 case UNW_X86_64_RAX: _msContext.Rax = value; break;
749 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
750 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
751 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
752 case UNW_REG_SP:
753 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
754 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
755 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
756 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
757 case UNW_X86_64_R8: _msContext.R8 = value; break;
758 case UNW_X86_64_R9: _msContext.R9 = value; break;
759 case UNW_X86_64_R10: _msContext.R10 = value; break;
760 case UNW_X86_64_R11: _msContext.R11 = value; break;
761 case UNW_X86_64_R12: _msContext.R12 = value; break;
762 case UNW_X86_64_R13: _msContext.R13 = value; break;
763 case UNW_X86_64_R14: _msContext.R14 = value; break;
764 case UNW_X86_64_R15: _msContext.R15 = value; break;
765#elif defined(_LIBUNWIND_TARGET_ARM)
766 case UNW_ARM_R0: _msContext.R0 = value; break;
767 case UNW_ARM_R1: _msContext.R1 = value; break;
768 case UNW_ARM_R2: _msContext.R2 = value; break;
769 case UNW_ARM_R3: _msContext.R3 = value; break;
770 case UNW_ARM_R4: _msContext.R4 = value; break;
771 case UNW_ARM_R5: _msContext.R5 = value; break;
772 case UNW_ARM_R6: _msContext.R6 = value; break;
773 case UNW_ARM_R7: _msContext.R7 = value; break;
774 case UNW_ARM_R8: _msContext.R8 = value; break;
775 case UNW_ARM_R9: _msContext.R9 = value; break;
776 case UNW_ARM_R10: _msContext.R10 = value; break;
777 case UNW_ARM_R11: _msContext.R11 = value; break;
778 case UNW_ARM_R12: _msContext.R12 = value; break;
779 case UNW_REG_SP:
780 case UNW_ARM_SP: _msContext.Sp = value; break;
781 case UNW_ARM_LR: _msContext.Lr = value; break;
782 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000783 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000784#elif defined(_LIBUNWIND_TARGET_AARCH64)
785 case UNW_REG_SP: _msContext.Sp = value; break;
786 case UNW_REG_IP: _msContext.Pc = value; break;
Fangrui Song5f263002021-08-20 14:26:27 -0700787 case UNW_AARCH64_X0:
788 case UNW_AARCH64_X1:
789 case UNW_AARCH64_X2:
790 case UNW_AARCH64_X3:
791 case UNW_AARCH64_X4:
792 case UNW_AARCH64_X5:
793 case UNW_AARCH64_X6:
794 case UNW_AARCH64_X7:
795 case UNW_AARCH64_X8:
796 case UNW_AARCH64_X9:
797 case UNW_AARCH64_X10:
798 case UNW_AARCH64_X11:
799 case UNW_AARCH64_X12:
800 case UNW_AARCH64_X13:
801 case UNW_AARCH64_X14:
802 case UNW_AARCH64_X15:
803 case UNW_AARCH64_X16:
804 case UNW_AARCH64_X17:
805 case UNW_AARCH64_X18:
806 case UNW_AARCH64_X19:
807 case UNW_AARCH64_X20:
808 case UNW_AARCH64_X21:
809 case UNW_AARCH64_X22:
810 case UNW_AARCH64_X23:
811 case UNW_AARCH64_X24:
812 case UNW_AARCH64_X25:
813 case UNW_AARCH64_X26:
814 case UNW_AARCH64_X27:
815 case UNW_AARCH64_X28:
816 case UNW_AARCH64_FP:
817 case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000818#endif
819 default:
820 _LIBUNWIND_ABORT("unsupported register");
821 }
822}
823
824template <typename A, typename R>
825bool UnwindCursor<A, R>::validFloatReg(int regNum) {
826#if defined(_LIBUNWIND_TARGET_ARM)
827 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
828 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000829#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700830 if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000831#else
832 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000833#endif
834 return false;
835}
836
837template <typename A, typename R>
838unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
839#if defined(_LIBUNWIND_TARGET_ARM)
840 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
841 union {
842 uint32_t w;
843 float f;
844 } d;
845 d.w = _msContext.S[regNum - UNW_ARM_S0];
846 return d.f;
847 }
848 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
849 union {
850 uint64_t w;
851 double d;
852 } d;
853 d.w = _msContext.D[regNum - UNW_ARM_D0];
854 return d.d;
855 }
856 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000857#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700858 return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000859#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000860 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000861 _LIBUNWIND_ABORT("float registers unimplemented");
862#endif
863}
864
865template <typename A, typename R>
866void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
867#if defined(_LIBUNWIND_TARGET_ARM)
868 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
869 union {
870 uint32_t w;
871 float f;
872 } d;
Martin Storsjö1c11cfe2022-05-07 00:26:45 +0300873 d.f = (float)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000874 _msContext.S[regNum - UNW_ARM_S0] = d.w;
875 }
876 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
877 union {
878 uint64_t w;
879 double d;
880 } d;
881 d.d = value;
882 _msContext.D[regNum - UNW_ARM_D0] = d.w;
883 }
884 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000885#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700886 _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000887#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000888 (void)regNum;
889 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000890 _LIBUNWIND_ABORT("float registers unimplemented");
891#endif
892}
893
894template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
895 RtlRestoreContext(&_msContext, nullptr);
896}
897
898#ifdef __arm__
899template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
900#endif
901
902template <typename A, typename R>
903const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000904 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000905}
906
907template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
908 return false;
909}
910
911#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
912
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000913/// UnwindCursor contains all state (including all register values) during
914/// an unwind. This is normally stack allocated inside a unw_cursor_t.
915template <typename A, typename R>
916class UnwindCursor : public AbstractUnwindCursor{
917 typedef typename A::pint_t pint_t;
918public:
919 UnwindCursor(unw_context_t *context, A &as);
920 UnwindCursor(A &as, void *threadArg);
921 virtual ~UnwindCursor() {}
922 virtual bool validReg(int);
923 virtual unw_word_t getReg(int);
924 virtual void setReg(int, unw_word_t);
925 virtual bool validFloatReg(int);
926 virtual unw_fpreg_t getFloatReg(int);
927 virtual void setFloatReg(int, unw_fpreg_t);
928 virtual int step();
929 virtual void getInfo(unw_proc_info_t *);
930 virtual void jumpto();
931 virtual bool isSignalFrame();
932 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
933 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
934 virtual const char *getRegisterName(int num);
935#ifdef __arm__
936 virtual void saveVFPAsX();
937#endif
938
Xing Xuebbcbce92022-04-13 11:01:59 -0400939#ifdef _AIX
940 virtual uintptr_t getDataRelBase();
941#endif
942
gejin7f493162021-08-26 16:20:38 +0800943#if defined(_LIBUNWIND_USE_CET)
944 virtual void *get_registers() { return &_registers; }
945#endif
Xing Xuebbcbce92022-04-13 11:01:59 -0400946
Petr Hosek36f61542019-02-02 21:15:49 +0000947 // libunwind does not and should not depend on C++ library which means that we
Gabriel Ravier42aa6de2022-08-20 18:09:03 -0700948 // need our own definition of inline placement new.
Petr Hosek36f61542019-02-02 21:15:49 +0000949 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
950
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000951private:
952
Ranjeet Singh421231a2017-03-31 15:28:06 +0000953#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000954 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000955
956 int stepWithEHABI() {
957 size_t len = 0;
958 size_t off = 0;
959 // FIXME: Calling decode_eht_entry() here is violating the libunwind
960 // abstraction layer.
961 const uint32_t *ehtp =
962 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
963 &off, &len);
964 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
965 _URC_CONTINUE_UNWIND)
966 return UNW_STEP_END;
967 return UNW_STEP_SUCCESS;
968 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000969#endif
970
Shoaib Meenai67bace72022-05-23 22:11:34 -0700971#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
Ryan Pricharda684bed2021-01-13 16:38:36 -0800972 bool setInfoForSigReturn() {
973 R dummy;
974 return setInfoForSigReturn(dummy);
975 }
976 int stepThroughSigReturn() {
977 R dummy;
978 return stepThroughSigReturn(dummy);
979 }
Shoaib Meenai67bace72022-05-23 22:11:34 -0700980#if defined(_LIBUNWIND_TARGET_AARCH64)
Ryan Pricharda684bed2021-01-13 16:38:36 -0800981 bool setInfoForSigReturn(Registers_arm64 &);
982 int stepThroughSigReturn(Registers_arm64 &);
Shoaib Meenai67bace72022-05-23 22:11:34 -0700983#endif
984#if defined(_LIBUNWIND_TARGET_S390X)
Ulrich Weigandf1108b62022-05-04 10:43:11 +0200985 bool setInfoForSigReturn(Registers_s390x &);
986 int stepThroughSigReturn(Registers_s390x &);
Shoaib Meenai67bace72022-05-23 22:11:34 -0700987#endif
Ryan Pricharda684bed2021-01-13 16:38:36 -0800988 template <typename Registers> bool setInfoForSigReturn(Registers &) {
989 return false;
990 }
991 template <typename Registers> int stepThroughSigReturn(Registers &) {
992 return UNW_STEP_END;
993 }
994#endif
995
Ranjeet Singh421231a2017-03-31 15:28:06 +0000996#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Prichard1ae2b942020-08-18 02:31:38 -0700997 bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
998 const typename CFI_Parser<A>::CIE_Info &cieInfo,
999 pint_t pc, uintptr_t dso_base);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001000 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
1001 uint32_t fdeSectionOffsetHint=0);
1002 int stepWithDwarfFDE() {
1003 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
1004 (pint_t)this->getReg(UNW_REG_IP),
1005 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -07001006 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001007 }
1008#endif
1009
Ranjeet Singh421231a2017-03-31 15:28:06 +00001010#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001011 bool getInfoFromCompactEncodingSection(pint_t pc,
1012 const UnwindInfoSections &sects);
1013 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001014 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001015 if ( compactSaysUseDwarf() )
1016 return stepWithDwarfFDE();
1017 #endif
1018 R dummy;
1019 return stepWithCompactEncoding(dummy);
1020 }
1021
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001022#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001023 int stepWithCompactEncoding(Registers_x86_64 &) {
1024 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
1025 _info.format, _info.start_ip, _addressSpace, _registers);
1026 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001027#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001028
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001029#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001030 int stepWithCompactEncoding(Registers_x86 &) {
1031 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
1032 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
1033 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001034#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001035
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001036#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001037 int stepWithCompactEncoding(Registers_ppc &) {
1038 return UNW_EINVAL;
1039 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001040#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001041
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001042#if defined(_LIBUNWIND_TARGET_PPC64)
1043 int stepWithCompactEncoding(Registers_ppc64 &) {
1044 return UNW_EINVAL;
1045 }
1046#endif
1047
1048
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001049#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001050 int stepWithCompactEncoding(Registers_arm64 &) {
1051 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
1052 _info.format, _info.start_ip, _addressSpace, _registers);
1053 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001054#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001055
John Baldwin56441d42017-12-12 21:43:36 +00001056#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1057 int stepWithCompactEncoding(Registers_mips_o32 &) {
1058 return UNW_EINVAL;
1059 }
1060#endif
1061
John Baldwin541a4352018-01-09 17:07:18 +00001062#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1063 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +00001064 return UNW_EINVAL;
1065 }
1066#endif
1067
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001068#if defined(_LIBUNWIND_TARGET_SPARC)
1069 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1070#endif
1071
Koakumaf2ef96e2022-02-05 13:08:26 -08001072#if defined(_LIBUNWIND_TARGET_SPARC64)
1073 int stepWithCompactEncoding(Registers_sparc64 &) { return UNW_EINVAL; }
1074#endif
1075
Sam Elliott81f7e172019-12-16 16:35:17 +00001076#if defined (_LIBUNWIND_TARGET_RISCV)
1077 int stepWithCompactEncoding(Registers_riscv &) {
1078 return UNW_EINVAL;
1079 }
1080#endif
1081
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001082 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1083 R dummy;
1084 return compactSaysUseDwarf(dummy, offset);
1085 }
1086
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001087#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001088 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1089 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1090 if (offset)
1091 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1092 return true;
1093 }
1094 return false;
1095 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001096#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001097
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001098#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001099 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1100 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1101 if (offset)
1102 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1103 return true;
1104 }
1105 return false;
1106 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001107#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001108
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001109#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001110 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1111 return true;
1112 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001113#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001114
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001115#if defined(_LIBUNWIND_TARGET_PPC64)
1116 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1117 return true;
1118 }
1119#endif
1120
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001121#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001122 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1123 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1124 if (offset)
1125 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1126 return true;
1127 }
1128 return false;
1129 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001130#endif
John Baldwin56441d42017-12-12 21:43:36 +00001131
1132#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1133 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1134 return true;
1135 }
1136#endif
1137
John Baldwin541a4352018-01-09 17:07:18 +00001138#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1139 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001140 return true;
1141 }
1142#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001143
1144#if defined(_LIBUNWIND_TARGET_SPARC)
1145 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1146#endif
1147
Koakumaf2ef96e2022-02-05 13:08:26 -08001148#if defined(_LIBUNWIND_TARGET_SPARC64)
1149 bool compactSaysUseDwarf(Registers_sparc64 &, uint32_t *) const {
1150 return true;
1151 }
1152#endif
1153
Sam Elliott81f7e172019-12-16 16:35:17 +00001154#if defined (_LIBUNWIND_TARGET_RISCV)
1155 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1156 return true;
1157 }
1158#endif
1159
Ranjeet Singh421231a2017-03-31 15:28:06 +00001160#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001161
Ranjeet Singh421231a2017-03-31 15:28:06 +00001162#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001163 compact_unwind_encoding_t dwarfEncoding() const {
1164 R dummy;
1165 return dwarfEncoding(dummy);
1166 }
1167
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001168#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001169 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1170 return UNWIND_X86_64_MODE_DWARF;
1171 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001172#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001173
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001174#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001175 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1176 return UNWIND_X86_MODE_DWARF;
1177 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001178#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001179
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001180#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001181 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1182 return 0;
1183 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001184#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001185
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001186#if defined(_LIBUNWIND_TARGET_PPC64)
1187 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1188 return 0;
1189 }
1190#endif
1191
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001192#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001193 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1194 return UNWIND_ARM64_MODE_DWARF;
1195 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001196#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001197
Martin Storsjoa72285f2017-11-02 08:16:16 +00001198#if defined(_LIBUNWIND_TARGET_ARM)
1199 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1200 return 0;
1201 }
1202#endif
1203
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001204#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001205 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1206 return 0;
1207 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001208#endif
John Baldwin56441d42017-12-12 21:43:36 +00001209
Brian Cainb432c472020-04-09 00:14:02 -05001210#if defined (_LIBUNWIND_TARGET_HEXAGON)
1211 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1212 return 0;
1213 }
1214#endif
1215
John Baldwin56441d42017-12-12 21:43:36 +00001216#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1217 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1218 return 0;
1219 }
1220#endif
1221
John Baldwin541a4352018-01-09 17:07:18 +00001222#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1223 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001224 return 0;
1225 }
1226#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001227
1228#if defined(_LIBUNWIND_TARGET_SPARC)
1229 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1230#endif
1231
Koakumaf2ef96e2022-02-05 13:08:26 -08001232#if defined(_LIBUNWIND_TARGET_SPARC64)
1233 compact_unwind_encoding_t dwarfEncoding(Registers_sparc64 &) const {
1234 return 0;
1235 }
1236#endif
1237
Sam Elliott81f7e172019-12-16 16:35:17 +00001238#if defined (_LIBUNWIND_TARGET_RISCV)
1239 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1240 return 0;
1241 }
1242#endif
1243
Ulrich Weigand393e3ee2022-05-02 14:35:29 +02001244#if defined (_LIBUNWIND_TARGET_S390X)
1245 compact_unwind_encoding_t dwarfEncoding(Registers_s390x &) const {
1246 return 0;
1247 }
1248#endif
1249
Ranjeet Singh421231a2017-03-31 15:28:06 +00001250#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001251
Charles Davisfa2e6202018-08-30 21:29:00 +00001252#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1253 // For runtime environments using SEH unwind data without Windows runtime
1254 // support.
1255 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1256 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1257 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1258 /* FIXME: Implement */
1259 *base = 0;
1260 return nullptr;
1261 }
1262 bool getInfoFromSEH(pint_t pc);
1263 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1264#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1265
Xing Xuebbcbce92022-04-13 11:01:59 -04001266#if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
1267 bool getInfoFromTBTable(pint_t pc, R &registers);
1268 int stepWithTBTable(pint_t pc, tbtable *TBTable, R &registers,
1269 bool &isSignalFrame);
1270 int stepWithTBTableData() {
1271 return stepWithTBTable(reinterpret_cast<pint_t>(this->getReg(UNW_REG_IP)),
1272 reinterpret_cast<tbtable *>(_info.unwind_info),
1273 _registers, _isSignalFrame);
1274 }
1275#endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001276
1277 A &_addressSpace;
1278 R _registers;
1279 unw_proc_info_t _info;
1280 bool _unwindInfoMissing;
1281 bool _isSignalFrame;
Shoaib Meenai67bace72022-05-23 22:11:34 -07001282#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
Ryan Pricharda684bed2021-01-13 16:38:36 -08001283 bool _isSigReturn = false;
1284#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001285};
1286
1287
1288template <typename A, typename R>
1289UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1290 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1291 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001292 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001293 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001294 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1295 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001296 memset(&_info, 0, sizeof(_info));
1297}
1298
1299template <typename A, typename R>
1300UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1301 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1302 memset(&_info, 0, sizeof(_info));
1303 // FIXME
1304 // fill in _registers from thread arg
1305}
1306
1307
1308template <typename A, typename R>
1309bool UnwindCursor<A, R>::validReg(int regNum) {
1310 return _registers.validRegister(regNum);
1311}
1312
1313template <typename A, typename R>
1314unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1315 return _registers.getRegister(regNum);
1316}
1317
1318template <typename A, typename R>
1319void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1320 _registers.setRegister(regNum, (typename A::pint_t)value);
1321}
1322
1323template <typename A, typename R>
1324bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1325 return _registers.validFloatRegister(regNum);
1326}
1327
1328template <typename A, typename R>
1329unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1330 return _registers.getFloatRegister(regNum);
1331}
1332
1333template <typename A, typename R>
1334void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1335 _registers.setFloatRegister(regNum, value);
1336}
1337
1338template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1339 _registers.jumpto();
1340}
1341
1342#ifdef __arm__
1343template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1344 _registers.saveVFPAsX();
1345}
1346#endif
1347
Xing Xuebbcbce92022-04-13 11:01:59 -04001348#ifdef _AIX
1349template <typename A, typename R>
1350uintptr_t UnwindCursor<A, R>::getDataRelBase() {
1351 return reinterpret_cast<uintptr_t>(_info.extra);
1352}
1353#endif
1354
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001355template <typename A, typename R>
1356const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1357 return _registers.getRegisterName(regNum);
1358}
1359
1360template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1361 return _isSignalFrame;
1362}
1363
Charles Davisfa2e6202018-08-30 21:29:00 +00001364#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1365
Ranjeet Singh421231a2017-03-31 15:28:06 +00001366#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001367template<typename A>
1368struct EHABISectionIterator {
1369 typedef EHABISectionIterator _Self;
1370
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001371 typedef typename A::pint_t value_type;
1372 typedef typename A::pint_t* pointer;
1373 typedef typename A::pint_t& reference;
1374 typedef size_t size_type;
1375 typedef size_t difference_type;
1376
1377 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1378 return _Self(addressSpace, sects, 0);
1379 }
1380 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001381 return _Self(addressSpace, sects,
1382 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001383 }
1384
1385 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1386 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1387
1388 _Self& operator++() { ++_i; return *this; }
1389 _Self& operator+=(size_t a) { _i += a; return *this; }
1390 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1391 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1392
1393 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1394 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1395
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001396 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001397
1398 bool operator==(const _Self& other) const {
1399 assert(_addressSpace == other._addressSpace);
1400 assert(_sects == other._sects);
1401 return _i == other._i;
1402 }
1403
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001404 bool operator!=(const _Self& other) const {
1405 assert(_addressSpace == other._addressSpace);
1406 assert(_sects == other._sects);
1407 return _i != other._i;
1408 }
1409
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001410 typename A::pint_t operator*() const { return functionAddress(); }
1411
1412 typename A::pint_t functionAddress() const {
1413 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1414 EHABIIndexEntry, _i, functionOffset);
1415 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1416 }
1417
1418 typename A::pint_t dataAddress() {
1419 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1420 EHABIIndexEntry, _i, data);
1421 return indexAddr;
1422 }
1423
1424 private:
1425 size_t _i;
1426 A* _addressSpace;
1427 const UnwindInfoSections* _sects;
1428};
1429
Petr Hosekac0d9e02019-01-29 22:26:18 +00001430namespace {
1431
1432template <typename A>
1433EHABISectionIterator<A> EHABISectionUpperBound(
1434 EHABISectionIterator<A> first,
1435 EHABISectionIterator<A> last,
1436 typename A::pint_t value) {
1437 size_t len = last - first;
1438 while (len > 0) {
1439 size_t l2 = len / 2;
1440 EHABISectionIterator<A> m = first + l2;
1441 if (value < *m) {
1442 len = l2;
1443 } else {
1444 first = ++m;
1445 len -= l2 + 1;
1446 }
1447 }
1448 return first;
1449}
1450
1451}
1452
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001453template <typename A, typename R>
1454bool UnwindCursor<A, R>::getInfoFromEHABISection(
1455 pint_t pc,
1456 const UnwindInfoSections &sects) {
1457 EHABISectionIterator<A> begin =
1458 EHABISectionIterator<A>::begin(_addressSpace, sects);
1459 EHABISectionIterator<A> end =
1460 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001461 if (begin == end)
1462 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001463
Petr Hosekac0d9e02019-01-29 22:26:18 +00001464 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001465 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001466 return false;
1467 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1468
1469 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001470 // If an exception is thrown from a function, corresponding to the last entry
1471 // in the table, we don't really know the function extent and have to choose a
1472 // value for nextPC. Choosing max() will allow the range check during trace to
1473 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001474 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001475 pint_t indexDataAddr = itThisPC.dataAddress();
1476
1477 if (indexDataAddr == 0)
1478 return false;
1479
1480 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1481 if (indexData == UNW_EXIDX_CANTUNWIND)
1482 return false;
1483
1484 // If the high bit is set, the exception handling table entry is inline inside
1485 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001486 // the table points at an offset in the exception handling table (section 5
1487 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001488 pint_t exceptionTableAddr;
1489 uint32_t exceptionTableData;
1490 bool isSingleWordEHT;
1491 if (indexData & 0x80000000) {
1492 exceptionTableAddr = indexDataAddr;
1493 // TODO(ajwong): Should this data be 0?
1494 exceptionTableData = indexData;
1495 isSingleWordEHT = true;
1496 } else {
1497 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1498 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1499 isSingleWordEHT = false;
1500 }
1501
1502 // Now we know the 3 things:
1503 // exceptionTableAddr -- exception handler table entry.
1504 // exceptionTableData -- the data inside the first word of the eht entry.
1505 // isSingleWordEHT -- whether the entry is in the index.
1506 unw_word_t personalityRoutine = 0xbadf00d;
1507 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001508 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001509
1510 // If the high bit in the exception handling table entry is set, the entry is
1511 // in compact form (section 6.3 EHABI).
1512 if (exceptionTableData & 0x80000000) {
1513 // Grab the index of the personality routine from the compact form.
1514 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1515 uint32_t extraWords = 0;
1516 switch (choice) {
1517 case 0:
1518 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1519 extraWords = 0;
1520 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001521 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001522 break;
1523 case 1:
1524 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1525 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1526 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001527 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001528 break;
1529 case 2:
1530 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1531 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1532 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001533 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001534 break;
1535 default:
1536 _LIBUNWIND_ABORT("unknown personality routine");
1537 return false;
1538 }
1539
1540 if (isSingleWordEHT) {
1541 if (extraWords != 0) {
1542 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1543 "requires extra words");
1544 return false;
1545 }
1546 }
1547 } else {
1548 pint_t personalityAddr =
1549 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1550 personalityRoutine = personalityAddr;
1551
1552 // ARM EHABI # 6.2, # 9.2
1553 //
1554 // +---- ehtp
1555 // v
1556 // +--------------------------------------+
1557 // | +--------+--------+--------+-------+ |
1558 // | |0| prel31 to personalityRoutine | |
1559 // | +--------+--------+--------+-------+ |
1560 // | | N | unwind opcodes | | <-- UnwindData
1561 // | +--------+--------+--------+-------+ |
1562 // | | Word 2 unwind opcodes | |
1563 // | +--------+--------+--------+-------+ |
1564 // | ... |
1565 // | +--------+--------+--------+-------+ |
1566 // | | Word N unwind opcodes | |
1567 // | +--------+--------+--------+-------+ |
1568 // | | LSDA | | <-- lsda
1569 // | | ... | |
1570 // | +--------+--------+--------+-------+ |
1571 // +--------------------------------------+
1572
1573 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1574 uint32_t FirstDataWord = *UnwindData;
1575 size_t N = ((FirstDataWord >> 24) & 0xff);
1576 size_t NDataWords = N + 1;
1577 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1578 }
1579
1580 _info.start_ip = thisPC;
1581 _info.end_ip = nextPC;
1582 _info.handler = personalityRoutine;
1583 _info.unwind_info = exceptionTableAddr;
1584 _info.lsda = lsda;
1585 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001586 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001587
1588 return true;
1589}
1590#endif
1591
Ranjeet Singh421231a2017-03-31 15:28:06 +00001592#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001593template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001594bool UnwindCursor<A, R>::getInfoFromFdeCie(
1595 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1596 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1597 uintptr_t dso_base) {
1598 typename CFI_Parser<A>::PrologInfo prolog;
1599 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1600 R::getArch(), &prolog)) {
1601 // Save off parsed FDE info
1602 _info.start_ip = fdeInfo.pcStart;
1603 _info.end_ip = fdeInfo.pcEnd;
1604 _info.lsda = fdeInfo.lsda;
1605 _info.handler = cieInfo.personality;
1606 // Some frameless functions need SP altered when resuming in function, so
1607 // propagate spExtraArgSize.
1608 _info.gp = prolog.spExtraArgSize;
1609 _info.flags = 0;
1610 _info.format = dwarfEncoding();
1611 _info.unwind_info = fdeInfo.fdeStart;
1612 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1613 _info.extra = static_cast<unw_word_t>(dso_base);
1614 return true;
1615 }
1616 return false;
1617}
1618
1619template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001620bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1621 const UnwindInfoSections &sects,
1622 uint32_t fdeSectionOffsetHint) {
1623 typename CFI_Parser<A>::FDE_Info fdeInfo;
1624 typename CFI_Parser<A>::CIE_Info cieInfo;
1625 bool foundFDE = false;
1626 bool foundInCache = false;
1627 // If compact encoding table gave offset into dwarf section, go directly there
1628 if (fdeSectionOffsetHint != 0) {
1629 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001630 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001631 sects.dwarf_section + fdeSectionOffsetHint,
1632 &fdeInfo, &cieInfo);
1633 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001634#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001635 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1636 foundFDE = EHHeaderParser<A>::findFDE(
1637 _addressSpace, pc, sects.dwarf_index_section,
1638 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1639 }
1640#endif
1641 if (!foundFDE) {
1642 // otherwise, search cache of previously found FDEs.
1643 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1644 if (cachedFDE != 0) {
1645 foundFDE =
1646 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001647 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001648 cachedFDE, &fdeInfo, &cieInfo);
1649 foundInCache = foundFDE;
1650 }
1651 }
1652 if (!foundFDE) {
1653 // Still not found, do full scan of __eh_frame section.
1654 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001655 sects.dwarf_section_length, 0,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001656 &fdeInfo, &cieInfo);
1657 }
1658 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001659 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001660 // Add to cache (to make next lookup faster) if we had no hint
1661 // and there was no index.
1662 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001663 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001664 if (sects.dwarf_index_section == 0)
1665 #endif
1666 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1667 fdeInfo.fdeStart);
1668 }
1669 return true;
1670 }
1671 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001672 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001673 return false;
1674}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001675#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001676
1677
Ranjeet Singh421231a2017-03-31 15:28:06 +00001678#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001679template <typename A, typename R>
1680bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1681 const UnwindInfoSections &sects) {
1682 const bool log = false;
1683 if (log)
1684 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1685 (uint64_t)pc, (uint64_t)sects.dso_base);
1686
1687 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1688 sects.compact_unwind_section);
1689 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1690 return false;
1691
1692 // do a binary search of top level index to find page with unwind info
1693 pint_t targetFunctionOffset = pc - sects.dso_base;
1694 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1695 sects.compact_unwind_section
1696 + sectionHeader.indexSectionOffset());
1697 uint32_t low = 0;
1698 uint32_t high = sectionHeader.indexCount();
1699 uint32_t last = high - 1;
1700 while (low < high) {
1701 uint32_t mid = (low + high) / 2;
1702 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1703 //mid, low, high, topIndex.functionOffset(mid));
1704 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1705 if ((mid == last) ||
1706 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1707 low = mid;
1708 break;
1709 } else {
1710 low = mid + 1;
1711 }
1712 } else {
1713 high = mid;
1714 }
1715 }
1716 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1717 const uint32_t firstLevelNextPageFunctionOffset =
1718 topIndex.functionOffset(low + 1);
1719 const pint_t secondLevelAddr =
1720 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1721 const pint_t lsdaArrayStartAddr =
1722 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1723 const pint_t lsdaArrayEndAddr =
1724 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1725 if (log)
1726 fprintf(stderr, "\tfirst level search for result index=%d "
1727 "to secondLevelAddr=0x%llX\n",
1728 low, (uint64_t) secondLevelAddr);
1729 // do a binary search of second level page index
1730 uint32_t encoding = 0;
1731 pint_t funcStart = 0;
1732 pint_t funcEnd = 0;
1733 pint_t lsda = 0;
1734 pint_t personality = 0;
1735 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1736 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1737 // regular page
1738 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1739 secondLevelAddr);
1740 UnwindSectionRegularArray<A> pageIndex(
1741 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1742 // binary search looks for entry with e where index[e].offset <= pc <
1743 // index[e+1].offset
1744 if (log)
1745 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1746 "regular page starting at secondLevelAddr=0x%llX\n",
1747 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1748 low = 0;
1749 high = pageHeader.entryCount();
1750 while (low < high) {
1751 uint32_t mid = (low + high) / 2;
1752 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1753 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1754 // at end of table
1755 low = mid;
1756 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1757 break;
1758 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1759 // next is too big, so we found it
1760 low = mid;
1761 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1762 break;
1763 } else {
1764 low = mid + 1;
1765 }
1766 } else {
1767 high = mid;
1768 }
1769 }
1770 encoding = pageIndex.encoding(low);
1771 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1772 if (pc < funcStart) {
1773 if (log)
1774 fprintf(
1775 stderr,
1776 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1777 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1778 return false;
1779 }
1780 if (pc > funcEnd) {
1781 if (log)
1782 fprintf(
1783 stderr,
1784 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1785 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1786 return false;
1787 }
1788 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1789 // compressed page
1790 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1791 secondLevelAddr);
1792 UnwindSectionCompressedArray<A> pageIndex(
1793 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1794 const uint32_t targetFunctionPageOffset =
1795 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1796 // binary search looks for entry with e where index[e].offset <= pc <
1797 // index[e+1].offset
1798 if (log)
1799 fprintf(stderr, "\tbinary search of compressed page starting at "
1800 "secondLevelAddr=0x%llX\n",
1801 (uint64_t) secondLevelAddr);
1802 low = 0;
1803 last = pageHeader.entryCount() - 1;
1804 high = pageHeader.entryCount();
1805 while (low < high) {
1806 uint32_t mid = (low + high) / 2;
1807 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1808 if ((mid == last) ||
1809 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1810 low = mid;
1811 break;
1812 } else {
1813 low = mid + 1;
1814 }
1815 } else {
1816 high = mid;
1817 }
1818 }
1819 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1820 + sects.dso_base;
1821 if (low < last)
1822 funcEnd =
1823 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1824 + sects.dso_base;
1825 else
1826 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1827 if (pc < funcStart) {
Nico Weber5f424e32021-07-02 10:03:32 -04001828 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1829 "not in second level compressed unwind table. "
1830 "funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001831 (uint64_t) pc, (uint64_t) funcStart);
1832 return false;
1833 }
1834 if (pc > funcEnd) {
Nico Weber5f424e32021-07-02 10:03:32 -04001835 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1836 "not in second level compressed unwind table. "
1837 "funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001838 (uint64_t) pc, (uint64_t) funcEnd);
1839 return false;
1840 }
1841 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1842 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1843 // encoding is in common table in section header
1844 encoding = _addressSpace.get32(
1845 sects.compact_unwind_section +
1846 sectionHeader.commonEncodingsArraySectionOffset() +
1847 encodingIndex * sizeof(uint32_t));
1848 } else {
1849 // encoding is in page specific table
1850 uint16_t pageEncodingIndex =
1851 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1852 encoding = _addressSpace.get32(secondLevelAddr +
1853 pageHeader.encodingsPageOffset() +
1854 pageEncodingIndex * sizeof(uint32_t));
1855 }
1856 } else {
Nico Weber5f424e32021-07-02 10:03:32 -04001857 _LIBUNWIND_DEBUG_LOG(
1858 "malformed __unwind_info at 0x%0llX bad second level page",
1859 (uint64_t)sects.compact_unwind_section);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001860 return false;
1861 }
1862
1863 // look up LSDA, if encoding says function has one
1864 if (encoding & UNWIND_HAS_LSDA) {
1865 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1866 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1867 low = 0;
1868 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1869 sizeof(unwind_info_section_header_lsda_index_entry);
1870 // binary search looks for entry with exact match for functionOffset
1871 if (log)
1872 fprintf(stderr,
1873 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1874 funcStartOffset);
1875 while (low < high) {
1876 uint32_t mid = (low + high) / 2;
1877 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1878 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1879 break;
1880 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1881 low = mid + 1;
1882 } else {
1883 high = mid;
1884 }
1885 }
1886 if (lsda == 0) {
1887 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001888 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001889 encoding, (uint64_t) pc);
1890 return false;
1891 }
1892 }
1893
Louis Dionned3bbbc32020-08-11 15:24:21 -04001894 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001895 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1896 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1897 if (personalityIndex != 0) {
1898 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001899 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001900 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001901 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001902 encoding, personalityIndex,
1903 sectionHeader.personalityArrayCount());
1904 return false;
1905 }
1906 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1907 sects.compact_unwind_section +
1908 sectionHeader.personalityArraySectionOffset() +
1909 personalityIndex * sizeof(uint32_t));
1910 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1911 personality = _addressSpace.getP(personalityPointer);
1912 if (log)
1913 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1914 "personalityDelta=0x%08X, personality=0x%08llX\n",
1915 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1916 }
1917
1918 if (log)
1919 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1920 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1921 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1922 _info.start_ip = funcStart;
1923 _info.end_ip = funcEnd;
1924 _info.lsda = lsda;
1925 _info.handler = personality;
1926 _info.gp = 0;
1927 _info.flags = 0;
1928 _info.format = encoding;
1929 _info.unwind_info = 0;
1930 _info.unwind_info_size = 0;
1931 _info.extra = sects.dso_base;
1932 return true;
1933}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001934#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001935
1936
Charles Davisfa2e6202018-08-30 21:29:00 +00001937#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1938template <typename A, typename R>
1939bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1940 pint_t base;
1941 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1942 if (!unwindEntry) {
1943 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1944 return false;
1945 }
1946 _info.gp = 0;
1947 _info.flags = 0;
1948 _info.format = 0;
1949 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1950 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1951 _info.extra = base;
1952 _info.start_ip = base + unwindEntry->BeginAddress;
1953#ifdef _LIBUNWIND_TARGET_X86_64
1954 _info.end_ip = base + unwindEntry->EndAddress;
1955 // Only fill in the handler and LSDA if they're stale.
1956 if (pc != getLastPC()) {
1957 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1958 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1959 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1960 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1961 // these structures.)
1962 // N.B. UNWIND_INFO structs are DWORD-aligned.
1963 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1964 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1965 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1966 if (*handler) {
1967 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1968 } else
1969 _info.handler = 0;
1970 } else {
1971 _info.lsda = 0;
1972 _info.handler = 0;
1973 }
1974 }
Charles Davisfa2e6202018-08-30 21:29:00 +00001975#endif
1976 setLastPC(pc);
1977 return true;
1978}
1979#endif
1980
Xing Xuebbcbce92022-04-13 11:01:59 -04001981#if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
1982// Masks for traceback table field xtbtable.
1983enum xTBTableMask : uint8_t {
1984 reservedBit = 0x02, // The traceback table was incorrectly generated if set
1985 // (see comments in function getInfoFromTBTable().
1986 ehInfoBit = 0x08 // Exception handling info is present if set
1987};
1988
1989enum frameType : unw_word_t {
1990 frameWithXLEHStateTable = 0,
1991 frameWithEHInfo = 1
1992};
1993
1994extern "C" {
1995typedef _Unwind_Reason_Code __xlcxx_personality_v0_t(int, _Unwind_Action,
1996 uint64_t,
1997 _Unwind_Exception *,
1998 struct _Unwind_Context *);
1999__attribute__((__weak__)) __xlcxx_personality_v0_t __xlcxx_personality_v0;
2000}
2001
2002static __xlcxx_personality_v0_t *xlcPersonalityV0;
2003static RWMutex xlcPersonalityV0InitLock;
2004
2005template <typename A, typename R>
2006bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R &registers) {
2007 uint32_t *p = reinterpret_cast<uint32_t *>(pc);
2008
2009 // Keep looking forward until a word of 0 is found. The traceback
2010 // table starts at the following word.
2011 while (*p)
2012 ++p;
2013 tbtable *TBTable = reinterpret_cast<tbtable *>(p + 1);
2014
2015 if (_LIBUNWIND_TRACING_UNWINDING) {
2016 char functionBuf[512];
2017 const char *functionName = functionBuf;
2018 unw_word_t offset;
2019 if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
2020 functionName = ".anonymous.";
2021 }
2022 _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
2023 __func__, functionName,
2024 reinterpret_cast<void *>(TBTable));
2025 }
2026
2027 // If the traceback table does not contain necessary info, bypass this frame.
2028 if (!TBTable->tb.has_tboff)
2029 return false;
2030
2031 // Structure tbtable_ext contains important data we are looking for.
2032 p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
2033
2034 // Skip field parminfo if it exists.
2035 if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
2036 ++p;
2037
2038 // p now points to tb_offset, the offset from start of function to TB table.
2039 unw_word_t start_ip =
2040 reinterpret_cast<unw_word_t>(TBTable) - *p - sizeof(uint32_t);
2041 unw_word_t end_ip = reinterpret_cast<unw_word_t>(TBTable);
2042 ++p;
2043
2044 _LIBUNWIND_TRACE_UNWINDING("start_ip=%p, end_ip=%p\n",
2045 reinterpret_cast<void *>(start_ip),
2046 reinterpret_cast<void *>(end_ip));
2047
2048 // Skip field hand_mask if it exists.
2049 if (TBTable->tb.int_hndl)
2050 ++p;
2051
2052 unw_word_t lsda = 0;
2053 unw_word_t handler = 0;
2054 unw_word_t flags = frameType::frameWithXLEHStateTable;
2055
2056 if (TBTable->tb.lang == TB_CPLUSPLUS && TBTable->tb.has_ctl) {
2057 // State table info is available. The ctl_info field indicates the
2058 // number of CTL anchors. There should be only one entry for the C++
2059 // state table.
2060 assert(*p == 1 && "libunwind: there must be only one ctl_info entry");
2061 ++p;
2062 // p points to the offset of the state table into the stack.
2063 pint_t stateTableOffset = *p++;
2064
2065 int framePointerReg;
2066
2067 // Skip fields name_len and name if exist.
2068 if (TBTable->tb.name_present) {
2069 const uint16_t name_len = *(reinterpret_cast<uint16_t *>(p));
2070 p = reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(p) + name_len +
2071 sizeof(uint16_t));
2072 }
2073
2074 if (TBTable->tb.uses_alloca)
2075 framePointerReg = *(reinterpret_cast<char *>(p));
2076 else
2077 framePointerReg = 1; // default frame pointer == SP
2078
2079 _LIBUNWIND_TRACE_UNWINDING(
2080 "framePointerReg=%d, framePointer=%p, "
2081 "stateTableOffset=%#lx\n",
2082 framePointerReg,
2083 reinterpret_cast<void *>(_registers.getRegister(framePointerReg)),
2084 stateTableOffset);
2085 lsda = _registers.getRegister(framePointerReg) + stateTableOffset;
2086
2087 // Since the traceback table generated by the legacy XLC++ does not
2088 // provide the location of the personality for the state table,
2089 // function __xlcxx_personality_v0(), which is the personality for the state
2090 // table and is exported from libc++abi, is directly assigned as the
2091 // handler here. When a legacy XLC++ frame is encountered, the symbol
2092 // is resolved dynamically using dlopen() to avoid hard dependency from
2093 // libunwind on libc++abi.
2094
2095 // Resolve the function pointer to the state table personality if it has
2096 // not already.
2097 if (xlcPersonalityV0 == NULL) {
2098 xlcPersonalityV0InitLock.lock();
2099 if (xlcPersonalityV0 == NULL) {
2100 // If libc++abi is statically linked in, symbol __xlcxx_personality_v0
2101 // has been resolved at the link time.
2102 xlcPersonalityV0 = &__xlcxx_personality_v0;
2103 if (xlcPersonalityV0 == NULL) {
2104 // libc++abi is dynamically linked. Resolve __xlcxx_personality_v0
2105 // using dlopen().
2106 const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)";
2107 void *libHandle;
Xing Xuec38cbd42022-08-08 17:21:30 -04002108 // The AIX dlopen() sets errno to 0 when it is successful, which
2109 // clobbers the value of errno from the user code. This is an AIX
2110 // bug because according to POSIX it should not set errno to 0. To
2111 // workaround before AIX fixes the bug, errno is saved and restored.
2112 int saveErrno = errno;
Xing Xuebbcbce92022-04-13 11:01:59 -04002113 libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW);
2114 if (libHandle == NULL) {
2115 _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n",
2116 errno);
2117 assert(0 && "dlopen() failed");
2118 }
2119 xlcPersonalityV0 = reinterpret_cast<__xlcxx_personality_v0_t *>(
2120 dlsym(libHandle, "__xlcxx_personality_v0"));
2121 if (xlcPersonalityV0 == NULL) {
2122 _LIBUNWIND_TRACE_UNWINDING("dlsym() failed with errno=%d\n", errno);
2123 assert(0 && "dlsym() failed");
2124 }
2125 dlclose(libHandle);
Xing Xuec38cbd42022-08-08 17:21:30 -04002126 errno = saveErrno;
Xing Xuebbcbce92022-04-13 11:01:59 -04002127 }
2128 }
2129 xlcPersonalityV0InitLock.unlock();
2130 }
2131 handler = reinterpret_cast<unw_word_t>(xlcPersonalityV0);
2132 _LIBUNWIND_TRACE_UNWINDING("State table: LSDA=%p, Personality=%p\n",
2133 reinterpret_cast<void *>(lsda),
2134 reinterpret_cast<void *>(handler));
2135 } else if (TBTable->tb.longtbtable) {
2136 // This frame has the traceback table extension. Possible cases are
2137 // 1) a C++ frame that has the 'eh_info' structure; 2) a C++ frame that
2138 // is not EH aware; or, 3) a frame of other languages. We need to figure out
2139 // if the traceback table extension contains the 'eh_info' structure.
2140 //
2141 // We also need to deal with the complexity arising from some XL compiler
2142 // versions use the wrong ordering of 'longtbtable' and 'has_vec' bits
2143 // where the 'longtbtable' bit is meant to be the 'has_vec' bit and vice
2144 // versa. For frames of code generated by those compilers, the 'longtbtable'
2145 // bit may be set but there isn't really a traceback table extension.
2146 //
2147 // In </usr/include/sys/debug.h>, there is the following definition of
2148 // 'struct tbtable_ext'. It is not really a structure but a dummy to
2149 // collect the description of optional parts of the traceback table.
2150 //
2151 // struct tbtable_ext {
2152 // ...
2153 // char alloca_reg; /* Register for alloca automatic storage */
2154 // struct vec_ext vec_ext; /* Vector extension (if has_vec is set) */
2155 // unsigned char xtbtable; /* More tbtable fields, if longtbtable is set*/
2156 // };
2157 //
2158 // Depending on how the 'has_vec'/'longtbtable' bit is interpreted, the data
2159 // following 'alloca_reg' can be treated either as 'struct vec_ext' or
2160 // 'unsigned char xtbtable'. 'xtbtable' bits are defined in
2161 // </usr/include/sys/debug.h> as flags. The 7th bit '0x02' is currently
2162 // unused and should not be set. 'struct vec_ext' is defined in
2163 // </usr/include/sys/debug.h> as follows:
2164 //
2165 // struct vec_ext {
2166 // unsigned vr_saved:6; /* Number of non-volatile vector regs saved
2167 // */
2168 // /* first register saved is assumed to be */
2169 // /* 32 - vr_saved */
2170 // unsigned saves_vrsave:1; /* Set if vrsave is saved on the stack */
2171 // unsigned has_varargs:1;
2172 // ...
2173 // };
2174 //
2175 // Here, the 7th bit is used as 'saves_vrsave'. To determine whether it
2176 // is 'struct vec_ext' or 'xtbtable' that follows 'alloca_reg',
2177 // we checks if the 7th bit is set or not because 'xtbtable' should
2178 // never have the 7th bit set. The 7th bit of 'xtbtable' will be reserved
2179 // in the future to make sure the mitigation works. This mitigation
2180 // is not 100% bullet proof because 'struct vec_ext' may not always have
2181 // 'saves_vrsave' bit set.
2182 //
2183 // 'reservedBit' is defined in enum 'xTBTableMask' above as the mask for
2184 // checking the 7th bit.
2185
2186 // p points to field name len.
2187 uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
2188
2189 // Skip fields name_len and name if they exist.
2190 if (TBTable->tb.name_present) {
2191 const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
2192 charPtr = charPtr + name_len + sizeof(uint16_t);
2193 }
2194
2195 // Skip field alloc_reg if it exists.
2196 if (TBTable->tb.uses_alloca)
2197 ++charPtr;
2198
2199 // Check traceback table bit has_vec. Skip struct vec_ext if it exists.
2200 if (TBTable->tb.has_vec)
2201 // Note struct vec_ext does exist at this point because whether the
2202 // ordering of longtbtable and has_vec bits is correct or not, both
2203 // are set.
2204 charPtr += sizeof(struct vec_ext);
2205
2206 // charPtr points to field 'xtbtable'. Check if the EH info is available.
2207 // Also check if the reserved bit of the extended traceback table field
2208 // 'xtbtable' is set. If it is, the traceback table was incorrectly
2209 // generated by an XL compiler that uses the wrong ordering of 'longtbtable'
2210 // and 'has_vec' bits and this is in fact 'struct vec_ext'. So skip the
2211 // frame.
2212 if ((*charPtr & xTBTableMask::ehInfoBit) &&
2213 !(*charPtr & xTBTableMask::reservedBit)) {
2214 // Mark this frame has the new EH info.
2215 flags = frameType::frameWithEHInfo;
2216
2217 // eh_info is available.
2218 charPtr++;
2219 // The pointer is 4-byte aligned.
2220 if (reinterpret_cast<uintptr_t>(charPtr) % 4)
2221 charPtr += 4 - reinterpret_cast<uintptr_t>(charPtr) % 4;
2222 uintptr_t *ehInfo =
2223 reinterpret_cast<uintptr_t *>(*(reinterpret_cast<uintptr_t *>(
2224 registers.getRegister(2) +
2225 *(reinterpret_cast<uintptr_t *>(charPtr)))));
2226
2227 // ehInfo points to structure en_info. The first member is version.
2228 // Only version 0 is currently supported.
2229 assert(*(reinterpret_cast<uint32_t *>(ehInfo)) == 0 &&
2230 "libunwind: ehInfo version other than 0 is not supported");
2231
2232 // Increment ehInfo to point to member lsda.
2233 ++ehInfo;
2234 lsda = *ehInfo++;
2235
2236 // enInfo now points to member personality.
2237 handler = *ehInfo;
2238
2239 _LIBUNWIND_TRACE_UNWINDING("Range table: LSDA=%#lx, Personality=%#lx\n",
2240 lsda, handler);
2241 }
2242 }
2243
2244 _info.start_ip = start_ip;
2245 _info.end_ip = end_ip;
2246 _info.lsda = lsda;
2247 _info.handler = handler;
2248 _info.gp = 0;
2249 _info.flags = flags;
2250 _info.format = 0;
2251 _info.unwind_info = reinterpret_cast<unw_word_t>(TBTable);
2252 _info.unwind_info_size = 0;
2253 _info.extra = registers.getRegister(2);
2254
2255 return true;
2256}
2257
2258// Step back up the stack following the frame back link.
2259template <typename A, typename R>
2260int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
2261 R &registers, bool &isSignalFrame) {
2262 if (_LIBUNWIND_TRACING_UNWINDING) {
2263 char functionBuf[512];
2264 const char *functionName = functionBuf;
2265 unw_word_t offset;
2266 if (!getFunctionName(functionBuf, sizeof(functionBuf), &offset)) {
2267 functionName = ".anonymous.";
2268 }
2269 _LIBUNWIND_TRACE_UNWINDING("%s: Look up traceback table of func=%s at %p",
2270 __func__, functionName,
2271 reinterpret_cast<void *>(TBTable));
2272 }
2273
2274#if defined(__powerpc64__)
2275 // Instruction to reload TOC register "l r2,40(r1)"
2276 const uint32_t loadTOCRegInst = 0xe8410028;
2277 const int32_t unwPPCF0Index = UNW_PPC64_F0;
2278 const int32_t unwPPCV0Index = UNW_PPC64_V0;
2279#else
2280 // Instruction to reload TOC register "l r2,20(r1)"
2281 const uint32_t loadTOCRegInst = 0x80410014;
2282 const int32_t unwPPCF0Index = UNW_PPC_F0;
2283 const int32_t unwPPCV0Index = UNW_PPC_V0;
2284#endif
2285
2286 R newRegisters = registers;
2287
2288 // lastStack points to the stack frame of the next routine up.
2289 pint_t lastStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
2290
2291 // Return address is the address after call site instruction.
2292 pint_t returnAddress;
2293
2294 if (isSignalFrame) {
2295 _LIBUNWIND_TRACE_UNWINDING("Possible signal handler frame: lastStack=%p",
2296 reinterpret_cast<void *>(lastStack));
2297
2298 sigcontext *sigContext = reinterpret_cast<sigcontext *>(
2299 reinterpret_cast<char *>(lastStack) + STKMIN);
2300 returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
2301
2302 _LIBUNWIND_TRACE_UNWINDING("From sigContext=%p, returnAddress=%p\n",
2303 reinterpret_cast<void *>(sigContext),
2304 reinterpret_cast<void *>(returnAddress));
2305
2306 if (returnAddress < 0x10000000) {
2307 // Try again using STKMINALIGN
2308 sigContext = reinterpret_cast<sigcontext *>(
2309 reinterpret_cast<char *>(lastStack) + STKMINALIGN);
2310 returnAddress = sigContext->sc_jmpbuf.jmp_context.iar;
2311 if (returnAddress < 0x10000000) {
2312 _LIBUNWIND_TRACE_UNWINDING("Bad returnAddress=%p\n",
2313 reinterpret_cast<void *>(returnAddress));
2314 return UNW_EBADFRAME;
2315 } else {
2316 _LIBUNWIND_TRACE_UNWINDING("Tried again using STKMINALIGN: "
2317 "sigContext=%p, returnAddress=%p. "
2318 "Seems to be a valid address\n",
2319 reinterpret_cast<void *>(sigContext),
2320 reinterpret_cast<void *>(returnAddress));
2321 }
2322 }
2323 // Restore the condition register from sigcontext.
2324 newRegisters.setCR(sigContext->sc_jmpbuf.jmp_context.cr);
2325
2326 // Restore GPRs from sigcontext.
2327 for (int i = 0; i < 32; ++i)
2328 newRegisters.setRegister(i, sigContext->sc_jmpbuf.jmp_context.gpr[i]);
2329
2330 // Restore FPRs from sigcontext.
2331 for (int i = 0; i < 32; ++i)
2332 newRegisters.setFloatRegister(i + unwPPCF0Index,
2333 sigContext->sc_jmpbuf.jmp_context.fpr[i]);
2334
2335 // Restore vector registers if there is an associated extended context
2336 // structure.
2337 if (sigContext->sc_jmpbuf.jmp_context.msr & __EXTCTX) {
2338 ucontext_t *uContext = reinterpret_cast<ucontext_t *>(sigContext);
2339 if (uContext->__extctx->__extctx_magic == __EXTCTX_MAGIC) {
2340 for (int i = 0; i < 32; ++i)
2341 newRegisters.setVectorRegister(
2342 i + unwPPCV0Index, *(reinterpret_cast<v128 *>(
2343 &(uContext->__extctx->__vmx.__vr[i]))));
2344 }
2345 }
2346 } else {
2347 // Step up a normal frame.
2348 returnAddress = reinterpret_cast<pint_t *>(lastStack)[2];
2349
2350 _LIBUNWIND_TRACE_UNWINDING("Extract info from lastStack=%p, "
2351 "returnAddress=%p\n",
2352 reinterpret_cast<void *>(lastStack),
2353 reinterpret_cast<void *>(returnAddress));
2354 _LIBUNWIND_TRACE_UNWINDING("fpr_regs=%d, gpr_regs=%d, saves_cr=%d\n",
2355 TBTable->tb.fpr_saved, TBTable->tb.gpr_saved,
2356 TBTable->tb.saves_cr);
2357
2358 // Restore FP registers.
2359 char *ptrToRegs = reinterpret_cast<char *>(lastStack);
2360 double *FPRegs = reinterpret_cast<double *>(
2361 ptrToRegs - (TBTable->tb.fpr_saved * sizeof(double)));
2362 for (int i = 0; i < TBTable->tb.fpr_saved; ++i)
2363 newRegisters.setFloatRegister(
2364 32 - TBTable->tb.fpr_saved + i + unwPPCF0Index, FPRegs[i]);
2365
2366 // Restore GP registers.
2367 ptrToRegs = reinterpret_cast<char *>(FPRegs);
2368 uintptr_t *GPRegs = reinterpret_cast<uintptr_t *>(
2369 ptrToRegs - (TBTable->tb.gpr_saved * sizeof(uintptr_t)));
2370 for (int i = 0; i < TBTable->tb.gpr_saved; ++i)
2371 newRegisters.setRegister(32 - TBTable->tb.gpr_saved + i, GPRegs[i]);
2372
2373 // Restore Vector registers.
2374 ptrToRegs = reinterpret_cast<char *>(GPRegs);
2375
2376 // Restore vector registers only if this is a Clang frame. Also
2377 // check if traceback table bit has_vec is set. If it is, structure
2378 // vec_ext is available.
2379 if (_info.flags == frameType::frameWithEHInfo && TBTable->tb.has_vec) {
2380
2381 // Get to the vec_ext structure to check if vector registers are saved.
2382 uint32_t *p = reinterpret_cast<uint32_t *>(&TBTable->tb_ext);
2383
2384 // Skip field parminfo if exists.
2385 if (TBTable->tb.fixedparms || TBTable->tb.floatparms)
2386 ++p;
2387
2388 // Skip field tb_offset if exists.
2389 if (TBTable->tb.has_tboff)
2390 ++p;
2391
2392 // Skip field hand_mask if exists.
2393 if (TBTable->tb.int_hndl)
2394 ++p;
2395
2396 // Skip fields ctl_info and ctl_info_disp if exist.
2397 if (TBTable->tb.has_ctl) {
2398 // Skip field ctl_info.
2399 ++p;
2400 // Skip field ctl_info_disp.
2401 ++p;
2402 }
2403
2404 // Skip fields name_len and name if exist.
2405 // p is supposed to point to field name_len now.
2406 uint8_t *charPtr = reinterpret_cast<uint8_t *>(p);
2407 if (TBTable->tb.name_present) {
2408 const uint16_t name_len = *(reinterpret_cast<uint16_t *>(charPtr));
2409 charPtr = charPtr + name_len + sizeof(uint16_t);
2410 }
2411
2412 // Skip field alloc_reg if it exists.
2413 if (TBTable->tb.uses_alloca)
2414 ++charPtr;
2415
2416 struct vec_ext *vec_ext = reinterpret_cast<struct vec_ext *>(charPtr);
2417
2418 _LIBUNWIND_TRACE_UNWINDING("vr_saved=%d\n", vec_ext->vr_saved);
2419
2420 // Restore vector register(s) if saved on the stack.
2421 if (vec_ext->vr_saved) {
2422 // Saved vector registers are 16-byte aligned.
2423 if (reinterpret_cast<uintptr_t>(ptrToRegs) % 16)
2424 ptrToRegs -= reinterpret_cast<uintptr_t>(ptrToRegs) % 16;
2425 v128 *VecRegs = reinterpret_cast<v128 *>(ptrToRegs - vec_ext->vr_saved *
2426 sizeof(v128));
2427 for (int i = 0; i < vec_ext->vr_saved; ++i) {
2428 newRegisters.setVectorRegister(
2429 32 - vec_ext->vr_saved + i + unwPPCV0Index, VecRegs[i]);
2430 }
2431 }
2432 }
2433 if (TBTable->tb.saves_cr) {
2434 // Get the saved condition register. The condition register is only
2435 // a single word.
2436 newRegisters.setCR(
2437 *(reinterpret_cast<uint32_t *>(lastStack + sizeof(uintptr_t))));
2438 }
2439
2440 // Restore the SP.
2441 newRegisters.setSP(lastStack);
2442
2443 // The first instruction after return.
2444 uint32_t firstInstruction = *(reinterpret_cast<uint32_t *>(returnAddress));
2445
2446 // Do we need to set the TOC register?
2447 _LIBUNWIND_TRACE_UNWINDING(
2448 "Current gpr2=%p\n",
2449 reinterpret_cast<void *>(newRegisters.getRegister(2)));
2450 if (firstInstruction == loadTOCRegInst) {
2451 _LIBUNWIND_TRACE_UNWINDING(
2452 "Set gpr2=%p from frame\n",
2453 reinterpret_cast<void *>(reinterpret_cast<pint_t *>(lastStack)[5]));
2454 newRegisters.setRegister(2, reinterpret_cast<pint_t *>(lastStack)[5]);
2455 }
2456 }
2457 _LIBUNWIND_TRACE_UNWINDING("lastStack=%p, returnAddress=%p, pc=%p\n",
2458 reinterpret_cast<void *>(lastStack),
2459 reinterpret_cast<void *>(returnAddress),
2460 reinterpret_cast<void *>(pc));
2461
2462 // The return address is the address after call site instruction, so
Gabriel Ravier42aa6de2022-08-20 18:09:03 -07002463 // setting IP to that simulates a return.
Xing Xuebbcbce92022-04-13 11:01:59 -04002464 newRegisters.setIP(reinterpret_cast<uintptr_t>(returnAddress));
2465
2466 // Simulate the step by replacing the register set with the new ones.
2467 registers = newRegisters;
2468
2469 // Check if the next frame is a signal frame.
2470 pint_t nextStack = *(reinterpret_cast<pint_t *>(registers.getSP()));
2471
2472 // Return address is the address after call site instruction.
2473 pint_t nextReturnAddress = reinterpret_cast<pint_t *>(nextStack)[2];
2474
2475 if (nextReturnAddress > 0x01 && nextReturnAddress < 0x10000) {
2476 _LIBUNWIND_TRACE_UNWINDING("The next is a signal handler frame: "
2477 "nextStack=%p, next return address=%p\n",
2478 reinterpret_cast<void *>(nextStack),
2479 reinterpret_cast<void *>(nextReturnAddress));
2480 isSignalFrame = true;
2481 } else {
2482 isSignalFrame = false;
2483 }
2484
2485 return UNW_STEP_SUCCESS;
2486}
2487#endif // defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
Charles Davisfa2e6202018-08-30 21:29:00 +00002488
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002489template <typename A, typename R>
2490void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
Shoaib Meenai67bace72022-05-23 22:11:34 -07002491#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002492 _isSigReturn = false;
2493#endif
2494
2495 pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Ranjeet Singh421231a2017-03-31 15:28:06 +00002496#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002497 // Remove the thumb bit so the IP represents the actual instruction address.
2498 // This matches the behaviour of _Unwind_GetIP on arm.
2499 pc &= (pint_t)~0x1;
2500#endif
2501
Sterling Augustinec100c4d2020-03-30 15:20:26 -07002502 // Exit early if at the top of the stack.
2503 if (pc == 0) {
2504 _unwindInfoMissing = true;
2505 return;
2506 }
2507
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002508 // If the last line of a function is a "throw" the compiler sometimes
2509 // emits no instructions after the call to __cxa_throw. This means
2510 // the return address is actually the start of the next function.
2511 // To disambiguate this, back up the pc when we know it is a return
2512 // address.
2513 if (isReturnAddress)
Xing Xuebbcbce92022-04-13 11:01:59 -04002514#if defined(_AIX)
2515 // PC needs to be a 4-byte aligned address to be able to look for a
2516 // word of 0 that indicates the start of the traceback table at the end
2517 // of a function on AIX.
2518 pc -= 4;
2519#else
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002520 --pc;
Xing Xuebbcbce92022-04-13 11:01:59 -04002521#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002522
2523 // Ask address space object to find unwind sections for this pc.
2524 UnwindInfoSections sects;
2525 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002526#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002527 // If there is a compact unwind encoding table, look there first.
2528 if (sects.compact_unwind_section != 0) {
2529 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002530 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002531 // Found info in table, done unless encoding says to use dwarf.
2532 uint32_t dwarfOffset;
2533 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
2534 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
2535 // found info in dwarf, done
2536 return;
2537 }
2538 }
2539 #endif
2540 // If unwind table has entry, but entry says there is no unwind info,
2541 // record that we have no unwind info.
2542 if (_info.format == 0)
2543 _unwindInfoMissing = true;
2544 return;
2545 }
2546 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00002547#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002548
Charles Davisfa2e6202018-08-30 21:29:00 +00002549#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
2550 // If there is SEH unwind info, look there next.
2551 if (this->getInfoFromSEH(pc))
2552 return;
2553#endif
2554
Xing Xuebbcbce92022-04-13 11:01:59 -04002555#if defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
2556 // If there is unwind info in the traceback table, look there next.
2557 if (this->getInfoFromTBTable(pc, _registers))
2558 return;
2559#endif
2560
Ranjeet Singh421231a2017-03-31 15:28:06 +00002561#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002562 // If there is dwarf unwind info, look there next.
2563 if (sects.dwarf_section != 0) {
2564 if (this->getInfoFromDwarfSection(pc, sects)) {
2565 // found info in dwarf, done
2566 return;
2567 }
2568 }
2569#endif
2570
Ranjeet Singh421231a2017-03-31 15:28:06 +00002571#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002572 // If there is ARM EHABI unwind info, look there next.
2573 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
2574 return;
2575#endif
2576 }
2577
Ranjeet Singh421231a2017-03-31 15:28:06 +00002578#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002579 // There is no static unwind info for this pc. Look to see if an FDE was
2580 // dynamically registered for it.
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -07002581 pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
2582 pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002583 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002584 typename CFI_Parser<A>::FDE_Info fdeInfo;
2585 typename CFI_Parser<A>::CIE_Info cieInfo;
2586 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
2587 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002588 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002589 }
2590
2591 // Lastly, ask AddressSpace object about platform specific ways to locate
2592 // other FDEs.
2593 pint_t fde;
2594 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002595 typename CFI_Parser<A>::FDE_Info fdeInfo;
2596 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002597 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
2598 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07002599 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
2600 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002601 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002602 }
2603 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00002604#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002605
Shoaib Meenai67bace72022-05-23 22:11:34 -07002606#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002607 if (setInfoForSigReturn())
2608 return;
2609#endif
2610
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002611 // no unwind info, flag that we can't reliably unwind
2612 _unwindInfoMissing = true;
2613}
2614
Shoaib Meenai67bace72022-05-23 22:11:34 -07002615#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \
2616 defined(_LIBUNWIND_TARGET_AARCH64)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002617template <typename A, typename R>
2618bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2619 // Look for the sigreturn trampoline. The trampoline's body is two
2620 // specific instructions (see below). Typically the trampoline comes from the
2621 // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2622 // own restorer function, though, or user-mode QEMU might write a trampoline
2623 // onto the stack.
2624 //
2625 // This special code path is a fallback that is only used if the trampoline
2626 // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2627 // constant for the PC needs to be defined before DWARF can handle a signal
2628 // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2629 // - The PC points at a function compiled without unwind info, and which is
2630 // part of an execute-only mapping (e.g. using -Wl,--execute-only).
2631 // - The PC is invalid and happens to point to unreadable or unmapped memory.
2632 //
2633 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2634 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Shoaib Meenai8c606b02022-05-25 21:39:12 -07002635 // The PC might contain an invalid address if the unwind info is bad, so
2636 // directly accessing it could cause a segfault. Use process_vm_readv to read
2637 // the memory safely instead. process_vm_readv was added in Linux 3.2, and
2638 // AArch64 supported was added in Linux 3.7, so the syscall is guaranteed to
2639 // be present. Unfortunately, there are Linux AArch64 environments where the
2640 // libc wrapper for the syscall might not be present (e.g. Android 5), so call
2641 // the syscall directly instead.
2642 uint32_t instructions[2];
2643 struct iovec local_iov = {&instructions, sizeof instructions};
2644 struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions};
2645 long bytesRead =
2646 syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0);
Ryan Pricharda684bed2021-01-13 16:38:36 -08002647 // Look for instructions: mov x8, #0x8b; svc #0x0
Shoaib Meenai8c606b02022-05-25 21:39:12 -07002648 if (bytesRead != sizeof instructions || instructions[0] != 0xd2801168 ||
2649 instructions[1] != 0xd4000001)
2650 return false;
2651
2652 _info = {};
2653 _info.start_ip = pc;
2654 _info.end_ip = pc + 4;
2655 _isSigReturn = true;
2656 return true;
Ryan Pricharda684bed2021-01-13 16:38:36 -08002657}
2658
2659template <typename A, typename R>
2660int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2661 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2662 // - 128-byte siginfo struct
2663 // - ucontext struct:
2664 // - 8-byte long (uc_flags)
2665 // - 8-byte pointer (uc_link)
2666 // - 24-byte stack_t
2667 // - 128-byte signal set
2668 // - 8 bytes of padding because sigcontext has 16-byte alignment
2669 // - sigcontext/mcontext_t
2670 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2671 const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2672
2673 // Offsets from sigcontext to each register.
2674 const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2675 const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2676 const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2677
2678 pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2679
2680 for (int i = 0; i <= 30; ++i) {
2681 uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2682 static_cast<pint_t>(i * 8));
Fangrui Song5f263002021-08-20 14:26:27 -07002683 _registers.setRegister(UNW_AARCH64_X0 + i, value);
Ryan Pricharda684bed2021-01-13 16:38:36 -08002684 }
2685 _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2686 _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2687 _isSignalFrame = true;
2688 return UNW_STEP_SUCCESS;
2689}
Shoaib Meenai67bace72022-05-23 22:11:34 -07002690#endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
2691 // defined(_LIBUNWIND_TARGET_AARCH64)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002692
Shoaib Meenai67bace72022-05-23 22:11:34 -07002693#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \
2694 defined(_LIBUNWIND_TARGET_S390X)
Ulrich Weigandf1108b62022-05-04 10:43:11 +02002695template <typename A, typename R>
2696bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_s390x &) {
2697 // Look for the sigreturn trampoline. The trampoline's body is a
2698 // specific instruction (see below). Typically the trampoline comes from the
2699 // vDSO (i.e. the __kernel_[rt_]sigreturn function). A libc might provide its
2700 // own restorer function, though, or user-mode QEMU might write a trampoline
2701 // onto the stack.
2702 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Ulrich Weigand955e2ff2022-07-18 16:54:48 +02002703 // The PC might contain an invalid address if the unwind info is bad, so
2704 // directly accessing it could cause a segfault. Use process_vm_readv to
2705 // read the memory safely instead.
2706 uint16_t inst;
2707 struct iovec local_iov = {&inst, sizeof inst};
2708 struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof inst};
2709 long bytesRead = process_vm_readv(getpid(), &local_iov, 1, &remote_iov, 1, 0);
2710 if (bytesRead == sizeof inst && (inst == 0x0a77 || inst == 0x0aad)) {
Ulrich Weigandf1108b62022-05-04 10:43:11 +02002711 _info = {};
2712 _info.start_ip = pc;
2713 _info.end_ip = pc + 2;
2714 _isSigReturn = true;
2715 return true;
2716 }
2717 return false;
2718}
2719
2720template <typename A, typename R>
2721int UnwindCursor<A, R>::stepThroughSigReturn(Registers_s390x &) {
2722 // Determine current SP.
2723 const pint_t sp = static_cast<pint_t>(this->getReg(UNW_REG_SP));
2724 // According to the s390x ABI, the CFA is at (incoming) SP + 160.
2725 const pint_t cfa = sp + 160;
2726
2727 // Determine current PC and instruction there (this must be either
2728 // a "svc __NR_sigreturn" or "svc __NR_rt_sigreturn").
2729 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2730 const uint16_t inst = _addressSpace.get16(pc);
2731
2732 // Find the addresses of the signo and sigcontext in the frame.
2733 pint_t pSigctx = 0;
2734 pint_t pSigno = 0;
2735
2736 // "svc __NR_sigreturn" uses a non-RT signal trampoline frame.
2737 if (inst == 0x0a77) {
2738 // Layout of a non-RT signal trampoline frame, starting at the CFA:
2739 // - 8-byte signal mask
2740 // - 8-byte pointer to sigcontext, followed by signo
2741 // - 4-byte signo
2742 pSigctx = _addressSpace.get64(cfa + 8);
2743 pSigno = pSigctx + 344;
2744 }
2745
2746 // "svc __NR_rt_sigreturn" uses a RT signal trampoline frame.
2747 if (inst == 0x0aad) {
2748 // Layout of a RT signal trampoline frame, starting at the CFA:
2749 // - 8-byte retcode (+ alignment)
2750 // - 128-byte siginfo struct (starts with signo)
2751 // - ucontext struct:
2752 // - 8-byte long (uc_flags)
2753 // - 8-byte pointer (uc_link)
2754 // - 24-byte stack_t
2755 // - 8 bytes of padding because sigcontext has 16-byte alignment
2756 // - sigcontext/mcontext_t
2757 pSigctx = cfa + 8 + 128 + 8 + 8 + 24 + 8;
2758 pSigno = cfa + 8;
2759 }
2760
2761 assert(pSigctx != 0);
2762 assert(pSigno != 0);
2763
2764 // Offsets from sigcontext to each register.
2765 const pint_t kOffsetPc = 8;
2766 const pint_t kOffsetGprs = 16;
2767 const pint_t kOffsetFprs = 216;
2768
2769 // Restore all registers.
2770 for (int i = 0; i < 16; ++i) {
2771 uint64_t value = _addressSpace.get64(pSigctx + kOffsetGprs +
2772 static_cast<pint_t>(i * 8));
2773 _registers.setRegister(UNW_S390X_R0 + i, value);
2774 }
2775 for (int i = 0; i < 16; ++i) {
2776 static const int fpr[16] = {
2777 UNW_S390X_F0, UNW_S390X_F1, UNW_S390X_F2, UNW_S390X_F3,
2778 UNW_S390X_F4, UNW_S390X_F5, UNW_S390X_F6, UNW_S390X_F7,
2779 UNW_S390X_F8, UNW_S390X_F9, UNW_S390X_F10, UNW_S390X_F11,
2780 UNW_S390X_F12, UNW_S390X_F13, UNW_S390X_F14, UNW_S390X_F15
2781 };
2782 double value = _addressSpace.getDouble(pSigctx + kOffsetFprs +
2783 static_cast<pint_t>(i * 8));
2784 _registers.setFloatRegister(fpr[i], value);
2785 }
2786 _registers.setIP(_addressSpace.get64(pSigctx + kOffsetPc));
2787
2788 // SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr
2789 // after the faulting instruction rather than before it.
2790 // Do not set _isSignalFrame in that case.
2791 uint32_t signo = _addressSpace.get32(pSigno);
2792 _isSignalFrame = (signo != 4 && signo != 5 && signo != 8);
2793
2794 return UNW_STEP_SUCCESS;
2795}
Shoaib Meenai67bace72022-05-23 22:11:34 -07002796#endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
2797 // defined(_LIBUNWIND_TARGET_S390X)
Ulrich Weigandf1108b62022-05-04 10:43:11 +02002798
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002799template <typename A, typename R>
2800int UnwindCursor<A, R>::step() {
2801 // Bottom of stack is defined is when unwind info cannot be found.
2802 if (_unwindInfoMissing)
2803 return UNW_STEP_END;
2804
2805 // Use unwinding info to modify register set as if function returned.
2806 int result;
Shoaib Meenai67bace72022-05-23 22:11:34 -07002807#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002808 if (_isSigReturn) {
2809 result = this->stepThroughSigReturn();
2810 } else
2811#endif
2812 {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002813#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002814 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00002815#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002816 result = this->stepWithSEHData();
Xing Xuebbcbce92022-04-13 11:01:59 -04002817#elif defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
2818 result = this->stepWithTBTableData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002819#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002820 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002821#elif defined(_LIBUNWIND_ARM_EHABI)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002822 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002823#else
2824 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00002825 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002826 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00002827 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002828#endif
Ryan Pricharda684bed2021-01-13 16:38:36 -08002829 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002830
2831 // update info based on new PC
2832 if (result == UNW_STEP_SUCCESS) {
2833 this->setInfoBasedOnIPRegister(true);
2834 if (_unwindInfoMissing)
2835 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002836 }
2837
2838 return result;
2839}
2840
2841template <typename A, typename R>
2842void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002843 if (_unwindInfoMissing)
2844 memset(info, 0, sizeof(*info));
2845 else
2846 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002847}
2848
2849template <typename A, typename R>
2850bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2851 unw_word_t *offset) {
2852 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2853 buf, bufLen, offset);
2854}
2855
gejin7f493162021-08-26 16:20:38 +08002856#if defined(_LIBUNWIND_USE_CET)
2857extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
2858 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
2859 return co->get_registers();
2860}
2861#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002862} // namespace libunwind
2863
2864#endif // __UNWINDCURSOR_HPP__