1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_COMPILER_TYPES_H 3 #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." 4 #endif 5 6 /* 7 * Common definitions for all gcc versions go here. 8 */ 9 #define GCC_VERSION (__GNUC__ * 10000 \ 10 + __GNUC_MINOR__ * 100 \ 11 + __GNUC_PATCHLEVEL__) 12 13 /* 14 * This macro obfuscates arithmetic on a variable address so that gcc 15 * shouldn't recognize the original var, and make assumptions about it. 16 * 17 * This is needed because the C standard makes it undefined to do 18 * pointer arithmetic on "objects" outside their boundaries and the 19 * gcc optimizers assume this is the case. In particular they 20 * assume such arithmetic does not wrap. 21 * 22 * A miscompilation has been observed because of this on PPC. 23 * To work around it we hide the relationship of the pointer and the object 24 * using this macro. 25 * 26 * Versions of the ppc64 compiler before 4.1 had a bug where use of 27 * RELOC_HIDE could trash r30. The bug can be worked around by changing 28 * the inline assembly constraint from =g to =r, in this particular 29 * case either is valid. 30 */ 31 #define RELOC_HIDE(ptr, off) \ 32 ({ \ 33 unsigned long __ptr; \ 34 __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \ 35 (typeof(ptr)) (__ptr + (off)); \ 36 }) 37 38 #ifdef CONFIG_RETPOLINE 39 #define __noretpoline __attribute__((__indirect_branch__("keep"))) 40 #endif 41 42 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) 43 44 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) 45 #define __latent_entropy __attribute__((latent_entropy)) 46 #endif 47 48 /* 49 * calling noreturn functions, __builtin_unreachable() and __builtin_trap() 50 * confuse the stack allocation in gcc, leading to overly large stack 51 * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365 52 * 53 * Adding an empty inline assembly before it works around the problem 54 */ 55 #define barrier_before_unreachable() asm volatile("") 56 57 /* 58 * Mark a position in code as unreachable. This can be used to 59 * suppress control flow warnings after asm blocks that transfer 60 * control elsewhere. 61 */ 62 #define unreachable() \ 63 do { \ 64 annotate_unreachable(); \ 65 barrier_before_unreachable(); \ 66 __builtin_unreachable(); \ 67 } while (0) 68 69 /* 70 * GCC 'asm goto' with outputs miscompiles certain code sequences: 71 * 72 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113921 73 * 74 * Work around it via the same compiler barrier quirk that we used 75 * to use for the old 'asm goto' workaround. 76 * 77 * Also, always mark such 'asm goto' statements as volatile: all 78 * asm goto statements are supposed to be volatile as per the 79 * documentation, but some versions of gcc didn't actually do 80 * that for asms with outputs: 81 * 82 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98619 83 */ 84 #ifdef CONFIG_GCC_ASM_GOTO_OUTPUT_WORKAROUND 85 #define asm_goto_output(x...) \ 86 do { asm volatile goto(x); asm (""); } while (0) 87 #endif 88 89 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP) 90 #define __HAVE_BUILTIN_BSWAP32__ 91 #define __HAVE_BUILTIN_BSWAP64__ 92 #define __HAVE_BUILTIN_BSWAP16__ 93 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */ 94 95 #if GCC_VERSION >= 70000 96 #define KASAN_ABI_VERSION 5 97 #else 98 #define KASAN_ABI_VERSION 4 99 #endif 100 101 #ifdef CONFIG_SHADOW_CALL_STACK 102 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack"))) 103 #endif 104 105 #ifdef __SANITIZE_HWADDRESS__ 106 #define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress"))) 107 #else 108 #define __no_sanitize_address __attribute__((__no_sanitize_address__)) 109 #endif 110 111 #if defined(__SANITIZE_THREAD__) 112 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__)) 113 #else 114 #define __no_sanitize_thread 115 #endif 116 117 #define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__)) 118 119 /* 120 * Only supported since gcc >= 12 121 */ 122 #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__) 123 #define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__)) 124 #else 125 #define __no_sanitize_coverage 126 #endif 127 128 /* 129 * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel, 130 * matching the defines used by Clang. 131 */ 132 #ifdef __SANITIZE_HWADDRESS__ 133 #define __SANITIZE_ADDRESS__ 134 #endif 135 136 /* 137 * GCC does not support KMSAN. 138 */ 139 #define __no_sanitize_memory 140 #define __no_kmsan_checks 141 142 /* 143 * Turn individual warnings and errors on and off locally, depending 144 * on version. 145 */ 146 #define __diag_GCC(version, severity, s) \ 147 __diag_GCC_ ## version(__diag_GCC_ ## severity s) 148 149 /* Severity used in pragma directives */ 150 #define __diag_GCC_ignore ignored 151 #define __diag_GCC_warn warning 152 #define __diag_GCC_error error 153 154 #define __diag_str1(s) #s 155 #define __diag_str(s) __diag_str1(s) 156 #define __diag(s) _Pragma(__diag_str(GCC diagnostic s)) 157 158 #if GCC_VERSION >= 80000 159 #define __diag_GCC_8(s) __diag(s) 160 #else 161 #define __diag_GCC_8(s) 162 #endif 163 164 #define __diag_ignore_all(option, comment) \ 165 __diag_GCC(8, ignore, option) 166 167 /* 168 * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size" 169 * attribute) do not work, and must be disabled. 170 */ 171 #if GCC_VERSION < 90100 172 #undef __alloc_size__ 173 #endif 174