xref: /OK3568_Linux_fs/kernel/include/linux/compiler-clang.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __LINUX_COMPILER_TYPES_H
3*4882a593Smuzhiyun #error "Please don't include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
4*4882a593Smuzhiyun #endif
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #define CLANG_VERSION (__clang_major__ * 10000	\
7*4882a593Smuzhiyun 		     + __clang_minor__ * 100	\
8*4882a593Smuzhiyun 		     + __clang_patchlevel__)
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #if CLANG_VERSION < 100001
11*4882a593Smuzhiyun #ifndef __BPF_TRACING__
12*4882a593Smuzhiyun # error Sorry, your version of Clang is too old - please use 10.0.1 or newer.
13*4882a593Smuzhiyun #endif
14*4882a593Smuzhiyun #endif
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* Compiler specific definitions for Clang compiler */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* same as gcc, this was present in clang-2.6 so we can assume it works
19*4882a593Smuzhiyun  * with any version that can compile the kernel
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* all clang versions usable with the kernel support KASAN ABI version 5 */
24*4882a593Smuzhiyun #define KASAN_ABI_VERSION 5
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Note: Checking __has_feature(*_sanitizer) is only true if the feature is
28*4882a593Smuzhiyun  * enabled. Therefore it is not required to additionally check defined(CONFIG_*)
29*4882a593Smuzhiyun  * to avoid adding redundant attributes in other configurations.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
33*4882a593Smuzhiyun /* Emulate GCC's __SANITIZE_ADDRESS__ flag */
34*4882a593Smuzhiyun #define __SANITIZE_ADDRESS__
35*4882a593Smuzhiyun #define __no_sanitize_address \
36*4882a593Smuzhiyun 		__attribute__((no_sanitize("address", "hwaddress")))
37*4882a593Smuzhiyun #else
38*4882a593Smuzhiyun #define __no_sanitize_address
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #if __has_feature(thread_sanitizer)
42*4882a593Smuzhiyun /* emulate gcc's __SANITIZE_THREAD__ flag */
43*4882a593Smuzhiyun #define __SANITIZE_THREAD__
44*4882a593Smuzhiyun #define __no_sanitize_thread \
45*4882a593Smuzhiyun 		__attribute__((no_sanitize("thread")))
46*4882a593Smuzhiyun #else
47*4882a593Smuzhiyun #define __no_sanitize_thread
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
51*4882a593Smuzhiyun #define __HAVE_BUILTIN_BSWAP32__
52*4882a593Smuzhiyun #define __HAVE_BUILTIN_BSWAP64__
53*4882a593Smuzhiyun #define __HAVE_BUILTIN_BSWAP16__
54*4882a593Smuzhiyun #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #if __has_feature(undefined_behavior_sanitizer)
57*4882a593Smuzhiyun /* GCC does not have __SANITIZE_UNDEFINED__ */
58*4882a593Smuzhiyun #define __no_sanitize_undefined \
59*4882a593Smuzhiyun 		__attribute__((no_sanitize("undefined")))
60*4882a593Smuzhiyun #else
61*4882a593Smuzhiyun #define __no_sanitize_undefined
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together
66*4882a593Smuzhiyun  * with no_sanitize("coverage"). Prior versions of Clang support coverage
67*4882a593Smuzhiyun  * instrumentation, but cannot be queried for support by the preprocessor.
68*4882a593Smuzhiyun  */
69*4882a593Smuzhiyun #if __has_feature(coverage_sanitizer)
70*4882a593Smuzhiyun #define __no_sanitize_coverage __attribute__((no_sanitize("coverage")))
71*4882a593Smuzhiyun #else
72*4882a593Smuzhiyun #define __no_sanitize_coverage
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun  * Not all versions of clang implement the type-generic versions
77*4882a593Smuzhiyun  * of the builtin overflow checkers. Fortunately, clang implements
78*4882a593Smuzhiyun  * __has_builtin allowing us to avoid awkward version
79*4882a593Smuzhiyun  * checks. Unfortunately, we don't know which version of gcc clang
80*4882a593Smuzhiyun  * pretends to be, so the macro may or may not be defined.
81*4882a593Smuzhiyun  */
82*4882a593Smuzhiyun #if __has_builtin(__builtin_mul_overflow) && \
83*4882a593Smuzhiyun     __has_builtin(__builtin_add_overflow) && \
84*4882a593Smuzhiyun     __has_builtin(__builtin_sub_overflow)
85*4882a593Smuzhiyun #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
86*4882a593Smuzhiyun #endif
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #if __has_feature(shadow_call_stack)
89*4882a593Smuzhiyun # define __noscs	__attribute__((__no_sanitize__("shadow-call-stack")))
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #define __nocfi		__attribute__((__no_sanitize__("cfi")))
93*4882a593Smuzhiyun #define __cficanonical	__attribute__((__cfi_canonical_jump_table__))
94