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