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>
6 This file is part of libunwind.
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:
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
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. */
27 /* This files contains libunwind-internal definitions which are
28 subject to frequent change and are not to be exposed to
41 /* For now, turn off per-thread caching. It uses up too much TLS
42 memory per thread even when the thread never uses libunwind at
47 /* Platform-independent libunwind-internal declarations. */
49 #include <sys/types.h> /* HP-UX needs this before include of pthread.h */
52 #include <libunwind.h>
60 #if defined(HAVE_ELF_H)
62 #elif defined(HAVE_SYS_ELF_H)
65 # error Could not locate <elf.h>
68 #if defined(HAVE_ENDIAN_H)
70 #elif defined(HAVE_SYS_ENDIAN_H)
71 # include <sys/endian.h>
73 # define __LITTLE_ENDIAN 1234
74 # define __BIG_ENDIAN 4321
76 # define __BYTE_ORDER __BIG_ENDIAN
77 # elif defined(__QNX__)
78 # if defined(__BIGENDIAN__)
79 # define __BYTE_ORDER __BIG_ENDIAN
80 # elif defined(__LITTLEENDIAN__)
81 # define __BYTE_ORDER __LITTLE_ENDIAN
83 # error Host has unknown byte-order.
86 # error Host has unknown byte-order.
90 #if defined(HAVE__BUILTIN_UNREACHABLE)
91 # define unreachable() __builtin_unreachable()
93 # define unreachable() do { } while (1)
102 /* Make it easy to write thread-safe code which may or may not be
103 linked against libpthread. The macros below can be used
104 unconditionally and if -lpthread is around, they'll call the
105 corresponding routines otherwise, they do nothing. */
107 #pragma weak pthread_mutex_init
108 #pragma weak pthread_mutex_lock
109 #pragma weak pthread_mutex_unlock
111 #define mutex_init(l) \
112 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
113 #define mutex_lock(l) \
114 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
115 #define mutex_unlock(l) \
116 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
118 #ifdef HAVE_ATOMIC_OPS_H
119 # include <atomic_ops.h>
121 cmpxchg_ptr (void *addr, void *old, void *new)
131 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
133 # define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
134 # define fetch_and_add(_ptr, value) AO_fetch_and_add(_ptr, value)
135 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
136 # if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
137 # define HAVE_CMPXCHG
139 # define HAVE_FETCH_AND_ADD
140 #elif defined(HAVE_SYNC_ATOMICS) || defined(HAVE_IA64INTRIN_H)
141 # ifdef HAVE_IA64INTRIN_H
142 # include <ia64intrin.h>
145 cmpxchg_ptr (void *addr, void *old, void *new)
155 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
157 # define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
158 # define fetch_and_add(_ptr, value) __sync_fetch_and_add(_ptr, value)
159 # define HAVE_CMPXCHG
160 # define HAVE_FETCH_AND_ADD
162 #define atomic_read(ptr) (*(ptr))
164 #define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
165 #define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
167 #define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
169 /* Type of a mask that can be used to inhibit preemption. At the
170 userlevel, preemption is caused by signals and hence sigset_t is
171 appropriate. In constrast, the Linux kernel uses "unsigned long"
172 to hold the processor "flags" instead. */
173 typedef sigset_t intrmask_t;
175 extern intrmask_t unwi_full_mask;
177 /* Silence compiler warnings about variables which are used only if libunwind
178 is configured in a certain way */
179 static inline void mark_as_used(void *v UNUSED) {
182 #if defined(CONFIG_BLOCK_SIGNALS)
183 # define SIGPROCMASK(how, new_mask, old_mask) \
184 sigprocmask((how), (new_mask), (old_mask))
186 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
189 /* Prefer adaptive mutexes if available */
190 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
191 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
193 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
196 #define define_lock(name) \
197 pthread_mutex_t name = UNW_PTHREAD_MUTEX_INITIALIZER
198 #define lock_init(l) mutex_init (l)
199 #define lock_acquire(l,m) \
201 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
204 #define lock_release(l,m) \
207 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
210 #define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
212 #ifndef MAP_ANONYMOUS
213 # define MAP_ANONYMOUS MAP_ANON
215 #define GET_MEMORY(mem, size) \
217 /* Hopefully, mmap() goes straight through to a system call stub... */ \
218 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
219 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
220 if (mem == MAP_FAILED) \
224 #define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
225 #define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
226 #define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
227 #define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
228 #define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
229 #define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
231 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
234 int need_unwind_info, void *arg);
235 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
239 int need_unwind_info,
241 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
242 unw_proc_info_t *pi, void *arg);
244 /* These handle the remote (cross-address-space) case of accessing
245 dynamic unwind info. */
247 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
250 int need_unwind_info,
252 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
255 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
257 extern unw_dyn_info_list_t _U_dyn_info_list;
258 extern pthread_mutex_t _U_dyn_info_list_lock;
261 #define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
262 extern long unwi_debug_level;
265 # define Debug(level,format...) \
267 if (unwi_debug_level >= level) \
272 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
273 fprintf (stderr, format); \
276 # define Dprintf(format...) fprintf (stderr, format)
278 # define Debug(level,format...)
279 # define Dprintf(format...)
282 static ALWAYS_INLINE int
283 print_error (const char *string)
285 return write (2, string, strlen (string));
288 #define mi_init UNWI_ARCH_OBJ(mi_init)
290 extern void mi_init (void); /* machine-independent initializations */
291 extern unw_word_t _U_dyn_info_list_addr (void);
293 /* This is needed/used by ELF targets only. */
297 void *image; /* pointer to mmap'd image */
298 size_t size; /* (file-) size of the image */
304 unw_dyn_info_t di_cache;
305 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
310 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
314 static inline void invalidate_edi (struct elf_dyn_info *edi)
317 munmap (edi->ei.image, edi->ei.size);
318 memset (edi, 0, sizeof (*edi));
319 edi->di_cache.format = -1;
320 edi->di_debug.format = -1;
322 edi->di_arm.format = -1;
327 /* Provide a place holder for architecture to override for fast access
328 to memory when known not to need to validate and know the access
329 will be local to the process. A suitable override will improve
330 unw_tdep_trace() performance in particular. */
331 #define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
332 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
335 /* Define GNU and processor specific values for the Phdr p_type field in case
336 they aren't defined by <elf.h>. */
337 #ifndef PT_GNU_EH_FRAME
338 # define PT_GNU_EH_FRAME 0x6474e550
339 #endif /* !PT_GNU_EH_FRAME */
341 # define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
342 #endif /* !PT_ARM_EXIDX */
344 #include "tdep/libunwind_i.h"
346 #ifndef tdep_get_func_addr
347 # define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
350 #ifndef DWARF_VAL_LOC
351 # define DWARF_IS_VAL_LOC(l) 0
352 # define DWARF_VAL_LOC(c,v) DWARF_NULL_LOC
355 #define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
357 #endif /* libunwind_i_h */