xref: /openbmc/u-boot/include/linux/compiler-gcc.h (revision 9b2c282b348dfe966bbba967dc7a45ce817cce50)
195ffaba3SGraeme Russ #ifndef __LINUX_COMPILER_H
295ffaba3SGraeme Russ #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
395ffaba3SGraeme Russ #endif
495ffaba3SGraeme Russ 
595ffaba3SGraeme Russ /*
695ffaba3SGraeme Russ  * Common definitions for all gcc versions go here.
795ffaba3SGraeme Russ  */
8fb8ffd7cSMasahiro Yamada #define GCC_VERSION (__GNUC__ * 10000		\
9fb8ffd7cSMasahiro Yamada 		     + __GNUC_MINOR__ * 100	\
10fb8ffd7cSMasahiro Yamada 		     + __GNUC_PATCHLEVEL__)
1195ffaba3SGraeme Russ 
1295ffaba3SGraeme Russ /* Optimization barrier */
13*9b2c282bSTom Rini 
1495ffaba3SGraeme Russ /* The "volatile" is due to gcc bugs */
1595ffaba3SGraeme Russ #define barrier() __asm__ __volatile__("": : :"memory")
16*9b2c282bSTom Rini /*
17*9b2c282bSTom Rini  * This version is i.e. to prevent dead stores elimination on @ptr
18*9b2c282bSTom Rini  * where gcc and llvm may behave differently when otherwise using
19*9b2c282bSTom Rini  * normal barrier(): while gcc behavior gets along with a normal
20*9b2c282bSTom Rini  * barrier(), llvm needs an explicit input variable to be assumed
21*9b2c282bSTom Rini  * clobbered. The issue is as follows: while the inline asm might
22*9b2c282bSTom Rini  * access any memory it wants, the compiler could have fit all of
23*9b2c282bSTom Rini  * @ptr into memory registers instead, and since @ptr never escaped
24*9b2c282bSTom Rini  * from that, it proofed that the inline asm wasn't touching any of
25*9b2c282bSTom Rini  * it. This version works well with both compilers, i.e. we're telling
26*9b2c282bSTom Rini  * the compiler that the inline asm absolutely may see the contents
27*9b2c282bSTom Rini  * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
28*9b2c282bSTom Rini  */
29*9b2c282bSTom Rini #define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
3095ffaba3SGraeme Russ 
3195ffaba3SGraeme Russ /*
3295ffaba3SGraeme Russ  * This macro obfuscates arithmetic on a variable address so that gcc
3395ffaba3SGraeme Russ  * shouldn't recognize the original var, and make assumptions about it.
3495ffaba3SGraeme Russ  *
3595ffaba3SGraeme Russ  * This is needed because the C standard makes it undefined to do
3695ffaba3SGraeme Russ  * pointer arithmetic on "objects" outside their boundaries and the
3795ffaba3SGraeme Russ  * gcc optimizers assume this is the case. In particular they
3895ffaba3SGraeme Russ  * assume such arithmetic does not wrap.
3995ffaba3SGraeme Russ  *
4095ffaba3SGraeme Russ  * A miscompilation has been observed because of this on PPC.
4195ffaba3SGraeme Russ  * To work around it we hide the relationship of the pointer and the object
4295ffaba3SGraeme Russ  * using this macro.
4395ffaba3SGraeme Russ  *
4495ffaba3SGraeme Russ  * Versions of the ppc64 compiler before 4.1 had a bug where use of
4595ffaba3SGraeme Russ  * RELOC_HIDE could trash r30. The bug can be worked around by changing
4695ffaba3SGraeme Russ  * the inline assembly constraint from =g to =r, in this particular
4795ffaba3SGraeme Russ  * case either is valid.
4895ffaba3SGraeme Russ  */
4995ffaba3SGraeme Russ #define RELOC_HIDE(ptr, off)						\
50*9b2c282bSTom Rini ({									\
51*9b2c282bSTom Rini 	unsigned long __ptr;						\
5295ffaba3SGraeme Russ 	__asm__ ("" : "=r"(__ptr) : "0"(ptr));				\
53*9b2c282bSTom Rini 	(typeof(ptr)) (__ptr + (off));					\
54*9b2c282bSTom Rini })
5595ffaba3SGraeme Russ 
56fb8ffd7cSMasahiro Yamada /* Make the optimizer believe the variable can be manipulated arbitrarily. */
57*9b2c282bSTom Rini #define OPTIMIZER_HIDE_VAR(var)						\
58*9b2c282bSTom Rini 	__asm__ ("" : "=r" (var) : "0" (var))
59fb8ffd7cSMasahiro Yamada 
60fb8ffd7cSMasahiro Yamada #ifdef __CHECKER__
61*9b2c282bSTom Rini #define __must_be_array(a)	0
62fb8ffd7cSMasahiro Yamada #else
6395ffaba3SGraeme Russ /* &a[0] degrades to a pointer: a different type from an array */
64fb8ffd7cSMasahiro Yamada #define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
65fb8ffd7cSMasahiro Yamada #endif
6695ffaba3SGraeme Russ 
6795ffaba3SGraeme Russ /*
6895ffaba3SGraeme Russ  * Force always-inline if the user requests it so via the .config,
6995ffaba3SGraeme Russ  * or if gcc is too old:
7095ffaba3SGraeme Russ  */
7195ffaba3SGraeme Russ #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||		\
7295ffaba3SGraeme Russ     !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
73fb8ffd7cSMasahiro Yamada #define inline		inline		__attribute__((always_inline)) notrace
74fb8ffd7cSMasahiro Yamada #define __inline__	__inline__	__attribute__((always_inline)) notrace
75fb8ffd7cSMasahiro Yamada #define __inline	__inline	__attribute__((always_inline)) notrace
76fb8ffd7cSMasahiro Yamada #else
77fb8ffd7cSMasahiro Yamada /* A lot of inline functions can cause havoc with function tracing */
78fb8ffd7cSMasahiro Yamada #define inline		inline		notrace
79fb8ffd7cSMasahiro Yamada #define __inline__	__inline__	notrace
80fb8ffd7cSMasahiro Yamada #define __inline	__inline	notrace
8195ffaba3SGraeme Russ #endif
8295ffaba3SGraeme Russ 
83*9b2c282bSTom Rini #define __always_inline	inline __attribute__((always_inline))
84*9b2c282bSTom Rini #define  noinline	__attribute__((noinline))
85*9b2c282bSTom Rini 
8695ffaba3SGraeme Russ #define __deprecated	__attribute__((deprecated))
8795ffaba3SGraeme Russ #define __packed	__attribute__((packed))
8895ffaba3SGraeme Russ #define __weak		__attribute__((weak))
89*9b2c282bSTom Rini #define __alias(symbol)	__attribute__((alias(#symbol)))
9095ffaba3SGraeme Russ 
9195ffaba3SGraeme Russ /*
92*9b2c282bSTom Rini  * it doesn't make sense on ARM (currently the only user of __naked)
93*9b2c282bSTom Rini  * to trace naked functions because then mcount is called without
94*9b2c282bSTom Rini  * stack and frame pointer being set up and there is no chance to
95*9b2c282bSTom Rini  * restore the lr register to the value before mcount was called.
96fb8ffd7cSMasahiro Yamada  *
97*9b2c282bSTom Rini  * The asm() bodies of naked functions often depend on standard calling
98*9b2c282bSTom Rini  * conventions, therefore they must be noinline and noclone.
99*9b2c282bSTom Rini  *
100*9b2c282bSTom Rini  * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
101*9b2c282bSTom Rini  * See GCC PR44290.
10295ffaba3SGraeme Russ  */
103fb8ffd7cSMasahiro Yamada #define __naked		__attribute__((naked)) noinline __noclone notrace
10495ffaba3SGraeme Russ 
10595ffaba3SGraeme Russ #define __noreturn	__attribute__((noreturn))
10695ffaba3SGraeme Russ 
10795ffaba3SGraeme Russ /*
10895ffaba3SGraeme Russ  * From the GCC manual:
10995ffaba3SGraeme Russ  *
11095ffaba3SGraeme Russ  * Many functions have no effects except the return value and their
11195ffaba3SGraeme Russ  * return value depends only on the parameters and/or global
11295ffaba3SGraeme Russ  * variables.  Such a function can be subject to common subexpression
11395ffaba3SGraeme Russ  * elimination and loop optimization just as an arithmetic operator
11495ffaba3SGraeme Russ  * would be.
11595ffaba3SGraeme Russ  * [...]
11695ffaba3SGraeme Russ  */
11795ffaba3SGraeme Russ #define __pure			__attribute__((pure))
11895ffaba3SGraeme Russ #define __aligned(x)		__attribute__((aligned(x)))
11995ffaba3SGraeme Russ #define __printf(a, b)		__attribute__((format(printf, a, b)))
120fb8ffd7cSMasahiro Yamada #define __scanf(a, b)		__attribute__((format(scanf, a, b)))
12195ffaba3SGraeme Russ #define __attribute_const__	__attribute__((__const__))
12295ffaba3SGraeme Russ #define __maybe_unused		__attribute__((unused))
12395ffaba3SGraeme Russ #define __always_unused		__attribute__((unused))
12495ffaba3SGraeme Russ 
125*9b2c282bSTom Rini /* gcc version specific checks */
126*9b2c282bSTom Rini 
127*9b2c282bSTom Rini #if GCC_VERSION < 30200
128*9b2c282bSTom Rini # error Sorry, your compiler is too old - please upgrade it.
129*9b2c282bSTom Rini #endif
130*9b2c282bSTom Rini 
131*9b2c282bSTom Rini #if GCC_VERSION < 30300
132*9b2c282bSTom Rini # define __used			__attribute__((__unused__))
133*9b2c282bSTom Rini #else
134*9b2c282bSTom Rini # define __used			__attribute__((__used__))
135*9b2c282bSTom Rini #endif
136*9b2c282bSTom Rini 
137*9b2c282bSTom Rini #ifdef CONFIG_GCOV_KERNEL
138*9b2c282bSTom Rini # if GCC_VERSION < 30400
139*9b2c282bSTom Rini #   error "GCOV profiling support for gcc versions below 3.4 not included"
140*9b2c282bSTom Rini # endif /* __GNUC_MINOR__ */
141*9b2c282bSTom Rini #endif /* CONFIG_GCOV_KERNEL */
142*9b2c282bSTom Rini 
143*9b2c282bSTom Rini #if GCC_VERSION >= 30400
144*9b2c282bSTom Rini #define __must_check		__attribute__((warn_unused_result))
145*9b2c282bSTom Rini #endif
146*9b2c282bSTom Rini 
147*9b2c282bSTom Rini #if GCC_VERSION >= 40000
148*9b2c282bSTom Rini 
149*9b2c282bSTom Rini /* GCC 4.1.[01] miscompiles __weak */
150*9b2c282bSTom Rini #ifdef __KERNEL__
151*9b2c282bSTom Rini # if GCC_VERSION >= 40100 &&  GCC_VERSION <= 40101
152*9b2c282bSTom Rini #  error Your version of gcc miscompiles the __weak directive
153*9b2c282bSTom Rini # endif
154*9b2c282bSTom Rini #endif
155*9b2c282bSTom Rini 
156*9b2c282bSTom Rini #define __used			__attribute__((__used__))
157*9b2c282bSTom Rini #define __compiler_offsetof(a, b)					\
158*9b2c282bSTom Rini 	__builtin_offsetof(a, b)
159*9b2c282bSTom Rini 
160*9b2c282bSTom Rini #if GCC_VERSION >= 40100 && GCC_VERSION < 40600
161*9b2c282bSTom Rini # define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
162*9b2c282bSTom Rini #endif
163*9b2c282bSTom Rini 
164*9b2c282bSTom Rini #if GCC_VERSION >= 40300
165*9b2c282bSTom Rini /* Mark functions as cold. gcc will assume any path leading to a call
166*9b2c282bSTom Rini  * to them will be unlikely.  This means a lot of manual unlikely()s
167*9b2c282bSTom Rini  * are unnecessary now for any paths leading to the usual suspects
168*9b2c282bSTom Rini  * like BUG(), printk(), panic() etc. [but let's keep them for now for
169*9b2c282bSTom Rini  * older compilers]
170*9b2c282bSTom Rini  *
171*9b2c282bSTom Rini  * Early snapshots of gcc 4.3 don't support this and we can't detect this
172*9b2c282bSTom Rini  * in the preprocessor, but we can live with this because they're unreleased.
173*9b2c282bSTom Rini  * Maketime probing would be overkill here.
174*9b2c282bSTom Rini  *
175*9b2c282bSTom Rini  * gcc also has a __attribute__((__hot__)) to move hot functions into
176*9b2c282bSTom Rini  * a special section, but I don't see any sense in this right now in
177*9b2c282bSTom Rini  * the kernel context
178*9b2c282bSTom Rini  */
179*9b2c282bSTom Rini #define __cold			__attribute__((__cold__))
180*9b2c282bSTom Rini 
181*9b2c282bSTom Rini #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
182*9b2c282bSTom Rini 
183*9b2c282bSTom Rini #ifndef __CHECKER__
184*9b2c282bSTom Rini # define __compiletime_warning(message) __attribute__((warning(message)))
185*9b2c282bSTom Rini # define __compiletime_error(message) __attribute__((error(message)))
186*9b2c282bSTom Rini #endif /* __CHECKER__ */
187*9b2c282bSTom Rini #endif /* GCC_VERSION >= 40300 */
188*9b2c282bSTom Rini 
189*9b2c282bSTom Rini #if GCC_VERSION >= 40500
190*9b2c282bSTom Rini /*
191*9b2c282bSTom Rini  * Mark a position in code as unreachable.  This can be used to
192*9b2c282bSTom Rini  * suppress control flow warnings after asm blocks that transfer
193*9b2c282bSTom Rini  * control elsewhere.
194*9b2c282bSTom Rini  *
195*9b2c282bSTom Rini  * Early snapshots of gcc 4.5 don't support this and we can't detect
196*9b2c282bSTom Rini  * this in the preprocessor, but we can live with this because they're
197*9b2c282bSTom Rini  * unreleased.  Really, we need to have autoconf for the kernel.
198*9b2c282bSTom Rini  */
199*9b2c282bSTom Rini #define unreachable() __builtin_unreachable()
200*9b2c282bSTom Rini 
201*9b2c282bSTom Rini /* Mark a function definition as prohibited from being cloned. */
202*9b2c282bSTom Rini #define __noclone	__attribute__((__noclone__))
203*9b2c282bSTom Rini 
204*9b2c282bSTom Rini #endif /* GCC_VERSION >= 40500 */
205*9b2c282bSTom Rini 
206*9b2c282bSTom Rini #if GCC_VERSION >= 40600
207*9b2c282bSTom Rini /*
208*9b2c282bSTom Rini  * When used with Link Time Optimization, gcc can optimize away C functions or
209*9b2c282bSTom Rini  * variables which are referenced only from assembly code.  __visible tells the
210*9b2c282bSTom Rini  * optimizer that something else uses this function or variable, thus preventing
211*9b2c282bSTom Rini  * this.
212*9b2c282bSTom Rini  */
213*9b2c282bSTom Rini #define __visible	__attribute__((externally_visible))
214*9b2c282bSTom Rini #endif
215*9b2c282bSTom Rini 
216*9b2c282bSTom Rini 
217*9b2c282bSTom Rini #if GCC_VERSION >= 40900 && !defined(__CHECKER__)
218*9b2c282bSTom Rini /*
219*9b2c282bSTom Rini  * __assume_aligned(n, k): Tell the optimizer that the returned
220*9b2c282bSTom Rini  * pointer can be assumed to be k modulo n. The second argument is
221*9b2c282bSTom Rini  * optional (default 0), so we use a variadic macro to make the
222*9b2c282bSTom Rini  * shorthand.
223*9b2c282bSTom Rini  *
224*9b2c282bSTom Rini  * Beware: Do not apply this to functions which may return
225*9b2c282bSTom Rini  * ERR_PTRs. Also, it is probably unwise to apply it to functions
226*9b2c282bSTom Rini  * returning extra information in the low bits (but in that case the
227*9b2c282bSTom Rini  * compiler should see some alignment anyway, when the return value is
228*9b2c282bSTom Rini  * massaged by 'flags = ptr & 3; ptr &= ~3;').
229*9b2c282bSTom Rini  */
230*9b2c282bSTom Rini #define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
231*9b2c282bSTom Rini #endif
232*9b2c282bSTom Rini 
233*9b2c282bSTom Rini /*
234*9b2c282bSTom Rini  * GCC 'asm goto' miscompiles certain code sequences:
235*9b2c282bSTom Rini  *
236*9b2c282bSTom Rini  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
237*9b2c282bSTom Rini  *
238*9b2c282bSTom Rini  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
239*9b2c282bSTom Rini  *
240*9b2c282bSTom Rini  * (asm goto is automatically volatile - the naming reflects this.)
241*9b2c282bSTom Rini  */
242*9b2c282bSTom Rini #define asm_volatile_goto(x...)	do { asm goto(x); asm (""); } while (0)
243*9b2c282bSTom Rini 
244*9b2c282bSTom Rini #ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
245*9b2c282bSTom Rini #if GCC_VERSION >= 40400
246*9b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP32__
247*9b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP64__
248*9b2c282bSTom Rini #endif
249*9b2c282bSTom Rini #if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600)
250*9b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP16__
251*9b2c282bSTom Rini #endif
252*9b2c282bSTom Rini #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
253*9b2c282bSTom Rini 
254*9b2c282bSTom Rini #if GCC_VERSION >= 50000
255*9b2c282bSTom Rini #define KASAN_ABI_VERSION 4
256*9b2c282bSTom Rini #elif GCC_VERSION >= 40902
257*9b2c282bSTom Rini #define KASAN_ABI_VERSION 3
258*9b2c282bSTom Rini #endif
259*9b2c282bSTom Rini 
260*9b2c282bSTom Rini #if GCC_VERSION >= 40902
261*9b2c282bSTom Rini /*
262*9b2c282bSTom Rini  * Tell the compiler that address safety instrumentation (KASAN)
263*9b2c282bSTom Rini  * should not be applied to that function.
264*9b2c282bSTom Rini  * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
265*9b2c282bSTom Rini  */
266*9b2c282bSTom Rini #define __no_sanitize_address __attribute__((no_sanitize_address))
267*9b2c282bSTom Rini #endif
268*9b2c282bSTom Rini 
269*9b2c282bSTom Rini #endif	/* gcc version >= 40000 specific checks */
270fb8ffd7cSMasahiro Yamada 
271fb8ffd7cSMasahiro Yamada #if !defined(__noclone)
272fb8ffd7cSMasahiro Yamada #define __noclone	/* not needed */
273fb8ffd7cSMasahiro Yamada #endif
274fb8ffd7cSMasahiro Yamada 
275*9b2c282bSTom Rini #if !defined(__no_sanitize_address)
276*9b2c282bSTom Rini #define __no_sanitize_address
277*9b2c282bSTom Rini #endif
278*9b2c282bSTom Rini 
279fb8ffd7cSMasahiro Yamada /*
280fb8ffd7cSMasahiro Yamada  * A trick to suppress uninitialized variable warning without generating any
281fb8ffd7cSMasahiro Yamada  * code
282fb8ffd7cSMasahiro Yamada  */
283fb8ffd7cSMasahiro Yamada #define uninitialized_var(x) x = x
284