brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.6 KiB · 30e9d15 Raw
157 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// Various support for assembler.10//11//===----------------------------------------------------------------------===//12 13// Some toolchains do not support .cfi asm directives, so we have to hide14// 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_startproc23# define CFI_ENDPROC .cfi_endproc24# define CFI_ADJUST_CFA_OFFSET(n) .cfi_adjust_cfa_offset n25# define CFI_DEF_CFA_OFFSET(n) .cfi_def_cfa_offset n26# define CFI_REL_OFFSET(reg, n) .cfi_rel_offset reg, n27# define CFI_OFFSET(reg, n) .cfi_offset reg, n28# define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg29# define CFI_DEF_CFA(reg, n) .cfi_def_cfa reg, n30# define CFI_RESTORE(reg) .cfi_restore reg31 32#else  // No CFI33# define CFI_INL_ADJUST_CFA_OFFSET(n)34# define CFI_STARTPROC35# define CFI_ENDPROC36# 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#endif44 45#if defined(__aarch64__) && defined(__ARM_FEATURE_BTI_DEFAULT)46# define ASM_STARTPROC CFI_STARTPROC; hint #3447# define C_ASM_STARTPROC SANITIZER_STRINGIFY(CFI_STARTPROC) "\nhint #34"48#else49# define ASM_STARTPROC CFI_STARTPROC50# define C_ASM_STARTPROC SANITIZER_STRINGIFY(CFI_STARTPROC)51#endif52#define ASM_ENDPROC CFI_ENDPROC53#define C_ASM_ENDPROC SANITIZER_STRINGIFY(CFI_ENDPROC)54 55#if defined(__x86_64__) || defined(__i386__) || defined(__sparc__)56# define ASM_TAIL_CALL jmp57#elif defined(__arm__) || defined(__aarch64__) || defined(__mips__) || \58    defined(__powerpc__) || defined(__loongarch_lp64)59# define ASM_TAIL_CALL b60#elif defined(__s390__)61# define ASM_TAIL_CALL jg62#elif defined(__riscv)63# define ASM_TAIL_CALL tail64#endif65 66// Currently, almost all of the shared libraries rely on the value of67// $t9 to get the address of current function, instead of PCREL, even68// on MIPSr6. To be compatiable with them, we have to set $t9 properly.69// MIPS uses GOT to get the address of preemptible functions.70#if defined(__mips64)71#  define C_ASM_TAIL_CALL(t_func, i_func)                       \72    "lui $t8, %hi(%neg(%gp_rel(" t_func ")))\n"                 \73    "daddu $t8, $t8, $t9\n"                                     \74    "daddiu $t8, $t8, %lo(%neg(%gp_rel(" t_func ")))\n"         \75    "ld $t9, %got_disp(" i_func ")($t8)\n"                      \76    "jr $t9\n"77#elif defined(__mips__)78#  define C_ASM_TAIL_CALL(t_func, i_func)                       \79    ".set    noreorder\n"                                       \80    ".cpload $t9\n"                                             \81    ".set    reorder\n"                                         \82    "lw $t9, %got(" i_func ")($gp)\n"                           \83    "jr $t9\n"84#elif defined(ASM_TAIL_CALL)85#  define C_ASM_TAIL_CALL(t_func, i_func)                       \86    SANITIZER_STRINGIFY(ASM_TAIL_CALL) " " i_func87#endif88 89#if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \90    defined(__riscv)91# define ASM_PREEMPTIBLE_SYM(sym) sym@plt92#else93# define ASM_PREEMPTIBLE_SYM(sym) sym94#endif95 96#if !defined(__APPLE__)97# define ASM_HIDDEN(symbol) .hidden symbol98# if defined(__arm__) || defined(__aarch64__)99#  define ASM_TYPE_FUNCTION(symbol) .type symbol, %function100# else101#  define ASM_TYPE_FUNCTION(symbol) .type symbol, @function102# endif103# define ASM_SIZE(symbol) .size symbol, .-symbol104# define ASM_SYMBOL(symbol) symbol105# define ASM_SYMBOL_INTERCEPTOR(symbol) symbol106# if defined(__i386__) || defined(__powerpc__) || defined(__s390__) || \107     defined(__sparc__)108// For details, see interception.h109#  define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol110#  define ASM_TRAMPOLINE_ALIAS(symbol, name)                                   \111         .weak symbol;                                                         \112         .set symbol, ASM_WRAPPER_NAME(name)113#  define ASM_INTERCEPTOR_TRAMPOLINE(name)114#  define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 0115# else  // Architecture supports interceptor trampoline116// Keep trampoline implementation in sync with interception/interception.h117#  define ASM_WRAPPER_NAME(symbol) ___interceptor_##symbol118#  define ASM_TRAMPOLINE_ALIAS(symbol, name)                                   \119         .weak symbol;                                                         \120         .set symbol, __interceptor_trampoline_##name121#  define ASM_INTERCEPTOR_TRAMPOLINE(name)                                     \122         .weak __interceptor_##name;                                           \123         .set __interceptor_##name, ASM_WRAPPER_NAME(name);                    \124         .globl __interceptor_trampoline_##name;                               \125         ASM_TYPE_FUNCTION(__interceptor_trampoline_##name);                   \126         __interceptor_trampoline_##name:                                      \127                 ASM_STARTPROC;                                                \128                 ASM_TAIL_CALL ASM_PREEMPTIBLE_SYM(__interceptor_##name);      \129                 ASM_ENDPROC;                                                  \130         ASM_SIZE(__interceptor_trampoline_##name)131#  define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 1132# endif  // Architecture supports interceptor trampoline133#else134# define ASM_HIDDEN(symbol)135# define ASM_TYPE_FUNCTION(symbol)136# define ASM_SIZE(symbol)137# define ASM_SYMBOL(symbol) _##symbol138# define ASM_SYMBOL_INTERCEPTOR(symbol) _wrap_##symbol139# define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol140#endif141 142#if defined(__ELF__) && (defined(__GNU__) || defined(__FreeBSD__) || \143                         defined(__Fuchsia__) || defined(__linux__))144// clang-format off145#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits146// clang-format on147#else148#define NO_EXEC_STACK_DIRECTIVE149#endif150 151#if (defined(__x86_64__) || defined(__i386__)) && defined(__has_include) && __has_include(<cet.h>)152#include <cet.h>153#endif154#ifndef _CET_ENDBR155#define _CET_ENDBR156#endif157