1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 // 8 // Defines macros used within libunwind project. 9 // 10 //===----------------------------------------------------------------------===// 11 12 13 #ifndef LIBUNWIND_CONFIG_H 14 #define LIBUNWIND_CONFIG_H 15 16 #include <assert.h> 17 #include <stdio.h> 18 #include <stdint.h> 19 #include <stdlib.h> 20 21 #include <__libunwind_config.h> 22 23 // Platform specific configuration defines. 24 #ifdef __APPLE__ 25 #if defined(FOR_DYLD) 26 #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1 27 #else 28 #define _LIBUNWIND_SUPPORT_COMPACT_UNWIND 1 29 #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1 30 #endif 31 #elif defined(_WIN32) 32 #ifdef __SEH__ 33 #define _LIBUNWIND_SUPPORT_SEH_UNWIND 1 34 #else 35 #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1 36 #endif 37 #elif defined(_LIBUNWIND_IS_BAREMETAL) 38 #if !defined(_LIBUNWIND_ARM_EHABI) 39 #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1 40 #define _LIBUNWIND_SUPPORT_DWARF_INDEX 1 41 #endif 42 #elif defined(__BIONIC__) && defined(_LIBUNWIND_ARM_EHABI) 43 // For ARM EHABI, Bionic didn't implement dl_iterate_phdr until API 21. After 44 // API 21, dl_iterate_phdr exists, but dl_unwind_find_exidx is much faster. 45 #define _LIBUNWIND_USE_DL_UNWIND_FIND_EXIDX 1 46 #elif defined(_AIX) 47 // The traceback table at the end of each function is used for unwinding. 48 #define _LIBUNWIND_SUPPORT_TBTAB_UNWIND 1 49 #else 50 // Assume an ELF system with a dl_iterate_phdr function. 51 #define _LIBUNWIND_USE_DL_ITERATE_PHDR 1 52 #if !defined(_LIBUNWIND_ARM_EHABI) 53 #define _LIBUNWIND_SUPPORT_DWARF_UNWIND 1 54 #define _LIBUNWIND_SUPPORT_DWARF_INDEX 1 55 #endif 56 #endif 57 58 #if defined(_LIBUNWIND_HIDE_SYMBOLS) 59 // The CMake file passes -fvisibility=hidden to control ELF/Mach-O visibility. 60 #define _LIBUNWIND_EXPORT 61 #define _LIBUNWIND_HIDDEN 62 #else 63 #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) 64 #define _LIBUNWIND_EXPORT __declspec(dllexport) 65 #define _LIBUNWIND_HIDDEN 66 #else 67 #define _LIBUNWIND_EXPORT __attribute__((visibility("default"))) 68 #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden"))) 69 #endif 70 #endif 71 72 #define STR(a) #a 73 #define XSTR(a) STR(a) 74 #define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name 75 76 #if defined(__APPLE__) 77 #if defined(_LIBUNWIND_HIDE_SYMBOLS) 78 #define _LIBUNWIND_ALIAS_VISIBILITY(name) __asm__(".private_extern " name); 79 #else 80 #define _LIBUNWIND_ALIAS_VISIBILITY(name) 81 #endif 82 #define _LIBUNWIND_WEAK_ALIAS(name, aliasname) \ 83 __asm__(".globl " SYMBOL_NAME(aliasname)); \ 84 __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \ 85 _LIBUNWIND_ALIAS_VISIBILITY(SYMBOL_NAME(aliasname)) 86 #elif defined(__ELF__) || defined(_AIX) 87 #define _LIBUNWIND_WEAK_ALIAS(name, aliasname) \ 88 extern "C" _LIBUNWIND_EXPORT __typeof(name) aliasname \ 89 __attribute__((weak, alias(#name))); 90 #elif defined(_WIN32) 91 #if defined(__MINGW32__) 92 #define _LIBUNWIND_WEAK_ALIAS(name, aliasname) \ 93 extern "C" _LIBUNWIND_EXPORT __typeof(name) aliasname \ 94 __attribute__((alias(#name))); 95 #else 96 #define _LIBUNWIND_WEAK_ALIAS(name, aliasname) \ 97 __pragma(comment(linker, "/alternatename:" SYMBOL_NAME(aliasname) "=" \ 98 SYMBOL_NAME(name))) \ 99 extern "C" _LIBUNWIND_EXPORT __typeof(name) aliasname; 100 #endif 101 #else 102 #error Unsupported target 103 #endif 104 105 // Apple/armv7k defaults to DWARF/Compact unwinding, but its libunwind also 106 // needs to include the SJLJ APIs. 107 #if (defined(__APPLE__) && defined(__arm__)) || defined(__USING_SJLJ_EXCEPTIONS__) 108 #define _LIBUNWIND_BUILD_SJLJ_APIS 109 #endif 110 111 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) 112 #define _LIBUNWIND_SUPPORT_FRAME_APIS 113 #endif 114 115 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || \ 116 (!defined(__APPLE__) && defined(__arm__)) || defined(__aarch64__) || \ 117 defined(__mips__) || defined(__riscv) || defined(__hexagon__) || \ 118 defined(__sparc__) || defined(__s390x__) || defined(__loongarch__) 119 #if !defined(_LIBUNWIND_BUILD_SJLJ_APIS) 120 #define _LIBUNWIND_BUILD_ZERO_COST_APIS 121 #endif 122 #endif 123 124 #ifndef _LIBUNWIND_REMEMBER_HEAP_ALLOC 125 #if defined(_LIBUNWIND_REMEMBER_STACK_ALLOC) || defined(__APPLE__) || \ 126 defined(__linux__) || defined(__ANDROID__) || defined(__MINGW32__) || \ 127 defined(_LIBUNWIND_IS_BAREMETAL) 128 #define _LIBUNWIND_REMEMBER_ALLOC(_size) alloca(_size) 129 #define _LIBUNWIND_REMEMBER_FREE(_ptr) \ 130 do { \ 131 } while (0) 132 #elif defined(_WIN32) 133 #define _LIBUNWIND_REMEMBER_ALLOC(_size) _malloca(_size) 134 #define _LIBUNWIND_REMEMBER_FREE(_ptr) _freea(_ptr) 135 #define _LIBUNWIND_REMEMBER_CLEANUP_NEEDED 136 #else 137 #define _LIBUNWIND_REMEMBER_ALLOC(_size) malloc(_size) 138 #define _LIBUNWIND_REMEMBER_FREE(_ptr) free(_ptr) 139 #define _LIBUNWIND_REMEMBER_CLEANUP_NEEDED 140 #endif 141 #else /* _LIBUNWIND_REMEMBER_HEAP_ALLOC */ 142 #define _LIBUNWIND_REMEMBER_ALLOC(_size) malloc(_size) 143 #define _LIBUNWIND_REMEMBER_FREE(_ptr) free(_ptr) 144 #define _LIBUNWIND_REMEMBER_CLEANUP_NEEDED 145 #endif 146 147 #if defined(NDEBUG) && defined(_LIBUNWIND_IS_BAREMETAL) 148 #define _LIBUNWIND_ABORT(msg) \ 149 do { \ 150 abort(); \ 151 } while (0) 152 #else 153 #define _LIBUNWIND_ABORT(msg) \ 154 do { \ 155 fprintf(stderr, "libunwind: %s - %s\n", __func__, msg); \ 156 fflush(stderr); \ 157 abort(); \ 158 } while (0) 159 #endif 160 161 #if defined(NDEBUG) && defined(_LIBUNWIND_IS_BAREMETAL) 162 #define _LIBUNWIND_LOG0(msg) 163 #define _LIBUNWIND_LOG(msg, ...) 164 #else 165 #define _LIBUNWIND_LOG0(msg) do { \ 166 fprintf(stderr, "libunwind: " msg "\n"); \ 167 fflush(stderr); \ 168 } while (0) 169 #define _LIBUNWIND_LOG(msg, ...) do { \ 170 fprintf(stderr, "libunwind: " msg "\n", __VA_ARGS__); \ 171 fflush(stderr); \ 172 } while (0) 173 #endif 174 175 #if defined(NDEBUG) 176 #define _LIBUNWIND_LOG_IF_FALSE(x) x 177 #else 178 #define _LIBUNWIND_LOG_IF_FALSE(x) \ 179 do { \ 180 bool _ret = x; \ 181 if (!_ret) \ 182 _LIBUNWIND_LOG("" #x " failed in %s", __FUNCTION__); \ 183 } while (0) 184 #endif 185 186 // Macros that define away in non-Debug builds 187 #ifdef NDEBUG 188 #define _LIBUNWIND_DEBUG_LOG(msg, ...) 189 #define _LIBUNWIND_TRACE_API(msg, ...) 190 #define _LIBUNWIND_TRACING_UNWINDING (0) 191 #define _LIBUNWIND_TRACING_DWARF (0) 192 #define _LIBUNWIND_TRACE_UNWINDING(msg, ...) 193 #define _LIBUNWIND_TRACE_DWARF(...) 194 #else 195 #ifdef __cplusplus 196 extern "C" { 197 #endif 198 extern bool logAPIs(void); 199 extern bool logUnwinding(void); 200 extern bool logDWARF(void); 201 #ifdef __cplusplus 202 } 203 #endif 204 #define _LIBUNWIND_DEBUG_LOG(msg, ...) _LIBUNWIND_LOG(msg, __VA_ARGS__) 205 #define _LIBUNWIND_TRACE_API(msg, ...) \ 206 do { \ 207 if (logAPIs()) \ 208 _LIBUNWIND_LOG(msg, __VA_ARGS__); \ 209 } while (0) 210 #define _LIBUNWIND_TRACING_UNWINDING logUnwinding() 211 #define _LIBUNWIND_TRACING_DWARF logDWARF() 212 #define _LIBUNWIND_TRACE_UNWINDING(msg, ...) \ 213 do { \ 214 if (logUnwinding()) \ 215 _LIBUNWIND_LOG(msg, __VA_ARGS__); \ 216 } while (0) 217 #define _LIBUNWIND_TRACE_DWARF(...) \ 218 do { \ 219 if (logDWARF()) \ 220 fprintf(stderr, __VA_ARGS__); \ 221 } while (0) 222 #endif 223 224 #ifdef __cplusplus 225 // Used to fit UnwindCursor and Registers_xxx types against unw_context_t / 226 // unw_cursor_t sized memory blocks. 227 #if defined(_LIBUNWIND_IS_NATIVE_ONLY) 228 # define COMP_OP == 229 #else 230 # define COMP_OP <= 231 #endif 232 template <typename _Type, typename _Mem> 233 struct check_fit { 234 template <typename T> 235 struct blk_count { 236 static const size_t count = 237 (sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t); 238 }; 239 static const bool does_fit = 240 (blk_count<_Type>::count COMP_OP blk_count<_Mem>::count); 241 }; 242 #undef COMP_OP 243 #endif // __cplusplus 244 245 #endif // LIBUNWIND_CONFIG_H 246