xref: /linux/include/linux/compiler-gcc.h (revision 9591fdb0611dccdeeeeacb99d89f0098737d209b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #error "Please do not 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 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
39 #define __latent_entropy __attribute__((latent_entropy))
40 #endif
41 
42 /*
43  * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
44  * confuse the stack allocation in gcc, leading to overly large stack
45  * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
46  *
47  * Adding an empty inline assembly before it works around the problem
48  */
49 #define barrier_before_unreachable() asm volatile("")
50 
51 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
52 #define __HAVE_BUILTIN_BSWAP32__
53 #define __HAVE_BUILTIN_BSWAP64__
54 #define __HAVE_BUILTIN_BSWAP16__
55 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
56 
57 #if GCC_VERSION >= 70000
58 #define KASAN_ABI_VERSION 5
59 #else
60 #define KASAN_ABI_VERSION 4
61 #endif
62 
63 #ifdef CONFIG_SHADOW_CALL_STACK
64 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
65 #endif
66 
67 #ifdef __SANITIZE_HWADDRESS__
68 #define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
69 #else
70 #define __no_sanitize_address __attribute__((__no_sanitize_address__))
71 #endif
72 
73 #if defined(__SANITIZE_THREAD__)
74 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
75 #else
76 #define __no_sanitize_thread
77 #endif
78 
79 #define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__))
80 
81 /*
82  * Only supported since gcc >= 12
83  */
84 #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
85 #define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__))
86 #else
87 #define __no_sanitize_coverage
88 #endif
89 
90 /*
91  * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel,
92  * matching the defines used by Clang.
93  */
94 #ifdef __SANITIZE_HWADDRESS__
95 #define __SANITIZE_ADDRESS__
96 #endif
97 
98 /*
99  * GCC does not support KMSAN.
100  */
101 #define __no_sanitize_memory
102 #define __no_kmsan_checks
103 
104 /*
105  * Turn individual warnings and errors on and off locally, depending
106  * on version.
107  */
108 #define __diag_GCC(version, severity, s) \
109 	__diag_GCC_ ## version(__diag_GCC_ ## severity s)
110 
111 /* Severity used in pragma directives */
112 #define __diag_GCC_ignore	ignored
113 #define __diag_GCC_warn		warning
114 #define __diag_GCC_error	error
115 
116 #define __diag_str1(s)		#s
117 #define __diag_str(s)		__diag_str1(s)
118 #define __diag(s)		_Pragma(__diag_str(GCC diagnostic s))
119 
120 #if GCC_VERSION >= 80000
121 #define __diag_GCC_8(s)		__diag(s)
122 #else
123 #define __diag_GCC_8(s)
124 #endif
125 
126 #define __diag_GCC_all(s)	__diag(s)
127 
128 #define __diag_ignore_all(option, comment) \
129 	__diag(__diag_GCC_ignore option)
130 
131 /*
132  * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size"
133  * attribute) do not work, and must be disabled.
134  */
135 #if GCC_VERSION < 90100
136 #undef __alloc_size__
137 #endif
138 
139 /*
140  * Declare compiler support for __typeof_unqual__() operator.
141  *
142  * Bindgen uses LLVM even if our C compiler is GCC, so we cannot
143  * rely on the auto-detected CONFIG_CC_HAS_TYPEOF_UNQUAL.
144  */
145 #define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >= 14)
146