1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6
7 This file is part of libunwind.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27
28 #include "libunwind_i.h"
29 #include "unwind_i.h"
30 #include <signal.h>
31
32 /* Recognise PLT entries such as:
33 3bdf0: ff 25 e2 49 13 00 jmpq *0x1349e2(%rip)
34 3bdf6: 68 ae 03 00 00 pushq $0x3ae
35 3bdfb: e9 00 c5 ff ff jmpq 38300 <_init+0x18> */
36 static int
is_plt_entry(struct dwarf_cursor * c)37 is_plt_entry (struct dwarf_cursor *c)
38 {
39 unw_word_t w0, w1;
40 unw_accessors_t *a;
41 int ret;
42
43 a = unw_get_accessors_int (c->as);
44 if ((ret = (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg)) < 0
45 || (ret = (*a->access_mem) (c->as, c->ip + 8, &w1, 0, c->as_arg)) < 0)
46 return 0;
47
48 ret = (((w0 & 0xffff) == 0x25ff)
49 && (((w0 >> 48) & 0xff) == 0x68)
50 && (((w1 >> 24) & 0xff) == 0xe9));
51
52 Debug (14, "ip=0x%lx => 0x%016lx 0x%016lx, ret = %d\n", c->ip, w0, w1, ret);
53 return ret;
54 }
55
56 int
unw_step(unw_cursor_t * cursor)57 unw_step (unw_cursor_t *cursor)
58 {
59 struct cursor *c = (struct cursor *) cursor;
60 int ret, i;
61
62 #if CONSERVATIVE_CHECKS
63 int val = 0;
64 if (c->dwarf.as == unw_local_addr_space) {
65 val = dwarf_get_validate(&c->dwarf);
66 dwarf_set_validate(&c->dwarf, 1);
67 }
68 #endif
69
70 Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
71 c, c->dwarf.ip, c->dwarf.cfa);
72
73 /* Try DWARF-based unwinding... */
74 c->sigcontext_format = X86_64_SCF_NONE;
75 ret = dwarf_step (&c->dwarf);
76
77 #if CONSERVATIVE_CHECKS
78 if (c->dwarf.as == unw_local_addr_space) {
79 dwarf_set_validate(&c->dwarf, val);
80 }
81 #endif
82
83 if (ret < 0 && ret != -UNW_ENOINFO)
84 {
85 Debug (2, "returning %d\n", ret);
86 return ret;
87 }
88
89 if (likely (ret >= 0))
90 {
91 /* x86_64 ABI specifies that end of call-chain is marked with a
92 NULL RBP or undefined return address */
93 if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
94 {
95 c->dwarf.ip = 0;
96 ret = 0;
97 }
98 }
99 else
100 {
101 /* DWARF failed. There isn't much of a usable frame-chain on x86-64,
102 but we do need to handle two special-cases:
103
104 (i) signal trampoline: Old kernels and older libcs don't
105 export the vDSO needed to get proper unwind info for the
106 trampoline. Recognize that case by looking at the code
107 and filling in things by hand.
108
109 (ii) PLT (shared-library) call-stubs: PLT stubs are invoked
110 via CALLQ. Try this for all non-signal trampoline
111 code. */
112
113 unw_word_t invalid_prev_rip = 0;
114 unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
115 struct dwarf_loc rbp_loc = DWARF_NULL_LOC, rsp_loc = DWARF_NULL_LOC, rip_loc = DWARF_NULL_LOC;
116
117 /* We could get here because of missing/bad unwind information.
118 Validate all addresses before dereferencing. */
119 if (c->dwarf.as == unw_local_addr_space) {
120 dwarf_set_validate(&c->dwarf, 1);
121 }
122
123 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
124
125 if (unw_is_signal_frame (cursor) > 0)
126 {
127 ret = x86_64_handle_signal_frame(cursor);
128 if (ret < 0)
129 {
130 Debug (2, "returning 0\n");
131 return 0;
132 }
133 }
134 else if (is_plt_entry (&c->dwarf))
135 {
136 /* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
137 Debug (2, "found plt entry\n");
138 c->frame_info.cfa_reg_offset = 8;
139 c->frame_info.cfa_reg_rsp = -1;
140 c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
141 c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
142 c->dwarf.cfa += 8;
143 }
144 else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
145 {
146 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
147 c->dwarf.loc[i] = DWARF_NULL_LOC;
148 }
149 else
150 {
151 unw_word_t rbp;
152
153 ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
154 if (ret < 0)
155 {
156 Debug (2, "returning %d [RBP=0x%lx]\n", ret,
157 DWARF_GET_LOC (c->dwarf.loc[RBP]));
158 return ret;
159 }
160
161 unw_word_t not_used;
162 invalid_prev_rip = dwarf_get(&c->dwarf, DWARF_MEM_LOC(c->dwarf, prev_ip), ¬_used);
163
164 if (!rbp && invalid_prev_rip == 0)
165 {
166 /* Looks like we may have reached the end of the call-chain. */
167 rbp_loc = DWARF_NULL_LOC;
168 rsp_loc = DWARF_NULL_LOC;
169 rip_loc = DWARF_NULL_LOC;
170 }
171 else
172 {
173 /*
174 * Check if previous RIP was invalid
175 * This could happen if a bad function pointer was
176 * followed and so the stack wasn't updated by the
177 * preamble
178 */
179 int rip_fixup_success = 0;
180 if (invalid_prev_rip != 0)
181 {
182 Debug (2, "Previous RIP 0x%lx was invalid, attempting fixup\n", prev_ip);
183 unw_word_t rsp;
184 ret = dwarf_get (&c->dwarf, c->dwarf.loc[RSP], &rsp);
185
186 /*Test to see if what we think is the previous RIP is valid*/
187 unw_word_t new_ip = 0;
188 if (dwarf_get(&c->dwarf, DWARF_MEM_LOC(c->dwarf, rsp), &new_ip) == 0)
189 {
190 Debug (2, "RSP 0x%lx looks valid\n", rsp);
191 if ((ret = dwarf_get(&c->dwarf, DWARF_MEM_LOC(c->dwarf, new_ip), ¬_used)) == 0)
192 {
193 Debug (2, "new_ip 0x%lx looks valid\n", new_ip);
194 rip_fixup_success = 1;
195 c->frame_info.cfa_reg_offset = 8;
196 c->frame_info.cfa_reg_rsp = 1;
197 c->frame_info.rbp_cfa_offset = -1;
198 c->frame_info.rsp_cfa_offset = -1;
199 c->frame_info.frame_type = UNW_X86_64_FRAME_OTHER;
200 /*
201 * The call should have pushed RIP to the stack
202 * and since there was no preamble RSP hasn't been
203 * touched so RIP should be at RSP.
204 */
205 c->dwarf.cfa += 8;
206 /* Optimised x64 binaries don't use RBP it seems? */
207 rbp_loc = DWARF_LOC (rbp, 0);
208 rsp_loc = DWARF_LOC (rsp, 0);
209 rip_loc = DWARF_LOC (rsp, 0);
210 }
211 else
212 Debug (2, "new_ip 0x%lx dwarf_get(&c->dwarf, DWARF_MEM_LOC(c->dwarf, new_ip), ¬_used) != 0\n", new_ip);
213 }
214 else
215 Debug (2, "rsp 0x%lx dwarf_get(&c->dwarf, DWARF_MEM_LOC(c->dwarf, rsp), &new_ip) != 0\n", rsp);
216 }
217 /*
218 * If the previous rip we found on the stack didn't look valid fall back
219 * to the previous method for finding a valid stack frame
220 */
221 if (!rip_fixup_success)
222 {
223 Debug (2, "RIP fixup didn't work, falling back\n");
224 unw_word_t rbp1 = 0;
225 rbp_loc = DWARF_LOC(rbp, 0);
226 rsp_loc = DWARF_VAL_LOC(c, rbp + 16);
227 rip_loc = DWARF_LOC (rbp + 8, 0);
228 ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
229 Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
230 (unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
231 rbp, c->dwarf.cfa, rbp1);
232
233 /* Heuristic to determine incorrect guess. For RBP to be a
234 valid frame it needs to be above current CFA, but don't
235 let it go more than a little. Note that we can't deduce
236 anything about new RBP (rbp1) since it may not be a frame
237 pointer in the frame above. Just check we get the value. */
238 if (ret < 0
239 || rbp < c->dwarf.cfa
240 || (rbp - c->dwarf.cfa) > 0x4000)
241 {
242 rip_loc = DWARF_NULL_LOC;
243 rbp_loc = DWARF_NULL_LOC;
244 }
245
246 c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
247 c->frame_info.cfa_reg_rsp = 0;
248 c->frame_info.cfa_reg_offset = 16;
249 c->frame_info.rbp_cfa_offset = -16;
250 c->dwarf.cfa += 16;
251
252 }
253 }
254 /* Mark all registers unsaved */
255 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
256 c->dwarf.loc[i] = DWARF_NULL_LOC;
257
258 c->dwarf.loc[RBP] = rbp_loc;
259 c->dwarf.loc[RSP] = rsp_loc;
260 c->dwarf.loc[RIP] = rip_loc;
261 c->dwarf.use_prev_instr = 1;
262 }
263
264 if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]) && invalid_prev_rip == 0)
265 {
266 ret = 0;
267 Debug (2, "NULL %%rbp loc, returning %d\n", ret);
268 return ret;
269 }
270 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
271 {
272 ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
273 Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
274 (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
275 (unsigned long long) c->dwarf.ip);
276 if (ret < 0)
277 {
278 Debug (2, "returning %d\n", ret);
279 return ret;
280 }
281 #if __sun
282 if (c->dwarf.ip == 0)
283 {
284 Debug (2, "returning 0\n");
285 return ret;
286 }
287 #endif
288 ret = 1;
289 }
290 else
291 c->dwarf.ip = 0;
292
293 if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
294 return -UNW_EBADFRAME;
295 }
296 Debug (2, "returning %d\n", ret);
297 return ret;
298 }
299