1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3    Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
4 
5 This file is part of libunwind.
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25 
26 #ifndef SH_LIBUNWIND_I_H
27 #define SH_LIBUNWIND_I_H
28 
29 /* Target-dependent definitions that are internal to libunwind but need
30    to be shared with target-independent code.  */
31 
32 #include <stdlib.h>
33 #include <libunwind.h>
34 
35 #include "elf32.h"
36 /* ANDROID support update. */
37 #include "map_info.h"
38 /* End of ANDROID update. */
39 #include "mempool.h"
40 #include "dwarf.h"
41 
42 typedef struct
43   {
44     /* no sh-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         SH_SCF_NONE,                   /* no signal frame */
73         SH_SCF_LINUX_SIGFRAME,         /* non-RT signal frame */
74         SH_SCF_LINUX_RT_SIGFRAME,      /* RT signal frame */
75       }
76     sigcontext_format;
77     unw_word_t sigcontext_addr;
78     unw_word_t sigcontext_sp;
79     unw_word_t sigcontext_pc;
80   };
81 
82 #define DWARF_GET_LOC(l)	((l).val)
83 
84 #ifdef UNW_LOCAL_ONLY
85 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
86 # define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
87 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
88 # define DWARF_IS_REG_LOC(l)	0
89 # define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
90 				 tdep_uc_addr((c)->as_arg, (r)), 0))
91 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
92 # define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
93 				 tdep_uc_addr((c)->as_arg, (r)), 0))
94 
95 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)96 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
97 {
98 /* ANDROID support update. */
99   unw_fpreg_t *addr = (unw_fpreg_t *) DWARF_GET_LOC (loc);
100   if (!addr || !map_local_is_readable ((unw_word_t) addr, sizeof(unw_fpreg_t)))
101     return -1;
102 
103   *val = *addr;
104   return 0;
105 /* End of ANDROID update. */
106 }
107 
108 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)109 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
110 {
111 /* ANDROID support update. */
112   unw_fpreg_t *addr = (unw_fpreg_t *) DWARF_GET_LOC (loc);
113   if (!addr || !map_local_is_writable ((unw_word_t) addr, sizeof(unw_fpreg_t)))
114     return -1;
115 
116   *addr = val;
117   return 0;
118 /* End of ANDROID update. */
119 }
120 
121 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)122 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
123 {
124 /* ANDROID support update. */
125   if (!DWARF_GET_LOC (loc))
126     return -1;
127   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
128 				   0, c->as_arg);
129 /* End of ANDROID update. */
130 }
131 
132 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)133 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
134 {
135 /* ANDROID support update. */
136   if (!DWARF_GET_LOC (loc))
137     return -1;
138   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
139 				   1, c->as_arg);
140 /* End of ANDROID update. */
141 }
142 
143 #else /* !UNW_LOCAL_ONLY */
144 # define DWARF_LOC_TYPE_FP	(1 << 0)
145 # define DWARF_LOC_TYPE_REG	(1 << 1)
146 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
147 # define DWARF_IS_NULL_LOC(l)						\
148 		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
149 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
150 # define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
151 # define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
152 # define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
153 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
154 # define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
155 						| DWARF_LOC_TYPE_FP))
156 
157 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)158 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
159 {
160   char *valp = (char *) &val;
161   unw_word_t addr;
162   int ret;
163 
164   if (DWARF_IS_NULL_LOC (loc))
165     return -UNW_EBADREG;
166 
167   if (DWARF_IS_REG_LOC (loc))
168     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
169 				       val, 0, c->as_arg);
170 
171   addr = DWARF_GET_LOC (loc);
172   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
173 				       0, c->as_arg)) < 0)
174     return ret;
175 
176   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
177 				   c->as_arg);
178 }
179 
180 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)181 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
182 {
183   char *valp = (char *) &val;
184   unw_word_t addr;
185   int ret;
186 
187   if (DWARF_IS_NULL_LOC (loc))
188     return -UNW_EBADREG;
189 
190   if (DWARF_IS_REG_LOC (loc))
191     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
192 				       &val, 1, c->as_arg);
193 
194   addr = DWARF_GET_LOC (loc);
195   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
196 				       1, c->as_arg)) < 0)
197     return ret;
198 
199   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
200 				   1, c->as_arg);
201 }
202 
203 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)204 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
205 {
206   if (DWARF_IS_NULL_LOC (loc))
207     return -UNW_EBADREG;
208 
209   /* If a code-generator were to save a value of type unw_word_t in a
210      floating-point register, we would have to support this case.  I
211      suppose it could happen with MMX registers, but does it really
212      happen?  */
213   assert (!DWARF_IS_FP_LOC (loc));
214 
215   if (DWARF_IS_REG_LOC (loc))
216     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
217 				     0, c->as_arg);
218   else
219     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
220 				     0, c->as_arg);
221 }
222 
223 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)224 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
225 {
226   if (DWARF_IS_NULL_LOC (loc))
227     return -UNW_EBADREG;
228 
229   /* If a code-generator were to save a value of type unw_word_t in a
230      floating-point register, we would have to support this case.  I
231      suppose it could happen with MMX registers, but does it really
232      happen?  */
233   assert (!DWARF_IS_FP_LOC (loc));
234 
235   if (DWARF_IS_REG_LOC (loc))
236     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
237 				     1, c->as_arg);
238   else
239     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
240 				     1, c->as_arg);
241 }
242 
243 #endif /* !UNW_LOCAL_ONLY */
244 
245 #define tdep_getcontext_trace           unw_getcontext
246 #define tdep_init_done			UNW_OBJ(init_done)
247 #define tdep_init			UNW_OBJ(init)
248 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
249    tdep_search_unwind_table.  */
250 #define tdep_search_unwind_table	dwarf_search_unwind_table
251 #define tdep_find_unwind_table		dwarf_find_unwind_table
252 #define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
253 #define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
254 #define tdep_access_reg			UNW_OBJ(access_reg)
255 #define tdep_access_fpreg		UNW_OBJ(access_fpreg)
256 #define tdep_fetch_frame(c,ip,n)	do {} while(0)
257 #define tdep_cache_frame(c,rs)		do {} while(0)
258 #define tdep_reuse_frame(c,rs)		do {} while(0)
259 #define tdep_stash_frame(c,rs)		do {} while(0)
260 #define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
261 
262 #ifdef UNW_LOCAL_ONLY
263 # define tdep_find_proc_info(c,ip,n)				\
264 	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
265 				       (c)->as_arg)
266 # define tdep_put_unwind_info(as,pi,arg)		\
267 	dwarf_put_unwind_info((as), (pi), (arg))
268 #else
269 # define tdep_find_proc_info(c,ip,n)					\
270 	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
271 				       (c)->as_arg)
272 # define tdep_put_unwind_info(as,pi,arg)		\
273 	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
274 #endif
275 
276 #define tdep_get_as(c)			((c)->dwarf.as)
277 #define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
278 #define tdep_get_ip(c)			((c)->dwarf.ip)
279 #define tdep_big_endian(as)		((as)->big_endian)
280 
281 extern int tdep_init_done;
282 
283 extern void tdep_init (void);
284 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
285 				     unw_dyn_info_t *di, unw_proc_info_t *pi,
286 				     int need_unwind_info, void *arg);
287 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
288 /* ANDROID support update. */
289 extern int tdep_get_elf_image (unw_addr_space_t as, struct elf_image *ei,
290 			       pid_t pid, unw_word_t ip,
291 			       unsigned long *segbase, unsigned long *mapoff,
292 			       char **path, void *as_arg);
293 /* End of ANDROID update. */
294 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
295 			    unw_word_t *valp, int write);
296 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
297 			      unw_fpreg_t *valp, int write);
298 
299 #endif /* SH_LIBUNWIND_I_H */
300