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