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