• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MIPS_LIBUNWIND_I_H
26 #define MIPS_LIBUNWIND_I_H
27 
28 /* Target-dependent definitions that are internal to libunwind but need
29    to be shared with target-independent code.  */
30 
31 #include <stdlib.h>
32 #include <libunwind.h>
33 #include <stdatomic.h>
34 
35 #if !defined(UNW_REMOTE_ONLY) && _MIPS_SIM == _ABI64
36 # include "elf64.h"
37 #else
38 # include "elf32.h"
39 #endif
40 #include "mempool.h"
41 #include "dwarf.h"
42 
43 typedef struct
44   {
45     /* no mips-specific fast trace */
46   }
47 unw_tdep_frame_t;
48 
49 struct unw_addr_space
50   {
51     struct unw_accessors acc;
52 
53     int big_endian;
54     mips_abi_t abi;
55     unsigned int addr_size;
56 
57     unw_caching_policy_t caching_policy;
58     _Atomic uint32_t cache_generation;
59     unw_word_t dyn_generation;          /* see dyn-common.h */
60     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
61     struct dwarf_rs_cache global_cache;
62     struct unw_debug_frame_list *debug_frames;
63 };
64 
65 #define tdep_big_endian(as)             ((as)->big_endian)
66 
67 struct cursor
68   {
69     struct dwarf_cursor dwarf;          /* must be first */
70     unw_word_t sigcontext_addr;
71   };
72 
73 #define DWARF_GET_LOC(l)        ((l).val)
74 
75 #ifndef UNW_REMOTE_ONLY
76 # if _MIPS_SIM == _ABIN32
77 typedef long long mips_reg_t;
78 # else
79 typedef long mips_reg_t;
80 # endif
81 #endif
82 
83 #ifdef UNW_LOCAL_ONLY
84 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
85 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
86 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
87 # define DWARF_IS_REG_LOC(l)    0
88 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t) (intptr_t)           \
89                                  tdep_uc_addr((c)->as_arg, (r)), 0))
90 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
91 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t) (intptr_t)           \
92                                  tdep_uc_addr((c)->as_arg, (r)), 0))
93 
94 /* FIXME: Implement these for the MIPS FPU.  */
95 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)96 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
97 {
98   if (!DWARF_GET_LOC (loc))
99     return -1;
100   *val = *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc);
101   return 0;
102 }
103 
104 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)105 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
106 {
107   if (!DWARF_GET_LOC (loc))
108     return -1;
109   *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
110   return 0;
111 }
112 
113 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)114 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
115 {
116   if (!DWARF_GET_LOC (loc))
117     return -1;
118   *val = *(mips_reg_t *) (intptr_t) DWARF_GET_LOC (loc);
119   return 0;
120 }
121 
122 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)123 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
124 {
125   if (!DWARF_GET_LOC (loc))
126     return -1;
127   *(mips_reg_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
128   return 0;
129 }
130 
131 #else /* !UNW_LOCAL_ONLY */
132 # define DWARF_LOC_TYPE_FP      (1 << 0)
133 # define DWARF_LOC_TYPE_REG     (1 << 1)
134 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
135 # define DWARF_IS_NULL_LOC(l)                                           \
136                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
137 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
138 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
139 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
140 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
141 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
142 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
143                                                 | DWARF_LOC_TYPE_FP))
144 
145 static inline int
read_s32(struct dwarf_cursor * c,unw_word_t addr,unw_word_t * val)146 read_s32 (struct dwarf_cursor *c, unw_word_t addr, unw_word_t *val)
147 {
148   int offset = addr & 4;
149   int ret;
150   unw_word_t memval;
151 
152   ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
153   if (ret < 0)
154     return ret;
155 
156   if ((offset != 0) == tdep_big_endian (c->as))
157     *val = (int32_t) memval;
158   else
159     *val = (int32_t) (memval >> 32);
160 
161   return 0;
162 }
163 
164 static inline int
write_s32(struct dwarf_cursor * c,unw_word_t addr,const unw_word_t * val)165 write_s32 (struct dwarf_cursor *c, unw_word_t addr, const unw_word_t *val)
166 {
167   int offset = addr & 4;
168   int ret;
169   unw_word_t memval;
170 
171   ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
172   if (ret < 0)
173     return ret;
174 
175   if ((offset != 0) == tdep_big_endian (c->as))
176     memval = (memval & ~0xffffffffLL) | (uint32_t) *val;
177   else
178     memval = (memval & 0xffffffffLL) | (uint32_t) (*val << 32);
179 
180   return (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 1, c->as_arg);
181 }
182 
183 /* FIXME: Implement these for the MIPS FPU.  */
184 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)185 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
186 {
187   char *valp = (char *) &val;
188   unw_word_t addr;
189   int ret;
190 
191   if (DWARF_IS_NULL_LOC (loc))
192     return -UNW_EBADREG;
193 
194   if (DWARF_IS_REG_LOC (loc))
195     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
196                                        val, 0, c->as_arg);
197 
198   addr = DWARF_GET_LOC (loc);
199   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
200                                        0, c->as_arg)) < 0)
201     return ret;
202 
203   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
204                                    c->as_arg);
205 }
206 
207 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)208 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
209 {
210   char *valp = (char *) &val;
211   unw_word_t addr;
212   int ret;
213 
214   if (DWARF_IS_NULL_LOC (loc))
215     return -UNW_EBADREG;
216 
217   if (DWARF_IS_REG_LOC (loc))
218     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
219                                        &val, 1, c->as_arg);
220 
221   addr = DWARF_GET_LOC (loc);
222   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
223                                        1, c->as_arg)) < 0)
224     return ret;
225 
226   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
227                                    1, c->as_arg);
228 }
229 
230 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)231 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
232 {
233   if (DWARF_IS_NULL_LOC (loc))
234     return -UNW_EBADREG;
235 
236   /* If a code-generator were to save a value of type unw_word_t in a
237      floating-point register, we would have to support this case.  I
238      suppose it could happen with MMX registers, but does it really
239      happen?  */
240   assert (!DWARF_IS_FP_LOC (loc));
241 
242   if (DWARF_IS_REG_LOC (loc))
243     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
244                                      0, c->as_arg);
245   else if (c->as->abi == UNW_MIPS_ABI_O32)
246     return read_s32 (c, DWARF_GET_LOC (loc), val);
247   else if (c->as->abi == UNW_MIPS_ABI_N32) {
248     if (tdep_big_endian(c->as))
249       return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc) + 4, val,
250                                        0, c->as_arg);
251     else
252       return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
253                                        0, c->as_arg);
254   }
255   else
256     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
257                                      0, c->as_arg);
258 }
259 
260 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)261 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
262 {
263   if (DWARF_IS_NULL_LOC (loc))
264     return -UNW_EBADREG;
265 
266   /* If a code-generator were to save a value of type unw_word_t in a
267      floating-point register, we would have to support this case.  I
268      suppose it could happen with MMX registers, but does it really
269      happen?  */
270   assert (!DWARF_IS_FP_LOC (loc));
271 
272   if (DWARF_IS_REG_LOC (loc))
273     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
274                                      1, c->as_arg);
275   else if (c->as->abi == UNW_MIPS_ABI_O32)
276     return write_s32 (c, DWARF_GET_LOC (loc), &val);
277   else
278     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
279                                      1, c->as_arg);
280 }
281 
282 #endif /* !UNW_LOCAL_ONLY */
283 
284 #define tdep_getcontext_trace           unw_getcontext
285 #define tdep_init_done                  UNW_OBJ(init_done)
286 #define tdep_init                       UNW_OBJ(init)
287 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
288    tdep_search_unwind_table.  */
289 #define tdep_search_unwind_table        dwarf_search_unwind_table
290 #define tdep_find_unwind_table          dwarf_find_unwind_table
291 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
292 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
293 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
294 #define tdep_access_reg                 UNW_OBJ(access_reg)
295 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
296 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
297 #define tdep_cache_frame(c)             0
298 #define tdep_reuse_frame(c,frame)       do {} while(0)
299 #define tdep_stash_frame(c,rs)          do {} while(0)
300 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
301 
302 #ifdef UNW_LOCAL_ONLY
303 # define tdep_find_proc_info(c,ip,n)                            \
304         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
305                                        (c)->as_arg)
306 # define tdep_put_unwind_info(as,pi,arg)                \
307         dwarf_put_unwind_info((as), (pi), (arg))
308 #else
309 # define tdep_find_proc_info(c,ip,n)                                    \
310         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
311                                        (c)->as_arg)
312 # define tdep_put_unwind_info(as,pi,arg)                \
313         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
314 #endif
315 
316 #define tdep_get_as(c)                  ((c)->dwarf.as)
317 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
318 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
319 
320 extern atomic_bool tdep_init_done;
321 
322 extern void tdep_init (void);
323 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
324                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
325                                      int need_unwind_info, void *arg);
326 extern void *tdep_uc_addr (ucontext_t *uc, int reg);
327 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
328                                unsigned long *segbase, unsigned long *mapoff,
329                                char *path, size_t pathlen);
330 extern void tdep_get_exe_image_path (char *path);
331 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
332                             unw_word_t *valp, int write);
333 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
334                               unw_fpreg_t *valp, int write);
335 
336 #endif /* MIPS_LIBUNWIND_I_H */
337