1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3    Copyright (C) 2011-2013 Linaro Limited
4    Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
5 
6 This file is part of libunwind.
7 
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15 
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26 
27 #include "unwind_i.h"
28 #include "dwarf_i.h"
29 
30 PROTECTED int
unw_step(unw_cursor_t * cursor)31 unw_step (unw_cursor_t *cursor)
32 {
33   struct cursor *c = (struct cursor *) cursor;
34   int ret;
35 
36   Debug (1, "(cursor=%p, ip=0x%lx, cfa=0x%lx))\n",
37          c, c->dwarf.ip, c->dwarf.cfa);
38 
39   /* Check if this is a signal frame. */
40   ret = unw_is_signal_frame (cursor);
41   if (ret < 0)
42     {
43       Debug (2, "returning %d\n", ret);
44       return ret;
45     }
46   if (ret)
47     {
48       ret = unw_handle_signal_frame (cursor);
49       Debug (2, "returning %d\n", ret);
50       return ret;
51     }
52 
53   ret = dwarf_step (&c->dwarf);
54   Debug(1, "dwarf_step()=%d\n", ret);
55 
56   if (unlikely (ret == -UNW_ESTOPUNWIND))
57     {
58       Debug (2, "returning %d\n", ret);
59       return ret;
60     }
61 
62   if (ret < 0 && ret != -UNW_ENOINFO)
63     {
64       Debug (2, "returning %d\n", 0);
65       return 0;
66     }
67 
68   if (ret >= 0)
69     {
70       ret = (c->dwarf.ip == 0) ? 0 : 1;
71     }
72   else // ret == -UNW_ENOINFO
73     {
74       // If there's no unwind info fall back to a heuristic.
75       // TODO: Make configurable? unw_step_etc?
76       // Note: This is copied from x86_64.
77       // TODO: arm64 has a canonical frame pointer: r29. But it's not clear
78       // yet whether the heuristics that x86_64/Gstep.c employees can just be
79       // carried over.
80 
81       unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
82       struct dwarf_loc sp_loc, pc_loc;
83 
84       // We could get here because of missing/bad unwind information.
85       // Validate all addresses from now on before dereferencing.
86       c->validate = 1;
87 
88       Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
89 
90       if (DWARF_IS_NULL_LOC (c->dwarf.loc[UNW_AARCH64_SP]))
91         {
92           for (int i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
93             c->dwarf.loc[i] = DWARF_NULL_LOC;
94         }
95       else
96         {
97           unw_word_t sp;
98 
99           ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_AARCH64_SP], &sp);
100           if (ret < 0)
101             {
102               Debug (2, "returning %d [SP=0x%lx]\n", ret,
103                      DWARF_GET_LOC (c->dwarf.loc[UNW_AARCH64_SP]));
104               return ret;
105             }
106 
107           if (!sp)
108             {
109               /* Looks like we may have reached the end of the call-chain.  */
110               sp_loc = DWARF_NULL_LOC;
111               pc_loc = DWARF_NULL_LOC;
112             }
113           else
114             {
115               unw_word_t sp1 = 0;
116               sp_loc = DWARF_LOC (sp, 0);
117               pc_loc = DWARF_LOC (sp + 8, 0);
118               ret = dwarf_get (&c->dwarf, sp_loc, &sp1);
119               Debug (1, "[SP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
120                      (unsigned long) DWARF_GET_LOC (c->dwarf.loc[UNW_AARCH64_SP]),
121                      sp, c->dwarf.cfa, sp1);
122 
123 #if 0 // TODO: wip
124               /* Heuristic to determine incorrect guess.  For SP to be a
125                  valid frame it needs to be above current CFA, but don't
126                  let it go more than a little.  Note that we can't deduce
127                  anything about new SP (sp1) since it may not be a frame
128                  pointer in the frame above.  Just check we get the value. */
129               if (ret < 0
130                   || rbp < c->dwarf.cfa
131                   || (rbp - c->dwarf.cfa) > 0x4000)
132                 {
133                   pc_loc = DWARF_NULL_LOC;
134                   sp_loc = DWARF_NULL_LOC;
135                 }
136 #endif
137 
138               c->frame_info.frame_type = UNW_AARCH64_FRAME_GUESSED;
139               c->frame_info.cfa_reg_sp = 0;
140               c->frame_info.cfa_reg_offset = 16;
141               c->frame_info.sp_cfa_offset = -16;
142               c->dwarf.cfa += 16;
143             }
144 
145           /* Mark all registers unsaved */
146           for (int i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
147             c->dwarf.loc[i] = DWARF_NULL_LOC;
148 
149           c->dwarf.loc[UNW_AARCH64_SP] = sp_loc;
150           c->dwarf.loc[UNW_AARCH64_PC] = pc_loc;
151           c->dwarf.use_prev_instr = 1;
152         }
153 
154       c->dwarf.ret_addr_column = UNW_AARCH64_PC;
155 
156       if (DWARF_IS_NULL_LOC (c->dwarf.loc[UNW_AARCH64_SP]))
157         {
158           ret = 0;
159           Debug (2, "NULL %%sp loc, returning %d\n", ret);
160           return ret;
161         }
162       if (!DWARF_IS_NULL_LOC (c->dwarf.loc[UNW_AARCH64_PC]))
163         {
164           ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_AARCH64_PC], &c->dwarf.ip);
165           Debug (1, "Frame Chain [PC=0x%Lx] = 0x%Lx\n",
166                      (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[UNW_AARCH64_PC]),
167                      (unsigned long long) c->dwarf.ip);
168           if (ret < 0)
169             {
170               Debug (2, "returning %d\n", ret);
171               return ret;
172             }
173           ret = 1;
174         }
175       else
176         {
177           c->dwarf.ip = 0;
178           ret = 0;
179         }
180 
181       if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
182         {
183           ret = -UNW_EBADFRAME;
184           Debug (2, "returning %d\n", ret);
185           return ret;
186         }
187     }
188 
189   Debug (2, "returning %d\n", ret);
190   return ret;
191 }
192