llvm/compiler-rt/lib/sanitizer_common/sanitizer_asm.h

//===-- sanitizer_asm.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Various support for assembler.
//
//===----------------------------------------------------------------------===//

// Some toolchains do not support .cfi asm directives, so we have to hide
// them inside macros.
#if defined(__clang__) ||                                                      \
    (defined(__GNUC__) && defined(__GCC_HAVE_DWARF2_CFI_ASM))
  // GCC defined __GCC_HAVE_DWARF2_CFI_ASM if it supports CFI.
  // Clang seems to support CFI by default (or not?).
  // We need two versions of macros: for inline asm and standalone asm files.
#define CFI_INL_ADJUST_CFA_OFFSET(n)

#define CFI_STARTPROC
#define CFI_ENDPROC
#define CFI_ADJUST_CFA_OFFSET(n)
#define CFI_DEF_CFA_OFFSET(n)
#define CFI_REL_OFFSET(reg, n)
#define CFI_OFFSET(reg, n)
#define CFI_DEF_CFA_REGISTER(reg)
#define CFI_DEF_CFA(reg, n)
#define CFI_RESTORE(reg)

#else  // No CFI
#define CFI_INL_ADJUST_CFA_OFFSET
#define CFI_STARTPROC
#define CFI_ENDPROC
#define CFI_ADJUST_CFA_OFFSET
#define CFI_DEF_CFA_OFFSET
#define CFI_REL_OFFSET
#define CFI_OFFSET
#define CFI_DEF_CFA_REGISTER
#define CFI_DEF_CFA
#define CFI_RESTORE
#endif

#if defined(__aarch64__) && defined(__ARM_FEATURE_BTI_DEFAULT)
#define ASM_STARTPROC
#define C_ASM_STARTPROC
#else
#define ASM_STARTPROC
#define C_ASM_STARTPROC
#endif
#define ASM_ENDPROC
#define C_ASM_ENDPROC

#if defined(__x86_64__) || defined(__i386__) || defined(__sparc__)
#define ASM_TAIL_CALL
#elif defined(__arm__) || defined(__aarch64__) || defined(__mips__) || \
    defined(__powerpc__) || defined(__loongarch_lp64)
#define ASM_TAIL_CALL
#elif defined(__s390__)
#define ASM_TAIL_CALL
#elif defined(__riscv)
#define ASM_TAIL_CALL
#endif

// Currently, almost all of the shared libraries rely on the value of
// $t9 to get the address of current function, instead of PCREL, even
// on MIPSr6. To be compatiable with them, we have to set $t9 properly.
// MIPS uses GOT to get the address of preemptible functions.
#if defined(__mips64)
#define C_ASM_TAIL_CALL
#elif defined(__mips__)
#define C_ASM_TAIL_CALL
#elif defined(ASM_TAIL_CALL)
#define C_ASM_TAIL_CALL(t_func, i_func)
#endif

#if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \
    defined(__riscv)
#define ASM_PREEMPTIBLE_SYM(sym)
#else
#define ASM_PREEMPTIBLE_SYM
#endif

#if !defined(__APPLE__)
#define ASM_HIDDEN(symbol)
# if defined(__arm__) || defined(__aarch64__)
#define ASM_TYPE_FUNCTION
# else
#define ASM_TYPE_FUNCTION(symbol)
# endif
#define ASM_SIZE(symbol)
#define ASM_SYMBOL(symbol)
#define ASM_SYMBOL_INTERCEPTOR(symbol)
# if defined(__i386__) || defined(__powerpc__) || defined(__s390__) || \
     defined(__sparc__)
// For details, see interception.h
#define ASM_WRAPPER_NAME
#define ASM_TRAMPOLINE_ALIAS
#define ASM_INTERCEPTOR_TRAMPOLINE
#define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
# else  // Architecture supports interceptor trampoline
// Keep trampoline implementation in sync with interception/interception.h
#define ASM_WRAPPER_NAME(symbol)
#define ASM_TRAMPOLINE_ALIAS(symbol, name)
#define ASM_INTERCEPTOR_TRAMPOLINE(name)
#define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
# endif  // Architecture supports interceptor trampoline
#else
#define ASM_HIDDEN
#define ASM_TYPE_FUNCTION
#define ASM_SIZE
#define ASM_SYMBOL
#define ASM_SYMBOL_INTERCEPTOR
#define ASM_WRAPPER_NAME
#endif

#if defined(__ELF__) && (defined(__GNU__) || defined(__FreeBSD__) || \
                         defined(__Fuchsia__) || defined(__linux__))
// clang-format off
#define NO_EXEC_STACK_DIRECTIVE
// clang-format on
#else
#define NO_EXEC_STACK_DIRECTIVE
#endif

#if (defined(__x86_64__) || defined(__i386__)) && defined(__has_include) && __has_include(<cet.h>)
#include <cet.h>
#endif
#ifndef _CET_ENDBR
#define _CET_ENDBR
#endif