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 "unwind_i.h"
29 #include "dwarf_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 (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%lx 0x%lx, ret = %d\n", c->ip, w0, w1, ret);
53   return ret;
54 }
55 
56 PROTECTED 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 = c->validate;
64   c->validate = 1;
65 #endif
66 
67   Debug (1, "(cursor=%p, ip=0x%lx, cfa=0x%lx)\n",
68          c, c->dwarf.ip, c->dwarf.cfa);
69 
70   /* Try DWARF-based unwinding... */
71   c->sigcontext_format = X86_64_SCF_NONE;
72   ret = dwarf_step (&c->dwarf);
73 
74 #if CONSERVATIVE_CHECKS
75   c->validate = val;
76 #endif
77 
78   if (ret < 0 && ret != -UNW_ENOINFO)
79     {
80       Debug (2, "returning %d\n", ret);
81       return ret;
82     }
83 
84   if (likely (ret >= 0))
85     {
86       /* x86_64 ABI specifies that end of call-chain is marked with a
87          NULL RBP or undefined return address  */
88       if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP])
89           || DWARF_IS_NULL_LOC(c->dwarf.loc[c->dwarf.ret_addr_column]))
90         {
91           c->dwarf.ip = 0;
92           ret = 0;
93         }
94     }
95   else // ret == -UNW_ENOINFO
96     {
97       /* DWARF failed.  There isn't much of a usable frame-chain on x86-64,
98          but we do need to handle two special-cases:
99 
100           (i) signal trampoline: Old kernels and older libcs don't
101               export the vDSO needed to get proper unwind info for the
102               trampoline.  Recognize that case by looking at the code
103               and filling in things by hand.
104 
105           (ii) PLT (shared-library) call-stubs: PLT stubs are invoked
106               via CALLQ.  Try this for all non-signal trampoline
107               code.  */
108 
109       unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
110       struct dwarf_loc rbp_loc, rsp_loc, rip_loc;
111 
112       /* We could get here because of missing/bad unwind information.
113          Validate all addresses from now on before dereferencing. */
114       c->validate = 1;
115 
116       Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
117 
118       if (unw_is_signal_frame (cursor))
119         {
120           ret = unw_handle_signal_frame(cursor);
121           if (ret < 0)
122             {
123               Debug (2, "returning 0\n");
124               return 0;
125             }
126         }
127       else if (is_plt_entry (&c->dwarf))
128         {
129           /* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
130           Debug (2, "found plt entry\n");
131           c->frame_info.cfa_reg_offset = 8;
132           c->frame_info.cfa_reg_sp = -1;
133           c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
134           c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
135           c->dwarf.cfa += 8;
136         }
137       else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
138         {
139           for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
140             c->dwarf.loc[i] = DWARF_NULL_LOC;
141         }
142       else
143         {
144           unw_word_t rbp;
145 
146           ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
147           if (ret < 0)
148             {
149               Debug (2, "returning %d [RBP=0x%lx]\n", ret,
150                      DWARF_GET_LOC (c->dwarf.loc[RBP]));
151               return ret;
152             }
153 
154           if (!rbp)
155             {
156               /* Looks like we may have reached the end of the call-chain.  */
157               rbp_loc = DWARF_NULL_LOC;
158               rsp_loc = DWARF_NULL_LOC;
159               rip_loc = DWARF_NULL_LOC;
160             }
161           else
162             {
163               unw_word_t rbp1 = 0;
164               rbp_loc = DWARF_LOC(rbp, 0);
165               rsp_loc = DWARF_NULL_LOC;
166               rip_loc = DWARF_LOC (rbp + 8, 0);
167               ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
168               Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
169                      (unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
170                      rbp, c->dwarf.cfa, rbp1);
171 
172               /* Heuristic to determine incorrect guess.  For RBP to be a
173                  valid frame it needs to be above current CFA, but don't
174                  let it go more than a little.  Note that we can't deduce
175                  anything about new RBP (rbp1) since it may not be a frame
176                  pointer in the frame above.  Just check we get the value. */
177               if (ret < 0
178                   || rbp < c->dwarf.cfa
179                   || (rbp - c->dwarf.cfa) > 0x4000)
180                 {
181                   rip_loc = DWARF_NULL_LOC;
182                   rbp_loc = DWARF_NULL_LOC;
183                 }
184 
185               c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
186               c->frame_info.cfa_reg_sp = 0;
187               c->frame_info.cfa_reg_offset = 16;
188               c->frame_info.fp_cfa_offset = -16;
189               c->dwarf.cfa += 16;
190             }
191 
192           /* Mark all registers unsaved */
193           for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
194             c->dwarf.loc[i] = DWARF_NULL_LOC;
195 
196           c->dwarf.loc[RBP] = rbp_loc;
197           c->dwarf.loc[RSP] = rsp_loc;
198           c->dwarf.loc[RIP] = rip_loc;
199           c->dwarf.use_prev_instr = 1;
200         }
201 
202       c->dwarf.ret_addr_column = RIP;
203 
204       if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
205         {
206           ret = 0;
207           Debug (2, "NULL %%rbp loc, returning %d\n", ret);
208           return ret;
209         }
210       if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
211         {
212           ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
213           Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
214                      (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
215                      (unsigned long long) c->dwarf.ip);
216           if (ret < 0)
217             {
218               Debug (2, "returning %d\n", ret);
219               return ret;
220             }
221           ret = 1;
222         }
223       else
224         {
225           c->dwarf.ip = 0;
226           ret = 0;
227         }
228 
229       if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
230         {
231           ret = -UNW_EBADFRAME;
232           Debug (2, "returning %d\n", ret);
233           return ret;
234         }
235     }
236 
237   Debug (2, "returning %d\n", ret);
238   return ret;
239 }
240