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