1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2015 Imagination Technologies Limited
3 Copyright (C) 2008 CodeSourcery
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include "unwind_i.h"
27 #include "offsets.h"
28
29 static int
mips_handle_signal_frame(unw_cursor_t * cursor)30 mips_handle_signal_frame (unw_cursor_t *cursor)
31 {
32 struct cursor *c = (struct cursor *) cursor;
33 unw_word_t sc_addr, sp_addr = c->dwarf.cfa;
34 unw_word_t ra, fp;
35 int i, ret;
36
37 switch (unw_is_signal_frame (cursor)) {
38 case 1:
39 sc_addr = sp_addr + LINUX_SF_TRAMP_SIZE + sizeof (siginfo_t) +
40 LINUX_UC_MCONTEXT_OFF;
41 break;
42 case 2:
43 sc_addr = sp_addr + LINUX_UC_MCONTEXT_OFF;
44 break;
45 default:
46 return -UNW_EUNSPEC;
47 }
48
49 if (tdep_big_endian(c->dwarf.as))
50 sc_addr += 4;
51
52 c->sigcontext_addr = sc_addr;
53
54 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
55 c->dwarf.loc[i] = DWARF_NULL_LOC;
56
57 /* Update the dwarf cursor. */
58 c->dwarf.loc[UNW_MIPS_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0);
59 c->dwarf.loc[UNW_MIPS_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0);
60 c->dwarf.loc[UNW_MIPS_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0);
61 c->dwarf.loc[UNW_MIPS_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0);
62 c->dwarf.loc[UNW_MIPS_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0);
63 c->dwarf.loc[UNW_MIPS_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0);
64 c->dwarf.loc[UNW_MIPS_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0);
65 c->dwarf.loc[UNW_MIPS_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0);
66 c->dwarf.loc[UNW_MIPS_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0);
67 c->dwarf.loc[UNW_MIPS_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0);
68 c->dwarf.loc[UNW_MIPS_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0);
69 c->dwarf.loc[UNW_MIPS_R11] = DWARF_LOC (sc_addr + LINUX_SC_R11_OFF, 0);
70 c->dwarf.loc[UNW_MIPS_R12] = DWARF_LOC (sc_addr + LINUX_SC_R12_OFF, 0);
71 c->dwarf.loc[UNW_MIPS_R13] = DWARF_LOC (sc_addr + LINUX_SC_R13_OFF, 0);
72 c->dwarf.loc[UNW_MIPS_R14] = DWARF_LOC (sc_addr + LINUX_SC_R14_OFF, 0);
73 c->dwarf.loc[UNW_MIPS_R15] = DWARF_LOC (sc_addr + LINUX_SC_R15_OFF, 0);
74 c->dwarf.loc[UNW_MIPS_R16] = DWARF_LOC (sc_addr + LINUX_SC_R16_OFF, 0);
75 c->dwarf.loc[UNW_MIPS_R17] = DWARF_LOC (sc_addr + LINUX_SC_R17_OFF, 0);
76 c->dwarf.loc[UNW_MIPS_R18] = DWARF_LOC (sc_addr + LINUX_SC_R18_OFF, 0);
77 c->dwarf.loc[UNW_MIPS_R19] = DWARF_LOC (sc_addr + LINUX_SC_R19_OFF, 0);
78 c->dwarf.loc[UNW_MIPS_R20] = DWARF_LOC (sc_addr + LINUX_SC_R20_OFF, 0);
79 c->dwarf.loc[UNW_MIPS_R21] = DWARF_LOC (sc_addr + LINUX_SC_R21_OFF, 0);
80 c->dwarf.loc[UNW_MIPS_R22] = DWARF_LOC (sc_addr + LINUX_SC_R22_OFF, 0);
81 c->dwarf.loc[UNW_MIPS_R23] = DWARF_LOC (sc_addr + LINUX_SC_R23_OFF, 0);
82 c->dwarf.loc[UNW_MIPS_R24] = DWARF_LOC (sc_addr + LINUX_SC_R24_OFF, 0);
83 c->dwarf.loc[UNW_MIPS_R25] = DWARF_LOC (sc_addr + LINUX_SC_R25_OFF, 0);
84 c->dwarf.loc[UNW_MIPS_R26] = DWARF_LOC (sc_addr + LINUX_SC_R26_OFF, 0);
85 c->dwarf.loc[UNW_MIPS_R27] = DWARF_LOC (sc_addr + LINUX_SC_R27_OFF, 0);
86 c->dwarf.loc[UNW_MIPS_R28] = DWARF_LOC (sc_addr + LINUX_SC_R28_OFF, 0);
87 c->dwarf.loc[UNW_MIPS_R29] = DWARF_LOC (sc_addr + LINUX_SC_R29_OFF, 0);
88 c->dwarf.loc[UNW_MIPS_R30] = DWARF_LOC (sc_addr + LINUX_SC_R30_OFF, 0);
89 c->dwarf.loc[UNW_MIPS_R31] = DWARF_LOC (sc_addr + LINUX_SC_R31_OFF, 0);
90 c->dwarf.loc[UNW_MIPS_PC] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
91
92 /* Set SP/CFA and PC/IP. */
93 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_MIPS_R29], &c->dwarf.cfa);
94
95 if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_PC_OFF, 0),
96 &c->dwarf.ip)) < 0)
97 return ret;
98
99 if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_R31_OFF, 0),
100 &ra)) < 0)
101 return ret;
102 if ((ret = dwarf_get(&c->dwarf, DWARF_LOC(sc_addr + LINUX_SC_R30_OFF, 0),
103 &fp)) < 0)
104 return ret;
105
106 Debug (2, "SH (ip=0x%016llx, ra=0x%016llx, sp=0x%016llx, fp=0x%016llx)\n",
107 (unsigned long long)c->dwarf.ip, (unsigned long long)ra,
108 (unsigned long long)c->dwarf.cfa, (unsigned long long)fp);
109
110 c->dwarf.pi_valid = 0;
111 c->dwarf.use_prev_instr = 0;
112
113 return 1;
114 }
115
116
117
118 static inline
is_valid_fp_val(unw_word_t cfa_val,unw_word_t fp_val)119 int is_valid_fp_val(unw_word_t cfa_val, unw_word_t fp_val)
120 {
121 return fp_val > 0 && cfa_val > 0 && fp_val >cfa_val && (fp_val - cfa_val < 0x4000);
122 }
123
_step_n64(struct cursor * c)124 static int _step_n64(struct cursor *c)
125 {
126 #define FP_REG UNW_MIPS_R30
127 #define SP_REG UNW_MIPS_R29
128 #define RA_REG UNW_MIPS_R31
129
130 //TODO:handle plt entry
131 int ret;
132 unw_word_t current_fp_val = 0;
133 unw_word_t current_ra_val = 0;
134 unw_word_t current_sp_val = 0;
135 struct dwarf_loc up_fp_loc = DWARF_NULL_LOC;
136 struct dwarf_loc up_ra_loc = DWARF_NULL_LOC;
137
138 ret = dwarf_get (&c->dwarf, c->dwarf.loc[SP_REG], ¤t_sp_val);
139 if (ret < 0)
140 {
141 Debug (2, "returning %d [SP=0x%lx]\n", ret,
142 DWARF_GET_LOC (c->dwarf.loc[FP_REG]));
143 return ret;
144 }
145 ret = dwarf_get (&c->dwarf, c->dwarf.loc[FP_REG], ¤t_fp_val);
146 if (ret < 0)
147 {
148 Debug (2, "returning %d [FP=0x%lx]\n", ret,
149 DWARF_GET_LOC (c->dwarf.loc[FP_REG]));
150 return ret;
151 }
152 ret = dwarf_get (&c->dwarf, c->dwarf.loc[RA_REG], ¤t_ra_val);
153 if (ret < 0)
154 {
155 Debug (2, "returning %d [RA=0x%lx]\n", ret,
156 DWARF_GET_LOC (c->dwarf.loc[RA_REG]));
157 return ret;
158 }
159
160 Debug(2, "BEGIN GUESSING WITH SP:%p FP:%p CFA:%p at %p, RA:%p\n",
161 current_sp_val, current_fp_val, c->dwarf.cfa,
162 c->dwarf.ip, current_ra_val
163 );
164
165 if (current_fp_val == current_sp_val) {
166 // Don't adjust FP
167 up_fp_loc = c->dwarf.loc[FP_REG];
168 up_ra_loc = c->dwarf.loc[RA_REG];
169 } else if (is_valid_fp_val(c->dwarf.cfa, current_fp_val)) {
170 /* Heuristic to determine incorrect guess. For FP to be a
171 valid frame it needs to be above current CFA, but don't
172 let it go more than a little. Note that we can't deduce
173 anything about new FP (fp1) since it may not be a frame
174 pointer in the frame above. Just check we get the value. */
175 up_fp_loc = DWARF_MEM_LOC (c, current_fp_val+16);
176 up_ra_loc = DWARF_MEM_LOC (c, current_fp_val+24);
177 unw_word_t up_fp_val = 0;
178 ret = dwarf_get (&c->dwarf, up_fp_loc, &up_fp_val);
179 if (ret > 0 && is_valid_fp_val(current_fp_val, up_fp_val)) {
180 c->dwarf.loc[FP_REG] = up_fp_loc;
181 }
182 }
183
184 if (DWARF_IS_NULL_LOC (up_fp_loc))
185 {
186 ret = 0;
187 Debug (2, "NULL %%fp loc, returning %d\n", ret);
188 return ret;
189 }
190
191 c->dwarf.loc[UNW_MIPS_PC] = c->dwarf.loc[RA_REG];
192 c->dwarf.loc[RA_REG] = up_ra_loc;
193 c->dwarf.loc[SP_REG] = up_fp_loc;
194 c->dwarf.loc[FP_REG] = up_fp_loc;
195 c->dwarf.use_prev_instr = 1;
196
197 if (c->dwarf.ip == current_ra_val && current_fp_val == current_sp_val) {
198 // Backtrace stopped: frame did not save the PC
199 c->dwarf.ip = 0;
200 } else {
201 c->dwarf.ip = current_ra_val;
202 }
203 return (c->dwarf.ip == 0) ? 0 : 1;
204 }
205
206 int
unw_step(unw_cursor_t * cursor)207 unw_step (unw_cursor_t *cursor)
208 {
209 struct cursor *c = (struct cursor *) cursor;
210 int ret;
211
212 ret = mips_handle_signal_frame (cursor);
213 if (ret < 0)
214 /* Not a signal frame, try DWARF-based unwinding. */
215 ret = dwarf_step (&c->dwarf);
216
217 if (unlikely (ret == -UNW_ESTOPUNWIND))
218 return ret;
219
220 if (unlikely (ret < 0))
221 {
222 #if _MIPS_SIM == _ABI64
223 return _step_n64(c);
224 #else
225 return ret;
226 #endif
227 }
228
229 return (c->dwarf.ip == 0) ? 0 : 1;
230 }
231