1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2002-2005 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.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 X86_LIBUNWIND_I_H
27#define X86_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 x86-specific fast trace */
45  }
46unw_tdep_frame_t;
47
48struct unw_addr_space
49  {
50    struct unw_accessors acc;
51    unw_caching_policy_t caching_policy;
52#ifdef HAVE_ATOMIC_OPS_H
53    AO_t cache_generation;
54#else
55    uint32_t cache_generation;
56#endif
57    unw_word_t dyn_generation;		/* see dyn-common.h */
58    unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
59    struct dwarf_rs_cache global_cache;
60    struct unw_debug_frame_list *debug_frames;
61    /* ANDROID support update. */
62#if defined(__linux__)
63    struct map_info *map_list;
64#endif
65    /* End of ANDROID update. */
66   };
67
68struct cursor
69  {
70    struct dwarf_cursor dwarf;		/* must be first */
71
72    /* Format of sigcontext structure and address at which it is
73       stored: */
74    enum
75      {
76	X86_SCF_NONE,			/* no signal frame encountered */
77	X86_SCF_LINUX_SIGFRAME,		/* Linux x86 sigcontext */
78	X86_SCF_LINUX_RT_SIGFRAME,	/* POSIX ucontext_t */
79	X86_SCF_FREEBSD_SIGFRAME,	/* FreeBSD x86 sigcontext */
80	X86_SCF_FREEBSD_SIGFRAME4,	/* FreeBSD 4.x x86 sigcontext */
81	X86_SCF_FREEBSD_OSIGFRAME,	/* FreeBSD pre-4.x x86 sigcontext */
82	X86_SCF_FREEBSD_SYSCALL,	/* FreeBSD x86 syscall */
83      }
84    sigcontext_format;
85    unw_word_t sigcontext_addr;
86    int validate;
87    ucontext_t *uc;
88  };
89
90static inline ucontext_t *
91dwarf_get_uc(const struct dwarf_cursor *cursor)
92{
93  const struct cursor *c = (struct cursor *) cursor->as_arg;
94  return c->uc;
95}
96
97#define DWARF_GET_LOC(l)	((l).val)
98
99#ifdef UNW_LOCAL_ONLY
100# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
101# define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
102# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
103# define DWARF_IS_REG_LOC(l)	0
104# define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
105				 tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
106# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
107# define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
108				 tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
109
110static inline int
111dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
112{
113  if (!DWARF_GET_LOC (loc))
114    return -1;
115  *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
116  return 0;
117}
118
119static inline int
120dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
121{
122  if (!DWARF_GET_LOC (loc))
123    return -1;
124  *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
125  return 0;
126}
127
128static inline int
129dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
130{
131  if (!DWARF_GET_LOC (loc))
132    return -1;
133  return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
134				   0, c->as_arg);
135}
136
137static inline int
138dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
139{
140  if (!DWARF_GET_LOC (loc))
141    return -1;
142  return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
143				   1, c->as_arg);
144}
145
146#else /* !UNW_LOCAL_ONLY */
147# define DWARF_LOC_TYPE_FP	(1 << 0)
148# define DWARF_LOC_TYPE_REG	(1 << 1)
149# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
150# define DWARF_IS_NULL_LOC(l)						\
151		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
152# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
153# define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
154# define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
155# define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
156# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
157# define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
158						| DWARF_LOC_TYPE_FP))
159
160static inline int
161dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
162{
163  char *valp = (char *) &val;
164  unw_word_t addr;
165  int ret;
166
167  if (DWARF_IS_NULL_LOC (loc))
168    return -UNW_EBADREG;
169
170  if (DWARF_IS_REG_LOC (loc))
171    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
172				       val, 0, c->as_arg);
173
174  addr = DWARF_GET_LOC (loc);
175  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
176				       0, c->as_arg)) < 0)
177    return ret;
178
179  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
180				   c->as_arg);
181}
182
183static inline int
184dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
185{
186  char *valp = (char *) &val;
187  unw_word_t addr;
188  int ret;
189
190  if (DWARF_IS_NULL_LOC (loc))
191    return -UNW_EBADREG;
192
193  if (DWARF_IS_REG_LOC (loc))
194    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
195				       &val, 1, c->as_arg);
196
197  addr = DWARF_GET_LOC (loc);
198  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
199				       1, c->as_arg)) < 0)
200    return ret;
201
202  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
203				   1, c->as_arg);
204}
205
206static inline int
207dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
208{
209  if (DWARF_IS_NULL_LOC (loc))
210    return -UNW_EBADREG;
211
212  /* If a code-generator were to save a value of type unw_word_t in a
213     floating-point register, we would have to support this case.  I
214     suppose it could happen with MMX registers, but does it really
215     happen?  */
216  assert (!DWARF_IS_FP_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
238  if (DWARF_IS_REG_LOC (loc))
239    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
240				     1, c->as_arg);
241  else
242    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
243				     1, c->as_arg);
244}
245
246#endif /* !UNW_LOCAL_ONLY */
247
248#define tdep_getcontext_trace           unw_getcontext
249#define tdep_init_done			UNW_OBJ(init_done)
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_find_unwind_table		dwarf_find_unwind_table
255#define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
256#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
257#define tdep_access_reg			UNW_OBJ(access_reg)
258#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
259#define tdep_fetch_frame(c,ip,n)	do {} while(0)
260#define tdep_cache_frame(c,rs)		do {} while(0)
261#define tdep_reuse_frame(c,rs)		do {} while(0)
262#define tdep_stash_frame(c,rs)		do {} while(0)
263#define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
264
265#ifdef UNW_LOCAL_ONLY
266# define tdep_find_proc_info(c,ip,n)				\
267	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
268				       (c)->as_arg)
269# define tdep_put_unwind_info(as,pi,arg)		\
270	dwarf_put_unwind_info((as), (pi), (arg))
271#else
272# define tdep_find_proc_info(c,ip,n)					\
273	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
274				       (c)->as_arg)
275# define tdep_put_unwind_info(as,pi,arg)		\
276	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
277#endif
278
279#define tdep_get_as(c)			((c)->dwarf.as)
280#define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
281#define tdep_get_ip(c)			((c)->dwarf.ip)
282#define tdep_big_endian(as)		0
283
284extern int tdep_init_done;
285
286extern void tdep_init (void);
287extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
288				     unw_dyn_info_t *di, unw_proc_info_t *pi,
289				     int need_unwind_info, void *arg);
290extern void *tdep_uc_addr (ucontext_t *uc, int reg);
291/* ANDROID support update. */
292extern int tdep_get_elf_image (unw_addr_space_t as, struct elf_image *ei,
293			       pid_t pid, unw_word_t ip,
294			       unsigned long *segbase, unsigned long *mapoff,
295			       char **path);
296/* End of ANDROID update. */
297extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
298			    unw_word_t *valp, int write);
299extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
300			      unw_fpreg_t *valp, int write);
301
302#endif /* X86_LIBUNWIND_I_H */
303