/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LINUX_COMPILER_TYPES_H
#error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
#endif
/*
* Common definitions for all gcc versions go here.
*/
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
/*
* This macro obfuscates arithmetic on a variable address so that gcc
* shouldn't recognize the original var, and make assumptions about it.
*
* This is needed because the C standard makes it undefined to do
* pointer arithmetic on "objects" outside their boundaries and the
* gcc optimizers assume this is the case. In particular they
* assume such arithmetic does not wrap.
*
* A miscompilation has been observed because of this on PPC.
* To work around it we hide the relationship of the pointer and the object
* using this macro.
*
* Versions of the ppc64 compiler before 4.1 had a bug where use of
* RELOC_HIDE could trash r30. The bug can be worked around by changing
* the inline assembly constraint from =g to =r, in this particular
* case either is valid.
*/
#define RELOC_HIDE(ptr, off) \
({ \
unsigned long __ptr; \
__asm__ (RAP_SAFE_ASM "" : "=r"(__ptr) : "0"(ptr)); \
(typeof(ptr)) (__ptr + (off)); \
})
#if defined(CONFIG_RETPOLINE) && defined(RETPOLINE_PLUGIN)
#define __noretpoline __attribute__((__indirect_branch__("keep")))
#endif
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, __PASTE(__KBUILD_BASENAME, __PASTE(_,prefix))), __PASTE(_,__COUNTER__))
#define __bos(ptr, arg) __builtin_object_size((ptr), (arg))
#define __bos0(ptr) __bos((ptr), 0)
#define __bos1(ptr) __bos((ptr), 1)
#ifdef RANDSTRUCT_PLUGIN
#define __randomize_layout __attribute__((randomize_layout))
#define __no_randomize_layout __attribute__((no_randomize_layout))
#endif
/*
* The type_canary plugin can verify the integrity of objects at runtime, given
* its type is a structure containing a __canary(...) annotated member.
*
* The first argument of the __canary() attribute annotation specifies the mode
* of operation and must be either "static" or "dynamic". The former uses a
* fixed per-type compile time random value to compare the canary member
* against. The latter extents this scheme by taking an object's address into
* account as well (at a slightly higher runtime cost).
*
* Optional flags can be appended after the mode argument, separated by comma.
* So far only the "noinstr" option is supported. It disables the otherwise
* automatic runtime instrumentation and verification. Use of this flage
* therefore requires manual assignment and check of the canary member using
* the __builtin_canary_value() helper.
*
* The __canary_copy() helper should be used to inherit an object's canary
* value for direct object copies, e.g. via memcpy(). It's needed because the
* target's canary value may implicitly be invalidated by the memcpy() if the
* 'dynamic' canary mode is used for the involved type.
*/
#ifdef TYPE_CANARY_PLUGIN
#define __canary(...) __attribute__((canary(__VA_ARGS__)))
#define __canary_value(obj, ...) __builtin_canary_value(obj, ## __VA_ARGS__)
#define __canary_copy(dst, src) __builtin_canary_copy((dst), (src))
#endif
#ifdef CONSTIFY_PLUGIN
#define __no_const __attribute__((no_const))
#define __do_const __attribute__((do_const))
#define __mutable_const __attribute__((mutable_const))
#define __mutable __attribute__((mutable_const))
#define __is_mutable(exp, ...) __builtin_mutable_p(exp, ## __VA_ARGS__)
#endif
#ifdef SIZE_OVERFLOW_PLUGIN
#define __size_overflow(...) __attribute__((size_overflow(__VA_ARGS__)))
#define __intentional_overflow(...) __attribute__((intentional_overflow(__VA_ARGS__)))
/* disable all SO dataflow analysis */
#define __turn_off_size_overflow __attribute__((intentional_overflow(-1)))
/* skip SO dataflow analysis for this function, but preserve backward dataflow analysis */
#define __skip_size_overflow __attribute__((intentional_overflow(0)))
#endif
#if defined(CONFIG_PAX_RESPECTRE_PLUGIN) && !defined(CONFIG_PAX_RESPECTRE_PLUGIN_EINLINE)
#define __respectre_inline static inline __late_inline
#endif
#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
#define __latent_entropy __attribute__((latent_entropy))
#endif
/*
* calling noreturn functions, __builtin_unreachable() and __builtin_trap()
* confuse the stack allocation in gcc, leading to overly large stack
* frames, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
*
* Adding an empty inline assembly before it works around the problem
*/
#ifndef barrier_before_unreachable
# define barrier_before_unreachable() asm volatile(RAP_SAFE_ASM "")
#endif
/*
* The initify gcc-plugin attempts to identify const arguments that are only
* used during init (see __init and __exit), so they can be moved to the
* .init.rodata/.exit.rodata section. If an argument is passed to a non-init
* function, it must normally be assumed that such an argument has been
* captured by that function and may be used in the future when .init/.exit has
* been unmapped from memory. In order to identify functions that are confirmed
* to not capture their arguments, the __nocapture() attribute is used so that
* initify can better identify candidate variables.
*/
#ifdef INITIFY_PLUGIN
#define __nocapture(...) __attribute__((nocapture(__VA_ARGS__)))
#define __unverified_nocapture(...) __attribute__((unverified_nocapture(__VA_ARGS__)))
#endif
#ifdef RAP_PLUGIN_HASH
#define __rap_hash __attribute__((rap_hash))
#define __rap_hash_bpf __attribute__((rap_hash_extra("bpf")))
#define __rap_hash_icall __attribute__((rap_hash_icall))
#define __rap_hash_override(f) __attribute__((rap_hash_override(f)))
#endif
#ifdef AUTOSLAB_PLUGIN
#define __alloc_gfp_flags(arg) __attribute((alloc_flags(arg, "gfp")))
#define __alloc_node(arg) __attribute((alloc_node(arg)))
#define __alloc __attribute__((alloc))
#endif
#ifdef RAP_PLUGIN_HASH
#define __convertible __attribute__((convertible))
#define __indirectly_callable __attribute__((indirectly_callable))
#endif
/*
* Mark a position in code as unreachable. This can be used to
* suppress control flow warnings after asm blocks that transfer
* control elsewhere.
*/
#define unreachable() \
do { \
annotate_unreachable(); \
barrier_before_unreachable(); \
__builtin_unreachable(); \
} while (0)
#define naked_unreachable() unreachable()
#ifdef UTILITIES_PLUGIN
#define __noderef __attribute__((noderef))
#define __read_once __attribute__((read_once))
#define __write_once __attribute__((write_once))
#define __section_type(t) __attribute__((section_type(#t)))
#define __late_inline __attribute__((late_inline))
#define __leaf_only __attribute__((leaf_only)) __flatten
#define __maybe_used __used __attribute__((maybe_used))
#define __nolocal __attribute__((nolocal))
#define __nolocal_arg(...) __attribute__((nolocal(__VA_ARGS__)))
#define __traits(...) __attribute__((traits(__VA_ARGS__)))
#define __diagnose_error(...) __attribute__((diagnose("error", __VA_ARGS__)))
#endif
/*
* GCC 'asm goto' with outputs miscompiles certain code sequences:
*
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113921
*
* Work around it via the same compiler barrier quirk that we used
* to use for the old 'asm goto' workaround.
*
* Also, always mark such 'asm goto' statements as volatile: all
* asm goto statements are supposed to be volatile as per the
* documentation, but some versions of gcc didn't actually do
* that for asms with outputs:
*
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98619
*/
#ifdef CONFIG_GCC_ASM_GOTO_OUTPUT_WORKAROUND
#define asm_goto_output(x...) \
do { asm volatile goto(x); asm (""); } while (0)
#endif
#if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP) && !defined(CONFIG_PAX_SIZE_OVERFLOW)
#define __HAVE_BUILTIN_BSWAP32__
#define __HAVE_BUILTIN_BSWAP64__
#define __HAVE_BUILTIN_BSWAP16__
#endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
#if GCC_VERSION >= 70000
#define KASAN_ABI_VERSION 5
#else
#define KASAN_ABI_VERSION 4
#endif
#ifdef CONFIG_SHADOW_CALL_STACK
#define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
#endif
#ifdef __SANITIZE_HWADDRESS__
#define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
#else
#define __no_sanitize_address __attribute__((__no_sanitize_address__))
#endif
#if defined(__SANITIZE_THREAD__)
#define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
#else
#define __no_sanitize_thread
#endif
#define __no_sanitize_undefined __attribute__((__no_sanitize_undefined__))
/*
* Only supported since gcc >= 12
*/
#if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__)
#define __no_sanitize_coverage __attribute__((__no_sanitize_coverage__))
#else
#define __no_sanitize_coverage
#endif
/*
* Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel,
* matching the defines used by Clang.
*/
#ifdef __SANITIZE_HWADDRESS__
#define __SANITIZE_ADDRESS__
#endif
/*
* GCC does not support KMSAN.
*/
#define __no_sanitize_memory
#define __no_kmsan_checks
/*
* Turn individual warnings and errors on and off locally, depending
* on version.
*/
#define __diag_GCC(version, severity, s) \
__diag_GCC_ ## version(__diag_GCC_ ## severity s)
/* Severity used in pragma directives */
#define __diag_GCC_ignore ignored
#define __diag_GCC_warn warning
#define __diag_GCC_error error
#define __diag_str1(s) #s
#define __diag_str(s) __diag_str1(s)
#define __diag(s) _Pragma(__diag_str(GCC diagnostic s))
#if GCC_VERSION >= 80000
#define __diag_GCC_8(s) __diag(s)
#else
#define __diag_GCC_8(s)
#endif
#define __diag_ignore_all(option, comment) \
__diag_GCC(8, ignore, option)