Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2008 CodeSourcery |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame^] | 3 | Copyright 2011 Linaro Limited |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 4 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 25 | |
| 26 | #include "unwind_i.h" |
| 27 | #include "offsets.h" |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame^] | 28 | #include "ex_tables.h" |
| 29 | |
| 30 | static inline int |
| 31 | arm_exidx_step (struct cursor *c) |
| 32 | { |
| 33 | struct arm_exidx_table *table = arm_exidx_table_find (c->frame.pc); |
| 34 | if (NULL == table) |
| 35 | return -UNW_ENOINFO; |
| 36 | |
| 37 | struct arm_exidx_entry *entry = arm_exidx_table_lookup (table, c->frame.pc); |
| 38 | if (NULL == entry) |
| 39 | return -UNW_ENOINFO; |
| 40 | |
| 41 | struct arm_exidx_vrs s; |
| 42 | arm_exidx_frame_to_vrs (&c->frame, &s); |
| 43 | |
| 44 | uint8_t buf[32]; |
| 45 | int ret = arm_exidx_extract (entry, buf); |
| 46 | if (ret < 0) |
| 47 | return ret; |
| 48 | |
| 49 | ret = arm_exidx_decode (buf, ret, &arm_exidx_vrs_callback, &s); |
| 50 | if (ret < 0) |
| 51 | return -ret; |
| 52 | |
| 53 | return arm_exidx_vrs_to_frame (&s, &c->frame); |
| 54 | } |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 55 | |
| 56 | PROTECTED int |
| 57 | unw_step (unw_cursor_t *cursor) |
| 58 | { |
| 59 | struct cursor *c = (struct cursor *) cursor; |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 60 | int ret = -UNW_EUNSPEC; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 61 | |
| 62 | Debug (1, "(cursor=%p)\n", c); |
| 63 | |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame^] | 64 | if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX)) |
| 65 | { |
| 66 | ret = arm_exidx_step (c); |
| 67 | if (ret >= 0) |
| 68 | return ret; |
| 69 | if (ret < 0 && ret != -UNW_ENOINFO) |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | /* Next, try DWARF-based unwinding. */ |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 74 | if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF)) |
| 75 | { |
| 76 | ret = dwarf_step (&c->dwarf); |
| 77 | Debug(1, "dwarf_step()=%d\n", ret); |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 78 | |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 79 | if (unlikely (ret == -UNW_ESTOPUNWIND)) |
| 80 | return ret; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 81 | |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 82 | if (ret < 0 && ret != -UNW_ENOINFO) |
| 83 | { |
| 84 | Debug (2, "returning %d\n", ret); |
| 85 | return ret; |
| 86 | } |
| 87 | } |
| 88 | |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 89 | if (unlikely (ret < 0)) |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 90 | { |
| 91 | if (UNW_TRY_METHOD(UNW_ARM_METHOD_FRAME)) |
| 92 | { |
| 93 | ret = UNW_ESUCCESS; |
| 94 | /* DWARF unwinding failed, try to follow APCS/optimized APCS frame chain */ |
| 95 | unw_word_t instr, i; |
| 96 | Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret); |
| 97 | dwarf_loc_t ip_loc, fp_loc; |
| 98 | unw_word_t frame; |
| 99 | /* Mark all registers unsaved, since we don't know where |
| 100 | they are saved (if at all), except for the EBP and |
| 101 | EIP. */ |
| 102 | if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_ARM_R11], &frame) < 0) |
| 103 | { |
| 104 | return 0; |
| 105 | } |
| 106 | for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) { |
| 107 | c->dwarf.loc[i] = DWARF_NULL_LOC; |
| 108 | } |
| 109 | if (frame) |
| 110 | { |
| 111 | if (dwarf_get(&c->dwarf, DWARF_LOC(frame, 0), &instr) < 0) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | instr -= 8; |
| 116 | if (dwarf_get(&c->dwarf, DWARF_LOC(instr, 0), &instr) < 0) |
| 117 | { |
| 118 | return 0; |
| 119 | } |
| 120 | if ((instr & 0xFFFFD800) == 0xE92DD800) |
| 121 | { |
| 122 | /* Standard APCS frame. */ |
| 123 | ip_loc = DWARF_LOC(frame - 4, 0); |
| 124 | fp_loc = DWARF_LOC(frame - 12, 0); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | /* Codesourcery optimized normal frame. */ |
| 129 | ip_loc = DWARF_LOC(frame, 0); |
| 130 | fp_loc = DWARF_LOC(frame - 4, 0); |
| 131 | } |
| 132 | if (dwarf_get(&c->dwarf, ip_loc, &c->dwarf.ip) < 0) |
| 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | c->dwarf.loc[UNW_ARM_R12] = ip_loc; |
| 137 | c->dwarf.loc[UNW_ARM_R11] = fp_loc; |
| 138 | Debug(15, "ip=%lx\n", c->dwarf.ip); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | ret = -UNW_ENOINFO; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return ret == -UNW_ENOINFO ? 0 : 1; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 147 | } |