Merge tag 'compiler-attributes-for-linus-v5.15-rc1-v2' of git://github.com/ojeda...
[linux-2.6-microblaze.git] / include / linux / compiler-gcc.h
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 /*
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 #ifdef CONFIG_RETPOLINE
39 #define __noretpoline __attribute__((__indirect_branch__("keep")))
40 #endif
41
42 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
43
44 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
45
46 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
47 #define __latent_entropy __attribute__((latent_entropy))
48 #endif
49
50 /*
51  * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
52  * confuse the stack allocation in gcc, leading to overly large stack
53  * frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
54  *
55  * Adding an empty inline assembly before it works around the problem
56  */
57 #define barrier_before_unreachable() asm volatile("")
58
59 /*
60  * Mark a position in code as unreachable.  This can be used to
61  * suppress control flow warnings after asm blocks that transfer
62  * control elsewhere.
63  */
64 #define unreachable() \
65         do {                                    \
66                 annotate_unreachable();         \
67                 barrier_before_unreachable();   \
68                 __builtin_unreachable();        \
69         } while (0)
70
71 #if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__)
72 #define __randomize_layout __attribute__((randomize_layout))
73 #define __no_randomize_layout __attribute__((no_randomize_layout))
74 /* This anon struct can add padding, so only enable it under randstruct. */
75 #define randomized_struct_fields_start  struct {
76 #define randomized_struct_fields_end    } __randomize_layout;
77 #endif
78
79 /*
80  * GCC 'asm goto' miscompiles certain code sequences:
81  *
82  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
83  *
84  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
85  *
86  * (asm goto is automatically volatile - the naming reflects this.)
87  */
88 #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
89
90 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
91 #define __HAVE_BUILTIN_BSWAP32__
92 #define __HAVE_BUILTIN_BSWAP64__
93 #define __HAVE_BUILTIN_BSWAP16__
94 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
95
96 #if GCC_VERSION >= 70000
97 #define KASAN_ABI_VERSION 5
98 #elif GCC_VERSION >= 50000
99 #define KASAN_ABI_VERSION 4
100 #elif GCC_VERSION >= 40902
101 #define KASAN_ABI_VERSION 3
102 #endif
103
104 #if __has_attribute(__no_sanitize_address__)
105 #define __no_sanitize_address __attribute__((no_sanitize_address))
106 #else
107 #define __no_sanitize_address
108 #endif
109
110 #if defined(__SANITIZE_THREAD__) && __has_attribute(__no_sanitize_thread__)
111 #define __no_sanitize_thread __attribute__((no_sanitize_thread))
112 #else
113 #define __no_sanitize_thread
114 #endif
115
116 #if __has_attribute(__no_sanitize_undefined__)
117 #define __no_sanitize_undefined __attribute__((no_sanitize_undefined))
118 #else
119 #define __no_sanitize_undefined
120 #endif
121
122 #if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
123 #define __no_sanitize_coverage __attribute__((no_sanitize_coverage))
124 #else
125 #define __no_sanitize_coverage
126 #endif
127
128 #if GCC_VERSION >= 50100
129 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
130 #endif
131
132 /*
133  * Turn individual warnings and errors on and off locally, depending
134  * on version.
135  */
136 #define __diag_GCC(version, severity, s) \
137         __diag_GCC_ ## version(__diag_GCC_ ## severity s)
138
139 /* Severity used in pragma directives */
140 #define __diag_GCC_ignore       ignored
141 #define __diag_GCC_warn         warning
142 #define __diag_GCC_error        error
143
144 #define __diag_str1(s)          #s
145 #define __diag_str(s)           __diag_str1(s)
146 #define __diag(s)               _Pragma(__diag_str(GCC diagnostic s))
147
148 #if GCC_VERSION >= 80000
149 #define __diag_GCC_8(s)         __diag(s)
150 #else
151 #define __diag_GCC_8(s)
152 #endif