1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2005 Hewlett-Packard Co
3 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
5
6 This file is part of libunwind.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26
27 /* This files contains libunwind-internal definitions which are
28 subject to frequent change and are not to be exposed to
29 libunwind-users. */
30
31 #ifndef libunwind_i_h
32 #define libunwind_i_h
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include "compiler.h"
39
40 #if defined(HAVE___CACHE_PER_THREAD) && HAVE___CACHE_PER_THREAD
41 #define UNWI_DEFAULT_CACHING_POLICY UNW_CACHE_PER_THREAD
42 #else
43 #define UNWI_DEFAULT_CACHING_POLICY UNW_CACHE_GLOBAL
44 #endif
45
46 /* Platform-independent libunwind-internal declarations. */
47
48 #include <sys/types.h> /* HP-UX needs this before include of pthread.h */
49
50 #include <assert.h>
51 #include <libunwind.h>
52 #include <pthread.h>
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/mman.h>
58
59 #if defined(HAVE_ELF_H)
60 # include <elf.h>
61 #elif defined(HAVE_SYS_ELF_H)
62 # include <sys/elf.h>
63 #else
64 # error Could not locate <elf.h>
65 #endif
66 #if defined(ELFCLASS32)
67 # define UNW_ELFCLASS32 ELFCLASS32
68 #else
69 # define UNW_ELFCLASS32 1
70 #endif
71 #if defined(ELFCLASS64)
72 # define UNW_ELFCLASS64 ELFCLASS64
73 #else
74 # define UNW_ELFCLASS64 2
75 #endif
76
77 #if defined(HAVE_ENDIAN_H)
78 # include <endian.h>
79 #elif defined(HAVE_SYS_ENDIAN_H)
80 # include <sys/endian.h>
81 #elif defined(HAVE_SYS_PARAM_H)
82 # include <sys/param.h>
83 #endif
84
85 #if defined(__LITTLE_ENDIAN)
86 # define UNW_LITTLE_ENDIAN __LITTLE_ENDIAN
87 #elif defined(_LITTLE_ENDIAN)
88 # define UNW_LITTLE_ENDIAN _LITTLE_ENDIAN
89 #elif defined(LITTLE_ENDIAN)
90 # define UNW_LITTLE_ENDIAN LITTLE_ENDIAN
91 #else
92 # define UNW_LITTLE_ENDIAN 1234
93 #endif
94
95 #if defined(__BIG_ENDIAN)
96 # define UNW_BIG_ENDIAN __BIG_ENDIAN
97 #elif defined(_BIG_ENDIAN)
98 # define UNW_BIG_ENDIAN _BIG_ENDIAN
99 #elif defined(BIG_ENDIAN)
100 # define UNW_BIG_ENDIAN BIG_ENDIAN
101 #else
102 # define UNW_BIG_ENDIAN 4321
103 #endif
104
105 #if defined(__BYTE_ORDER)
106 # define UNW_BYTE_ORDER __BYTE_ORDER
107 #elif defined(_BYTE_ORDER)
108 # define UNW_BYTE_ORDER _BYTE_ORDER
109 #elif defined(BIG_ENDIAN)
110 # define UNW_BYTE_ORDER BYTE_ORDER
111 #else
112 # if defined(__hpux)
113 # define UNW_BYTE_ORDER UNW_BIG_ENDIAN
114 # else
115 # error Target has unknown byte ordering.
116 # endif
117 #endif
118
119 static inline int
byte_order_is_valid(int byte_order)120 byte_order_is_valid(int byte_order)
121 {
122 return byte_order == UNW_BIG_ENDIAN
123 || byte_order == UNW_LITTLE_ENDIAN;
124 }
125
126 static inline int
byte_order_is_big_endian(int byte_order)127 byte_order_is_big_endian(int byte_order)
128 {
129 return byte_order == UNW_BIG_ENDIAN;
130 }
131
132 static inline int
target_is_big_endian()133 target_is_big_endian()
134 {
135 return byte_order_is_big_endian(UNW_BYTE_ORDER);
136 }
137
138 #if defined(HAVE__BUILTIN_UNREACHABLE)
139 # define unreachable() __builtin_unreachable()
140 #else
141 # define unreachable() do { } while (1)
142 #endif
143
144 #ifdef DEBUG
145 # define UNW_DEBUG 1
146 #else
147 # define UNW_DEBUG 0
148 #endif
149
150 /* Make it easy to write thread-safe code which may or may not be
151 linked against libpthread. The macros below can be used
152 unconditionally and if -lpthread is around, they'll call the
153 corresponding routines otherwise, they do nothing. */
154
155 #pragma weak pthread_mutex_init
156 #pragma weak pthread_mutex_lock
157 #pragma weak pthread_mutex_unlock
158
159 #define mutex_init(l) \
160 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
161 #define mutex_lock(l) \
162 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
163 #define mutex_unlock(l) \
164 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
165
166 #define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
167 #define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
168
169 #define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
170
171 /* Type of a mask that can be used to inhibit preemption. At the
172 userlevel, preemption is caused by signals and hence sigset_t is
173 appropriate. In constrast, the Linux kernel uses "unsigned long"
174 to hold the processor "flags" instead. */
175 typedef sigset_t intrmask_t;
176
177 extern intrmask_t unwi_full_mask;
178
179 /* Silence compiler warnings about variables which are used only if libunwind
180 is configured in a certain way */
mark_as_used(void * v UNUSED)181 static inline void mark_as_used(void *v UNUSED) {
182 }
183
184 #if defined(CONFIG_BLOCK_SIGNALS)
185 # define SIGPROCMASK(how, new_mask, old_mask) \
186 sigprocmask((how), (new_mask), (old_mask))
187 #else
188 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
189 #endif
190
191 /* Prefer adaptive mutexes if available */
192 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
193 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
194 #else
195 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
196 #endif
197
198 #define define_lock(name) \
199 pthread_mutex_t name = UNW_PTHREAD_MUTEX_INITIALIZER
200 #define lock_init(l) mutex_init (l)
201 #define lock_acquire(l,m) \
202 do { \
203 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
204 mutex_lock (l); \
205 } while (0)
206 #define lock_release(l,m) \
207 do { \
208 mutex_unlock (l); \
209 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
210 } while (0)
211
212 #define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
213
214 #ifndef MAP_ANONYMOUS
215 # define MAP_ANONYMOUS MAP_ANON
216 #endif
217 #define GET_MEMORY(mem, size) \
218 do { \
219 /* Hopefully, mmap() goes straight through to a system call stub... */ \
220 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
221 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
222 if (mem == MAP_FAILED) \
223 mem = NULL; \
224 } while (0)
225
226 #define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
227 #define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
228 #define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
229 #define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
230 #define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
231 #define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
232
233 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
234 unw_word_t ip,
235 unw_proc_info_t *pi,
236 int need_unwind_info, void *arg);
237 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
238 unw_word_t ip,
239 unw_proc_info_t *pi,
240 unw_dyn_info_t *di,
241 int need_unwind_info,
242 void *arg);
243 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
244 unw_proc_info_t *pi, void *arg);
245
246 /* These handle the remote (cross-address-space) case of accessing
247 dynamic unwind info. */
248
249 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
250 unw_word_t ip,
251 unw_proc_info_t *pi,
252 int need_unwind_info,
253 void *arg);
254 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
255 unw_proc_info_t *pi,
256 void *arg);
257 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
258
259 extern unw_dyn_info_list_t _U_dyn_info_list;
260 extern pthread_mutex_t _U_dyn_info_list_lock;
261
262 #if UNW_DEBUG
263 #define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
264 extern long unwi_debug_level;
265
266 # include <stdio.h>
267 # define Debug(level, /* format */ ...) \
268 do { \
269 if (unwi_debug_level >= level) \
270 { \
271 int _n = level; \
272 if (_n > 16) \
273 _n = 16; \
274 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
275 fprintf (stderr, /* format */ __VA_ARGS__); \
276 } \
277 } while (0)
278 # define Dprintf(/* format */ ...) \
279 fprintf (stderr, /* format */ __VA_ARGS__)
280 #else
281 # define Debug(level, /* format */ ...)
282 # define Dprintf( /* format */ ...)
283 #endif
284
285 static ALWAYS_INLINE int
print_error(const char * string)286 print_error (const char *string)
287 {
288 return write (2, string, strlen (string));
289 }
290
291 #define mi_init UNWI_ARCH_OBJ(mi_init)
292
293 extern void mi_init (void); /* machine-independent initializations */
294 extern unw_word_t _U_dyn_info_list_addr (void);
295
296 /* This is needed/used by ELF targets only. */
297
298 struct elf_image
299 {
300 void *image; /* pointer to mmap'd image */
301 size_t size; /* (file-) size of the image */
302 };
303
304 struct elf_dyn_info
305 {
306 struct elf_image ei;
307 unw_dyn_info_t di_cache;
308 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
309 #if UNW_TARGET_IA64
310 unw_dyn_info_t ktab;
311 #endif
312 #if UNW_TARGET_ARM
313 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
314 #endif
315 };
316
invalidate_edi(struct elf_dyn_info * edi)317 static inline void invalidate_edi (struct elf_dyn_info *edi)
318 {
319 if (edi->ei.image)
320 munmap (edi->ei.image, edi->ei.size);
321 memset (edi, 0, sizeof (*edi));
322 edi->di_cache.format = -1;
323 edi->di_debug.format = -1;
324 #if UNW_TARGET_ARM
325 edi->di_arm.format = -1;
326 #endif
327 }
328
329
330 /* Provide a place holder for architecture to override for fast access
331 to memory when known not to need to validate and know the access
332 will be local to the process. A suitable override will improve
333 unw_tdep_trace() performance in particular. */
334 #define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
335 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
336 while (0)
337
338 /* Define GNU and processor specific values for the Phdr p_type field in case
339 they aren't defined by <elf.h>. */
340 #ifndef PT_GNU_EH_FRAME
341 # define PT_GNU_EH_FRAME 0x6474e550
342 #endif /* !PT_GNU_EH_FRAME */
343 #ifndef PT_ARM_EXIDX
344 # define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
345 #endif /* !PT_ARM_EXIDX */
346
347 #include "tdep/libunwind_i.h"
348
349 #ifndef tdep_get_func_addr
350 # define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
351 #endif
352
353 #ifndef DWARF_VAL_LOC
354 # define DWARF_IS_VAL_LOC(l) 0
355 # define DWARF_VAL_LOC(c,v) DWARF_NULL_LOC
356 #endif
357
358 #define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
359
360 #endif /* libunwind_i_h */
361