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 #if defined(HAVE___CACHE_PER_THREAD) && HAVE___CACHE_PER_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 #include <errno.h>
59 #include <stdio.h>
60 #ifdef PARSE_BUILD_ID
61 #include <link.h>
62 #endif
63
64 #if defined(HAVE_ELF_H)
65 # include <elf.h>
66 #elif defined(HAVE_SYS_ELF_H)
67 # include <sys/elf.h>
68 #else
69 # error Could not locate <elf.h>
70 #endif
71 #if defined(ELFCLASS32)
72 # define UNW_ELFCLASS32 ELFCLASS32
73 #else
74 # define UNW_ELFCLASS32 1
75 #endif
76 #if defined(ELFCLASS64)
77 # define UNW_ELFCLASS64 ELFCLASS64
78 #else
79 # define UNW_ELFCLASS64 2
80 #endif
81
82 #if defined(HAVE_ENDIAN_H)
83 # include <endian.h>
84 #elif defined(HAVE_SYS_ENDIAN_H)
85 # include <sys/endian.h>
86 #elif defined(HAVE_SYS_PARAM_H)
87 # include <sys/param.h>
88 #endif
89
90 #if defined(__LITTLE_ENDIAN)
91 # define UNW_LITTLE_ENDIAN __LITTLE_ENDIAN
92 #elif defined(_LITTLE_ENDIAN)
93 # define UNW_LITTLE_ENDIAN _LITTLE_ENDIAN
94 #elif defined(LITTLE_ENDIAN)
95 # define UNW_LITTLE_ENDIAN LITTLE_ENDIAN
96 #else
97 # define UNW_LITTLE_ENDIAN 1234
98 #endif
99
100 #if defined(__BIG_ENDIAN)
101 # define UNW_BIG_ENDIAN __BIG_ENDIAN
102 #elif defined(_BIG_ENDIAN)
103 # define UNW_BIG_ENDIAN _BIG_ENDIAN
104 #elif defined(BIG_ENDIAN)
105 # define UNW_BIG_ENDIAN BIG_ENDIAN
106 #else
107 # define UNW_BIG_ENDIAN 4321
108 #endif
109
110 #if defined(__BYTE_ORDER)
111 # define UNW_BYTE_ORDER __BYTE_ORDER
112 #elif defined(_BYTE_ORDER)
113 # define UNW_BYTE_ORDER _BYTE_ORDER
114 #elif defined(BIG_ENDIAN)
115 # define UNW_BYTE_ORDER BYTE_ORDER
116 #else
117 # if defined(__hpux)
118 # define UNW_BYTE_ORDER UNW_BIG_ENDIAN
119 # else
120 # error Target has unknown byte ordering.
121 # endif
122 #endif
123
124 #define UNW_TEMP_FAILURE_RETRY(exp) \
125 ({ \
126 long int _rc; \
127 do { \
128 _rc = (long int)(exp); \
129 } while ((_rc == -1) && (errno == EINTR)); \
130 _rc; \
131 })
132
133 static inline int
byte_order_is_valid(int byte_order)134 byte_order_is_valid(int byte_order)
135 {
136 return byte_order != UNW_BIG_ENDIAN
137 && byte_order != UNW_LITTLE_ENDIAN;
138 }
139
140 static inline int
byte_order_is_big_endian(int byte_order)141 byte_order_is_big_endian(int byte_order)
142 {
143 return byte_order == UNW_BIG_ENDIAN;
144 }
145
146 static inline int
target_is_big_endian(void)147 target_is_big_endian(void)
148 {
149 return byte_order_is_big_endian(UNW_BYTE_ORDER);
150 }
151
152 #if defined(HAVE__BUILTIN_UNREACHABLE)
153 # define unreachable() __builtin_unreachable()
154 #else
155 # define unreachable() do { } while (1)
156 #endif
157
158 #ifdef DEBUG
159 # define UNW_DEBUG 1
160 #else
161 # define UNW_DEBUG 0
162 #endif
163
164 /* Make it easy to write thread-safe code which may or may not be
165 linked against libpthread. The macros below can be used
166 unconditionally and if -lpthread is around, they'll call the
167 corresponding routines otherwise, they do nothing. */
168
169 #pragma weak pthread_mutex_init
170 #pragma weak pthread_mutex_lock
171 #pragma weak pthread_mutex_unlock
172
173 #define mutex_init(l) \
174 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
175 #define mutex_lock(l) \
176 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
177 #define mutex_unlock(l) \
178 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
179
180 #define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
181 #define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
182
183 #define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
184
185 /* Type of a mask that can be used to inhibit preemption. At the
186 userlevel, preemption is caused by signals and hence sigset_t is
187 appropriate. In contrast, the Linux kernel uses "unsigned long"
188 to hold the processor "flags" instead. */
189 typedef sigset_t intrmask_t;
190
191 extern intrmask_t unwi_full_mask;
192
193 /* Silence compiler warnings about variables which are used only if libunwind
194 is configured in a certain way */
mark_as_used(void * v UNUSED)195 static inline void mark_as_used(void *v UNUSED) {
196 }
197
198 #if defined(CONFIG_BLOCK_SIGNALS)
199 # define SIGPROCMASK(how, new_mask, old_mask) \
200 sigprocmask((how), (new_mask), (old_mask))
201 #else
202 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
203 #endif
204
205 /* Prefer adaptive mutexes if available */
206 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
207 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
208 #else
209 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
210 #endif
211
212 #define define_lock(name) \
213 pthread_mutex_t name = UNW_PTHREAD_MUTEX_INITIALIZER
214 #define lock_init(l) mutex_init (l)
215 #define lock_acquire(l,m) \
216 do { \
217 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
218 mutex_lock (l); \
219 } while (0)
220 #define lock_release(l,m) \
221 do { \
222 mutex_unlock (l); \
223 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
224 } while (0)
225
226 #define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
227
228 #ifndef MAP_ANONYMOUS
229 # define MAP_ANONYMOUS MAP_ANON
230 #endif
231 #define GET_MEMORY(mem, size) \
232 do { \
233 /* Hopefully, mmap() goes straight through to a system call stub... */ \
234 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
235 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
236 if (mem == MAP_FAILED) \
237 mem = NULL; \
238 } while (0)
239
240 #define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
241 #define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
242 #define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
243 #define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
244 #define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
245 #define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
246
247 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
248 unw_word_t ip,
249 unw_proc_info_t *pi,
250 int need_unwind_info, void *arg);
251 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
252 unw_word_t ip,
253 unw_proc_info_t *pi,
254 unw_dyn_info_t *di,
255 int need_unwind_info,
256 void *arg);
257 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
258 unw_proc_info_t *pi, void *arg);
259
260 /* These handle the remote (cross-address-space) case of accessing
261 dynamic unwind info. */
262
263 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
264 unw_word_t ip,
265 unw_proc_info_t *pi,
266 int need_unwind_info,
267 void *arg);
268 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
269 unw_proc_info_t *pi,
270 void *arg);
271 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
272
273 extern unw_dyn_info_list_t _U_dyn_info_list;
274 extern pthread_mutex_t _U_dyn_info_list_lock;
275
276 #if UNW_DEBUG
277 #define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
278 extern long unwi_debug_level;
279
280 # include <stdio.h>
281 # define Debug(level, /* format */ ...) \
282 do { \
283 if (unwi_debug_level >= level) \
284 { \
285 int _n = level; \
286 if (_n > 16) \
287 _n = 16; \
288 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
289 fprintf (stderr, /* format */ __VA_ARGS__); \
290 } \
291 } while (0)
292 # define Dprintf(/* format */ ...) \
293 fprintf (stderr, /* format */ __VA_ARGS__)
294 #else
295 # define Debug(level, /* format */ ...)
296 # define Dprintf(/* format */ ...) \
297 fprintf (stderr, /* format */ __VA_ARGS__)
298 #endif
299
300 static ALWAYS_INLINE int
print_error(const char * string)301 print_error (const char *string)
302 {
303 return write (2, string, strlen (string));
304 }
305
306 #define mi_init UNWI_ARCH_OBJ(mi_init)
307
308 extern void mi_init (void); /* machine-independent initializations */
309 extern unw_word_t _U_dyn_info_list_addr (void);
310
311 /* This is needed/used by ELF targets only. */
312
313 struct elf_dyn_info
314 {
315 /* Add For Cache MAP And ELF*/
316 /* Removed: struct elf_image ei; */
317 /* Add For Cache MAP And ELF */
318 unw_word_t start_ip;
319 unw_word_t end_ip;
320 unw_dyn_info_t di_cache;
321 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
322 #if UNW_TARGET_IA64
323 unw_dyn_info_t ktab;
324 #endif
325 #if UNW_TARGET_ARM
326 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
327 #endif
328 };
329 #ifdef PARSE_BUILD_ID
330 struct build_id_note {
331 ElfW(Nhdr) nhdr;
332 char name[4];
333 uint8_t build_id[0];
334 };
335 #endif
336 struct elf_image
337 {
338 void *image; /* pointer to mmap'd image */
339 size_t size; /* (file-) size of the image */
340 int has_dyn_info;
341 struct elf_dyn_info elf_dyn_info;
342 int load_bias;
343 int load_offset;
344 char* strtab;
345 size_t lib_name_offset;
346 #ifdef PARSE_BUILD_ID
347 struct build_id_note* build_id_note;
348 #endif
349 struct elf_image* mdi;
350 int has_try_load;
351 };
352
invalidate_edi(struct elf_dyn_info * edi)353 static inline void invalidate_edi (struct elf_dyn_info *edi)
354 {
355 /* Add For Cache MAP And ELF*/
356 /* Removed: if (edi->ei.image) */
357 /* munmap (edi->ei.image, edi->ei.size); */
358 /* Add For Cache MAP And ELF */
359 memset (edi, 0, sizeof (*edi));
360 edi->di_cache.format = -1;
361 edi->di_debug.format = -1;
362 #if UNW_TARGET_ARM
363 edi->di_arm.format = -1;
364 #endif
365 }
366
clear_ptrauth(unw_word_t ip)367 static inline unw_word_t clear_ptrauth(unw_word_t ip)
368 {
369 #ifdef __aarch64__
370 unw_word_t insn_mask = 0xFFFFFF8000000000; // mask all bits from [63:40];
371 return ip & (~insn_mask);
372 #else
373 return ip;
374 #endif
375 }
376
377 /* Provide a place holder for architecture to override for fast access
378 to memory when known not to need to validate and know the access
379 will be local to the process. A suitable override will improve
380 unw_tdep_trace() performance in particular. */
381 #define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
382 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
383 while (0)
384
385 /* Define GNU and processor specific values for the Phdr p_type field in case
386 they aren't defined by <elf.h>. */
387 #ifndef PT_GNU_EH_FRAME
388 # define PT_GNU_EH_FRAME 0x6474e550
389 #endif /* !PT_GNU_EH_FRAME */
390 #ifndef PT_ARM_EXIDX
391 # define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
392 #endif /* !PT_ARM_EXIDX */
393
394 #include "tdep/libunwind_i.h"
395
396 #ifndef TDEP_DWARF_SP
397 #define TDEP_DWARF_SP UNW_TDEP_SP
398 #endif
399
400 #ifndef tdep_get_func_addr
401 # define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
402 #endif
403
404 #ifndef DWARF_VAL_LOC
405 # define DWARF_IS_VAL_LOC(l) 0
406 # define DWARF_VAL_LOC(c,v) DWARF_NULL_LOC
407 #endif
408
409 #define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
410
411 #endif /* libunwind_i_h */
412