blob: e2de6d56a33d942c93b6a10d8067a10cc6d89c19 [file] [log] [blame]
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.hpp ---------------------------===//
2//
Chandler Carruthb81a9422019-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 Abdulrasool675df582015-04-24 19:39:17 +00006//
7//
Ed Maste17473fd2016-08-30 13:08:21 +00008// C++ interface to lower levels of libunwind
Saleem Abdulrasool675df582015-04-24 19:39:17 +00009//===----------------------------------------------------------------------===//
10
11#ifndef __UNWINDCURSOR_HPP__
12#define __UNWINDCURSOR_HPP__
13
Saleem Abdulrasool675df582015-04-24 19:39:17 +000014#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
Saleem Abdulrasool675df582015-04-24 19:39:17 +000017#include <unwind.h>
18
Charles Davis1f89d782018-08-30 21:29:00 +000019#ifdef _WIN32
20 #include <windows.h>
21 #include <ntverp.h>
22#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +000023#ifdef __APPLE__
24 #include <mach-o/dyld.h>
25#endif
26
Charles Davis1f89d782018-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 Abdulrasool675df582015-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 Storsjo12339e42017-10-23 19:29:36 +000073#include "RWMutex.hpp"
Saleem Abdulrasool675df582015-04-24 19:39:17 +000074#include "Unwind-EHABI.h"
75
76namespace libunwind {
77
Ranjeet Singh7d674132017-03-31 15:28:06 +000078#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-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:
84 static pint_t findFDE(pint_t mh, pint_t pc);
85 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
86 static void removeAllIn(pint_t mh);
87 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
88 unw_word_t ip_end,
89 unw_word_t fde, unw_word_t mh));
90
91private:
92
93 struct entry {
94 pint_t mh;
95 pint_t ip_start;
96 pint_t ip_end;
97 pint_t fde;
98 };
99
100 // These fields are all static to avoid needing an initializer.
101 // There is only one instance of this class per process.
Martin Storsjo12339e42017-10-23 19:29:36 +0000102 static RWMutex _lock;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000103#ifdef __APPLE__
104 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
105 static bool _registeredForDyldUnloads;
106#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000107 static entry *_buffer;
108 static entry *_bufferUsed;
109 static entry *_bufferEnd;
110 static entry _initialBuffer[64];
111};
112
113template <typename A>
114typename DwarfFDECache<A>::entry *
115DwarfFDECache<A>::_buffer = _initialBuffer;
116
117template <typename A>
118typename DwarfFDECache<A>::entry *
119DwarfFDECache<A>::_bufferUsed = _initialBuffer;
120
121template <typename A>
122typename DwarfFDECache<A>::entry *
123DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
124
125template <typename A>
126typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
127
128template <typename A>
Martin Storsjo12339e42017-10-23 19:29:36 +0000129RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000130
131#ifdef __APPLE__
132template <typename A>
133bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
134#endif
135
136template <typename A>
137typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
138 pint_t result = 0;
Martin Storsjo12339e42017-10-23 19:29:36 +0000139 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000140 for (entry *p = _buffer; p < _bufferUsed; ++p) {
141 if ((mh == p->mh) || (mh == 0)) {
142 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
143 result = p->fde;
144 break;
145 }
146 }
147 }
Martin Storsjo12339e42017-10-23 19:29:36 +0000148 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000149 return result;
150}
151
152template <typename A>
153void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
154 pint_t fde) {
Peter Zotov543f8482015-11-09 06:57:29 +0000155#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo12339e42017-10-23 19:29:36 +0000156 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000157 if (_bufferUsed >= _bufferEnd) {
158 size_t oldSize = (size_t)(_bufferEnd - _buffer);
159 size_t newSize = oldSize * 4;
160 // Can't use operator new (we are below it).
161 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
162 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
163 if (_buffer != _initialBuffer)
164 free(_buffer);
165 _buffer = newBuffer;
166 _bufferUsed = &newBuffer[oldSize];
167 _bufferEnd = &newBuffer[newSize];
168 }
169 _bufferUsed->mh = mh;
170 _bufferUsed->ip_start = ip_start;
171 _bufferUsed->ip_end = ip_end;
172 _bufferUsed->fde = fde;
173 ++_bufferUsed;
174#ifdef __APPLE__
175 if (!_registeredForDyldUnloads) {
176 _dyld_register_func_for_remove_image(&dyldUnloadHook);
177 _registeredForDyldUnloads = true;
178 }
179#endif
Martin Storsjo12339e42017-10-23 19:29:36 +0000180 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov543f8482015-11-09 06:57:29 +0000181#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000182}
183
184template <typename A>
185void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo12339e42017-10-23 19:29:36 +0000186 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000187 entry *d = _buffer;
188 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
189 if (s->mh != mh) {
190 if (d != s)
191 *d = *s;
192 ++d;
193 }
194 }
195 _bufferUsed = d;
Martin Storsjo12339e42017-10-23 19:29:36 +0000196 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000197}
198
199#ifdef __APPLE__
200template <typename A>
201void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
202 removeAllIn((pint_t) mh);
203}
204#endif
205
206template <typename A>
207void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
208 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Martin Storsjo12339e42017-10-23 19:29:36 +0000209 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000210 for (entry *p = _buffer; p < _bufferUsed; ++p) {
211 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
212 }
Martin Storsjo12339e42017-10-23 19:29:36 +0000213 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000214}
Ranjeet Singh7d674132017-03-31 15:28:06 +0000215#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000216
217
218#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
219
Ranjeet Singh7d674132017-03-31 15:28:06 +0000220#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000221template <typename A> class UnwindSectionHeader {
222public:
223 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
224 : _addressSpace(addressSpace), _addr(addr) {}
225
226 uint32_t version() const {
227 return _addressSpace.get32(_addr +
228 offsetof(unwind_info_section_header, version));
229 }
230 uint32_t commonEncodingsArraySectionOffset() const {
231 return _addressSpace.get32(_addr +
232 offsetof(unwind_info_section_header,
233 commonEncodingsArraySectionOffset));
234 }
235 uint32_t commonEncodingsArrayCount() const {
236 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
237 commonEncodingsArrayCount));
238 }
239 uint32_t personalityArraySectionOffset() const {
240 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
241 personalityArraySectionOffset));
242 }
243 uint32_t personalityArrayCount() const {
244 return _addressSpace.get32(
245 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
246 }
247 uint32_t indexSectionOffset() const {
248 return _addressSpace.get32(
249 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
250 }
251 uint32_t indexCount() const {
252 return _addressSpace.get32(
253 _addr + offsetof(unwind_info_section_header, indexCount));
254 }
255
256private:
257 A &_addressSpace;
258 typename A::pint_t _addr;
259};
260
261template <typename A> class UnwindSectionIndexArray {
262public:
263 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
264 : _addressSpace(addressSpace), _addr(addr) {}
265
266 uint32_t functionOffset(uint32_t index) const {
267 return _addressSpace.get32(
268 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
269 functionOffset));
270 }
271 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
272 return _addressSpace.get32(
273 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
274 secondLevelPagesSectionOffset));
275 }
276 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
277 return _addressSpace.get32(
278 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
279 lsdaIndexArraySectionOffset));
280 }
281
282private:
283 A &_addressSpace;
284 typename A::pint_t _addr;
285};
286
287template <typename A> class UnwindSectionRegularPageHeader {
288public:
289 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
290 : _addressSpace(addressSpace), _addr(addr) {}
291
292 uint32_t kind() const {
293 return _addressSpace.get32(
294 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
295 }
296 uint16_t entryPageOffset() const {
297 return _addressSpace.get16(
298 _addr + offsetof(unwind_info_regular_second_level_page_header,
299 entryPageOffset));
300 }
301 uint16_t entryCount() const {
302 return _addressSpace.get16(
303 _addr +
304 offsetof(unwind_info_regular_second_level_page_header, entryCount));
305 }
306
307private:
308 A &_addressSpace;
309 typename A::pint_t _addr;
310};
311
312template <typename A> class UnwindSectionRegularArray {
313public:
314 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
315 : _addressSpace(addressSpace), _addr(addr) {}
316
317 uint32_t functionOffset(uint32_t index) const {
318 return _addressSpace.get32(
319 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
320 functionOffset));
321 }
322 uint32_t encoding(uint32_t index) const {
323 return _addressSpace.get32(
324 _addr +
325 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
326 }
327
328private:
329 A &_addressSpace;
330 typename A::pint_t _addr;
331};
332
333template <typename A> class UnwindSectionCompressedPageHeader {
334public:
335 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
336 : _addressSpace(addressSpace), _addr(addr) {}
337
338 uint32_t kind() const {
339 return _addressSpace.get32(
340 _addr +
341 offsetof(unwind_info_compressed_second_level_page_header, kind));
342 }
343 uint16_t entryPageOffset() const {
344 return _addressSpace.get16(
345 _addr + offsetof(unwind_info_compressed_second_level_page_header,
346 entryPageOffset));
347 }
348 uint16_t entryCount() const {
349 return _addressSpace.get16(
350 _addr +
351 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
352 }
353 uint16_t encodingsPageOffset() const {
354 return _addressSpace.get16(
355 _addr + offsetof(unwind_info_compressed_second_level_page_header,
356 encodingsPageOffset));
357 }
358 uint16_t encodingsCount() const {
359 return _addressSpace.get16(
360 _addr + offsetof(unwind_info_compressed_second_level_page_header,
361 encodingsCount));
362 }
363
364private:
365 A &_addressSpace;
366 typename A::pint_t _addr;
367};
368
369template <typename A> class UnwindSectionCompressedArray {
370public:
371 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
372 : _addressSpace(addressSpace), _addr(addr) {}
373
374 uint32_t functionOffset(uint32_t index) const {
375 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
376 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
377 }
378 uint16_t encodingIndex(uint32_t index) const {
379 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
380 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
381 }
382
383private:
384 A &_addressSpace;
385 typename A::pint_t _addr;
386};
387
388template <typename A> class UnwindSectionLsdaArray {
389public:
390 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
391 : _addressSpace(addressSpace), _addr(addr) {}
392
393 uint32_t functionOffset(uint32_t index) const {
394 return _addressSpace.get32(
395 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
396 index, functionOffset));
397 }
398 uint32_t lsdaOffset(uint32_t index) const {
399 return _addressSpace.get32(
400 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
401 index, lsdaOffset));
402 }
403
404private:
405 A &_addressSpace;
406 typename A::pint_t _addr;
407};
Ranjeet Singh7d674132017-03-31 15:28:06 +0000408#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000409
410class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
411public:
412 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
413 // This avoids an unnecessary dependency to libc++abi.
414 void operator delete(void *, size_t) {}
415
416 virtual ~AbstractUnwindCursor() {}
417 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
418 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
419 virtual void setReg(int, unw_word_t) {
420 _LIBUNWIND_ABORT("setReg not implemented");
421 }
422 virtual bool validFloatReg(int) {
423 _LIBUNWIND_ABORT("validFloatReg not implemented");
424 }
425 virtual unw_fpreg_t getFloatReg(int) {
426 _LIBUNWIND_ABORT("getFloatReg not implemented");
427 }
428 virtual void setFloatReg(int, unw_fpreg_t) {
429 _LIBUNWIND_ABORT("setFloatReg not implemented");
430 }
431 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
432 virtual void getInfo(unw_proc_info_t *) {
433 _LIBUNWIND_ABORT("getInfo not implemented");
434 }
435 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
436 virtual bool isSignalFrame() {
437 _LIBUNWIND_ABORT("isSignalFrame not implemented");
438 }
439 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
440 _LIBUNWIND_ABORT("getFunctionName not implemented");
441 }
442 virtual void setInfoBasedOnIPRegister(bool = false) {
443 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
444 }
445 virtual const char *getRegisterName(int) {
446 _LIBUNWIND_ABORT("getRegisterName not implemented");
447 }
448#ifdef __arm__
449 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
450#endif
451};
452
Charles Davis1f89d782018-08-30 21:29:00 +0000453#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
454
455/// \c UnwindCursor contains all state (including all register values) during
456/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
457template <typename A, typename R>
458class UnwindCursor : public AbstractUnwindCursor {
459 typedef typename A::pint_t pint_t;
460public:
461 UnwindCursor(unw_context_t *context, A &as);
462 UnwindCursor(CONTEXT *context, A &as);
463 UnwindCursor(A &as, void *threadArg);
464 virtual ~UnwindCursor() {}
465 virtual bool validReg(int);
466 virtual unw_word_t getReg(int);
467 virtual void setReg(int, unw_word_t);
468 virtual bool validFloatReg(int);
469 virtual unw_fpreg_t getFloatReg(int);
470 virtual void setFloatReg(int, unw_fpreg_t);
471 virtual int step();
472 virtual void getInfo(unw_proc_info_t *);
473 virtual void jumpto();
474 virtual bool isSignalFrame();
475 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
476 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
477 virtual const char *getRegisterName(int num);
478#ifdef __arm__
479 virtual void saveVFPAsX();
480#endif
481
482 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
483 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
484
Martin Storsjo8ee1c592019-02-03 22:16:53 +0000485 // libunwind does not and should not depend on C++ library which means that we
486 // need our own defition of inline placement new.
487 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
488
Charles Davis1f89d782018-08-30 21:29:00 +0000489private:
490
491 pint_t getLastPC() const { return _dispContext.ControlPc; }
492 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
493 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
494 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
495 &_dispContext.ImageBase,
496 _dispContext.HistoryTable);
497 *base = _dispContext.ImageBase;
498 return _dispContext.FunctionEntry;
499 }
500 bool getInfoFromSEH(pint_t pc);
501 int stepWithSEHData() {
502 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
503 _dispContext.ImageBase,
504 _dispContext.ControlPc,
505 _dispContext.FunctionEntry,
506 _dispContext.ContextRecord,
507 &_dispContext.HandlerData,
508 &_dispContext.EstablisherFrame,
509 NULL);
510 // Update some fields of the unwind info now, since we have them.
511 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
512 if (_dispContext.LanguageHandler) {
513 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
514 } else
515 _info.handler = 0;
516 return UNW_STEP_SUCCESS;
517 }
518
519 A &_addressSpace;
520 unw_proc_info_t _info;
521 DISPATCHER_CONTEXT _dispContext;
522 CONTEXT _msContext;
523 UNWIND_HISTORY_TABLE _histTable;
524 bool _unwindInfoMissing;
525};
526
527
528template <typename A, typename R>
529UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
530 : _addressSpace(as), _unwindInfoMissing(false) {
531 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
532 "UnwindCursor<> does not fit in unw_cursor_t");
533 memset(&_info, 0, sizeof(_info));
534 memset(&_histTable, 0, sizeof(_histTable));
535 _dispContext.ContextRecord = &_msContext;
536 _dispContext.HistoryTable = &_histTable;
537 // Initialize MS context from ours.
538 R r(context);
539 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
540#if defined(_LIBUNWIND_TARGET_X86_64)
541 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
542 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
543 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
544 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
545 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
546 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
547 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
548 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
549 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
550 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
551 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
552 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
553 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
554 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
555 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
556 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
557 _msContext.Rip = r.getRegister(UNW_REG_IP);
558 union {
559 v128 v;
560 M128A m;
561 } t;
562 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
563 _msContext.Xmm0 = t.m;
564 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
565 _msContext.Xmm1 = t.m;
566 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
567 _msContext.Xmm2 = t.m;
568 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
569 _msContext.Xmm3 = t.m;
570 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
571 _msContext.Xmm4 = t.m;
572 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
573 _msContext.Xmm5 = t.m;
574 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
575 _msContext.Xmm6 = t.m;
576 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
577 _msContext.Xmm7 = t.m;
578 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
579 _msContext.Xmm8 = t.m;
580 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
581 _msContext.Xmm9 = t.m;
582 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
583 _msContext.Xmm10 = t.m;
584 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
585 _msContext.Xmm11 = t.m;
586 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
587 _msContext.Xmm12 = t.m;
588 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
589 _msContext.Xmm13 = t.m;
590 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
591 _msContext.Xmm14 = t.m;
592 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
593 _msContext.Xmm15 = t.m;
594#elif defined(_LIBUNWIND_TARGET_ARM)
595 _msContext.R0 = r.getRegister(UNW_ARM_R0);
596 _msContext.R1 = r.getRegister(UNW_ARM_R1);
597 _msContext.R2 = r.getRegister(UNW_ARM_R2);
598 _msContext.R3 = r.getRegister(UNW_ARM_R3);
599 _msContext.R4 = r.getRegister(UNW_ARM_R4);
600 _msContext.R5 = r.getRegister(UNW_ARM_R5);
601 _msContext.R6 = r.getRegister(UNW_ARM_R6);
602 _msContext.R7 = r.getRegister(UNW_ARM_R7);
603 _msContext.R8 = r.getRegister(UNW_ARM_R8);
604 _msContext.R9 = r.getRegister(UNW_ARM_R9);
605 _msContext.R10 = r.getRegister(UNW_ARM_R10);
606 _msContext.R11 = r.getRegister(UNW_ARM_R11);
607 _msContext.R12 = r.getRegister(UNW_ARM_R12);
608 _msContext.Sp = r.getRegister(UNW_ARM_SP);
609 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjod7431592018-08-31 14:56:55 +0000610 _msContext.Pc = r.getRegister(UNW_ARM_IP);
611 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davis1f89d782018-08-30 21:29:00 +0000612 union {
613 uint64_t w;
614 double d;
615 } d;
Martin Storsjod7431592018-08-31 14:56:55 +0000616 d.d = r.getFloatRegister(i);
617 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davis1f89d782018-08-30 21:29:00 +0000618 }
Martin Storsjo9defb522018-12-18 20:05:59 +0000619#elif defined(_LIBUNWIND_TARGET_AARCH64)
620 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
621 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
622 _msContext.Sp = r.getRegister(UNW_REG_SP);
623 _msContext.Pc = r.getRegister(UNW_REG_IP);
624 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
625 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
Charles Davis1f89d782018-08-30 21:29:00 +0000626#endif
627}
628
629template <typename A, typename R>
630UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
631 : _addressSpace(as), _unwindInfoMissing(false) {
632 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
633 "UnwindCursor<> does not fit in unw_cursor_t");
634 memset(&_info, 0, sizeof(_info));
635 memset(&_histTable, 0, sizeof(_histTable));
636 _dispContext.ContextRecord = &_msContext;
637 _dispContext.HistoryTable = &_histTable;
638 _msContext = *context;
639}
640
641
642template <typename A, typename R>
643bool UnwindCursor<A, R>::validReg(int regNum) {
644 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
645#if defined(_LIBUNWIND_TARGET_X86_64)
646 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
647#elif defined(_LIBUNWIND_TARGET_ARM)
648 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjo9defb522018-12-18 20:05:59 +0000649#elif defined(_LIBUNWIND_TARGET_AARCH64)
650 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davis1f89d782018-08-30 21:29:00 +0000651#endif
652 return false;
653}
654
655template <typename A, typename R>
656unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
657 switch (regNum) {
658#if defined(_LIBUNWIND_TARGET_X86_64)
659 case UNW_REG_IP: return _msContext.Rip;
660 case UNW_X86_64_RAX: return _msContext.Rax;
661 case UNW_X86_64_RDX: return _msContext.Rdx;
662 case UNW_X86_64_RCX: return _msContext.Rcx;
663 case UNW_X86_64_RBX: return _msContext.Rbx;
664 case UNW_REG_SP:
665 case UNW_X86_64_RSP: return _msContext.Rsp;
666 case UNW_X86_64_RBP: return _msContext.Rbp;
667 case UNW_X86_64_RSI: return _msContext.Rsi;
668 case UNW_X86_64_RDI: return _msContext.Rdi;
669 case UNW_X86_64_R8: return _msContext.R8;
670 case UNW_X86_64_R9: return _msContext.R9;
671 case UNW_X86_64_R10: return _msContext.R10;
672 case UNW_X86_64_R11: return _msContext.R11;
673 case UNW_X86_64_R12: return _msContext.R12;
674 case UNW_X86_64_R13: return _msContext.R13;
675 case UNW_X86_64_R14: return _msContext.R14;
676 case UNW_X86_64_R15: return _msContext.R15;
677#elif defined(_LIBUNWIND_TARGET_ARM)
678 case UNW_ARM_R0: return _msContext.R0;
679 case UNW_ARM_R1: return _msContext.R1;
680 case UNW_ARM_R2: return _msContext.R2;
681 case UNW_ARM_R3: return _msContext.R3;
682 case UNW_ARM_R4: return _msContext.R4;
683 case UNW_ARM_R5: return _msContext.R5;
684 case UNW_ARM_R6: return _msContext.R6;
685 case UNW_ARM_R7: return _msContext.R7;
686 case UNW_ARM_R8: return _msContext.R8;
687 case UNW_ARM_R9: return _msContext.R9;
688 case UNW_ARM_R10: return _msContext.R10;
689 case UNW_ARM_R11: return _msContext.R11;
690 case UNW_ARM_R12: return _msContext.R12;
691 case UNW_REG_SP:
692 case UNW_ARM_SP: return _msContext.Sp;
693 case UNW_ARM_LR: return _msContext.Lr;
694 case UNW_REG_IP:
Martin Storsjod7431592018-08-31 14:56:55 +0000695 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjo9defb522018-12-18 20:05:59 +0000696#elif defined(_LIBUNWIND_TARGET_AARCH64)
697 case UNW_REG_SP: return _msContext.Sp;
698 case UNW_REG_IP: return _msContext.Pc;
699 default: return _msContext.X[regNum - UNW_ARM64_X0];
Charles Davis1f89d782018-08-30 21:29:00 +0000700#endif
701 }
702 _LIBUNWIND_ABORT("unsupported register");
703}
704
705template <typename A, typename R>
706void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
707 switch (regNum) {
708#if defined(_LIBUNWIND_TARGET_X86_64)
709 case UNW_REG_IP: _msContext.Rip = value; break;
710 case UNW_X86_64_RAX: _msContext.Rax = value; break;
711 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
712 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
713 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
714 case UNW_REG_SP:
715 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
716 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
717 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
718 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
719 case UNW_X86_64_R8: _msContext.R8 = value; break;
720 case UNW_X86_64_R9: _msContext.R9 = value; break;
721 case UNW_X86_64_R10: _msContext.R10 = value; break;
722 case UNW_X86_64_R11: _msContext.R11 = value; break;
723 case UNW_X86_64_R12: _msContext.R12 = value; break;
724 case UNW_X86_64_R13: _msContext.R13 = value; break;
725 case UNW_X86_64_R14: _msContext.R14 = value; break;
726 case UNW_X86_64_R15: _msContext.R15 = value; break;
727#elif defined(_LIBUNWIND_TARGET_ARM)
728 case UNW_ARM_R0: _msContext.R0 = value; break;
729 case UNW_ARM_R1: _msContext.R1 = value; break;
730 case UNW_ARM_R2: _msContext.R2 = value; break;
731 case UNW_ARM_R3: _msContext.R3 = value; break;
732 case UNW_ARM_R4: _msContext.R4 = value; break;
733 case UNW_ARM_R5: _msContext.R5 = value; break;
734 case UNW_ARM_R6: _msContext.R6 = value; break;
735 case UNW_ARM_R7: _msContext.R7 = value; break;
736 case UNW_ARM_R8: _msContext.R8 = value; break;
737 case UNW_ARM_R9: _msContext.R9 = value; break;
738 case UNW_ARM_R10: _msContext.R10 = value; break;
739 case UNW_ARM_R11: _msContext.R11 = value; break;
740 case UNW_ARM_R12: _msContext.R12 = value; break;
741 case UNW_REG_SP:
742 case UNW_ARM_SP: _msContext.Sp = value; break;
743 case UNW_ARM_LR: _msContext.Lr = value; break;
744 case UNW_REG_IP:
Martin Storsjod7431592018-08-31 14:56:55 +0000745 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjo9defb522018-12-18 20:05:59 +0000746#elif defined(_LIBUNWIND_TARGET_AARCH64)
747 case UNW_REG_SP: _msContext.Sp = value; break;
748 case UNW_REG_IP: _msContext.Pc = value; break;
749 case UNW_ARM64_X0:
750 case UNW_ARM64_X1:
751 case UNW_ARM64_X2:
752 case UNW_ARM64_X3:
753 case UNW_ARM64_X4:
754 case UNW_ARM64_X5:
755 case UNW_ARM64_X6:
756 case UNW_ARM64_X7:
757 case UNW_ARM64_X8:
758 case UNW_ARM64_X9:
759 case UNW_ARM64_X10:
760 case UNW_ARM64_X11:
761 case UNW_ARM64_X12:
762 case UNW_ARM64_X13:
763 case UNW_ARM64_X14:
764 case UNW_ARM64_X15:
765 case UNW_ARM64_X16:
766 case UNW_ARM64_X17:
767 case UNW_ARM64_X18:
768 case UNW_ARM64_X19:
769 case UNW_ARM64_X20:
770 case UNW_ARM64_X21:
771 case UNW_ARM64_X22:
772 case UNW_ARM64_X23:
773 case UNW_ARM64_X24:
774 case UNW_ARM64_X25:
775 case UNW_ARM64_X26:
776 case UNW_ARM64_X27:
777 case UNW_ARM64_X28:
778 case UNW_ARM64_FP:
779 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davis1f89d782018-08-30 21:29:00 +0000780#endif
781 default:
782 _LIBUNWIND_ABORT("unsupported register");
783 }
784}
785
786template <typename A, typename R>
787bool UnwindCursor<A, R>::validFloatReg(int regNum) {
788#if defined(_LIBUNWIND_TARGET_ARM)
789 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
790 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjo9defb522018-12-18 20:05:59 +0000791#elif defined(_LIBUNWIND_TARGET_AARCH64)
792 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
Martin Storsjo0b120ac2019-01-22 22:12:23 +0000793#else
794 (void)regNum;
Charles Davis1f89d782018-08-30 21:29:00 +0000795#endif
796 return false;
797}
798
799template <typename A, typename R>
800unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
801#if defined(_LIBUNWIND_TARGET_ARM)
802 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
803 union {
804 uint32_t w;
805 float f;
806 } d;
807 d.w = _msContext.S[regNum - UNW_ARM_S0];
808 return d.f;
809 }
810 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
811 union {
812 uint64_t w;
813 double d;
814 } d;
815 d.w = _msContext.D[regNum - UNW_ARM_D0];
816 return d.d;
817 }
818 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjo9defb522018-12-18 20:05:59 +0000819#elif defined(_LIBUNWIND_TARGET_AARCH64)
820 return _msContext.V[regNum - UNW_ARM64_D0].D[0];
Charles Davis1f89d782018-08-30 21:29:00 +0000821#else
Martin Storsjo0b120ac2019-01-22 22:12:23 +0000822 (void)regNum;
Charles Davis1f89d782018-08-30 21:29:00 +0000823 _LIBUNWIND_ABORT("float registers unimplemented");
824#endif
825}
826
827template <typename A, typename R>
828void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
829#if defined(_LIBUNWIND_TARGET_ARM)
830 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
831 union {
832 uint32_t w;
833 float f;
834 } d;
835 d.f = value;
836 _msContext.S[regNum - UNW_ARM_S0] = d.w;
837 }
838 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
839 union {
840 uint64_t w;
841 double d;
842 } d;
843 d.d = value;
844 _msContext.D[regNum - UNW_ARM_D0] = d.w;
845 }
846 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjo9defb522018-12-18 20:05:59 +0000847#elif defined(_LIBUNWIND_TARGET_AARCH64)
848 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davis1f89d782018-08-30 21:29:00 +0000849#else
Martin Storsjo0b120ac2019-01-22 22:12:23 +0000850 (void)regNum;
851 (void)value;
Charles Davis1f89d782018-08-30 21:29:00 +0000852 _LIBUNWIND_ABORT("float registers unimplemented");
853#endif
854}
855
856template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
857 RtlRestoreContext(&_msContext, nullptr);
858}
859
860#ifdef __arm__
861template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
862#endif
863
864template <typename A, typename R>
865const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo69d9b842018-12-12 22:24:42 +0000866 return R::getRegisterName(regNum);
Charles Davis1f89d782018-08-30 21:29:00 +0000867}
868
869template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
870 return false;
871}
872
873#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
874
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000875/// UnwindCursor contains all state (including all register values) during
876/// an unwind. This is normally stack allocated inside a unw_cursor_t.
877template <typename A, typename R>
878class UnwindCursor : public AbstractUnwindCursor{
879 typedef typename A::pint_t pint_t;
880public:
881 UnwindCursor(unw_context_t *context, A &as);
882 UnwindCursor(A &as, void *threadArg);
883 virtual ~UnwindCursor() {}
884 virtual bool validReg(int);
885 virtual unw_word_t getReg(int);
886 virtual void setReg(int, unw_word_t);
887 virtual bool validFloatReg(int);
888 virtual unw_fpreg_t getFloatReg(int);
889 virtual void setFloatReg(int, unw_fpreg_t);
890 virtual int step();
891 virtual void getInfo(unw_proc_info_t *);
892 virtual void jumpto();
893 virtual bool isSignalFrame();
894 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
895 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
896 virtual const char *getRegisterName(int num);
897#ifdef __arm__
898 virtual void saveVFPAsX();
899#endif
900
Petr Hosek57c06d62019-02-02 21:15:49 +0000901 // libunwind does not and should not depend on C++ library which means that we
902 // need our own defition of inline placement new.
903 static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
904
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000905private:
906
Ranjeet Singh7d674132017-03-31 15:28:06 +0000907#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000908 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chien9f323e02015-05-29 15:33:38 +0000909
910 int stepWithEHABI() {
911 size_t len = 0;
912 size_t off = 0;
913 // FIXME: Calling decode_eht_entry() here is violating the libunwind
914 // abstraction layer.
915 const uint32_t *ehtp =
916 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
917 &off, &len);
918 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
919 _URC_CONTINUE_UNWIND)
920 return UNW_STEP_END;
921 return UNW_STEP_SUCCESS;
922 }
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000923#endif
924
Ranjeet Singh7d674132017-03-31 15:28:06 +0000925#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000926 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
927 uint32_t fdeSectionOffsetHint=0);
928 int stepWithDwarfFDE() {
929 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
930 (pint_t)this->getReg(UNW_REG_IP),
931 (pint_t)_info.unwind_info,
932 _registers);
933 }
934#endif
935
Ranjeet Singh7d674132017-03-31 15:28:06 +0000936#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000937 bool getInfoFromCompactEncodingSection(pint_t pc,
938 const UnwindInfoSections &sects);
939 int stepWithCompactEncoding() {
Ranjeet Singh7d674132017-03-31 15:28:06 +0000940 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000941 if ( compactSaysUseDwarf() )
942 return stepWithDwarfFDE();
943 #endif
944 R dummy;
945 return stepWithCompactEncoding(dummy);
946 }
947
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000948#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000949 int stepWithCompactEncoding(Registers_x86_64 &) {
950 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
951 _info.format, _info.start_ip, _addressSpace, _registers);
952 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000953#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000954
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000955#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000956 int stepWithCompactEncoding(Registers_x86 &) {
957 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
958 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
959 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000960#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000961
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000962#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000963 int stepWithCompactEncoding(Registers_ppc &) {
964 return UNW_EINVAL;
965 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000966#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000967
Martin Storsjodc25db12018-01-02 22:11:30 +0000968#if defined(_LIBUNWIND_TARGET_PPC64)
969 int stepWithCompactEncoding(Registers_ppc64 &) {
970 return UNW_EINVAL;
971 }
972#endif
973
974
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000975#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000976 int stepWithCompactEncoding(Registers_arm64 &) {
977 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
978 _info.format, _info.start_ip, _addressSpace, _registers);
979 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +0000980#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000981
John Baldwin375a3662017-12-12 21:43:36 +0000982#if defined(_LIBUNWIND_TARGET_MIPS_O32)
983 int stepWithCompactEncoding(Registers_mips_o32 &) {
984 return UNW_EINVAL;
985 }
986#endif
987
John Baldwinda0af182018-01-09 17:07:18 +0000988#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
989 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin375a3662017-12-12 21:43:36 +0000990 return UNW_EINVAL;
991 }
992#endif
993
Daniel Cederman3847b162019-01-14 10:15:20 +0000994#if defined(_LIBUNWIND_TARGET_SPARC)
995 int stepWithCompactEncoding(Registers_sparc &) { return UNW_EINVAL; }
996#endif
997
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000998 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
999 R dummy;
1000 return compactSaysUseDwarf(dummy, offset);
1001 }
1002
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001003#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001004 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
1005 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
1006 if (offset)
1007 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
1008 return true;
1009 }
1010 return false;
1011 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001012#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001013
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001014#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001015 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1016 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1017 if (offset)
1018 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1019 return true;
1020 }
1021 return false;
1022 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001023#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001024
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001025#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001026 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1027 return true;
1028 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001029#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001030
Martin Storsjodc25db12018-01-02 22:11:30 +00001031#if defined(_LIBUNWIND_TARGET_PPC64)
1032 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1033 return true;
1034 }
1035#endif
1036
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001037#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001038 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1039 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1040 if (offset)
1041 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1042 return true;
1043 }
1044 return false;
1045 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001046#endif
John Baldwin375a3662017-12-12 21:43:36 +00001047
1048#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1049 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1050 return true;
1051 }
1052#endif
1053
John Baldwinda0af182018-01-09 17:07:18 +00001054#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1055 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin375a3662017-12-12 21:43:36 +00001056 return true;
1057 }
1058#endif
Daniel Cederman3847b162019-01-14 10:15:20 +00001059
1060#if defined(_LIBUNWIND_TARGET_SPARC)
1061 bool compactSaysUseDwarf(Registers_sparc &, uint32_t *) const { return true; }
1062#endif
1063
Ranjeet Singh7d674132017-03-31 15:28:06 +00001064#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001065
Ranjeet Singh7d674132017-03-31 15:28:06 +00001066#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001067 compact_unwind_encoding_t dwarfEncoding() const {
1068 R dummy;
1069 return dwarfEncoding(dummy);
1070 }
1071
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001072#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001073 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1074 return UNWIND_X86_64_MODE_DWARF;
1075 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001076#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001077
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001078#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001079 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1080 return UNWIND_X86_MODE_DWARF;
1081 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001082#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001083
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001084#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001085 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1086 return 0;
1087 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001088#endif
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001089
Martin Storsjodc25db12018-01-02 22:11:30 +00001090#if defined(_LIBUNWIND_TARGET_PPC64)
1091 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1092 return 0;
1093 }
1094#endif
1095
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001096#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001097 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1098 return UNWIND_ARM64_MODE_DWARF;
1099 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001100#endif
Peter Zotovd4255ab2015-08-31 05:26:37 +00001101
Martin Storsjo23a943b2017-11-02 08:16:16 +00001102#if defined(_LIBUNWIND_TARGET_ARM)
1103 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1104 return 0;
1105 }
1106#endif
1107
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001108#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotovd4255ab2015-08-31 05:26:37 +00001109 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1110 return 0;
1111 }
Asiri Rathnayaked2d1ea92016-05-25 12:36:34 +00001112#endif
John Baldwin375a3662017-12-12 21:43:36 +00001113
1114#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1115 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1116 return 0;
1117 }
1118#endif
1119
John Baldwinda0af182018-01-09 17:07:18 +00001120#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1121 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin375a3662017-12-12 21:43:36 +00001122 return 0;
1123 }
1124#endif
Daniel Cederman3847b162019-01-14 10:15:20 +00001125
1126#if defined(_LIBUNWIND_TARGET_SPARC)
1127 compact_unwind_encoding_t dwarfEncoding(Registers_sparc &) const { return 0; }
1128#endif
1129
Ranjeet Singh7d674132017-03-31 15:28:06 +00001130#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001131
Charles Davis1f89d782018-08-30 21:29:00 +00001132#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1133 // For runtime environments using SEH unwind data without Windows runtime
1134 // support.
1135 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1136 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1137 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1138 /* FIXME: Implement */
1139 *base = 0;
1140 return nullptr;
1141 }
1142 bool getInfoFromSEH(pint_t pc);
1143 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1144#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1145
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001146
1147 A &_addressSpace;
1148 R _registers;
1149 unw_proc_info_t _info;
1150 bool _unwindInfoMissing;
1151 bool _isSignalFrame;
1152};
1153
1154
1155template <typename A, typename R>
1156UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1157 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1158 _isSignalFrame(false) {
Asiri Rathnayake98bc6762016-05-26 21:45:54 +00001159 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001160 "UnwindCursor<> does not fit in unw_cursor_t");
1161 memset(&_info, 0, sizeof(_info));
1162}
1163
1164template <typename A, typename R>
1165UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1166 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1167 memset(&_info, 0, sizeof(_info));
1168 // FIXME
1169 // fill in _registers from thread arg
1170}
1171
1172
1173template <typename A, typename R>
1174bool UnwindCursor<A, R>::validReg(int regNum) {
1175 return _registers.validRegister(regNum);
1176}
1177
1178template <typename A, typename R>
1179unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1180 return _registers.getRegister(regNum);
1181}
1182
1183template <typename A, typename R>
1184void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1185 _registers.setRegister(regNum, (typename A::pint_t)value);
1186}
1187
1188template <typename A, typename R>
1189bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1190 return _registers.validFloatRegister(regNum);
1191}
1192
1193template <typename A, typename R>
1194unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1195 return _registers.getFloatRegister(regNum);
1196}
1197
1198template <typename A, typename R>
1199void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1200 _registers.setFloatRegister(regNum, value);
1201}
1202
1203template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1204 _registers.jumpto();
1205}
1206
1207#ifdef __arm__
1208template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1209 _registers.saveVFPAsX();
1210}
1211#endif
1212
1213template <typename A, typename R>
1214const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1215 return _registers.getRegisterName(regNum);
1216}
1217
1218template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1219 return _isSignalFrame;
1220}
1221
Charles Davis1f89d782018-08-30 21:29:00 +00001222#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1223
Ranjeet Singh7d674132017-03-31 15:28:06 +00001224#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001225template<typename A>
1226struct EHABISectionIterator {
1227 typedef EHABISectionIterator _Self;
1228
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001229 typedef typename A::pint_t value_type;
1230 typedef typename A::pint_t* pointer;
1231 typedef typename A::pint_t& reference;
1232 typedef size_t size_type;
1233 typedef size_t difference_type;
1234
1235 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1236 return _Self(addressSpace, sects, 0);
1237 }
1238 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten99794722017-03-07 15:21:57 +00001239 return _Self(addressSpace, sects,
1240 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001241 }
1242
1243 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1244 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1245
1246 _Self& operator++() { ++_i; return *this; }
1247 _Self& operator+=(size_t a) { _i += a; return *this; }
1248 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1249 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1250
1251 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1252 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1253
1254 size_t operator-(const _Self& other) { return _i - other._i; }
1255
1256 bool operator==(const _Self& other) const {
1257 assert(_addressSpace == other._addressSpace);
1258 assert(_sects == other._sects);
1259 return _i == other._i;
1260 }
1261
1262 typename A::pint_t operator*() const { return functionAddress(); }
1263
1264 typename A::pint_t functionAddress() const {
1265 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1266 EHABIIndexEntry, _i, functionOffset);
1267 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1268 }
1269
1270 typename A::pint_t dataAddress() {
1271 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1272 EHABIIndexEntry, _i, data);
1273 return indexAddr;
1274 }
1275
1276 private:
1277 size_t _i;
1278 A* _addressSpace;
1279 const UnwindInfoSections* _sects;
1280};
1281
Petr Hosek3aa86fb2019-01-29 22:26:18 +00001282namespace {
1283
1284template <typename A>
1285EHABISectionIterator<A> EHABISectionUpperBound(
1286 EHABISectionIterator<A> first,
1287 EHABISectionIterator<A> last,
1288 typename A::pint_t value) {
1289 size_t len = last - first;
1290 while (len > 0) {
1291 size_t l2 = len / 2;
1292 EHABISectionIterator<A> m = first + l2;
1293 if (value < *m) {
1294 len = l2;
1295 } else {
1296 first = ++m;
1297 len -= l2 + 1;
1298 }
1299 }
1300 return first;
1301}
1302
1303}
1304
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001305template <typename A, typename R>
1306bool UnwindCursor<A, R>::getInfoFromEHABISection(
1307 pint_t pc,
1308 const UnwindInfoSections &sects) {
1309 EHABISectionIterator<A> begin =
1310 EHABISectionIterator<A>::begin(_addressSpace, sects);
1311 EHABISectionIterator<A> end =
1312 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikovf6717c22017-07-24 09:19:32 +00001313 if (begin == end)
1314 return false;
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001315
Petr Hosek3aa86fb2019-01-29 22:26:18 +00001316 EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
Momchil Velikovf6717c22017-07-24 09:19:32 +00001317 if (itNextPC == begin)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001318 return false;
1319 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1320
1321 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikovf6717c22017-07-24 09:19:32 +00001322 // If an exception is thrown from a function, corresponding to the last entry
1323 // in the table, we don't really know the function extent and have to choose a
1324 // value for nextPC. Choosing max() will allow the range check during trace to
1325 // succeed.
Petr Hosek3aa86fb2019-01-29 22:26:18 +00001326 pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001327 pint_t indexDataAddr = itThisPC.dataAddress();
1328
1329 if (indexDataAddr == 0)
1330 return false;
1331
1332 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1333 if (indexData == UNW_EXIDX_CANTUNWIND)
1334 return false;
1335
1336 // If the high bit is set, the exception handling table entry is inline inside
1337 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1338 // the table points at an offset in the exception handling table (section 5 EHABI).
1339 pint_t exceptionTableAddr;
1340 uint32_t exceptionTableData;
1341 bool isSingleWordEHT;
1342 if (indexData & 0x80000000) {
1343 exceptionTableAddr = indexDataAddr;
1344 // TODO(ajwong): Should this data be 0?
1345 exceptionTableData = indexData;
1346 isSingleWordEHT = true;
1347 } else {
1348 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1349 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1350 isSingleWordEHT = false;
1351 }
1352
1353 // Now we know the 3 things:
1354 // exceptionTableAddr -- exception handler table entry.
1355 // exceptionTableData -- the data inside the first word of the eht entry.
1356 // isSingleWordEHT -- whether the entry is in the index.
1357 unw_word_t personalityRoutine = 0xbadf00d;
1358 bool scope32 = false;
Logan Chien9f323e02015-05-29 15:33:38 +00001359 uintptr_t lsda;
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001360
1361 // If the high bit in the exception handling table entry is set, the entry is
1362 // in compact form (section 6.3 EHABI).
1363 if (exceptionTableData & 0x80000000) {
1364 // Grab the index of the personality routine from the compact form.
1365 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1366 uint32_t extraWords = 0;
1367 switch (choice) {
1368 case 0:
1369 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1370 extraWords = 0;
1371 scope32 = false;
Logan Chien9f323e02015-05-29 15:33:38 +00001372 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001373 break;
1374 case 1:
1375 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1376 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1377 scope32 = false;
Logan Chien9f323e02015-05-29 15:33:38 +00001378 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001379 break;
1380 case 2:
1381 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1382 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1383 scope32 = true;
Logan Chien9f323e02015-05-29 15:33:38 +00001384 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001385 break;
1386 default:
1387 _LIBUNWIND_ABORT("unknown personality routine");
1388 return false;
1389 }
1390
1391 if (isSingleWordEHT) {
1392 if (extraWords != 0) {
1393 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1394 "requires extra words");
1395 return false;
1396 }
1397 }
1398 } else {
1399 pint_t personalityAddr =
1400 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1401 personalityRoutine = personalityAddr;
1402
1403 // ARM EHABI # 6.2, # 9.2
1404 //
1405 // +---- ehtp
1406 // v
1407 // +--------------------------------------+
1408 // | +--------+--------+--------+-------+ |
1409 // | |0| prel31 to personalityRoutine | |
1410 // | +--------+--------+--------+-------+ |
1411 // | | N | unwind opcodes | | <-- UnwindData
1412 // | +--------+--------+--------+-------+ |
1413 // | | Word 2 unwind opcodes | |
1414 // | +--------+--------+--------+-------+ |
1415 // | ... |
1416 // | +--------+--------+--------+-------+ |
1417 // | | Word N unwind opcodes | |
1418 // | +--------+--------+--------+-------+ |
1419 // | | LSDA | | <-- lsda
1420 // | | ... | |
1421 // | +--------+--------+--------+-------+ |
1422 // +--------------------------------------+
1423
1424 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1425 uint32_t FirstDataWord = *UnwindData;
1426 size_t N = ((FirstDataWord >> 24) & 0xff);
1427 size_t NDataWords = N + 1;
1428 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1429 }
1430
1431 _info.start_ip = thisPC;
1432 _info.end_ip = nextPC;
1433 _info.handler = personalityRoutine;
1434 _info.unwind_info = exceptionTableAddr;
1435 _info.lsda = lsda;
1436 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1437 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1438
1439 return true;
1440}
1441#endif
1442
Ranjeet Singh7d674132017-03-31 15:28:06 +00001443#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001444template <typename A, typename R>
1445bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1446 const UnwindInfoSections &sects,
1447 uint32_t fdeSectionOffsetHint) {
1448 typename CFI_Parser<A>::FDE_Info fdeInfo;
1449 typename CFI_Parser<A>::CIE_Info cieInfo;
1450 bool foundFDE = false;
1451 bool foundInCache = false;
1452 // If compact encoding table gave offset into dwarf section, go directly there
1453 if (fdeSectionOffsetHint != 0) {
1454 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1455 (uint32_t)sects.dwarf_section_length,
1456 sects.dwarf_section + fdeSectionOffsetHint,
1457 &fdeInfo, &cieInfo);
1458 }
Ranjeet Singh7d674132017-03-31 15:28:06 +00001459#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001460 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1461 foundFDE = EHHeaderParser<A>::findFDE(
1462 _addressSpace, pc, sects.dwarf_index_section,
1463 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1464 }
1465#endif
1466 if (!foundFDE) {
1467 // otherwise, search cache of previously found FDEs.
1468 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1469 if (cachedFDE != 0) {
1470 foundFDE =
1471 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1472 (uint32_t)sects.dwarf_section_length,
1473 cachedFDE, &fdeInfo, &cieInfo);
1474 foundInCache = foundFDE;
1475 }
1476 }
1477 if (!foundFDE) {
1478 // Still not found, do full scan of __eh_frame section.
1479 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1480 (uint32_t)sects.dwarf_section_length, 0,
1481 &fdeInfo, &cieInfo);
1482 }
1483 if (foundFDE) {
1484 typename CFI_Parser<A>::PrologInfo prolog;
1485 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
Daniel Cederman3847b162019-01-14 10:15:20 +00001486 R::getArch(), &prolog)) {
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001487 // Save off parsed FDE info
1488 _info.start_ip = fdeInfo.pcStart;
1489 _info.end_ip = fdeInfo.pcEnd;
1490 _info.lsda = fdeInfo.lsda;
1491 _info.handler = cieInfo.personality;
1492 _info.gp = prolog.spExtraArgSize;
1493 _info.flags = 0;
1494 _info.format = dwarfEncoding();
1495 _info.unwind_info = fdeInfo.fdeStart;
1496 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1497 _info.extra = (unw_word_t) sects.dso_base;
1498
1499 // Add to cache (to make next lookup faster) if we had no hint
1500 // and there was no index.
1501 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh7d674132017-03-31 15:28:06 +00001502 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001503 if (sects.dwarf_index_section == 0)
1504 #endif
1505 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1506 fdeInfo.fdeStart);
1507 }
1508 return true;
1509 }
1510 }
Ed Masteadc29082016-08-30 15:38:10 +00001511 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001512 return false;
1513}
Ranjeet Singh7d674132017-03-31 15:28:06 +00001514#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001515
1516
Ranjeet Singh7d674132017-03-31 15:28:06 +00001517#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001518template <typename A, typename R>
1519bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1520 const UnwindInfoSections &sects) {
1521 const bool log = false;
1522 if (log)
1523 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1524 (uint64_t)pc, (uint64_t)sects.dso_base);
1525
1526 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1527 sects.compact_unwind_section);
1528 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1529 return false;
1530
1531 // do a binary search of top level index to find page with unwind info
1532 pint_t targetFunctionOffset = pc - sects.dso_base;
1533 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1534 sects.compact_unwind_section
1535 + sectionHeader.indexSectionOffset());
1536 uint32_t low = 0;
1537 uint32_t high = sectionHeader.indexCount();
1538 uint32_t last = high - 1;
1539 while (low < high) {
1540 uint32_t mid = (low + high) / 2;
1541 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1542 //mid, low, high, topIndex.functionOffset(mid));
1543 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1544 if ((mid == last) ||
1545 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1546 low = mid;
1547 break;
1548 } else {
1549 low = mid + 1;
1550 }
1551 } else {
1552 high = mid;
1553 }
1554 }
1555 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1556 const uint32_t firstLevelNextPageFunctionOffset =
1557 topIndex.functionOffset(low + 1);
1558 const pint_t secondLevelAddr =
1559 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1560 const pint_t lsdaArrayStartAddr =
1561 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1562 const pint_t lsdaArrayEndAddr =
1563 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1564 if (log)
1565 fprintf(stderr, "\tfirst level search for result index=%d "
1566 "to secondLevelAddr=0x%llX\n",
1567 low, (uint64_t) secondLevelAddr);
1568 // do a binary search of second level page index
1569 uint32_t encoding = 0;
1570 pint_t funcStart = 0;
1571 pint_t funcEnd = 0;
1572 pint_t lsda = 0;
1573 pint_t personality = 0;
1574 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1575 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1576 // regular page
1577 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1578 secondLevelAddr);
1579 UnwindSectionRegularArray<A> pageIndex(
1580 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1581 // binary search looks for entry with e where index[e].offset <= pc <
1582 // index[e+1].offset
1583 if (log)
1584 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1585 "regular page starting at secondLevelAddr=0x%llX\n",
1586 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1587 low = 0;
1588 high = pageHeader.entryCount();
1589 while (low < high) {
1590 uint32_t mid = (low + high) / 2;
1591 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1592 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1593 // at end of table
1594 low = mid;
1595 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1596 break;
1597 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1598 // next is too big, so we found it
1599 low = mid;
1600 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1601 break;
1602 } else {
1603 low = mid + 1;
1604 }
1605 } else {
1606 high = mid;
1607 }
1608 }
1609 encoding = pageIndex.encoding(low);
1610 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1611 if (pc < funcStart) {
1612 if (log)
1613 fprintf(
1614 stderr,
1615 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1616 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1617 return false;
1618 }
1619 if (pc > funcEnd) {
1620 if (log)
1621 fprintf(
1622 stderr,
1623 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1624 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1625 return false;
1626 }
1627 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1628 // compressed page
1629 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1630 secondLevelAddr);
1631 UnwindSectionCompressedArray<A> pageIndex(
1632 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1633 const uint32_t targetFunctionPageOffset =
1634 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1635 // binary search looks for entry with e where index[e].offset <= pc <
1636 // index[e+1].offset
1637 if (log)
1638 fprintf(stderr, "\tbinary search of compressed page starting at "
1639 "secondLevelAddr=0x%llX\n",
1640 (uint64_t) secondLevelAddr);
1641 low = 0;
1642 last = pageHeader.entryCount() - 1;
1643 high = pageHeader.entryCount();
1644 while (low < high) {
1645 uint32_t mid = (low + high) / 2;
1646 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1647 if ((mid == last) ||
1648 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1649 low = mid;
1650 break;
1651 } else {
1652 low = mid + 1;
1653 }
1654 } else {
1655 high = mid;
1656 }
1657 }
1658 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1659 + sects.dso_base;
1660 if (low < last)
1661 funcEnd =
1662 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1663 + sects.dso_base;
1664 else
1665 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1666 if (pc < funcStart) {
1667 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Masteadc29082016-08-30 15:38:10 +00001668 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001669 (uint64_t) pc, (uint64_t) funcStart);
1670 return false;
1671 }
1672 if (pc > funcEnd) {
1673 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Masteadc29082016-08-30 15:38:10 +00001674 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001675 (uint64_t) pc, (uint64_t) funcEnd);
1676 return false;
1677 }
1678 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1679 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1680 // encoding is in common table in section header
1681 encoding = _addressSpace.get32(
1682 sects.compact_unwind_section +
1683 sectionHeader.commonEncodingsArraySectionOffset() +
1684 encodingIndex * sizeof(uint32_t));
1685 } else {
1686 // encoding is in page specific table
1687 uint16_t pageEncodingIndex =
1688 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1689 encoding = _addressSpace.get32(secondLevelAddr +
1690 pageHeader.encodingsPageOffset() +
1691 pageEncodingIndex * sizeof(uint32_t));
1692 }
1693 } else {
1694 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Masteadc29082016-08-30 15:38:10 +00001695 "level page",
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001696 (uint64_t) sects.compact_unwind_section);
1697 return false;
1698 }
1699
1700 // look up LSDA, if encoding says function has one
1701 if (encoding & UNWIND_HAS_LSDA) {
1702 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1703 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1704 low = 0;
1705 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1706 sizeof(unwind_info_section_header_lsda_index_entry);
1707 // binary search looks for entry with exact match for functionOffset
1708 if (log)
1709 fprintf(stderr,
1710 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1711 funcStartOffset);
1712 while (low < high) {
1713 uint32_t mid = (low + high) / 2;
1714 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1715 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1716 break;
1717 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1718 low = mid + 1;
1719 } else {
1720 high = mid;
1721 }
1722 }
1723 if (lsda == 0) {
1724 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Masteadc29082016-08-30 15:38:10 +00001725 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001726 encoding, (uint64_t) pc);
1727 return false;
1728 }
1729 }
1730
1731 // extact personality routine, if encoding says function has one
1732 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1733 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1734 if (personalityIndex != 0) {
1735 --personalityIndex; // change 1-based to zero-based index
1736 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1737 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Masteadc29082016-08-30 15:38:10 +00001738 "but personality table has only %d entires",
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001739 encoding, personalityIndex,
1740 sectionHeader.personalityArrayCount());
1741 return false;
1742 }
1743 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1744 sects.compact_unwind_section +
1745 sectionHeader.personalityArraySectionOffset() +
1746 personalityIndex * sizeof(uint32_t));
1747 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1748 personality = _addressSpace.getP(personalityPointer);
1749 if (log)
1750 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1751 "personalityDelta=0x%08X, personality=0x%08llX\n",
1752 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1753 }
1754
1755 if (log)
1756 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1757 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1758 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1759 _info.start_ip = funcStart;
1760 _info.end_ip = funcEnd;
1761 _info.lsda = lsda;
1762 _info.handler = personality;
1763 _info.gp = 0;
1764 _info.flags = 0;
1765 _info.format = encoding;
1766 _info.unwind_info = 0;
1767 _info.unwind_info_size = 0;
1768 _info.extra = sects.dso_base;
1769 return true;
1770}
Ranjeet Singh7d674132017-03-31 15:28:06 +00001771#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001772
1773
Charles Davis1f89d782018-08-30 21:29:00 +00001774#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1775template <typename A, typename R>
1776bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1777 pint_t base;
1778 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1779 if (!unwindEntry) {
1780 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1781 return false;
1782 }
1783 _info.gp = 0;
1784 _info.flags = 0;
1785 _info.format = 0;
1786 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1787 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1788 _info.extra = base;
1789 _info.start_ip = base + unwindEntry->BeginAddress;
1790#ifdef _LIBUNWIND_TARGET_X86_64
1791 _info.end_ip = base + unwindEntry->EndAddress;
1792 // Only fill in the handler and LSDA if they're stale.
1793 if (pc != getLastPC()) {
1794 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1795 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1796 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1797 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1798 // these structures.)
1799 // N.B. UNWIND_INFO structs are DWORD-aligned.
1800 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1801 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1802 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1803 if (*handler) {
1804 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1805 } else
1806 _info.handler = 0;
1807 } else {
1808 _info.lsda = 0;
1809 _info.handler = 0;
1810 }
1811 }
1812#elif defined(_LIBUNWIND_TARGET_ARM)
1813 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1814 _info.lsda = 0; // FIXME
1815 _info.handler = 0; // FIXME
1816#endif
1817 setLastPC(pc);
1818 return true;
1819}
1820#endif
1821
1822
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001823template <typename A, typename R>
1824void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1825 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh7d674132017-03-31 15:28:06 +00001826#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001827 // Remove the thumb bit so the IP represents the actual instruction address.
1828 // This matches the behaviour of _Unwind_GetIP on arm.
1829 pc &= (pint_t)~0x1;
1830#endif
1831
1832 // If the last line of a function is a "throw" the compiler sometimes
1833 // emits no instructions after the call to __cxa_throw. This means
1834 // the return address is actually the start of the next function.
1835 // To disambiguate this, back up the pc when we know it is a return
1836 // address.
1837 if (isReturnAddress)
1838 --pc;
1839
1840 // Ask address space object to find unwind sections for this pc.
1841 UnwindInfoSections sects;
1842 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh7d674132017-03-31 15:28:06 +00001843#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001844 // If there is a compact unwind encoding table, look there first.
1845 if (sects.compact_unwind_section != 0) {
1846 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh7d674132017-03-31 15:28:06 +00001847 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001848 // Found info in table, done unless encoding says to use dwarf.
1849 uint32_t dwarfOffset;
1850 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1851 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1852 // found info in dwarf, done
1853 return;
1854 }
1855 }
1856 #endif
1857 // If unwind table has entry, but entry says there is no unwind info,
1858 // record that we have no unwind info.
1859 if (_info.format == 0)
1860 _unwindInfoMissing = true;
1861 return;
1862 }
1863 }
Ranjeet Singh7d674132017-03-31 15:28:06 +00001864#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001865
Charles Davis1f89d782018-08-30 21:29:00 +00001866#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1867 // If there is SEH unwind info, look there next.
1868 if (this->getInfoFromSEH(pc))
1869 return;
1870#endif
1871
Ranjeet Singh7d674132017-03-31 15:28:06 +00001872#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001873 // If there is dwarf unwind info, look there next.
1874 if (sects.dwarf_section != 0) {
1875 if (this->getInfoFromDwarfSection(pc, sects)) {
1876 // found info in dwarf, done
1877 return;
1878 }
1879 }
1880#endif
1881
Ranjeet Singh7d674132017-03-31 15:28:06 +00001882#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001883 // If there is ARM EHABI unwind info, look there next.
1884 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1885 return;
1886#endif
1887 }
1888
Ranjeet Singh7d674132017-03-31 15:28:06 +00001889#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001890 // There is no static unwind info for this pc. Look to see if an FDE was
1891 // dynamically registered for it.
1892 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1893 if (cachedFDE != 0) {
1894 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1895 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1896 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1897 cachedFDE, &fdeInfo, &cieInfo);
1898 if (msg == NULL) {
1899 typename CFI_Parser<A>::PrologInfo prolog;
1900 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
Daniel Cederman3847b162019-01-14 10:15:20 +00001901 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001902 // save off parsed FDE info
1903 _info.start_ip = fdeInfo.pcStart;
1904 _info.end_ip = fdeInfo.pcEnd;
1905 _info.lsda = fdeInfo.lsda;
1906 _info.handler = cieInfo.personality;
1907 _info.gp = prolog.spExtraArgSize;
1908 // Some frameless functions need SP
1909 // altered when resuming in function.
1910 _info.flags = 0;
1911 _info.format = dwarfEncoding();
1912 _info.unwind_info = fdeInfo.fdeStart;
1913 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1914 _info.extra = 0;
1915 return;
1916 }
1917 }
1918 }
1919
1920 // Lastly, ask AddressSpace object about platform specific ways to locate
1921 // other FDEs.
1922 pint_t fde;
1923 if (_addressSpace.findOtherFDE(pc, fde)) {
1924 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1925 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1926 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1927 // Double check this FDE is for a function that includes the pc.
1928 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1929 typename CFI_Parser<A>::PrologInfo prolog;
Daniel Cederman3847b162019-01-14 10:15:20 +00001930 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1931 pc, R::getArch(), &prolog)) {
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001932 // save off parsed FDE info
1933 _info.start_ip = fdeInfo.pcStart;
1934 _info.end_ip = fdeInfo.pcEnd;
1935 _info.lsda = fdeInfo.lsda;
1936 _info.handler = cieInfo.personality;
1937 _info.gp = prolog.spExtraArgSize;
1938 _info.flags = 0;
1939 _info.format = dwarfEncoding();
1940 _info.unwind_info = fdeInfo.fdeStart;
1941 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1942 _info.extra = 0;
1943 return;
1944 }
1945 }
1946 }
1947 }
Ranjeet Singh7d674132017-03-31 15:28:06 +00001948#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001949
1950 // no unwind info, flag that we can't reliably unwind
1951 _unwindInfoMissing = true;
1952}
1953
1954template <typename A, typename R>
1955int UnwindCursor<A, R>::step() {
1956 // Bottom of stack is defined is when unwind info cannot be found.
1957 if (_unwindInfoMissing)
1958 return UNW_STEP_END;
1959
1960 // Use unwinding info to modify register set as if function returned.
1961 int result;
Ranjeet Singh7d674132017-03-31 15:28:06 +00001962#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001963 result = this->stepWithCompactEncoding();
Charles Davis1f89d782018-08-30 21:29:00 +00001964#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1965 result = this->stepWithSEHData();
Ranjeet Singh7d674132017-03-31 15:28:06 +00001966#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001967 result = this->stepWithDwarfFDE();
Ranjeet Singh7d674132017-03-31 15:28:06 +00001968#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chien9f323e02015-05-29 15:33:38 +00001969 result = this->stepWithEHABI();
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001970#else
1971 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davis1f89d782018-08-30 21:29:00 +00001972 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001973 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien61278582015-07-19 15:23:10 +00001974 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001975#endif
1976
1977 // update info based on new PC
1978 if (result == UNW_STEP_SUCCESS) {
1979 this->setInfoBasedOnIPRegister(true);
1980 if (_unwindInfoMissing)
1981 return UNW_STEP_END;
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001982 }
1983
1984 return result;
1985}
1986
1987template <typename A, typename R>
1988void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1989 *info = _info;
1990}
1991
1992template <typename A, typename R>
1993bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1994 unw_word_t *offset) {
1995 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1996 buf, bufLen, offset);
1997}
1998
1999} // namespace libunwind
2000
2001#endif // __UNWINDCURSOR_HPP__