blob: 18a780919b1146836ff67a07ce55d1a8aff27f22 [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//
9// C++ interface to lower levels of libuwind
10//===----------------------------------------------------------------------===//
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>
19#include <pthread.h>
20#include <unwind.h>
21
22#ifdef __APPLE__
23 #include <mach-o/dyld.h>
24#endif
25
26#include "config.h"
27
28#include "AddressSpace.hpp"
29#include "CompactUnwinder.hpp"
30#include "config.h"
31#include "DwarfInstructions.hpp"
32#include "EHHeaderParser.hpp"
33#include "libunwind.h"
34#include "Registers.hpp"
35#include "Unwind-EHABI.h"
36
37namespace libunwind {
38
39#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
40/// Cache of recently found FDEs.
41template <typename A>
42class _LIBUNWIND_HIDDEN DwarfFDECache {
43 typedef typename A::pint_t pint_t;
44public:
45 static pint_t findFDE(pint_t mh, pint_t pc);
46 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
47 static void removeAllIn(pint_t mh);
48 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
49 unw_word_t ip_end,
50 unw_word_t fde, unw_word_t mh));
51
52private:
53
54 struct entry {
55 pint_t mh;
56 pint_t ip_start;
57 pint_t ip_end;
58 pint_t fde;
59 };
60
61 // These fields are all static to avoid needing an initializer.
62 // There is only one instance of this class per process.
63 static pthread_rwlock_t _lock;
64#ifdef __APPLE__
65 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
66 static bool _registeredForDyldUnloads;
67#endif
68 // Can't use std::vector<> here because this code is below libc++.
69 static entry *_buffer;
70 static entry *_bufferUsed;
71 static entry *_bufferEnd;
72 static entry _initialBuffer[64];
73};
74
75template <typename A>
76typename DwarfFDECache<A>::entry *
77DwarfFDECache<A>::_buffer = _initialBuffer;
78
79template <typename A>
80typename DwarfFDECache<A>::entry *
81DwarfFDECache<A>::_bufferUsed = _initialBuffer;
82
83template <typename A>
84typename DwarfFDECache<A>::entry *
85DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
86
87template <typename A>
88typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
89
90template <typename A>
91pthread_rwlock_t DwarfFDECache<A>::_lock = PTHREAD_RWLOCK_INITIALIZER;
92
93#ifdef __APPLE__
94template <typename A>
95bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
96#endif
97
98template <typename A>
99typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
100 pint_t result = 0;
101 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_rdlock(&_lock));
102 for (entry *p = _buffer; p < _bufferUsed; ++p) {
103 if ((mh == p->mh) || (mh == 0)) {
104 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
105 result = p->fde;
106 break;
107 }
108 }
109 }
110 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
111 return result;
112}
113
114template <typename A>
115void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
116 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000117#if !defined(_LIBUNWIND_NO_HEAP)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000118 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
119 if (_bufferUsed >= _bufferEnd) {
120 size_t oldSize = (size_t)(_bufferEnd - _buffer);
121 size_t newSize = oldSize * 4;
122 // Can't use operator new (we are below it).
123 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
124 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
125 if (_buffer != _initialBuffer)
126 free(_buffer);
127 _buffer = newBuffer;
128 _bufferUsed = &newBuffer[oldSize];
129 _bufferEnd = &newBuffer[newSize];
130 }
131 _bufferUsed->mh = mh;
132 _bufferUsed->ip_start = ip_start;
133 _bufferUsed->ip_end = ip_end;
134 _bufferUsed->fde = fde;
135 ++_bufferUsed;
136#ifdef __APPLE__
137 if (!_registeredForDyldUnloads) {
138 _dyld_register_func_for_remove_image(&dyldUnloadHook);
139 _registeredForDyldUnloads = true;
140 }
141#endif
142 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
Peter Zotov0717a2e2015-11-09 06:57:29 +0000143#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144}
145
146template <typename A>
147void DwarfFDECache<A>::removeAllIn(pint_t mh) {
148 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
149 entry *d = _buffer;
150 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
151 if (s->mh != mh) {
152 if (d != s)
153 *d = *s;
154 ++d;
155 }
156 }
157 _bufferUsed = d;
158 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
159}
160
161#ifdef __APPLE__
162template <typename A>
163void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
164 removeAllIn((pint_t) mh);
165}
166#endif
167
168template <typename A>
169void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
170 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
171 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
172 for (entry *p = _buffer; p < _bufferUsed; ++p) {
173 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
174 }
175 _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
176}
177#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
178
179
180#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
181
182#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
183template <typename A> class UnwindSectionHeader {
184public:
185 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
186 : _addressSpace(addressSpace), _addr(addr) {}
187
188 uint32_t version() const {
189 return _addressSpace.get32(_addr +
190 offsetof(unwind_info_section_header, version));
191 }
192 uint32_t commonEncodingsArraySectionOffset() const {
193 return _addressSpace.get32(_addr +
194 offsetof(unwind_info_section_header,
195 commonEncodingsArraySectionOffset));
196 }
197 uint32_t commonEncodingsArrayCount() const {
198 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
199 commonEncodingsArrayCount));
200 }
201 uint32_t personalityArraySectionOffset() const {
202 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
203 personalityArraySectionOffset));
204 }
205 uint32_t personalityArrayCount() const {
206 return _addressSpace.get32(
207 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
208 }
209 uint32_t indexSectionOffset() const {
210 return _addressSpace.get32(
211 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
212 }
213 uint32_t indexCount() const {
214 return _addressSpace.get32(
215 _addr + offsetof(unwind_info_section_header, indexCount));
216 }
217
218private:
219 A &_addressSpace;
220 typename A::pint_t _addr;
221};
222
223template <typename A> class UnwindSectionIndexArray {
224public:
225 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
226 : _addressSpace(addressSpace), _addr(addr) {}
227
228 uint32_t functionOffset(uint32_t index) const {
229 return _addressSpace.get32(
230 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
231 functionOffset));
232 }
233 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
234 return _addressSpace.get32(
235 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
236 secondLevelPagesSectionOffset));
237 }
238 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
239 return _addressSpace.get32(
240 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
241 lsdaIndexArraySectionOffset));
242 }
243
244private:
245 A &_addressSpace;
246 typename A::pint_t _addr;
247};
248
249template <typename A> class UnwindSectionRegularPageHeader {
250public:
251 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
252 : _addressSpace(addressSpace), _addr(addr) {}
253
254 uint32_t kind() const {
255 return _addressSpace.get32(
256 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
257 }
258 uint16_t entryPageOffset() const {
259 return _addressSpace.get16(
260 _addr + offsetof(unwind_info_regular_second_level_page_header,
261 entryPageOffset));
262 }
263 uint16_t entryCount() const {
264 return _addressSpace.get16(
265 _addr +
266 offsetof(unwind_info_regular_second_level_page_header, entryCount));
267 }
268
269private:
270 A &_addressSpace;
271 typename A::pint_t _addr;
272};
273
274template <typename A> class UnwindSectionRegularArray {
275public:
276 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
277 : _addressSpace(addressSpace), _addr(addr) {}
278
279 uint32_t functionOffset(uint32_t index) const {
280 return _addressSpace.get32(
281 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
282 functionOffset));
283 }
284 uint32_t encoding(uint32_t index) const {
285 return _addressSpace.get32(
286 _addr +
287 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
288 }
289
290private:
291 A &_addressSpace;
292 typename A::pint_t _addr;
293};
294
295template <typename A> class UnwindSectionCompressedPageHeader {
296public:
297 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
298 : _addressSpace(addressSpace), _addr(addr) {}
299
300 uint32_t kind() const {
301 return _addressSpace.get32(
302 _addr +
303 offsetof(unwind_info_compressed_second_level_page_header, kind));
304 }
305 uint16_t entryPageOffset() const {
306 return _addressSpace.get16(
307 _addr + offsetof(unwind_info_compressed_second_level_page_header,
308 entryPageOffset));
309 }
310 uint16_t entryCount() const {
311 return _addressSpace.get16(
312 _addr +
313 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
314 }
315 uint16_t encodingsPageOffset() const {
316 return _addressSpace.get16(
317 _addr + offsetof(unwind_info_compressed_second_level_page_header,
318 encodingsPageOffset));
319 }
320 uint16_t encodingsCount() const {
321 return _addressSpace.get16(
322 _addr + offsetof(unwind_info_compressed_second_level_page_header,
323 encodingsCount));
324 }
325
326private:
327 A &_addressSpace;
328 typename A::pint_t _addr;
329};
330
331template <typename A> class UnwindSectionCompressedArray {
332public:
333 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
334 : _addressSpace(addressSpace), _addr(addr) {}
335
336 uint32_t functionOffset(uint32_t index) const {
337 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
338 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
339 }
340 uint16_t encodingIndex(uint32_t index) const {
341 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
342 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
343 }
344
345private:
346 A &_addressSpace;
347 typename A::pint_t _addr;
348};
349
350template <typename A> class UnwindSectionLsdaArray {
351public:
352 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
353 : _addressSpace(addressSpace), _addr(addr) {}
354
355 uint32_t functionOffset(uint32_t index) const {
356 return _addressSpace.get32(
357 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
358 index, functionOffset));
359 }
360 uint32_t lsdaOffset(uint32_t index) const {
361 return _addressSpace.get32(
362 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
363 index, lsdaOffset));
364 }
365
366private:
367 A &_addressSpace;
368 typename A::pint_t _addr;
369};
370#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
371
372class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
373public:
374 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
375 // This avoids an unnecessary dependency to libc++abi.
376 void operator delete(void *, size_t) {}
377
378 virtual ~AbstractUnwindCursor() {}
379 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
380 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
381 virtual void setReg(int, unw_word_t) {
382 _LIBUNWIND_ABORT("setReg not implemented");
383 }
384 virtual bool validFloatReg(int) {
385 _LIBUNWIND_ABORT("validFloatReg not implemented");
386 }
387 virtual unw_fpreg_t getFloatReg(int) {
388 _LIBUNWIND_ABORT("getFloatReg not implemented");
389 }
390 virtual void setFloatReg(int, unw_fpreg_t) {
391 _LIBUNWIND_ABORT("setFloatReg not implemented");
392 }
393 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
394 virtual void getInfo(unw_proc_info_t *) {
395 _LIBUNWIND_ABORT("getInfo not implemented");
396 }
397 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
398 virtual bool isSignalFrame() {
399 _LIBUNWIND_ABORT("isSignalFrame not implemented");
400 }
401 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
402 _LIBUNWIND_ABORT("getFunctionName not implemented");
403 }
404 virtual void setInfoBasedOnIPRegister(bool = false) {
405 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
406 }
407 virtual const char *getRegisterName(int) {
408 _LIBUNWIND_ABORT("getRegisterName not implemented");
409 }
410#ifdef __arm__
411 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
412#endif
413};
414
415/// UnwindCursor contains all state (including all register values) during
416/// an unwind. This is normally stack allocated inside a unw_cursor_t.
417template <typename A, typename R>
418class UnwindCursor : public AbstractUnwindCursor{
419 typedef typename A::pint_t pint_t;
420public:
421 UnwindCursor(unw_context_t *context, A &as);
422 UnwindCursor(A &as, void *threadArg);
423 virtual ~UnwindCursor() {}
424 virtual bool validReg(int);
425 virtual unw_word_t getReg(int);
426 virtual void setReg(int, unw_word_t);
427 virtual bool validFloatReg(int);
428 virtual unw_fpreg_t getFloatReg(int);
429 virtual void setFloatReg(int, unw_fpreg_t);
430 virtual int step();
431 virtual void getInfo(unw_proc_info_t *);
432 virtual void jumpto();
433 virtual bool isSignalFrame();
434 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
435 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
436 virtual const char *getRegisterName(int num);
437#ifdef __arm__
438 virtual void saveVFPAsX();
439#endif
440
441private:
442
Logan Chien06b0c7a2015-07-19 15:23:10 +0000443#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000444 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000445
446 int stepWithEHABI() {
447 size_t len = 0;
448 size_t off = 0;
449 // FIXME: Calling decode_eht_entry() here is violating the libunwind
450 // abstraction layer.
451 const uint32_t *ehtp =
452 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
453 &off, &len);
454 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
455 _URC_CONTINUE_UNWIND)
456 return UNW_STEP_END;
457 return UNW_STEP_SUCCESS;
458 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000459#endif
460
461#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
462 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
463 uint32_t fdeSectionOffsetHint=0);
464 int stepWithDwarfFDE() {
465 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
466 (pint_t)this->getReg(UNW_REG_IP),
467 (pint_t)_info.unwind_info,
468 _registers);
469 }
470#endif
471
472#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
473 bool getInfoFromCompactEncodingSection(pint_t pc,
474 const UnwindInfoSections &sects);
475 int stepWithCompactEncoding() {
476 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
477 if ( compactSaysUseDwarf() )
478 return stepWithDwarfFDE();
479 #endif
480 R dummy;
481 return stepWithCompactEncoding(dummy);
482 }
483
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000484#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000485 int stepWithCompactEncoding(Registers_x86_64 &) {
486 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
487 _info.format, _info.start_ip, _addressSpace, _registers);
488 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000489#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000490
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000491#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000492 int stepWithCompactEncoding(Registers_x86 &) {
493 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
494 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
495 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000496#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000497
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000498#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000499 int stepWithCompactEncoding(Registers_ppc &) {
500 return UNW_EINVAL;
501 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000502#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000503
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000504#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000505 int stepWithCompactEncoding(Registers_arm64 &) {
506 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
507 _info.format, _info.start_ip, _addressSpace, _registers);
508 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000509#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000510
511 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
512 R dummy;
513 return compactSaysUseDwarf(dummy, offset);
514 }
515
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000516#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000517 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
518 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
519 if (offset)
520 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
521 return true;
522 }
523 return false;
524 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000525#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000526
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000527#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000528 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
529 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
530 if (offset)
531 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
532 return true;
533 }
534 return false;
535 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000536#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000537
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000538#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000539 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
540 return true;
541 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000542#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000543
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000544#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000545 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
546 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
547 if (offset)
548 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
549 return true;
550 }
551 return false;
552 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000553#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000554#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
555
556#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
557 compact_unwind_encoding_t dwarfEncoding() const {
558 R dummy;
559 return dwarfEncoding(dummy);
560 }
561
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000562#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000563 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
564 return UNWIND_X86_64_MODE_DWARF;
565 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000566#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000567
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000568#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000569 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
570 return UNWIND_X86_MODE_DWARF;
571 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000572#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000573
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000574#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000575 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
576 return 0;
577 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000578#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000579
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000580#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000581 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
582 return UNWIND_ARM64_MODE_DWARF;
583 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000584#endif
Peter Zotov8d639992015-08-31 05:26:37 +0000585
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000586#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +0000587 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
588 return 0;
589 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000590#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000591#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
592
593
594 A &_addressSpace;
595 R _registers;
596 unw_proc_info_t _info;
597 bool _unwindInfoMissing;
598 bool _isSignalFrame;
599};
600
601
602template <typename A, typename R>
603UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
604 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
605 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +0000606 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000607 "UnwindCursor<> does not fit in unw_cursor_t");
608 memset(&_info, 0, sizeof(_info));
609}
610
611template <typename A, typename R>
612UnwindCursor<A, R>::UnwindCursor(A &as, void *)
613 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
614 memset(&_info, 0, sizeof(_info));
615 // FIXME
616 // fill in _registers from thread arg
617}
618
619
620template <typename A, typename R>
621bool UnwindCursor<A, R>::validReg(int regNum) {
622 return _registers.validRegister(regNum);
623}
624
625template <typename A, typename R>
626unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
627 return _registers.getRegister(regNum);
628}
629
630template <typename A, typename R>
631void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
632 _registers.setRegister(regNum, (typename A::pint_t)value);
633}
634
635template <typename A, typename R>
636bool UnwindCursor<A, R>::validFloatReg(int regNum) {
637 return _registers.validFloatRegister(regNum);
638}
639
640template <typename A, typename R>
641unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
642 return _registers.getFloatRegister(regNum);
643}
644
645template <typename A, typename R>
646void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
647 _registers.setFloatRegister(regNum, value);
648}
649
650template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
651 _registers.jumpto();
652}
653
654#ifdef __arm__
655template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
656 _registers.saveVFPAsX();
657}
658#endif
659
660template <typename A, typename R>
661const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
662 return _registers.getRegisterName(regNum);
663}
664
665template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
666 return _isSignalFrame;
667}
668
Logan Chien06b0c7a2015-07-19 15:23:10 +0000669#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000670struct EHABIIndexEntry {
671 uint32_t functionOffset;
672 uint32_t data;
673};
674
675template<typename A>
676struct EHABISectionIterator {
677 typedef EHABISectionIterator _Self;
678
679 typedef std::random_access_iterator_tag iterator_category;
680 typedef typename A::pint_t value_type;
681 typedef typename A::pint_t* pointer;
682 typedef typename A::pint_t& reference;
683 typedef size_t size_type;
684 typedef size_t difference_type;
685
686 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
687 return _Self(addressSpace, sects, 0);
688 }
689 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
690 return _Self(addressSpace, sects, sects.arm_section_length);
691 }
692
693 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
694 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
695
696 _Self& operator++() { ++_i; return *this; }
697 _Self& operator+=(size_t a) { _i += a; return *this; }
698 _Self& operator--() { assert(_i > 0); --_i; return *this; }
699 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
700
701 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
702 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
703
704 size_t operator-(const _Self& other) { return _i - other._i; }
705
706 bool operator==(const _Self& other) const {
707 assert(_addressSpace == other._addressSpace);
708 assert(_sects == other._sects);
709 return _i == other._i;
710 }
711
712 typename A::pint_t operator*() const { return functionAddress(); }
713
714 typename A::pint_t functionAddress() const {
715 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
716 EHABIIndexEntry, _i, functionOffset);
717 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
718 }
719
720 typename A::pint_t dataAddress() {
721 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
722 EHABIIndexEntry, _i, data);
723 return indexAddr;
724 }
725
726 private:
727 size_t _i;
728 A* _addressSpace;
729 const UnwindInfoSections* _sects;
730};
731
732template <typename A, typename R>
733bool UnwindCursor<A, R>::getInfoFromEHABISection(
734 pint_t pc,
735 const UnwindInfoSections &sects) {
736 EHABISectionIterator<A> begin =
737 EHABISectionIterator<A>::begin(_addressSpace, sects);
738 EHABISectionIterator<A> end =
739 EHABISectionIterator<A>::end(_addressSpace, sects);
740
741 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
742 if (itNextPC == begin || itNextPC == end)
743 return false;
744 EHABISectionIterator<A> itThisPC = itNextPC - 1;
745
746 pint_t thisPC = itThisPC.functionAddress();
747 pint_t nextPC = itNextPC.functionAddress();
748 pint_t indexDataAddr = itThisPC.dataAddress();
749
750 if (indexDataAddr == 0)
751 return false;
752
753 uint32_t indexData = _addressSpace.get32(indexDataAddr);
754 if (indexData == UNW_EXIDX_CANTUNWIND)
755 return false;
756
757 // If the high bit is set, the exception handling table entry is inline inside
758 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
759 // the table points at an offset in the exception handling table (section 5 EHABI).
760 pint_t exceptionTableAddr;
761 uint32_t exceptionTableData;
762 bool isSingleWordEHT;
763 if (indexData & 0x80000000) {
764 exceptionTableAddr = indexDataAddr;
765 // TODO(ajwong): Should this data be 0?
766 exceptionTableData = indexData;
767 isSingleWordEHT = true;
768 } else {
769 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
770 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
771 isSingleWordEHT = false;
772 }
773
774 // Now we know the 3 things:
775 // exceptionTableAddr -- exception handler table entry.
776 // exceptionTableData -- the data inside the first word of the eht entry.
777 // isSingleWordEHT -- whether the entry is in the index.
778 unw_word_t personalityRoutine = 0xbadf00d;
779 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000780 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000781
782 // If the high bit in the exception handling table entry is set, the entry is
783 // in compact form (section 6.3 EHABI).
784 if (exceptionTableData & 0x80000000) {
785 // Grab the index of the personality routine from the compact form.
786 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
787 uint32_t extraWords = 0;
788 switch (choice) {
789 case 0:
790 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
791 extraWords = 0;
792 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000793 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000794 break;
795 case 1:
796 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
797 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
798 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +0000799 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000800 break;
801 case 2:
802 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
803 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
804 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +0000805 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000806 break;
807 default:
808 _LIBUNWIND_ABORT("unknown personality routine");
809 return false;
810 }
811
812 if (isSingleWordEHT) {
813 if (extraWords != 0) {
814 _LIBUNWIND_ABORT("index inlined table detected but pr function "
815 "requires extra words");
816 return false;
817 }
818 }
819 } else {
820 pint_t personalityAddr =
821 exceptionTableAddr + signExtendPrel31(exceptionTableData);
822 personalityRoutine = personalityAddr;
823
824 // ARM EHABI # 6.2, # 9.2
825 //
826 // +---- ehtp
827 // v
828 // +--------------------------------------+
829 // | +--------+--------+--------+-------+ |
830 // | |0| prel31 to personalityRoutine | |
831 // | +--------+--------+--------+-------+ |
832 // | | N | unwind opcodes | | <-- UnwindData
833 // | +--------+--------+--------+-------+ |
834 // | | Word 2 unwind opcodes | |
835 // | +--------+--------+--------+-------+ |
836 // | ... |
837 // | +--------+--------+--------+-------+ |
838 // | | Word N unwind opcodes | |
839 // | +--------+--------+--------+-------+ |
840 // | | LSDA | | <-- lsda
841 // | | ... | |
842 // | +--------+--------+--------+-------+ |
843 // +--------------------------------------+
844
845 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
846 uint32_t FirstDataWord = *UnwindData;
847 size_t N = ((FirstDataWord >> 24) & 0xff);
848 size_t NDataWords = N + 1;
849 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
850 }
851
852 _info.start_ip = thisPC;
853 _info.end_ip = nextPC;
854 _info.handler = personalityRoutine;
855 _info.unwind_info = exceptionTableAddr;
856 _info.lsda = lsda;
857 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
858 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
859
860 return true;
861}
862#endif
863
864#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
865template <typename A, typename R>
866bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
867 const UnwindInfoSections &sects,
868 uint32_t fdeSectionOffsetHint) {
869 typename CFI_Parser<A>::FDE_Info fdeInfo;
870 typename CFI_Parser<A>::CIE_Info cieInfo;
871 bool foundFDE = false;
872 bool foundInCache = false;
873 // If compact encoding table gave offset into dwarf section, go directly there
874 if (fdeSectionOffsetHint != 0) {
875 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
876 (uint32_t)sects.dwarf_section_length,
877 sects.dwarf_section + fdeSectionOffsetHint,
878 &fdeInfo, &cieInfo);
879 }
880#if _LIBUNWIND_SUPPORT_DWARF_INDEX
881 if (!foundFDE && (sects.dwarf_index_section != 0)) {
882 foundFDE = EHHeaderParser<A>::findFDE(
883 _addressSpace, pc, sects.dwarf_index_section,
884 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
885 }
886#endif
887 if (!foundFDE) {
888 // otherwise, search cache of previously found FDEs.
889 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
890 if (cachedFDE != 0) {
891 foundFDE =
892 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
893 (uint32_t)sects.dwarf_section_length,
894 cachedFDE, &fdeInfo, &cieInfo);
895 foundInCache = foundFDE;
896 }
897 }
898 if (!foundFDE) {
899 // Still not found, do full scan of __eh_frame section.
900 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
901 (uint32_t)sects.dwarf_section_length, 0,
902 &fdeInfo, &cieInfo);
903 }
904 if (foundFDE) {
905 typename CFI_Parser<A>::PrologInfo prolog;
906 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
907 &prolog)) {
908 // Save off parsed FDE info
909 _info.start_ip = fdeInfo.pcStart;
910 _info.end_ip = fdeInfo.pcEnd;
911 _info.lsda = fdeInfo.lsda;
912 _info.handler = cieInfo.personality;
913 _info.gp = prolog.spExtraArgSize;
914 _info.flags = 0;
915 _info.format = dwarfEncoding();
916 _info.unwind_info = fdeInfo.fdeStart;
917 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
918 _info.extra = (unw_word_t) sects.dso_base;
919
920 // Add to cache (to make next lookup faster) if we had no hint
921 // and there was no index.
922 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
923 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
924 if (sects.dwarf_index_section == 0)
925 #endif
926 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
927 fdeInfo.fdeStart);
928 }
929 return true;
930 }
931 }
932 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX\n", (uint64_t)pc);
933 return false;
934}
935#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
936
937
938#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
939template <typename A, typename R>
940bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
941 const UnwindInfoSections &sects) {
942 const bool log = false;
943 if (log)
944 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
945 (uint64_t)pc, (uint64_t)sects.dso_base);
946
947 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
948 sects.compact_unwind_section);
949 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
950 return false;
951
952 // do a binary search of top level index to find page with unwind info
953 pint_t targetFunctionOffset = pc - sects.dso_base;
954 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
955 sects.compact_unwind_section
956 + sectionHeader.indexSectionOffset());
957 uint32_t low = 0;
958 uint32_t high = sectionHeader.indexCount();
959 uint32_t last = high - 1;
960 while (low < high) {
961 uint32_t mid = (low + high) / 2;
962 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
963 //mid, low, high, topIndex.functionOffset(mid));
964 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
965 if ((mid == last) ||
966 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
967 low = mid;
968 break;
969 } else {
970 low = mid + 1;
971 }
972 } else {
973 high = mid;
974 }
975 }
976 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
977 const uint32_t firstLevelNextPageFunctionOffset =
978 topIndex.functionOffset(low + 1);
979 const pint_t secondLevelAddr =
980 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
981 const pint_t lsdaArrayStartAddr =
982 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
983 const pint_t lsdaArrayEndAddr =
984 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
985 if (log)
986 fprintf(stderr, "\tfirst level search for result index=%d "
987 "to secondLevelAddr=0x%llX\n",
988 low, (uint64_t) secondLevelAddr);
989 // do a binary search of second level page index
990 uint32_t encoding = 0;
991 pint_t funcStart = 0;
992 pint_t funcEnd = 0;
993 pint_t lsda = 0;
994 pint_t personality = 0;
995 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
996 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
997 // regular page
998 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
999 secondLevelAddr);
1000 UnwindSectionRegularArray<A> pageIndex(
1001 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1002 // binary search looks for entry with e where index[e].offset <= pc <
1003 // index[e+1].offset
1004 if (log)
1005 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1006 "regular page starting at secondLevelAddr=0x%llX\n",
1007 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1008 low = 0;
1009 high = pageHeader.entryCount();
1010 while (low < high) {
1011 uint32_t mid = (low + high) / 2;
1012 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1013 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1014 // at end of table
1015 low = mid;
1016 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1017 break;
1018 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1019 // next is too big, so we found it
1020 low = mid;
1021 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1022 break;
1023 } else {
1024 low = mid + 1;
1025 }
1026 } else {
1027 high = mid;
1028 }
1029 }
1030 encoding = pageIndex.encoding(low);
1031 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1032 if (pc < funcStart) {
1033 if (log)
1034 fprintf(
1035 stderr,
1036 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1037 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1038 return false;
1039 }
1040 if (pc > funcEnd) {
1041 if (log)
1042 fprintf(
1043 stderr,
1044 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1045 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1046 return false;
1047 }
1048 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1049 // compressed page
1050 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1051 secondLevelAddr);
1052 UnwindSectionCompressedArray<A> pageIndex(
1053 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1054 const uint32_t targetFunctionPageOffset =
1055 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1056 // binary search looks for entry with e where index[e].offset <= pc <
1057 // index[e+1].offset
1058 if (log)
1059 fprintf(stderr, "\tbinary search of compressed page starting at "
1060 "secondLevelAddr=0x%llX\n",
1061 (uint64_t) secondLevelAddr);
1062 low = 0;
1063 last = pageHeader.entryCount() - 1;
1064 high = pageHeader.entryCount();
1065 while (low < high) {
1066 uint32_t mid = (low + high) / 2;
1067 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1068 if ((mid == last) ||
1069 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1070 low = mid;
1071 break;
1072 } else {
1073 low = mid + 1;
1074 }
1075 } else {
1076 high = mid;
1077 }
1078 }
1079 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1080 + sects.dso_base;
1081 if (low < last)
1082 funcEnd =
1083 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1084 + sects.dso_base;
1085 else
1086 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1087 if (pc < funcStart) {
1088 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1089 "level compressed unwind table. funcStart=0x%llX\n",
1090 (uint64_t) pc, (uint64_t) funcStart);
1091 return false;
1092 }
1093 if (pc > funcEnd) {
1094 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
1095 "level compressed unwind table. funcEnd=0x%llX\n",
1096 (uint64_t) pc, (uint64_t) funcEnd);
1097 return false;
1098 }
1099 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1100 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1101 // encoding is in common table in section header
1102 encoding = _addressSpace.get32(
1103 sects.compact_unwind_section +
1104 sectionHeader.commonEncodingsArraySectionOffset() +
1105 encodingIndex * sizeof(uint32_t));
1106 } else {
1107 // encoding is in page specific table
1108 uint16_t pageEncodingIndex =
1109 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1110 encoding = _addressSpace.get32(secondLevelAddr +
1111 pageHeader.encodingsPageOffset() +
1112 pageEncodingIndex * sizeof(uint32_t));
1113 }
1114 } else {
1115 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
1116 "level page\n",
1117 (uint64_t) sects.compact_unwind_section);
1118 return false;
1119 }
1120
1121 // look up LSDA, if encoding says function has one
1122 if (encoding & UNWIND_HAS_LSDA) {
1123 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1124 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1125 low = 0;
1126 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1127 sizeof(unwind_info_section_header_lsda_index_entry);
1128 // binary search looks for entry with exact match for functionOffset
1129 if (log)
1130 fprintf(stderr,
1131 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1132 funcStartOffset);
1133 while (low < high) {
1134 uint32_t mid = (low + high) / 2;
1135 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1136 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1137 break;
1138 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1139 low = mid + 1;
1140 } else {
1141 high = mid;
1142 }
1143 }
1144 if (lsda == 0) {
1145 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
1146 "pc=0x%0llX, but lsda table has no entry\n",
1147 encoding, (uint64_t) pc);
1148 return false;
1149 }
1150 }
1151
1152 // extact personality routine, if encoding says function has one
1153 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1154 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1155 if (personalityIndex != 0) {
1156 --personalityIndex; // change 1-based to zero-based index
1157 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1158 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
1159 "but personality table has only %d entires\n",
1160 encoding, personalityIndex,
1161 sectionHeader.personalityArrayCount());
1162 return false;
1163 }
1164 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1165 sects.compact_unwind_section +
1166 sectionHeader.personalityArraySectionOffset() +
1167 personalityIndex * sizeof(uint32_t));
1168 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1169 personality = _addressSpace.getP(personalityPointer);
1170 if (log)
1171 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1172 "personalityDelta=0x%08X, personality=0x%08llX\n",
1173 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1174 }
1175
1176 if (log)
1177 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1178 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1179 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1180 _info.start_ip = funcStart;
1181 _info.end_ip = funcEnd;
1182 _info.lsda = lsda;
1183 _info.handler = personality;
1184 _info.gp = 0;
1185 _info.flags = 0;
1186 _info.format = encoding;
1187 _info.unwind_info = 0;
1188 _info.unwind_info_size = 0;
1189 _info.extra = sects.dso_base;
1190 return true;
1191}
1192#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1193
1194
1195template <typename A, typename R>
1196void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1197 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Logan Chien06b0c7a2015-07-19 15:23:10 +00001198#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001199 // Remove the thumb bit so the IP represents the actual instruction address.
1200 // This matches the behaviour of _Unwind_GetIP on arm.
1201 pc &= (pint_t)~0x1;
1202#endif
1203
1204 // If the last line of a function is a "throw" the compiler sometimes
1205 // emits no instructions after the call to __cxa_throw. This means
1206 // the return address is actually the start of the next function.
1207 // To disambiguate this, back up the pc when we know it is a return
1208 // address.
1209 if (isReturnAddress)
1210 --pc;
1211
1212 // Ask address space object to find unwind sections for this pc.
1213 UnwindInfoSections sects;
1214 if (_addressSpace.findUnwindSections(pc, sects)) {
1215#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1216 // If there is a compact unwind encoding table, look there first.
1217 if (sects.compact_unwind_section != 0) {
1218 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
1219 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1220 // Found info in table, done unless encoding says to use dwarf.
1221 uint32_t dwarfOffset;
1222 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1223 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1224 // found info in dwarf, done
1225 return;
1226 }
1227 }
1228 #endif
1229 // If unwind table has entry, but entry says there is no unwind info,
1230 // record that we have no unwind info.
1231 if (_info.format == 0)
1232 _unwindInfoMissing = true;
1233 return;
1234 }
1235 }
1236#endif // _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1237
1238#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1239 // If there is dwarf unwind info, look there next.
1240 if (sects.dwarf_section != 0) {
1241 if (this->getInfoFromDwarfSection(pc, sects)) {
1242 // found info in dwarf, done
1243 return;
1244 }
1245 }
1246#endif
1247
Logan Chien06b0c7a2015-07-19 15:23:10 +00001248#if _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001249 // If there is ARM EHABI unwind info, look there next.
1250 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1251 return;
1252#endif
1253 }
1254
1255#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1256 // There is no static unwind info for this pc. Look to see if an FDE was
1257 // dynamically registered for it.
1258 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1259 if (cachedFDE != 0) {
1260 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1261 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1262 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1263 cachedFDE, &fdeInfo, &cieInfo);
1264 if (msg == NULL) {
1265 typename CFI_Parser<A>::PrologInfo prolog;
1266 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1267 pc, &prolog)) {
1268 // save off parsed FDE info
1269 _info.start_ip = fdeInfo.pcStart;
1270 _info.end_ip = fdeInfo.pcEnd;
1271 _info.lsda = fdeInfo.lsda;
1272 _info.handler = cieInfo.personality;
1273 _info.gp = prolog.spExtraArgSize;
1274 // Some frameless functions need SP
1275 // altered when resuming in function.
1276 _info.flags = 0;
1277 _info.format = dwarfEncoding();
1278 _info.unwind_info = fdeInfo.fdeStart;
1279 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1280 _info.extra = 0;
1281 return;
1282 }
1283 }
1284 }
1285
1286 // Lastly, ask AddressSpace object about platform specific ways to locate
1287 // other FDEs.
1288 pint_t fde;
1289 if (_addressSpace.findOtherFDE(pc, fde)) {
1290 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1291 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1292 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1293 // Double check this FDE is for a function that includes the pc.
1294 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1295 typename CFI_Parser<A>::PrologInfo prolog;
1296 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1297 cieInfo, pc, &prolog)) {
1298 // save off parsed FDE info
1299 _info.start_ip = fdeInfo.pcStart;
1300 _info.end_ip = fdeInfo.pcEnd;
1301 _info.lsda = fdeInfo.lsda;
1302 _info.handler = cieInfo.personality;
1303 _info.gp = prolog.spExtraArgSize;
1304 _info.flags = 0;
1305 _info.format = dwarfEncoding();
1306 _info.unwind_info = fdeInfo.fdeStart;
1307 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1308 _info.extra = 0;
1309 return;
1310 }
1311 }
1312 }
1313 }
1314#endif // #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
1315
1316 // no unwind info, flag that we can't reliably unwind
1317 _unwindInfoMissing = true;
1318}
1319
1320template <typename A, typename R>
1321int UnwindCursor<A, R>::step() {
1322 // Bottom of stack is defined is when unwind info cannot be found.
1323 if (_unwindInfoMissing)
1324 return UNW_STEP_END;
1325
1326 // Use unwinding info to modify register set as if function returned.
1327 int result;
1328#if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
1329 result = this->stepWithCompactEncoding();
1330#elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
1331 result = this->stepWithDwarfFDE();
Logan Chien06b0c7a2015-07-19 15:23:10 +00001332#elif _LIBUNWIND_ARM_EHABI
Logan Chiena54f0962015-05-29 15:33:38 +00001333 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001334#else
1335 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
1336 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001337 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001338#endif
1339
1340 // update info based on new PC
1341 if (result == UNW_STEP_SUCCESS) {
1342 this->setInfoBasedOnIPRegister(true);
1343 if (_unwindInfoMissing)
1344 return UNW_STEP_END;
1345 if (_info.gp)
1346 setReg(UNW_REG_SP, getReg(UNW_REG_SP) + _info.gp);
1347 }
1348
1349 return result;
1350}
1351
1352template <typename A, typename R>
1353void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1354 *info = _info;
1355}
1356
1357template <typename A, typename R>
1358bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1359 unw_word_t *offset) {
1360 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1361 buf, bufLen, offset);
1362}
1363
1364} // namespace libunwind
1365
1366#endif // __UNWINDCURSOR_HPP__