blob: f0360939fdce3a19b60b50f0f222d3d59f7d8e94 [file] [log] [blame]
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -07001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
Zachary T Welch6a671542011-03-15 18:15:43 +00003 Copyright 2011 Linaro Limited
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -07004
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26#include "unwind_i.h"
27#include "offsets.h"
Zachary T Welch6a671542011-03-15 18:15:43 +000028#include "ex_tables.h"
29
30static inline int
31arm_exidx_step (struct cursor *c)
32{
Ken Wernercf8d5e42011-03-23 15:54:59 +000033 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 Welch6a671542011-03-15 18:15:43 +000046 if (NULL == table)
47 return -UNW_ENOINFO;
48
Ken Wernercf8d5e42011-03-23 15:54:59 +000049 entry = arm_exidx_table_lookup (table, (void *)c->dwarf.ip);
Zachary T Welch6a671542011-03-15 18:15:43 +000050 if (NULL == entry)
51 return -UNW_ENOINFO;
52
Ken Wernercf8d5e42011-03-23 15:54:59 +000053 ret = arm_exidx_extract (entry, buf);
Zachary T Welch6a671542011-03-15 18:15:43 +000054 if (ret < 0)
55 return ret;
56
Ken Wernercf8d5e42011-03-23 15:54:59 +000057 ret = arm_exidx_decode (buf, ret, &c->dwarf);
Zachary T Welch6a671542011-03-15 18:15:43 +000058 if (ret < 0)
Ken Wernercf8d5e42011-03-23 15:54:59 +000059 return ret;
Zachary T Welch6a671542011-03-15 18:15:43 +000060
Ken Wernercf8d5e42011-03-23 15:54:59 +000061 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 Welch6a671542011-03-15 18:15:43 +000069}
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070070
71PROTECTED int
72unw_step (unw_cursor_t *cursor)
73{
74 struct cursor *c = (struct cursor *) cursor;
Arun Sharma00aed962010-05-26 19:28:44 -070075 int ret = -UNW_EUNSPEC;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070076
77 Debug (1, "(cursor=%p)\n", c);
78
Ken Wernerdcb8d0d2011-03-23 15:55:02 +000079 /* First, try DWARF-based unwinding. */
Arun Sharma00aed962010-05-26 19:28:44 -070080 if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
81 {
82 ret = dwarf_step (&c->dwarf);
83 Debug(1, "dwarf_step()=%d\n", ret);
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070084
Arun Sharma00aed962010-05-26 19:28:44 -070085 if (unlikely (ret == -UNW_ESTOPUNWIND))
86 return ret;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070087
Arun Sharma00aed962010-05-26 19:28:44 -070088 if (ret < 0 && ret != -UNW_ENOINFO)
89 {
90 Debug (2, "returning %d\n", ret);
91 return ret;
92 }
93 }
94
Ken Wernerdcb8d0d2011-03-23 15:55:02 +000095 /* 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 Jacobowitz3842dac2008-02-04 17:16:37 -0700107 if (unlikely (ret < 0))
Arun Sharma00aed962010-05-26 19:28:44 -0700108 {
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 Jacobowitz3842dac2008-02-04 17:16:37 -0700165}