Imported Upstream version 1.2
[platform/upstream/libunwind.git] / include / tdep-x86 / libunwind_i.h
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2002-2005 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26 #ifndef X86_LIBUNWIND_I_H
27 #define X86_LIBUNWIND_I_H
28
29 /* Target-dependent definitions that are internal to libunwind but need
30    to be shared with target-independent code.  */
31
32 #include <stdlib.h>
33 #include <libunwind.h>
34
35 #include "elf32.h"
36 #include "mempool.h"
37 #include "dwarf.h"
38
39 typedef struct
40   {
41     /* no x86-specific fast trace */
42   }
43 unw_tdep_frame_t;
44
45 struct unw_addr_space
46   {
47     struct unw_accessors acc;
48     unw_caching_policy_t caching_policy;
49 #ifdef HAVE_ATOMIC_OPS_H
50     AO_t cache_generation;
51 #else
52     uint32_t cache_generation;
53 #endif
54     unw_word_t dyn_generation;          /* see dyn-common.h */
55     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
56     struct dwarf_rs_cache global_cache;
57     struct unw_debug_frame_list *debug_frames;
58    };
59
60 struct cursor
61   {
62     struct dwarf_cursor dwarf;          /* must be first */
63
64     /* Format of sigcontext structure and address at which it is
65        stored: */
66     enum
67       {
68         X86_SCF_NONE,                   /* no signal frame encountered */
69         X86_SCF_LINUX_SIGFRAME,         /* Linux x86 sigcontext */
70         X86_SCF_LINUX_RT_SIGFRAME,      /* POSIX ucontext_t */
71         X86_SCF_FREEBSD_SIGFRAME,       /* FreeBSD x86 sigcontext */
72         X86_SCF_FREEBSD_SIGFRAME4,      /* FreeBSD 4.x x86 sigcontext */
73         X86_SCF_FREEBSD_OSIGFRAME,      /* FreeBSD pre-4.x x86 sigcontext */
74         X86_SCF_FREEBSD_SYSCALL,        /* FreeBSD x86 syscall */
75       }
76     sigcontext_format;
77     unw_word_t sigcontext_addr;
78     int validate;
79     ucontext_t *uc;
80   };
81
82 static inline ucontext_t *
83 dwarf_get_uc(const struct dwarf_cursor *cursor)
84 {
85   const struct cursor *c = (struct cursor *) cursor->as_arg;
86   return c->uc;
87 }
88
89 #define DWARF_GET_LOC(l)        ((l).val)
90
91 #ifdef UNW_LOCAL_ONLY
92 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
93 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
94 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
95 # define DWARF_IS_REG_LOC(l)    0
96 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
97                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
98 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
99 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
100                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
101
102 static inline int
103 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
104 {
105   if (!DWARF_GET_LOC (loc))
106     return -1;
107   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
108   return 0;
109 }
110
111 static inline int
112 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
113 {
114   if (!DWARF_GET_LOC (loc))
115     return -1;
116   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
117   return 0;
118 }
119
120 static inline int
121 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
122 {
123   if (!DWARF_GET_LOC (loc))
124     return -1;
125   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
126                                    0, c->as_arg);
127 }
128
129 static inline int
130 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
131 {
132   if (!DWARF_GET_LOC (loc))
133     return -1;
134   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
135                                    1, c->as_arg);
136 }
137
138 #else /* !UNW_LOCAL_ONLY */
139 # define DWARF_LOC_TYPE_FP      (1 << 0)
140 # define DWARF_LOC_TYPE_REG     (1 << 1)
141 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
142 # define DWARF_IS_NULL_LOC(l)                                           \
143                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
144 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
145 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
146 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
147 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
148 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
149 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
150                                                 | DWARF_LOC_TYPE_FP))
151
152 static inline int
153 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
154 {
155   char *valp = (char *) &val;
156   unw_word_t addr;
157   int ret;
158
159   if (DWARF_IS_NULL_LOC (loc))
160     return -UNW_EBADREG;
161
162   if (DWARF_IS_REG_LOC (loc))
163     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
164                                        val, 0, c->as_arg);
165
166   addr = DWARF_GET_LOC (loc);
167   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
168                                        0, c->as_arg)) < 0)
169     return ret;
170
171   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
172                                    c->as_arg);
173 }
174
175 static inline int
176 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
177 {
178   char *valp = (char *) &val;
179   unw_word_t addr;
180   int ret;
181
182   if (DWARF_IS_NULL_LOC (loc))
183     return -UNW_EBADREG;
184
185   if (DWARF_IS_REG_LOC (loc))
186     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
187                                        &val, 1, c->as_arg);
188
189   addr = DWARF_GET_LOC (loc);
190   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
191                                        1, c->as_arg)) < 0)
192     return ret;
193
194   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
195                                    1, c->as_arg);
196 }
197
198 static inline int
199 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
200 {
201   if (DWARF_IS_NULL_LOC (loc))
202     return -UNW_EBADREG;
203
204   /* If a code-generator were to save a value of type unw_word_t in a
205      floating-point register, we would have to support this case.  I
206      suppose it could happen with MMX registers, but does it really
207      happen?  */
208   assert (!DWARF_IS_FP_LOC (loc));
209
210   if (DWARF_IS_REG_LOC (loc))
211     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
212                                      0, c->as_arg);
213   else
214     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
215                                      0, c->as_arg);
216 }
217
218 static inline int
219 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
220 {
221   if (DWARF_IS_NULL_LOC (loc))
222     return -UNW_EBADREG;
223
224   /* If a code-generator were to save a value of type unw_word_t in a
225      floating-point register, we would have to support this case.  I
226      suppose it could happen with MMX registers, but does it really
227      happen?  */
228   assert (!DWARF_IS_FP_LOC (loc));
229
230   if (DWARF_IS_REG_LOC (loc))
231     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
232                                      1, c->as_arg);
233   else
234     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
235                                      1, c->as_arg);
236 }
237
238 #endif /* !UNW_LOCAL_ONLY */
239
240 #define tdep_getcontext_trace           unw_getcontext
241 #define tdep_init_done                  UNW_OBJ(init_done)
242 #define tdep_init                       UNW_OBJ(init)
243 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
244    tdep_search_unwind_table.  */
245 #define tdep_search_unwind_table        dwarf_search_unwind_table
246 #define tdep_find_unwind_table          dwarf_find_unwind_table
247 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
248 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
249 #define tdep_access_reg                 UNW_OBJ(access_reg)
250 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
251 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
252 #define tdep_cache_frame(c,rs)          do {} while(0)
253 #define tdep_reuse_frame(c,rs)          do {} while(0)
254 #define tdep_stash_frame(c,rs)          do {} while(0)
255 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
256
257 #ifdef UNW_LOCAL_ONLY
258 # define tdep_find_proc_info(c,ip,n)                            \
259         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
260                                        (c)->as_arg)
261 # define tdep_put_unwind_info(as,pi,arg)                \
262         dwarf_put_unwind_info((as), (pi), (arg))
263 #else
264 # define tdep_find_proc_info(c,ip,n)                                    \
265         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
266                                        (c)->as_arg)
267 # define tdep_put_unwind_info(as,pi,arg)                \
268         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
269 #endif
270
271 #define tdep_get_as(c)                  ((c)->dwarf.as)
272 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
273 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
274 #define tdep_big_endian(as)             0
275
276 extern int tdep_init_done;
277
278 extern void tdep_init (void);
279 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
280                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
281                                      int need_unwind_info, void *arg);
282 extern void *tdep_uc_addr (ucontext_t *uc, int reg);
283 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
284                                unsigned long *segbase, unsigned long *mapoff,
285                                char *path, size_t pathlen);
286 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
287                             unw_word_t *valp, int write);
288 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
289                               unw_fpreg_t *valp, int write);
290
291 #endif /* X86_LIBUNWIND_I_H */