assembly.h 10 KB

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