linux/include/linux/compiler_types.h

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LINUX_COMPILER_TYPES_H
#define __LINUX_COMPILER_TYPES_H

/*
 * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21.
 * In the meantime, to support gcc < 10, we implement __has_builtin
 * by hand.
 */
#ifndef __has_builtin
#define __has_builtin
#endif

#ifndef __ASSEMBLY__

/*
 * Skipped when running bindgen due to a libclang issue;
 * see https://github.com/rust-lang/rust-bindgen/issues/2244.
 */
#if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
	__has_attribute(btf_type_tag) && !defined(__BINDGEN__)
#define BTF_TYPE_TAG
#else
#define BTF_TYPE_TAG
#endif

/* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
#ifdef __CHECKER__
/* address spaces */
#define __kernel
#define __user
#define __iomem
#define __percpu
#define __rcu
static inline void __chk_user_ptr(const volatile void __user *ptr) { }
static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
/* context/locking */
#define __must_hold
#define __acquires
#define __cond_acquires
#define __releases
#define __acquire
#define __release
#define __cond_lock
/* other */
#define __force
#define __nocast
#define __safe
#define __private
#define ACCESS_PRIVATE
#else /* __CHECKER__ */
/* address spaces */
#define __kernel
# ifdef STRUCTLEAK_PLUGIN
#define __user
# else
#define __user
# endif
#define __iomem
#define __percpu
#define __rcu

#define __chk_user_ptr
#define __chk_io_ptr
/* context/locking */
#define __must_hold
#define __acquires
#define __cond_acquires
#define __releases
#define __acquire
#define __release
#define __cond_lock
/* other */
#define __force
#define __nocast
#define __safe
#define __private
#define ACCESS_PRIVATE
#define __builtin_warning
#endif /* __CHECKER__ */

/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
#define ___PASTE
#define __PASTE

#ifdef __KERNEL__

/* Attributes */
#include <linux/compiler_attributes.h>

#if CONFIG_FUNCTION_ALIGNMENT > 0
#define __function_aligned
#else
#define __function_aligned
#endif

/*
 *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
 *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
 *
 * When -falign-functions=N is in use, we must avoid the cold attribute as
 * GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
 * callees of cold functions as cold themselves, so it's not sufficient to add
 * __function_aligned here as that will not ensure that callees are correctly
 * aligned.
 *
 * See:
 *
 *   https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
 *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
 */
#if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
#define __cold
#else
#define __cold
#endif

/*
 * On x86-64 and arm64 targets, __preserve_most changes the calling convention
 * of a function to make the code in the caller as unintrusive as possible. This
 * convention behaves identically to the C calling convention on how arguments
 * and return values are passed, but uses a different set of caller- and callee-
 * saved registers.
 *
 * The purpose is to alleviates the burden of saving and recovering a large
 * register set before and after the call in the caller.  This is beneficial for
 * rarely taken slow paths, such as error-reporting functions that may be called
 * from hot paths.
 *
 * Note: This may conflict with instrumentation inserted on function entry which
 * does not use __preserve_most or equivalent convention (if in assembly). Since
 * function tracing assumes the normal C calling convention, where the attribute
 * is supported, __preserve_most implies notrace.  It is recommended to restrict
 * use of the attribute to functions that should or already disable tracing.
 *
 * Optional: not supported by gcc.
 *
 * clang: https://clang.llvm.org/docs/AttributeReference.html#preserve-most
 */
#if __has_attribute(__preserve_most__) && (defined(CONFIG_X86_64) || defined(CONFIG_ARM64))
#define __preserve_most
#else
#define __preserve_most
#endif

/*
 * Annotating a function/variable with __retain tells the compiler to place
 * the object in its own section and set the flag SHF_GNU_RETAIN. This flag
 * instructs the linker to retain the object during garbage-cleanup or LTO
 * phases.
 *
 * Note that the __used macro is also used to prevent functions or data
 * being optimized out, but operates at the compiler/IR-level and may still
 * allow unintended removal of objects during linking.
 *
 * Optional: only supported since gcc >= 11, clang >= 13
 *
 *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute
 * clang: https://clang.llvm.org/docs/AttributeReference.html#retain
 */
#if __has_attribute(__retain__) && \
	(defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || \
	 defined(CONFIG_LTO_CLANG))
#define __retain
#else
#define __retain
#endif

/* Compiler specific macros. */
#ifdef __clang__
#include <linux/compiler-clang.h>
#elif defined(__GNUC__)
/* The above compilers also define __GNUC__, so order is important here. */
#include <linux/compiler-gcc.h>
#else
#error "Unknown compiler"
#endif

