blob: ab556d78fa1ea490e5fc576d58b5b8f2dd96e2ae [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{
33 struct arm_exidx_table *table = arm_exidx_table_find (c->frame.pc);
34 if (NULL == table)
35 return -UNW_ENOINFO;
36
37 struct arm_exidx_entry *entry = arm_exidx_table_lookup (table, c->frame.pc);
38 if (NULL == entry)
39 return -UNW_ENOINFO;
40
41 struct arm_exidx_vrs s;
42 arm_exidx_frame_to_vrs (&c->frame, &s);
43
44 uint8_t buf[32];
45 int ret = arm_exidx_extract (entry, buf);
46 if (ret < 0)
47 return ret;
48
49 ret = arm_exidx_decode (buf, ret, &arm_exidx_vrs_callback, &s);
50 if (ret < 0)
51 return -ret;
52
53 return arm_exidx_vrs_to_frame (&s, &c->frame);
54}
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070055
56PROTECTED int
57unw_step (unw_cursor_t *cursor)
58{
59 struct cursor *c = (struct cursor *) cursor;
Arun Sharma00aed962010-05-26 19:28:44 -070060 int ret = -UNW_EUNSPEC;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070061
62 Debug (1, "(cursor=%p)\n", c);
63
Zachary T Welch6a671542011-03-15 18:15:43 +000064 if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX))
65 {
66 ret = arm_exidx_step (c);
67 if (ret >= 0)
68 return ret;
69 if (ret < 0 && ret != -UNW_ENOINFO)
70 return ret;
71 }
72
73 /* Next, try DWARF-based unwinding. */
Arun Sharma00aed962010-05-26 19:28:44 -070074 if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
75 {
76 ret = dwarf_step (&c->dwarf);
77 Debug(1, "dwarf_step()=%d\n", ret);
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070078
Arun Sharma00aed962010-05-26 19:28:44 -070079 if (unlikely (ret == -UNW_ESTOPUNWIND))
80 return ret;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070081
Arun Sharma00aed962010-05-26 19:28:44 -070082 if (ret < 0 && ret != -UNW_ENOINFO)
83 {
84 Debug (2, "returning %d\n", ret);
85 return ret;
86 }
87 }
88
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -070089 if (unlikely (ret < 0))
Arun Sharma00aed962010-05-26 19:28:44 -070090 {
91 if (UNW_TRY_METHOD(UNW_ARM_METHOD_FRAME))
92 {
93 ret = UNW_ESUCCESS;
94 /* DWARF unwinding failed, try to follow APCS/optimized APCS frame chain */
95 unw_word_t instr, i;
96 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
97 dwarf_loc_t ip_loc, fp_loc;
98 unw_word_t frame;
99 /* Mark all registers unsaved, since we don't know where
100 they are saved (if at all), except for the EBP and
101 EIP. */
102 if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_ARM_R11], &frame) < 0)
103 {
104 return 0;
105 }
106 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) {
107 c->dwarf.loc[i] = DWARF_NULL_LOC;
108 }
109 if (frame)
110 {
111 if (dwarf_get(&c->dwarf, DWARF_LOC(frame, 0), &instr) < 0)
112 {
113 return 0;
114 }
115 instr -= 8;
116 if (dwarf_get(&c->dwarf, DWARF_LOC(instr, 0), &instr) < 0)
117 {
118 return 0;
119 }
120 if ((instr & 0xFFFFD800) == 0xE92DD800)
121 {
122 /* Standard APCS frame. */
123 ip_loc = DWARF_LOC(frame - 4, 0);
124 fp_loc = DWARF_LOC(frame - 12, 0);
125 }
126 else
127 {
128 /* Codesourcery optimized normal frame. */
129 ip_loc = DWARF_LOC(frame, 0);
130 fp_loc = DWARF_LOC(frame - 4, 0);
131 }
132 if (dwarf_get(&c->dwarf, ip_loc, &c->dwarf.ip) < 0)
133 {
134 return 0;
135 }
136 c->dwarf.loc[UNW_ARM_R12] = ip_loc;
137 c->dwarf.loc[UNW_ARM_R11] = fp_loc;
138 Debug(15, "ip=%lx\n", c->dwarf.ip);
139 }
140 else
141 {
142 ret = -UNW_ENOINFO;
143 }
144 }
145 }
146 return ret == -UNW_ENOINFO ? 0 : 1;
Daniel Jacobowitz3842dac2008-02-04 17:16:37 -0700147}