XenevaOS
Loading...
Searching...
No Matches
compiler-clang.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_COMPILER_TYPES_H
3#error "Please do not include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
4#endif
5
6/* Compiler specific definitions for Clang compiler */
7
8/* all clang versions usable with the kernel support KASAN ABI version 5 */
9#define KASAN_ABI_VERSION 5
10
11/*
12 * Clang 22 added preprocessor macros to match GCC, in hopes of eventually
13 * dropping __has_feature support for sanitizers:
14 * https://github.com/llvm/llvm-project/commit/568c23bbd3303518c5056d7f03444dae4fdc8a9c
15 * Create these macros for older versions of clang so that it is easy to clean
16 * up once the minimum supported version of LLVM for building the kernel always
17 * creates these macros.
18 *
19 * Note: Checking __has_feature(*_sanitizer) is only true if the feature is
20 * enabled. Therefore it is not required to additionally check defined(CONFIG_*)
21 * to avoid adding redundant attributes in other configurations.
22 */
23#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
24#define __SANITIZE_ADDRESS__
25#endif
26#if __has_feature(hwaddress_sanitizer) && !defined(__SANITIZE_HWADDRESS__)
27#define __SANITIZE_HWADDRESS__
28#endif
29#if __has_feature(thread_sanitizer) && !defined(__SANITIZE_THREAD__)
30#define __SANITIZE_THREAD__
31#endif
32
33/*
34 * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel.
35 */
36#ifdef __SANITIZE_HWADDRESS__
37#define __SANITIZE_ADDRESS__
38#endif
39
40#ifdef __SANITIZE_ADDRESS__
41#define __no_sanitize_address \
42 __attribute__((no_sanitize("address", "hwaddress")))
43#else
44#define __no_sanitize_address
45#endif
46
47#ifdef __SANITIZE_THREAD__
48#define __no_sanitize_thread \
49 __attribute__((no_sanitize("thread")))
50#else
51#define __no_sanitize_thread
52#endif
53
54#if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
55#define __HAVE_BUILTIN_BSWAP32__
56#define __HAVE_BUILTIN_BSWAP64__
57#define __HAVE_BUILTIN_BSWAP16__
58#endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
59
60#if __has_feature(undefined_behavior_sanitizer)
61/* GCC does not have __SANITIZE_UNDEFINED__ */
62#define __no_sanitize_undefined \
63 __attribute__((no_sanitize("undefined")))
64#else
65#define __no_sanitize_undefined
66#endif
67
68#if __has_feature(memory_sanitizer)
69#define __SANITIZE_MEMORY__
70/*
71 * Unlike other sanitizers, KMSAN still inserts code into functions marked with
72 * no_sanitize("kernel-memory"). Using disable_sanitizer_instrumentation
73 * provides the behavior consistent with other __no_sanitize_ attributes,
74 * guaranteeing that __no_sanitize_memory functions remain uninstrumented.
75 */
76#define __no_sanitize_memory __disable_sanitizer_instrumentation
77
78/*
79 * The __no_kmsan_checks attribute ensures that a function does not produce
80 * false positive reports by:
81 * - initializing all local variables and memory stores in this function;
82 * - skipping all shadow checks;
83 * - passing initialized arguments to this function's callees.
84 */
85#define __no_kmsan_checks __attribute__((no_sanitize("kernel-memory")))
86#else
87#define __no_sanitize_memory
88#define __no_kmsan_checks
89#endif
90
91/*
92 * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together
93 * with no_sanitize("coverage"). Prior versions of Clang support coverage
94 * instrumentation, but cannot be queried for support by the preprocessor.
95 */
96#if __has_feature(coverage_sanitizer)
97#define __no_sanitize_coverage __attribute__((no_sanitize("coverage")))
98#else
99#define __no_sanitize_coverage
100#endif
101
102/* Only Clang needs to disable the coverage sanitizer for kstack_erase. */
103#define __no_kstack_erase __no_sanitize_coverage
104
105#if __has_feature(shadow_call_stack)
106# define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
107#endif
108
109/*
110 * Turn individual warnings and errors on and off locally, depending
111 * on version.
112 */
113#define __diag_clang(version, severity, s) \
114 __diag_clang_ ## version(__diag_clang_ ## severity s)
115
116/* Severity used in pragma directives */
117#define __diag_clang_ignore ignored
118#define __diag_clang_warn warning
119#define __diag_clang_error error
120
121#define __diag_str1(s) #s
122#define __diag_str(s) __diag_str1(s)
123#define __diag(s) _Pragma(__diag_str(clang diagnostic s))
124
125#if CONFIG_CLANG_VERSION >= 230000
126#define __diag_clang_23(s) __diag(s)
127#else
128#define __diag_clang_23(s)
129#endif
130
131#define __diag_clang_all(s) __diag(s)
132
133#define __diag_ignore_all(option, comment) \
134 __diag_clang(all, ignore, option)
135
136/*
137 * clang has horrible behavior with "g" or "rm" constraints for asm
138 * inputs, turning them into something worse than "m". Avoid using
139 * constraints with multiple possible uses (but "ir" seems to be ok):
140 *
141 * https://github.com/llvm/llvm-project/issues/20571
142 */
143#define ASM_INPUT_G "ir"
144#define ASM_INPUT_RM "r"
145#define ASM_OUTPUT_RM "=r"
146
147/*
148 * Declare compiler support for __typeof_unqual__() operator.
149 *
150 * Bindgen uses LLVM even if our C compiler is GCC, so we cannot
151 * rely on the auto-detected CONFIG_CC_HAS_TYPEOF_UNQUAL.
152 */
153#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ > 19 || (__clang_major__ == 19 && __clang_minor__ > 0))