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