diff options
Diffstat (limited to 'include/linux/compiler_types.h')
-rw-r--r-- | include/linux/compiler_types.h | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index db192becfe..d925b3da29 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -1,3 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + #ifndef __LINUX_COMPILER_TYPES_H #define __LINUX_COMPILER_TYPES_H @@ -54,6 +56,16 @@ extern void __chk_io_ptr(const volatile void __iomem *); #ifdef __KERNEL__ +/* + * Note: do not use this directly. Instead, use __alloc_size() since it is conditionally + * available and includes other attributes. For GCC < 9.1, __alloc_size__ gets undefined + * in compiler-gcc.h, due to misbehaviors. + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute + * clang: https://clang.llvm.org/docs/AttributeReference.html#alloc-size + */ +#define __alloc_size__(x, ...) __attribute__((__alloc_size__(x, ## __VA_ARGS__))) + /* Compiler specific macros. */ #ifdef __clang__ #include <linux/compiler-clang.h> @@ -110,6 +122,41 @@ struct ftrace_likely_data { #define __deprecated #define __deprecated_for_modules +#ifndef __has_attribute +#define __has_attribute(...) 0 +#endif + +/* + * Add the pseudo keyword 'fallthrough' so case statement blocks + * must end with any of these keywords: + * break; + * fallthrough; + * continue; + * goto <label>; + * return [expression]; + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes + */ +#if __has_attribute(__fallthrough__) +# define fallthrough __attribute__((__fallthrough__)) +#else +# define fallthrough do {} while (0) /* fallthrough */ +#endif + +/* + * Optional: only supported since GCC >= 11.1, clang >= 7.0. + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fstack_005fprotector-function-attribute + * clang: https://clang.llvm.org/docs/AttributeReference.html#no-stack-protector-safebuffers + */ +#if __has_attribute(__no_stack_protector__) +# define __no_stack_protector __attribute__((__no_stack_protector__)) +#elif ! defined CONFIG_STACKPROTECTOR +# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector"))) +#else +# define __no_stack_protector +#endif + #endif /* __KERNEL__ */ #endif /* __ASSEMBLY__ */ @@ -151,6 +198,20 @@ struct ftrace_likely_data { #define __assume_aligned(a, ...) #endif +/* + * Any place that could be marked with the "alloc_size" attribute is also + * a place to be marked with the "malloc" attribute, except those that may + * be performing a _reallocation_, as that may alias the existing pointer. + * For these, use __realloc_size(). + */ +#ifdef __alloc_size__ +# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc +# define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) +#else +# define __alloc_size(x, ...) __malloc +# define __realloc_size(x, ...) +#endif + /* Are two types/vars the same type (ignoring qualifiers)? */ #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) @@ -199,8 +260,8 @@ struct ftrace_likely_data { #define __pure __attribute__((pure)) #define __aligned(x) __attribute__((aligned(x))) #define __aligned_largest __attribute__((aligned)) -#define __printf(a, b) __attribute__((format(printf, a, b))) -#define __scanf(a, b) __attribute__((format(scanf, a, b))) +#define __printf(a, b) __attribute__((format(__printf__, a, b))) +#define __scanf(a, b) __attribute__((format(__scanf__, a, b))) #define __maybe_unused __attribute__((unused)) #define __always_unused __attribute__((unused)) #define __mode(x) __attribute__((mode(x))) @@ -213,6 +274,12 @@ struct ftrace_likely_data { #define __cold __attribute__((cold)) #define __section(S) __attribute__((__section__(#S))) +#ifdef __clang__ +#define __ll_elem(S) __section(S) __used __no_sanitize_address +#else +#define __ll_elem(S) __section(S) __used +#endif + #ifdef CONFIG_ENABLE_MUST_CHECK #define __must_check __attribute__((warn_unused_result)) @@ -282,4 +349,11 @@ struct ftrace_likely_data { */ #define noinline_for_stack noinline +/* code that can't be instrumented at all */ +#define noinstr \ + noinline notrace __no_sanitize_address __no_stack_protector + +#define __prereloc \ + notrace __no_sanitize_address __no_stack_protector + #endif /* __LINUX_COMPILER_TYPES_H */ |