blob: 524b30181b9c86a2315fc900f185b207d221ff35 [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>
Matt Fischereac65dc2013-04-15 09:53:29 -050059
60#if defined(HAVE_ELF_H)
61# include <elf.h>
62#elif defined(HAVE_SYS_ELF_H)
63# include <sys/elf.h>
64#else
65# error Could not locate <elf.h>
66#endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +000067
Konstantin Belousov905034c2010-03-06 00:41:37 +020068#if defined(HAVE_ENDIAN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +000069# include <endian.h>
Konstantin Belousov905034c2010-03-06 00:41:37 +020070#elif defined(HAVE_SYS_ENDIAN_H)
71# include <sys/endian.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000072#else
73# define __LITTLE_ENDIAN 1234
74# define __BIG_ENDIAN 4321
75# if defined(__hpux)
76# define __BYTE_ORDER __BIG_ENDIAN
Matt Fischereac65dc2013-04-15 09:53:29 -050077# elif defined(__QNX__)
78# if defined(__BIGENDIAN__)
79# define __BYTE_ORDER __BIG_ENDIAN
80# elif defined(__LITTLEENDIAN__)
81# define __BYTE_ORDER __LITTLE_ENDIAN
82# else
83# error Host has unknown byte-order.
84# endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +000085# else
86# error Host has unknown byte-order.
87# endif
88#endif
89
Ladislav Michl10b064f2012-11-13 11:19:47 +010090#if defined(HAVE__BUILTIN_UNREACHABLE)
91# define unreachable() __builtin_unreachable()
92#else
93# define unreachable() do { } while (1)
94#endif
95
hp.com!davidmd5ab8982005-05-20 11:28:16 +000096#ifdef DEBUG
97# define UNW_DEBUG 1
98#else
99# define UNW_DEBUG 0
100#endif
101
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000102/* 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. */
106
107#pragma weak pthread_mutex_init
108#pragma weak pthread_mutex_lock
109#pragma weak pthread_mutex_unlock
110
111#define mutex_init(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300112 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000113#define mutex_lock(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300114 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000115#define mutex_unlock(l) \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300116 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000117
118#ifdef HAVE_ATOMIC_OPS_H
119# include <atomic_ops.h>
120static inline int
121cmpxchg_ptr (void *addr, void *old, void *new)
122{
123 union
124 {
125 void *vp;
126 AO_t *aop;
127 }
128 u;
129
130 u.vp = addr;
131 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
132}
133# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
Tommi Rantala9a3565d2012-09-19 14:00:20 +0300134# define fetch_and_add(_ptr, value) AO_fetch_and_add(_ptr, value)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000135 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
136# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
137# define HAVE_CMPXCHG
138# endif
Tommi Rantala9a3565d2012-09-19 14:00:20 +0300139# define HAVE_FETCH_AND_ADD
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300140#elif defined(HAVE_SYNC_ATOMICS) || defined(HAVE_IA64INTRIN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000141# ifdef HAVE_IA64INTRIN_H
142# include <ia64intrin.h>
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300143# endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000144static inline int
145cmpxchg_ptr (void *addr, void *old, void *new)
146{
147 union
148 {
149 void *vp;
150 long *vlp;
151 }
152 u;
153
154 u.vp = addr;
155 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
156}
Tommi Rantalac2d6f852012-09-06 15:42:35 +0300157# 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
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000161#endif
162#define atomic_read(ptr) (*(ptr))
163
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)
166
167#define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
168
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. */
173typedef sigset_t intrmask_t;
174
175extern intrmask_t unwi_full_mask;
176
Arun Sharma491d5762009-10-16 14:01:50 -0700177/* Silence compiler warnings about variables which are used only if libunwind
178 is configured in a certain way */
Tommi Rantalaa15874f2012-02-14 15:37:55 +0200179static inline void mark_as_used(void *v UNUSED) {
Arun Sharma491d5762009-10-16 14:01:50 -0700180}
181
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700182#if defined(CONFIG_BLOCK_SIGNALS)
Arun Sharmaaf9daf62009-10-15 19:29:49 -0700183# define SIGPROCMASK(how, new_mask, old_mask) \
184 sigprocmask((how), (new_mask), (old_mask))
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700185#else
Arun Sharma4ab26bc2009-10-16 15:52:44 -0700186# define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700187#endif
188
Christopher Ferrisb8627d92014-02-19 16:49:00 -0800189/* ANDROID support update. */
190#define lock_var(name) \
191 pthread_mutex_t name
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000192#define define_lock(name) \
Christopher Ferrisb8627d92014-02-19 16:49:00 -0800193 lock_var (name) = PTHREAD_MUTEX_INITIALIZER
194/* End of ANDROID update. */
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000195#define lock_init(l) mutex_init (l)
196#define lock_acquire(l,m) \
197do { \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700198 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000199 mutex_lock (l); \
200} while (0)
201#define lock_release(l,m) \
202do { \
203 mutex_unlock (l); \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700204 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000205} while (0)
206
207#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
208
Konstantin Belousov8ccebc92010-03-06 16:23:24 +0200209#ifndef MAP_ANONYMOUS
210# define MAP_ANONYMOUS MAP_ANON
211#endif
Arun Sharmac0a9d0c2011-01-23 18:06:51 -0800212#define GET_MEMORY(mem, size) \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000213do { \
214 /* Hopefully, mmap() goes straight through to a system call stub... */ \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300215 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
216 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000217 if (mem == MAP_FAILED) \
218 mem = NULL; \
219} while (0)
220
221#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
222#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
223#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
224#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
225#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
226#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
227
228extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
229 unw_word_t ip,
230 unw_proc_info_t *pi,
231 int need_unwind_info, void *arg);
232extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
233 unw_word_t ip,
234 unw_proc_info_t *pi,
235 unw_dyn_info_t *di,
236 int need_unwind_info,
237 void *arg);
238extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
239 unw_proc_info_t *pi, void *arg);
240
241/* These handle the remote (cross-address-space) case of accessing
242 dynamic unwind info. */
243
244extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
245 unw_word_t ip,
246 unw_proc_info_t *pi,
247 int need_unwind_info,
248 void *arg);
249extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
250 unw_proc_info_t *pi,
251 void *arg);
252extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
253
254extern unw_dyn_info_list_t _U_dyn_info_list;
255extern pthread_mutex_t _U_dyn_info_list_lock;
256
257#if UNW_DEBUG
Christopher Ferris76dbee52014-02-12 21:12:09 -0800258# define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000259extern long unwi_debug_level;
260
Christopher Ferris76dbee52014-02-12 21:12:09 -0800261# ifdef ANDROID
262# include <log/log.h>
263# define LOG_TAG "libunwind"
264
265# define Debug(level, format, ...) \
266do { \
267 if (unwi_debug_level >= (level)) \
268 { \
269 ALOGI("%*c>%s: " format, ((level) <= 16) ? (level) : 16, ' ', \
270 __FUNCTION__, ##__VA_ARGS__); \
271 } \
272} while (0)
273# define Dprintf(format, ...) ALOGI(format, ##__VA_ARGS__);
274#else
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000275# include <stdio.h>
276# define Debug(level,format...) \
277do { \
278 if (unwi_debug_level >= level) \
279 { \
280 int _n = level; \
281 if (_n > 16) \
282 _n = 16; \
283 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
284 fprintf (stderr, format); \
285 } \
286} while (0)
Arun Sharmaec53de82009-03-16 18:42:27 +0000287# define Dprintf(format...) fprintf (stderr, format)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000288# ifdef __GNUC__
289# undef inline
290# define inline UNUSED
291# endif
Christopher Ferris76dbee52014-02-12 21:12:09 -0800292# endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000293#else
294# define Debug(level,format...)
Arun Sharmaec53de82009-03-16 18:42:27 +0000295# define Dprintf(format...)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000296#endif
297
Arun Sharma491d5762009-10-16 14:01:50 -0700298static ALWAYS_INLINE int
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000299print_error (const char *string)
300{
Arun Sharma491d5762009-10-16 14:01:50 -0700301 return write (2, string, strlen (string));
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000302}
303
304#define mi_init UNWI_ARCH_OBJ(mi_init)
305
306extern void mi_init (void); /* machine-independent initializations */
307extern unw_word_t _U_dyn_info_list_addr (void);
308
309/* This is needed/used by ELF targets only. */
310
311struct elf_image
312 {
313 void *image; /* pointer to mmap'd image */
314 size_t size; /* (file-) size of the image */
315 };
316
Arun Sharma25ee9f82012-03-12 18:45:08 -0700317struct elf_dyn_info
318 {
Christopher Ferris16b95a62014-01-22 16:07:26 -0800319 /* ANDROID support update.*/
320 /* Removed: struct elf_image ei; */
321 /* End of ANDROID update. */
Arun Sharma25ee9f82012-03-12 18:45:08 -0700322 unw_dyn_info_t di_cache;
323 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
324#if UNW_TARGET_IA64
325 unw_dyn_info_t ktab;
326#endif
327#if UNW_TARGET_ARM
328 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
329#endif
330 };
331
Tommi Rantala18c26d42012-08-12 21:24:35 +0300332static inline void invalidate_edi (struct elf_dyn_info *edi)
Arun Sharma25ee9f82012-03-12 18:45:08 -0700333{
Christopher Ferris16b95a62014-01-22 16:07:26 -0800334 /* ANDROID support update.*/
335 /* Removed: if (edi->ei.image) */
336 /* munmap (edi->ei.image, edi->ei.size); */
337 /* End of ANDROID update. */
Arun Sharma25ee9f82012-03-12 18:45:08 -0700338 memset (edi, 0, sizeof (*edi));
339 edi->di_cache.format = -1;
340 edi->di_debug.format = -1;
341#if UNW_TARGET_ARM
342 edi->di_arm.format = -1;
343#endif
344}
345
346
Lassi Tuuraae5c1f22011-04-17 20:33:09 -0700347/* Provide a place holder for architecture to override for fast access
348 to memory when known not to need to validate and know the access
349 will be local to the process. A suitable override will improve
350 unw_tdep_trace() performance in particular. */
351#define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
352 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
353 while (0)
354
Ken Werner91494b72011-10-25 15:19:49 +0000355/* Define GNU and processor specific values for the Phdr p_type field in case
356 they aren't defined by <elf.h>. */
357#ifndef PT_GNU_EH_FRAME
358# define PT_GNU_EH_FRAME 0x6474e550
359#endif /* !PT_GNU_EH_FRAME */
360#ifndef PT_ARM_EXIDX
361# define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
362#endif /* !PT_ARM_EXIDX */
363
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000364#include "tdep/libunwind_i.h"
365
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600366#ifndef tdep_get_func_addr
367# define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
368#endif
369
Tommi Rantalac2f75742012-09-04 17:04:14 +0300370#define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
371
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000372#endif /* libunwind_i_h */