blob: 9164d78ef95286b3f74ae839e4f62afe7cbb32a9 [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
Ken Wernerfd21d072011-04-01 18:41:16 +020030#define arm_exidx_step UNW_OBJ(arm_exidx_step)
31
Zachary T Welch6a671542011-03-15 18:15:43 +000032static inline int
33arm_exidx_step (struct cursor *c)
34{
Ken Wernercf8d5e42011-03-23 15:54:59 +000035 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 Welch6a671542011-03-15 18:15:43 +000048 if (NULL == table)
49 return -UNW_ENOINFO;
50
Ken Wernercf8d5e42011-03-23 15:54:59 +000051 entry = arm_exidx_table_lookup (table, (void *)c->dwarf.ip);
Zachary T Welch6a671542011-03-15 18:15:43 +000052 if (NULL == entry)
53 return -UNW_ENOINFO;
54
Ken Wernercf8d5e42011-03-23 15:54:59 +000055 ret = arm_exidx_extract (entry, buf);
Zachary T Welch6a671542011-03-15 18:15:43 +000056 if (ret < 0)
57 return ret;
58
Ken Wernercf8d5e42011-03-23 15:54:59 +000059 ret = arm_exidx_decode (buf, ret, &c->dwarf);
Zachary T Welch6a671542011-03-15 18:15:43 +000060 if (ret < 0)
Ken Wernercf8d5e42011-03-23 15:54:59 +000061 return ret;
Zachary T Welch6a671542011-03-15 18:15:43 +000062
Ken Wernercf8d5e42011-03-23 15:54:59 +000063 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 Welch6a671542011-03-15 18:15:43 +000071}
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070072
73PROTECTED int
74unw_step (unw_cursor_t *cursor)
75{
76 struct cursor *c = (struct cursor *) cursor;
Arun Sharma00aed962010-05-26 19:28:44 -070077 int ret = -UNW_EUNSPEC;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070078
79 Debug (1, "(cursor=%p)\n", c);
80
Ken Wernerdcb8d0d2011-03-23 15:55:02 +000081 /* First, try DWARF-based unwinding. */
Arun Sharma00aed962010-05-26 19:28:44 -070082 if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
83 {
84 ret = dwarf_step (&c->dwarf);
85 Debug(1, "dwarf_step()=%d\n", ret);
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070086
Arun Sharma00aed962010-05-26 19:28:44 -070087 if (unlikely (ret == -UNW_ESTOPUNWIND))
88 return ret;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070089
Arun Sharma00aed962010-05-26 19:28:44 -070090 if (ret < 0 && ret != -UNW_ENOINFO)
91 {
92 Debug (2, "returning %d\n", ret);
93 return ret;
94 }
95 }
96
Ken Wernerdcb8d0d2011-03-23 15:55:02 +000097 /* 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 Jacobowitz3842dac2008-02-04 17:16:37 -0700109 if (unlikely (ret < 0))
Arun Sharma00aed962010-05-26 19:28:44 -0700110 {
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 Jacobowitz3842dac2008-02-04 17:16:37 -0700167}