blob: 8373b61eb4247747a0910427c17c99d1f780e315 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.hpp ---------------------------===//
2//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
Ed Maste6f723382016-08-30 13:08:21 +00008// C++ interface to lower levels of libunwind
Saleem Abdulrasool17552662015-04-24 19:39:17 +00009//===----------------------------------------------------------------------===//
10
11#ifndef __UNWINDCURSOR_HPP__
12#define __UNWINDCURSOR_HPP__
13
Saleem Abdulrasool17552662015-04-24 19:39:17 +000014#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000017#include <unwind.h>
18
Charles Davisfa2e6202018-08-30 21:29:00 +000019#ifdef _WIN32
20 #include <windows.h>
21 #include <ntverp.h>
22#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000023#ifdef __APPLE__
24 #include <mach-o/dyld.h>
25#endif
26
Charles Davisfa2e6202018-08-30 21:29:00 +000027#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
28// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
29// earlier) SDKs.
30// MinGW-w64 has always provided this struct.
31 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
32 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
33struct _DISPATCHER_CONTEXT {
34 ULONG64 ControlPc;
35 ULONG64 ImageBase;
36 PRUNTIME_FUNCTION FunctionEntry;
37 ULONG64 EstablisherFrame;
38 ULONG64 TargetIp;
39 PCONTEXT ContextRecord;
40 PEXCEPTION_ROUTINE LanguageHandler;
41 PVOID HandlerData;
42 PUNWIND_HISTORY_TABLE HistoryTable;
43 ULONG ScopeIndex;
44 ULONG Fill0;
45};
46 #endif
47
48struct UNWIND_INFO {
49 uint8_t Version : 3;
50 uint8_t Flags : 5;
51 uint8_t SizeOfProlog;
52 uint8_t CountOfCodes;
53 uint8_t FrameRegister : 4;
54 uint8_t FrameOffset : 4;
55 uint16_t UnwindCodes[2];
56};
57
58extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
59 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
60 struct _Unwind_Context *);
61
62#endif
63
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064#include "config.h"
65
66#include "AddressSpace.hpp"
67#include "CompactUnwinder.hpp"
68#include "config.h"
69#include "DwarfInstructions.hpp"
70#include "EHHeaderParser.hpp"
71#include "libunwind.h"
72#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000073#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000074#include "Unwind-EHABI.h"
75
76namespace libunwind {
77
Ranjeet Singh421231a2017-03-31 15:28:06 +000078#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079/// Cache of recently found FDEs.
80template <typename A>
81class _LIBUNWIND_HIDDEN DwarfFDECache {
82 typedef typename A::pint_t pint_t;
83public:
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -070084 static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000085 static pint_t findFDE(pint_t mh, pint_t pc);
86 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
87 static void removeAllIn(pint_t mh);
88 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
89 unw_word_t ip_end,
90 unw_word_t fde, unw_word_t mh));
91
92private:
93
94 struct entry {
95 pint_t mh;
96 pint_t ip_start;
97 pint_t ip_end;
98 pint_t fde;
99 };
100
101 // These fields are all static to avoid needing an initializer.
102 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000103 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000104#ifdef __APPLE__
105 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
106 static bool _registeredForDyldUnloads;
107#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000108 static entry *_buffer;
109 static entry *_bufferUsed;
110 static entry *_bufferEnd;
111 static entry _initialBuffer[64];
112};
113
114template <typename A>
115typename DwarfFDECache<A>::entry *
116DwarfFDECache<A>::_buffer = _initialBuffer;
117
118template <typename A>
119typename DwarfFDECache<A>::entry *
120DwarfFDECache<A>::_bufferUsed = _initialBuffer;
121
122template <typename A>
123typename DwarfFDECache<A>::entry *
124DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
125
126template <typename A>
127typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
128
129template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000130RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000131
132#ifdef __APPLE__
133template <typename A>
134bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
135#endif
136
137template <typename A>
138typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
139 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000140 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000141 for (entry *p = _buffer; p < _bufferUsed; ++p) {
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -0700142 if ((mh == p->mh) || (mh == kSearchAll)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000143 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
144 result = p->fde;
145 break;
146 }
147 }
148 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000149 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000150 return result;
151}
152
153template <typename A>
154void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
155 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000156#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000157 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000158 if (_bufferUsed >= _bufferEnd) {
159 size_t oldSize = (size_t)(_bufferEnd - _buffer);
160 size_t newSize = oldSize * 4;
161 // Can't use operator new (we are below it).
162 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
163 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
164 if (_buffer != _initialBuffer)
165 free(_buffer);
166 _buffer = newBuffer;
167 _bufferUsed = &newBuffer[oldSize];
168 _bufferEnd = &newBuffer[newSize];
169 }
170 _bufferUsed->mh = mh;
171 _bufferUsed->ip_start = ip_start;
172 _bufferUsed->ip_end = ip_end;
173 _bufferUsed->fde = fde;
174 ++_bufferUsed;
175#ifdef __APPLE__
176 if (!_registeredForDyldUnloads) {
177 _dyld_register_func_for_remove_image(&dyldUnloadHook);
178 _registeredForDyldUnloads = true;
179 }
180#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000181 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000182#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000183}
184
185template <typename A>
186void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000187 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000188 entry *d = _buffer;
189 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
190 if (s->mh != mh) {
191 if (d != s)
192 *d = *s;
193 ++d;
194 }
195 }
196 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000197 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000198}
199
200#ifdef __APPLE__
201template <typename A>
202void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
203 removeAllIn((pint_t) mh);
204}
205#endif
206
207template <typename A>
208void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
209 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 +0000210 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000211 for (entry *p = _buffer; p < _bufferUsed; ++p) {
212 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
213 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000214 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000215}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000216#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000217
218
219#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
220
Ranjeet Singh421231a2017-03-31 15:28:06 +0000221#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000222template <typename A> class UnwindSectionHeader {
223public:
224 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
225 : _addressSpace(addressSpace), _addr(addr) {}
226
227 uint32_t version() const {
228 return _addressSpace.get32(_addr +
229 offsetof(unwind_info_section_header, version));
230 }
231 uint32_t commonEncodingsArraySectionOffset() const {
232 return _addressSpace.get32(_addr +
233 offsetof(unwind_info_section_header,
234 commonEncodingsArraySectionOffset));
235 }
236 uint32_t commonEncodingsArrayCount() const {
237 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
238 commonEncodingsArrayCount));
239 }
240 uint32_t personalityArraySectionOffset() const {
241 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
242 personalityArraySectionOffset));
243 }
244 uint32_t personalityArrayCount() const {
245 return _addressSpace.get32(
246 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
247 }
248 uint32_t indexSectionOffset() const {
249 return _addressSpace.get32(
250 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
251 }
252 uint32_t indexCount() const {
253 return _addressSpace.get32(
254 _addr + offsetof(unwind_info_section_header, indexCount));
255 }
256
257private:
258 A &_addressSpace;
259 typename A::pint_t _addr;
260};
261
262template <typename A> class UnwindSectionIndexArray {
263public:
264 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
265 : _addressSpace(addressSpace), _addr(addr) {}
266
267 uint32_t functionOffset(uint32_t index) const {
268 return _addressSpace.get32(
269 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
270 functionOffset));
271 }
272 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
273 return _addressSpace.get32(
274 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
275 secondLevelPagesSectionOffset));
276 }
277 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
278 return _addressSpace.get32(
279 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
280 lsdaIndexArraySectionOffset));
281 }
282
283private:
284 A &_addressSpace;
285 typename A::pint_t _addr;
286};
287
288template <typename A> class UnwindSectionRegularPageHeader {
289public:
290 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
291 : _addressSpace(addressSpace), _addr(addr) {}
292
293 uint32_t kind() const {
294 return _addressSpace.get32(
295 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
296 }
297 uint16_t entryPageOffset() const {
298 return _addressSpace.get16(
299 _addr + offsetof(unwind_info_regular_second_level_page_header,
300 entryPageOffset));
301 }
302 uint16_t entryCount() const {
303 return _addressSpace.get16(
304 _addr +
305 offsetof(unwind_info_regular_second_level_page_header, entryCount));
306 }
307
308private:
309 A &_addressSpace;
310 typename A::pint_t _addr;
311};
312
313template <typename A> class UnwindSectionRegularArray {
314public:
315 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
316 : _addressSpace(addressSpace), _addr(addr) {}
317
318 uint32_t functionOffset(uint32_t index) const {
319 return _addressSpace.get32(
320 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
321 functionOffset));
322 }
323 uint32_t encoding(uint32_t index) const {
324 return _addressSpace.get32(
325 _addr +
326 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
327 }
328
329private:
330 A &_addressSpace;
331 typename A::pint_t _addr;
332};
333
334template <typename A> class UnwindSectionCompressedPageHeader {
335public:
336 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
337 : _addressSpace(addressSpace), _addr(addr) {}
338
339 uint32_t kind() const {
340 return _addressSpace.get32(
341 _addr +
342 offsetof(unwind_info_compressed_second_level_page_header, kind));
343 }
344 uint16_t entryPageOffset() const {
345 return _addressSpace.get16(
346 _addr + offsetof(unwind_info_compressed_second_level_page_header,
347 entryPageOffset));
348 }
349 uint16_t entryCount() const {
350 return _addressSpace.get16(
351 _addr +
352 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
353 }
354 uint16_t encodingsPageOffset() const {
355 return _addressSpace.get16(
356 _addr + offsetof(unwind_info_compressed_second_level_page_header,
357 encodingsPageOffset));
358 }
359 uint16_t encodingsCount() const {
360 return _addressSpace.get16(
361 _addr + offsetof(unwind_info_compressed_second_level_page_header,
362 encodingsCount));
363 }
364
365private:
366 A &_addressSpace;
367 typename A::pint_t _addr;
368};
369
370template <typename A> class UnwindSectionCompressedArray {
371public:
372 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
373 : _addressSpace(addressSpace), _addr(addr) {}
374
375 uint32_t functionOffset(uint32_t index) const {
376 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
377 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
378 }
379 uint16_t encodingIndex(uint32_t index) const {
380 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
381 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
382 }
383
384private:
385 A &_addressSpace;
386 typename A::pint_t _addr;
387};
388
389template <typename A> class UnwindSectionLsdaArray {
390public:
391 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
392 : _addressSpace(addressSpace), _addr(addr) {}
393
394 uint32_t functionOffset(uint32_t index) const {
395 return _addressSpace.get32(
396 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
397 index, functionOffset));
398 }
399 uint32_t lsdaOffset(uint32_t index) const {
400 return _addressSpace.get32(
401 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
402 index, lsdaOffset));
403 }
404
405private:
406 A &_addressSpace;
407 typename A::pint_t _addr;
408};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000409#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000410
411class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
412public:
413 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
414 // This avoids an unnecessary dependency to libc++abi.
415 void operator delete(void *, size_t) {}
416
417 virtual ~AbstractUnwindCursor() {}
418 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
419 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
420 virtual void setReg(int, unw_word_t) {
421 _LIBUNWIND_ABORT("setReg not implemented");
422 }
423 virtual bool validFloatReg(int) {
424 _LIBUNWIND_ABORT("validFloatReg not implemented");
425 }
426 virtual unw_fpreg_t getFloatReg(int) {
427 _LIBUNWIND_ABORT("getFloatReg not implemented");
428 }
429 virtual void setFloatReg(int, unw_fpreg_t) {
430 _LIBUNWIND_ABORT("setFloatReg not implemented");
431 }
432 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
433 virtual void getInfo(unw_proc_info_t *) {
434 _LIBUNWIND_ABORT("getInfo not implemented");
435 }
436 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
437 virtual bool isSignalFrame() {
438 _LIBUNWIND_ABORT("isSignalFrame not implemented");
439 }
440 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
441 _LIBUNWIND_ABORT("getFunctionName not implemented");
442 }
443 virtual void setInfoBasedOnIPRegister(bool = false) {
444 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
445 }
446 virtual const char *getRegisterName(int) {
447 _LIBUNWIND_ABORT("getRegisterName not implemented");
448 }
449#ifdef __arm__
450 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
451#endif
452};
453
Charles Davisfa2e6202018-08-30 21:29:00 +0000454#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
455
456/// \c UnwindCursor contains all state (including all register values) during
457/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
458template <typename A, typename R>
459class UnwindCursor : public AbstractUnwindCursor {
460 typedef typename A::pint_t pint_t;
461public:
462 UnwindCursor(unw_context_t *context, A &as);
463 UnwindCursor(CONTEXT *context, A &as);
464 UnwindCursor(A &as, void *threadArg);
465 virtual ~UnwindCursor() {}
466 virtual bool validReg(int);
467 virtual unw_word_t getReg(int);
468 virtual void setReg(int, unw_word_t);
469 virtual bool validFloatReg(int);
470 virtual unw_fpreg_t getFloatReg(int);
471 virtual void setFloatReg(int, unw_fpreg_t);
472 virtual int step();
473 virtual void getInfo(unw_proc_info_t *);
474 virtual void jumpto();
475 virtual bool isSignalFrame();
476 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
477 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
478 virtual const char *getRegisterName(int num);
479#ifdef __arm__
480 virtual void saveVFPAsX();
481#endif
482
483 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
484 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
485
Martin Storsjo5f5036e2019-02-03 22:16:53 +0000486 // libunwind does not and should not depend on C++ library which means that we
487 // need our own defition of inline placement new.
488 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
489
Charles Davisfa2e6202018-08-30 21:29:00 +0000490private:
491
492 pint_t getLastPC() const { return _dispContext.ControlPc; }
493 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
494 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
495 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
496 &_dispContext.ImageBase,
497 _dispContext.HistoryTable);
498 *base = _dispContext.ImageBase;
499 return _dispContext.FunctionEntry;
500 }
501 bool getInfoFromSEH(pint_t pc);
502 int stepWithSEHData() {
503 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
504 _dispContext.ImageBase,
505 _dispContext.ControlPc,
506 _dispContext.FunctionEntry,
507 _dispContext.ContextRecord,
508 &_dispContext.HandlerData,
509 &_dispContext.EstablisherFrame,
510 NULL);
511 // Update some fields of the unwind info now, since we have them.
512 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
513 if (_dispContext.LanguageHandler) {
514 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
515 } else
516 _info.handler = 0;
517 return UNW_STEP_SUCCESS;
518 }
519
520 A &_addressSpace;
521 unw_proc_info_t _info;
522 DISPATCHER_CONTEXT _dispContext;
523 CONTEXT _msContext;
524 UNWIND_HISTORY_TABLE _histTable;
525 bool _unwindInfoMissing;
526};
527
528
529template <typename A, typename R>
530UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
531 : _addressSpace(as), _unwindInfoMissing(false) {
532 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
533 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +0300534 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
535 "UnwindCursor<> requires more alignment than unw_cursor_t");
Charles Davisfa2e6202018-08-30 21:29:00 +0000536 memset(&_info, 0, sizeof(_info));
537 memset(&_histTable, 0, sizeof(_histTable));
538 _dispContext.ContextRecord = &_msContext;
539 _dispContext.HistoryTable = &_histTable;
540 // Initialize MS context from ours.
541 R r(context);
542 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
543#if defined(_LIBUNWIND_TARGET_X86_64)
544 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
545 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
546 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
547 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
548 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
549 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
550 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
551 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
552 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
553 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
554 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
555 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
556 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
557 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
558 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
559 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
560 _msContext.Rip = r.getRegister(UNW_REG_IP);
561 union {
562 v128 v;
563 M128A m;
564 } t;
565 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
566 _msContext.Xmm0 = t.m;
567 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
568 _msContext.Xmm1 = t.m;
569 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
570 _msContext.Xmm2 = t.m;
571 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
572 _msContext.Xmm3 = t.m;
573 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
574 _msContext.Xmm4 = t.m;
575 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
576 _msContext.Xmm5 = t.m;
577 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
578 _msContext.Xmm6 = t.m;
579 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
580 _msContext.Xmm7 = t.m;
581 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
582 _msContext.Xmm8 = t.m;
583 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
584 _msContext.Xmm9 = t.m;
585 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
586 _msContext.Xmm10 = t.m;
587 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
588 _msContext.Xmm11 = t.m;
589 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
590 _msContext.Xmm12 = t.m;
591 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
592 _msContext.Xmm13 = t.m;
593 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
594 _msContext.Xmm14 = t.m;
595 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
596 _msContext.Xmm15 = t.m;
597#elif defined(_LIBUNWIND_TARGET_ARM)
598 _msContext.R0 = r.getRegister(UNW_ARM_R0);
599 _msContext.R1 = r.getRegister(UNW_ARM_R1);
600 _msContext.R2 = r.getRegister(UNW_ARM_R2);
601 _msContext.R3 = r.getRegister(UNW_ARM_R3);
602 _msContext.R4 = r.getRegister(UNW_ARM_R4);
603 _msContext.R5 = r.getRegister(UNW_ARM_R5);
604 _msContext.R6 = r.getRegister(UNW_ARM_R6);
605 _msContext.R7 = r.getRegister(UNW_ARM_R7);
606 _msContext.R8 = r.getRegister(UNW_ARM_R8);
607 _msContext.R9 = r.getRegister(UNW_ARM_R9);
608 _msContext.R10 = r.getRegister(UNW_ARM_R10);
609 _msContext.R11 = r.getRegister(UNW_ARM_R11);
610 _msContext.R12 = r.getRegister(UNW_ARM_R12);
611 _msContext.Sp = r.getRegister(UNW_ARM_SP);
612 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000613 _msContext.Pc = r.getRegister(UNW_ARM_IP);
614 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000615 union {
616 uint64_t w;
617 double d;
618 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000619 d.d = r.getFloatRegister(i);
620 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000621 }
Martin Storsjoce150112018-12-18 20:05:59 +0000622#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700623 for (int i = UNW_AARCH64_X0; i <= UNW_ARM64_X30; ++i)
624 _msContext.X[i - UNW_AARCH64_X0] = r.getRegister(i);
Martin Storsjoce150112018-12-18 20:05:59 +0000625 _msContext.Sp = r.getRegister(UNW_REG_SP);
626 _msContext.Pc = r.getRegister(UNW_REG_IP);
Fangrui Song5f263002021-08-20 14:26:27 -0700627 for (int i = UNW_AARCH64_V0; i <= UNW_ARM64_D31; ++i)
628 _msContext.V[i - UNW_AARCH64_V0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000629#endif
630}
631
632template <typename A, typename R>
633UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
634 : _addressSpace(as), _unwindInfoMissing(false) {
635 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
636 "UnwindCursor<> does not fit in unw_cursor_t");
637 memset(&_info, 0, sizeof(_info));
638 memset(&_histTable, 0, sizeof(_histTable));
639 _dispContext.ContextRecord = &_msContext;
640 _dispContext.HistoryTable = &_histTable;
641 _msContext = *context;
642}
643
644
645template <typename A, typename R>
646bool UnwindCursor<A, R>::validReg(int regNum) {
647 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
648#if defined(_LIBUNWIND_TARGET_X86_64)
649 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
650#elif defined(_LIBUNWIND_TARGET_ARM)
651 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000652#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700653 if (regNum >= UNW_AARCH64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000654#endif
655 return false;
656}
657
658template <typename A, typename R>
659unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
660 switch (regNum) {
661#if defined(_LIBUNWIND_TARGET_X86_64)
662 case UNW_REG_IP: return _msContext.Rip;
663 case UNW_X86_64_RAX: return _msContext.Rax;
664 case UNW_X86_64_RDX: return _msContext.Rdx;
665 case UNW_X86_64_RCX: return _msContext.Rcx;
666 case UNW_X86_64_RBX: return _msContext.Rbx;
667 case UNW_REG_SP:
668 case UNW_X86_64_RSP: return _msContext.Rsp;
669 case UNW_X86_64_RBP: return _msContext.Rbp;
670 case UNW_X86_64_RSI: return _msContext.Rsi;
671 case UNW_X86_64_RDI: return _msContext.Rdi;
672 case UNW_X86_64_R8: return _msContext.R8;
673 case UNW_X86_64_R9: return _msContext.R9;
674 case UNW_X86_64_R10: return _msContext.R10;
675 case UNW_X86_64_R11: return _msContext.R11;
676 case UNW_X86_64_R12: return _msContext.R12;
677 case UNW_X86_64_R13: return _msContext.R13;
678 case UNW_X86_64_R14: return _msContext.R14;
679 case UNW_X86_64_R15: return _msContext.R15;
680#elif defined(_LIBUNWIND_TARGET_ARM)
681 case UNW_ARM_R0: return _msContext.R0;
682 case UNW_ARM_R1: return _msContext.R1;
683 case UNW_ARM_R2: return _msContext.R2;
684 case UNW_ARM_R3: return _msContext.R3;
685 case UNW_ARM_R4: return _msContext.R4;
686 case UNW_ARM_R5: return _msContext.R5;
687 case UNW_ARM_R6: return _msContext.R6;
688 case UNW_ARM_R7: return _msContext.R7;
689 case UNW_ARM_R8: return _msContext.R8;
690 case UNW_ARM_R9: return _msContext.R9;
691 case UNW_ARM_R10: return _msContext.R10;
692 case UNW_ARM_R11: return _msContext.R11;
693 case UNW_ARM_R12: return _msContext.R12;
694 case UNW_REG_SP:
695 case UNW_ARM_SP: return _msContext.Sp;
696 case UNW_ARM_LR: return _msContext.Lr;
697 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000698 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000699#elif defined(_LIBUNWIND_TARGET_AARCH64)
700 case UNW_REG_SP: return _msContext.Sp;
701 case UNW_REG_IP: return _msContext.Pc;
Fangrui Song5f263002021-08-20 14:26:27 -0700702 default: return _msContext.X[regNum - UNW_AARCH64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000703#endif
704 }
705 _LIBUNWIND_ABORT("unsupported register");
706}
707
708template <typename A, typename R>
709void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
710 switch (regNum) {
711#if defined(_LIBUNWIND_TARGET_X86_64)
712 case UNW_REG_IP: _msContext.Rip = value; break;
713 case UNW_X86_64_RAX: _msContext.Rax = value; break;
714 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
715 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
716 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
717 case UNW_REG_SP:
718 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
719 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
720 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
721 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
722 case UNW_X86_64_R8: _msContext.R8 = value; break;
723 case UNW_X86_64_R9: _msContext.R9 = value; break;
724 case UNW_X86_64_R10: _msContext.R10 = value; break;
725 case UNW_X86_64_R11: _msContext.R11 = value; break;
726 case UNW_X86_64_R12: _msContext.R12 = value; break;
727 case UNW_X86_64_R13: _msContext.R13 = value; break;
728 case UNW_X86_64_R14: _msContext.R14 = value; break;
729 case UNW_X86_64_R15: _msContext.R15 = value; break;
730#elif defined(_LIBUNWIND_TARGET_ARM)
731 case UNW_ARM_R0: _msContext.R0 = value; break;
732 case UNW_ARM_R1: _msContext.R1 = value; break;
733 case UNW_ARM_R2: _msContext.R2 = value; break;
734 case UNW_ARM_R3: _msContext.R3 = value; break;
735 case UNW_ARM_R4: _msContext.R4 = value; break;
736 case UNW_ARM_R5: _msContext.R5 = value; break;
737 case UNW_ARM_R6: _msContext.R6 = value; break;
738 case UNW_ARM_R7: _msContext.R7 = value; break;
739 case UNW_ARM_R8: _msContext.R8 = value; break;
740 case UNW_ARM_R9: _msContext.R9 = value; break;
741 case UNW_ARM_R10: _msContext.R10 = value; break;
742 case UNW_ARM_R11: _msContext.R11 = value; break;
743 case UNW_ARM_R12: _msContext.R12 = value; break;
744 case UNW_REG_SP:
745 case UNW_ARM_SP: _msContext.Sp = value; break;
746 case UNW_ARM_LR: _msContext.Lr = value; break;
747 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000748 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000749#elif defined(_LIBUNWIND_TARGET_AARCH64)
750 case UNW_REG_SP: _msContext.Sp = value; break;
751 case UNW_REG_IP: _msContext.Pc = value; break;
Fangrui Song5f263002021-08-20 14:26:27 -0700752 case UNW_AARCH64_X0:
753 case UNW_AARCH64_X1:
754 case UNW_AARCH64_X2:
755 case UNW_AARCH64_X3:
756 case UNW_AARCH64_X4:
757 case UNW_AARCH64_X5:
758 case UNW_AARCH64_X6:
759 case UNW_AARCH64_X7:
760 case UNW_AARCH64_X8:
761 case UNW_AARCH64_X9:
762 case UNW_AARCH64_X10:
763 case UNW_AARCH64_X11:
764 case UNW_AARCH64_X12:
765 case UNW_AARCH64_X13:
766 case UNW_AARCH64_X14:
767 case UNW_AARCH64_X15:
768 case UNW_AARCH64_X16:
769 case UNW_AARCH64_X17:
770 case UNW_AARCH64_X18:
771 case UNW_AARCH64_X19:
772 case UNW_AARCH64_X20:
773 case UNW_AARCH64_X21:
774 case UNW_AARCH64_X22:
775 case UNW_AARCH64_X23:
776 case UNW_AARCH64_X24:
777 case UNW_AARCH64_X25:
778 case UNW_AARCH64_X26:
779 case UNW_AARCH64_X27:
780 case UNW_AARCH64_X28:
781 case UNW_AARCH64_FP:
782 case UNW_AARCH64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000783#endif
784 default:
785 _LIBUNWIND_ABORT("unsupported register");
786 }
787}
788
789template <typename A, typename R>
790bool UnwindCursor<A, R>::validFloatReg(int regNum) {
791#if defined(_LIBUNWIND_TARGET_ARM)
792 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
793 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000794#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700795 if (regNum >= UNW_AARCH64_V0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo059a1632019-01-22 22:12:23 +0000796#else
797 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000798#endif
799 return false;
800}
801
802template <typename A, typename R>
803unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
804#if defined(_LIBUNWIND_TARGET_ARM)
805 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
806 union {
807 uint32_t w;
808 float f;
809 } d;
810 d.w = _msContext.S[regNum - UNW_ARM_S0];
811 return d.f;
812 }
813 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
814 union {
815 uint64_t w;
816 double d;
817 } d;
818 d.w = _msContext.D[regNum - UNW_ARM_D0];
819 return d.d;
820 }
821 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000822#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700823 return _msContext.V[regNum - UNW_AARCH64_V0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000824#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000825 (void)regNum;
Charles Davisfa2e6202018-08-30 21:29:00 +0000826 _LIBUNWIND_ABORT("float registers unimplemented");
827#endif
828}
829
830template <typename A, typename R>
831void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
832#if defined(_LIBUNWIND_TARGET_ARM)
833 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
834 union {
835 uint32_t w;
836 float f;
837 } d;
838 d.f = value;
839 _msContext.S[regNum - UNW_ARM_S0] = d.w;
840 }
841 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
842 union {
843 uint64_t w;
844 double d;
845 } d;
846 d.d = value;
847 _msContext.D[regNum - UNW_ARM_D0] = d.w;
848 }
849 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000850#elif defined(_LIBUNWIND_TARGET_AARCH64)
Fangrui Song5f263002021-08-20 14:26:27 -0700851 _msContext.V[regNum - UNW_AARCH64_V0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000852#else
Martin Storsjo059a1632019-01-22 22:12:23 +0000853 (void)regNum;
854 (void)value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000855 _LIBUNWIND_ABORT("float registers unimplemented");
856#endif
857}
858
859template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
860 RtlRestoreContext(&_msContext, nullptr);
861}
862
863#ifdef __arm__
864template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
865#endif
866
867template <typename A, typename R>
868const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000869 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000870}
871
872template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
873 return false;
874}
875
876#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
877
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000878/// UnwindCursor contains all state (including all register values) during
879/// an unwind. This is normally stack allocated inside a unw_cursor_t.
880template <typename A, typename R>
881class UnwindCursor : public AbstractUnwindCursor{
882 typedef typename A::pint_t pint_t;
883public:
884 UnwindCursor(unw_context_t *context, A &as);
885 UnwindCursor(A &as, void *threadArg);
886 virtual ~UnwindCursor() {}
887 virtual bool validReg(int);
888 virtual unw_word_t getReg(int);
889 virtual void setReg(int, unw_word_t);
890 virtual bool validFloatReg(int);
891 virtual unw_fpreg_t getFloatReg(int);
892 virtual void setFloatReg(int, unw_fpreg_t);
893 virtual int step();
894 virtual void getInfo(unw_proc_info_t *);
895 virtual void jumpto();
896 virtual bool isSignalFrame();
897 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
898 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
899 virtual const char *getRegisterName(int num);
900#ifdef __arm__
901 virtual void saveVFPAsX();
902#endif
903
Petr Hosek36f61542019-02-02 21:15:49 +0000904 // libunwind does not and should not depend on C++ library which means that we
905 // need our own defition of inline placement new.
906 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
907
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000908private:
909
Ranjeet Singh421231a2017-03-31 15:28:06 +0000910#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000911 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000912
913 int stepWithEHABI() {
914 size_t len = 0;
915 size_t off = 0;
916 // FIXME: Calling decode_eht_entry() here is violating the libunwind
917 // abstraction layer.
918 const uint32_t *ehtp =
919 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
920 &off, &len);
921 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
922 _URC_CONTINUE_UNWIND)
923 return UNW_STEP_END;
924 return UNW_STEP_SUCCESS;
925 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000926#endif
927
Ryan Pricharda684bed2021-01-13 16:38:36 -0800928#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
929 bool setInfoForSigReturn() {
930 R dummy;
931 return setInfoForSigReturn(dummy);
932 }
933 int stepThroughSigReturn() {
934 R dummy;
935 return stepThroughSigReturn(dummy);
936 }
937 bool setInfoForSigReturn(Registers_arm64 &);
938 int stepThroughSigReturn(Registers_arm64 &);
939 template <typename Registers> bool setInfoForSigReturn(Registers &) {
940 return false;
941 }
942 template <typename Registers> int stepThroughSigReturn(Registers &) {
943 return UNW_STEP_END;
944 }
945#endif
946
Ranjeet Singh421231a2017-03-31 15:28:06 +0000947#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Prichard1ae2b942020-08-18 02:31:38 -0700948 bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
949 const typename CFI_Parser<A>::CIE_Info &cieInfo,
950 pint_t pc, uintptr_t dso_base);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000951 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
952 uint32_t fdeSectionOffsetHint=0);
953 int stepWithDwarfFDE() {
954 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
955 (pint_t)this->getReg(UNW_REG_IP),
956 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -0700957 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000958 }
959#endif
960
Ranjeet Singh421231a2017-03-31 15:28:06 +0000961#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000962 bool getInfoFromCompactEncodingSection(pint_t pc,
963 const UnwindInfoSections &sects);
964 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000965 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000966 if ( compactSaysUseDwarf() )
967 return stepWithDwarfFDE();
968 #endif
969 R dummy;
970 return stepWithCompactEncoding(dummy);
971 }
972
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000973#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000974 int stepWithCompactEncoding(Registers_x86_64 &) {
975 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
976 _info.format, _info.start_ip, _addressSpace, _registers);
977 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000978#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000979
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000980#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000981 int stepWithCompactEncoding(Registers_x86 &) {
982 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
983 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
984 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000985#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000986
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000987#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000988 int stepWithCompactEncoding(Registers_ppc &) {
989 return UNW_EINVAL;
990 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000991#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000992
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000993#if defined(_LIBUNWIND_TARGET_PPC64)
994 int stepWithCompactEncoding(Registers_ppc64 &) {
995 return UNW_EINVAL;
996 }
997#endif
998
999
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001000#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001001 int stepWithCompactEncoding(Registers_arm64 &) {
1002 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
1003 _info.format, _info.start_ip, _addressSpace, _registers);
1004 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001005#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001006
John Baldwin56441d42017-12-12 21:43:36 +00001007#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1008 int stepWithCompactEncoding(Registers_mips_o32 &) {
1009 return UNW_EINVAL;
1010 }
1011#endif
1012
John Baldwin541a4352018-01-09 17:07:18 +00001013#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1014 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +00001015 return UNW_EINVAL;
1016 }
1017#endif
1018
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001019#if defined(_LIBUNWIND_TARGET_SPARC)
1020 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1021#endif
1022
Sam Elliott81f7e172019-12-16 16:35:17 +00001023#if defined (_LIBUNWIND_TARGET_RISCV)
1024 int stepWithCompactEncoding(Registers_riscv &) {
1025 return UNW_EINVAL;
1026 }
1027#endif
1028
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001029 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1030 R dummy;
1031 return compactSaysUseDwarf(dummy, offset);
1032 }
1033
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001034#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001035 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1036 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1037 if (offset)
1038 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1039 return true;
1040 }
1041 return false;
1042 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001043#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001044
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001045#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001046 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1047 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1048 if (offset)
1049 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1050 return true;
1051 }
1052 return false;
1053 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001054#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001055
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001056#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001057 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1058 return true;
1059 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001060#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001061
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001062#if defined(_LIBUNWIND_TARGET_PPC64)
1063 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1064 return true;
1065 }
1066#endif
1067
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001068#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001069 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1070 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1071 if (offset)
1072 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1073 return true;
1074 }
1075 return false;
1076 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001077#endif
John Baldwin56441d42017-12-12 21:43:36 +00001078
1079#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1080 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1081 return true;
1082 }
1083#endif
1084
John Baldwin541a4352018-01-09 17:07:18 +00001085#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1086 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001087 return true;
1088 }
1089#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001090
1091#if defined(_LIBUNWIND_TARGET_SPARC)
1092 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1093#endif
1094
Sam Elliott81f7e172019-12-16 16:35:17 +00001095#if defined (_LIBUNWIND_TARGET_RISCV)
1096 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1097 return true;
1098 }
1099#endif
1100
Ranjeet Singh421231a2017-03-31 15:28:06 +00001101#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001102
Ranjeet Singh421231a2017-03-31 15:28:06 +00001103#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001104 compact_unwind_encoding_t dwarfEncoding() const {
1105 R dummy;
1106 return dwarfEncoding(dummy);
1107 }
1108
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001109#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001110 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1111 return UNWIND_X86_64_MODE_DWARF;
1112 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001113#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001114
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001115#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001116 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1117 return UNWIND_X86_MODE_DWARF;
1118 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001119#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001120
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001121#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001122 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1123 return 0;
1124 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001125#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001126
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001127#if defined(_LIBUNWIND_TARGET_PPC64)
1128 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1129 return 0;
1130 }
1131#endif
1132
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001133#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001134 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1135 return UNWIND_ARM64_MODE_DWARF;
1136 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001137#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001138
Martin Storsjoa72285f2017-11-02 08:16:16 +00001139#if defined(_LIBUNWIND_TARGET_ARM)
1140 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1141 return 0;
1142 }
1143#endif
1144
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001145#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001146 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1147 return 0;
1148 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001149#endif
John Baldwin56441d42017-12-12 21:43:36 +00001150
Brian Cainb432c472020-04-09 00:14:02 -05001151#if defined (_LIBUNWIND_TARGET_HEXAGON)
1152 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1153 return 0;
1154 }
1155#endif
1156
John Baldwin56441d42017-12-12 21:43:36 +00001157#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1158 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1159 return 0;
1160 }
1161#endif
1162
John Baldwin541a4352018-01-09 17:07:18 +00001163#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1164 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001165 return 0;
1166 }
1167#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001168
1169#if defined(_LIBUNWIND_TARGET_SPARC)
1170 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1171#endif
1172
Sam Elliott81f7e172019-12-16 16:35:17 +00001173#if defined (_LIBUNWIND_TARGET_RISCV)
1174 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1175 return 0;
1176 }
1177#endif
1178
Ranjeet Singh421231a2017-03-31 15:28:06 +00001179#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001180
Charles Davisfa2e6202018-08-30 21:29:00 +00001181#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1182 // For runtime environments using SEH unwind data without Windows runtime
1183 // support.
1184 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1185 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1186 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1187 /* FIXME: Implement */
1188 *base = 0;
1189 return nullptr;
1190 }
1191 bool getInfoFromSEH(pint_t pc);
1192 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1193#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1194
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001195
1196 A &_addressSpace;
1197 R _registers;
1198 unw_proc_info_t _info;
1199 bool _unwindInfoMissing;
1200 bool _isSignalFrame;
Ryan Pricharda684bed2021-01-13 16:38:36 -08001201#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1202 bool _isSigReturn = false;
1203#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001204};
1205
1206
1207template <typename A, typename R>
1208UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1209 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1210 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001211 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001212 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001213 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1214 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001215 memset(&_info, 0, sizeof(_info));
1216}
1217
1218template <typename A, typename R>
1219UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1220 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1221 memset(&_info, 0, sizeof(_info));
1222 // FIXME
1223 // fill in _registers from thread arg
1224}
1225
1226
1227template <typename A, typename R>
1228bool UnwindCursor<A, R>::validReg(int regNum) {
1229 return _registers.validRegister(regNum);
1230}
1231
1232template <typename A, typename R>
1233unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1234 return _registers.getRegister(regNum);
1235}
1236
1237template <typename A, typename R>
1238void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1239 _registers.setRegister(regNum, (typename A::pint_t)value);
1240}
1241
1242template <typename A, typename R>
1243bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1244 return _registers.validFloatRegister(regNum);
1245}
1246
1247template <typename A, typename R>
1248unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1249 return _registers.getFloatRegister(regNum);
1250}
1251
1252template <typename A, typename R>
1253void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1254 _registers.setFloatRegister(regNum, value);
1255}
1256
1257template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1258 _registers.jumpto();
1259}
1260
1261#ifdef __arm__
1262template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1263 _registers.saveVFPAsX();
1264}
1265#endif
1266
1267template <typename A, typename R>
1268const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1269 return _registers.getRegisterName(regNum);
1270}
1271
1272template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1273 return _isSignalFrame;
1274}
1275
Charles Davisfa2e6202018-08-30 21:29:00 +00001276#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1277
Ranjeet Singh421231a2017-03-31 15:28:06 +00001278#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001279template<typename A>
1280struct EHABISectionIterator {
1281 typedef EHABISectionIterator _Self;
1282
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001283 typedef typename A::pint_t value_type;
1284 typedef typename A::pint_t* pointer;
1285 typedef typename A::pint_t& reference;
1286 typedef size_t size_type;
1287 typedef size_t difference_type;
1288
1289 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1290 return _Self(addressSpace, sects, 0);
1291 }
1292 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001293 return _Self(addressSpace, sects,
1294 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001295 }
1296
1297 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1298 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1299
1300 _Self& operator++() { ++_i; return *this; }
1301 _Self& operator+=(size_t a) { _i += a; return *this; }
1302 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1303 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1304
1305 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1306 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1307
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001308 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001309
1310 bool operator==(const _Self& other) const {
1311 assert(_addressSpace == other._addressSpace);
1312 assert(_sects == other._sects);
1313 return _i == other._i;
1314 }
1315
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001316 bool operator!=(const _Self& other) const {
1317 assert(_addressSpace == other._addressSpace);
1318 assert(_sects == other._sects);
1319 return _i != other._i;
1320 }
1321
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001322 typename A::pint_t operator*() const { return functionAddress(); }
1323
1324 typename A::pint_t functionAddress() const {
1325 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1326 EHABIIndexEntry, _i, functionOffset);
1327 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1328 }
1329
1330 typename A::pint_t dataAddress() {
1331 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1332 EHABIIndexEntry, _i, data);
1333 return indexAddr;
1334 }
1335
1336 private:
1337 size_t _i;
1338 A* _addressSpace;
1339 const UnwindInfoSections* _sects;
1340};
1341
Petr Hosekac0d9e02019-01-29 22:26:18 +00001342namespace {
1343
1344template <typename A>
1345EHABISectionIterator<A> EHABISectionUpperBound(
1346 EHABISectionIterator<A> first,
1347 EHABISectionIterator<A> last,
1348 typename A::pint_t value) {
1349 size_t len = last - first;
1350 while (len > 0) {
1351 size_t l2 = len / 2;
1352 EHABISectionIterator<A> m = first + l2;
1353 if (value < *m) {
1354 len = l2;
1355 } else {
1356 first = ++m;
1357 len -= l2 + 1;
1358 }
1359 }
1360 return first;
1361}
1362
1363}
1364
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001365template <typename A, typename R>
1366bool UnwindCursor<A, R>::getInfoFromEHABISection(
1367 pint_t pc,
1368 const UnwindInfoSections &sects) {
1369 EHABISectionIterator<A> begin =
1370 EHABISectionIterator<A>::begin(_addressSpace, sects);
1371 EHABISectionIterator<A> end =
1372 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001373 if (begin == end)
1374 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001375
Petr Hosekac0d9e02019-01-29 22:26:18 +00001376 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001377 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001378 return false;
1379 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1380
1381 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001382 // If an exception is thrown from a function, corresponding to the last entry
1383 // in the table, we don't really know the function extent and have to choose a
1384 // value for nextPC. Choosing max() will allow the range check during trace to
1385 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001386 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001387 pint_t indexDataAddr = itThisPC.dataAddress();
1388
1389 if (indexDataAddr == 0)
1390 return false;
1391
1392 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1393 if (indexData == UNW_EXIDX_CANTUNWIND)
1394 return false;
1395
1396 // If the high bit is set, the exception handling table entry is inline inside
1397 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001398 // the table points at an offset in the exception handling table (section 5
1399 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001400 pint_t exceptionTableAddr;
1401 uint32_t exceptionTableData;
1402 bool isSingleWordEHT;
1403 if (indexData & 0x80000000) {
1404 exceptionTableAddr = indexDataAddr;
1405 // TODO(ajwong): Should this data be 0?
1406 exceptionTableData = indexData;
1407 isSingleWordEHT = true;
1408 } else {
1409 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1410 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1411 isSingleWordEHT = false;
1412 }
1413
1414 // Now we know the 3 things:
1415 // exceptionTableAddr -- exception handler table entry.
1416 // exceptionTableData -- the data inside the first word of the eht entry.
1417 // isSingleWordEHT -- whether the entry is in the index.
1418 unw_word_t personalityRoutine = 0xbadf00d;
1419 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001420 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001421
1422 // If the high bit in the exception handling table entry is set, the entry is
1423 // in compact form (section 6.3 EHABI).
1424 if (exceptionTableData & 0x80000000) {
1425 // Grab the index of the personality routine from the compact form.
1426 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1427 uint32_t extraWords = 0;
1428 switch (choice) {
1429 case 0:
1430 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1431 extraWords = 0;
1432 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001433 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001434 break;
1435 case 1:
1436 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1437 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1438 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001439 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001440 break;
1441 case 2:
1442 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1443 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1444 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001445 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001446 break;
1447 default:
1448 _LIBUNWIND_ABORT("unknown personality routine");
1449 return false;
1450 }
1451
1452 if (isSingleWordEHT) {
1453 if (extraWords != 0) {
1454 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1455 "requires extra words");
1456 return false;
1457 }
1458 }
1459 } else {
1460 pint_t personalityAddr =
1461 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1462 personalityRoutine = personalityAddr;
1463
1464 // ARM EHABI # 6.2, # 9.2
1465 //
1466 // +---- ehtp
1467 // v
1468 // +--------------------------------------+
1469 // | +--------+--------+--------+-------+ |
1470 // | |0| prel31 to personalityRoutine | |
1471 // | +--------+--------+--------+-------+ |
1472 // | | N | unwind opcodes | | <-- UnwindData
1473 // | +--------+--------+--------+-------+ |
1474 // | | Word 2 unwind opcodes | |
1475 // | +--------+--------+--------+-------+ |
1476 // | ... |
1477 // | +--------+--------+--------+-------+ |
1478 // | | Word N unwind opcodes | |
1479 // | +--------+--------+--------+-------+ |
1480 // | | LSDA | | <-- lsda
1481 // | | ... | |
1482 // | +--------+--------+--------+-------+ |
1483 // +--------------------------------------+
1484
1485 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1486 uint32_t FirstDataWord = *UnwindData;
1487 size_t N = ((FirstDataWord >> 24) & 0xff);
1488 size_t NDataWords = N + 1;
1489 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1490 }
1491
1492 _info.start_ip = thisPC;
1493 _info.end_ip = nextPC;
1494 _info.handler = personalityRoutine;
1495 _info.unwind_info = exceptionTableAddr;
1496 _info.lsda = lsda;
1497 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001498 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001499
1500 return true;
1501}
1502#endif
1503
Ranjeet Singh421231a2017-03-31 15:28:06 +00001504#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001505template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001506bool UnwindCursor<A, R>::getInfoFromFdeCie(
1507 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1508 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1509 uintptr_t dso_base) {
1510 typename CFI_Parser<A>::PrologInfo prolog;
1511 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1512 R::getArch(), &prolog)) {
1513 // Save off parsed FDE info
1514 _info.start_ip = fdeInfo.pcStart;
1515 _info.end_ip = fdeInfo.pcEnd;
1516 _info.lsda = fdeInfo.lsda;
1517 _info.handler = cieInfo.personality;
1518 // Some frameless functions need SP altered when resuming in function, so
1519 // propagate spExtraArgSize.
1520 _info.gp = prolog.spExtraArgSize;
1521 _info.flags = 0;
1522 _info.format = dwarfEncoding();
1523 _info.unwind_info = fdeInfo.fdeStart;
1524 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1525 _info.extra = static_cast<unw_word_t>(dso_base);
1526 return true;
1527 }
1528 return false;
1529}
1530
1531template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001532bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1533 const UnwindInfoSections &sects,
1534 uint32_t fdeSectionOffsetHint) {
1535 typename CFI_Parser<A>::FDE_Info fdeInfo;
1536 typename CFI_Parser<A>::CIE_Info cieInfo;
1537 bool foundFDE = false;
1538 bool foundInCache = false;
1539 // If compact encoding table gave offset into dwarf section, go directly there
1540 if (fdeSectionOffsetHint != 0) {
1541 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001542 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001543 sects.dwarf_section + fdeSectionOffsetHint,
1544 &fdeInfo, &cieInfo);
1545 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001546#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001547 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1548 foundFDE = EHHeaderParser<A>::findFDE(
1549 _addressSpace, pc, sects.dwarf_index_section,
1550 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1551 }
1552#endif
1553 if (!foundFDE) {
1554 // otherwise, search cache of previously found FDEs.
1555 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1556 if (cachedFDE != 0) {
1557 foundFDE =
1558 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001559 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001560 cachedFDE, &fdeInfo, &cieInfo);
1561 foundInCache = foundFDE;
1562 }
1563 }
1564 if (!foundFDE) {
1565 // Still not found, do full scan of __eh_frame section.
1566 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001567 sects.dwarf_section_length, 0,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001568 &fdeInfo, &cieInfo);
1569 }
1570 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001571 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001572 // Add to cache (to make next lookup faster) if we had no hint
1573 // and there was no index.
1574 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001575 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001576 if (sects.dwarf_index_section == 0)
1577 #endif
1578 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1579 fdeInfo.fdeStart);
1580 }
1581 return true;
1582 }
1583 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001584 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001585 return false;
1586}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001587#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001588
1589
Ranjeet Singh421231a2017-03-31 15:28:06 +00001590#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001591template <typename A, typename R>
1592bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1593 const UnwindInfoSections &sects) {
1594 const bool log = false;
1595 if (log)
1596 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1597 (uint64_t)pc, (uint64_t)sects.dso_base);
1598
1599 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1600 sects.compact_unwind_section);
1601 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1602 return false;
1603
1604 // do a binary search of top level index to find page with unwind info
1605 pint_t targetFunctionOffset = pc - sects.dso_base;
1606 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1607 sects.compact_unwind_section
1608 + sectionHeader.indexSectionOffset());
1609 uint32_t low = 0;
1610 uint32_t high = sectionHeader.indexCount();
1611 uint32_t last = high - 1;
1612 while (low < high) {
1613 uint32_t mid = (low + high) / 2;
1614 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1615 //mid, low, high, topIndex.functionOffset(mid));
1616 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1617 if ((mid == last) ||
1618 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1619 low = mid;
1620 break;
1621 } else {
1622 low = mid + 1;
1623 }
1624 } else {
1625 high = mid;
1626 }
1627 }
1628 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1629 const uint32_t firstLevelNextPageFunctionOffset =
1630 topIndex.functionOffset(low + 1);
1631 const pint_t secondLevelAddr =
1632 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1633 const pint_t lsdaArrayStartAddr =
1634 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1635 const pint_t lsdaArrayEndAddr =
1636 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1637 if (log)
1638 fprintf(stderr, "\tfirst level search for result index=%d "
1639 "to secondLevelAddr=0x%llX\n",
1640 low, (uint64_t) secondLevelAddr);
1641 // do a binary search of second level page index
1642 uint32_t encoding = 0;
1643 pint_t funcStart = 0;
1644 pint_t funcEnd = 0;
1645 pint_t lsda = 0;
1646 pint_t personality = 0;
1647 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1648 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1649 // regular page
1650 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1651 secondLevelAddr);
1652 UnwindSectionRegularArray<A> pageIndex(
1653 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1654 // binary search looks for entry with e where index[e].offset <= pc <
1655 // index[e+1].offset
1656 if (log)
1657 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1658 "regular page starting at secondLevelAddr=0x%llX\n",
1659 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1660 low = 0;
1661 high = pageHeader.entryCount();
1662 while (low < high) {
1663 uint32_t mid = (low + high) / 2;
1664 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1665 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1666 // at end of table
1667 low = mid;
1668 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1669 break;
1670 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1671 // next is too big, so we found it
1672 low = mid;
1673 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1674 break;
1675 } else {
1676 low = mid + 1;
1677 }
1678 } else {
1679 high = mid;
1680 }
1681 }
1682 encoding = pageIndex.encoding(low);
1683 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1684 if (pc < funcStart) {
1685 if (log)
1686 fprintf(
1687 stderr,
1688 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1689 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1690 return false;
1691 }
1692 if (pc > funcEnd) {
1693 if (log)
1694 fprintf(
1695 stderr,
1696 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1697 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1698 return false;
1699 }
1700 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1701 // compressed page
1702 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1703 secondLevelAddr);
1704 UnwindSectionCompressedArray<A> pageIndex(
1705 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1706 const uint32_t targetFunctionPageOffset =
1707 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1708 // binary search looks for entry with e where index[e].offset <= pc <
1709 // index[e+1].offset
1710 if (log)
1711 fprintf(stderr, "\tbinary search of compressed page starting at "
1712 "secondLevelAddr=0x%llX\n",
1713 (uint64_t) secondLevelAddr);
1714 low = 0;
1715 last = pageHeader.entryCount() - 1;
1716 high = pageHeader.entryCount();
1717 while (low < high) {
1718 uint32_t mid = (low + high) / 2;
1719 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1720 if ((mid == last) ||
1721 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1722 low = mid;
1723 break;
1724 } else {
1725 low = mid + 1;
1726 }
1727 } else {
1728 high = mid;
1729 }
1730 }
1731 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1732 + sects.dso_base;
1733 if (low < last)
1734 funcEnd =
1735 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1736 + sects.dso_base;
1737 else
1738 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1739 if (pc < funcStart) {
Nico Weber5f424e32021-07-02 10:03:32 -04001740 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1741 "not in second level compressed unwind table. "
1742 "funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001743 (uint64_t) pc, (uint64_t) funcStart);
1744 return false;
1745 }
1746 if (pc > funcEnd) {
Nico Weber5f424e32021-07-02 10:03:32 -04001747 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX "
1748 "not in second level compressed unwind table. "
1749 "funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001750 (uint64_t) pc, (uint64_t) funcEnd);
1751 return false;
1752 }
1753 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1754 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1755 // encoding is in common table in section header
1756 encoding = _addressSpace.get32(
1757 sects.compact_unwind_section +
1758 sectionHeader.commonEncodingsArraySectionOffset() +
1759 encodingIndex * sizeof(uint32_t));
1760 } else {
1761 // encoding is in page specific table
1762 uint16_t pageEncodingIndex =
1763 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1764 encoding = _addressSpace.get32(secondLevelAddr +
1765 pageHeader.encodingsPageOffset() +
1766 pageEncodingIndex * sizeof(uint32_t));
1767 }
1768 } else {
Nico Weber5f424e32021-07-02 10:03:32 -04001769 _LIBUNWIND_DEBUG_LOG(
1770 "malformed __unwind_info at 0x%0llX bad second level page",
1771 (uint64_t)sects.compact_unwind_section);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001772 return false;
1773 }
1774
1775 // look up LSDA, if encoding says function has one
1776 if (encoding & UNWIND_HAS_LSDA) {
1777 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1778 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1779 low = 0;
1780 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1781 sizeof(unwind_info_section_header_lsda_index_entry);
1782 // binary search looks for entry with exact match for functionOffset
1783 if (log)
1784 fprintf(stderr,
1785 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1786 funcStartOffset);
1787 while (low < high) {
1788 uint32_t mid = (low + high) / 2;
1789 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1790 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1791 break;
1792 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1793 low = mid + 1;
1794 } else {
1795 high = mid;
1796 }
1797 }
1798 if (lsda == 0) {
1799 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001800 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001801 encoding, (uint64_t) pc);
1802 return false;
1803 }
1804 }
1805
Louis Dionned3bbbc32020-08-11 15:24:21 -04001806 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001807 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1808 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1809 if (personalityIndex != 0) {
1810 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001811 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001812 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001813 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001814 encoding, personalityIndex,
1815 sectionHeader.personalityArrayCount());
1816 return false;
1817 }
1818 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1819 sects.compact_unwind_section +
1820 sectionHeader.personalityArraySectionOffset() +
1821 personalityIndex * sizeof(uint32_t));
1822 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1823 personality = _addressSpace.getP(personalityPointer);
1824 if (log)
1825 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1826 "personalityDelta=0x%08X, personality=0x%08llX\n",
1827 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1828 }
1829
1830 if (log)
1831 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1832 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1833 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1834 _info.start_ip = funcStart;
1835 _info.end_ip = funcEnd;
1836 _info.lsda = lsda;
1837 _info.handler = personality;
1838 _info.gp = 0;
1839 _info.flags = 0;
1840 _info.format = encoding;
1841 _info.unwind_info = 0;
1842 _info.unwind_info_size = 0;
1843 _info.extra = sects.dso_base;
1844 return true;
1845}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001846#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001847
1848
Charles Davisfa2e6202018-08-30 21:29:00 +00001849#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1850template <typename A, typename R>
1851bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1852 pint_t base;
1853 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1854 if (!unwindEntry) {
1855 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1856 return false;
1857 }
1858 _info.gp = 0;
1859 _info.flags = 0;
1860 _info.format = 0;
1861 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1862 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1863 _info.extra = base;
1864 _info.start_ip = base + unwindEntry->BeginAddress;
1865#ifdef _LIBUNWIND_TARGET_X86_64
1866 _info.end_ip = base + unwindEntry->EndAddress;
1867 // Only fill in the handler and LSDA if they're stale.
1868 if (pc != getLastPC()) {
1869 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1870 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1871 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1872 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1873 // these structures.)
1874 // N.B. UNWIND_INFO structs are DWORD-aligned.
1875 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1876 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1877 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1878 if (*handler) {
1879 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1880 } else
1881 _info.handler = 0;
1882 } else {
1883 _info.lsda = 0;
1884 _info.handler = 0;
1885 }
1886 }
1887#elif defined(_LIBUNWIND_TARGET_ARM)
1888 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1889 _info.lsda = 0; // FIXME
1890 _info.handler = 0; // FIXME
1891#endif
1892 setLastPC(pc);
1893 return true;
1894}
1895#endif
1896
1897
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001898template <typename A, typename R>
1899void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
Ryan Pricharda684bed2021-01-13 16:38:36 -08001900#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
1901 _isSigReturn = false;
1902#endif
1903
1904 pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
Ranjeet Singh421231a2017-03-31 15:28:06 +00001905#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001906 // Remove the thumb bit so the IP represents the actual instruction address.
1907 // This matches the behaviour of _Unwind_GetIP on arm.
1908 pc &= (pint_t)~0x1;
1909#endif
1910
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001911 // Exit early if at the top of the stack.
1912 if (pc == 0) {
1913 _unwindInfoMissing = true;
1914 return;
1915 }
1916
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001917 // If the last line of a function is a "throw" the compiler sometimes
1918 // emits no instructions after the call to __cxa_throw. This means
1919 // the return address is actually the start of the next function.
1920 // To disambiguate this, back up the pc when we know it is a return
1921 // address.
1922 if (isReturnAddress)
1923 --pc;
1924
1925 // Ask address space object to find unwind sections for this pc.
1926 UnwindInfoSections sects;
1927 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001928#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001929 // If there is a compact unwind encoding table, look there first.
1930 if (sects.compact_unwind_section != 0) {
1931 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001932 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001933 // Found info in table, done unless encoding says to use dwarf.
1934 uint32_t dwarfOffset;
1935 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1936 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1937 // found info in dwarf, done
1938 return;
1939 }
1940 }
1941 #endif
1942 // If unwind table has entry, but entry says there is no unwind info,
1943 // record that we have no unwind info.
1944 if (_info.format == 0)
1945 _unwindInfoMissing = true;
1946 return;
1947 }
1948 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001949#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001950
Charles Davisfa2e6202018-08-30 21:29:00 +00001951#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1952 // If there is SEH unwind info, look there next.
1953 if (this->getInfoFromSEH(pc))
1954 return;
1955#endif
1956
Ranjeet Singh421231a2017-03-31 15:28:06 +00001957#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001958 // If there is dwarf unwind info, look there next.
1959 if (sects.dwarf_section != 0) {
1960 if (this->getInfoFromDwarfSection(pc, sects)) {
1961 // found info in dwarf, done
1962 return;
1963 }
1964 }
1965#endif
1966
Ranjeet Singh421231a2017-03-31 15:28:06 +00001967#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001968 // If there is ARM EHABI unwind info, look there next.
1969 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1970 return;
1971#endif
1972 }
1973
Ranjeet Singh421231a2017-03-31 15:28:06 +00001974#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001975 // There is no static unwind info for this pc. Look to see if an FDE was
1976 // dynamically registered for it.
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -07001977 pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
1978 pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001979 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001980 typename CFI_Parser<A>::FDE_Info fdeInfo;
1981 typename CFI_Parser<A>::CIE_Info cieInfo;
1982 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
1983 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001984 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001985 }
1986
1987 // Lastly, ask AddressSpace object about platform specific ways to locate
1988 // other FDEs.
1989 pint_t fde;
1990 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001991 typename CFI_Parser<A>::FDE_Info fdeInfo;
1992 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001993 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1994 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001995 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
1996 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001997 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001998 }
1999 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00002000#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002001
Ryan Pricharda684bed2021-01-13 16:38:36 -08002002#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2003 if (setInfoForSigReturn())
2004 return;
2005#endif
2006
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002007 // no unwind info, flag that we can't reliably unwind
2008 _unwindInfoMissing = true;
2009}
2010
Ryan Pricharda684bed2021-01-13 16:38:36 -08002011#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2012template <typename A, typename R>
2013bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_arm64 &) {
2014 // Look for the sigreturn trampoline. The trampoline's body is two
2015 // specific instructions (see below). Typically the trampoline comes from the
2016 // vDSO[1] (i.e. the __kernel_rt_sigreturn function). A libc might provide its
2017 // own restorer function, though, or user-mode QEMU might write a trampoline
2018 // onto the stack.
2019 //
2020 // This special code path is a fallback that is only used if the trampoline
2021 // lacks proper (e.g. DWARF) unwind info. On AArch64, a new DWARF register
2022 // constant for the PC needs to be defined before DWARF can handle a signal
2023 // trampoline. This code may segfault if the target PC is unreadable, e.g.:
2024 // - The PC points at a function compiled without unwind info, and which is
2025 // part of an execute-only mapping (e.g. using -Wl,--execute-only).
2026 // - The PC is invalid and happens to point to unreadable or unmapped memory.
2027 //
2028 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/vdso/sigreturn.S
2029 const pint_t pc = static_cast<pint_t>(this->getReg(UNW_REG_IP));
2030 // Look for instructions: mov x8, #0x8b; svc #0x0
2031 if (_addressSpace.get32(pc) == 0xd2801168 &&
2032 _addressSpace.get32(pc + 4) == 0xd4000001) {
2033 _info = {};
2034 _isSigReturn = true;
2035 return true;
2036 }
2037 return false;
2038}
2039
2040template <typename A, typename R>
2041int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
2042 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2043 // - 128-byte siginfo struct
2044 // - ucontext struct:
2045 // - 8-byte long (uc_flags)
2046 // - 8-byte pointer (uc_link)
2047 // - 24-byte stack_t
2048 // - 128-byte signal set
2049 // - 8 bytes of padding because sigcontext has 16-byte alignment
2050 // - sigcontext/mcontext_t
2051 // [1] https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c
2052 const pint_t kOffsetSpToSigcontext = (128 + 8 + 8 + 24 + 128 + 8); // 304
2053
2054 // Offsets from sigcontext to each register.
2055 const pint_t kOffsetGprs = 8; // offset to "__u64 regs[31]" field
2056 const pint_t kOffsetSp = 256; // offset to "__u64 sp" field
2057 const pint_t kOffsetPc = 264; // offset to "__u64 pc" field
2058
2059 pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2060
2061 for (int i = 0; i <= 30; ++i) {
2062 uint64_t value = _addressSpace.get64(sigctx + kOffsetGprs +
2063 static_cast<pint_t>(i * 8));
Fangrui Song5f263002021-08-20 14:26:27 -07002064 _registers.setRegister(UNW_AARCH64_X0 + i, value);
Ryan Pricharda684bed2021-01-13 16:38:36 -08002065 }
2066 _registers.setSP(_addressSpace.get64(sigctx + kOffsetSp));
2067 _registers.setIP(_addressSpace.get64(sigctx + kOffsetPc));
2068 _isSignalFrame = true;
2069 return UNW_STEP_SUCCESS;
2070}
2071#endif // defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2072
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002073template <typename A, typename R>
2074int UnwindCursor<A, R>::step() {
2075 // Bottom of stack is defined is when unwind info cannot be found.
2076 if (_unwindInfoMissing)
2077 return UNW_STEP_END;
2078
2079 // Use unwinding info to modify register set as if function returned.
2080 int result;
Ryan Pricharda684bed2021-01-13 16:38:36 -08002081#if defined(_LIBUNWIND_TARGET_LINUX) && defined(_LIBUNWIND_TARGET_AARCH64)
2082 if (_isSigReturn) {
2083 result = this->stepThroughSigReturn();
2084 } else
2085#endif
2086 {
Ranjeet Singh421231a2017-03-31 15:28:06 +00002087#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002088 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00002089#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002090 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002091#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002092 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00002093#elif defined(_LIBUNWIND_ARM_EHABI)
Ryan Pricharda684bed2021-01-13 16:38:36 -08002094 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002095#else
2096 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00002097 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002098 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00002099 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002100#endif
Ryan Pricharda684bed2021-01-13 16:38:36 -08002101 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002102
2103 // update info based on new PC
2104 if (result == UNW_STEP_SUCCESS) {
2105 this->setInfoBasedOnIPRegister(true);
2106 if (_unwindInfoMissing)
2107 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002108 }
2109
2110 return result;
2111}
2112
2113template <typename A, typename R>
2114void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002115 if (_unwindInfoMissing)
2116 memset(info, 0, sizeof(*info));
2117 else
2118 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002119}
2120
2121template <typename A, typename R>
2122bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2123 unw_word_t *offset) {
2124 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2125 buf, bufLen, offset);
2126}
2127
2128} // namespace libunwind
2129
2130#endif // __UNWINDCURSOR_HPP__