libunwind_i.h revision 8d5b1aeeffb80515197fd7aeee0b3fbfac904ecd
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#include "mempool.h"
37#include "dwarf.h"
38
39typedef struct
40  {
41    /* no sh-specific fast trace */
42  }
43unw_tdep_frame_t;
44
45struct 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
61struct cursor
62  {
63    struct dwarf_cursor dwarf;		/* must be first */
64    enum
65      {
66        SH_SCF_NONE,                   /* no signal frame */
67        SH_SCF_LINUX_SIGFRAME,         /* non-RT signal frame */
68        SH_SCF_LINUX_RT_SIGFRAME,      /* RT signal frame */
69      }
70    sigcontext_format;
71    unw_word_t sigcontext_addr;
72    unw_word_t sigcontext_sp;
73    unw_word_t sigcontext_pc;
74  };
75
76#define DWARF_GET_LOC(l)	((l).val)
77
78#ifdef UNW_LOCAL_ONLY
79# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
80# define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
81# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
82# define DWARF_IS_REG_LOC(l)	0
83# define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
84				 tdep_uc_addr((c)->as_arg, (r)), 0))
85# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
86# define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
87				 tdep_uc_addr((c)->as_arg, (r)), 0))
88
89static inline int
90dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
91{
92  if (!DWARF_GET_LOC (loc))
93    return -1;
94  *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
95  return 0;
96}
97
98static inline int
99dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
100{
101  if (!DWARF_GET_LOC (loc))
102    return -1;
103  *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
104  return 0;
105}
106
107static inline int
108dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
109{
110  if (!DWARF_GET_LOC (loc))
111    return -1;
112  *val = *(unw_word_t *) DWARF_GET_LOC (loc);
113  return 0;
114}
115
116static inline int
117dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
118{
119  if (!DWARF_GET_LOC (loc))
120    return -1;
121  *(unw_word_t *) DWARF_GET_LOC (loc) = val;
122  return 0;
123}
124
125#else /* !UNW_LOCAL_ONLY */
126# define DWARF_LOC_TYPE_FP	(1 << 0)
127# define DWARF_LOC_TYPE_REG	(1 << 1)
128# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
129# define DWARF_IS_NULL_LOC(l)						\
130		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
131# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
132# define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
133# define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
134# define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
135# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
136# define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
137						| DWARF_LOC_TYPE_FP))
138
139static inline int
140dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
141{
142  char *valp = (char *) &val;
143  unw_word_t addr;
144  int ret;
145
146  if (DWARF_IS_NULL_LOC (loc))
147    return -UNW_EBADREG;
148
149  if (DWARF_IS_REG_LOC (loc))
150    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
151				       val, 0, c->as_arg);
152
153  addr = DWARF_GET_LOC (loc);
154  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
155				       0, c->as_arg)) < 0)
156    return ret;
157
158  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
159				   c->as_arg);
160}
161
162static inline int
163dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
164{
165  char *valp = (char *) &val;
166  unw_word_t addr;
167  int ret;
168
169  if (DWARF_IS_NULL_LOC (loc))
170    return -UNW_EBADREG;
171
172  if (DWARF_IS_REG_LOC (loc))
173    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
174				       &val, 1, c->as_arg);
175
176  addr = DWARF_GET_LOC (loc);
177  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
178				       1, c->as_arg)) < 0)
179    return ret;
180
181  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
182				   1, c->as_arg);
183}
184
185static inline int
186dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
187{
188  if (DWARF_IS_NULL_LOC (loc))
189    return -UNW_EBADREG;
190
191  /* If a code-generator were to save a value of type unw_word_t in a
192     floating-point register, we would have to support this case.  I
193     suppose it could happen with MMX registers, but does it really
194     happen?  */
195  assert (!DWARF_IS_FP_LOC (loc));
196
197  if (DWARF_IS_REG_LOC (loc))
198    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
199				     0, c->as_arg);
200  else
201    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
202				     0, c->as_arg);
203}
204
205static inline int
206dwarf_put (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				     1, c->as_arg);
220  else
221    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
222				     1, c->as_arg);
223}
224
225#endif /* !UNW_LOCAL_ONLY */
226
227#define tdep_getcontext_trace           unw_getcontext
228#define tdep_init_done			UNW_OBJ(init_done)
229#define tdep_init			UNW_OBJ(init)
230/* Platforms that support UNW_INFO_FORMAT_TABLE need to define
231   tdep_search_unwind_table.  */
232#define tdep_search_unwind_table	dwarf_search_unwind_table
233#define tdep_find_unwind_table		dwarf_find_unwind_table
234#define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
235#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
236#define tdep_access_reg			UNW_OBJ(access_reg)
237#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
238#define tdep_fetch_frame(c,ip,n)	do {} while(0)
239#define tdep_cache_frame(c,rs)		do {} while(0)
240#define tdep_reuse_frame(c,rs)		do {} while(0)
241#define tdep_stash_frame(c,rs)		do {} while(0)
242#define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
243
244#ifdef UNW_LOCAL_ONLY
245# define tdep_find_proc_info(c,ip,n)				\
246	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
247				       (c)->as_arg)
248# define tdep_put_unwind_info(as,pi,arg)		\
249	dwarf_put_unwind_info((as), (pi), (arg))
250#else
251# define tdep_find_proc_info(c,ip,n)					\
252	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
253				       (c)->as_arg)
254# define tdep_put_unwind_info(as,pi,arg)		\
255	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
256#endif
257
258#define tdep_get_as(c)			((c)->dwarf.as)
259#define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
260#define tdep_get_ip(c)			((c)->dwarf.ip)
261#define tdep_big_endian(as)		((as)->big_endian)
262
263extern int tdep_init_done;
264
265extern void tdep_init (void);
266extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
267				     unw_dyn_info_t *di, unw_proc_info_t *pi,
268				     int need_unwind_info, void *arg);
269extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
270extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
271			       unsigned long *segbase, unsigned long *mapoff,
272			       char *path, size_t pathlen);
273extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
274			    unw_word_t *valp, int write);
275extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
276			      unw_fpreg_t *valp, int write);
277
278#endif /* SH_LIBUNWIND_I_H */
279