libunwind_i.h revision ac6c0a6535975f1dc2da6e4e2766614baac2a14a
1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2001-2005 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4   Copyright (C) 2013 Linaro Limited
5
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27#ifndef AARCH64_LIBUNWIND_I_H
28#define AARCH64_LIBUNWIND_I_H
29
30/* Target-dependent definitions that are internal to libunwind but need
31   to be shared with target-independent code.  */
32
33#include <stdlib.h>
34#include <libunwind.h>
35
36#include "elf64.h"
37#include "mempool.h"
38#include "dwarf.h"
39
40typedef struct
41  {
42    /* no aarch64-specific fast trace */
43  }
44unw_tdep_frame_t;
45
46#ifdef UNW_LOCAL_ONLY
47
48typedef unw_word_t aarch64_loc_t;
49
50#else /* !UNW_LOCAL_ONLY */
51
52typedef struct aarch64_loc
53  {
54    unw_word_t w0, w1;
55  }
56aarch64_loc_t;
57
58#endif /* !UNW_LOCAL_ONLY */
59
60struct unw_addr_space
61  {
62    struct unw_accessors acc;
63    int big_endian;
64    unw_caching_policy_t caching_policy;
65#ifdef HAVE_ATOMIC_OPS_H
66    AO_t cache_generation;
67#else
68    uint32_t cache_generation;
69#endif
70    unw_word_t dyn_generation;          /* see dyn-common.h */
71    unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
72    struct dwarf_rs_cache global_cache;
73    struct unw_debug_frame_list *debug_frames;
74   };
75
76struct cursor
77  {
78    struct dwarf_cursor dwarf;          /* must be first */
79    enum
80      {
81        AARCH64_SCF_NONE,
82        AARCH64_SCF_LINUX_RT_SIGFRAME,
83      }
84    sigcontext_format;
85    unw_word_t sigcontext_addr;
86    unw_word_t sigcontext_sp;
87    unw_word_t sigcontext_pc;
88  };
89
90#define DWARF_GET_LOC(l)        ((l).val)
91
92#ifdef UNW_LOCAL_ONLY
93# define DWARF_NULL_LOC         DWARF_LOC (0, 0)
94# define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
95# define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
96# define DWARF_IS_REG_LOC(l)    0
97# define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
98                                 tdep_uc_addr((c)->as_arg, (r)), 0))
99# define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
100# define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
101                                 tdep_uc_addr((c)->as_arg, (r)), 0))
102
103static inline int
104dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
105{
106  if (!DWARF_GET_LOC (loc))
107    return -1;
108  *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
109  return 0;
110}
111
112static inline int
113dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
114{
115  if (!DWARF_GET_LOC (loc))
116    return -1;
117  *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
118  return 0;
119}
120
121static inline int
122dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
123{
124  if (!DWARF_GET_LOC (loc))
125    return -1;
126  *val = *(unw_word_t *) DWARF_GET_LOC (loc);
127  return 0;
128}
129
130static inline int
131dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
132{
133  if (!DWARF_GET_LOC (loc))
134    return -1;
135  *(unw_word_t *) DWARF_GET_LOC (loc) = val;
136  return 0;
137}
138
139#else /* !UNW_LOCAL_ONLY */
140# define DWARF_LOC_TYPE_FP      (1 << 0)
141# define DWARF_LOC_TYPE_REG     (1 << 1)
142# define DWARF_NULL_LOC         DWARF_LOC (0, 0)
143# define DWARF_IS_NULL_LOC(l)                                           \
144                ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
145# define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
146# define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
147# define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
148# define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
149# define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
150# define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
151                                                | DWARF_LOC_TYPE_FP))
152
153static inline int
154dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
155{
156  char *valp = (char *) &val;
157  unw_word_t addr;
158  int ret;
159
160  if (DWARF_IS_NULL_LOC (loc))
161    return -UNW_EBADREG;
162
163  if (DWARF_IS_REG_LOC (loc))
164    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
165                                       val, 0, c->as_arg);
166
167  addr = DWARF_GET_LOC (loc);
168  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
169                                       0, c->as_arg)) < 0)
170    return ret;
171
172  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
173                                   c->as_arg);
174}
175
176static inline int
177dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
178{
179  char *valp = (char *) &val;
180  unw_word_t addr;
181  int ret;
182
183  if (DWARF_IS_NULL_LOC (loc))
184    return -UNW_EBADREG;
185
186  if (DWARF_IS_REG_LOC (loc))
187    return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
188                                       &val, 1, c->as_arg);
189
190  addr = DWARF_GET_LOC (loc);
191  if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
192                                       1, c->as_arg)) < 0)
193    return ret;
194
195  return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
196                                   1, c->as_arg);
197}
198
199static inline int
200dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
201{
202  if (DWARF_IS_NULL_LOC (loc))
203    return -UNW_EBADREG;
204
205  /* If a code-generator were to save a value of type unw_word_t in a
206     floating-point register, we would have to support this case.  I
207     suppose it could happen with MMX registers, but does it really
208     happen?  */
209  assert (!DWARF_IS_FP_LOC (loc));
210
211  if (DWARF_IS_REG_LOC (loc))
212    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
213                                     0, c->as_arg);
214  else
215    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
216                                     0, c->as_arg);
217}
218
219static inline int
220dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
221{
222  if (DWARF_IS_NULL_LOC (loc))
223    return -UNW_EBADREG;
224
225  /* If a code-generator were to save a value of type unw_word_t in a
226     floating-point register, we would have to support this case.  I
227     suppose it could happen with MMX registers, but does it really
228     happen?  */
229  assert (!DWARF_IS_FP_LOC (loc));
230
231  if (DWARF_IS_REG_LOC (loc))
232    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
233                                     1, c->as_arg);
234  else
235    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
236                                     1, c->as_arg);
237}
238
239#endif /* !UNW_LOCAL_ONLY */
240
241
242
243#define tdep_getcontext_trace           unw_getcontext
244#define tdep_init_done			UNW_OBJ(init_done)
245#define tdep_init			UNW_OBJ(init)
246/* Platforms that support UNW_INFO_FORMAT_TABLE need to define
247   tdep_search_unwind_table.  */
248#define tdep_search_unwind_table	dwarf_search_unwind_table
249#define tdep_find_unwind_table	        dwarf_find_unwind_table
250#define tdep_uc_addr			UNW_OBJ(uc_addr)
251#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
252#define tdep_access_reg			UNW_OBJ(access_reg)
253#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
254#define tdep_fetch_frame(c,ip,n)	do {} while(0)
255#define tdep_cache_frame(c,rs)		do {} while(0)
256#define tdep_reuse_frame(c,rs)		do {} while(0)
257#define tdep_stash_frame(c,rs)		do {} while(0)
258#define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
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
274#define tdep_get_as(c)                  ((c)->dwarf.as)
275#define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
276#define tdep_get_ip(c)                  ((c)->dwarf.ip)
277#define tdep_big_endian(as)             ((as)->big_endian)
278
279extern int tdep_init_done;
280
281extern void tdep_init (void);
282extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
283				     unw_dyn_info_t *di, unw_proc_info_t *pi,
284				     int need_unwind_info, void *arg);
285extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
286extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
287			       unsigned long *segbase, unsigned long *mapoff,
288			       char *path, size_t pathlen);
289extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
290			    unw_word_t *valp, int write);
291extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
292			      unw_fpreg_t *valp, int write);
293
294#endif /* AARCH64_LIBUNWIND_I_H */
295