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