sanitizer_asm.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Some toolchains do not support .cfi asm directives, so we have to hide
  13. // them inside macros.
  14. #if defined(__clang__) || \
  15. (defined(__GNUC__) && defined(__GCC_HAVE_DWARF2_CFI_ASM))
  16. // GCC defined __GCC_HAVE_DWARF2_CFI_ASM if it supports CFI.
  17. // Clang seems to support CFI by default (or not?).
  18. // We need two versions of macros: for inline asm and standalone asm files.
  19. # define CFI_INL_ADJUST_CFA_OFFSET(n) ".cfi_adjust_cfa_offset " #n ";"
  20. # define CFI_STARTPROC .cfi_startproc
  21. # define CFI_ENDPROC .cfi_endproc
  22. # define CFI_ADJUST_CFA_OFFSET(n) .cfi_adjust_cfa_offset n
  23. # define CFI_DEF_CFA_OFFSET(n) .cfi_def_cfa_offset n
  24. # define CFI_REL_OFFSET(reg, n) .cfi_rel_offset reg, n
  25. # define CFI_OFFSET(reg, n) .cfi_offset reg, n
  26. # define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg
  27. # define CFI_DEF_CFA(reg, n) .cfi_def_cfa reg, n
  28. # define CFI_RESTORE(reg) .cfi_restore reg
  29. #else // No CFI
  30. # define CFI_INL_ADJUST_CFA_OFFSET(n)
  31. # define CFI_STARTPROC
  32. # define CFI_ENDPROC
  33. # define CFI_ADJUST_CFA_OFFSET(n)
  34. # define CFI_DEF_CFA_OFFSET(n)
  35. # define CFI_REL_OFFSET(reg, n)
  36. # define CFI_OFFSET(reg, n)
  37. # define CFI_DEF_CFA_REGISTER(reg)
  38. # define CFI_DEF_CFA(reg, n)
  39. # define CFI_RESTORE(reg)
  40. #endif
  41. #if defined(__x86_64__) || defined(__i386__) || defined(__sparc__)
  42. # define ASM_TAIL_CALL jmp
  43. #elif defined(__arm__) || defined(__aarch64__) || defined(__mips__) || \
  44. defined(__powerpc__) || defined(__loongarch_lp64)
  45. # define ASM_TAIL_CALL b
  46. #elif defined(__s390__)
  47. # define ASM_TAIL_CALL jg
  48. #elif defined(__riscv)
  49. # define ASM_TAIL_CALL tail
  50. #endif
  51. // Currently, almost all of the shared libraries rely on the value of
  52. // $t9 to get the address of current function, instead of PCREL, even
  53. // on MIPSr6. To be compatiable with them, we have to set $t9 properly.
  54. // MIPS uses GOT to get the address of preemptible functions.
  55. #if defined(__mips64)
  56. # define C_ASM_TAIL_CALL(t_func, i_func) \
  57. "lui $t8, %hi(%neg(%gp_rel(" t_func ")))\n" \
  58. "daddu $t8, $t8, $t9\n" \
  59. "daddiu $t8, $t8, %lo(%neg(%gp_rel(" t_func ")))\n" \
  60. "ld $t9, %got_disp(" i_func ")($t8)\n" \
  61. "jr $t9\n"
  62. #elif defined(__mips__)
  63. # define C_ASM_TAIL_CALL(t_func, i_func) \
  64. ".set noreorder\n" \
  65. ".cpload $t9\n" \
  66. ".set reorder\n" \
  67. "lw $t9, %got(" i_func ")($gp)\n" \
  68. "jr $t9\n"
  69. #elif defined(ASM_TAIL_CALL)
  70. # define C_ASM_TAIL_CALL(t_func, i_func) \
  71. SANITIZER_STRINGIFY(ASM_TAIL_CALL) " " i_func
  72. #endif
  73. #if defined(__ELF__) && defined(__x86_64__) || defined(__i386__) || \
  74. defined(__riscv)
  75. # define ASM_PREEMPTIBLE_SYM(sym) sym@plt
  76. #else
  77. # define ASM_PREEMPTIBLE_SYM(sym) sym
  78. #endif
  79. #if !defined(__APPLE__)
  80. # define ASM_HIDDEN(symbol) .hidden symbol
  81. # if defined(__arm__) || defined(__aarch64__)
  82. # define ASM_TYPE_FUNCTION(symbol) .type symbol, %function
  83. # else
  84. # define ASM_TYPE_FUNCTION(symbol) .type symbol, @function
  85. # endif
  86. # define ASM_SIZE(symbol) .size symbol, .-symbol
  87. # define ASM_SYMBOL(symbol) symbol
  88. # define ASM_SYMBOL_INTERCEPTOR(symbol) symbol
  89. # if defined(__i386__) || defined(__powerpc__) || defined(__s390__) || \
  90. defined(__sparc__)
  91. // For details, see interception.h
  92. # define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol
  93. # define ASM_TRAMPOLINE_ALIAS(symbol, name) \
  94. .weak symbol; \
  95. .set symbol, ASM_WRAPPER_NAME(name)
  96. # define ASM_INTERCEPTOR_TRAMPOLINE(name)
  97. # define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 0
  98. # else // Architecture supports interceptor trampoline
  99. // Keep trampoline implementation in sync with interception/interception.h
  100. # define ASM_WRAPPER_NAME(symbol) ___interceptor_##symbol
  101. # define ASM_TRAMPOLINE_ALIAS(symbol, name) \
  102. .weak symbol; \
  103. .set symbol, __interceptor_trampoline_##name
  104. # define ASM_INTERCEPTOR_TRAMPOLINE(name) \
  105. .weak __interceptor_##name; \
  106. .set __interceptor_##name, ASM_WRAPPER_NAME(name); \
  107. .globl __interceptor_trampoline_##name; \
  108. ASM_TYPE_FUNCTION(__interceptor_trampoline_##name); \
  109. __interceptor_trampoline_##name: \
  110. CFI_STARTPROC; \
  111. ASM_TAIL_CALL ASM_PREEMPTIBLE_SYM(__interceptor_##name); \
  112. CFI_ENDPROC; \
  113. ASM_SIZE(__interceptor_trampoline_##name)
  114. # define ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT 1
  115. # endif // Architecture supports interceptor trampoline
  116. #else
  117. # define ASM_HIDDEN(symbol)
  118. # define ASM_TYPE_FUNCTION(symbol)
  119. # define ASM_SIZE(symbol)
  120. # define ASM_SYMBOL(symbol) _##symbol
  121. # define ASM_SYMBOL_INTERCEPTOR(symbol) _wrap_##symbol
  122. # define ASM_WRAPPER_NAME(symbol) __interceptor_##symbol
  123. #endif
  124. #if defined(__ELF__) && (defined(__GNU__) || defined(__FreeBSD__) || \
  125. defined(__Fuchsia__) || defined(__linux__))
  126. // clang-format off
  127. #define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
  128. // clang-format on
  129. #else
  130. #define NO_EXEC_STACK_DIRECTIVE
  131. #endif
  132. #if (defined(__x86_64__) || defined(__i386__)) && defined(__has_include) && __has_include(<cet.h>)
  133. #include <cet.h>
  134. #endif
  135. #ifndef _CET_ENDBR
  136. #define _CET_ENDBR
  137. #endif