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