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