libunwind_i.h revision 9e98f15e9aee12e67cd5956d06ccb559f6a06213
1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2008 CodeSourcery
3
4This file is part of libunwind.
5
6Permission is hereby granted, free of charge, to any person obtaining
7a copy of this software and associated documentation files (the
8"Software"), to deal in the Software without restriction, including
9without limitation the rights to use, copy, modify, merge, publish,
10distribute, sublicense, and/or sell copies of the Software, and to
11permit persons to whom the Software is furnished to do so, subject to
12the following conditions:
13
14The above copyright notice and this permission notice shall be
15included in all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23WITH 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
39struct unw_addr_space
40  {
41    struct unw_accessors acc;
42    int big_endian;
43    unw_caching_policy_t caching_policy;
44#ifdef HAVE_ATOMIC_OPS_H
45    AO_t cache_generation;
46#else
47    uint32_t cache_generation;
48#endif
49    unw_word_t dyn_generation;		/* see dyn-common.h */
50    unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
51    struct dwarf_rs_cache global_cache;
52    struct unw_debug_frame_list *debug_frames;
53  };
54
55struct cursor
56  {
57    struct dwarf_cursor dwarf;		/* must be first */
58    unw_word_t sigcontext_addr;
59  };
60
61#define DWARF_GET_LOC(l)	((l).val)
62
63#ifdef UNW_LOCAL_ONLY
64# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
65# define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
66# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
67# define DWARF_IS_REG_LOC(l)	0
68# define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
69				 tdep_uc_addr((c)->as_arg, (r)), 0))
70# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
71# define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
72				 tdep_uc_addr((c)->as_arg, (r)), 0))
73
74static inline int
75dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
76{
77  if (!DWARF_GET_LOC (loc))
78    return -1;
79  *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
80  return 0;
81}
82
83static inline int
84dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
85{
86  if (!DWARF_GET_LOC (loc))
87    return -1;
88  *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
89  return 0;
90}
91
92static inline int
93dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
94{
95  if (!DWARF_GET_LOC (loc))
96    return -1;
97  *val = *(unw_word_t *) DWARF_GET_LOC (loc);
98  return 0;
99}
100
101static inline int
102dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
103{
104  if (!DWARF_GET_LOC (loc))
105    return -1;
106  *(unw_word_t *) DWARF_GET_LOC (loc) = val;
107  return 0;
108}
109
110#else /* !UNW_LOCAL_ONLY */
111# define DWARF_LOC_TYPE_FP	(1 << 0)
112# define DWARF_LOC_TYPE_REG	(1 << 1)
113# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
114# define DWARF_IS_NULL_LOC(l)						\
115		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
116# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
117# define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
118# define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
119# define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
120# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
121# define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
122						| DWARF_LOC_TYPE_FP))
123
124static inline int
125dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
126{
127  char *valp = (char *) &val;
128  unw_word_t addr;
129  int ret;
130
131  if (DWARF_IS_NULL_LOC (loc))
132    return -UNW_EBADREG;
133
134  if (DWARF_IS_REG_LOC (loc))
135    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
136				       val, 0, c->as_arg);
137
138  addr = DWARF_GET_LOC (loc);
139  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
140				       0, c->as_arg)) < 0)
141    return ret;
142
143  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
144				   c->as_arg);
145}
146
147static inline int
148dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
149{
150  char *valp = (char *) &val;
151  unw_word_t addr;
152  int ret;
153
154  if (DWARF_IS_NULL_LOC (loc))
155    return -UNW_EBADREG;
156
157  if (DWARF_IS_REG_LOC (loc))
158    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
159				       &val, 1, c->as_arg);
160
161  addr = DWARF_GET_LOC (loc);
162  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
163				       1, c->as_arg)) < 0)
164    return ret;
165
166  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
167				   1, c->as_arg);
168}
169
170static inline int
171dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
172{
173  if (DWARF_IS_NULL_LOC (loc))
174    return -UNW_EBADREG;
175
176  /* If a code-generator were to save a value of type unw_word_t in a
177     floating-point register, we would have to support this case.  I
178     suppose it could happen with MMX registers, but does it really
179     happen?  */
180  assert (!DWARF_IS_FP_LOC (loc));
181
182  if (DWARF_IS_REG_LOC (loc))
183    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
184				     0, c->as_arg);
185  else
186    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
187				     0, c->as_arg);
188}
189
190static inline int
191dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
192{
193  if (DWARF_IS_NULL_LOC (loc))
194    return -UNW_EBADREG;
195
196  /* If a code-generator were to save a value of type unw_word_t in a
197     floating-point register, we would have to support this case.  I
198     suppose it could happen with MMX registers, but does it really
199     happen?  */
200  assert (!DWARF_IS_FP_LOC (loc));
201
202  if (DWARF_IS_REG_LOC (loc))
203    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
204				     1, c->as_arg);
205  else
206    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
207				     1, c->as_arg);
208}
209
210#endif /* !UNW_LOCAL_ONLY */
211
212#define tdep_needs_initialization	UNW_OBJ(needs_initialization)
213#define tdep_init			UNW_OBJ(init)
214/* Platforms that support UNW_INFO_FORMAT_TABLE need to define
215   tdep_search_unwind_table.  */
216#define tdep_search_unwind_table	dwarf_search_unwind_table
217#define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
218#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
219#define tdep_access_reg			UNW_OBJ(access_reg)
220#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
221#define tdep_fetch_frame(c,ip,n)	do {} while(0)
222#define tdep_cache_frame(c,rs)		do {} while(0)
223#define tdep_reuse_frame(c,rs)		do {} while(0)
224#define tdep_stash_frame(c,rs)		do {} while(0)
225
226#ifdef UNW_LOCAL_ONLY
227# define tdep_find_proc_info(c,ip,n)				\
228	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
229				       (c)->as_arg)
230# define tdep_put_unwind_info(as,pi,arg)		\
231	dwarf_put_unwind_info((as), (pi), (arg))
232#else
233# define tdep_find_proc_info(c,ip,n)					\
234	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
235				       (c)->as_arg)
236# define tdep_put_unwind_info(as,pi,arg)		\
237	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
238#endif
239
240#define tdep_get_as(c)			((c)->dwarf.as)
241#define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
242#define tdep_get_ip(c)			((c)->dwarf.ip)
243#define tdep_big_endian(as)		((as)->big_endian)
244
245extern int tdep_needs_initialization;
246
247extern void tdep_init (void);
248extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
249				     unw_dyn_info_t *di, unw_proc_info_t *pi,
250				     int need_unwind_info, void *arg);
251extern void *tdep_uc_addr (ucontext_t *uc, int reg);
252extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
253			       unsigned long *segbase, unsigned long *mapoff,
254			       char *path, size_t pathlen);
255extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
256			    unw_word_t *valp, int write);
257extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
258			      unw_fpreg_t *valp, int write);
259
260/* unwinding method selection support */
261#define UNW_ARM_METHOD_ALL          0xFF
262#define UNW_ARM_METHOD_DWARF        0x01
263#define UNW_ARM_METHOD_FRAME        0x02
264#define UNW_ARM_METHOD_EXIDX        0x04
265
266#define unwi_unwind_method   UNW_OBJ(unwind_method)
267extern int unwi_unwind_method;
268
269#define UNW_TRY_METHOD(x)   (unwi_unwind_method & x)
270
271#endif /* ARM_LIBUNWIND_I_H */
272