1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "unwind_i.h"
29
30 #ifdef UNW_REMOTE_ONLY
31
32 /* unw_local_addr_space is a NULL pointer in this case. */
33 unw_addr_space_t unw_local_addr_space;
34
35 #else /* !UNW_REMOTE_ONLY */
36
37 static struct unw_addr_space local_addr_space;
38
39 unw_addr_space_t unw_local_addr_space = &local_addr_space;
40
41 /* Return the address of the 64-bit slot in UC for REG (even for o32,
42 where registers are 32-bit, the slots are still 64-bit). */
43
44 static inline void *
uc_addr(ucontext_t * uc,int reg)45 uc_addr (ucontext_t *uc, int reg)
46 {
47 if (reg >= UNW_MIPS_R0 && reg < UNW_MIPS_R0 + 32)
48 return &uc->uc_mcontext.gregs[reg - UNW_MIPS_R0];
49 else if (reg == UNW_MIPS_PC)
50 return &uc->uc_mcontext.pc;
51 else
52 return NULL;
53 }
54
55 # ifdef UNW_LOCAL_ONLY
56
57 HIDDEN void *
tdep_uc_addr(ucontext_t * uc,int reg)58 tdep_uc_addr (ucontext_t *uc, int reg)
59 {
60 char *addr = uc_addr (uc, reg);
61
62 if (((reg >= UNW_MIPS_R0 && reg <= UNW_MIPS_R31) || reg == UNW_MIPS_PC)
63 && tdep_big_endian (unw_local_addr_space)
64 && unw_local_addr_space->abi == UNW_MIPS_ABI_O32)
65 addr += 4;
66
67 return addr;
68 }
69
70 # endif /* UNW_LOCAL_ONLY */
71
72 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)73 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
74 {
75 /* it's a no-op */
76 }
77
78 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)79 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
80 void *arg)
81 {
82 #ifndef UNW_LOCAL_ONLY
83 # pragma weak _U_dyn_info_list_addr
84 if (!_U_dyn_info_list_addr)
85 return -UNW_ENOINFO;
86 #endif
87 // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
88 *dyn_info_list_addr = _U_dyn_info_list_addr ();
89 return 0;
90 }
91
92 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)93 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
94 void *arg)
95 {
96 if (write)
97 {
98 Debug (16, "mem[%llx] <- %llx\n", (long long) addr, (long long) *val);
99 *(unw_word_t *) (intptr_t) addr = *val;
100 }
101 else
102 {
103 *val = *(unw_word_t *) (intptr_t) addr;
104 Debug (16, "mem[%llx] -> %llx\n", (long long) addr, (long long) *val);
105 }
106 return 0;
107 }
108
109 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)110 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
111 void *arg)
112 {
113 unw_word_t *addr;
114 ucontext_t *uc = arg;
115
116 if (unw_is_fpreg (reg))
117 goto badreg;
118
119 Debug (16, "reg = %s\n", unw_regname (reg));
120 if (!(addr = uc_addr (uc, reg)))
121 goto badreg;
122
123 if (write)
124 {
125 *(unw_word_t *) (intptr_t) addr = (mips_reg_t) *val;
126 Debug (12, "%s <- %llx\n", unw_regname (reg), (long long) *val);
127 }
128 else
129 {
130 *val = (mips_reg_t) *(unw_word_t *) (intptr_t) addr;
131 Debug (12, "%s -> %llx\n", unw_regname (reg), (long long) *val);
132 }
133 return 0;
134
135 badreg:
136 Debug (1, "bad register number %u\n", reg);
137 return -UNW_EBADREG;
138 }
139
140 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)141 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
142 int write, void *arg)
143 {
144 ucontext_t *uc = arg;
145 unw_fpreg_t *addr;
146
147 if (!unw_is_fpreg (reg))
148 goto badreg;
149
150 if (!(addr = uc_addr (uc, reg)))
151 goto badreg;
152
153 if (write)
154 {
155 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
156 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
157 *(unw_fpreg_t *) (intptr_t) addr = *val;
158 }
159 else
160 {
161 *val = *(unw_fpreg_t *) (intptr_t) addr;
162 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
163 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
164 }
165 return 0;
166
167 badreg:
168 Debug (1, "bad register number %u\n", reg);
169 /* attempt to access a non-preserved register */
170 return -UNW_EBADREG;
171 }
172
173 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)174 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
175 char *buf, size_t buf_len, unw_word_t *offp,
176 void *arg)
177 {
178
179 return elf_w (get_proc_name) (as, getpid (), ip, buf, buf_len, offp);
180 }
181
182 HIDDEN void
mips_local_addr_space_init(void)183 mips_local_addr_space_init (void)
184 {
185 memset (&local_addr_space, 0, sizeof (local_addr_space));
186 local_addr_space.big_endian = target_is_big_endian();
187 #if _MIPS_SIM == _ABIO32
188 local_addr_space.abi = UNW_MIPS_ABI_O32;
189 #elif _MIPS_SIM == _ABIN32
190 local_addr_space.abi = UNW_MIPS_ABI_N32;
191 #elif _MIPS_SIM == _ABI64
192 local_addr_space.abi = UNW_MIPS_ABI_N64;
193 #else
194 # error Unsupported ABI
195 #endif
196 local_addr_space.addr_size = sizeof (void *);
197 local_addr_space.caching_policy = UNWI_DEFAULT_CACHING_POLICY;
198 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
199 local_addr_space.acc.put_unwind_info = put_unwind_info;
200 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
201 local_addr_space.acc.access_mem = access_mem;
202 local_addr_space.acc.access_reg = access_reg;
203 local_addr_space.acc.access_fpreg = access_fpreg;
204 local_addr_space.acc.resume = NULL; /* mips_local_resume? FIXME! */
205 local_addr_space.acc.get_proc_name = get_static_proc_name;
206 unw_flush_cache (&local_addr_space, 0, 0);
207 }
208
209 #endif /* !UNW_REMOTE_ONLY */
210