2602f41c4f79fc94224772a9931c5dd429cdfe1d
[platform/upstream/libunwind.git] / include / tdep-arm / libunwind_i.h
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
34 #include "elf32.h"
35 #include "mempool.h"
36 #include "dwarf.h"
37 #include "ex_tables.h"
38
39 typedef enum
40   {
41     UNW_ARM_FRAME_SYSCALL = -3,      /* r7 saved in r12, sp offset zero */
42     UNW_ARM_FRAME_STANDARD = -2,     /* regular r7, sp +/- offset */
43     UNW_ARM_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
44     UNW_ARM_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
45     UNW_ARM_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
46   }
47 unw_tdep_frame_type_t;
48
49 typedef struct
50   {
51     uint32_t virtual_address;
52     int32_t frame_type     : 3;  /* unw_tdep_frame_type_t classification */
53     int32_t last_frame     : 1;  /* non-zero if last frame in chain */
54     int32_t cfa_reg_sp     : 1;  /* cfa dwarf base register is sp vs. r7 */
55     int32_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
56     int32_t r7_cfa_offset  : 30; /* r7 saved at this offset from cfa (-1 = not saved) */
57     int32_t lr_cfa_offset  : 30; /* lr saved at this offset from cfa (-1 = not saved) */
58     int32_t sp_cfa_offset  : 30; /* sp saved at this offset from cfa (-1 = not saved) */
59   }
60 unw_tdep_frame_t;
61
62 struct unw_addr_space
63   {
64     struct unw_accessors acc;
65     int big_endian;
66     unw_caching_policy_t caching_policy;
67 #ifdef HAVE_ATOMIC_OPS_H
68     AO_t cache_generation;
69 #else
70     uint32_t cache_generation;
71 #endif
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   };
77
78 struct cursor
79   {
80     struct dwarf_cursor dwarf;          /* must be first */
81
82     unw_tdep_frame_t frame_info;        /* quick tracing assist info */
83
84     enum
85       {
86         ARM_SCF_NONE,                   /* no signal frame */
87         ARM_SCF_LINUX_SIGFRAME,         /* non-RT signal frame, kernel >=2.6.18 */
88         ARM_SCF_LINUX_RT_SIGFRAME,      /* RT signal frame, kernel >=2.6.18 */
89         ARM_SCF_LINUX_OLD_SIGFRAME,     /* non-RT signal frame, kernel < 2.6.18 */
90         ARM_SCF_LINUX_OLD_RT_SIGFRAME,  /* RT signal frame, kernel < 2.6.18 */
91         ARM_SCF_FREEBSD_SIGFRAME,       /* FreeBSD sigframe */
92         ARM_SCF_FREEBSD_SYSCALL,        /* FreeBSD syscall stub */
93       }
94     sigcontext_format;
95     unw_word_t sigcontext_addr;
96     unw_word_t sigcontext_sp;
97     unw_word_t sigcontext_pc;
98     int validate;
99   };
100
101 #define DWARF_GET_LOC(l)        ((l).val)
102
103 #ifdef UNW_LOCAL_ONLY
104 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
105 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
106 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
107 # define DWARF_IS_REG_LOC(l)    0
108 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
109                                  tdep_uc_addr((c)->as_arg, (r)), 0))
110 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
111 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
112                                  tdep_uc_addr((c)->as_arg, (r)), 0))
113
114 static inline int
115 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
116 {
117   if (!DWARF_GET_LOC (loc))
118     return -1;
119   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
120   return 0;
121 }
122
123 static inline int
124 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
125 {
126   if (!DWARF_GET_LOC (loc))
127     return -1;
128   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
129   return 0;
130 }
131
132 static inline int
133 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
134 {
135   if (!DWARF_GET_LOC (loc))
136     return -1;
137   *val = *(unw_word_t *) DWARF_GET_LOC (loc);
138   return 0;
139 }
140
141 static inline int
142 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
143 {
144   if (!DWARF_GET_LOC (loc))
145     return -1;
146   *(unw_word_t *) DWARF_GET_LOC (loc) = val;
147   return 0;
148 }
149
150 #else /* !UNW_LOCAL_ONLY */
151 # define DWARF_LOC_TYPE_FP      (1 << 0)
152 # define DWARF_LOC_TYPE_REG     (1 << 1)
153 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
154 # define DWARF_IS_NULL_LOC(l)                                           \
155                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
156 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
157 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
158 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
159 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
160 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
161 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
162                                                 | DWARF_LOC_TYPE_FP))
163
164 static inline int
165 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
166 {
167   char *valp = (char *) &val;
168   unw_word_t addr;
169   int ret;
170
171   if (DWARF_IS_NULL_LOC (loc))
172     return -UNW_EBADREG;
173
174   if (DWARF_IS_REG_LOC (loc))
175     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
176                                        val, 0, c->as_arg);
177
178   addr = DWARF_GET_LOC (loc);
179   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
180                                        0, c->as_arg)) < 0)
181     return ret;
182
183   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
184                                    c->as_arg);
185 }
186
187 static inline int
188 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
189 {
190   char *valp = (char *) &val;
191   unw_word_t addr;
192   int ret;
193
194   if (DWARF_IS_NULL_LOC (loc))
195     return -UNW_EBADREG;
196
197   if (DWARF_IS_REG_LOC (loc))
198     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
199                                        &val, 1, c->as_arg);
200
201   addr = DWARF_GET_LOC (loc);
202   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
203                                        1, c->as_arg)) < 0)
204     return ret;
205
206   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
207                                    1, c->as_arg);
208 }
209
210 static inline int
211 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
212 {
213   if (DWARF_IS_NULL_LOC (loc))
214     return -UNW_EBADREG;
215
216   /* If a code-generator were to save a value of type unw_word_t in a
217      floating-point register, we would have to support this case.  I
218      suppose it could happen with MMX registers, but does it really
219      happen?  */
220   assert (!DWARF_IS_FP_LOC (loc));
221
222   if (DWARF_IS_REG_LOC (loc))
223     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
224                                      0, c->as_arg);
225   else
226     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
227                                      0, c->as_arg);
228 }
229
230 static inline int
231 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
232 {
233   if (DWARF_IS_NULL_LOC (loc))
234     return -UNW_EBADREG;
235
236   /* If a code-generator were to save a value of type unw_word_t in a
237      floating-point register, we would have to support this case.  I
238      suppose it could happen with MMX registers, but does it really
239      happen?  */
240   assert (!DWARF_IS_FP_LOC (loc));
241
242   if (DWARF_IS_REG_LOC (loc))
243     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
244                                      1, c->as_arg);
245   else
246     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
247                                      1, c->as_arg);
248 }
249
250 #endif /* !UNW_LOCAL_ONLY */
251
252 #define tdep_getcontext_trace           unw_getcontext
253 #define tdep_init_done                  UNW_OBJ(init_done)
254 #define tdep_init                       UNW_OBJ(init)
255 #define arm_find_proc_info              UNW_OBJ(find_proc_info)
256 #define arm_put_unwind_info             UNW_OBJ(put_unwind_info)
257 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
258    tdep_search_unwind_table.  */
259 #define tdep_search_unwind_table        UNW_OBJ(search_unwind_table)
260 #define tdep_find_unwind_table          dwarf_find_unwind_table
261 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
262 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
263 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
264 #define tdep_access_reg                 UNW_OBJ(access_reg)
265 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
266 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
267 #define tdep_cache_frame(c)             0
268 #define tdep_reuse_frame(c,frame)       do {} while(0)
269 #define tdep_stash_frame                UNW_OBJ(tdep_stash_frame)
270 #define tdep_trace                      UNW_OBJ(tdep_trace)
271
272 #ifdef UNW_LOCAL_ONLY
273 # define tdep_find_proc_info(c,ip,n)                            \
274         arm_find_proc_info((c)->as, (ip), &(c)->pi, (n),        \
275                                        (c)->as_arg)
276 # define tdep_put_unwind_info(as,pi,arg)                \
277         arm_put_unwind_info((as), (pi), (arg))
278 #else
279 # define tdep_find_proc_info(c,ip,n)                                    \
280         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
281                                        (c)->as_arg)
282 # define tdep_put_unwind_info(as,pi,arg)                \
283         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
284 #endif
285
286 #define tdep_get_as(c)                  ((c)->dwarf.as)
287 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
288 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
289 #define tdep_big_endian(as)             ((as)->big_endian)
290
291 extern int tdep_init_done;
292
293 extern void tdep_init (void);
294 extern int arm_find_proc_info (unw_addr_space_t as, unw_word_t ip,
295                                unw_proc_info_t *pi, int need_unwind_info,
296                                void *arg);
297 extern void arm_put_unwind_info (unw_addr_space_t as,
298                                   unw_proc_info_t *pi, void *arg);
299 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
300                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
301                                      int need_unwind_info, void *arg);
302 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
303 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
304                                unsigned long *segbase, unsigned long *mapoff,
305                                char *path, size_t pathlen);
306 extern void tdep_get_exe_image_path (char *path);
307 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
308                             unw_word_t *valp, int write);
309 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
310                               unw_fpreg_t *valp, int write);
311 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
312 extern void tdep_stash_frame (struct dwarf_cursor *c,
313                               struct dwarf_reg_state *rs);
314
315 /* unwinding method selection support */
316 #define UNW_ARM_METHOD_ALL          0xFF
317 #define UNW_ARM_METHOD_DWARF        0x01
318 #define UNW_ARM_METHOD_FRAME        0x02
319 #define UNW_ARM_METHOD_EXIDX        0x04
320
321 #define unwi_unwind_method   UNW_OBJ(unwind_method)
322 extern int unwi_unwind_method;
323
324 #define UNW_TRY_METHOD(x)   (unwi_unwind_method & x)
325
326 #endif /* ARM_LIBUNWIND_I_H */