e28b08957bb4a1b5726e0ac2f68e32523331a5bd
[cascardo/linux.git] / include / linux / compiler-gcc.h
1 #ifndef __LINUX_COMPILER_H
2 #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
3 #endif
4
5 /*
6  * Common definitions for all gcc versions go here.
7  */
8 #define GCC_VERSION (__GNUC__ * 10000           \
9                      + __GNUC_MINOR__ * 100     \
10                      + __GNUC_PATCHLEVEL__)
11
12
13 /* Optimization barrier */
14 /* The "volatile" is due to gcc bugs */
15 #define barrier() __asm__ __volatile__("": : :"memory")
16
17 /*
18  * This macro obfuscates arithmetic on a variable address so that gcc
19  * shouldn't recognize the original var, and make assumptions about it.
20  *
21  * This is needed because the C standard makes it undefined to do
22  * pointer arithmetic on "objects" outside their boundaries and the
23  * gcc optimizers assume this is the case. In particular they
24  * assume such arithmetic does not wrap.
25  *
26  * A miscompilation has been observed because of this on PPC.
27  * To work around it we hide the relationship of the pointer and the object
28  * using this macro.
29  *
30  * Versions of the ppc64 compiler before 4.1 had a bug where use of
31  * RELOC_HIDE could trash r30. The bug can be worked around by changing
32  * the inline assembly constraint from =g to =r, in this particular
33  * case either is valid.
34  */
35 #define RELOC_HIDE(ptr, off)                                            \
36 ({                                                                      \
37         unsigned long __ptr;                                            \
38         __asm__ ("" : "=r"(__ptr) : "0"(ptr));                          \
39         (typeof(ptr)) (__ptr + (off));                                  \
40 })
41
42 /* Make the optimizer believe the variable can be manipulated arbitrarily. */
43 #define OPTIMIZER_HIDE_VAR(var)                                         \
44         __asm__ ("" : "=r" (var) : "0" (var))
45
46 #ifdef __CHECKER__
47 #define __must_be_array(a)      0
48 #else
49 /* &a[0] degrades to a pointer: a different type from an array */
50 #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
51 #endif
52
53 /*
54  * Force always-inline if the user requests it so via the .config,
55  * or if gcc is too old:
56  */
57 #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||                \
58     !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
59 #define inline          inline          __attribute__((always_inline)) notrace
60 #define __inline__      __inline__      __attribute__((always_inline)) notrace
61 #define __inline        __inline        __attribute__((always_inline)) notrace
62 #else
63 /* A lot of inline functions can cause havoc with function tracing */
64 #define inline          inline          notrace
65 #define __inline__      __inline__      notrace
66 #define __inline        __inline        notrace
67 #endif
68
69 #define __always_inline inline __attribute__((always_inline))
70 #define  noinline       __attribute__((noinline))
71
72 #define __deprecated    __attribute__((deprecated))
73 #define __packed        __attribute__((packed))
74 #define __weak          __attribute__((weak))
75 #define __alias(symbol) __attribute__((alias(#symbol)))
76
77 /*
78  * it doesn't make sense on ARM (currently the only user of __naked)
79  * to trace naked functions because then mcount is called without
80  * stack and frame pointer being set up and there is no chance to
81  * restore the lr register to the value before mcount was called.
82  *
83  * The asm() bodies of naked functions often depend on standard calling
84  * conventions, therefore they must be noinline and noclone.
85  *
86  * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
87  * See GCC PR44290.
88  */
89 #define __naked         __attribute__((naked)) noinline __noclone notrace
90
91 #define __noreturn      __attribute__((noreturn))
92
93 /*
94  * From the GCC manual:
95  *
96  * Many functions have no effects except the return value and their
97  * return value depends only on the parameters and/or global
98  * variables.  Such a function can be subject to common subexpression
99  * elimination and loop optimization just as an arithmetic operator
100  * would be.
101  * [...]
102  */
103 #define __pure                  __attribute__((pure))
104 #define __aligned(x)            __attribute__((aligned(x)))
105 #define __printf(a, b)          __attribute__((format(printf, a, b)))
106 #define __scanf(a, b)           __attribute__((format(scanf, a, b)))
107 #define __attribute_const__     __attribute__((__const__))
108 #define __maybe_unused          __attribute__((unused))
109 #define __always_unused         __attribute__((unused))
110
111 /* gcc version specific checks */
112
113 #if GCC_VERSION < 30200
114 # error Sorry, your compiler is too old - please upgrade it.
115 #endif
116
117 #if GCC_VERSION < 30300
118 # define __used                 __attribute__((__unused__))
119 #else
120 # define __used                 __attribute__((__used__))
121 #endif
122
123 #ifdef CONFIG_GCOV_KERNEL
124 # if GCC_VERSION < 30400
125 #   error "GCOV profiling support for gcc versions below 3.4 not included"
126 # endif /* __GNUC_MINOR__ */
127 #endif /* CONFIG_GCOV_KERNEL */
128
129 #if GCC_VERSION >= 30400
130 #define __must_check            __attribute__((warn_unused_result))
131 #endif
132
133 #if GCC_VERSION >= 40000
134
135 /* GCC 4.1.[01] miscompiles __weak */
136 #ifdef __KERNEL__
137 # if GCC_VERSION >= 40100 &&  GCC_VERSION <= 40101
138 #  error Your version of gcc miscompiles the __weak directive
139 # endif
140 #endif
141
142 #define __used                  __attribute__((__used__))
143 #define __compiler_offsetof(a, b)                                       \
144         __builtin_offsetof(a, b)
145
146 #if GCC_VERSION >= 40100 && GCC_VERSION < 40600
147 # define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
148 #endif
149
150 #if GCC_VERSION >= 40300
151 /* Mark functions as cold. gcc will assume any path leading to a call
152  * to them will be unlikely.  This means a lot of manual unlikely()s
153  * are unnecessary now for any paths leading to the usual suspects
154  * like BUG(), printk(), panic() etc. [but let's keep them for now for
155  * older compilers]
156  *
157  * Early snapshots of gcc 4.3 don't support this and we can't detect this
158  * in the preprocessor, but we can live with this because they're unreleased.
159  * Maketime probing would be overkill here.
160  *
161  * gcc also has a __attribute__((__hot__)) to move hot functions into
162  * a special section, but I don't see any sense in this right now in
163  * the kernel context
164  */
165 #define __cold                  __attribute__((__cold__))
166
167 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
168
169 #ifndef __CHECKER__
170 # define __compiletime_warning(message) __attribute__((warning(message)))
171 # define __compiletime_error(message) __attribute__((error(message)))
172 #endif /* __CHECKER__ */
173 #endif /* GCC_VERSION >= 40300 */
174
175 #if GCC_VERSION >= 40500
176 /*
177  * Mark a position in code as unreachable.  This can be used to
178  * suppress control flow warnings after asm blocks that transfer
179  * control elsewhere.
180  *
181  * Early snapshots of gcc 4.5 don't support this and we can't detect
182  * this in the preprocessor, but we can live with this because they're
183  * unreleased.  Really, we need to have autoconf for the kernel.
184  */
185 #define unreachable() __builtin_unreachable()
186
187 /* Mark a function definition as prohibited from being cloned. */
188 #define __noclone       __attribute__((__noclone__))
189
190 #endif /* GCC_VERSION >= 40500 */
191
192 #if GCC_VERSION >= 40600
193 /*
194  * Tell the optimizer that something else uses this function or variable.
195  */
196 #define __visible       __attribute__((externally_visible))
197 #endif
198
199 /*
200  * GCC 'asm goto' miscompiles certain code sequences:
201  *
202  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
203  *
204  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
205  *
206  * (asm goto is automatically volatile - the naming reflects this.)
207  */
208 #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
209
210 #ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
211 #if GCC_VERSION >= 40400
212 #define __HAVE_BUILTIN_BSWAP32__
213 #define __HAVE_BUILTIN_BSWAP64__
214 #endif
215 #if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600)
216 #define __HAVE_BUILTIN_BSWAP16__
217 #endif
218 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
219
220 #if GCC_VERSION >= 50000
221 #define KASAN_ABI_VERSION 4
222 #elif GCC_VERSION >= 40902
223 #define KASAN_ABI_VERSION 3
224 #endif
225
226 #endif  /* gcc version >= 40000 specific checks */
227
228 #if !defined(__noclone)
229 #define __noclone       /* not needed */
230 #endif
231
232 /*
233  * A trick to suppress uninitialized variable warning without generating any
234  * code
235  */
236 #define uninitialized_var(x) x = x