1 //===-- sanitizer_asm.h -----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Various support for assembler. 10 // 11 //===----------------------------------------------------------------------===// 12 13 // Some toolchains do not support .cfi asm directives, so we have to hide 14 // them inside macros. 15 #if defined(__clang__) || \ 16 (defined(__GNUC__) && defined(__GCC_HAVE_DWARF2_CFI_ASM)) 17 // GCC defined __GCC_HAVE_DWARF2_CFI_ASM if it supports CFI. 18 // Clang seems to support CFI by default (or not?). 19 // We need two versions of macros: for inline asm and standalone asm files. 20 # define CFI_INL_ADJUST_CFA_OFFSET(n) ".cfi_adjust_cfa_offset " #n ";" 21 22 # define CFI_STARTPROC .cfi_startproc 23 # define CFI_ENDPROC .cfi_endproc 24 # define CFI_ADJUST_CFA_OFFSET(n) .cfi_adjust_cfa_offset n 25 # define CFI_DEF_CFA_OFFSET(n) .cfi_def_cfa_offset n 26 # define CFI_REL_OFFSET(reg, n) .cfi_rel_offset reg, n 27 # define CFI_OFFSET(reg, n) .cfi_offset reg, n 28 # define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg 29 # define CFI_DEF_CFA(reg, n) .cfi_def_cfa reg, n 30 # define CFI_RESTORE(reg) .cfi_restore reg 31 32 #else // No CFI 33 # define CFI_INL_ADJUST_CFA_OFFSET(n) 34 # define CFI_STARTPROC 35 # define CFI_ENDPROC 36 # define CFI_ADJUST_CFA_OFFSET(n) 37 # define CFI_DEF_CFA_OFFSET(n) 38 # define CFI_REL_OFFSET(reg, n) 39 # define CFI_OFFSET(reg, n) 40 # define CFI_DEF_CFA_REGISTER(reg) 41 # define CFI_DEF_CFA(reg, n) 42 # define CFI_RESTORE(reg) 43 #endif 44 45 #if defined(__x86_64__) || defined(__i386__) || defined(__sparc__) 46 # define ASM_TAIL_CALL jmp 47 #elif defined(__arm__) || defined(__aarch64__) || defined(__mips__) || \ 48 defined(__powerpc__) || defined(__loongarch_lp64) 49 # define ASM_TAIL_CALL b 50 #elif defined(__s390__) 51 # define ASM_TAIL_CALL jg 52 #elif defined(__riscv) 53 # define ASM_TAIL_CALL tail 54 #endif 55 56 #if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \ 57 defined(__riscv) 58 # define ASM_PREEMPTIBLE_SYM(sym) sym@plt 59 #else 60 # define ASM_PREEMPTIBLE_SYM(sym) sym 61 #endif 62 63 #if !defined(__APPLE__) 64 # define ASM_HIDDEN(symbol) .hidden symbol 65 # define ASM_TYPE_FUNCTION(symbol) .type symbol, %function 66 # define ASM_SIZE(symbol) .size symbol, .-symbol 67 # define ASM_SYMBOL(symbol) symbol 68 # define ASM_SYMBOL_INTERCEPTOR(symbol) symbol 69 # if defined(__i386__) || defined(__powerpc__) || defined(__s390__) || \ 70 defined(__sparc__) 71 // For details, see interception.h 72 # define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol 73 # define ASM_TRAMPOLINE_ALIAS(symbol, name) \ 74 .weak symbol; \ 75 .set symbol, ASM_WRAPPER_NAME(name) 76 # define ASM_INTERCEPTOR_TRAMPOLINE(name) 77 # define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 0 78 # else // Architecture supports interceptor trampoline 79 // Keep trampoline implementation in sync with interception/interception.h 80 # define ASM_WRAPPER_NAME(symbol) ___interceptor_##symbol 81 # define ASM_TRAMPOLINE_ALIAS(symbol, name) \ 82 .weak symbol; \ 83 .set symbol, __interceptor_trampoline_##name 84 # define ASM_INTERCEPTOR_TRAMPOLINE(name) \ 85 .weak __interceptor_##name; \ 86 .set __interceptor_##name, ASM_WRAPPER_NAME(name); \ 87 .globl __interceptor_trampoline_##name; \ 88 ASM_TYPE_FUNCTION(__interceptor_trampoline_##name); \ 89 __interceptor_trampoline_##name: \ 90 CFI_STARTPROC; \ 91 ASM_TAIL_CALL ASM_PREEMPTIBLE_SYM(__interceptor_##name); \ 92 CFI_ENDPROC; \ 93 ASM_SIZE(__interceptor_trampoline_##name) 94 # define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 1 95 # endif // Architecture supports interceptor trampoline 96 #else 97 # define ASM_HIDDEN(symbol) 98 # define ASM_TYPE_FUNCTION(symbol) 99 # define ASM_SIZE(symbol) 100 # define ASM_SYMBOL(symbol) _##symbol 101 # define ASM_SYMBOL_INTERCEPTOR(symbol) _wrap_##symbol 102 # define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol 103 #endif 104 105 #if defined(__ELF__) && (defined(__GNU__) || defined(__FreeBSD__) || \ 106 defined(__Fuchsia__) || defined(__linux__)) 107 // clang-format off 108 #define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits 109 // clang-format on 110 #else 111 #define NO_EXEC_STACK_DIRECTIVE 112 #endif 113 114 #if (defined(__x86_64__) || defined(__i386__)) && defined(__has_include) && __has_include(<cet.h>) 115 #include <cet.h> 116 #endif 117 #ifndef _CET_ENDBR 118 #define _CET_ENDBR 119 #endif 120