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