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 /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 */
14 #if GCC_VERSION < 40900
15 # error Sorry, your version of GCC is too old - please use 4.9 or newer.
16 #endif
17 
18 /*
19  * This macro obfuscates arithmetic on a variable address so that gcc
20  * shouldn't recognize the original var, and make assumptions about it.
21  *
22  * This is needed because the C standard makes it undefined to do
23  * pointer arithmetic on "objects" outside their boundaries and the
24  * gcc optimizers assume this is the case. In particular they
25  * assume such arithmetic does not wrap.
26  *
27  * A miscompilation has been observed because of this on PPC.
28  * To work around it we hide the relationship of the pointer and the object
29  * using this macro.
30  *
31  * Versions of the ppc64 compiler before 4.1 had a bug where use of
32  * RELOC_HIDE could trash r30. The bug can be worked around by changing
33  * the inline assembly constraint from =g to =r, in this particular
34  * case either is valid.
35  */
36 #define RELOC_HIDE(ptr, off)						\
37 ({									\
38 	unsigned long __ptr;						\
39 	__asm__ ("" : "=r"(__ptr) : "0"(ptr));				\
40 	(typeof(ptr)) (__ptr + (off));					\
41 })
42 
43 #ifdef CONFIG_RETPOLINE
44 #define __noretpoline __attribute__((__indirect_branch__("keep")))
45 #endif
46 
47 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
48 
49 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
50 
51 #define __compiletime_warning(message) __attribute__((__warning__(message)))
52 #define __compiletime_error(message) __attribute__((__error__(message)))
53 
54 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
55 #define __latent_entropy __attribute__((latent_entropy))
56 #endif
57 
58 /*
59  * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
60  * confuse the stack allocation in gcc, leading to overly large stack
61  * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
62  *
63  * Adding an empty inline assembly before it works around the problem
64  */
65 #define barrier_before_unreachable() asm volatile("")
66 
67 /*
68  * Mark a position in code as unreachable.  This can be used to
69  * suppress control flow warnings after asm blocks that transfer
70  * control elsewhere.
71  */
72 #define unreachable() \
73 	do {					\
74 		annotate_unreachable();		\
75 		barrier_before_unreachable();	\
76 		__builtin_unreachable();	\
77 	} while (0)
78 
79 #if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__)
80 #define __randomize_layout __attribute__((randomize_layout))
81 #define __no_randomize_layout __attribute__((no_randomize_layout))
82 /* This anon struct can add padding, so only enable it under randstruct. */
83 #define randomized_struct_fields_start	struct {
84 #define randomized_struct_fields_end	} __randomize_layout;
85 #endif
86 
87 /*
88  * GCC 'asm goto' miscompiles certain code sequences:
89  *
90  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
91  *
92  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
93  *
94  * (asm goto is automatically volatile - the naming reflects this.)
95  */
96 #define asm_volatile_goto(x...)	do { asm goto(x); asm (""); } while (0)
97 
98 /*
99  * sparse (__CHECKER__) pretends to be gcc, but can't do constant
100  * folding in __builtin_bswap*() (yet), so don't set these for it.
101  */
102 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP) && !defined(__CHECKER__)
103 #define __HAVE_BUILTIN_BSWAP32__
104 #define __HAVE_BUILTIN_BSWAP64__
105 #define __HAVE_BUILTIN_BSWAP16__
106 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP && !__CHECKER__ */
107 
108 #if GCC_VERSION >= 70000
109 #define KASAN_ABI_VERSION 5
110 #elif GCC_VERSION >= 50000
111 #define KASAN_ABI_VERSION 4
112 #elif GCC_VERSION >= 40902
113 #define KASAN_ABI_VERSION 3
114 #endif
115 
116 #if __has_attribute(__no_sanitize_address__)
117 #define __no_sanitize_address __attribute__((no_sanitize_address))
118 #else
119 #define __no_sanitize_address
120 #endif
121 
122 #if defined(__SANITIZE_THREAD__) && __has_attribute(__no_sanitize_thread__)
123 #define __no_sanitize_thread __attribute__((no_sanitize_thread))
124 #else
125 #define __no_sanitize_thread
126 #endif
127 
128 #if __has_attribute(__no_sanitize_undefined__)
129 #define __no_sanitize_undefined __attribute__((no_sanitize_undefined))
130 #else
131 #define __no_sanitize_undefined
132 #endif
133 
134 #if GCC_VERSION >= 50100
135 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
136 #endif
137 
138 /*
139  * Turn individual warnings and errors on and off locally, depending
140  * on version.
141  */
142 #define __diag_GCC(version, severity, s) \
143 	__diag_GCC_ ## version(__diag_GCC_ ## severity s)
144 
145 /* Severity used in pragma directives */
146 #define __diag_GCC_ignore	ignored
147 #define __diag_GCC_warn		warning
148 #define __diag_GCC_error	error
149 
150 #define __diag_str1(s)		#s
151 #define __diag_str(s)		__diag_str1(s)
152 #define __diag(s)		_Pragma(__diag_str(GCC diagnostic s))
153 
154 #if GCC_VERSION >= 80000
155 #define __diag_GCC_8(s)		__diag(s)
156 #else
157 #define __diag_GCC_8(s)
158 #endif
159