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