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 | { |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 33 | unw_word_t old_ip, old_cfa; |
| 34 | struct arm_exidx_table *table; |
| 35 | struct arm_exidx_entry *entry; |
| 36 | uint8_t buf[32]; |
| 37 | int ret; |
| 38 | |
| 39 | old_ip = c->dwarf.ip; |
| 40 | old_cfa = c->dwarf.cfa; |
| 41 | |
| 42 | /* mark PC unsaved */ |
| 43 | c->dwarf.loc[UNW_ARM_R15] = DWARF_NULL_LOC; |
| 44 | |
| 45 | table = arm_exidx_table_find ((void *)c->dwarf.ip); |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 46 | if (NULL == table) |
| 47 | return -UNW_ENOINFO; |
| 48 | |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 49 | entry = arm_exidx_table_lookup (table, (void *)c->dwarf.ip); |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 50 | if (NULL == entry) |
| 51 | return -UNW_ENOINFO; |
| 52 | |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 53 | ret = arm_exidx_extract (entry, buf); |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 57 | ret = arm_exidx_decode (buf, ret, &c->dwarf); |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 58 | if (ret < 0) |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 59 | return ret; |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 60 | |
Ken Werner | cf8d5e4 | 2011-03-23 15:54:59 +0000 | [diff] [blame] | 61 | if (c->dwarf.ip == old_ip && c->dwarf.cfa == old_cfa) |
| 62 | { |
| 63 | Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n", |
| 64 | __FUNCTION__, (long) c->dwarf.ip); |
| 65 | return -UNW_EBADFRAME; |
| 66 | } |
| 67 | |
| 68 | return (c->dwarf.ip == 0) ? 0 : 1; |
Zachary T Welch | 6a67154 | 2011-03-15 18:15:43 +0000 | [diff] [blame] | 69 | } |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 70 | |
| 71 | PROTECTED int |
| 72 | unw_step (unw_cursor_t *cursor) |
| 73 | { |
| 74 | struct cursor *c = (struct cursor *) cursor; |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 75 | int ret = -UNW_EUNSPEC; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 76 | |
| 77 | Debug (1, "(cursor=%p)\n", c); |
| 78 | |
Ken Werner | dcb8d0d | 2011-03-23 15:55:02 +0000 | [diff] [blame] | 79 | /* First, try DWARF-based unwinding. */ |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 80 | if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF)) |
| 81 | { |
| 82 | ret = dwarf_step (&c->dwarf); |
| 83 | Debug(1, "dwarf_step()=%d\n", ret); |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 84 | |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 85 | if (unlikely (ret == -UNW_ESTOPUNWIND)) |
| 86 | return ret; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 87 | |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 88 | if (ret < 0 && ret != -UNW_ENOINFO) |
| 89 | { |
| 90 | Debug (2, "returning %d\n", ret); |
| 91 | return ret; |
| 92 | } |
| 93 | } |
| 94 | |
Ken Werner | dcb8d0d | 2011-03-23 15:55:02 +0000 | [diff] [blame] | 95 | /* Next, try extbl-based unwinding. */ |
| 96 | if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX)) |
| 97 | { |
| 98 | ret = arm_exidx_step (c); |
| 99 | if (ret >= 0) |
| 100 | return 1; |
| 101 | if (ret == -UNW_ESTOPUNWIND) |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* Fall back on APCS frame parsing. |
| 106 | Note: This won't work in case the ARM EABI is used. */ |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 107 | if (unlikely (ret < 0)) |
Arun Sharma | 00aed96 | 2010-05-26 19:28:44 -0700 | [diff] [blame] | 108 | { |
| 109 | if (UNW_TRY_METHOD(UNW_ARM_METHOD_FRAME)) |
| 110 | { |
| 111 | ret = UNW_ESUCCESS; |
| 112 | /* DWARF unwinding failed, try to follow APCS/optimized APCS frame chain */ |
| 113 | unw_word_t instr, i; |
| 114 | Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret); |
| 115 | dwarf_loc_t ip_loc, fp_loc; |
| 116 | unw_word_t frame; |
| 117 | /* Mark all registers unsaved, since we don't know where |
| 118 | they are saved (if at all), except for the EBP and |
| 119 | EIP. */ |
| 120 | if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_ARM_R11], &frame) < 0) |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) { |
| 125 | c->dwarf.loc[i] = DWARF_NULL_LOC; |
| 126 | } |
| 127 | if (frame) |
| 128 | { |
| 129 | if (dwarf_get(&c->dwarf, DWARF_LOC(frame, 0), &instr) < 0) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | instr -= 8; |
| 134 | if (dwarf_get(&c->dwarf, DWARF_LOC(instr, 0), &instr) < 0) |
| 135 | { |
| 136 | return 0; |
| 137 | } |
| 138 | if ((instr & 0xFFFFD800) == 0xE92DD800) |
| 139 | { |
| 140 | /* Standard APCS frame. */ |
| 141 | ip_loc = DWARF_LOC(frame - 4, 0); |
| 142 | fp_loc = DWARF_LOC(frame - 12, 0); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | /* Codesourcery optimized normal frame. */ |
| 147 | ip_loc = DWARF_LOC(frame, 0); |
| 148 | fp_loc = DWARF_LOC(frame - 4, 0); |
| 149 | } |
| 150 | if (dwarf_get(&c->dwarf, ip_loc, &c->dwarf.ip) < 0) |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | c->dwarf.loc[UNW_ARM_R12] = ip_loc; |
| 155 | c->dwarf.loc[UNW_ARM_R11] = fp_loc; |
| 156 | Debug(15, "ip=%lx\n", c->dwarf.ip); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | ret = -UNW_ENOINFO; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return ret == -UNW_ENOINFO ? 0 : 1; |
Daniel Jacobowitz | 3842dac | 2008-02-04 17:16:37 -0700 | [diff] [blame] | 165 | } |