/*
 * Some architectures need to provide custom definitions of macros provided
 * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
 * conditionally rather than using an asm-generic wrapper in order to avoid
 * build failures if any C compilation, which will include this file via an
 * -include argument in c_flags, occurs prior to the asm-generic wrappers being
 * generated.
 */
#ifdef CONFIG_HAVE_ARCH_COMPILER_H
#include <asm/compiler.h>
#endif

struct ftrace_branch_data {
	const char *func;
	const char *file;
	unsigned line;
	union {
		struct {
			unsigned long correct;
			unsigned long incorrect;
		};
		struct {
			unsigned long miss;
			unsigned long hit;
		};
		unsigned long miss_hit[2];
	};
};

struct ftrace_likely_data {
	struct ftrace_branch_data	data;
	unsigned long			constant;
};

#if defined(CC_USING_HOTPATCH)
#define notrace
#elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
#define notrace
#else
#define notrace
#endif

/*
 * it doesn't make sense on ARM (currently the only user of __naked)
 * to trace naked functions because then mcount is called without
 * stack and frame pointer being set up and there is no chance to
 * restore the lr register to the value before mcount was called.
 */
#define __naked

/*
 * Prefer gnu_inline, so that extern inline functions do not emit an
 * externally visible function. This makes extern inline behave as per gnu89
 * semantics rather than c99. This prevents multiple symbol definition errors
 * of extern inline functions at link time.
 * A lot of inline functions can cause havoc with function tracing.
 */
#define inline inline __gnu_inline __inline_maybe_unused notrace

/*
 * gcc provides both __inline__ and __inline as alternate spellings of
 * the inline keyword, though the latter is undocumented. New kernel
 * code should only use the inline spelling, but some existing code
 * uses __inline__. Since we #define inline above, to ensure
 * __inline__ has the same semantics, we need this #define.
 *
 * However, the spelling __inline is strictly reserved for referring
 * to the bare keyword.
 */
#define __inline__ inline

/*
 * GCC does not warn about unused static inline functions for -Wunused-function.
 * Suppress the warning in clang as well by using __maybe_unused, but enable it
 * for W=1 build. This will allow clang to find unused functions. Remove the
 * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
 */
#ifdef KBUILD_EXTRA_WARN1
#define __inline_maybe_unused
#else
#define __inline_maybe_unused
#endif

/*
 * Rather then using noinline to prevent stack consumption, use
 * noinline_for_stack instead.  For documentation reasons.
 */
#define noinline_for_stack

/*
 * Sanitizer helper attributes: Because using __always_inline and
 * __no_sanitize_* conflict, provide helper attributes that will either expand
 * to __no_sanitize_* in compilation units where instrumentation is enabled
 * (__SANITIZE_*__), or __always_inline in compilation units without
 * instrumentation (__SANITIZE_*__ undefined).
 */
#ifdef __SANITIZE_ADDRESS__
/*
 * We can't declare function 'inline' because __no_sanitize_address conflicts
 * with inlining. Attempt to inline it may cause a build failure.
 *     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
 * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
 */
#define __no_kasan_or_inline
#define __no_sanitize_or_inline
#else
#define __no_kasan_or_inline
#endif

#ifdef __SANITIZE_THREAD__
/*
 * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
 * atomics even with __no_sanitize_thread (to avoid false positives in userspace
 * ThreadSanitizer). The kernel's requirements are stricter and we really do not
 * want any instrumentation with __no_kcsan.
 *
 * Therefore we add __disable_sanitizer_instrumentation where available to
 * disable all instrumentation. See Kconfig.kcsan where this is mandatory.
 */
#define __no_kcsan
/*
 * Type qualifier to mark variables where all data-racy accesses should be
 * ignored by KCSAN. Note, the implementation simply marks these variables as
 * volatile, since KCSAN will treat such accesses as "marked".
 */
#define __data_racy
#define __no_sanitize_or_inline
#else
#define __no_kcsan
#define __data_racy
#endif

#ifdef __SANITIZE_MEMORY__
/*
 * Similarly to KASAN and KCSAN, KMSAN loses function attributes of inlined
 * functions, therefore disabling KMSAN checks also requires disabling inlining.
 *
 * __no_sanitize_or_inline effectively prevents KMSAN from reporting errors
 * within the function and marks all its outputs as initialized.
 */
#define __no_sanitize_or_inline
#endif

#ifndef __no_sanitize_or_inline
#define __no_sanitize_or_inline
#endif

