blob: 966a3e36517f10d2bfa9e25d3eb89b51bf3444f8 [file] [log] [blame]
hp.com!davidmd5ab8982005-05-20 11:28:16 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2005 Hewlett-Packard Co
David Mosberger-Tange6b9f352007-08-22 13:02:09 -06003 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
hp.com!davidmd5ab8982005-05-20 11:28:16 +00005
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
Tommi Rantaladc680c02012-09-19 12:12:16 +030038#include "compiler.h"
39
hp.com!davidmd5ab8982005-05-20 11:28:16 +000040#ifdef HAVE___THREAD
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
43 all. */
44# undef HAVE___THREAD
45#endif
46
47/* Platform-independent libunwind-internal declarations. */
48
49#include <sys/types.h> /* HP-UX needs this before include of pthread.h */
50
51#include <assert.h>
52#include <libunwind.h>
53#include <pthread.h>
54#include <signal.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
Ken Werner2c865b62011-10-20 16:51:45 +000058#include <sys/mman.h>
Ken Werner91494b72011-10-25 15:19:49 +000059#include <elf.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000060
Konstantin Belousov905034c2010-03-06 00:41:37 +020061#if defined(HAVE_ENDIAN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +000062# include <endian.h>
Konstantin Belousov905034c2010-03-06 00:41:37 +020063#elif defined(HAVE_SYS_ENDIAN_H)
64# include <sys/endian.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000065#else
66# define __LITTLE_ENDIAN 1234
67# define __BIG_ENDIAN 4321
68# if defined(__hpux)
69# define __BYTE_ORDER __BIG_ENDIAN
70# else
71# error Host has unknown byte-order.
72# endif
73#endif
74
Ladislav Michl10b064f2012-11-13 11:19:47 +010075#if defined(HAVE__BUILTIN_UNREACHABLE)
76# define unreachable() __builtin_unreachable()
77#else
78# define unreachable() do { } while (1)
79#endif
80
hp.com!davidmd5ab8982005-05-20 11:28:16 +000081#ifdef DEBUG
82# define UNW_DEBUG 1
83#else
84# define UNW_DEBUG 0
85#endif
86
hp.com!davidmd5ab8982005-05-20 11:28:16 +000087/* Make it easy to write thread-safe code which may or may not be
88 linked against libpthread. The macros below can be used
89 unconditionally and if -lpthread is around, they'll call the
90 corresponding routines otherwise, they do nothing. */
91
92#pragma weak pthread_mutex_init
93#pragma weak pthread_mutex_lock
94#pragma weak pthread_mutex_unlock
95
96#define mutex_init(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +030097 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +000098#define mutex_lock(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +030099 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000100#define mutex_unlock(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300101 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000102
103#ifdef HAVE_ATOMIC_OPS_H
104# include <atomic_ops.h>
105static inline int
106cmpxchg_ptr (void *addr, void *old, void *new)
107{
108 union
109 {
110 void *vp;
111 AO_t *aop;
112 }
113 u;
114
115 u.vp = addr;
116 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
117}
118# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
Tommi Rantala9a3565d2012-09-19 14:00:20 +0300119# define fetch_and_add(_ptr, value) AO_fetch_and_add(_ptr, value)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000120 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
121# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
122# define HAVE_CMPXCHG
123# endif
Tommi Rantala9a3565d2012-09-19 14:00:20 +0300124# define HAVE_FETCH_AND_ADD
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300125#elif defined(HAVE_SYNC_ATOMICS) || defined(HAVE_IA64INTRIN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000126# ifdef HAVE_IA64INTRIN_H
127# include <ia64intrin.h>
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300128# endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000129static inline int
130cmpxchg_ptr (void *addr, void *old, void *new)
131{
132 union
133 {
134 void *vp;
135 long *vlp;
136 }
137 u;
138
139 u.vp = addr;
140 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
141}
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300142# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
143# define fetch_and_add(_ptr, value) __sync_fetch_and_add(_ptr, value)
144# define HAVE_CMPXCHG
145# define HAVE_FETCH_AND_ADD
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000146#endif
147#define atomic_read(ptr) (*(ptr))
148
149#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
150#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
151
152#define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
153
154/* Type of a mask that can be used to inhibit preemption. At the
155 userlevel, preemption is caused by signals and hence sigset_t is
156 appropriate. In constrast, the Linux kernel uses "unsigned long"
157 to hold the processor "flags" instead. */
158typedef sigset_t intrmask_t;
159
160extern intrmask_t unwi_full_mask;
161
Arun Sharma491d5762009-10-16 14:01:50 -0700162/* Silence compiler warnings about variables which are used only if libunwind
163 is configured in a certain way */
Tommi Rantalaa15874f2012-02-14 15:37:55 +0200164static inline void mark_as_used(void *v UNUSED) {
Arun Sharma491d5762009-10-16 14:01:50 -0700165}
166
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700167#if defined(CONFIG_BLOCK_SIGNALS)
Arun Sharmaaf9daf62009-10-15 19:29:49 -0700168# define SIGPROCMASK(how, new_mask, old_mask) \
169 sigprocmask((how), (new_mask), (old_mask))
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700170#else
Arun Sharma4ab26bc2009-10-16 15:52:44 -0700171# define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700172#endif
173
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000174#define define_lock(name) \
175 pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
176#define lock_init(l) mutex_init (l)
177#define lock_acquire(l,m) \
178do { \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700179 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000180 mutex_lock (l); \
181} while (0)
182#define lock_release(l,m) \
183do { \
184 mutex_unlock (l); \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700185 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000186} while (0)
187
188#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
189
Konstantin Belousov8ccebc92010-03-06 16:23:24 +0200190#ifndef MAP_ANONYMOUS
191# define MAP_ANONYMOUS MAP_ANON
192#endif
Arun Sharmac0a9d0c2011-01-23 18:06:51 -0800193#define GET_MEMORY(mem, size) \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000194do { \
195 /* Hopefully, mmap() goes straight through to a system call stub... */ \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300196 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
197 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000198 if (mem == MAP_FAILED) \
199 mem = NULL; \
200} while (0)
201
202#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
203#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
204#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
205#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
206#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
207#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
208
209extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
210 unw_word_t ip,
211 unw_proc_info_t *pi,
212 int need_unwind_info, void *arg);
213extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
214 unw_word_t ip,
215 unw_proc_info_t *pi,
216 unw_dyn_info_t *di,
217 int need_unwind_info,
218 void *arg);
219extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
220 unw_proc_info_t *pi, void *arg);
221
222/* These handle the remote (cross-address-space) case of accessing
223 dynamic unwind info. */
224
225extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
226 unw_word_t ip,
227 unw_proc_info_t *pi,
228 int need_unwind_info,
229 void *arg);
230extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
231 unw_proc_info_t *pi,
232 void *arg);
233extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
234
235extern unw_dyn_info_list_t _U_dyn_info_list;
236extern pthread_mutex_t _U_dyn_info_list_lock;
237
238#if UNW_DEBUG
239#define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
240extern long unwi_debug_level;
241
242# include <stdio.h>
243# define Debug(level,format...) \
244do { \
245 if (unwi_debug_level >= level) \
246 { \
247 int _n = level; \
248 if (_n > 16) \
249 _n = 16; \
250 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
251 fprintf (stderr, format); \
252 } \
253} while (0)
Arun Sharmaec53de82009-03-16 18:42:27 +0000254# define Dprintf(format...) fprintf (stderr, format)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000255# ifdef __GNUC__
256# undef inline
257# define inline UNUSED
258# endif
259#else
260# define Debug(level,format...)
Arun Sharmaec53de82009-03-16 18:42:27 +0000261# define Dprintf(format...)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000262#endif
263
Arun Sharma491d5762009-10-16 14:01:50 -0700264static ALWAYS_INLINE int
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000265print_error (const char *string)
266{
Arun Sharma491d5762009-10-16 14:01:50 -0700267 return write (2, string, strlen (string));
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000268}
269
270#define mi_init UNWI_ARCH_OBJ(mi_init)
271
272extern void mi_init (void); /* machine-independent initializations */
273extern unw_word_t _U_dyn_info_list_addr (void);
274
275/* This is needed/used by ELF targets only. */
276
277struct elf_image
278 {
279 void *image; /* pointer to mmap'd image */
280 size_t size; /* (file-) size of the image */
281 };
282
Arun Sharma25ee9f82012-03-12 18:45:08 -0700283struct elf_dyn_info
284 {
285 struct elf_image ei;
286 unw_dyn_info_t di_cache;
287 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
288#if UNW_TARGET_IA64
289 unw_dyn_info_t ktab;
290#endif
291#if UNW_TARGET_ARM
292 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
293#endif
294 };
295
Tommi Rantala18c26d42012-08-12 21:24:35 +0300296static inline void invalidate_edi (struct elf_dyn_info *edi)
Arun Sharma25ee9f82012-03-12 18:45:08 -0700297{
298 if (edi->ei.image)
299 munmap (edi->ei.image, edi->ei.size);
300 memset (edi, 0, sizeof (*edi));
301 edi->di_cache.format = -1;
302 edi->di_debug.format = -1;
303#if UNW_TARGET_ARM
304 edi->di_arm.format = -1;
305#endif
306}
307
308
Lassi Tuuraae5c1f22011-04-17 20:33:09 -0700309/* Provide a place holder for architecture to override for fast access
310 to memory when known not to need to validate and know the access
311 will be local to the process. A suitable override will improve
312 unw_tdep_trace() performance in particular. */
313#define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
314 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
315 while (0)
316
Ken Werner91494b72011-10-25 15:19:49 +0000317/* Define GNU and processor specific values for the Phdr p_type field in case
318 they aren't defined by <elf.h>. */
319#ifndef PT_GNU_EH_FRAME
320# define PT_GNU_EH_FRAME 0x6474e550
321#endif /* !PT_GNU_EH_FRAME */
322#ifndef PT_ARM_EXIDX
323# define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
324#endif /* !PT_ARM_EXIDX */
325
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000326#include "tdep/libunwind_i.h"
327
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600328#ifndef tdep_get_func_addr
329# define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
330#endif
331
Tommi Rantalac2f75742012-09-04 17:04:14 +0300332#define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
333
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000334#endif /* libunwind_i_h */