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