/*
 * Apply __counted_by() when the Endianness matches to increase test coverage.
 */
#ifdef __LITTLE_ENDIAN
#define __counted_by_le
#define __counted_by_be
#else
#define __counted_by_le
#define __counted_by_be
#endif

/* Do not trap wrapping arithmetic within an annotated function. */
#ifdef CONFIG_UBSAN_SIGNED_WRAP
#define __signed_wrap
#else
#define __signed_wrap
#endif

/* Section for code which can't be instrumented at all */
#define __noinstr_section

#define noinstr

/*
 * The __cpuidle section is used twofold:
 *
 *  1) the original use -- identifying if a CPU is 'stuck' in idle state based
 *     on it's instruction pointer. See cpu_in_idle().
 *
 *  2) supressing instrumentation around where cpuidle disables RCU; where the
 *     function isn't strictly required for #1, this is interchangeable with
 *     noinstr.
 */
#define __cpuidle

#endif /* __KERNEL__ */

#endif /* __ASSEMBLY__ */

/*
 * The below symbols may be defined for one or more, but not ALL, of the above
 * compilers. We don't consider that to be an error, so set them to nothing.
 * For example, some of them are for compiler specific plugins.
 */
#ifndef __latent_entropy
#define __latent_entropy
#endif

#if defined(RANDSTRUCT) && !defined(__CHECKER__)
#define __randomize_layout
#define __no_randomize_layout
/* This anon struct can add padding, so only enable it under randstruct. */
#define randomized_struct_fields_start
#define randomized_struct_fields_end
#else
#define __randomize_layout
#define __no_randomize_layout
#define randomized_struct_fields_start
#define randomized_struct_fields_end
#endif

#ifndef __noscs
#define __noscs
#endif

#ifndef __nocfi
#define __nocfi
#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
#define __realloc_size
#else
#define __alloc_size(x, ...)
#define __realloc_size(x, ...)
#endif

/*
 * When the size of an allocated object is needed, use the best available
 * mechanism to find it. (For cases where sizeof() cannot be used.)
 */
#if __has_builtin(__builtin_dynamic_object_size)
#define __struct_size(p)
#define __member_size(p)
#else
#define __struct_size
#define __member_size
#endif

/* Determine if an attribute has been applied to a variable. */
#if __has_builtin(__builtin_has_attribute)
#define __annotated
#else
#define __annotated(var, attr)
#endif

/*
 * Some versions of gcc do not mark 'asm goto' volatile:
 *
 *  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
 *
 * We do it here by hand, because it doesn't hurt.
 */
#ifndef asm_goto_output
#define asm_goto_output(x...)
#endif

/*
 * Clang has trouble with constraints with multiple
 * alternative behaviors (mainly "g" and "rm").
 */
#ifndef ASM_INPUT_G
  #define ASM_INPUT_G
  #define ASM_INPUT_RM
#endif

#ifdef CONFIG_CC_HAS_ASM_INLINE
#define asm_inline
#else
#define asm_inline
#endif

/* Are two types/vars the same type (ignoring qualifiers)? */
#define __same_type(a, b)

/*
 * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
 *			       non-scalar types unchanged.
 */
/*
 * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
 * is not type-compatible with 'signed char', and we define a separate case.
 */
#define __scalar_type_to_expr_cases(type)

#define __unqual_scalar_typeof(x)

/* Is this type a native word size -- useful for atomic operations */
#define __native_word(t)

#ifdef __OPTIMIZE__
#define __compiletime_assert
#else
#define __compiletime_assert(condition, msg, prefix, suffix)
#endif

#define _compiletime_assert(condition, msg, prefix, suffix)

/**
 * compiletime_assert - break build and emit msg if condition is false
 * @condition: a compile-time constant condition to check
 * @msg:       a message to emit if condition is false
 *
 * In tradition of POSIX assert, this macro will break the build if the
 * supplied condition is *false*, emitting the supplied error message if the
 * compiler has support to do so.
 */
#define compiletime_assert(condition, msg)

#define compiletime_assert_atomic_type(t)

/* Helpers for emitting diagnostics in pragmas. */
#ifndef __diag
#define __diag(string)
#endif

#ifndef __diag_GCC
#define __diag_GCC(version, severity, string)
#endif

#define __diag_push()
#define __diag_pop()

#define __diag_ignore(compiler, version, option, comment)
#define __diag_warn(compiler, version, option, comment)
#define __diag_error(compiler, version, option, comment)

#ifndef __diag_ignore_all
#define __diag_ignore_all(option, comment)
#endif

#endif /* __LINUX_COMPILER_TYPES_H */