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