• 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    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6 
7 This file is part of libunwind.
8 
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16 
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
27 
28 #ifndef X86_64_LIBUNWIND_I_H
29 #define X86_64_LIBUNWIND_I_H
30 
31 /* Target-dependent definitions that are internal to libunwind but need
32    to be shared with target-independent code.  */
33 
34 #include <stdlib.h>
35 #include <libunwind.h>
36 
37 #include "elf64.h"
38 /* ANDROID support update. */
39 #include "map_info.h"
40 /* End of ANDROID update. */
41 #include "mempool.h"
42 #include "dwarf.h"
43 
44 typedef enum
45   {
46     UNW_X86_64_FRAME_STANDARD = -2,     /* regular rbp, rsp +/- offset */
47     UNW_X86_64_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
48     UNW_X86_64_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
49     UNW_X86_64_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
50   }
51 unw_tdep_frame_type_t;
52 
53 typedef struct
54   {
55     uint64_t virtual_address;
56     int64_t frame_type     : 2;  /* unw_tdep_frame_type_t classification */
57     int64_t last_frame     : 1;  /* non-zero if last frame in chain */
58     int64_t cfa_reg_rsp    : 1;  /* cfa dwarf base register is rsp vs. rbp */
59     int64_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
60     int64_t rbp_cfa_offset : 15; /* rbp saved at this offset from cfa (-1 = not saved) */
61     int64_t rsp_cfa_offset : 15; /* rsp saved at this offset from cfa (-1 = not saved) */
62   }
63 unw_tdep_frame_t;
64 
65 struct unw_addr_space
66   {
67     struct unw_accessors acc;
68     unw_caching_policy_t caching_policy;
69 #ifdef HAVE_ATOMIC_OPS_H
70     AO_t cache_generation;
71 #else
72     uint32_t cache_generation;
73 #endif
74     unw_word_t dyn_generation;		/* see dyn-common.h */
75     unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
76     struct dwarf_rs_cache global_cache;
77     struct unw_debug_frame_list *debug_frames;
78     /* ANDROID support update. */
79     struct map_info *map_list;
80     /* End of ANDROID update. */
81    };
82 
83 struct cursor
84   {
85     struct dwarf_cursor dwarf;		/* must be first */
86 
87     unw_tdep_frame_t frame_info;	/* quick tracing assist info */
88 
89     /* Format of sigcontext structure and address at which it is
90        stored: */
91     enum
92       {
93 	X86_64_SCF_NONE,		/* no signal frame encountered */
94 	X86_64_SCF_LINUX_RT_SIGFRAME,	/* Linux ucontext_t */
95 	X86_64_SCF_FREEBSD_SIGFRAME,	/* FreeBSD signal frame */
96 	X86_64_SCF_FREEBSD_SYSCALL,	/* FreeBSD syscall */
97       }
98     sigcontext_format;
99     unw_word_t sigcontext_addr;
100     int validate;
101     ucontext_t *uc;
102   };
103 
104 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)105 dwarf_get_uc(const struct dwarf_cursor *cursor)
106 {
107   const struct cursor *c = (struct cursor *) cursor->as_arg;
108   return c->uc;
109 }
110 
111 #define DWARF_GET_LOC(l)	((l).val)
112 
113 #ifdef UNW_LOCAL_ONLY
114 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
115 # define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
116 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
117 # define DWARF_IS_REG_LOC(l)	0
118 # define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
119 				 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
120 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
121 # define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
122 				 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
123 #else /* !UNW_LOCAL_ONLY */
124 
125 # define DWARF_LOC_TYPE_FP	(1 << 0)
126 # define DWARF_LOC_TYPE_REG	(1 << 1)
127 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
128 # define DWARF_IS_NULL_LOC(l)						\
129 		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
130 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
131 # define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
132 # define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
133 # define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
134 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
135 # define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
136 						| DWARF_LOC_TYPE_FP))
137 
138 #endif /* !UNW_LOCAL_ONLY */
139 
140 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)141 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
142 {
143   if (DWARF_IS_NULL_LOC (loc))
144     return -UNW_EBADREG;
145 
146   abort ();
147 }
148 
149 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)150 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
151 {
152   if (DWARF_IS_NULL_LOC (loc))
153     return -UNW_EBADREG;
154 
155   abort ();
156 }
157 
158 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)159 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
160 {
161   if (DWARF_IS_NULL_LOC (loc))
162     return -UNW_EBADREG;
163 
164   if (DWARF_IS_REG_LOC (loc))
165     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
166 				     0, c->as_arg);
167   else
168     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
169 				     0, c->as_arg);
170 }
171 
172 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)173 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
174 {
175   if (DWARF_IS_NULL_LOC (loc))
176     return -UNW_EBADREG;
177 
178   if (DWARF_IS_REG_LOC (loc))
179     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
180 				     1, c->as_arg);
181   else
182     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
183 				     1, c->as_arg);
184 }
185 
186 #define tdep_getcontext_trace	        UNW_ARCH_OBJ(getcontext_trace)
187 #define tdep_init_done			UNW_OBJ(init_done)
188 #define tdep_init_mem_validate		UNW_OBJ(init_mem_validate)
189 #define tdep_init			UNW_OBJ(init)
190 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
191    tdep_search_unwind_table.  */
192 #define tdep_search_unwind_table	dwarf_search_unwind_table
193 #define tdep_find_unwind_table		dwarf_find_unwind_table
194 #define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
195 #define tdep_access_reg			UNW_OBJ(access_reg)
196 #define tdep_access_fpreg		UNW_OBJ(access_fpreg)
197 #if __linux__
198 # define tdep_fetch_frame		UNW_OBJ(fetch_frame)
199 # define tdep_cache_frame		UNW_OBJ(cache_frame)
200 # define tdep_reuse_frame		UNW_OBJ(reuse_frame)
201 #else
202 # define tdep_fetch_frame(c,ip,n)	do {} while(0)
203 # define tdep_cache_frame(c,rs)		do {} while(0)
204 # define tdep_reuse_frame(c,rs)		do {} while(0)
205 #endif
206 #define tdep_stash_frame		UNW_OBJ(stash_frame)
207 #define tdep_trace			UNW_OBJ(tdep_trace)
208 #define x86_64_r_uc_addr                UNW_OBJ(r_uc_addr)
209 
210 #ifdef UNW_LOCAL_ONLY
211 # define tdep_find_proc_info(c,ip,n)				\
212 	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
213 				       (c)->as_arg)
214 # define tdep_put_unwind_info(as,pi,arg)		\
215 	dwarf_put_unwind_info((as), (pi), (arg))
216 #else
217 # define tdep_find_proc_info(c,ip,n)					\
218 	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
219 				       (c)->as_arg)
220 # define tdep_put_unwind_info(as,pi,arg)			\
221 	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
222 #endif
223 
224 #define tdep_get_as(c)			((c)->dwarf.as)
225 #define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
226 #define tdep_get_ip(c)			((c)->dwarf.ip)
227 #define tdep_big_endian(as)		0
228 
229 extern int tdep_init_done;
230 
231 extern void tdep_init (void);
232 extern void tdep_init_mem_validate (void);
233 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
234 				     unw_dyn_info_t *di, unw_proc_info_t *pi,
235 				     int need_unwind_info, void *arg);
236 extern void *x86_64_r_uc_addr (ucontext_t *uc, int reg);
237 /* ANDROID support update. */
238 extern int tdep_get_elf_image (unw_addr_space_t as, struct elf_image *ei,
239 			       pid_t pid, unw_word_t ip,
240 			       unsigned long *segbase, unsigned long *mapoff,
241 			       char **path, void *as_arg);
242 /* End of ANDROID update. */
243 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
244 			    unw_word_t *valp, int write);
245 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
246 			      unw_fpreg_t *valp, int write);
247 #if __linux__
248 extern void tdep_fetch_frame (struct dwarf_cursor *c, unw_word_t ip,
249 			      int need_unwind_info);
250 extern void tdep_cache_frame (struct dwarf_cursor *c,
251 			      struct dwarf_reg_state *rs);
252 extern void tdep_reuse_frame (struct dwarf_cursor *c,
253 			      struct dwarf_reg_state *rs);
254 extern void tdep_stash_frame (struct dwarf_cursor *c,
255 			      struct dwarf_reg_state *rs);
256 #endif
257 
258 extern int tdep_getcontext_trace (unw_tdep_context_t *);
259 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
260 
261 #endif /* X86_64_LIBUNWIND_I_H */
262