Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
d1515582 | 2 | #ifndef __LINUX_COMPILER_TYPES_H |
94f582f8 RD |
3 | #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead." |
4 | #endif | |
1da177e4 LT |
5 | |
6 | /* | |
7 | * Common definitions for all gcc versions go here. | |
8 | */ | |
f6d133f8 JP |
9 | #define GCC_VERSION (__GNUC__ * 10000 \ |
10 | + __GNUC_MINOR__ * 100 \ | |
11 | + __GNUC_PATCHLEVEL__) | |
1da177e4 | 12 | |
c8d52465 | 13 | /* |
85c210ed AK |
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 | * | |
c8d52465 AB |
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 | */ | |
f6d133f8 JP |
31 | #define RELOC_HIDE(ptr, off) \ |
32 | ({ \ | |
33 | unsigned long __ptr; \ | |
34 | __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \ | |
35 | (typeof(ptr)) (__ptr + (off)); \ | |
36 | }) | |
a1365647 | 37 | |
aefb2f2e | 38 | #ifdef CONFIG_MITIGATION_RETPOLINE |
5c67a52f | 39 | #define __noretpoline __attribute__((__indirect_branch__("keep"))) |
87358710 DW |
40 | #endif |
41 | ||
989bd500 | 42 | #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) |
0766f788 ER |
43 | #define __latent_entropy __attribute__((latent_entropy)) |
44 | #endif | |
0766f788 | 45 | |
173a3efd AB |
46 | /* |
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 | |
50 | * | |
51 | * Adding an empty inline assembly before it works around the problem | |
52 | */ | |
53 | #define barrier_before_unreachable() asm volatile("") | |
54 | ||
cb984d10 JP |
55 | /* |
56 | * Mark a position in code as unreachable. This can be used to | |
57 | * suppress control flow warnings after asm blocks that transfer | |
58 | * control elsewhere. | |
cb984d10 | 59 | */ |
4e4636cf | 60 | #define unreachable() \ |
173a3efd AB |
61 | do { \ |
62 | annotate_unreachable(); \ | |
63 | barrier_before_unreachable(); \ | |
64 | __builtin_unreachable(); \ | |
65 | } while (0) | |
cb984d10 | 66 | |
4356e9f8 LT |
67 | /* |
68 | * GCC 'asm goto' with outputs miscompiles certain code sequences: | |
69 | * | |
68fb3ca0 | 70 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113921 |
4356e9f8 | 71 | * |
68fb3ca0 | 72 | * Work around it via the same compiler barrier quirk that we used |
4356e9f8 LT |
73 | * to use for the old 'asm goto' workaround. |
74 | * | |
75 | * Also, always mark such 'asm goto' statements as volatile: all | |
76 | * asm goto statements are supposed to be volatile as per the | |
77 | * documentation, but some versions of gcc didn't actually do | |
78 | * that for asms with outputs: | |
79 | * | |
80 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98619 | |
81 | */ | |
68fb3ca0 | 82 | #ifdef CONFIG_GCC_ASM_GOTO_OUTPUT_WORKAROUND |
4356e9f8 LT |
83 | #define asm_goto_output(x...) \ |
84 | do { asm volatile goto(x); asm (""); } while (0) | |
68fb3ca0 | 85 | #endif |
4356e9f8 | 86 | |
d991bb1c | 87 | #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP) |
cb984d10 JP |
88 | #define __HAVE_BUILTIN_BSWAP32__ |
89 | #define __HAVE_BUILTIN_BSWAP64__ | |
cb984d10 | 90 | #define __HAVE_BUILTIN_BSWAP16__ |
d991bb1c | 91 | #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */ |
cb984d10 | 92 | |
045d599a DV |
93 | #if GCC_VERSION >= 70000 |
94 | #define KASAN_ABI_VERSION 5 | |
4e59869a | 95 | #else |
cb984d10 | 96 | #define KASAN_ABI_VERSION 4 |
cb984d10 JP |
97 | #endif |
98 | ||
afcf5441 DL |
99 | #ifdef CONFIG_SHADOW_CALL_STACK |
100 | #define __noscs __attribute__((__no_sanitize__("shadow-call-stack"))) | |
101 | #endif | |
102 | ||
6e2be1f2 | 103 | #define __no_sanitize_address __attribute__((__no_sanitize_address__)) |
2bd926b4 | 104 | |
095ac076 | 105 | #if defined(__SANITIZE_THREAD__) |
6e2be1f2 | 106 | #define __no_sanitize_thread __attribute__((__no_sanitize_thread__)) |
dfd402a4 ME |
107 | #else |
108 | #define __no_sanitize_thread | |
109 | #endif | |
110 | ||
6e2be1f2 | 111 | #define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__)) |
5144f8a8 | 112 | |
f39556bc MO |
113 | /* |
114 | * Only supported since gcc >= 12 | |
115 | */ | |
540540d0 | 116 | #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__) |
6e2be1f2 | 117 | #define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__)) |
540540d0 ME |
118 | #else |
119 | #define __no_sanitize_coverage | |
120 | #endif | |
121 | ||
9a48e756 KC |
122 | /* |
123 | * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel, | |
124 | * matching the defines used by Clang. | |
125 | */ | |
126 | #ifdef __SANITIZE_HWADDRESS__ | |
127 | #define __SANITIZE_ADDRESS__ | |
128 | #endif | |
129 | ||
9b448bc2 AP |
130 | /* |
131 | * GCC does not support KMSAN. | |
132 | */ | |
133 | #define __no_sanitize_memory | |
134 | #define __no_kmsan_checks | |
135 | ||
8793bb7f AB |
136 | /* |
137 | * Turn individual warnings and errors on and off locally, depending | |
138 | * on version. | |
139 | */ | |
140 | #define __diag_GCC(version, severity, s) \ | |
141 | __diag_GCC_ ## version(__diag_GCC_ ## severity s) | |
142 | ||
143 | /* Severity used in pragma directives */ | |
144 | #define __diag_GCC_ignore ignored | |
145 | #define __diag_GCC_warn warning | |
146 | #define __diag_GCC_error error | |
147 | ||
8793bb7f AB |
148 | #define __diag_str1(s) #s |
149 | #define __diag_str(s) __diag_str1(s) | |
150 | #define __diag(s) _Pragma(__diag_str(GCC diagnostic s)) | |
8793bb7f AB |
151 | |
152 | #if GCC_VERSION >= 80000 | |
153 | #define __diag_GCC_8(s) __diag(s) | |
154 | #else | |
155 | #define __diag_GCC_8(s) | |
156 | #endif | |
86cffecd | 157 | |
4d1ea705 | 158 | #define __diag_ignore_all(option, comment) \ |
689b097a | 159 | __diag(__diag_GCC_ignore option) |
4d1ea705 | 160 | |
86cffecd KC |
161 | /* |
162 | * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size" | |
163 | * attribute) do not work, and must be disabled. | |
164 | */ | |
165 | #if GCC_VERSION < 90100 | |
166 | #undef __alloc_size__ | |
167 | #endif |