• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdatomic.h>
35 
36 #include "elf32.h"
37 #include "mempool.h"
38 #include "dwarf.h"
39 
40 typedef struct
41   {
42     /* no x86-specific fast trace */
43   }
44 unw_tdep_frame_t;
45 
46 struct unw_addr_space
47   {
48     struct unw_accessors acc;
49     unw_caching_policy_t caching_policy;
50     _Atomic uint32_t cache_generation;
51     unw_word_t dyn_generation;          /* see dyn-common.h */
52     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
53     struct dwarf_rs_cache global_cache;
54     struct unw_debug_frame_list *debug_frames;
55    };
56 
57 struct cursor
58   {
59     struct dwarf_cursor dwarf;          /* must be first */
60 
61     /* Format of sigcontext structure and address at which it is
62        stored: */
63     enum
64       {
65         X86_SCF_NONE,                   /* no signal frame encountered */
66         X86_SCF_LINUX_SIGFRAME,         /* Linux x86 sigcontext */
67         X86_SCF_LINUX_RT_SIGFRAME,      /* POSIX ucontext_t */
68         X86_SCF_FREEBSD_SIGFRAME,       /* FreeBSD x86 sigcontext */
69         X86_SCF_FREEBSD_SIGFRAME4,      /* FreeBSD 4.x x86 sigcontext */
70         X86_SCF_FREEBSD_OSIGFRAME,      /* FreeBSD pre-4.x x86 sigcontext */
71         X86_SCF_FREEBSD_SYSCALL,        /* FreeBSD x86 syscall */
72       }
73     sigcontext_format;
74     unw_word_t sigcontext_addr;
75     int validate;
76     ucontext_t *uc;
77   };
78 
79 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)80 dwarf_get_uc(const struct dwarf_cursor *cursor)
81 {
82   const struct cursor *c = (struct cursor *) cursor->as_arg;
83   return c->uc;
84 }
85 
86 #define DWARF_GET_LOC(l)        ((l).val)
87 
88 #ifdef UNW_LOCAL_ONLY
89 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
90 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
91 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
92 # define DWARF_IS_REG_LOC(l)    0
93 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
94                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
95 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
96 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
97                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
98 
99 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)100 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
101 {
102   if (!DWARF_GET_LOC (loc))
103     return -1;
104   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
105   return 0;
106 }
107 
108 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)109 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
110 {
111   if (!DWARF_GET_LOC (loc))
112     return -1;
113   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
114   return 0;
115 }
116 
117 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)118 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
119 {
120   if (!DWARF_GET_LOC (loc))
121     return -1;
122   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
123                                    0, c->as_arg);
124 }
125 
126 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)127 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
128 {
129   if (!DWARF_GET_LOC (loc))
130     return -1;
131   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
132                                    1, c->as_arg);
133 }
134 
135 #else /* !UNW_LOCAL_ONLY */
136 # define DWARF_LOC_TYPE_FP      (1 << 0)
137 # define DWARF_LOC_TYPE_REG     (1 << 1)
138 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
139 # define DWARF_IS_NULL_LOC(l)                                           \
140                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
141 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
142 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
143 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
144 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
145 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
146 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
147                                                 | DWARF_LOC_TYPE_FP))
148 
149 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)150 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
151 {
152   char *valp = (char *) &val;
153   unw_word_t addr;
154   int ret;
155 
156   if (DWARF_IS_NULL_LOC (loc))
157     return -UNW_EBADREG;
158 
159   if (DWARF_IS_REG_LOC (loc))
160     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
161                                        val, 0, c->as_arg);
162 
163   addr = DWARF_GET_LOC (loc);
164   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
165                                        0, c->as_arg)) < 0)
166     return ret;
167 
168   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
169                                    c->as_arg);
170 }
171 
172 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)173 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
174 {
175   char *valp = (char *) &val;
176   unw_word_t addr;
177   int ret;
178 
179   if (DWARF_IS_NULL_LOC (loc))
180     return -UNW_EBADREG;
181 
182   if (DWARF_IS_REG_LOC (loc))
183     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
184                                        &val, 1, c->as_arg);
185 
186   addr = DWARF_GET_LOC (loc);
187   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
188                                        1, c->as_arg)) < 0)
189     return ret;
190 
191   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
192                                    1, c->as_arg);
193 }
194 
195 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)196 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
197 {
198   if (DWARF_IS_NULL_LOC (loc))
199     return -UNW_EBADREG;
200 
201   /* If a code-generator were to save a value of type unw_word_t in a
202      floating-point register, we would have to support this case.  I
203      suppose it could happen with MMX registers, but does it really
204      happen?  */
205   assert (!DWARF_IS_FP_LOC (loc));
206 
207   if (DWARF_IS_REG_LOC (loc))
208     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
209                                      0, c->as_arg);
210   else
211     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
212                                      0, c->as_arg);
213 }
214 
215 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)216 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
217 {
218   if (DWARF_IS_NULL_LOC (loc))
219     return -UNW_EBADREG;
220 
221   /* If a code-generator were to save a value of type unw_word_t in a
222      floating-point register, we would have to support this case.  I
223      suppose it could happen with MMX registers, but does it really
224      happen?  */
225   assert (!DWARF_IS_FP_LOC (loc));
226 
227   if (DWARF_IS_REG_LOC (loc))
228     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
229                                      1, c->as_arg);
230   else
231     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
232                                      1, c->as_arg);
233 }
234 
235 #endif /* !UNW_LOCAL_ONLY */
236 
237 #define tdep_getcontext_trace           unw_getcontext
238 #define tdep_init_done                  UNW_OBJ(init_done)
239 #define tdep_init                       UNW_OBJ(init)
240 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
241    tdep_search_unwind_table.  */
242 #define tdep_search_unwind_table        dwarf_search_unwind_table
243 #define tdep_find_unwind_table          dwarf_find_unwind_table
244 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
245 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
246 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
247 #define tdep_access_reg                 UNW_OBJ(access_reg)
248 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
249 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
250 #define tdep_cache_frame(c)             0
251 #define tdep_reuse_frame(c,frame)       do {} while(0)
252 #define tdep_stash_frame(c,rs)          do {} while(0)
253 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
254 
255 #ifdef UNW_LOCAL_ONLY
256 # define tdep_find_proc_info(c,ip,n)                            \
257         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
258                                        (c)->as_arg)
259 # define tdep_put_unwind_info(as,pi,arg)                \
260         dwarf_put_unwind_info((as), (pi), (arg))
261 #else
262 # define tdep_find_proc_info(c,ip,n)                                    \
263         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
264                                        (c)->as_arg)
265 # define tdep_put_unwind_info(as,pi,arg)                \
266         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
267 #endif
268 
269 #define tdep_get_as(c)                  ((c)->dwarf.as)
270 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
271 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
272 #define tdep_big_endian(as)             0
273 
274 extern atomic_bool tdep_init_done;
275 
276 extern void tdep_init (void);
277 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
278                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
279                                      int need_unwind_info, void *arg);
280 extern void *tdep_uc_addr (ucontext_t *uc, int reg);
281 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
282                                unsigned long *segbase, unsigned long *mapoff,
283                                char *path, size_t pathlen);
284 extern void tdep_get_exe_image_path (char *path);
285 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
286                             unw_word_t *valp, int write);
287 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
288                               unw_fpreg_t *valp, int write);
289 
290 #endif /* X86_LIBUNWIND_I_H */
291