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