assembly.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //===-- assembly.h - compiler-rt assembler support macros -----------------===//
  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. // This file defines macros for use in compiler-rt assembler source.
  10. // This file is not part of the interface of this library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef COMPILERRT_ASSEMBLY_H
  14. #define COMPILERRT_ASSEMBLY_H
  15. #if defined(__linux__) && defined(__CET__)
  16. #if __has_include(<cet.h>)
  17. #include <cet.h>
  18. #endif
  19. #endif
  20. #if defined(__APPLE__) && defined(__aarch64__)
  21. #define SEPARATOR %%
  22. #else
  23. #define SEPARATOR ;
  24. #endif
  25. #if defined(__APPLE__)
  26. #define HIDDEN(name) .private_extern name
  27. #define LOCAL_LABEL(name) L_##name
  28. // tell linker it can break up file at label boundaries
  29. #define FILE_LEVEL_DIRECTIVE .subsections_via_symbols
  30. #define SYMBOL_IS_FUNC(name)
  31. #define CONST_SECTION .const
  32. #define NO_EXEC_STACK_DIRECTIVE
  33. #elif defined(__ELF__)
  34. #define HIDDEN(name) .hidden name
  35. #define LOCAL_LABEL(name) .L_##name
  36. #define FILE_LEVEL_DIRECTIVE
  37. #if defined(__arm__) || defined(__aarch64__)
  38. #define SYMBOL_IS_FUNC(name) .type name,%function
  39. #else
  40. #define SYMBOL_IS_FUNC(name) .type name,@function
  41. #endif
  42. #define CONST_SECTION .section .rodata
  43. #if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
  44. defined(__linux__)
  45. #define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
  46. #else
  47. #define NO_EXEC_STACK_DIRECTIVE
  48. #endif
  49. #else // !__APPLE__ && !__ELF__
  50. #define HIDDEN(name)
  51. #define LOCAL_LABEL(name) .L ## name
  52. #define FILE_LEVEL_DIRECTIVE
  53. #define SYMBOL_IS_FUNC(name) \
  54. .def name SEPARATOR \
  55. .scl 2 SEPARATOR \
  56. .type 32 SEPARATOR \
  57. .endef
  58. #define CONST_SECTION .section .rdata,"rd"
  59. #define NO_EXEC_STACK_DIRECTIVE
  60. #endif
  61. #if defined(__arm__) || defined(__aarch64__)
  62. #define FUNC_ALIGN \
  63. .text SEPARATOR \
  64. .balign 16 SEPARATOR
  65. #else
  66. #define FUNC_ALIGN
  67. #endif
  68. // BTI and PAC gnu property note
  69. #define NT_GNU_PROPERTY_TYPE_0 5
  70. #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
  71. #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 1
  72. #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 2
  73. #if defined(__ARM_FEATURE_BTI_DEFAULT)
  74. #define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
  75. #else
  76. #define BTI_FLAG 0
  77. #endif
  78. #if __ARM_FEATURE_PAC_DEFAULT & 3
  79. #define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
  80. #else
  81. #define PAC_FLAG 0
  82. #endif
  83. #define GNU_PROPERTY(type, value) \
  84. .pushsection .note.gnu.property, "a" SEPARATOR \
  85. .p2align 3 SEPARATOR \
  86. .word 4 SEPARATOR \
  87. .word 16 SEPARATOR \
  88. .word NT_GNU_PROPERTY_TYPE_0 SEPARATOR \
  89. .asciz "GNU" SEPARATOR \
  90. .word type SEPARATOR \
  91. .word 4 SEPARATOR \
  92. .word value SEPARATOR \
  93. .word 0 SEPARATOR \
  94. .popsection
  95. #if BTI_FLAG != 0
  96. #define BTI_C hint #34
  97. #define BTI_J hint #36
  98. #else
  99. #define BTI_C
  100. #define BTI_J
  101. #endif
  102. #if (BTI_FLAG | PAC_FLAG) != 0
  103. #define GNU_PROPERTY_BTI_PAC \
  104. GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
  105. #else
  106. #define GNU_PROPERTY_BTI_PAC
  107. #endif
  108. #if defined(__clang__) || defined(__GCC_HAVE_DWARF2_CFI_ASM)
  109. #define CFI_START .cfi_startproc
  110. #define CFI_END .cfi_endproc
  111. #else
  112. #define CFI_START
  113. #define CFI_END
  114. #endif
  115. #if defined(__arm__)
  116. // Determine actual [ARM][THUMB[1][2]] ISA using compiler predefined macros:
  117. // - for '-mthumb -march=armv6' compiler defines '__thumb__'
  118. // - for '-mthumb -march=armv7' compiler defines '__thumb__' and '__thumb2__'
  119. #if defined(__thumb2__) || defined(__thumb__)
  120. #define DEFINE_CODE_STATE .thumb SEPARATOR
  121. #define DECLARE_FUNC_ENCODING .thumb_func SEPARATOR
  122. #if defined(__thumb2__)
  123. #define USE_THUMB_2
  124. #define IT(cond) it cond
  125. #define ITT(cond) itt cond
  126. #define ITE(cond) ite cond
  127. #else
  128. #define USE_THUMB_1
  129. #define IT(cond)
  130. #define ITT(cond)
  131. #define ITE(cond)
  132. #endif // defined(__thumb__2)
  133. #else // !defined(__thumb2__) && !defined(__thumb__)
  134. #define DEFINE_CODE_STATE .arm SEPARATOR
  135. #define DECLARE_FUNC_ENCODING
  136. #define IT(cond)
  137. #define ITT(cond)
  138. #define ITE(cond)
  139. #endif
  140. #if defined(USE_THUMB_1) && defined(USE_THUMB_2)
  141. #error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."
  142. #endif
  143. #if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
  144. #define ARM_HAS_BX
  145. #endif
  146. #if !defined(__ARM_FEATURE_CLZ) && !defined(USE_THUMB_1) && \
  147. (__ARM_ARCH >= 6 || (__ARM_ARCH == 5 && !defined(__ARM_ARCH_5__)))
  148. #define __ARM_FEATURE_CLZ
  149. #endif
  150. #ifdef ARM_HAS_BX
  151. #define JMP(r) bx r
  152. #define JMPc(r, c) bx##c r
  153. #else
  154. #define JMP(r) mov pc, r
  155. #define JMPc(r, c) mov##c pc, r
  156. #endif
  157. // pop {pc} can't switch Thumb mode on ARMv4T
  158. #if __ARM_ARCH >= 5
  159. #define POP_PC() pop {pc}
  160. #else
  161. #define POP_PC() \
  162. pop {ip}; \
  163. JMP(ip)
  164. #endif
  165. #if defined(USE_THUMB_2)
  166. #define WIDE(op) op.w
  167. #else
  168. #define WIDE(op) op
  169. #endif
  170. #else // !defined(__arm)
  171. #define DECLARE_FUNC_ENCODING
  172. #define DEFINE_CODE_STATE
  173. #endif
  174. #define GLUE2_(a, b) a##b
  175. #define GLUE(a, b) GLUE2_(a, b)
  176. #define GLUE2(a, b) GLUE2_(a, b)
  177. #define GLUE3_(a, b, c) a##b##c
  178. #define GLUE3(a, b, c) GLUE3_(a, b, c)
  179. #define GLUE4_(a, b, c, d) a##b##c##d
  180. #define GLUE4(a, b, c, d) GLUE4_(a, b, c, d)
  181. #define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
  182. #ifdef VISIBILITY_HIDDEN
  183. #define DECLARE_SYMBOL_VISIBILITY(name) \
  184. HIDDEN(SYMBOL_NAME(name)) SEPARATOR
  185. #define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) \
  186. HIDDEN(name) SEPARATOR
  187. #else
  188. #define DECLARE_SYMBOL_VISIBILITY(name)
  189. #define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name)
  190. #endif
  191. #define DEFINE_COMPILERRT_FUNCTION(name) \
  192. DEFINE_CODE_STATE \
  193. FILE_LEVEL_DIRECTIVE SEPARATOR \
  194. .globl SYMBOL_NAME(name) SEPARATOR \
  195. SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
  196. DECLARE_SYMBOL_VISIBILITY(name) \
  197. DECLARE_FUNC_ENCODING \
  198. SYMBOL_NAME(name):
  199. #define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \
  200. DEFINE_CODE_STATE \
  201. FILE_LEVEL_DIRECTIVE SEPARATOR \
  202. .globl SYMBOL_NAME(name) SEPARATOR \
  203. SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
  204. DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
  205. .thumb_func SEPARATOR \
  206. SYMBOL_NAME(name):
  207. #define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \
  208. DEFINE_CODE_STATE \
  209. FILE_LEVEL_DIRECTIVE SEPARATOR \
  210. .globl SYMBOL_NAME(name) SEPARATOR \
  211. SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
  212. HIDDEN(SYMBOL_NAME(name)) SEPARATOR \
  213. DECLARE_FUNC_ENCODING \
  214. SYMBOL_NAME(name):
  215. #define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \
  216. DEFINE_CODE_STATE \
  217. .globl name SEPARATOR \
  218. SYMBOL_IS_FUNC(name) SEPARATOR \
  219. HIDDEN(name) SEPARATOR \
  220. DECLARE_FUNC_ENCODING \
  221. name:
  222. #define DEFINE_COMPILERRT_OUTLINE_FUNCTION_UNMANGLED(name) \
  223. DEFINE_CODE_STATE \
  224. FUNC_ALIGN \
  225. .globl name SEPARATOR \
  226. SYMBOL_IS_FUNC(name) SEPARATOR \
  227. DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) SEPARATOR \
  228. DECLARE_FUNC_ENCODING \
  229. name: \
  230. SEPARATOR CFI_START \
  231. SEPARATOR BTI_C
  232. #define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target) \
  233. .globl SYMBOL_NAME(name) SEPARATOR \
  234. SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
  235. DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
  236. .set SYMBOL_NAME(name), SYMBOL_NAME(target) SEPARATOR
  237. #if defined(__ARM_EABI__)
  238. #define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name) \
  239. DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)
  240. #else
  241. #define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)
  242. #endif
  243. #ifdef __ELF__
  244. #define END_COMPILERRT_FUNCTION(name) \
  245. .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
  246. #define END_COMPILERRT_OUTLINE_FUNCTION(name) \
  247. CFI_END SEPARATOR \
  248. .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
  249. #else
  250. #define END_COMPILERRT_FUNCTION(name)
  251. #define END_COMPILERRT_OUTLINE_FUNCTION(name) \
  252. CFI_END
  253. #endif
  254. #endif // COMPILERRT_ASSEMBLY_H