1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002, 2004 Hewlett-Packard Co
3 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
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 #ifdef UNW_REMOTE_ONLY
33
34 /* unw_local_addr_space is a NULL pointer in this case. */
35 unw_addr_space_t unw_local_addr_space;
36
37 #else /* !UNW_REMOTE_ONLY */
38
39 static struct unw_addr_space local_addr_space;
40
41 unw_addr_space_t unw_local_addr_space = &local_addr_space;
42
43 static inline void *
uc_addr(ucontext_t * uc,int reg)44 uc_addr (ucontext_t *uc, int reg)
45 {
46 void *addr;
47
48 if ((unsigned) (reg - UNW_HPPA_GR) < 32)
49 addr = &uc->uc_mcontext.sc_gr[reg - UNW_HPPA_GR];
50 else if ((unsigned) (reg - UNW_HPPA_FR) < 32)
51 addr = &uc->uc_mcontext.sc_fr[reg - UNW_HPPA_FR];
52 else
53 addr = NULL;
54 return addr;
55 }
56
57 # ifdef UNW_LOCAL_ONLY
58
59 void *
_Uhppa_uc_addr(ucontext_t * uc,int reg)60 _Uhppa_uc_addr (ucontext_t *uc, int reg)
61 {
62 return uc_addr (uc, reg);
63 }
64
65 # endif /* UNW_LOCAL_ONLY */
66
67 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)68 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
69 {
70 /* it's a no-op */
71 }
72
73 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)74 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
75 void *arg)
76 {
77 #ifndef UNW_LOCAL_ONLY
78 # pragma weak _U_dyn_info_list_addr
79 if (!_U_dyn_info_list_addr)
80 return -UNW_ENOINFO;
81 #endif
82 // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
83 *dyn_info_list_addr = _U_dyn_info_list_addr ();
84 return 0;
85 }
86
87 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)88 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
89 void *arg)
90 {
91 if (write)
92 {
93 Debug (12, "mem[%x] <- %x\n", addr, *val);
94 *(unw_word_t *) addr = *val;
95 }
96 else
97 {
98 *val = *(unw_word_t *) addr;
99 Debug (12, "mem[%x] -> %x\n", addr, *val);
100 }
101 return 0;
102 }
103
104 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)105 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
106 void *arg)
107 {
108 unw_word_t *addr;
109 ucontext_t *uc = arg;
110
111 if ((unsigned int) (reg - UNW_HPPA_FR) < 32)
112 goto badreg;
113
114 addr = uc_addr (uc, reg);
115 if (!addr)
116 goto badreg;
117
118 if (write)
119 {
120 *(unw_word_t *) addr = *val;
121 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
122 }
123 else
124 {
125 *val = *(unw_word_t *) addr;
126 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
127 }
128 return 0;
129
130 badreg:
131 Debug (1, "bad register number %u\n", reg);
132 return -UNW_EBADREG;
133 }
134
135 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)136 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
137 int write, void *arg)
138 {
139 ucontext_t *uc = arg;
140 unw_fpreg_t *addr;
141
142 if ((unsigned) (reg - UNW_HPPA_FR) > 32)
143 goto badreg;
144
145 addr = uc_addr (uc, reg);
146 if (!addr)
147 goto badreg;
148
149 if (write)
150 {
151 Debug (12, "%s <- %08x.%08x\n",
152 unw_regname (reg), val->raw.bits[1], val->raw.bits[0]);
153 *(unw_fpreg_t *) addr = *val;
154 }
155 else
156 {
157 *val = *(unw_fpreg_t *) addr;
158 Debug (12, "%s -> %08x.%08x\n",
159 unw_regname (reg), val->raw.bits[1], val->raw.bits[0]);
160 }
161 return 0;
162
163 badreg:
164 Debug (1, "bad register number %u\n", reg);
165 /* attempt to access a non-preserved register */
166 return -UNW_EBADREG;
167 }
168
169 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)170 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
171 char *buf, size_t buf_len, unw_word_t *offp,
172 void *arg)
173 {
174 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
175 }
176
177 HIDDEN void
hppa_local_addr_space_init(void)178 hppa_local_addr_space_init (void)
179 {
180 memset (&local_addr_space, 0, sizeof (local_addr_space));
181 local_addr_space.caching_policy = UNWI_DEFAULT_CACHING_POLICY;
182 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
183 local_addr_space.acc.put_unwind_info = put_unwind_info;
184 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
185 local_addr_space.acc.access_mem = access_mem;
186 local_addr_space.acc.access_reg = access_reg;
187 local_addr_space.acc.access_fpreg = access_fpreg;
188 local_addr_space.acc.resume = hppa_local_resume;
189 local_addr_space.acc.get_proc_name = get_static_proc_name;
190 unw_flush_cache (&local_addr_space, 0, 0);
191 }
192
193 #endif /* !UNW_REMOTE_ONLY */
194