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