xref: /linux-6.15/include/linux/compiler-gcc.h (revision ac053946)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2d1515582SWill Deacon #ifndef __LINUX_COMPILER_TYPES_H
3a7306f3cSNataniel Farzan #error "Please do not include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
494f582f8SRobert P. J. Day #endif
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds  * Common definitions for all gcc versions go here.
81da177e4SLinus Torvalds  */
93f3f8d2fSDaniel Santos #define GCC_VERSION (__GNUC__ * 10000		\
103f3f8d2fSDaniel Santos 		     + __GNUC_MINOR__ * 100	\
113f3f8d2fSDaniel Santos 		     + __GNUC_PATCHLEVEL__)
121da177e4SLinus Torvalds 
13c8d52465SAnton Blanchard /*
1485c210edSAndi Kleen  * This macro obfuscates arithmetic on a variable address so that gcc
1585c210edSAndi Kleen  * shouldn't recognize the original var, and make assumptions about it.
1685c210edSAndi Kleen  *
1785c210edSAndi Kleen  * This is needed because the C standard makes it undefined to do
1885c210edSAndi Kleen  * pointer arithmetic on "objects" outside their boundaries and the
1985c210edSAndi Kleen  * gcc optimizers assume this is the case. In particular they
2085c210edSAndi Kleen  * assume such arithmetic does not wrap.
2185c210edSAndi Kleen  *
2285c210edSAndi Kleen  * A miscompilation has been observed because of this on PPC.
2385c210edSAndi Kleen  * To work around it we hide the relationship of the pointer and the object
2485c210edSAndi Kleen  * using this macro.
2585c210edSAndi Kleen  *
26c8d52465SAnton Blanchard  * Versions of the ppc64 compiler before 4.1 had a bug where use of
27c8d52465SAnton Blanchard  * RELOC_HIDE could trash r30. The bug can be worked around by changing
28c8d52465SAnton Blanchard  * the inline assembly constraint from =g to =r, in this particular
29c8d52465SAnton Blanchard  * case either is valid.
30c8d52465SAnton Blanchard  */
311da177e4SLinus Torvalds #define RELOC_HIDE(ptr, off)						\
32f6d133f8SJoe Perches ({									\
33f6d133f8SJoe Perches 	unsigned long __ptr;						\
34c8d52465SAnton Blanchard 	__asm__ ("" : "=r"(__ptr) : "0"(ptr));				\
35f6d133f8SJoe Perches 	(typeof(ptr)) (__ptr + (off));					\
36f6d133f8SJoe Perches })
37a1365647SAndrew Morton 
38aefb2f2eSBreno Leitao #ifdef CONFIG_MITIGATION_RETPOLINE
395c67a52fSMiguel Ojeda #define __noretpoline __attribute__((__indirect_branch__("keep")))
4087358710SDavid Woodhouse #endif
4187358710SDavid Woodhouse 
42989bd500SMiguel Ojeda #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
430766f788SEmese Revfy #define __latent_entropy __attribute__((latent_entropy))
440766f788SEmese Revfy #endif
450766f788SEmese Revfy 
46cb984d10SJoe Perches /*
47173a3efdSArnd Bergmann  * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
48173a3efdSArnd Bergmann  * confuse the stack allocation in gcc, leading to overly large stack
49173a3efdSArnd Bergmann  * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
50173a3efdSArnd Bergmann  *
51173a3efdSArnd Bergmann  * Adding an empty inline assembly before it works around the problem
52173a3efdSArnd Bergmann  */
53173a3efdSArnd Bergmann #define barrier_before_unreachable() asm volatile("")
54173a3efdSArnd Bergmann 
55d991bb1cSLuc Van Oostenryck #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
56cb984d10SJoe Perches #define __HAVE_BUILTIN_BSWAP32__
57cb984d10SJoe Perches #define __HAVE_BUILTIN_BSWAP64__
58cb984d10SJoe Perches #define __HAVE_BUILTIN_BSWAP16__
59d991bb1cSLuc Van Oostenryck #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
60cb984d10SJoe Perches 
61045d599aSDmitry Vyukov #if GCC_VERSION >= 70000
62045d599aSDmitry Vyukov #define KASAN_ABI_VERSION 5
634e59869aSNick Desaulniers #else
64cb984d10SJoe Perches #define KASAN_ABI_VERSION 4
65cb984d10SJoe Perches #endif
66cb984d10SJoe Perches 
67afcf5441SDan Li #ifdef CONFIG_SHADOW_CALL_STACK
68afcf5441SDan Li #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
69afcf5441SDan Li #endif
70afcf5441SDan Li 
71894b00a3SMarco Elver #ifdef __SANITIZE_HWADDRESS__
72894b00a3SMarco Elver #define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
73894b00a3SMarco Elver #else
746e2be1f2SMiguel Ojeda #define __no_sanitize_address __attribute__((__no_sanitize_address__))
75894b00a3SMarco Elver #endif
762bd926b4SAndrey Konovalov 
77095ac076SMiguel Ojeda #if defined(__SANITIZE_THREAD__)
786e2be1f2SMiguel Ojeda #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
79dfd402a4SMarco Elver #else
80dfd402a4SMarco Elver #define __no_sanitize_thread
81dfd402a4SMarco Elver #endif
82dfd402a4SMarco Elver 
836e2be1f2SMiguel Ojeda #define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__))
845144f8a8SMarco Elver 
85f39556bcSMiguel Ojeda /*
86f39556bcSMiguel Ojeda  * Only supported since gcc >= 12
87f39556bcSMiguel Ojeda  */
88540540d0SMarco Elver #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
896e2be1f2SMiguel Ojeda #define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__))
90540540d0SMarco Elver #else
91540540d0SMarco Elver #define __no_sanitize_coverage
92540540d0SMarco Elver #endif
93540540d0SMarco Elver 
94c837fb37SBorislav Petkov /*
959a48e756SKees Cook  * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel,
969a48e756SKees Cook  * matching the defines used by Clang.
979a48e756SKees Cook  */
989a48e756SKees Cook #ifdef __SANITIZE_HWADDRESS__
999a48e756SKees Cook #define __SANITIZE_ADDRESS__
1009a48e756SKees Cook #endif
1019a48e756SKees Cook 
1029a48e756SKees Cook /*
1039b448bc2SAlexander Potapenko  * GCC does not support KMSAN.
1049b448bc2SAlexander Potapenko  */
1059b448bc2SAlexander Potapenko #define __no_sanitize_memory
1069b448bc2SAlexander Potapenko #define __no_kmsan_checks
1079b448bc2SAlexander Potapenko 
1089b448bc2SAlexander Potapenko /*
1098793bb7fSArnd Bergmann  * Turn individual warnings and errors on and off locally, depending
1108793bb7fSArnd Bergmann  * on version.
1118793bb7fSArnd Bergmann  */
1128793bb7fSArnd Bergmann #define __diag_GCC(version, severity, s) \
1138793bb7fSArnd Bergmann 	__diag_GCC_ ## version(__diag_GCC_ ## severity s)
1148793bb7fSArnd Bergmann 
1158793bb7fSArnd Bergmann /* Severity used in pragma directives */
1168793bb7fSArnd Bergmann #define __diag_GCC_ignore	ignored
1178793bb7fSArnd Bergmann #define __diag_GCC_warn		warning
1188793bb7fSArnd Bergmann #define __diag_GCC_error	error
1198793bb7fSArnd Bergmann 
1208793bb7fSArnd Bergmann #define __diag_str1(s)		#s
1218793bb7fSArnd Bergmann #define __diag_str(s)		__diag_str1(s)
1228793bb7fSArnd Bergmann #define __diag(s)		_Pragma(__diag_str(GCC diagnostic s))
1238793bb7fSArnd Bergmann 
1248793bb7fSArnd Bergmann #if GCC_VERSION >= 80000
1258793bb7fSArnd Bergmann #define __diag_GCC_8(s)		__diag(s)
1268793bb7fSArnd Bergmann #else
1278793bb7fSArnd Bergmann #define __diag_GCC_8(s)
1288793bb7fSArnd Bergmann #endif
12986cffecdSKees Cook 
1304d1ea705SKumar Kartikeya Dwivedi #define __diag_ignore_all(option, comment) \
131689b097aSYafang Shao 	__diag(__diag_GCC_ignore option)
1324d1ea705SKumar Kartikeya Dwivedi 
13386cffecdSKees Cook /*
13486cffecdSKees Cook  * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size"
13586cffecdSKees Cook  * attribute) do not work, and must be disabled.
13686cffecdSKees Cook  */
13786cffecdSKees Cook #if GCC_VERSION < 90100
13886cffecdSKees Cook #undef __alloc_size__
13986cffecdSKees Cook #endif
140*ac053946SUros Bizjak 
141*ac053946SUros Bizjak /*
142*ac053946SUros Bizjak  * Declare compiler support for __typeof_unqual__() operator.
143*ac053946SUros Bizjak  *
144*ac053946SUros Bizjak  * Bindgen uses LLVM even if our C compiler is GCC, so we cannot
145*ac053946SUros Bizjak  * rely on the auto-detected CONFIG_CC_HAS_TYPEOF_UNQUAL.
146*ac053946SUros Bizjak  */
147*ac053946SUros Bizjak #define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >= 14)
148