1 //===-- assembly.h - compiler-rt assembler support macros -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines macros for use in compiler-rt assembler source.
10 // This file is not part of the interface of this library.
12 //===----------------------------------------------------------------------===//
14 #ifndef COMPILERRT_ASSEMBLY_H
15 #define COMPILERRT_ASSEMBLY_H
17 #if defined(__linux__) && defined(__CET__)
18 #if __has_include(<cet.h>)
23 #if defined(__APPLE__) && defined(__aarch64__)
29 #if defined(__APPLE__)
30 #define HIDDEN(name) .private_extern name
31 #define LOCAL_LABEL(name) L_##name
32 // tell linker it can break up file at label boundaries
33 #define FILE_LEVEL_DIRECTIVE .subsections_via_symbols
34 #define SYMBOL_IS_FUNC(name)
35 #define CONST_SECTION .const
37 #define NO_EXEC_STACK_DIRECTIVE
39 #elif defined(__ELF__)
41 #define HIDDEN(name) .hidden name
42 #define LOCAL_LABEL(name) .L_##name
43 #define FILE_LEVEL_DIRECTIVE
44 #if defined(__arm__) || defined(__aarch64__)
45 #define SYMBOL_IS_FUNC(name) .type name,%function
47 #define SYMBOL_IS_FUNC(name) .type name,@function
49 #define CONST_SECTION .section .rodata
51 #if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
53 #define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
55 #define NO_EXEC_STACK_DIRECTIVE
58 #else // !__APPLE__ && !__ELF__
61 #define LOCAL_LABEL(name) .L ## name
62 #define FILE_LEVEL_DIRECTIVE
63 #define SYMBOL_IS_FUNC(name) \
68 #define CONST_SECTION .section .rdata,"rd"
70 #define NO_EXEC_STACK_DIRECTIVE
74 #if defined(__arm__) || defined(__aarch64__)
82 // BTI and PAC gnu property note
83 #define NT_GNU_PROPERTY_TYPE_0 5
84 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
85 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 1
86 #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 2
88 #if defined(__ARM_FEATURE_BTI_DEFAULT)
89 #define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
94 #if __ARM_FEATURE_PAC_DEFAULT & 3
95 #define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
100 #define GNU_PROPERTY(type, value) \
101 .pushsection .note.gnu.property, "a" SEPARATOR \
102 .p2align 3 SEPARATOR \
105 .word NT_GNU_PROPERTY_TYPE_0 SEPARATOR \
106 .asciz "GNU" SEPARATOR \
107 .word type SEPARATOR \
109 .word value SEPARATOR \
114 #define BTI_C hint #34
115 #define BTI_J hint #36
121 #if (BTI_FLAG | PAC_FLAG) != 0
122 #define GNU_PROPERTY_BTI_PAC \
123 GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
125 #define GNU_PROPERTY_BTI_PAC
128 #if defined(__clang__) || defined(__GCC_HAVE_DWARF2_CFI_ASM)
129 #define CFI_START .cfi_startproc
130 #define CFI_END .cfi_endproc
138 // Determine actual [ARM][THUMB[1][2]] ISA using compiler predefined macros:
139 // - for '-mthumb -march=armv6' compiler defines '__thumb__'
140 // - for '-mthumb -march=armv7' compiler defines '__thumb__' and '__thumb2__'
141 #if defined(__thumb2__) || defined(__thumb__)
142 #define DEFINE_CODE_STATE .thumb SEPARATOR
143 #define DECLARE_FUNC_ENCODING .thumb_func SEPARATOR
144 #if defined(__thumb2__)
146 #define IT(cond) it cond
147 #define ITT(cond) itt cond
148 #define ITE(cond) ite cond
154 #endif // defined(__thumb__2)
155 #else // !defined(__thumb2__) && !defined(__thumb__)
156 #define DEFINE_CODE_STATE .arm SEPARATOR
157 #define DECLARE_FUNC_ENCODING
163 #if defined(USE_THUMB_1) && defined(USE_THUMB_2)
164 #error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."
167 #if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
170 #if !defined(__ARM_FEATURE_CLZ) && !defined(USE_THUMB_1) && \
171 (__ARM_ARCH >= 6 || (__ARM_ARCH == 5 && !defined(__ARM_ARCH_5__)))
172 #define __ARM_FEATURE_CLZ
177 #define JMPc(r, c) bx##c r
179 #define JMP(r) mov pc, r
180 #define JMPc(r, c) mov##c pc, r
183 // pop {pc} can't switch Thumb mode on ARMv4T
185 #define POP_PC() pop {pc}
192 #if defined(USE_THUMB_2)
193 #define WIDE(op) op.w
197 #else // !defined(__arm)
198 #define DECLARE_FUNC_ENCODING
199 #define DEFINE_CODE_STATE
202 #define GLUE2_(a, b) a##b
203 #define GLUE(a, b) GLUE2_(a, b)
204 #define GLUE2(a, b) GLUE2_(a, b)
205 #define GLUE3_(a, b, c) a##b##c
206 #define GLUE3(a, b, c) GLUE3_(a, b, c)
207 #define GLUE4_(a, b, c, d) a##b##c##d
208 #define GLUE4(a, b, c, d) GLUE4_(a, b, c, d)
210 #define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
212 #ifdef VISIBILITY_HIDDEN
213 #define DECLARE_SYMBOL_VISIBILITY(name) \
214 HIDDEN(SYMBOL_NAME(name)) SEPARATOR
215 #define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) \
216 HIDDEN(name) SEPARATOR
218 #define DECLARE_SYMBOL_VISIBILITY(name)
219 #define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name)
222 #define DEFINE_COMPILERRT_FUNCTION(name) \
224 FILE_LEVEL_DIRECTIVE SEPARATOR \
225 .globl SYMBOL_NAME(name) SEPARATOR \
226 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
227 DECLARE_SYMBOL_VISIBILITY(name) \
228 DECLARE_FUNC_ENCODING \
231 #define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \
233 FILE_LEVEL_DIRECTIVE SEPARATOR \
234 .globl SYMBOL_NAME(name) SEPARATOR \
235 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
236 DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
237 .thumb_func SEPARATOR \
240 #define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \
242 FILE_LEVEL_DIRECTIVE SEPARATOR \
243 .globl SYMBOL_NAME(name) SEPARATOR \
244 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
245 HIDDEN(SYMBOL_NAME(name)) SEPARATOR \
246 DECLARE_FUNC_ENCODING \
249 #define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \
251 .globl name SEPARATOR \
252 SYMBOL_IS_FUNC(name) SEPARATOR \
253 HIDDEN(name) SEPARATOR \
254 DECLARE_FUNC_ENCODING \
257 #define DEFINE_COMPILERRT_OUTLINE_FUNCTION_UNMANGLED(name) \
260 .globl name SEPARATOR \
261 SYMBOL_IS_FUNC(name) SEPARATOR \
262 DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) SEPARATOR \
263 CFI_START SEPARATOR \
264 DECLARE_FUNC_ENCODING \
265 name: SEPARATOR BTI_C
267 #define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target) \
268 .globl SYMBOL_NAME(name) SEPARATOR \
269 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
270 DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
271 .set SYMBOL_NAME(name), SYMBOL_NAME(target) SEPARATOR
273 #if defined(__ARM_EABI__)
274 #define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name) \
275 DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)
277 #define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)
281 #define END_COMPILERRT_FUNCTION(name) \
282 .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
283 #define END_COMPILERRT_OUTLINE_FUNCTION(name) \
285 .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
287 #define END_COMPILERRT_FUNCTION(name)
288 #define END_COMPILERRT_OUTLINE_FUNCTION(name) \
292 #endif // COMPILERRT_ASSEMBLY_H