Initial import
[external/libunwind.git] / include / libunwind_i.h
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
6 This file is part of libunwind.
7
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:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
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.  */
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 #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>
58 #include <sys/mman.h>
59 #include <elf.h>
60
61 #if defined(HAVE_ENDIAN_H)
62 # include <endian.h>
63 #elif defined(HAVE_SYS_ENDIAN_H)
64 # include <sys/endian.h>
65 #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
75 #ifdef DEBUG
76 # define UNW_DEBUG      1
77 #else
78 # define UNW_DEBUG      0
79 #endif
80
81 /* Make it easy to write thread-safe code which may or may not be
82    linked against libpthread.  The macros below can be used
83    unconditionally and if -lpthread is around, they'll call the
84    corresponding routines otherwise, they do nothing.  */
85
86 #pragma weak pthread_mutex_init
87 #pragma weak pthread_mutex_lock
88 #pragma weak pthread_mutex_unlock
89
90 #define mutex_init(l)                                                   \
91         (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
92 #define mutex_lock(l)                                                   \
93         (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
94 #define mutex_unlock(l)                                                 \
95         (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
96
97 #ifdef HAVE_ATOMIC_OPS_H
98 # include <atomic_ops.h>
99 static inline int
100 cmpxchg_ptr (void *addr, void *old, void *new)
101 {
102   union
103     {
104       void *vp;
105       AO_t *aop;
106     }
107   u;
108
109   u.vp = addr;
110   return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
111 }
112 # define fetch_and_add1(_ptr)           AO_fetch_and_add1(_ptr)
113 # define fetch_and_add(_ptr, value)     AO_fetch_and_add(_ptr, value)
114    /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
115 #  if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
116 #   define HAVE_CMPXCHG
117 #  endif
118 # define HAVE_FETCH_AND_ADD
119 #elif defined(HAVE_SYNC_ATOMICS) || defined(HAVE_IA64INTRIN_H)
120 # ifdef HAVE_IA64INTRIN_H
121 #  include <ia64intrin.h>
122 # endif
123 static inline int
124 cmpxchg_ptr (void *addr, void *old, void *new)
125 {
126   union
127     {
128       void *vp;
129       long *vlp;
130     }
131   u;
132
133   u.vp = addr;
134   return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
135 }
136 # define fetch_and_add1(_ptr)           __sync_fetch_and_add(_ptr, 1)
137 # define fetch_and_add(_ptr, value)     __sync_fetch_and_add(_ptr, value)
138 # define HAVE_CMPXCHG
139 # define HAVE_FETCH_AND_ADD
140 #endif
141 #define atomic_read(ptr)        (*(ptr))
142
143 #define UNWI_OBJ(fn)      UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
144 #define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
145
146 #define unwi_full_mask    UNWI_ARCH_OBJ(full_mask)
147
148 /* Type of a mask that can be used to inhibit preemption.  At the
149    userlevel, preemption is caused by signals and hence sigset_t is
150    appropriate.  In constrast, the Linux kernel uses "unsigned long"
151    to hold the processor "flags" instead.  */
152 typedef sigset_t intrmask_t;
153
154 extern intrmask_t unwi_full_mask;
155
156 /* Silence compiler warnings about variables which are used only if libunwind
157    is configured in a certain way */
158 static inline void mark_as_used(void *v UNUSED) {
159 }
160
161 #if defined(CONFIG_BLOCK_SIGNALS)
162 # define SIGPROCMASK(how, new_mask, old_mask) \
163   sigprocmask((how), (new_mask), (old_mask))
164 #else
165 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
166 #endif
167
168 #define define_lock(name) \
169   pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
170 #define lock_init(l)            mutex_init (l)
171 #define lock_acquire(l,m)                               \
172 do {                                                    \
173   SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m));     \
174   mutex_lock (l);                                       \
175 } while (0)
176 #define lock_release(l,m)                       \
177 do {                                            \
178   mutex_unlock (l);                             \
179   SIGPROCMASK (SIG_SETMASK, &(m), NULL);        \
180 } while (0)
181
182 #define SOS_MEMORY_SIZE 16384   /* see src/mi/mempool.c */
183
184 #ifndef MAP_ANONYMOUS
185 # define MAP_ANONYMOUS MAP_ANON
186 #endif
187 #define GET_MEMORY(mem, size)                                               \
188 do {                                                                        \
189   /* Hopefully, mmap() goes straight through to a system call stub...  */   \
190   mem = mmap (NULL, size, PROT_READ | PROT_WRITE,                           \
191               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);                          \
192   if (mem == MAP_FAILED)                                                    \
193     mem = NULL;                                                             \
194 } while (0)
195
196 #define unwi_find_dynamic_proc_info     UNWI_OBJ(find_dynamic_proc_info)
197 #define unwi_extract_dynamic_proc_info  UNWI_OBJ(extract_dynamic_proc_info)
198 #define unwi_put_dynamic_unwind_info    UNWI_OBJ(put_dynamic_unwind_info)
199 #define unwi_dyn_remote_find_proc_info  UNWI_OBJ(dyn_remote_find_proc_info)
200 #define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
201 #define unwi_dyn_validate_cache         UNWI_OBJ(dyn_validate_cache)
202
203 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
204                                         unw_word_t ip,
205                                         unw_proc_info_t *pi,
206                                         int need_unwind_info, void *arg);
207 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
208                                            unw_word_t ip,
209                                            unw_proc_info_t *pi,
210                                            unw_dyn_info_t *di,
211                                            int need_unwind_info,
212                                            void *arg);
213 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
214                                           unw_proc_info_t *pi, void *arg);
215
216 /* These handle the remote (cross-address-space) case of accessing
217    dynamic unwind info. */
218
219 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
220                                            unw_word_t ip,
221                                            unw_proc_info_t *pi,
222                                            int need_unwind_info,
223                                            void *arg);
224 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
225                                              unw_proc_info_t *pi,
226                                              void *arg);
227 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
228
229 extern unw_dyn_info_list_t _U_dyn_info_list;
230 extern pthread_mutex_t _U_dyn_info_list_lock;
231
232 #if UNW_DEBUG
233 #define unwi_debug_level                UNWI_ARCH_OBJ(debug_level)
234 extern long unwi_debug_level;
235
236 # include <stdio.h>
237 # define Debug(level,format...)                                         \
238 do {                                                                    \
239   if (unwi_debug_level >= level)                                        \
240     {                                                                   \
241       int _n = level;                                                   \
242       if (_n > 16)                                                      \
243         _n = 16;                                                        \
244       fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__);              \
245       fprintf (stderr, format);                                         \
246     }                                                                   \
247 } while (0)
248 # define Dprintf(format...)         fprintf (stderr, format)
249 # ifdef __GNUC__
250 #  undef inline
251 #  define inline        UNUSED
252 # endif
253 #else
254 # define Debug(level,format...)
255 # define Dprintf(format...)
256 #endif
257
258 static ALWAYS_INLINE int
259 print_error (const char *string)
260 {
261   return write (2, string, strlen (string));
262 }
263
264 #define mi_init         UNWI_ARCH_OBJ(mi_init)
265
266 extern void mi_init (void);     /* machine-independent initializations */
267 extern unw_word_t _U_dyn_info_list_addr (void);
268
269 /* This is needed/used by ELF targets only.  */
270
271 struct elf_image
272   {
273     void *image;                /* pointer to mmap'd image */
274     size_t size;                /* (file-) size of the image */
275   };
276
277 struct elf_dyn_info
278   {
279     struct elf_image ei;
280     unw_dyn_info_t di_cache;
281     unw_dyn_info_t di_debug;    /* additional table info for .debug_frame */
282 #if UNW_TARGET_IA64
283     unw_dyn_info_t ktab;
284 #endif
285 #if UNW_TARGET_ARM
286     unw_dyn_info_t di_arm;      /* additional table info for .ARM.exidx */
287 #endif
288   };
289
290 static inline void invalidate_edi (struct elf_dyn_info *edi)
291 {
292   if (edi->ei.image)
293     munmap (edi->ei.image, edi->ei.size);
294   memset (edi, 0, sizeof (*edi));
295   edi->di_cache.format = -1;
296   edi->di_debug.format = -1;
297 #if UNW_TARGET_ARM
298   edi->di_arm.format = -1;
299 #endif
300 }
301
302
303 /* Provide a place holder for architecture to override for fast access
304    to memory when known not to need to validate and know the access
305    will be local to the process. A suitable override will improve
306    unw_tdep_trace() performance in particular. */
307 #define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
308   do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
309   while (0)
310
311 /* Define GNU and processor specific values for the Phdr p_type field in case
312    they aren't defined by <elf.h>.  */
313 #ifndef PT_GNU_EH_FRAME
314 # define PT_GNU_EH_FRAME        0x6474e550
315 #endif /* !PT_GNU_EH_FRAME */
316 #ifndef PT_ARM_EXIDX
317 # define PT_ARM_EXIDX           0x70000001      /* ARM unwind segment */
318 #endif /* !PT_ARM_EXIDX */
319
320 #include "tdep/libunwind_i.h"
321
322 #ifndef tdep_get_func_addr
323 # define tdep_get_func_addr(as,addr,v)          (*(v) = addr, 0)
324 #endif
325
326 #define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
327
328 #endif /* libunwind_i_h */