• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3 
4 This file is part of libunwind.
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24 
25 #ifndef ARM_LIBUNWIND_I_H
26 #define ARM_LIBUNWIND_I_H
27 
28 /* Target-dependent definitions that are internal to libunwind but need
29    to be shared with target-independent code.  */
30 
31 #include <stdlib.h>
32 #include <libunwind.h>
33 #include <stdatomic.h>
34 
35 #include "elf32.h"
36 /* Add For Cache MAP And ELF */
37 #include "map_info.h"
38 /* Add For Cache MAP And ELF */
39 #include "mempool.h"
40 #include "dwarf.h"
41 #include "ex_tables.h"
42 
43 typedef enum
44   {
45     UNW_ARM_FRAME_SYSCALL = -3,      /* r7 saved in r12, sp offset zero */
46     UNW_ARM_FRAME_STANDARD = -2,     /* regular r7, sp +/- offset */
47     UNW_ARM_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
48     UNW_ARM_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
49     UNW_ARM_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
50   }
51 unw_tdep_frame_type_t;
52 
53 typedef struct
54   {
55     uint32_t virtual_address;
56     int32_t frame_type     : 3;  /* unw_tdep_frame_type_t classification */
57     int32_t last_frame     : 1;  /* non-zero if last frame in chain */
58     int32_t cfa_reg_sp     : 1;  /* cfa dwarf base register is sp vs. r7 */
59     int32_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
60     int32_t r7_cfa_offset  : 30; /* r7 saved at this offset from cfa (-1 = not saved) */
61     int32_t lr_cfa_offset  : 30; /* lr saved at this offset from cfa (-1 = not saved) */
62     int32_t sp_cfa_offset  : 30; /* sp saved at this offset from cfa (-1 = not saved) */
63   }
64 unw_tdep_frame_t;
65 
66 struct unw_addr_space
67   {
68     struct unw_accessors acc;
69     int big_endian;
70     unw_caching_policy_t caching_policy;
71     _Atomic uint32_t cache_generation;
72     unw_word_t dyn_generation;          /* see dyn-common.h */
73     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
74     struct dwarf_rs_cache global_cache;
75     struct unw_debug_frame_list *debug_frames;
76     /* Add For Cache MAP And ELF */
77     struct map_info *map_list;
78     /* Add For Cache MAP And ELF */
79     struct cursor *cursor;
80     int pid;
81   };
82 
83 static inline struct cursor *
get_cursor_from_as(unw_addr_space_t as)84 get_cursor_from_as(unw_addr_space_t as)
85 {
86   if (as->cursor) {
87     return (struct cursor *)(as->cursor);
88   }
89 
90   return NULL;
91 }
92 
93 struct cursor
94   {
95     struct dwarf_cursor dwarf;          /* must be first */
96 
97     unw_tdep_frame_t frame_info;        /* quick tracing assist info */
98 
99     enum
100       {
101         ARM_SCF_NONE,                   /* no signal frame */
102         ARM_SCF_LINUX_SIGFRAME,         /* non-RT signal frame, kernel >=2.6.18 */
103         ARM_SCF_LINUX_RT_SIGFRAME,      /* RT signal frame, kernel >=2.6.18 */
104         ARM_SCF_LINUX_OLD_SIGFRAME,     /* non-RT signal frame, kernel < 2.6.18 */
105         ARM_SCF_LINUX_OLD_RT_SIGFRAME,  /* RT signal frame, kernel < 2.6.18 */
106         ARM_SCF_FREEBSD_SIGFRAME,	/* FreeBSD sigframe */
107 	ARM_SCF_FREEBSD_SYSCALL,	/* FreeBSD syscall stub */
108       }
109     sigcontext_format;
110     unw_word_t sigcontext_addr;
111     unw_word_t sigcontext_sp;
112     unw_word_t sigcontext_pc;
113     int validate;
114   };
115 
116 #define DWARF_GET_LOC(l)        ((l).val)
117 
118 #ifdef UNW_LOCAL_ONLY
119 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
120 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
121 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
122 # define DWARF_IS_REG_LOC(l)    0
123 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
124                                  tdep_uc_addr((c)->as_arg, (r)), 0))
125 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
126 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
127                                  tdep_uc_addr((c)->as_arg, (r)), 0))
128 
129 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)130 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
131 {
132   if (!DWARF_GET_LOC (loc))
133     return -1;
134   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
135   return 0;
136 }
137 
138 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)139 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
140 {
141   if (!DWARF_GET_LOC (loc))
142     return -1;
143   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
144   return 0;
145 }
146 
147 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)148 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
149 {
150   if (!DWARF_GET_LOC (loc))
151     return -1;
152   *val = *(unw_word_t *) DWARF_GET_LOC (loc);
153   return 0;
154 }
155 
156 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)157 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
158 {
159   if (!DWARF_GET_LOC (loc))
160     return -1;
161   *(unw_word_t *) DWARF_GET_LOC (loc) = val;
162   return 0;
163 }
164 
165 #else /* !UNW_LOCAL_ONLY */
166 # define DWARF_LOC_TYPE_FP      (1 << 0)
167 # define DWARF_LOC_TYPE_REG     (1 << 1)
168 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
169 
170 static inline int
dwarf_is_null_loc(dwarf_loc_t l)171 dwarf_is_null_loc(dwarf_loc_t l)
172 {
173   return l.val == 0 && l.type == 0;
174 }
175 
176 # define DWARF_IS_NULL_LOC(l)   dwarf_is_null_loc(l)
177 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
178 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
179 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
180 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
181 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
182 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
183                                                 | DWARF_LOC_TYPE_FP))
184 
185 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)186 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
187 {
188   char *valp = (char *) &val;
189   unw_word_t addr;
190   int ret;
191 
192   if (DWARF_IS_NULL_LOC (loc))
193     return -UNW_EBADREG;
194 
195   if (DWARF_IS_REG_LOC (loc))
196     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
197                                        val, 0, c->as_arg);
198 
199   addr = DWARF_GET_LOC (loc);
200   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
201                                        0, c->as_arg)) < 0)
202     return ret;
203 
204   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
205                                    c->as_arg);
206 }
207 
208 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)209 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
210 {
211   char *valp = (char *) &val;
212   unw_word_t addr;
213   int ret;
214 
215   if (DWARF_IS_NULL_LOC (loc))
216     return -UNW_EBADREG;
217 
218   if (DWARF_IS_REG_LOC (loc))
219     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
220                                        &val, 1, c->as_arg);
221 
222   addr = DWARF_GET_LOC (loc);
223   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
224                                        1, c->as_arg)) < 0)
225     return ret;
226 
227   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
228                                    1, c->as_arg);
229 }
230 
231 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)232 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
233 {
234   int reg_num;
235   if (DWARF_IS_NULL_LOC (loc))
236     return -UNW_EBADREG;
237 
238   /* If a code-generator were to save a value of type unw_word_t in a
239      floating-point register, we would have to support this case.  I
240      suppose it could happen with MMX registers, but does it really
241      happen?  */
242   assert (!DWARF_IS_FP_LOC (loc));
243 
244   if (DWARF_IS_REG_LOC (loc)) {
245     reg_num = DWARF_GET_LOC (loc);
246     if (reg_num >= 0 && reg_num < c->reg_sz) {
247       *val = c->ctx[reg_num];
248       return 1;
249     } else {
250       return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
251                                       0, c->as_arg);
252     }
253   }
254   else
255     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
256                                      0, c->as_arg);
257 }
258 
259 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)260 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
261 {
262   if (DWARF_IS_NULL_LOC (loc))
263     return -UNW_EBADREG;
264 
265   /* If a code-generator were to save a value of type unw_word_t in a
266      floating-point register, we would have to support this case.  I
267      suppose it could happen with MMX registers, but does it really
268      happen?  */
269   assert (!DWARF_IS_FP_LOC (loc));
270 
271   if (DWARF_IS_REG_LOC (loc))
272     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
273                                      1, c->as_arg);
274   else
275     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
276                                      1, c->as_arg);
277 }
278 
279 #endif /* !UNW_LOCAL_ONLY */
280 
281 #define tdep_getcontext_trace           unw_getcontext
282 #define tdep_init_done                  UNW_OBJ(init_done)
283 #define tdep_init                       UNW_OBJ(init)
284 #define arm_find_proc_info              UNW_OBJ(find_proc_info)
285 #define arm_find_proc_info2             UNW_OBJ(find_proc_info2)
286 #define arm_put_unwind_info             UNW_OBJ(put_unwind_info)
287 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
288    tdep_search_unwind_table.  */
289 #define tdep_search_unwind_table        UNW_OBJ(search_unwind_table)
290 #define tdep_find_unwind_table          dwarf_find_unwind_table
291 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
292 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
293 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
294 #define tdep_access_reg                 UNW_OBJ(access_reg)
295 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
296 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
297 #define tdep_cache_frame(c)             0
298 #define tdep_reuse_frame(c,frame)       do {} while(0)
299 #define tdep_stash_frame                UNW_OBJ(tdep_stash_frame)
300 #define tdep_trace                      UNW_OBJ(tdep_trace)
301 
302 #ifdef UNW_LOCAL_ONLY
303 # define tdep_find_proc_info(c,ip,n)                            \
304         arm_find_proc_info((c)->as, (ip), &(c)->pi, (n),        \
305                                        (c)->as_arg)
306 # define tdep_put_unwind_info(as,pi,arg)                \
307         arm_put_unwind_info((as), (pi), (arg))
308 #else
309 # define tdep_find_proc_info(c,ip,n)                                    \
310         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
311                                        (c)->as_arg)
312 # define tdep_put_unwind_info(as,pi,arg)                \
313         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
314 #endif
315 
316 #define tdep_get_as(c)                  ((c)->dwarf.as)
317 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
318 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
319 #define tdep_big_endian(as)             ((as)->big_endian)
320 
321 extern atomic_bool tdep_init_done;
322 
323 extern void tdep_init (void);
324 extern int arm_find_proc_info (unw_addr_space_t as, unw_word_t ip,
325                                unw_proc_info_t *pi, int need_unwind_info,
326                                void *arg);
327 extern int arm_find_proc_info2 (unw_addr_space_t as, unw_word_t ip,
328                                 unw_proc_info_t *pi, int need_unwind_info,
329                                 void *arg, int methods);
330 extern void arm_put_unwind_info (unw_addr_space_t as,
331                                   unw_proc_info_t *pi, void *arg);
332 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
333                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
334                                      int need_unwind_info, void *arg);
335 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
336 /* Add For Cache MAP And ELF */
337 extern struct map_info *tdep_get_elf_image (unw_addr_space_t as, pid_t pid,
338 					    unw_word_t ip);
339 /* Add For Cache MAP And ELF */
340 extern void tdep_get_exe_image_path (char *path);
341 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
342                             unw_word_t *valp, int write);
343 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
344                               unw_fpreg_t *valp, int write);
345 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
346 extern void tdep_stash_frame (struct dwarf_cursor *c,
347                               struct dwarf_reg_state *rs);
348 
349 /* unwinding method selection support */
350 #define UNW_ARM_METHOD_ALL          0xFF
351 #define UNW_ARM_METHOD_DWARF        0x01
352 #define UNW_ARM_METHOD_FRAME        0x02
353 #define UNW_ARM_METHOD_EXIDX        0x04
354 #define UNW_ARM_METHOD_LR           0x08
355 
356 #define unwi_unwind_method   UNW_OBJ(unwind_method)
357 extern int unwi_unwind_method;
358 
359 #define UNW_TRY_METHOD(x)   (unwi_unwind_method & x)
360 
361 #endif /* ARM_LIBUNWIND_I_H */
362