1 //===----------------------------- config.h -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //
9 //  Defines macros used within libuwind project.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 
14 #ifndef LIBUNWIND_CONFIG_H
15 #define LIBUNWIND_CONFIG_H
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 // Define static_assert() unless already defined by compiler.
22 #ifndef __has_feature
23   #define __has_feature(__x) 0
24 #endif
25 #if !(__has_feature(cxx_static_assert)) && !defined(static_assert)
26   #define static_assert(__b, __m) \
27       extern int compile_time_assert_failed[ ( __b ) ? 1 : -1 ]  \
28                                                   __attribute__( ( unused ) );
29 #endif
30 
31 // Platform specific configuration defines.
32 #ifdef __APPLE__
33   #if defined(FOR_DYLD)
34     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1
35     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND   0
36     #define _LIBUNWIND_SUPPORT_DWARF_INDEX    0
37   #else
38     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1
39     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND   1
40     #define _LIBUNWIND_SUPPORT_DWARF_INDEX    0
41   #endif
42 #else
43   #if defined(__ARM_DWARF_EH__) || !defined(__arm__)
44     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 0
45     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1
46     #define _LIBUNWIND_SUPPORT_DWARF_INDEX 1
47   #else
48     #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 0
49     #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 0
50     #define _LIBUNWIND_SUPPORT_DWARF_INDEX 0
51   #endif
52 #endif
53 
54 // FIXME: these macros are not correct for COFF targets
55 #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
56 #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
57 
58 #if (defined(__APPLE__) && defined(__arm__)) || defined(__USING_SJLJ_EXCEPTIONS__)
59 #define _LIBUNWIND_BUILD_SJLJ_APIS 1
60 #else
61 #define _LIBUNWIND_BUILD_SJLJ_APIS 0
62 #endif
63 
64 #if defined(__i386__) || defined(__x86_64__)
65 #define _LIBUNWIND_SUPPORT_FRAME_APIS 1
66 #else
67 #define _LIBUNWIND_SUPPORT_FRAME_APIS 0
68 #endif
69 
70 #if defined(__i386__) || defined(__x86_64__) ||                                \
71     (!defined(__APPLE__) && defined(__arm__)) ||                               \
72     (defined(__arm64__) || defined(__aarch64__)) ||                            \
73     (defined(__APPLE__) && defined(__mips__))
74 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 1
75 #else
76 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 0
77 #endif
78 
79 #define _LIBUNWIND_ABORT(msg)                                                  \
80   do {                                                                         \
81     fprintf(stderr, "libunwind: %s %s:%d - %s\n", __func__, __FILE__,          \
82             __LINE__, msg);                                                    \
83     fflush(stderr);                                                            \
84     abort();                                                                   \
85   } while (0)
86 #define _LIBUNWIND_LOG(msg, ...) fprintf(stderr, "libuwind: " msg, __VA_ARGS__)
87 
88 // Macros that define away in non-Debug builds
89 #ifdef NDEBUG
90   #define _LIBUNWIND_DEBUG_LOG(msg, ...)
91   #define _LIBUNWIND_TRACE_API(msg, ...)
92   #define _LIBUNWIND_TRACING_UNWINDING 0
93   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...)
94   #define _LIBUNWIND_LOG_NON_ZERO(x) x
95 #else
96   #ifdef __cplusplus
97     extern "C" {
98   #endif
99     extern  bool logAPIs();
100     extern  bool logUnwinding();
101   #ifdef __cplusplus
102     }
103   #endif
104   #define _LIBUNWIND_DEBUG_LOG(msg, ...)  _LIBUNWIND_LOG(msg, __VA_ARGS__)
105   #define _LIBUNWIND_LOG_NON_ZERO(x) \
106             do { \
107               int _err = x; \
108               if ( _err != 0 ) \
109                 _LIBUNWIND_LOG("" #x "=%d in %s", _err, __FUNCTION__); \
110              } while (0)
111   #define _LIBUNWIND_TRACE_API(msg, ...) \
112             do { \
113               if ( logAPIs() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
114             } while(0)
115   #define _LIBUNWIND_TRACE_UNWINDING(msg, ...) \
116             do { \
117               if ( logUnwinding() ) _LIBUNWIND_LOG(msg, __VA_ARGS__); \
118             } while(0)
119   #define _LIBUNWIND_TRACING_UNWINDING logUnwinding()
120 #endif
121 
122 #ifdef __cplusplus
123 // Used to fit UnwindCursor and Registers_xxx types against unw_context_t /
124 // unw_cursor_t sized memory blocks.
125 #if defined(_LIBUNWIND_IS_NATIVE_ONLY)
126 # define COMP_OP ==
127 #else
128 # define COMP_OP <
129 #endif
130 template <typename _Type, typename _Mem>
131 struct check_fit {
132   template <typename T>
133   struct blk_count {
134     static const size_t count =
135       (sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t);
136   };
137   static const bool does_fit =
138     (blk_count<_Type>::count COMP_OP blk_count<_Mem>::count);
139 };
140 #undef COMP_OP
141 #endif // __cplusplus
142 
143 #endif // LIBUNWIND_CONFIG_H
144