blob: f5082fd2b5b9c0f228ef15a43f2257b62acac29c [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
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000189#define define_lock(name) \
190 pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
191#define lock_init(l) mutex_init (l)
192#define lock_acquire(l,m) \
193do { \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700194 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000195 mutex_lock (l); \
196} while (0)
197#define lock_release(l,m) \
198do { \
199 mutex_unlock (l); \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700200 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000201} while (0)
202
203#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
204
Konstantin Belousov8ccebc92010-03-06 16:23:24 +0200205#ifndef MAP_ANONYMOUS
206# define MAP_ANONYMOUS MAP_ANON
207#endif
Arun Sharmac0a9d0c2011-01-23 18:06:51 -0800208#define GET_MEMORY(mem, size) \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000209do { \
210 /* Hopefully, mmap() goes straight through to a system call stub... */ \
Tommi Rantala890e23e2012-09-21 08:38:52 +0300211 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
212 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000213 if (mem == MAP_FAILED) \
214 mem = NULL; \
215} while (0)
216
217#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
218#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
219#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
220#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
221#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
222#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
223
224extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
225 unw_word_t ip,
226 unw_proc_info_t *pi,
227 int need_unwind_info, void *arg);
228extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
229 unw_word_t ip,
230 unw_proc_info_t *pi,
231 unw_dyn_info_t *di,
232 int need_unwind_info,
233 void *arg);
234extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
235 unw_proc_info_t *pi, void *arg);
236
237/* These handle the remote (cross-address-space) case of accessing
238 dynamic unwind info. */
239
240extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
241 unw_word_t ip,
242 unw_proc_info_t *pi,
243 int need_unwind_info,
244 void *arg);
245extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
246 unw_proc_info_t *pi,
247 void *arg);
248extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
249
250extern unw_dyn_info_list_t _U_dyn_info_list;
251extern pthread_mutex_t _U_dyn_info_list_lock;
252
253#if UNW_DEBUG
Christopher Ferris76dbee52014-02-12 21:12:09 -0800254# define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000255extern long unwi_debug_level;
256
Christopher Ferris76dbee52014-02-12 21:12:09 -0800257# ifdef ANDROID
258# include <log/log.h>
259# define LOG_TAG "libunwind"
260
261# define Debug(level, format, ...) \
262do { \
263 if (unwi_debug_level >= (level)) \
264 { \
265 ALOGI("%*c>%s: " format, ((level) <= 16) ? (level) : 16, ' ', \
266 __FUNCTION__, ##__VA_ARGS__); \
267 } \
268} while (0)
269# define Dprintf(format, ...) ALOGI(format, ##__VA_ARGS__);
270#else
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000271# include <stdio.h>
272# define Debug(level,format...) \
273do { \
274 if (unwi_debug_level >= level) \
275 { \
276 int _n = level; \
277 if (_n > 16) \
278 _n = 16; \
279 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
280 fprintf (stderr, format); \
281 } \
282} while (0)
Arun Sharmaec53de82009-03-16 18:42:27 +0000283# define Dprintf(format...) fprintf (stderr, format)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000284# ifdef __GNUC__
285# undef inline
286# define inline UNUSED
287# endif
Christopher Ferris76dbee52014-02-12 21:12:09 -0800288# endif
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000289#else
290# define Debug(level,format...)
Arun Sharmaec53de82009-03-16 18:42:27 +0000291# define Dprintf(format...)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000292#endif
293
Arun Sharma491d5762009-10-16 14:01:50 -0700294static ALWAYS_INLINE int
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000295print_error (const char *string)
296{
Arun Sharma491d5762009-10-16 14:01:50 -0700297 return write (2, string, strlen (string));
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000298}
299
300#define mi_init UNWI_ARCH_OBJ(mi_init)
301
302extern void mi_init (void); /* machine-independent initializations */
303extern unw_word_t _U_dyn_info_list_addr (void);
304
305/* This is needed/used by ELF targets only. */
306
307struct elf_image
308 {
309 void *image; /* pointer to mmap'd image */
310 size_t size; /* (file-) size of the image */
311 };
312
Arun Sharma25ee9f82012-03-12 18:45:08 -0700313struct elf_dyn_info
314 {
Christopher Ferris16b95a62014-01-22 16:07:26 -0800315 /* ANDROID support update.*/
316 /* Removed: struct elf_image ei; */
317 /* End of ANDROID update. */
Arun Sharma25ee9f82012-03-12 18:45:08 -0700318 unw_dyn_info_t di_cache;
319 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
320#if UNW_TARGET_IA64
321 unw_dyn_info_t ktab;
322#endif
323#if UNW_TARGET_ARM
324 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
325#endif
326 };
327
Tommi Rantala18c26d42012-08-12 21:24:35 +0300328static inline void invalidate_edi (struct elf_dyn_info *edi)
Arun Sharma25ee9f82012-03-12 18:45:08 -0700329{
Christopher Ferris16b95a62014-01-22 16:07:26 -0800330 /* ANDROID support update.*/
331 /* Removed: if (edi->ei.image) */
332 /* munmap (edi->ei.image, edi->ei.size); */
333 /* End of ANDROID update. */
Arun Sharma25ee9f82012-03-12 18:45:08 -0700334 memset (edi, 0, sizeof (*edi));
335 edi->di_cache.format = -1;
336 edi->di_debug.format = -1;
337#if UNW_TARGET_ARM
338 edi->di_arm.format = -1;
339#endif
340}
341
342
Lassi Tuuraae5c1f22011-04-17 20:33:09 -0700343/* Provide a place holder for architecture to override for fast access
344 to memory when known not to need to validate and know the access
345 will be local to the process. A suitable override will improve
346 unw_tdep_trace() performance in particular. */
347#define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
348 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
349 while (0)
350
Ken Werner91494b72011-10-25 15:19:49 +0000351/* Define GNU and processor specific values for the Phdr p_type field in case
352 they aren't defined by <elf.h>. */
353#ifndef PT_GNU_EH_FRAME
354# define PT_GNU_EH_FRAME 0x6474e550
355#endif /* !PT_GNU_EH_FRAME */
356#ifndef PT_ARM_EXIDX
357# define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
358#endif /* !PT_ARM_EXIDX */
359
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000360#include "tdep/libunwind_i.h"
361
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600362#ifndef tdep_get_func_addr
363# define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
364#endif
365
Tommi Rantalac2f75742012-09-04 17:04:14 +0300366#define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
367
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000368#endif /* libunwind_i_h */