Initial import
[external/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 struct
40   {
41     /* no arm-specific fast trace */
42   }
43 unw_tdep_frame_t;
44
45 struct unw_addr_space
46   {
47     struct unw_accessors acc;
48     int big_endian;
49     unw_caching_policy_t caching_policy;
50 #ifdef HAVE_ATOMIC_OPS_H
51     AO_t cache_generation;
52 #else
53     uint32_t cache_generation;
54 #endif
55     unw_word_t dyn_generation;          /* see dyn-common.h */
56     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
57     struct dwarf_rs_cache global_cache;
58     struct unw_debug_frame_list *debug_frames;
59   };
60
61 struct cursor
62   {
63     struct dwarf_cursor dwarf;          /* must be first */
64     enum
65       {
66         ARM_SCF_NONE,                   /* no signal frame */
67         ARM_SCF_LINUX_SIGFRAME,         /* non-RT signal frame, kernel >=2.6.18 */
68         ARM_SCF_LINUX_RT_SIGFRAME,      /* RT signal frame, kernel >=2.6.18 */
69         ARM_SCF_LINUX_OLD_SIGFRAME,     /* non-RT signal frame, kernel < 2.6.18 */
70         ARM_SCF_LINUX_OLD_RT_SIGFRAME   /* RT signal frame, kernel < 2.6.18 */
71       }
72     sigcontext_format;
73     unw_word_t sigcontext_addr;
74     unw_word_t sigcontext_sp;
75     unw_word_t sigcontext_pc;
76   };
77
78 #define DWARF_GET_LOC(l)        ((l).val)
79
80 #ifdef UNW_LOCAL_ONLY
81 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
82 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
83 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
84 # define DWARF_IS_REG_LOC(l)    0
85 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
86                                  tdep_uc_addr((c)->as_arg, (r)), 0))
87 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
88 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
89                                  tdep_uc_addr((c)->as_arg, (r)), 0))
90
91 static inline int
92 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
93 {
94   if (!DWARF_GET_LOC (loc))
95     return -1;
96   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
97   return 0;
98 }
99
100 static inline int
101 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
102 {
103   if (!DWARF_GET_LOC (loc))
104     return -1;
105   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
106   return 0;
107 }
108
109 static inline int
110 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
111 {
112   if (!DWARF_GET_LOC (loc))
113     return -1;
114   *val = *(unw_word_t *) DWARF_GET_LOC (loc);
115   return 0;
116 }
117
118 static inline int
119 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
120 {
121   if (!DWARF_GET_LOC (loc))
122     return -1;
123   *(unw_word_t *) DWARF_GET_LOC (loc) = val;
124   return 0;
125 }
126
127 #else /* !UNW_LOCAL_ONLY */
128 # define DWARF_LOC_TYPE_FP      (1 << 0)
129 # define DWARF_LOC_TYPE_REG     (1 << 1)
130 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
131 # define DWARF_IS_NULL_LOC(l)                                           \
132                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
133 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
134 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
135 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
136 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
137 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
138 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
139                                                 | DWARF_LOC_TYPE_FP))
140
141 static inline int
142 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
143 {
144   char *valp = (char *) &val;
145   unw_word_t addr;
146   int ret;
147
148   if (DWARF_IS_NULL_LOC (loc))
149     return -UNW_EBADREG;
150
151   if (DWARF_IS_REG_LOC (loc))
152     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
153                                        val, 0, c->as_arg);
154
155   addr = DWARF_GET_LOC (loc);
156   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
157                                        0, c->as_arg)) < 0)
158     return ret;
159
160   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
161                                    c->as_arg);
162 }
163
164 static inline int
165 dwarf_putfp (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, 1, 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                                        1, c->as_arg)) < 0)
181     return ret;
182
183   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
184                                    1, c->as_arg);
185 }
186
187 static inline int
188 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
189 {
190   if (DWARF_IS_NULL_LOC (loc))
191     return -UNW_EBADREG;
192
193   /* If a code-generator were to save a value of type unw_word_t in a
194      floating-point register, we would have to support this case.  I
195      suppose it could happen with MMX registers, but does it really
196      happen?  */
197   assert (!DWARF_IS_FP_LOC (loc));
198
199   if (DWARF_IS_REG_LOC (loc))
200     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
201                                      0, c->as_arg);
202   else
203     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
204                                      0, c->as_arg);
205 }
206
207 static inline int
208 dwarf_put (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                                      1, c->as_arg);
222   else
223     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
224                                      1, c->as_arg);
225 }
226
227 #endif /* !UNW_LOCAL_ONLY */
228
229 #define tdep_getcontext_trace           unw_getcontext
230 #define tdep_init_done                  UNW_OBJ(init_done)
231 #define tdep_init                       UNW_OBJ(init)
232 #define arm_find_proc_info              UNW_OBJ(find_proc_info)
233 #define arm_put_unwind_info             UNW_OBJ(put_unwind_info)
234 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
235    tdep_search_unwind_table.  */
236 #define tdep_search_unwind_table        UNW_OBJ(search_unwind_table)
237 #define tdep_find_unwind_table          dwarf_find_unwind_table
238 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
239 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
240 #define tdep_access_reg                 UNW_OBJ(access_reg)
241 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
242 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
243 #define tdep_cache_frame(c,rs)          do {} while(0)
244 #define tdep_reuse_frame(c,rs)          do {} while(0)
245 #define tdep_stash_frame(c,rs)          do {} while(0)
246 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
247
248 #ifdef UNW_LOCAL_ONLY
249 # define tdep_find_proc_info(c,ip,n)                            \
250         arm_find_proc_info((c)->as, (ip), &(c)->pi, (n),        \
251                                        (c)->as_arg)
252 # define tdep_put_unwind_info(as,pi,arg)                \
253         arm_put_unwind_info((as), (pi), (arg))
254 #else
255 # define tdep_find_proc_info(c,ip,n)                                    \
256         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
257                                        (c)->as_arg)
258 # define tdep_put_unwind_info(as,pi,arg)                \
259         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
260 #endif
261
262 #define tdep_get_as(c)                  ((c)->dwarf.as)
263 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
264 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
265 #define tdep_big_endian(as)             ((as)->big_endian)
266
267 extern int tdep_init_done;
268
269 extern void tdep_init (void);
270 extern int arm_find_proc_info (unw_addr_space_t as, unw_word_t ip,
271                                unw_proc_info_t *pi, int need_unwind_info,
272                                void *arg);
273 extern void arm_put_unwind_info (unw_addr_space_t as,
274                                   unw_proc_info_t *pi, void *arg);
275 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
276                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
277                                      int need_unwind_info, void *arg);
278 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
279 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
280                                unsigned long *segbase, unsigned long *mapoff,
281                                char *path, size_t pathlen);
282 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
283                             unw_word_t *valp, int write);
284 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
285                               unw_fpreg_t *valp, int write);
286
287 /* unwinding method selection support */
288 #define UNW_ARM_METHOD_ALL          0xFF
289 #define UNW_ARM_METHOD_DWARF        0x01
290 #define UNW_ARM_METHOD_FRAME        0x02
291 #define UNW_ARM_METHOD_EXIDX        0x04
292
293 #define unwi_unwind_method   UNW_OBJ(unwind_method)
294 extern int unwi_unwind_method;
295
296 #define UNW_TRY_METHOD(x)   (unwi_unwind_method & x)
297
298 #endif /* ARM_LIBUNWIND_I_H */