blob: 1b4b135f78b69c407437211df241c8dcc5bf9f30 [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
Zachary T Welch6a671542011-03-15 18:15:43 +000079 if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX))
80 {
81 ret = arm_exidx_step (c);
82 if (ret >= 0)
Ken Wernercf8d5e42011-03-23 15:54:59 +000083 return 1;
84 if (ret == -UNW_ESTOPUNWIND)
85 return 0;
Zachary T Welch6a671542011-03-15 18:15:43 +000086 }
87
88 /* Next, try DWARF-based unwinding. */
Arun Sharma00aed962010-05-26 19:28:44 -070089 if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
90 {
91 ret = dwarf_step (&c->dwarf);
92 Debug(1, "dwarf_step()=%d\n", ret);
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070093
Arun Sharma00aed962010-05-26 19:28:44 -070094 if (unlikely (ret == -UNW_ESTOPUNWIND))
95 return ret;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070096
Arun Sharma00aed962010-05-26 19:28:44 -070097 if (ret < 0 && ret != -UNW_ENOINFO)
98 {
99 Debug (2, "returning %d\n", ret);
100 return ret;
101 }
102 }
103
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -0700104 if (unlikely (ret < 0))
Arun Sharma00aed962010-05-26 19:28:44 -0700105 {
106 if (UNW_TRY_METHOD(UNW_ARM_METHOD_FRAME))
107 {
108 ret = UNW_ESUCCESS;
109 /* DWARF unwinding failed, try to follow APCS/optimized APCS frame chain */
110 unw_word_t instr, i;
111 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
112 dwarf_loc_t ip_loc, fp_loc;
113 unw_word_t frame;
114 /* Mark all registers unsaved, since we don't know where
115 they are saved (if at all), except for the EBP and
116 EIP. */
117 if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_ARM_R11], &frame) < 0)
118 {
119 return 0;
120 }
121 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) {
122 c->dwarf.loc[i] = DWARF_NULL_LOC;
123 }
124 if (frame)
125 {
126 if (dwarf_get(&c->dwarf, DWARF_LOC(frame, 0), &instr) < 0)
127 {
128 return 0;
129 }
130 instr -= 8;
131 if (dwarf_get(&c->dwarf, DWARF_LOC(instr, 0), &instr) < 0)
132 {
133 return 0;
134 }
135 if ((instr & 0xFFFFD800) == 0xE92DD800)
136 {
137 /* Standard APCS frame. */
138 ip_loc = DWARF_LOC(frame - 4, 0);
139 fp_loc = DWARF_LOC(frame - 12, 0);
140 }
141 else
142 {
143 /* Codesourcery optimized normal frame. */
144 ip_loc = DWARF_LOC(frame, 0);
145 fp_loc = DWARF_LOC(frame - 4, 0);
146 }
147 if (dwarf_get(&c->dwarf, ip_loc, &c->dwarf.ip) < 0)
148 {
149 return 0;
150 }
151 c->dwarf.loc[UNW_ARM_R12] = ip_loc;
152 c->dwarf.loc[UNW_ARM_R11] = fp_loc;
153 Debug(15, "ip=%lx\n", c->dwarf.ip);
154 }
155 else
156 {
157 ret = -UNW_ENOINFO;
158 }
159 }
160 }
161 return ret == -UNW_ENOINFO ? 0 : 1;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -0700162}