blob: 9f8fa65107b410c1bc46e363b2b31e9fbc2bc495 [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)
623 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
624 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
625 _msContext.Sp = r.getRegister(UNW_REG_SP);
626 _msContext.Pc = r.getRegister(UNW_REG_IP);
627 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
628 _msContext.V[i - UNW_ARM64_D0].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)
653 if (regNum >= UNW_ARM64_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;
702 default: return _msContext.X[regNum - UNW_ARM64_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;
752 case UNW_ARM64_X0:
753 case UNW_ARM64_X1:
754 case UNW_ARM64_X2:
755 case UNW_ARM64_X3:
756 case UNW_ARM64_X4:
757 case UNW_ARM64_X5:
758 case UNW_ARM64_X6:
759 case UNW_ARM64_X7:
760 case UNW_ARM64_X8:
761 case UNW_ARM64_X9:
762 case UNW_ARM64_X10:
763 case UNW_ARM64_X11:
764 case UNW_ARM64_X12:
765 case UNW_ARM64_X13:
766 case UNW_ARM64_X14:
767 case UNW_ARM64_X15:
768 case UNW_ARM64_X16:
769 case UNW_ARM64_X17:
770 case UNW_ARM64_X18:
771 case UNW_ARM64_X19:
772 case UNW_ARM64_X20:
773 case UNW_ARM64_X21:
774 case UNW_ARM64_X22:
775 case UNW_ARM64_X23:
776 case UNW_ARM64_X24:
777 case UNW_ARM64_X25:
778 case UNW_ARM64_X26:
779 case UNW_ARM64_X27:
780 case UNW_ARM64_X28:
781 case UNW_ARM64_FP:
782 case UNW_ARM64_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)
795 if (regNum >= UNW_ARM64_D0 && 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)
823 return _msContext.V[regNum - UNW_ARM64_D0].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)
851 _msContext.V[regNum - UNW_ARM64_D0].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
Ranjeet Singh421231a2017-03-31 15:28:06 +0000928#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Ryan Prichard1ae2b942020-08-18 02:31:38 -0700929 bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
930 const typename CFI_Parser<A>::CIE_Info &cieInfo,
931 pint_t pc, uintptr_t dso_base);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000932 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
933 uint32_t fdeSectionOffsetHint=0);
934 int stepWithDwarfFDE() {
935 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
936 (pint_t)this->getReg(UNW_REG_IP),
937 (pint_t)_info.unwind_info,
Sterling Augustineb6a66392019-10-31 12:45:20 -0700938 _registers, _isSignalFrame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000939 }
940#endif
941
Ranjeet Singh421231a2017-03-31 15:28:06 +0000942#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000943 bool getInfoFromCompactEncodingSection(pint_t pc,
944 const UnwindInfoSections &sects);
945 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000946 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000947 if ( compactSaysUseDwarf() )
948 return stepWithDwarfFDE();
949 #endif
950 R dummy;
951 return stepWithCompactEncoding(dummy);
952 }
953
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000954#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000955 int stepWithCompactEncoding(Registers_x86_64 &) {
956 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
957 _info.format, _info.start_ip, _addressSpace, _registers);
958 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000959#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000960
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000961#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000962 int stepWithCompactEncoding(Registers_x86 &) {
963 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
964 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
965 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000966#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000967
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000968#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000969 int stepWithCompactEncoding(Registers_ppc &) {
970 return UNW_EINVAL;
971 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000972#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000973
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000974#if defined(_LIBUNWIND_TARGET_PPC64)
975 int stepWithCompactEncoding(Registers_ppc64 &) {
976 return UNW_EINVAL;
977 }
978#endif
979
980
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000981#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000982 int stepWithCompactEncoding(Registers_arm64 &) {
983 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
984 _info.format, _info.start_ip, _addressSpace, _registers);
985 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000986#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000987
John Baldwin56441d42017-12-12 21:43:36 +0000988#if defined(_LIBUNWIND_TARGET_MIPS_O32)
989 int stepWithCompactEncoding(Registers_mips_o32 &) {
990 return UNW_EINVAL;
991 }
992#endif
993
John Baldwin541a4352018-01-09 17:07:18 +0000994#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
995 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000996 return UNW_EINVAL;
997 }
998#endif
999
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001000#if defined(_LIBUNWIND_TARGET_SPARC)
1001 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
1002#endif
1003
Sam Elliott81f7e172019-12-16 16:35:17 +00001004#if defined (_LIBUNWIND_TARGET_RISCV)
1005 int stepWithCompactEncoding(Registers_riscv &) {
1006 return UNW_EINVAL;
1007 }
1008#endif
1009
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001010 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
1011 R dummy;
1012 return compactSaysUseDwarf(dummy, offset);
1013 }
1014
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001015#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001016 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1017 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1018 if (offset)
1019 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1020 return true;
1021 }
1022 return false;
1023 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001024#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001025
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001026#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001027 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1028 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1029 if (offset)
1030 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1031 return true;
1032 }
1033 return false;
1034 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001035#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001036
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001037#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001038 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1039 return true;
1040 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001041#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001042
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001043#if defined(_LIBUNWIND_TARGET_PPC64)
1044 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1045 return true;
1046 }
1047#endif
1048
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001049#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001050 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1051 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1052 if (offset)
1053 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1054 return true;
1055 }
1056 return false;
1057 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001058#endif
John Baldwin56441d42017-12-12 21:43:36 +00001059
1060#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1061 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1062 return true;
1063 }
1064#endif
1065
John Baldwin541a4352018-01-09 17:07:18 +00001066#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1067 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001068 return true;
1069 }
1070#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001071
1072#if defined(_LIBUNWIND_TARGET_SPARC)
1073 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1074#endif
1075
Sam Elliott81f7e172019-12-16 16:35:17 +00001076#if defined (_LIBUNWIND_TARGET_RISCV)
1077 bool compactSaysUseDwarf(Registers_riscv &, uint32_t *) const {
1078 return true;
1079 }
1080#endif
1081
Ranjeet Singh421231a2017-03-31 15:28:06 +00001082#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001083
Ranjeet Singh421231a2017-03-31 15:28:06 +00001084#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001085 compact_unwind_encoding_t dwarfEncoding() const {
1086 R dummy;
1087 return dwarfEncoding(dummy);
1088 }
1089
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001090#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001091 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1092 return UNWIND_X86_64_MODE_DWARF;
1093 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001094#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001095
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001096#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001097 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1098 return UNWIND_X86_MODE_DWARF;
1099 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001100#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001101
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001102#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001103 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1104 return 0;
1105 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001106#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001107
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001108#if defined(_LIBUNWIND_TARGET_PPC64)
1109 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1110 return 0;
1111 }
1112#endif
1113
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001114#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001115 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1116 return UNWIND_ARM64_MODE_DWARF;
1117 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001118#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001119
Martin Storsjoa72285f2017-11-02 08:16:16 +00001120#if defined(_LIBUNWIND_TARGET_ARM)
1121 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1122 return 0;
1123 }
1124#endif
1125
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001126#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001127 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1128 return 0;
1129 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001130#endif
John Baldwin56441d42017-12-12 21:43:36 +00001131
Brian Cainb432c472020-04-09 00:14:02 -05001132#if defined (_LIBUNWIND_TARGET_HEXAGON)
1133 compact_unwind_encoding_t dwarfEncoding(Registers_hexagon &) const {
1134 return 0;
1135 }
1136#endif
1137
John Baldwin56441d42017-12-12 21:43:36 +00001138#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1139 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1140 return 0;
1141 }
1142#endif
1143
John Baldwin541a4352018-01-09 17:07:18 +00001144#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1145 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001146 return 0;
1147 }
1148#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +00001149
1150#if defined(_LIBUNWIND_TARGET_SPARC)
1151 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1152#endif
1153
Sam Elliott81f7e172019-12-16 16:35:17 +00001154#if defined (_LIBUNWIND_TARGET_RISCV)
1155 compact_unwind_encoding_t dwarfEncoding(Registers_riscv &) const {
1156 return 0;
1157 }
1158#endif
1159
Ranjeet Singh421231a2017-03-31 15:28:06 +00001160#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001161
Charles Davisfa2e6202018-08-30 21:29:00 +00001162#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1163 // For runtime environments using SEH unwind data without Windows runtime
1164 // support.
1165 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1166 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1167 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1168 /* FIXME: Implement */
1169 *base = 0;
1170 return nullptr;
1171 }
1172 bool getInfoFromSEH(pint_t pc);
1173 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1174#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1175
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001176
1177 A &_addressSpace;
1178 R _registers;
1179 unw_proc_info_t _info;
1180 bool _unwindInfoMissing;
1181 bool _isSignalFrame;
1182};
1183
1184
1185template <typename A, typename R>
1186UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1187 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1188 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001189 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001190 "UnwindCursor<> does not fit in unw_cursor_t");
Martin Storsjö1c0575e2020-08-17 22:41:58 +03001191 static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1192 "UnwindCursor<> requires more alignment than unw_cursor_t");
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001193 memset(&_info, 0, sizeof(_info));
1194}
1195
1196template <typename A, typename R>
1197UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1198 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1199 memset(&_info, 0, sizeof(_info));
1200 // FIXME
1201 // fill in _registers from thread arg
1202}
1203
1204
1205template <typename A, typename R>
1206bool UnwindCursor<A, R>::validReg(int regNum) {
1207 return _registers.validRegister(regNum);
1208}
1209
1210template <typename A, typename R>
1211unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1212 return _registers.getRegister(regNum);
1213}
1214
1215template <typename A, typename R>
1216void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1217 _registers.setRegister(regNum, (typename A::pint_t)value);
1218}
1219
1220template <typename A, typename R>
1221bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1222 return _registers.validFloatRegister(regNum);
1223}
1224
1225template <typename A, typename R>
1226unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1227 return _registers.getFloatRegister(regNum);
1228}
1229
1230template <typename A, typename R>
1231void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1232 _registers.setFloatRegister(regNum, value);
1233}
1234
1235template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1236 _registers.jumpto();
1237}
1238
1239#ifdef __arm__
1240template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1241 _registers.saveVFPAsX();
1242}
1243#endif
1244
1245template <typename A, typename R>
1246const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1247 return _registers.getRegisterName(regNum);
1248}
1249
1250template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1251 return _isSignalFrame;
1252}
1253
Charles Davisfa2e6202018-08-30 21:29:00 +00001254#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1255
Ranjeet Singh421231a2017-03-31 15:28:06 +00001256#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001257template<typename A>
1258struct EHABISectionIterator {
1259 typedef EHABISectionIterator _Self;
1260
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001261 typedef typename A::pint_t value_type;
1262 typedef typename A::pint_t* pointer;
1263 typedef typename A::pint_t& reference;
1264 typedef size_t size_type;
1265 typedef size_t difference_type;
1266
1267 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1268 return _Self(addressSpace, sects, 0);
1269 }
1270 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001271 return _Self(addressSpace, sects,
1272 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001273 }
1274
1275 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1276 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1277
1278 _Self& operator++() { ++_i; return *this; }
1279 _Self& operator+=(size_t a) { _i += a; return *this; }
1280 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1281 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1282
1283 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1284 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1285
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001286 size_t operator-(const _Self& other) const { return _i - other._i; }
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001287
1288 bool operator==(const _Self& other) const {
1289 assert(_addressSpace == other._addressSpace);
1290 assert(_sects == other._sects);
1291 return _i == other._i;
1292 }
1293
Saleem Abdulrasoolfbff6b12020-06-18 08:51:44 -07001294 bool operator!=(const _Self& other) const {
1295 assert(_addressSpace == other._addressSpace);
1296 assert(_sects == other._sects);
1297 return _i != other._i;
1298 }
1299
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001300 typename A::pint_t operator*() const { return functionAddress(); }
1301
1302 typename A::pint_t functionAddress() const {
1303 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1304 EHABIIndexEntry, _i, functionOffset);
1305 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1306 }
1307
1308 typename A::pint_t dataAddress() {
1309 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1310 EHABIIndexEntry, _i, data);
1311 return indexAddr;
1312 }
1313
1314 private:
1315 size_t _i;
1316 A* _addressSpace;
1317 const UnwindInfoSections* _sects;
1318};
1319
Petr Hosekac0d9e02019-01-29 22:26:18 +00001320namespace {
1321
1322template <typename A>
1323EHABISectionIterator<A> EHABISectionUpperBound(
1324 EHABISectionIterator<A> first,
1325 EHABISectionIterator<A> last,
1326 typename A::pint_t value) {
1327 size_t len = last - first;
1328 while (len > 0) {
1329 size_t l2 = len / 2;
1330 EHABISectionIterator<A> m = first + l2;
1331 if (value < *m) {
1332 len = l2;
1333 } else {
1334 first = ++m;
1335 len -= l2 + 1;
1336 }
1337 }
1338 return first;
1339}
1340
1341}
1342
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001343template <typename A, typename R>
1344bool UnwindCursor<A, R>::getInfoFromEHABISection(
1345 pint_t pc,
1346 const UnwindInfoSections &sects) {
1347 EHABISectionIterator<A> begin =
1348 EHABISectionIterator<A>::begin(_addressSpace, sects);
1349 EHABISectionIterator<A> end =
1350 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001351 if (begin == end)
1352 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001353
Petr Hosekac0d9e02019-01-29 22:26:18 +00001354 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001355 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001356 return false;
1357 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1358
1359 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001360 // If an exception is thrown from a function, corresponding to the last entry
1361 // in the table, we don't really know the function extent and have to choose a
1362 // value for nextPC. Choosing max() will allow the range check during trace to
1363 // succeed.
Petr Hosekac0d9e02019-01-29 22:26:18 +00001364 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001365 pint_t indexDataAddr = itThisPC.dataAddress();
1366
1367 if (indexDataAddr == 0)
1368 return false;
1369
1370 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1371 if (indexData == UNW_EXIDX_CANTUNWIND)
1372 return false;
1373
1374 // If the high bit is set, the exception handling table entry is inline inside
1375 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
Nico Weberd999d542020-02-03 14:16:52 -05001376 // the table points at an offset in the exception handling table (section 5
1377 // EHABI).
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001378 pint_t exceptionTableAddr;
1379 uint32_t exceptionTableData;
1380 bool isSingleWordEHT;
1381 if (indexData & 0x80000000) {
1382 exceptionTableAddr = indexDataAddr;
1383 // TODO(ajwong): Should this data be 0?
1384 exceptionTableData = indexData;
1385 isSingleWordEHT = true;
1386 } else {
1387 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1388 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1389 isSingleWordEHT = false;
1390 }
1391
1392 // Now we know the 3 things:
1393 // exceptionTableAddr -- exception handler table entry.
1394 // exceptionTableData -- the data inside the first word of the eht entry.
1395 // isSingleWordEHT -- whether the entry is in the index.
1396 unw_word_t personalityRoutine = 0xbadf00d;
1397 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001398 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001399
1400 // If the high bit in the exception handling table entry is set, the entry is
1401 // in compact form (section 6.3 EHABI).
1402 if (exceptionTableData & 0x80000000) {
1403 // Grab the index of the personality routine from the compact form.
1404 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1405 uint32_t extraWords = 0;
1406 switch (choice) {
1407 case 0:
1408 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1409 extraWords = 0;
1410 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001411 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001412 break;
1413 case 1:
1414 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1415 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1416 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001417 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001418 break;
1419 case 2:
1420 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1421 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1422 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001423 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001424 break;
1425 default:
1426 _LIBUNWIND_ABORT("unknown personality routine");
1427 return false;
1428 }
1429
1430 if (isSingleWordEHT) {
1431 if (extraWords != 0) {
1432 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1433 "requires extra words");
1434 return false;
1435 }
1436 }
1437 } else {
1438 pint_t personalityAddr =
1439 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1440 personalityRoutine = personalityAddr;
1441
1442 // ARM EHABI # 6.2, # 9.2
1443 //
1444 // +---- ehtp
1445 // v
1446 // +--------------------------------------+
1447 // | +--------+--------+--------+-------+ |
1448 // | |0| prel31 to personalityRoutine | |
1449 // | +--------+--------+--------+-------+ |
1450 // | | N | unwind opcodes | | <-- UnwindData
1451 // | +--------+--------+--------+-------+ |
1452 // | | Word 2 unwind opcodes | |
1453 // | +--------+--------+--------+-------+ |
1454 // | ... |
1455 // | +--------+--------+--------+-------+ |
1456 // | | Word N unwind opcodes | |
1457 // | +--------+--------+--------+-------+ |
1458 // | | LSDA | | <-- lsda
1459 // | | ... | |
1460 // | +--------+--------+--------+-------+ |
1461 // +--------------------------------------+
1462
1463 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1464 uint32_t FirstDataWord = *UnwindData;
1465 size_t N = ((FirstDataWord >> 24) & 0xff);
1466 size_t NDataWords = N + 1;
1467 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1468 }
1469
1470 _info.start_ip = thisPC;
1471 _info.end_ip = nextPC;
1472 _info.handler = personalityRoutine;
1473 _info.unwind_info = exceptionTableAddr;
1474 _info.lsda = lsda;
1475 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
Nico Weberd999d542020-02-03 14:16:52 -05001476 _info.flags = (isSingleWordEHT ? 1 : 0) | (scope32 ? 0x2 : 0); // Use enum?
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001477
1478 return true;
1479}
1480#endif
1481
Ranjeet Singh421231a2017-03-31 15:28:06 +00001482#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001483template <typename A, typename R>
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001484bool UnwindCursor<A, R>::getInfoFromFdeCie(
1485 const typename CFI_Parser<A>::FDE_Info &fdeInfo,
1486 const typename CFI_Parser<A>::CIE_Info &cieInfo, pint_t pc,
1487 uintptr_t dso_base) {
1488 typename CFI_Parser<A>::PrologInfo prolog;
1489 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1490 R::getArch(), &prolog)) {
1491 // Save off parsed FDE info
1492 _info.start_ip = fdeInfo.pcStart;
1493 _info.end_ip = fdeInfo.pcEnd;
1494 _info.lsda = fdeInfo.lsda;
1495 _info.handler = cieInfo.personality;
1496 // Some frameless functions need SP altered when resuming in function, so
1497 // propagate spExtraArgSize.
1498 _info.gp = prolog.spExtraArgSize;
1499 _info.flags = 0;
1500 _info.format = dwarfEncoding();
1501 _info.unwind_info = fdeInfo.fdeStart;
1502 _info.unwind_info_size = static_cast<uint32_t>(fdeInfo.fdeLength);
1503 _info.extra = static_cast<unw_word_t>(dso_base);
1504 return true;
1505 }
1506 return false;
1507}
1508
1509template <typename A, typename R>
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001510bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1511 const UnwindInfoSections &sects,
1512 uint32_t fdeSectionOffsetHint) {
1513 typename CFI_Parser<A>::FDE_Info fdeInfo;
1514 typename CFI_Parser<A>::CIE_Info cieInfo;
1515 bool foundFDE = false;
1516 bool foundInCache = false;
1517 // If compact encoding table gave offset into dwarf section, go directly there
1518 if (fdeSectionOffsetHint != 0) {
1519 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001520 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001521 sects.dwarf_section + fdeSectionOffsetHint,
1522 &fdeInfo, &cieInfo);
1523 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001524#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001525 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1526 foundFDE = EHHeaderParser<A>::findFDE(
1527 _addressSpace, pc, sects.dwarf_index_section,
1528 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1529 }
1530#endif
1531 if (!foundFDE) {
1532 // otherwise, search cache of previously found FDEs.
1533 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1534 if (cachedFDE != 0) {
1535 foundFDE =
1536 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001537 sects.dwarf_section_length,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001538 cachedFDE, &fdeInfo, &cieInfo);
1539 foundInCache = foundFDE;
1540 }
1541 }
1542 if (!foundFDE) {
1543 // Still not found, do full scan of __eh_frame section.
1544 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
Ryan Prichard0cff8572020-09-16 01:22:55 -07001545 sects.dwarf_section_length, 0,
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001546 &fdeInfo, &cieInfo);
1547 }
1548 if (foundFDE) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001549 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, sects.dso_base)) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001550 // Add to cache (to make next lookup faster) if we had no hint
1551 // and there was no index.
1552 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001553 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001554 if (sects.dwarf_index_section == 0)
1555 #endif
1556 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1557 fdeInfo.fdeStart);
1558 }
1559 return true;
1560 }
1561 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001562 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001563 return false;
1564}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001565#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001566
1567
Ranjeet Singh421231a2017-03-31 15:28:06 +00001568#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001569template <typename A, typename R>
1570bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1571 const UnwindInfoSections &sects) {
1572 const bool log = false;
1573 if (log)
1574 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1575 (uint64_t)pc, (uint64_t)sects.dso_base);
1576
1577 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1578 sects.compact_unwind_section);
1579 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1580 return false;
1581
1582 // do a binary search of top level index to find page with unwind info
1583 pint_t targetFunctionOffset = pc - sects.dso_base;
1584 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1585 sects.compact_unwind_section
1586 + sectionHeader.indexSectionOffset());
1587 uint32_t low = 0;
1588 uint32_t high = sectionHeader.indexCount();
1589 uint32_t last = high - 1;
1590 while (low < high) {
1591 uint32_t mid = (low + high) / 2;
1592 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1593 //mid, low, high, topIndex.functionOffset(mid));
1594 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1595 if ((mid == last) ||
1596 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1597 low = mid;
1598 break;
1599 } else {
1600 low = mid + 1;
1601 }
1602 } else {
1603 high = mid;
1604 }
1605 }
1606 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1607 const uint32_t firstLevelNextPageFunctionOffset =
1608 topIndex.functionOffset(low + 1);
1609 const pint_t secondLevelAddr =
1610 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1611 const pint_t lsdaArrayStartAddr =
1612 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1613 const pint_t lsdaArrayEndAddr =
1614 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1615 if (log)
1616 fprintf(stderr, "\tfirst level search for result index=%d "
1617 "to secondLevelAddr=0x%llX\n",
1618 low, (uint64_t) secondLevelAddr);
1619 // do a binary search of second level page index
1620 uint32_t encoding = 0;
1621 pint_t funcStart = 0;
1622 pint_t funcEnd = 0;
1623 pint_t lsda = 0;
1624 pint_t personality = 0;
1625 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1626 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1627 // regular page
1628 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1629 secondLevelAddr);
1630 UnwindSectionRegularArray<A> pageIndex(
1631 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1632 // binary search looks for entry with e where index[e].offset <= pc <
1633 // index[e+1].offset
1634 if (log)
1635 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1636 "regular page starting at secondLevelAddr=0x%llX\n",
1637 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1638 low = 0;
1639 high = pageHeader.entryCount();
1640 while (low < high) {
1641 uint32_t mid = (low + high) / 2;
1642 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1643 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1644 // at end of table
1645 low = mid;
1646 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1647 break;
1648 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1649 // next is too big, so we found it
1650 low = mid;
1651 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1652 break;
1653 } else {
1654 low = mid + 1;
1655 }
1656 } else {
1657 high = mid;
1658 }
1659 }
1660 encoding = pageIndex.encoding(low);
1661 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1662 if (pc < funcStart) {
1663 if (log)
1664 fprintf(
1665 stderr,
1666 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1667 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1668 return false;
1669 }
1670 if (pc > funcEnd) {
1671 if (log)
1672 fprintf(
1673 stderr,
1674 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1675 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1676 return false;
1677 }
1678 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1679 // compressed page
1680 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1681 secondLevelAddr);
1682 UnwindSectionCompressedArray<A> pageIndex(
1683 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1684 const uint32_t targetFunctionPageOffset =
1685 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1686 // binary search looks for entry with e where index[e].offset <= pc <
1687 // index[e+1].offset
1688 if (log)
1689 fprintf(stderr, "\tbinary search of compressed page starting at "
1690 "secondLevelAddr=0x%llX\n",
1691 (uint64_t) secondLevelAddr);
1692 low = 0;
1693 last = pageHeader.entryCount() - 1;
1694 high = pageHeader.entryCount();
1695 while (low < high) {
1696 uint32_t mid = (low + high) / 2;
1697 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1698 if ((mid == last) ||
1699 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1700 low = mid;
1701 break;
1702 } else {
1703 low = mid + 1;
1704 }
1705 } else {
1706 high = mid;
1707 }
1708 }
1709 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1710 + sects.dso_base;
1711 if (low < last)
1712 funcEnd =
1713 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1714 + sects.dso_base;
1715 else
1716 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1717 if (pc < funcStart) {
1718 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001719 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001720 (uint64_t) pc, (uint64_t) funcStart);
1721 return false;
1722 }
1723 if (pc > funcEnd) {
1724 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001725 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001726 (uint64_t) pc, (uint64_t) funcEnd);
1727 return false;
1728 }
1729 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1730 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1731 // encoding is in common table in section header
1732 encoding = _addressSpace.get32(
1733 sects.compact_unwind_section +
1734 sectionHeader.commonEncodingsArraySectionOffset() +
1735 encodingIndex * sizeof(uint32_t));
1736 } else {
1737 // encoding is in page specific table
1738 uint16_t pageEncodingIndex =
1739 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1740 encoding = _addressSpace.get32(secondLevelAddr +
1741 pageHeader.encodingsPageOffset() +
1742 pageEncodingIndex * sizeof(uint32_t));
1743 }
1744 } else {
1745 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001746 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001747 (uint64_t) sects.compact_unwind_section);
1748 return false;
1749 }
1750
1751 // look up LSDA, if encoding says function has one
1752 if (encoding & UNWIND_HAS_LSDA) {
1753 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1754 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1755 low = 0;
1756 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1757 sizeof(unwind_info_section_header_lsda_index_entry);
1758 // binary search looks for entry with exact match for functionOffset
1759 if (log)
1760 fprintf(stderr,
1761 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1762 funcStartOffset);
1763 while (low < high) {
1764 uint32_t mid = (low + high) / 2;
1765 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1766 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1767 break;
1768 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1769 low = mid + 1;
1770 } else {
1771 high = mid;
1772 }
1773 }
1774 if (lsda == 0) {
1775 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001776 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001777 encoding, (uint64_t) pc);
1778 return false;
1779 }
1780 }
1781
Louis Dionned3bbbc32020-08-11 15:24:21 -04001782 // extract personality routine, if encoding says function has one
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001783 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1784 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1785 if (personalityIndex != 0) {
1786 --personalityIndex; // change 1-based to zero-based index
Louis Dionne8c360cb2020-08-11 15:29:00 -04001787 if (personalityIndex >= sectionHeader.personalityArrayCount()) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001788 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Louis Dionne103afa42019-04-22 15:40:50 +00001789 "but personality table has only %d entries",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001790 encoding, personalityIndex,
1791 sectionHeader.personalityArrayCount());
1792 return false;
1793 }
1794 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1795 sects.compact_unwind_section +
1796 sectionHeader.personalityArraySectionOffset() +
1797 personalityIndex * sizeof(uint32_t));
1798 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1799 personality = _addressSpace.getP(personalityPointer);
1800 if (log)
1801 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1802 "personalityDelta=0x%08X, personality=0x%08llX\n",
1803 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1804 }
1805
1806 if (log)
1807 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1808 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1809 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1810 _info.start_ip = funcStart;
1811 _info.end_ip = funcEnd;
1812 _info.lsda = lsda;
1813 _info.handler = personality;
1814 _info.gp = 0;
1815 _info.flags = 0;
1816 _info.format = encoding;
1817 _info.unwind_info = 0;
1818 _info.unwind_info_size = 0;
1819 _info.extra = sects.dso_base;
1820 return true;
1821}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001822#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001823
1824
Charles Davisfa2e6202018-08-30 21:29:00 +00001825#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1826template <typename A, typename R>
1827bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1828 pint_t base;
1829 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1830 if (!unwindEntry) {
1831 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1832 return false;
1833 }
1834 _info.gp = 0;
1835 _info.flags = 0;
1836 _info.format = 0;
1837 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1838 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1839 _info.extra = base;
1840 _info.start_ip = base + unwindEntry->BeginAddress;
1841#ifdef _LIBUNWIND_TARGET_X86_64
1842 _info.end_ip = base + unwindEntry->EndAddress;
1843 // Only fill in the handler and LSDA if they're stale.
1844 if (pc != getLastPC()) {
1845 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1846 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1847 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1848 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1849 // these structures.)
1850 // N.B. UNWIND_INFO structs are DWORD-aligned.
1851 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1852 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1853 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1854 if (*handler) {
1855 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1856 } else
1857 _info.handler = 0;
1858 } else {
1859 _info.lsda = 0;
1860 _info.handler = 0;
1861 }
1862 }
1863#elif defined(_LIBUNWIND_TARGET_ARM)
1864 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1865 _info.lsda = 0; // FIXME
1866 _info.handler = 0; // FIXME
1867#endif
1868 setLastPC(pc);
1869 return true;
1870}
1871#endif
1872
1873
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001874template <typename A, typename R>
1875void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1876 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001877#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001878 // Remove the thumb bit so the IP represents the actual instruction address.
1879 // This matches the behaviour of _Unwind_GetIP on arm.
1880 pc &= (pint_t)~0x1;
1881#endif
1882
Sterling Augustinec100c4d2020-03-30 15:20:26 -07001883 // Exit early if at the top of the stack.
1884 if (pc == 0) {
1885 _unwindInfoMissing = true;
1886 return;
1887 }
1888
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001889 // If the last line of a function is a "throw" the compiler sometimes
1890 // emits no instructions after the call to __cxa_throw. This means
1891 // the return address is actually the start of the next function.
1892 // To disambiguate this, back up the pc when we know it is a return
1893 // address.
1894 if (isReturnAddress)
1895 --pc;
1896
1897 // Ask address space object to find unwind sections for this pc.
1898 UnwindInfoSections sects;
1899 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001900#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001901 // If there is a compact unwind encoding table, look there first.
1902 if (sects.compact_unwind_section != 0) {
1903 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001904 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001905 // Found info in table, done unless encoding says to use dwarf.
1906 uint32_t dwarfOffset;
1907 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1908 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1909 // found info in dwarf, done
1910 return;
1911 }
1912 }
1913 #endif
1914 // If unwind table has entry, but entry says there is no unwind info,
1915 // record that we have no unwind info.
1916 if (_info.format == 0)
1917 _unwindInfoMissing = true;
1918 return;
1919 }
1920 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001921#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001922
Charles Davisfa2e6202018-08-30 21:29:00 +00001923#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1924 // If there is SEH unwind info, look there next.
1925 if (this->getInfoFromSEH(pc))
1926 return;
1927#endif
1928
Ranjeet Singh421231a2017-03-31 15:28:06 +00001929#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001930 // If there is dwarf unwind info, look there next.
1931 if (sects.dwarf_section != 0) {
1932 if (this->getInfoFromDwarfSection(pc, sects)) {
1933 // found info in dwarf, done
1934 return;
1935 }
1936 }
1937#endif
1938
Ranjeet Singh421231a2017-03-31 15:28:06 +00001939#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001940 // If there is ARM EHABI unwind info, look there next.
1941 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1942 return;
1943#endif
1944 }
1945
Ranjeet Singh421231a2017-03-31 15:28:06 +00001946#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001947 // There is no static unwind info for this pc. Look to see if an FDE was
1948 // dynamically registered for it.
Ryan Prichard2cfcb8c2020-09-09 15:43:35 -07001949 pint_t cachedFDE = DwarfFDECache<A>::findFDE(DwarfFDECache<A>::kSearchAll,
1950 pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001951 if (cachedFDE != 0) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001952 typename CFI_Parser<A>::FDE_Info fdeInfo;
1953 typename CFI_Parser<A>::CIE_Info cieInfo;
1954 if (!CFI_Parser<A>::decodeFDE(_addressSpace, cachedFDE, &fdeInfo, &cieInfo))
1955 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001956 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001957 }
1958
1959 // Lastly, ask AddressSpace object about platform specific ways to locate
1960 // other FDEs.
1961 pint_t fde;
1962 if (_addressSpace.findOtherFDE(pc, fde)) {
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001963 typename CFI_Parser<A>::FDE_Info fdeInfo;
1964 typename CFI_Parser<A>::CIE_Info cieInfo;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001965 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1966 // Double check this FDE is for a function that includes the pc.
Ryan Prichard1ae2b942020-08-18 02:31:38 -07001967 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd))
1968 if (getInfoFromFdeCie(fdeInfo, cieInfo, pc, 0))
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001969 return;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001970 }
1971 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001972#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001973
1974 // no unwind info, flag that we can't reliably unwind
1975 _unwindInfoMissing = true;
1976}
1977
1978template <typename A, typename R>
1979int UnwindCursor<A, R>::step() {
1980 // Bottom of stack is defined is when unwind info cannot be found.
1981 if (_unwindInfoMissing)
1982 return UNW_STEP_END;
1983
1984 // Use unwinding info to modify register set as if function returned.
1985 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001986#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001987 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001988#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1989 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001990#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001991 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001992#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001993 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001994#else
1995 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001996 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001997 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001998 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001999#endif
2000
2001 // update info based on new PC
2002 if (result == UNW_STEP_SUCCESS) {
2003 this->setInfoBasedOnIPRegister(true);
2004 if (_unwindInfoMissing)
2005 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002006 }
2007
2008 return result;
2009}
2010
2011template <typename A, typename R>
2012void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
Saleem Abdulrasool78b42cc2019-09-20 15:53:42 +00002013 if (_unwindInfoMissing)
2014 memset(info, 0, sizeof(*info));
2015 else
2016 *info = _info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002017}
2018
2019template <typename A, typename R>
2020bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
2021 unw_word_t *offset) {
2022 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
2023 buf, bufLen, offset);
2024}
2025
2026} // namespace libunwind
2027
2028#endif // __UNWINDCURSOR_HPP__