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