1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.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 #include <stdlib.h>
28 #include <string.h>
29
30 #include "unwind_i.h"
31
32 static struct unw_addr_space local_addr_space;
33
34 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
35
36 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
37
38 static inline void *
uc_addr(ucontext_t * uc,int reg)39 uc_addr (ucontext_t *uc, int reg)
40 {
41 // Technically this is cheating as sp is a separate field in the struct,
42 // but it works.
43 if (reg >= UNW_AARCH64_X0 && reg <= UNW_AARCH64_V31)
44 return &uc->uc_mcontext.regs[reg - UNW_AARCH64_X0];
45 else
46 return NULL;
47 }
48
49 /* XXX fix me: there is currently no way to locate the dyn-info list
50 by a remote unwinder. On ia64, this is done via a special
51 unwind-table entry. Perhaps something similar can be done with
52 DWARF2 unwind info. */
53
54 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)55 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
56 {
57 /* it's a no-op */
58 }
59
60 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)61 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
62 void *arg)
63 {
64 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
65 return 0;
66 }
67
68 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)69 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
70 void *arg)
71 {
72 if (write)
73 {
74 Debug (16, "mem[%lx] <- %lx\n", addr, *val);
75 *(unw_word_t *) addr = *val;
76 }
77 else
78 {
79 *val = *(unw_word_t *) addr;
80 Debug (16, "mem[%lx] -> %lx\n", addr, *val);
81 }
82 return 0;
83 }
84
85 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)86 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
87 void *arg)
88 {
89 unw_word_t *addr;
90 ucontext_t *uc = arg;
91
92 if (unw_is_fpreg (reg))
93 goto badreg;
94
95 if (!(addr = uc_addr (uc, reg)))
96 goto badreg;
97
98 if (write)
99 {
100 *(unw_word_t *) addr = *val;
101 Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
102 }
103 else
104 {
105 *val = *(unw_word_t *) addr;
106 Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
107 }
108 return 0;
109
110 badreg:
111 Debug (1, "bad register number %u\n", reg);
112 return -UNW_EBADREG;
113 }
114
115 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)116 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
117 int write, void *arg)
118 {
119 ucontext_t *uc = arg;
120 unw_fpreg_t *addr;
121
122 if (!unw_is_fpreg (reg))
123 goto badreg;
124
125 if (!(addr = uc_addr (uc, reg)))
126 goto badreg;
127
128 if (write)
129 {
130 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
131 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
132 *(unw_fpreg_t *) addr = *val;
133 }
134 else
135 {
136 *val = *(unw_fpreg_t *) addr;
137 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
138 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
139 }
140 return 0;
141
142 badreg:
143 Debug (1, "bad register number %u\n", reg);
144 /* attempt to access a non-preserved register */
145 return -UNW_EBADREG;
146 }
147
148 static int
get_static_proc_name(unw_addr_space_t as,unw_word_t ip,char * buf,size_t buf_len,unw_word_t * offp,void * arg)149 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
150 char *buf, size_t buf_len, unw_word_t *offp,
151 void *arg)
152 {
153 return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
154 }
155
156 HIDDEN void
aarch64_local_addr_space_init(void)157 aarch64_local_addr_space_init (void)
158 {
159 memset (&local_addr_space, 0, sizeof (local_addr_space));
160 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
161 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
162 local_addr_space.acc.put_unwind_info = put_unwind_info;
163 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
164 local_addr_space.acc.access_mem = access_mem;
165 local_addr_space.acc.access_reg = access_reg;
166 local_addr_space.acc.access_fpreg = access_fpreg;
167 local_addr_space.acc.resume = NULL;
168 local_addr_space.acc.get_proc_name = get_static_proc_name;
169 local_addr_space.big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
170 unw_flush_cache (&local_addr_space, 0, 0);
171 }
172