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."
7 * Common definitions for all gcc versions go here.
9 #define GCC_VERSION (__GNUC__ * 10000 \
10 + __GNUC_MINOR__ * 100 \
11 + __GNUC_PATCHLEVEL__)
14 * This macro obfuscates arithmetic on a variable address so that gcc
15 * shouldn't recognize the original var, and make assumptions about it.
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.
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
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.
31 #define RELOC_HIDE(ptr, off) \
33 unsigned long __ptr; \
34 __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
35 (typeof(ptr)) (__ptr + (off)); \
38 #ifdef CONFIG_RETPOLINE
39 #define __noretpoline __attribute__((__indirect_branch__("keep")))
42 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
43 #define __latent_entropy __attribute__((latent_entropy))
47 * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
48 * confuse the stack allocation in gcc, leading to overly large stack
49 * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
51 * Adding an empty inline assembly before it works around the problem
53 #define barrier_before_unreachable() asm volatile("")
56 * Mark a position in code as unreachable. This can be used to
57 * suppress control flow warnings after asm blocks that transfer
60 #define unreachable() \
62 annotate_unreachable(); \
63 barrier_before_unreachable(); \
64 __builtin_unreachable(); \
68 * GCC 'asm goto' with outputs miscompiles certain code sequences:
70 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110420
71 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110422
73 * Work it around via the same compiler barrier quirk that we used
74 * to use for the old 'asm goto' workaround.
76 * Also, always mark such 'asm goto' statements as volatile: all
77 * asm goto statements are supposed to be volatile as per the
78 * documentation, but some versions of gcc didn't actually do
79 * that for asms with outputs:
81 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98619
83 #define asm_goto_output(x...) \
84 do { asm volatile goto(x); asm (""); } while (0)
86 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
87 #define __HAVE_BUILTIN_BSWAP32__
88 #define __HAVE_BUILTIN_BSWAP64__
89 #define __HAVE_BUILTIN_BSWAP16__
90 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
92 #if GCC_VERSION >= 70000
93 #define KASAN_ABI_VERSION 5
95 #define KASAN_ABI_VERSION 4
98 #ifdef CONFIG_SHADOW_CALL_STACK
99 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
102 #define __no_sanitize_address __attribute__((__no_sanitize_address__))
104 #if defined(__SANITIZE_THREAD__)
105 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
107 #define __no_sanitize_thread
110 #define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__))
113 * Only supported since gcc >= 12
115 #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
116 #define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__))
118 #define __no_sanitize_coverage
122 * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel,
123 * matching the defines used by Clang.
125 #ifdef __SANITIZE_HWADDRESS__
126 #define __SANITIZE_ADDRESS__
130 * GCC does not support KMSAN.
132 #define __no_sanitize_memory
133 #define __no_kmsan_checks
136 * Turn individual warnings and errors on and off locally, depending
139 #define __diag_GCC(version, severity, s) \
140 __diag_GCC_ ## version(__diag_GCC_ ## severity s)
142 /* Severity used in pragma directives */
143 #define __diag_GCC_ignore ignored
144 #define __diag_GCC_warn warning
145 #define __diag_GCC_error error
147 #define __diag_str1(s) #s
148 #define __diag_str(s) __diag_str1(s)
149 #define __diag(s) _Pragma(__diag_str(GCC diagnostic s))
151 #if GCC_VERSION >= 80000
152 #define __diag_GCC_8(s) __diag(s)
154 #define __diag_GCC_8(s)
157 #define __diag_ignore_all(option, comment) \
158 __diag(__diag_GCC_ignore option)
161 * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size"
162 * attribute) do not work, and must be disabled.
164 #if GCC_VERSION < 90100
165 #undef __alloc_size__