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
14#ifndef COMPILERRT_ASSEMBLY_H
15#define COMPILERRT_ASSEMBLY_H
16
17#if defined(__linux__) && defined(__CET__)
18#if __has_include(<cet.h>)
19#include <cet.h>
20#endif
21#endif
22
23#if defined(__APPLE__) && defined(__aarch64__)
24#define SEPARATOR %%
25#else
26#define SEPARATOR ;
27#endif
28
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
36
37#define NO_EXEC_STACK_DIRECTIVE
38
39#elif defined(__ELF__)
40
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
46#else
47#define SYMBOL_IS_FUNC(name) .type name,@function
48#endif
49#define CONST_SECTION .section .rodata
50
51#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
52 defined(__linux__)
53#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
54#else
55#define NO_EXEC_STACK_DIRECTIVE
56#endif
57
58#else // !__APPLE__ && !__ELF__
59
60#define HIDDEN(name)
61#define LOCAL_LABEL(name) .L ## name
62#define FILE_LEVEL_DIRECTIVE
63#define SYMBOL_IS_FUNC(name) \
64 .def FUNC_SYMBOL(name) SEPARATOR \
65 .scl 2 SEPARATOR \
66 .type 32 SEPARATOR \
67 .endef
68#define CONST_SECTION .section .rdata,"rd"
69
70#define NO_EXEC_STACK_DIRECTIVE
71
72#endif
73
74#if defined(__arm__) || defined(__aarch64__) || defined(__arm64ec__)
75#define FUNC_ALIGN \
76 .text SEPARATOR \
77 .balign 16 SEPARATOR
78#else
79#define FUNC_ALIGN
80#endif
81
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
87
88#if defined(__ARM_FEATURE_BTI_DEFAULT)
89#define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
90#else
91#define BTI_FLAG 0
92#endif
93
94#if __ARM_FEATURE_PAC_DEFAULT & 3
95#define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
96#else
97#define PAC_FLAG 0
98#endif
99
100#define GNU_PROPERTY(type, value) \
101 .pushsection .note.gnu.property, "a" SEPARATOR \
102 .p2align 3 SEPARATOR \
103 .word 4 SEPARATOR \
104 .word 16 SEPARATOR \
105 .word NT_GNU_PROPERTY_TYPE_0 SEPARATOR \
106 .asciz "GNU" SEPARATOR \
107 .word type SEPARATOR \
108 .word 4 SEPARATOR \
109 .word value SEPARATOR \
110 .word 0 SEPARATOR \
111 .popsection
112
113#if BTI_FLAG != 0
114#define BTI_C hint #34
115#define BTI_J hint #36
116#else
117#define BTI_C
118#define BTI_J
119#endif
120
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)
124#else
125#define GNU_PROPERTY_BTI_PAC
126#endif
127
128#if defined(__clang__) || defined(__GCC_HAVE_DWARF2_CFI_ASM)
129#define CFI_START .cfi_startproc
130#define CFI_END .cfi_endproc
131#else
132#define CFI_START
133#define CFI_END
134#endif
135
136#if defined(__arm__)
137
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__)
145#define USE_THUMB_2
146#define IT(cond) it cond
147#define ITT(cond) itt cond
148#define ITE(cond) ite cond
149#else
150#define USE_THUMB_1
151#define IT(cond)
152#define ITT(cond)
153#define 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
158#define IT(cond)
159#define ITT(cond)
160#define ITE(cond)
161#endif
162
163#if defined(USE_THUMB_1) && defined(USE_THUMB_2)
164#error "USE_THUMB_1 and USE_THUMB_2 can't be defined together."
165#endif
166
167#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
168#define ARM_HAS_BX
169#endif
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
173#endif
174
175#ifdef ARM_HAS_BX
176#define JMP(r) bx r
177#define JMPc(r, c) bx##c r
178#else
179#define JMP(r) mov pc, r
180#define JMPc(r, c) mov##c pc, r
181#endif
182
183// pop {pc} can't switch Thumb mode on ARMv4T
184#if __ARM_ARCH >= 5
185#define POP_PC() pop {pc}
186#else
187#define POP_PC() \
188 pop {ip}; \
189 JMP(ip)
190#endif
191
192#if defined(USE_THUMB_2)
193#define WIDE(op) op.w
194#else
195#define WIDE(op) op
196#endif
197
198#if defined(__ARM_FEATURE_PAC_DEFAULT) && defined(__ARM_FEATURE_BTI_DEFAULT)
199#define PACBTI_LANDING pacbti r12, lr, sp
200#elif defined(__ARM_FEATURE_PAC_DEFAULT)
201#define PACBTI_LANDING pac r12, lr, sp
202#elif defined(__ARM_FEATURE_BTI_DEFAULT)
203#define PACBTI_LANDING bti
204#else
205#define PACBTI_LANDING
206#endif
207
208#else // !defined(__arm)
209#define DECLARE_FUNC_ENCODING
210#define DEFINE_CODE_STATE
211#endif
212
213#define GLUE2_(a, b) a##b
214#define GLUE(a, b) GLUE2_(a, b)
215#define GLUE2(a, b) GLUE2_(a, b)
216#define GLUE3_(a, b, c) a##b##c
217#define GLUE3(a, b, c) GLUE3_(a, b, c)
218#define GLUE4_(a, b, c, d) a##b##c##d
219#define GLUE4(a, b, c, d) GLUE4_(a, b, c, d)
220
221#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
222#ifndef __arm64ec__
223#define FUNC_SYMBOL(name) name
224#else
225// On ARM64EC, function names and calls (but not address-taking or data symbol
226// references) use symbols prefixed with "#".
227#define QUOTE(a) #a
228#define STR(a) QUOTE(a)
229#define HASH #
230#define FUNC_SYMBOL(name) STR(GLUE2(HASH, name))
231#endif
232
233#ifdef VISIBILITY_HIDDEN
234#define DECLARE_SYMBOL_VISIBILITY(name) \
235 HIDDEN(SYMBOL_NAME(name)) SEPARATOR
236#define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name) \
237 HIDDEN(name) SEPARATOR
238#else
239#define DECLARE_SYMBOL_VISIBILITY(name)
240#define DECLARE_SYMBOL_VISIBILITY_UNMANGLED(name)
241#endif
242
243#define DEFINE_COMPILERRT_FUNCTION(name) \
244 DEFINE_CODE_STATE \
245 FILE_LEVEL_DIRECTIVE SEPARATOR \
246 .globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
247 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
248 DECLARE_SYMBOL_VISIBILITY(name) \
249 DECLARE_FUNC_ENCODING \
250 FUNC_SYMBOL(SYMBOL_NAME(name)):
251
252#define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \
253 DEFINE_CODE_STATE \
254 FILE_LEVEL_DIRECTIVE SEPARATOR \
255 .globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
256 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
257 DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
258 .thumb_func SEPARATOR \
259 FUNC_SYMBOL(SYMBOL_NAME(name)):
260
261#define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \
262 DEFINE_CODE_STATE \
263 FILE_LEVEL_DIRECTIVE SEPARATOR \
264 .globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
265 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
266 HIDDEN(SYMBOL_NAME(name)) SEPARATOR \
267 DECLARE_FUNC_ENCODING \
268 FUNC_SYMBOL(SYMBOL_NAME(name)):
269
270#define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \
271 DEFINE_CODE_STATE \
272 .globl FUNC_SYMBOL(name) SEPARATOR \
273 SYMBOL_IS_FUNC(name) SEPARATOR \
274 HIDDEN(name) SEPARATOR \
275 DECLARE_FUNC_ENCODING \
276 FUNC_SYMBOL(name):
277
278#define DEFINE_COMPILERRT_OUTLINE_FUNCTION_UNMANGLED(name) \
279 DEFINE_CODE_STATE \
280 FUNC_ALIGN \
281 .globl FUNC_SYMBOL(name) SEPARATOR \
282 SYMBOL_IS_FUNC(name) SEPARATOR \
283 DECLARE_SYMBOL_VISIBILITY_UNMANGLED(FUNC_SYMBOL(name)) SEPARATOR \
284 DECLARE_FUNC_ENCODING \
285 FUNC_SYMBOL(name): \
286 SEPARATOR CFI_START \
287 SEPARATOR BTI_C
288
289#define DEFINE_COMPILERRT_FUNCTION_ALIAS(name, target) \
290 .globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
291 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
292 DECLARE_SYMBOL_VISIBILITY(name) SEPARATOR \
293 .set FUNC_SYMBOL(SYMBOL_NAME(name)), FUNC_SYMBOL(target) SEPARATOR
294
295#if defined(__ARM_EABI__)
296#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name) \
297 DEFINE_COMPILERRT_FUNCTION_ALIAS(aeabi_name, name)
298#else
299#define DEFINE_AEABI_FUNCTION_ALIAS(aeabi_name, name)
300#endif
301
302#ifdef __ELF__
303#define END_COMPILERRT_FUNCTION(name) \
304 .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
305#define END_COMPILERRT_OUTLINE_FUNCTION(name) \
306 CFI_END SEPARATOR \
307 .size SYMBOL_NAME(name), . - SYMBOL_NAME(name)
308#else
309#define END_COMPILERRT_FUNCTION(name)
310#define END_COMPILERRT_OUTLINE_FUNCTION(name) \
311 CFI_END
312#endif
313
314#ifdef __arm__
315#include "int_endianness.h"
316
317#if _YUGA_BIG_ENDIAN
318#define VMOV_TO_DOUBLE(dst, src0, src1) vmov dst, src1, src0 SEPARATOR
319#define VMOV_FROM_DOUBLE(dst0, dst1, src) vmov dst1, dst0, src SEPARATOR
320#else
321#define VMOV_TO_DOUBLE(dst, src0, src1) vmov dst, src0, src1 SEPARATOR
322#define VMOV_FROM_DOUBLE(dst0, dst1, src) vmov dst0, dst1, src SEPARATOR
323#endif
324#endif
325
326#endif // COMPILERRT_ASSEMBLY_H
327

source code of compiler-rt/lib/builtins/assembly.h