libunwind_i.h revision 4499bb29a6214b5df5b3b21e03c6b4f3c8f26cfd
1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2006-2007 IBM
3   Contributed by
4     Corey Ashford <cjashfor@us.ibm.com>
5     Jose Flavio Aguilar Paulino <jflavio@br.ibm.com> <joseflavio@gmail.com>
6
7   Copied from libunwind-x86_64.h, modified slightly for building
8   frysk successfully on ppc64, by Wu Zhou <woodzltc@cn.ibm.com>
9   Will be replaced when libunwind is ready on ppc64 platform.
10
11This file is part of libunwind.
12
13Permission is hereby granted, free of charge, to any person obtaining
14a copy of this software and associated documentation files (the
15"Software"), to deal in the Software without restriction, including
16without limitation the rights to use, copy, modify, merge, publish,
17distribute, sublicense, and/or sell copies of the Software, and to
18permit persons to whom the Software is furnished to do so, subject to
19the following conditions:
20
21The above copyright notice and this permission notice shall be
22included in all copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
31
32#ifndef PPC32_LIBUNWIND_I_H
33#define PPC32_LIBUNWIND_I_H
34
35/* Target-dependent definitions that are internal to libunwind but need
36   to be shared with target-independent code.  */
37
38#include <stdlib.h>
39#include <libunwind.h>
40
41#include "elf32.h"
42#include "mempool.h"
43#include "dwarf.h"
44
45struct unw_addr_space
46{
47  struct unw_accessors acc;
48  unw_caching_policy_t caching_policy;
49#ifdef HAVE_ATOMIC_OPS_H
50  AO_t cache_generation;
51#else
52  uint32_t cache_generation;
53#endif
54  unw_word_t dyn_generation;	/* see dyn-common.h */
55  unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
56  struct dwarf_rs_cache global_cache;
57  int validate;
58};
59
60struct cursor
61{
62  struct dwarf_cursor dwarf;	/* must be first */
63
64  /* Format of sigcontext structure and address at which it is
65     stored: */
66  enum
67  {
68    PPC_SCF_NONE,		/* no signal frame encountered */
69    PPC_SCF_LINUX_RT_SIGFRAME	/* POSIX ucontext_t */
70  }
71  sigcontext_format;
72  unw_word_t sigcontext_addr;
73};
74
75#define DWARF_GET_LOC(l)	((l).val)
76
77#ifdef UNW_LOCAL_ONLY
78# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
79# define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
80# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
81# define DWARF_IS_REG_LOC(l)	0
82# define DWARF_IS_FP_LOC(l)	0
83# define DWARF_IS_V_LOC(l)	0
84# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
85# define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
86				 tdep_uc_addr((c)->as_arg, (r)), 0))
87# define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
88				 tdep_uc_addr((c)->as_arg, (r)), 0))
89# define DWARF_VREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
90				 tdep_uc_addr((c)->as_arg, (r)), 0))
91#else /* !UNW_LOCAL_ONLY */
92
93# define DWARF_LOC_TYPE_FP	(1 << 0)
94# define DWARF_LOC_TYPE_REG	(1 << 1)
95# define DWARF_LOC_TYPE_V	(1 << 2)
96# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
97# define DWARF_IS_NULL_LOC(l)						\
98		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
99# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
100# define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
101# define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
102# define DWARF_IS_V_LOC(l)	(((l).type & DWARF_LOC_TYPE_V) != 0)
103# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
104# define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
105# define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
106						| DWARF_LOC_TYPE_FP))
107# define DWARF_VREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
108						| DWARF_LOC_TYPE_V))
109
110#endif /* !UNW_LOCAL_ONLY */
111
112static inline int
113dwarf_getvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t * val)
114{
115  unw_word_t *valp = (unw_word_t *) val;
116  unw_word_t addr;
117  int ret;
118
119  if (DWARF_IS_NULL_LOC (loc))
120    return -UNW_EBADREG;
121
122  assert (DWARF_IS_V_LOC (loc));
123  assert (!DWARF_IS_FP_LOC (loc));
124
125  if (DWARF_IS_REG_LOC (loc))
126    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
127				      val, 0, c->as_arg);
128
129  addr = DWARF_GET_LOC (loc);
130
131  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, valp,
132				       0, c->as_arg)) < 0)
133    return ret;
134
135  return (*c->as->acc.access_mem) (c->as, addr + 8, valp + 1, 0, c->as_arg);
136}
137
138static inline int
139dwarf_putvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
140{
141  unw_word_t *valp = (unw_word_t *) & val;
142  unw_word_t addr;
143  int ret;
144
145  if (DWARF_IS_NULL_LOC (loc))
146    return -UNW_EBADREG;
147
148  assert (DWARF_IS_V_LOC (loc));
149  assert (!DWARF_IS_FP_LOC (loc));
150
151  if (DWARF_IS_REG_LOC (loc))
152    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
153				      &val, 1, c->as_arg);
154
155  addr = DWARF_GET_LOC (loc);
156  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, valp,
157				       1, c->as_arg)) < 0)
158    return ret;
159
160  return (*c->as->acc.access_mem) (c->as, addr + 8, valp + 1, 1, c->as_arg);
161}
162
163static inline int
164dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t * val)
165{
166  unw_word_t *valp = (unw_word_t *) val;
167  unw_word_t addr;
168
169  if (DWARF_IS_NULL_LOC (loc))
170    return -UNW_EBADREG;
171
172  assert (DWARF_IS_FP_LOC (loc));
173  assert (!DWARF_IS_V_LOC (loc));
174
175  if (DWARF_IS_REG_LOC (loc))
176    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
177				       val, 0, c->as_arg);
178
179  addr = DWARF_GET_LOC (loc);
180  return (*c->as->acc.access_mem) (c->as, addr + 0, valp, 0, c->as_arg);
181
182}
183
184static inline int
185dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
186{
187  unw_word_t *valp = (unw_word_t *) & val;
188  unw_word_t addr;
189
190  if (DWARF_IS_NULL_LOC (loc))
191    return -UNW_EBADREG;
192
193  assert (DWARF_IS_FP_LOC (loc));
194  assert (!DWARF_IS_V_LOC (loc));
195
196  if (DWARF_IS_REG_LOC (loc))
197    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
198				       &val, 1, c->as_arg);
199
200  addr = DWARF_GET_LOC (loc);
201
202  return (*c->as->acc.access_mem) (c->as, addr + 0, valp, 1, c->as_arg);
203}
204
205static inline int
206dwarf_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  assert (!DWARF_IS_V_LOC (loc));
217
218  if (DWARF_IS_REG_LOC (loc))
219    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
220				     0, c->as_arg);
221  else
222    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
223				     0, c->as_arg);
224}
225
226static inline int
227dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
228{
229  if (DWARF_IS_NULL_LOC (loc))
230    return -UNW_EBADREG;
231
232  /* If a code-generator were to save a value of type unw_word_t in a
233     floating-point register, we would have to support this case.  I
234     suppose it could happen with MMX registers, but does it really
235     happen?  */
236  assert (!DWARF_IS_FP_LOC (loc));
237  assert (!DWARF_IS_V_LOC (loc));
238
239  if (DWARF_IS_REG_LOC (loc))
240    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
241				     1, c->as_arg);
242  else
243    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
244				     1, c->as_arg);
245}
246
247
248
249#define tdep_needs_initialization	UNW_OBJ(needs_initialization)
250#define tdep_init			UNW_OBJ(init)
251/* Platforms that support UNW_INFO_FORMAT_TABLE need to define
252   tdep_search_unwind_table.  */
253#define tdep_search_unwind_table	dwarf_search_unwind_table
254#define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
255#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
256#define tdep_access_reg			UNW_OBJ(access_reg)
257#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
258#define tdep_get_func_addr		UNW_OBJ(get_func_addr)
259
260#ifdef UNW_LOCAL_ONLY
261# define tdep_find_proc_info(c,ip,n)				\
262	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
263				       (c)->as_arg)
264# define tdep_put_unwind_info(as,pi,arg)		\
265	dwarf_put_unwind_info((as), (pi), (arg))
266#else
267# define tdep_find_proc_info(c,ip,n)					\
268	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
269				       (c)->as_arg)
270# define tdep_put_unwind_info(as,pi,arg)			\
271	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
272#endif
273
274extern int tdep_fetch_proc_info_post (struct dwarf_cursor *c, unw_word_t ip,
275				      int need_unwind_info);
276
277#define tdep_get_as(c)			((c)->dwarf.as)
278#define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
279#define tdep_get_ip(c)			((c)->dwarf.ip)
280#define tdep_big_endian(as)		1
281
282extern int tdep_needs_initialization;
283
284extern void tdep_init (void);
285extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
286				     unw_dyn_info_t * di,
287				     unw_proc_info_t * pi,
288				     int need_unwind_info, void *arg);
289extern void *tdep_uc_addr (ucontext_t * uc, int reg);
290extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
291			       unsigned long *segbase, unsigned long *mapoff);
292extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
293			    unw_word_t * valp, int write);
294extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
295			      unw_fpreg_t * valp, int write);
296extern int tdep_get_func_addr (unw_addr_space_t as, unw_word_t addr,
297			       unw_word_t *entry_point);
298
299#endif /* PPC64_LIBUNWIND_I_H */
300