blob: 9791095ad157d6a9a86bd570a681fec7cee64353 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.hpp ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//
Ed Maste6f723382016-08-30 13:08:21 +00009// C++ interface to lower levels of libunwind
Saleem Abdulrasool17552662015-04-24 19:39:17 +000010//===----------------------------------------------------------------------===//
11
12#ifndef __UNWINDCURSOR_HPP__
13#define __UNWINDCURSOR_HPP__
14
15#include <algorithm>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000019#include <unwind.h>
20
Charles Davisfa2e6202018-08-30 21:29:00 +000021#ifdef _WIN32
22 #include <windows.h>
23 #include <ntverp.h>
24#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000025#ifdef __APPLE__
26 #include <mach-o/dyld.h>
27#endif
28
Charles Davisfa2e6202018-08-30 21:29:00 +000029#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
30// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
31// earlier) SDKs.
32// MinGW-w64 has always provided this struct.
33 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
34 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
35struct _DISPATCHER_CONTEXT {
36 ULONG64 ControlPc;
37 ULONG64 ImageBase;
38 PRUNTIME_FUNCTION FunctionEntry;
39 ULONG64 EstablisherFrame;
40 ULONG64 TargetIp;
41 PCONTEXT ContextRecord;
42 PEXCEPTION_ROUTINE LanguageHandler;
43 PVOID HandlerData;
44 PUNWIND_HISTORY_TABLE HistoryTable;
45 ULONG ScopeIndex;
46 ULONG Fill0;
47};
48 #endif
49
50struct UNWIND_INFO {
51 uint8_t Version : 3;
52 uint8_t Flags : 5;
53 uint8_t SizeOfProlog;
54 uint8_t CountOfCodes;
55 uint8_t FrameRegister : 4;
56 uint8_t FrameOffset : 4;
57 uint16_t UnwindCodes[2];
58};
59
60extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
61 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
62 struct _Unwind_Context *);
63
64#endif
65
Saleem Abdulrasool17552662015-04-24 19:39:17 +000066#include "config.h"
67
68#include "AddressSpace.hpp"
69#include "CompactUnwinder.hpp"
70#include "config.h"
71#include "DwarfInstructions.hpp"
72#include "EHHeaderParser.hpp"
73#include "libunwind.h"
74#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000075#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000076#include "Unwind-EHABI.h"
77
78namespace libunwind {
79
Ranjeet Singh421231a2017-03-31 15:28:06 +000080#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000081/// Cache of recently found FDEs.
82template <typename A>
83class _LIBUNWIND_HIDDEN DwarfFDECache {
84 typedef typename A::pint_t pint_t;
85public:
86 static pint_t findFDE(pint_t mh, pint_t pc);
87 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
88 static void removeAllIn(pint_t mh);
89 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
90 unw_word_t ip_end,
91 unw_word_t fde, unw_word_t mh));
92
93private:
94
95 struct entry {
96 pint_t mh;
97 pint_t ip_start;
98 pint_t ip_end;
99 pint_t fde;
100 };
101
102 // These fields are all static to avoid needing an initializer.
103 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000104 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105#ifdef __APPLE__
106 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
107 static bool _registeredForDyldUnloads;
108#endif
109 // Can't use std::vector<> here because this code is below libc++.
110 static entry *_buffer;
111 static entry *_bufferUsed;
112 static entry *_bufferEnd;
113 static entry _initialBuffer[64];
114};
115
116template <typename A>
117typename DwarfFDECache<A>::entry *
118DwarfFDECache<A>::_buffer = _initialBuffer;
119
120template <typename A>
121typename DwarfFDECache<A>::entry *
122DwarfFDECache<A>::_bufferUsed = _initialBuffer;
123
124template <typename A>
125typename DwarfFDECache<A>::entry *
126DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
127
128template <typename A>
129typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
130
131template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000132RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000133
134#ifdef __APPLE__
135template <typename A>
136bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
137#endif
138
139template <typename A>
140typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
141 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000142 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000143 for (entry *p = _buffer; p < _bufferUsed; ++p) {
144 if ((mh == p->mh) || (mh == 0)) {
145 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
146 result = p->fde;
147 break;
148 }
149 }
150 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000151 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000152 return result;
153}
154
155template <typename A>
156void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
157 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000158#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000159 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000160 if (_bufferUsed >= _bufferEnd) {
161 size_t oldSize = (size_t)(_bufferEnd - _buffer);
162 size_t newSize = oldSize * 4;
163 // Can't use operator new (we are below it).
164 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
165 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
166 if (_buffer != _initialBuffer)
167 free(_buffer);
168 _buffer = newBuffer;
169 _bufferUsed = &newBuffer[oldSize];
170 _bufferEnd = &newBuffer[newSize];
171 }
172 _bufferUsed->mh = mh;
173 _bufferUsed->ip_start = ip_start;
174 _bufferUsed->ip_end = ip_end;
175 _bufferUsed->fde = fde;
176 ++_bufferUsed;
177#ifdef __APPLE__
178 if (!_registeredForDyldUnloads) {
179 _dyld_register_func_for_remove_image(&dyldUnloadHook);
180 _registeredForDyldUnloads = true;
181 }
182#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000183 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000184#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000185}
186
187template <typename A>
188void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000189 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000190 entry *d = _buffer;
191 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
192 if (s->mh != mh) {
193 if (d != s)
194 *d = *s;
195 ++d;
196 }
197 }
198 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000199 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000200}
201
202#ifdef __APPLE__
203template <typename A>
204void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
205 removeAllIn((pint_t) mh);
206}
207#endif
208
209template <typename A>
210void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
211 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 +0000212 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000213 for (entry *p = _buffer; p < _bufferUsed; ++p) {
214 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
215 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000216 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000217}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000218#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000219
220
221#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
222
Ranjeet Singh421231a2017-03-31 15:28:06 +0000223#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000224template <typename A> class UnwindSectionHeader {
225public:
226 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
227 : _addressSpace(addressSpace), _addr(addr) {}
228
229 uint32_t version() const {
230 return _addressSpace.get32(_addr +
231 offsetof(unwind_info_section_header, version));
232 }
233 uint32_t commonEncodingsArraySectionOffset() const {
234 return _addressSpace.get32(_addr +
235 offsetof(unwind_info_section_header,
236 commonEncodingsArraySectionOffset));
237 }
238 uint32_t commonEncodingsArrayCount() const {
239 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
240 commonEncodingsArrayCount));
241 }
242 uint32_t personalityArraySectionOffset() const {
243 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
244 personalityArraySectionOffset));
245 }
246 uint32_t personalityArrayCount() const {
247 return _addressSpace.get32(
248 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
249 }
250 uint32_t indexSectionOffset() const {
251 return _addressSpace.get32(
252 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
253 }
254 uint32_t indexCount() const {
255 return _addressSpace.get32(
256 _addr + offsetof(unwind_info_section_header, indexCount));
257 }
258
259private:
260 A &_addressSpace;
261 typename A::pint_t _addr;
262};
263
264template <typename A> class UnwindSectionIndexArray {
265public:
266 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
267 : _addressSpace(addressSpace), _addr(addr) {}
268
269 uint32_t functionOffset(uint32_t index) const {
270 return _addressSpace.get32(
271 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
272 functionOffset));
273 }
274 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
275 return _addressSpace.get32(
276 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
277 secondLevelPagesSectionOffset));
278 }
279 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
280 return _addressSpace.get32(
281 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
282 lsdaIndexArraySectionOffset));
283 }
284
285private:
286 A &_addressSpace;
287 typename A::pint_t _addr;
288};
289
290template <typename A> class UnwindSectionRegularPageHeader {
291public:
292 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
293 : _addressSpace(addressSpace), _addr(addr) {}
294
295 uint32_t kind() const {
296 return _addressSpace.get32(
297 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
298 }
299 uint16_t entryPageOffset() const {
300 return _addressSpace.get16(
301 _addr + offsetof(unwind_info_regular_second_level_page_header,
302 entryPageOffset));
303 }
304 uint16_t entryCount() const {
305 return _addressSpace.get16(
306 _addr +
307 offsetof(unwind_info_regular_second_level_page_header, entryCount));
308 }
309
310private:
311 A &_addressSpace;
312 typename A::pint_t _addr;
313};
314
315template <typename A> class UnwindSectionRegularArray {
316public:
317 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
318 : _addressSpace(addressSpace), _addr(addr) {}
319
320 uint32_t functionOffset(uint32_t index) const {
321 return _addressSpace.get32(
322 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
323 functionOffset));
324 }
325 uint32_t encoding(uint32_t index) const {
326 return _addressSpace.get32(
327 _addr +
328 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
329 }
330
331private:
332 A &_addressSpace;
333 typename A::pint_t _addr;
334};
335
336template <typename A> class UnwindSectionCompressedPageHeader {
337public:
338 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
339 : _addressSpace(addressSpace), _addr(addr) {}
340
341 uint32_t kind() const {
342 return _addressSpace.get32(
343 _addr +
344 offsetof(unwind_info_compressed_second_level_page_header, kind));
345 }
346 uint16_t entryPageOffset() const {
347 return _addressSpace.get16(
348 _addr + offsetof(unwind_info_compressed_second_level_page_header,
349 entryPageOffset));
350 }
351 uint16_t entryCount() const {
352 return _addressSpace.get16(
353 _addr +
354 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
355 }
356 uint16_t encodingsPageOffset() const {
357 return _addressSpace.get16(
358 _addr + offsetof(unwind_info_compressed_second_level_page_header,
359 encodingsPageOffset));
360 }
361 uint16_t encodingsCount() const {
362 return _addressSpace.get16(
363 _addr + offsetof(unwind_info_compressed_second_level_page_header,
364 encodingsCount));
365 }
366
367private:
368 A &_addressSpace;
369 typename A::pint_t _addr;
370};
371
372template <typename A> class UnwindSectionCompressedArray {
373public:
374 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
375 : _addressSpace(addressSpace), _addr(addr) {}
376
377 uint32_t functionOffset(uint32_t index) const {
378 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
379 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
380 }
381 uint16_t encodingIndex(uint32_t index) const {
382 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
383 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
384 }
385
386private:
387 A &_addressSpace;
388 typename A::pint_t _addr;
389};
390
391template <typename A> class UnwindSectionLsdaArray {
392public:
393 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
394 : _addressSpace(addressSpace), _addr(addr) {}
395
396 uint32_t functionOffset(uint32_t index) const {
397 return _addressSpace.get32(
398 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
399 index, functionOffset));
400 }
401 uint32_t lsdaOffset(uint32_t index) const {
402 return _addressSpace.get32(
403 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
404 index, lsdaOffset));
405 }
406
407private:
408 A &_addressSpace;
409 typename A::pint_t _addr;
410};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000411#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000412
413class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
414public:
415 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
416 // This avoids an unnecessary dependency to libc++abi.
417 void operator delete(void *, size_t) {}
418
419 virtual ~AbstractUnwindCursor() {}
420 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
421 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
422 virtual void setReg(int, unw_word_t) {
423 _LIBUNWIND_ABORT("setReg not implemented");
424 }
425 virtual bool validFloatReg(int) {
426 _LIBUNWIND_ABORT("validFloatReg not implemented");
427 }
428 virtual unw_fpreg_t getFloatReg(int) {
429 _LIBUNWIND_ABORT("getFloatReg not implemented");
430 }
431 virtual void setFloatReg(int, unw_fpreg_t) {
432 _LIBUNWIND_ABORT("setFloatReg not implemented");
433 }
434 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
435 virtual void getInfo(unw_proc_info_t *) {
436 _LIBUNWIND_ABORT("getInfo not implemented");
437 }
438 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
439 virtual bool isSignalFrame() {
440 _LIBUNWIND_ABORT("isSignalFrame not implemented");
441 }
442 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
443 _LIBUNWIND_ABORT("getFunctionName not implemented");
444 }
445 virtual void setInfoBasedOnIPRegister(bool = false) {
446 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
447 }
448 virtual const char *getRegisterName(int) {
449 _LIBUNWIND_ABORT("getRegisterName not implemented");
450 }
451#ifdef __arm__
452 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
453#endif
454};
455
Charles Davisfa2e6202018-08-30 21:29:00 +0000456#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
457
458/// \c UnwindCursor contains all state (including all register values) during
459/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
460template <typename A, typename R>
461class UnwindCursor : public AbstractUnwindCursor {
462 typedef typename A::pint_t pint_t;
463public:
464 UnwindCursor(unw_context_t *context, A &as);
465 UnwindCursor(CONTEXT *context, A &as);
466 UnwindCursor(A &as, void *threadArg);
467 virtual ~UnwindCursor() {}
468 virtual bool validReg(int);
469 virtual unw_word_t getReg(int);
470 virtual void setReg(int, unw_word_t);
471 virtual bool validFloatReg(int);
472 virtual unw_fpreg_t getFloatReg(int);
473 virtual void setFloatReg(int, unw_fpreg_t);
474 virtual int step();
475 virtual void getInfo(unw_proc_info_t *);
476 virtual void jumpto();
477 virtual bool isSignalFrame();
478 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
479 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
480 virtual const char *getRegisterName(int num);
481#ifdef __arm__
482 virtual void saveVFPAsX();
483#endif
484
485 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
486 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
487
488private:
489
490 pint_t getLastPC() const { return _dispContext.ControlPc; }
491 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
492 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
493 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
494 &_dispContext.ImageBase,
495 _dispContext.HistoryTable);
496 *base = _dispContext.ImageBase;
497 return _dispContext.FunctionEntry;
498 }
499 bool getInfoFromSEH(pint_t pc);
500 int stepWithSEHData() {
501 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
502 _dispContext.ImageBase,
503 _dispContext.ControlPc,
504 _dispContext.FunctionEntry,
505 _dispContext.ContextRecord,
506 &_dispContext.HandlerData,
507 &_dispContext.EstablisherFrame,
508 NULL);
509 // Update some fields of the unwind info now, since we have them.
510 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
511 if (_dispContext.LanguageHandler) {
512 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
513 } else
514 _info.handler = 0;
515 return UNW_STEP_SUCCESS;
516 }
517
518 A &_addressSpace;
519 unw_proc_info_t _info;
520 DISPATCHER_CONTEXT _dispContext;
521 CONTEXT _msContext;
522 UNWIND_HISTORY_TABLE _histTable;
523 bool _unwindInfoMissing;
524};
525
526
527template <typename A, typename R>
528UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
529 : _addressSpace(as), _unwindInfoMissing(false) {
530 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
531 "UnwindCursor<> does not fit in unw_cursor_t");
532 memset(&_info, 0, sizeof(_info));
533 memset(&_histTable, 0, sizeof(_histTable));
534 _dispContext.ContextRecord = &_msContext;
535 _dispContext.HistoryTable = &_histTable;
536 // Initialize MS context from ours.
537 R r(context);
538 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
539#if defined(_LIBUNWIND_TARGET_X86_64)
540 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
541 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
542 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
543 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
544 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
545 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
546 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
547 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
548 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
549 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
550 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
551 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
552 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
553 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
554 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
555 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
556 _msContext.Rip = r.getRegister(UNW_REG_IP);
557 union {
558 v128 v;
559 M128A m;
560 } t;
561 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
562 _msContext.Xmm0 = t.m;
563 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
564 _msContext.Xmm1 = t.m;
565 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
566 _msContext.Xmm2 = t.m;
567 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
568 _msContext.Xmm3 = t.m;
569 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
570 _msContext.Xmm4 = t.m;
571 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
572 _msContext.Xmm5 = t.m;
573 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
574 _msContext.Xmm6 = t.m;
575 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
576 _msContext.Xmm7 = t.m;
577 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
578 _msContext.Xmm8 = t.m;
579 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
580 _msContext.Xmm9 = t.m;
581 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
582 _msContext.Xmm10 = t.m;
583 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
584 _msContext.Xmm11 = t.m;
585 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
586 _msContext.Xmm12 = t.m;
587 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
588 _msContext.Xmm13 = t.m;
589 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
590 _msContext.Xmm14 = t.m;
591 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
592 _msContext.Xmm15 = t.m;
593#elif defined(_LIBUNWIND_TARGET_ARM)
594 _msContext.R0 = r.getRegister(UNW_ARM_R0);
595 _msContext.R1 = r.getRegister(UNW_ARM_R1);
596 _msContext.R2 = r.getRegister(UNW_ARM_R2);
597 _msContext.R3 = r.getRegister(UNW_ARM_R3);
598 _msContext.R4 = r.getRegister(UNW_ARM_R4);
599 _msContext.R5 = r.getRegister(UNW_ARM_R5);
600 _msContext.R6 = r.getRegister(UNW_ARM_R6);
601 _msContext.R7 = r.getRegister(UNW_ARM_R7);
602 _msContext.R8 = r.getRegister(UNW_ARM_R8);
603 _msContext.R9 = r.getRegister(UNW_ARM_R9);
604 _msContext.R10 = r.getRegister(UNW_ARM_R10);
605 _msContext.R11 = r.getRegister(UNW_ARM_R11);
606 _msContext.R12 = r.getRegister(UNW_ARM_R12);
607 _msContext.Sp = r.getRegister(UNW_ARM_SP);
608 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000609 _msContext.Pc = r.getRegister(UNW_ARM_IP);
610 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000611 union {
612 uint64_t w;
613 double d;
614 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000615 d.d = r.getFloatRegister(i);
616 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000617 }
618#endif
619}
620
621template <typename A, typename R>
622UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
623 : _addressSpace(as), _unwindInfoMissing(false) {
624 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
625 "UnwindCursor<> does not fit in unw_cursor_t");
626 memset(&_info, 0, sizeof(_info));
627 memset(&_histTable, 0, sizeof(_histTable));
628 _dispContext.ContextRecord = &_msContext;
629 _dispContext.HistoryTable = &_histTable;
630 _msContext = *context;
631}
632
633
634template <typename A, typename R>
635bool UnwindCursor<A, R>::validReg(int regNum) {
636 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
637#if defined(_LIBUNWIND_TARGET_X86_64)
638 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
639#elif defined(_LIBUNWIND_TARGET_ARM)
640 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
641#endif
642 return false;
643}
644
645template <typename A, typename R>
646unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
647 switch (regNum) {
648#if defined(_LIBUNWIND_TARGET_X86_64)
649 case UNW_REG_IP: return _msContext.Rip;
650 case UNW_X86_64_RAX: return _msContext.Rax;
651 case UNW_X86_64_RDX: return _msContext.Rdx;
652 case UNW_X86_64_RCX: return _msContext.Rcx;
653 case UNW_X86_64_RBX: return _msContext.Rbx;
654 case UNW_REG_SP:
655 case UNW_X86_64_RSP: return _msContext.Rsp;
656 case UNW_X86_64_RBP: return _msContext.Rbp;
657 case UNW_X86_64_RSI: return _msContext.Rsi;
658 case UNW_X86_64_RDI: return _msContext.Rdi;
659 case UNW_X86_64_R8: return _msContext.R8;
660 case UNW_X86_64_R9: return _msContext.R9;
661 case UNW_X86_64_R10: return _msContext.R10;
662 case UNW_X86_64_R11: return _msContext.R11;
663 case UNW_X86_64_R12: return _msContext.R12;
664 case UNW_X86_64_R13: return _msContext.R13;
665 case UNW_X86_64_R14: return _msContext.R14;
666 case UNW_X86_64_R15: return _msContext.R15;
667#elif defined(_LIBUNWIND_TARGET_ARM)
668 case UNW_ARM_R0: return _msContext.R0;
669 case UNW_ARM_R1: return _msContext.R1;
670 case UNW_ARM_R2: return _msContext.R2;
671 case UNW_ARM_R3: return _msContext.R3;
672 case UNW_ARM_R4: return _msContext.R4;
673 case UNW_ARM_R5: return _msContext.R5;
674 case UNW_ARM_R6: return _msContext.R6;
675 case UNW_ARM_R7: return _msContext.R7;
676 case UNW_ARM_R8: return _msContext.R8;
677 case UNW_ARM_R9: return _msContext.R9;
678 case UNW_ARM_R10: return _msContext.R10;
679 case UNW_ARM_R11: return _msContext.R11;
680 case UNW_ARM_R12: return _msContext.R12;
681 case UNW_REG_SP:
682 case UNW_ARM_SP: return _msContext.Sp;
683 case UNW_ARM_LR: return _msContext.Lr;
684 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000685 case UNW_ARM_IP: return _msContext.Pc;
Charles Davisfa2e6202018-08-30 21:29:00 +0000686#endif
687 }
688 _LIBUNWIND_ABORT("unsupported register");
689}
690
691template <typename A, typename R>
692void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
693 switch (regNum) {
694#if defined(_LIBUNWIND_TARGET_X86_64)
695 case UNW_REG_IP: _msContext.Rip = value; break;
696 case UNW_X86_64_RAX: _msContext.Rax = value; break;
697 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
698 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
699 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
700 case UNW_REG_SP:
701 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
702 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
703 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
704 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
705 case UNW_X86_64_R8: _msContext.R8 = value; break;
706 case UNW_X86_64_R9: _msContext.R9 = value; break;
707 case UNW_X86_64_R10: _msContext.R10 = value; break;
708 case UNW_X86_64_R11: _msContext.R11 = value; break;
709 case UNW_X86_64_R12: _msContext.R12 = value; break;
710 case UNW_X86_64_R13: _msContext.R13 = value; break;
711 case UNW_X86_64_R14: _msContext.R14 = value; break;
712 case UNW_X86_64_R15: _msContext.R15 = value; break;
713#elif defined(_LIBUNWIND_TARGET_ARM)
714 case UNW_ARM_R0: _msContext.R0 = value; break;
715 case UNW_ARM_R1: _msContext.R1 = value; break;
716 case UNW_ARM_R2: _msContext.R2 = value; break;
717 case UNW_ARM_R3: _msContext.R3 = value; break;
718 case UNW_ARM_R4: _msContext.R4 = value; break;
719 case UNW_ARM_R5: _msContext.R5 = value; break;
720 case UNW_ARM_R6: _msContext.R6 = value; break;
721 case UNW_ARM_R7: _msContext.R7 = value; break;
722 case UNW_ARM_R8: _msContext.R8 = value; break;
723 case UNW_ARM_R9: _msContext.R9 = value; break;
724 case UNW_ARM_R10: _msContext.R10 = value; break;
725 case UNW_ARM_R11: _msContext.R11 = value; break;
726 case UNW_ARM_R12: _msContext.R12 = value; break;
727 case UNW_REG_SP:
728 case UNW_ARM_SP: _msContext.Sp = value; break;
729 case UNW_ARM_LR: _msContext.Lr = value; break;
730 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000731 case UNW_ARM_IP: _msContext.Pc = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000732#endif
733 default:
734 _LIBUNWIND_ABORT("unsupported register");
735 }
736}
737
738template <typename A, typename R>
739bool UnwindCursor<A, R>::validFloatReg(int regNum) {
740#if defined(_LIBUNWIND_TARGET_ARM)
741 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
742 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
743#endif
744 return false;
745}
746
747template <typename A, typename R>
748unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
749#if defined(_LIBUNWIND_TARGET_ARM)
750 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
751 union {
752 uint32_t w;
753 float f;
754 } d;
755 d.w = _msContext.S[regNum - UNW_ARM_S0];
756 return d.f;
757 }
758 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
759 union {
760 uint64_t w;
761 double d;
762 } d;
763 d.w = _msContext.D[regNum - UNW_ARM_D0];
764 return d.d;
765 }
766 _LIBUNWIND_ABORT("unsupported float register");
767#else
768 _LIBUNWIND_ABORT("float registers unimplemented");
769#endif
770}
771
772template <typename A, typename R>
773void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
774#if defined(_LIBUNWIND_TARGET_ARM)
775 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
776 union {
777 uint32_t w;
778 float f;
779 } d;
780 d.f = value;
781 _msContext.S[regNum - UNW_ARM_S0] = d.w;
782 }
783 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
784 union {
785 uint64_t w;
786 double d;
787 } d;
788 d.d = value;
789 _msContext.D[regNum - UNW_ARM_D0] = d.w;
790 }
791 _LIBUNWIND_ABORT("unsupported float register");
792#else
793 _LIBUNWIND_ABORT("float registers unimplemented");
794#endif
795}
796
797template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
798 RtlRestoreContext(&_msContext, nullptr);
799}
800
801#ifdef __arm__
802template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
803#endif
804
805template <typename A, typename R>
806const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000807 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000808}
809
810template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
811 return false;
812}
813
814#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
815
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000816/// UnwindCursor contains all state (including all register values) during
817/// an unwind. This is normally stack allocated inside a unw_cursor_t.
818template <typename A, typename R>
819class UnwindCursor : public AbstractUnwindCursor{
820 typedef typename A::pint_t pint_t;
821public:
822 UnwindCursor(unw_context_t *context, A &as);
823 UnwindCursor(A &as, void *threadArg);
824 virtual ~UnwindCursor() {}
825 virtual bool validReg(int);
826 virtual unw_word_t getReg(int);
827 virtual void setReg(int, unw_word_t);
828 virtual bool validFloatReg(int);
829 virtual unw_fpreg_t getFloatReg(int);
830 virtual void setFloatReg(int, unw_fpreg_t);
831 virtual int step();
832 virtual void getInfo(unw_proc_info_t *);
833 virtual void jumpto();
834 virtual bool isSignalFrame();
835 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
836 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
837 virtual const char *getRegisterName(int num);
838#ifdef __arm__
839 virtual void saveVFPAsX();
840#endif
841
842private:
843
Ranjeet Singh421231a2017-03-31 15:28:06 +0000844#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000845 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000846
847 int stepWithEHABI() {
848 size_t len = 0;
849 size_t off = 0;
850 // FIXME: Calling decode_eht_entry() here is violating the libunwind
851 // abstraction layer.
852 const uint32_t *ehtp =
853 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
854 &off, &len);
855 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
856 _URC_CONTINUE_UNWIND)
857 return UNW_STEP_END;
858 return UNW_STEP_SUCCESS;
859 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000860#endif
861
Ranjeet Singh421231a2017-03-31 15:28:06 +0000862#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000863 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
864 uint32_t fdeSectionOffsetHint=0);
865 int stepWithDwarfFDE() {
866 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
867 (pint_t)this->getReg(UNW_REG_IP),
868 (pint_t)_info.unwind_info,
869 _registers);
870 }
871#endif
872
Ranjeet Singh421231a2017-03-31 15:28:06 +0000873#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000874 bool getInfoFromCompactEncodingSection(pint_t pc,
875 const UnwindInfoSections &sects);
876 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000877 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000878 if ( compactSaysUseDwarf() )
879 return stepWithDwarfFDE();
880 #endif
881 R dummy;
882 return stepWithCompactEncoding(dummy);
883 }
884
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000885#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000886 int stepWithCompactEncoding(Registers_x86_64 &) {
887 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
888 _info.format, _info.start_ip, _addressSpace, _registers);
889 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000890#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000891
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000892#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000893 int stepWithCompactEncoding(Registers_x86 &) {
894 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
895 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
896 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000897#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000898
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000899#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000900 int stepWithCompactEncoding(Registers_ppc &) {
901 return UNW_EINVAL;
902 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000903#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000904
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000905#if defined(_LIBUNWIND_TARGET_PPC64)
906 int stepWithCompactEncoding(Registers_ppc64 &) {
907 return UNW_EINVAL;
908 }
909#endif
910
911
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000912#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000913 int stepWithCompactEncoding(Registers_arm64 &) {
914 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
915 _info.format, _info.start_ip, _addressSpace, _registers);
916 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000917#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000918
John Baldwin56441d42017-12-12 21:43:36 +0000919#if defined(_LIBUNWIND_TARGET_MIPS_O32)
920 int stepWithCompactEncoding(Registers_mips_o32 &) {
921 return UNW_EINVAL;
922 }
923#endif
924
John Baldwin541a4352018-01-09 17:07:18 +0000925#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
926 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000927 return UNW_EINVAL;
928 }
929#endif
930
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000931 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
932 R dummy;
933 return compactSaysUseDwarf(dummy, offset);
934 }
935
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000936#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000937 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
938 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
939 if (offset)
940 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
941 return true;
942 }
943 return false;
944 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000945#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000946
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000947#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000948 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
949 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
950 if (offset)
951 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
952 return true;
953 }
954 return false;
955 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000956#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000957
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000958#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000959 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
960 return true;
961 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000962#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000963
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000964#if defined(_LIBUNWIND_TARGET_PPC64)
965 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
966 return true;
967 }
968#endif
969
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000970#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000971 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
972 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
973 if (offset)
974 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
975 return true;
976 }
977 return false;
978 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000979#endif
John Baldwin56441d42017-12-12 21:43:36 +0000980
981#if defined(_LIBUNWIND_TARGET_MIPS_O32)
982 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
983 return true;
984 }
985#endif
986
John Baldwin541a4352018-01-09 17:07:18 +0000987#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
988 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +0000989 return true;
990 }
991#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +0000992#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000993
Ranjeet Singh421231a2017-03-31 15:28:06 +0000994#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000995 compact_unwind_encoding_t dwarfEncoding() const {
996 R dummy;
997 return dwarfEncoding(dummy);
998 }
999
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001000#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001001 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1002 return UNWIND_X86_64_MODE_DWARF;
1003 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001004#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001005
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001006#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001007 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1008 return UNWIND_X86_MODE_DWARF;
1009 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001010#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001011
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001012#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001013 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1014 return 0;
1015 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001016#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001017
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001018#if defined(_LIBUNWIND_TARGET_PPC64)
1019 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1020 return 0;
1021 }
1022#endif
1023
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001024#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001025 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1026 return UNWIND_ARM64_MODE_DWARF;
1027 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001028#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001029
Martin Storsjoa72285f2017-11-02 08:16:16 +00001030#if defined(_LIBUNWIND_TARGET_ARM)
1031 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1032 return 0;
1033 }
1034#endif
1035
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001036#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001037 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1038 return 0;
1039 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001040#endif
John Baldwin56441d42017-12-12 21:43:36 +00001041
1042#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1043 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1044 return 0;
1045 }
1046#endif
1047
John Baldwin541a4352018-01-09 17:07:18 +00001048#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1049 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001050 return 0;
1051 }
1052#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +00001053#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001054
Charles Davisfa2e6202018-08-30 21:29:00 +00001055#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1056 // For runtime environments using SEH unwind data without Windows runtime
1057 // support.
1058 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1059 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1060 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1061 /* FIXME: Implement */
1062 *base = 0;
1063 return nullptr;
1064 }
1065 bool getInfoFromSEH(pint_t pc);
1066 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1067#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1068
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001069
1070 A &_addressSpace;
1071 R _registers;
1072 unw_proc_info_t _info;
1073 bool _unwindInfoMissing;
1074 bool _isSignalFrame;
1075};
1076
1077
1078template <typename A, typename R>
1079UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1080 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1081 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001082 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001083 "UnwindCursor<> does not fit in unw_cursor_t");
1084 memset(&_info, 0, sizeof(_info));
1085}
1086
1087template <typename A, typename R>
1088UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1089 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1090 memset(&_info, 0, sizeof(_info));
1091 // FIXME
1092 // fill in _registers from thread arg
1093}
1094
1095
1096template <typename A, typename R>
1097bool UnwindCursor<A, R>::validReg(int regNum) {
1098 return _registers.validRegister(regNum);
1099}
1100
1101template <typename A, typename R>
1102unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1103 return _registers.getRegister(regNum);
1104}
1105
1106template <typename A, typename R>
1107void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1108 _registers.setRegister(regNum, (typename A::pint_t)value);
1109}
1110
1111template <typename A, typename R>
1112bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1113 return _registers.validFloatRegister(regNum);
1114}
1115
1116template <typename A, typename R>
1117unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1118 return _registers.getFloatRegister(regNum);
1119}
1120
1121template <typename A, typename R>
1122void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1123 _registers.setFloatRegister(regNum, value);
1124}
1125
1126template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1127 _registers.jumpto();
1128}
1129
1130#ifdef __arm__
1131template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1132 _registers.saveVFPAsX();
1133}
1134#endif
1135
1136template <typename A, typename R>
1137const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1138 return _registers.getRegisterName(regNum);
1139}
1140
1141template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1142 return _isSignalFrame;
1143}
1144
Charles Davisfa2e6202018-08-30 21:29:00 +00001145#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1146
Ranjeet Singh421231a2017-03-31 15:28:06 +00001147#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001148struct EHABIIndexEntry {
1149 uint32_t functionOffset;
1150 uint32_t data;
1151};
1152
1153template<typename A>
1154struct EHABISectionIterator {
1155 typedef EHABISectionIterator _Self;
1156
1157 typedef std::random_access_iterator_tag iterator_category;
1158 typedef typename A::pint_t value_type;
1159 typedef typename A::pint_t* pointer;
1160 typedef typename A::pint_t& reference;
1161 typedef size_t size_type;
1162 typedef size_t difference_type;
1163
1164 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1165 return _Self(addressSpace, sects, 0);
1166 }
1167 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001168 return _Self(addressSpace, sects,
1169 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001170 }
1171
1172 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1173 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1174
1175 _Self& operator++() { ++_i; return *this; }
1176 _Self& operator+=(size_t a) { _i += a; return *this; }
1177 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1178 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1179
1180 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1181 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1182
1183 size_t operator-(const _Self& other) { return _i - other._i; }
1184
1185 bool operator==(const _Self& other) const {
1186 assert(_addressSpace == other._addressSpace);
1187 assert(_sects == other._sects);
1188 return _i == other._i;
1189 }
1190
1191 typename A::pint_t operator*() const { return functionAddress(); }
1192
1193 typename A::pint_t functionAddress() const {
1194 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1195 EHABIIndexEntry, _i, functionOffset);
1196 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1197 }
1198
1199 typename A::pint_t dataAddress() {
1200 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1201 EHABIIndexEntry, _i, data);
1202 return indexAddr;
1203 }
1204
1205 private:
1206 size_t _i;
1207 A* _addressSpace;
1208 const UnwindInfoSections* _sects;
1209};
1210
1211template <typename A, typename R>
1212bool UnwindCursor<A, R>::getInfoFromEHABISection(
1213 pint_t pc,
1214 const UnwindInfoSections &sects) {
1215 EHABISectionIterator<A> begin =
1216 EHABISectionIterator<A>::begin(_addressSpace, sects);
1217 EHABISectionIterator<A> end =
1218 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001219 if (begin == end)
1220 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001221
1222 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001223 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001224 return false;
1225 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1226
1227 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001228 // If an exception is thrown from a function, corresponding to the last entry
1229 // in the table, we don't really know the function extent and have to choose a
1230 // value for nextPC. Choosing max() will allow the range check during trace to
1231 // succeed.
1232 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
1233 : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001234 pint_t indexDataAddr = itThisPC.dataAddress();
1235
1236 if (indexDataAddr == 0)
1237 return false;
1238
1239 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1240 if (indexData == UNW_EXIDX_CANTUNWIND)
1241 return false;
1242
1243 // If the high bit is set, the exception handling table entry is inline inside
1244 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1245 // the table points at an offset in the exception handling table (section 5 EHABI).
1246 pint_t exceptionTableAddr;
1247 uint32_t exceptionTableData;
1248 bool isSingleWordEHT;
1249 if (indexData & 0x80000000) {
1250 exceptionTableAddr = indexDataAddr;
1251 // TODO(ajwong): Should this data be 0?
1252 exceptionTableData = indexData;
1253 isSingleWordEHT = true;
1254 } else {
1255 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1256 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1257 isSingleWordEHT = false;
1258 }
1259
1260 // Now we know the 3 things:
1261 // exceptionTableAddr -- exception handler table entry.
1262 // exceptionTableData -- the data inside the first word of the eht entry.
1263 // isSingleWordEHT -- whether the entry is in the index.
1264 unw_word_t personalityRoutine = 0xbadf00d;
1265 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001266 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001267
1268 // If the high bit in the exception handling table entry is set, the entry is
1269 // in compact form (section 6.3 EHABI).
1270 if (exceptionTableData & 0x80000000) {
1271 // Grab the index of the personality routine from the compact form.
1272 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1273 uint32_t extraWords = 0;
1274 switch (choice) {
1275 case 0:
1276 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1277 extraWords = 0;
1278 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001279 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001280 break;
1281 case 1:
1282 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1283 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1284 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001285 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001286 break;
1287 case 2:
1288 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1289 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1290 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001291 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001292 break;
1293 default:
1294 _LIBUNWIND_ABORT("unknown personality routine");
1295 return false;
1296 }
1297
1298 if (isSingleWordEHT) {
1299 if (extraWords != 0) {
1300 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1301 "requires extra words");
1302 return false;
1303 }
1304 }
1305 } else {
1306 pint_t personalityAddr =
1307 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1308 personalityRoutine = personalityAddr;
1309
1310 // ARM EHABI # 6.2, # 9.2
1311 //
1312 // +---- ehtp
1313 // v
1314 // +--------------------------------------+
1315 // | +--------+--------+--------+-------+ |
1316 // | |0| prel31 to personalityRoutine | |
1317 // | +--------+--------+--------+-------+ |
1318 // | | N | unwind opcodes | | <-- UnwindData
1319 // | +--------+--------+--------+-------+ |
1320 // | | Word 2 unwind opcodes | |
1321 // | +--------+--------+--------+-------+ |
1322 // | ... |
1323 // | +--------+--------+--------+-------+ |
1324 // | | Word N unwind opcodes | |
1325 // | +--------+--------+--------+-------+ |
1326 // | | LSDA | | <-- lsda
1327 // | | ... | |
1328 // | +--------+--------+--------+-------+ |
1329 // +--------------------------------------+
1330
1331 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1332 uint32_t FirstDataWord = *UnwindData;
1333 size_t N = ((FirstDataWord >> 24) & 0xff);
1334 size_t NDataWords = N + 1;
1335 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1336 }
1337
1338 _info.start_ip = thisPC;
1339 _info.end_ip = nextPC;
1340 _info.handler = personalityRoutine;
1341 _info.unwind_info = exceptionTableAddr;
1342 _info.lsda = lsda;
1343 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1344 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1345
1346 return true;
1347}
1348#endif
1349
Ranjeet Singh421231a2017-03-31 15:28:06 +00001350#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001351template <typename A, typename R>
1352bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1353 const UnwindInfoSections &sects,
1354 uint32_t fdeSectionOffsetHint) {
1355 typename CFI_Parser<A>::FDE_Info fdeInfo;
1356 typename CFI_Parser<A>::CIE_Info cieInfo;
1357 bool foundFDE = false;
1358 bool foundInCache = false;
1359 // If compact encoding table gave offset into dwarf section, go directly there
1360 if (fdeSectionOffsetHint != 0) {
1361 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1362 (uint32_t)sects.dwarf_section_length,
1363 sects.dwarf_section + fdeSectionOffsetHint,
1364 &fdeInfo, &cieInfo);
1365 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001366#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001367 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1368 foundFDE = EHHeaderParser<A>::findFDE(
1369 _addressSpace, pc, sects.dwarf_index_section,
1370 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1371 }
1372#endif
1373 if (!foundFDE) {
1374 // otherwise, search cache of previously found FDEs.
1375 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1376 if (cachedFDE != 0) {
1377 foundFDE =
1378 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1379 (uint32_t)sects.dwarf_section_length,
1380 cachedFDE, &fdeInfo, &cieInfo);
1381 foundInCache = foundFDE;
1382 }
1383 }
1384 if (!foundFDE) {
1385 // Still not found, do full scan of __eh_frame section.
1386 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1387 (uint32_t)sects.dwarf_section_length, 0,
1388 &fdeInfo, &cieInfo);
1389 }
1390 if (foundFDE) {
1391 typename CFI_Parser<A>::PrologInfo prolog;
1392 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1393 &prolog)) {
1394 // Save off parsed FDE info
1395 _info.start_ip = fdeInfo.pcStart;
1396 _info.end_ip = fdeInfo.pcEnd;
1397 _info.lsda = fdeInfo.lsda;
1398 _info.handler = cieInfo.personality;
1399 _info.gp = prolog.spExtraArgSize;
1400 _info.flags = 0;
1401 _info.format = dwarfEncoding();
1402 _info.unwind_info = fdeInfo.fdeStart;
1403 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1404 _info.extra = (unw_word_t) sects.dso_base;
1405
1406 // Add to cache (to make next lookup faster) if we had no hint
1407 // and there was no index.
1408 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001409 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001410 if (sects.dwarf_index_section == 0)
1411 #endif
1412 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1413 fdeInfo.fdeStart);
1414 }
1415 return true;
1416 }
1417 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001418 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001419 return false;
1420}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001421#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001422
1423
Ranjeet Singh421231a2017-03-31 15:28:06 +00001424#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001425template <typename A, typename R>
1426bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1427 const UnwindInfoSections &sects) {
1428 const bool log = false;
1429 if (log)
1430 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1431 (uint64_t)pc, (uint64_t)sects.dso_base);
1432
1433 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1434 sects.compact_unwind_section);
1435 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1436 return false;
1437
1438 // do a binary search of top level index to find page with unwind info
1439 pint_t targetFunctionOffset = pc - sects.dso_base;
1440 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1441 sects.compact_unwind_section
1442 + sectionHeader.indexSectionOffset());
1443 uint32_t low = 0;
1444 uint32_t high = sectionHeader.indexCount();
1445 uint32_t last = high - 1;
1446 while (low < high) {
1447 uint32_t mid = (low + high) / 2;
1448 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1449 //mid, low, high, topIndex.functionOffset(mid));
1450 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1451 if ((mid == last) ||
1452 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1453 low = mid;
1454 break;
1455 } else {
1456 low = mid + 1;
1457 }
1458 } else {
1459 high = mid;
1460 }
1461 }
1462 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1463 const uint32_t firstLevelNextPageFunctionOffset =
1464 topIndex.functionOffset(low + 1);
1465 const pint_t secondLevelAddr =
1466 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1467 const pint_t lsdaArrayStartAddr =
1468 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1469 const pint_t lsdaArrayEndAddr =
1470 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1471 if (log)
1472 fprintf(stderr, "\tfirst level search for result index=%d "
1473 "to secondLevelAddr=0x%llX\n",
1474 low, (uint64_t) secondLevelAddr);
1475 // do a binary search of second level page index
1476 uint32_t encoding = 0;
1477 pint_t funcStart = 0;
1478 pint_t funcEnd = 0;
1479 pint_t lsda = 0;
1480 pint_t personality = 0;
1481 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1482 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1483 // regular page
1484 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1485 secondLevelAddr);
1486 UnwindSectionRegularArray<A> pageIndex(
1487 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1488 // binary search looks for entry with e where index[e].offset <= pc <
1489 // index[e+1].offset
1490 if (log)
1491 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1492 "regular page starting at secondLevelAddr=0x%llX\n",
1493 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1494 low = 0;
1495 high = pageHeader.entryCount();
1496 while (low < high) {
1497 uint32_t mid = (low + high) / 2;
1498 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1499 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1500 // at end of table
1501 low = mid;
1502 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1503 break;
1504 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1505 // next is too big, so we found it
1506 low = mid;
1507 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1508 break;
1509 } else {
1510 low = mid + 1;
1511 }
1512 } else {
1513 high = mid;
1514 }
1515 }
1516 encoding = pageIndex.encoding(low);
1517 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1518 if (pc < funcStart) {
1519 if (log)
1520 fprintf(
1521 stderr,
1522 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1523 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1524 return false;
1525 }
1526 if (pc > funcEnd) {
1527 if (log)
1528 fprintf(
1529 stderr,
1530 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1531 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1532 return false;
1533 }
1534 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1535 // compressed page
1536 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1537 secondLevelAddr);
1538 UnwindSectionCompressedArray<A> pageIndex(
1539 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1540 const uint32_t targetFunctionPageOffset =
1541 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1542 // binary search looks for entry with e where index[e].offset <= pc <
1543 // index[e+1].offset
1544 if (log)
1545 fprintf(stderr, "\tbinary search of compressed page starting at "
1546 "secondLevelAddr=0x%llX\n",
1547 (uint64_t) secondLevelAddr);
1548 low = 0;
1549 last = pageHeader.entryCount() - 1;
1550 high = pageHeader.entryCount();
1551 while (low < high) {
1552 uint32_t mid = (low + high) / 2;
1553 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1554 if ((mid == last) ||
1555 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1556 low = mid;
1557 break;
1558 } else {
1559 low = mid + 1;
1560 }
1561 } else {
1562 high = mid;
1563 }
1564 }
1565 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1566 + sects.dso_base;
1567 if (low < last)
1568 funcEnd =
1569 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1570 + sects.dso_base;
1571 else
1572 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1573 if (pc < funcStart) {
1574 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001575 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001576 (uint64_t) pc, (uint64_t) funcStart);
1577 return false;
1578 }
1579 if (pc > funcEnd) {
1580 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001581 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001582 (uint64_t) pc, (uint64_t) funcEnd);
1583 return false;
1584 }
1585 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1586 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1587 // encoding is in common table in section header
1588 encoding = _addressSpace.get32(
1589 sects.compact_unwind_section +
1590 sectionHeader.commonEncodingsArraySectionOffset() +
1591 encodingIndex * sizeof(uint32_t));
1592 } else {
1593 // encoding is in page specific table
1594 uint16_t pageEncodingIndex =
1595 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1596 encoding = _addressSpace.get32(secondLevelAddr +
1597 pageHeader.encodingsPageOffset() +
1598 pageEncodingIndex * sizeof(uint32_t));
1599 }
1600 } else {
1601 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001602 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001603 (uint64_t) sects.compact_unwind_section);
1604 return false;
1605 }
1606
1607 // look up LSDA, if encoding says function has one
1608 if (encoding & UNWIND_HAS_LSDA) {
1609 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1610 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1611 low = 0;
1612 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1613 sizeof(unwind_info_section_header_lsda_index_entry);
1614 // binary search looks for entry with exact match for functionOffset
1615 if (log)
1616 fprintf(stderr,
1617 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1618 funcStartOffset);
1619 while (low < high) {
1620 uint32_t mid = (low + high) / 2;
1621 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1622 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1623 break;
1624 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1625 low = mid + 1;
1626 } else {
1627 high = mid;
1628 }
1629 }
1630 if (lsda == 0) {
1631 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001632 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001633 encoding, (uint64_t) pc);
1634 return false;
1635 }
1636 }
1637
1638 // extact personality routine, if encoding says function has one
1639 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1640 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1641 if (personalityIndex != 0) {
1642 --personalityIndex; // change 1-based to zero-based index
1643 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1644 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001645 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001646 encoding, personalityIndex,
1647 sectionHeader.personalityArrayCount());
1648 return false;
1649 }
1650 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1651 sects.compact_unwind_section +
1652 sectionHeader.personalityArraySectionOffset() +
1653 personalityIndex * sizeof(uint32_t));
1654 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1655 personality = _addressSpace.getP(personalityPointer);
1656 if (log)
1657 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1658 "personalityDelta=0x%08X, personality=0x%08llX\n",
1659 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1660 }
1661
1662 if (log)
1663 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1664 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1665 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1666 _info.start_ip = funcStart;
1667 _info.end_ip = funcEnd;
1668 _info.lsda = lsda;
1669 _info.handler = personality;
1670 _info.gp = 0;
1671 _info.flags = 0;
1672 _info.format = encoding;
1673 _info.unwind_info = 0;
1674 _info.unwind_info_size = 0;
1675 _info.extra = sects.dso_base;
1676 return true;
1677}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001678#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001679
1680
Charles Davisfa2e6202018-08-30 21:29:00 +00001681#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1682template <typename A, typename R>
1683bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1684 pint_t base;
1685 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1686 if (!unwindEntry) {
1687 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1688 return false;
1689 }
1690 _info.gp = 0;
1691 _info.flags = 0;
1692 _info.format = 0;
1693 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1694 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1695 _info.extra = base;
1696 _info.start_ip = base + unwindEntry->BeginAddress;
1697#ifdef _LIBUNWIND_TARGET_X86_64
1698 _info.end_ip = base + unwindEntry->EndAddress;
1699 // Only fill in the handler and LSDA if they're stale.
1700 if (pc != getLastPC()) {
1701 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1702 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1703 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1704 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1705 // these structures.)
1706 // N.B. UNWIND_INFO structs are DWORD-aligned.
1707 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1708 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1709 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1710 if (*handler) {
1711 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1712 } else
1713 _info.handler = 0;
1714 } else {
1715 _info.lsda = 0;
1716 _info.handler = 0;
1717 }
1718 }
1719#elif defined(_LIBUNWIND_TARGET_ARM)
1720 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1721 _info.lsda = 0; // FIXME
1722 _info.handler = 0; // FIXME
1723#endif
1724 setLastPC(pc);
1725 return true;
1726}
1727#endif
1728
1729
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001730template <typename A, typename R>
1731void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1732 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001733#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001734 // Remove the thumb bit so the IP represents the actual instruction address.
1735 // This matches the behaviour of _Unwind_GetIP on arm.
1736 pc &= (pint_t)~0x1;
1737#endif
1738
1739 // If the last line of a function is a "throw" the compiler sometimes
1740 // emits no instructions after the call to __cxa_throw. This means
1741 // the return address is actually the start of the next function.
1742 // To disambiguate this, back up the pc when we know it is a return
1743 // address.
1744 if (isReturnAddress)
1745 --pc;
1746
1747 // Ask address space object to find unwind sections for this pc.
1748 UnwindInfoSections sects;
1749 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001750#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001751 // If there is a compact unwind encoding table, look there first.
1752 if (sects.compact_unwind_section != 0) {
1753 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001754 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001755 // Found info in table, done unless encoding says to use dwarf.
1756 uint32_t dwarfOffset;
1757 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1758 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1759 // found info in dwarf, done
1760 return;
1761 }
1762 }
1763 #endif
1764 // If unwind table has entry, but entry says there is no unwind info,
1765 // record that we have no unwind info.
1766 if (_info.format == 0)
1767 _unwindInfoMissing = true;
1768 return;
1769 }
1770 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001771#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001772
Charles Davisfa2e6202018-08-30 21:29:00 +00001773#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1774 // If there is SEH unwind info, look there next.
1775 if (this->getInfoFromSEH(pc))
1776 return;
1777#endif
1778
Ranjeet Singh421231a2017-03-31 15:28:06 +00001779#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001780 // If there is dwarf unwind info, look there next.
1781 if (sects.dwarf_section != 0) {
1782 if (this->getInfoFromDwarfSection(pc, sects)) {
1783 // found info in dwarf, done
1784 return;
1785 }
1786 }
1787#endif
1788
Ranjeet Singh421231a2017-03-31 15:28:06 +00001789#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001790 // If there is ARM EHABI unwind info, look there next.
1791 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1792 return;
1793#endif
1794 }
1795
Ranjeet Singh421231a2017-03-31 15:28:06 +00001796#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001797 // There is no static unwind info for this pc. Look to see if an FDE was
1798 // dynamically registered for it.
1799 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1800 if (cachedFDE != 0) {
1801 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1802 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1803 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1804 cachedFDE, &fdeInfo, &cieInfo);
1805 if (msg == NULL) {
1806 typename CFI_Parser<A>::PrologInfo prolog;
1807 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1808 pc, &prolog)) {
1809 // save off parsed FDE info
1810 _info.start_ip = fdeInfo.pcStart;
1811 _info.end_ip = fdeInfo.pcEnd;
1812 _info.lsda = fdeInfo.lsda;
1813 _info.handler = cieInfo.personality;
1814 _info.gp = prolog.spExtraArgSize;
1815 // Some frameless functions need SP
1816 // altered when resuming in function.
1817 _info.flags = 0;
1818 _info.format = dwarfEncoding();
1819 _info.unwind_info = fdeInfo.fdeStart;
1820 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1821 _info.extra = 0;
1822 return;
1823 }
1824 }
1825 }
1826
1827 // Lastly, ask AddressSpace object about platform specific ways to locate
1828 // other FDEs.
1829 pint_t fde;
1830 if (_addressSpace.findOtherFDE(pc, fde)) {
1831 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1832 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1833 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1834 // Double check this FDE is for a function that includes the pc.
1835 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1836 typename CFI_Parser<A>::PrologInfo prolog;
1837 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1838 cieInfo, pc, &prolog)) {
1839 // save off parsed FDE info
1840 _info.start_ip = fdeInfo.pcStart;
1841 _info.end_ip = fdeInfo.pcEnd;
1842 _info.lsda = fdeInfo.lsda;
1843 _info.handler = cieInfo.personality;
1844 _info.gp = prolog.spExtraArgSize;
1845 _info.flags = 0;
1846 _info.format = dwarfEncoding();
1847 _info.unwind_info = fdeInfo.fdeStart;
1848 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1849 _info.extra = 0;
1850 return;
1851 }
1852 }
1853 }
1854 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001855#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001856
1857 // no unwind info, flag that we can't reliably unwind
1858 _unwindInfoMissing = true;
1859}
1860
1861template <typename A, typename R>
1862int UnwindCursor<A, R>::step() {
1863 // Bottom of stack is defined is when unwind info cannot be found.
1864 if (_unwindInfoMissing)
1865 return UNW_STEP_END;
1866
1867 // Use unwinding info to modify register set as if function returned.
1868 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001869#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001870 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001871#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1872 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001873#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001874 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001875#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001876 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001877#else
1878 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001879 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001880 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001881 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001882#endif
1883
1884 // update info based on new PC
1885 if (result == UNW_STEP_SUCCESS) {
1886 this->setInfoBasedOnIPRegister(true);
1887 if (_unwindInfoMissing)
1888 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001889 }
1890
1891 return result;
1892}
1893
1894template <typename A, typename R>
1895void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1896 *info = _info;
1897}
1898
1899template <typename A, typename R>
1900bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1901 unw_word_t *offset) {
1902 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1903 buf, bufLen, offset);
1904}
1905
1906} // namespace libunwind
1907
1908#endif // __UNWINDCURSOR_HPP__