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