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