1/* ===-- assembly.h - libUnwind 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 libUnwind assembler source.
10 * This file is not part of the interface of this library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
14
15#ifndef UNWIND_ASSEMBLY_H
16#define UNWIND_ASSEMBLY_H
17
18#if defined(__x86_64__) && defined(__FreeBSD__) && \
19 !defined(_LIBUNWIND_IS_BAREMETAL)
20#include <sys/syscall.h>
21// <machine/sysarch.h> contains C declarations and cannot be included here.
22#define X86_64_GET_FSBASE 128 // AMD64_GET_FSBASE
23#define X86_64_GET_GSBASE 130 // AMD64_GET_GSBASE
24#define X86_64_BASE_SYSCALL SYS_sysarch
25#endif
26
27#if defined(__CET__)
28#include <cet.h>
29#define _LIBUNWIND_CET_ENDBR _CET_ENDBR
30#else
31#define _LIBUNWIND_CET_ENDBR
32#endif
33
34#if defined(__powerpc64__)
35#define SEPARATOR ;
36#define PPC64_OFFS_SRR0 0
37#define PPC64_OFFS_CR 272
38#define PPC64_OFFS_XER 280
39#define PPC64_OFFS_LR 288
40#define PPC64_OFFS_CTR 296
41#define PPC64_OFFS_VRSAVE 304
42#define PPC64_OFFS_FP 312
43#define PPC64_OFFS_V 824
44#elif defined(__APPLE__) && defined(__aarch64__)
45#define SEPARATOR %%
46#elif defined(__riscv)
47# define RISCV_ISIZE (__riscv_xlen / 8)
48# define RISCV_FOFFSET (RISCV_ISIZE * 32)
49# if defined(__riscv_flen)
50# define RISCV_FSIZE (__riscv_flen / 8)
51# endif
52
53# if __riscv_xlen == 64
54# define ILOAD ld
55# define ISTORE sd
56# elif __riscv_xlen == 32
57# define ILOAD lw
58# define ISTORE sw
59# else
60# error "Unsupported __riscv_xlen"
61# endif
62
63# if defined(__riscv_flen)
64# if __riscv_flen == 64
65# define FLOAD fld
66# define FSTORE fsd
67# elif __riscv_flen == 32
68# define FLOAD flw
69# define FSTORE fsw
70# else
71# error "Unsupported __riscv_flen"
72# endif
73# endif
74# define SEPARATOR ;
75#else
76#define SEPARATOR ;
77#endif
78
79#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1) && \
80 !defined(_AIX)
81#define PPC64_OPD1 .section .opd,"aw",@progbits SEPARATOR
82#define PPC64_OPD2 SEPARATOR \
83 .p2align 3 SEPARATOR \
84 .quad .Lfunc_begin0 SEPARATOR \
85 .quad .TOC.@tocbase SEPARATOR \
86 .quad 0 SEPARATOR \
87 .text SEPARATOR \
88.Lfunc_begin0:
89#else
90#define PPC64_OPD1
91#define PPC64_OPD2
92#endif
93
94#if defined(__aarch64__)
95#if defined(__ARM_FEATURE_GCS_DEFAULT) && defined(__ARM_FEATURE_BTI_DEFAULT)
96// Set BTI, PAC, and GCS gnu property bits
97#define GNU_PROPERTY 7
98// We indirectly branch to __libunwind_Registers_arm64_jumpto from
99// __unw_phase2_resume, so we need to use bti jc.
100#define AARCH64_BTI bti jc
101#elif defined(__ARM_FEATURE_GCS_DEFAULT)
102// Set GCS gnu property bit
103#define GNU_PROPERTY 4
104#elif defined(__ARM_FEATURE_BTI_DEFAULT)
105// Set BTI and PAC gnu property bits
106#define GNU_PROPERTY 3
107#define AARCH64_BTI bti c
108#endif
109#ifdef GNU_PROPERTY
110 .pushsection ".note.gnu.property", "a" SEPARATOR \
111 .balign 8 SEPARATOR \
112 .long 4 SEPARATOR \
113 .long 0x10 SEPARATOR \
114 .long 0x5 SEPARATOR \
115 .asciz "GNU" SEPARATOR \
116 .long 0xc0000000 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \
117 .long 4 SEPARATOR \
118 .long GNU_PROPERTY SEPARATOR \
119 .long 0 SEPARATOR \
120 .popsection SEPARATOR
121#endif
122#endif
123#if !defined(AARCH64_BTI)
124#define AARCH64_BTI
125#endif
126
127#if !defined(__aarch64__)
128#ifdef __ARM_FEATURE_PAC_DEFAULT
129 .eabi_attribute Tag_PAC_extension, 2
130 .eabi_attribute Tag_PACRET_use, 1
131#endif
132#ifdef __ARM_FEATURE_BTI_DEFAULT
133 .eabi_attribute Tag_BTI_extension, 1
134 .eabi_attribute Tag_BTI_use, 1
135#endif
136#endif
137
138#define GLUE2(a, b) a ## b
139#define GLUE(a, b) GLUE2(a, b)
140#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
141
142#if defined(__APPLE__)
143
144#if defined(__aarch64__) || defined(__arm64__) || defined(__arm64e__)
145#define _LIBUNWIND_TRACE_RET_INJECT 1
146#endif
147
148#define SYMBOL_IS_FUNC(name)
149#define HIDDEN_SYMBOL(name) .private_extern name
150#if defined(_LIBUNWIND_HIDE_SYMBOLS)
151#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
152#else
153#define EXPORT_SYMBOL(name)
154#endif
155#define WEAK_ALIAS(name, aliasname) \
156 .globl SYMBOL_NAME(aliasname) SEPARATOR \
157 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
158 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
159
160#define NO_EXEC_STACK_DIRECTIVE
161
162#elif defined(__ELF__)
163
164#if defined(__arm__)
165#define SYMBOL_IS_FUNC(name) .type name,%function
166#else
167#define SYMBOL_IS_FUNC(name) .type name,@function
168#endif
169#define HIDDEN_SYMBOL(name) .hidden name
170#if defined(_LIBUNWIND_HIDE_SYMBOLS)
171#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
172#else
173#define EXPORT_SYMBOL(name)
174#endif
175#define WEAK_SYMBOL(name) .weak name
176
177#if defined(__hexagon__)
178#define WEAK_ALIAS(name, aliasname) \
179 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
180 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
181 .equiv SYMBOL_NAME(aliasname), SYMBOL_NAME(name)
182#else
183#define WEAK_ALIAS(name, aliasname) \
184 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
185 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
186 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
187#endif
188
189#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
190 defined(__linux__)
191#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
192#else
193#define NO_EXEC_STACK_DIRECTIVE
194#endif
195
196#elif defined(_WIN32)
197
198#define SYMBOL_IS_FUNC(name) \
199 .def name SEPARATOR \
200 .scl 2 SEPARATOR \
201 .type 32 SEPARATOR \
202 .endef
203#define EXPORT_SYMBOL2(name) \
204 .section .drectve,"yn" SEPARATOR \
205 .ascii "-export:", #name, "\0" SEPARATOR \
206 .text
207#if defined(_LIBUNWIND_HIDE_SYMBOLS)
208#define EXPORT_SYMBOL(name)
209#else
210#define EXPORT_SYMBOL(name) EXPORT_SYMBOL2(name)
211#endif
212#define HIDDEN_SYMBOL(name)
213
214#if defined(__MINGW32__)
215#define WEAK_ALIAS(name, aliasname) \
216 .globl SYMBOL_NAME(aliasname) SEPARATOR \
217 EXPORT_SYMBOL(aliasname) SEPARATOR \
218 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
219#else
220#define WEAK_ALIAS3(name, aliasname) \
221 .section .drectve,"yn" SEPARATOR \
222 .ascii "-alternatename:", #aliasname, "=", #name, "\0" SEPARATOR \
223 .text
224#define WEAK_ALIAS2(name, aliasname) \
225 WEAK_ALIAS3(name, aliasname)
226#define WEAK_ALIAS(name, aliasname) \
227 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
228 WEAK_ALIAS2(SYMBOL_NAME(name), SYMBOL_NAME(aliasname))
229#endif
230
231#define NO_EXEC_STACK_DIRECTIVE
232
233#elif defined(__sparc__)
234
235#elif defined(_AIX)
236
237#if defined(__powerpc64__)
238#define VBYTE_LEN 8
239#define CSECT_ALIGN 3
240#else
241#define VBYTE_LEN 4
242#define CSECT_ALIGN 2
243#endif
244
245// clang-format off
246#define DEFINE_LIBUNWIND_FUNCTION_AND_WEAK_ALIAS(name, aliasname) \
247 .csect .text[PR], 2 SEPARATOR \
248 .csect .name[PR], 2 SEPARATOR \
249 .globl name[DS] SEPARATOR \
250 .globl .name[PR] SEPARATOR \
251 .align 4 SEPARATOR \
252 .csect name[DS], CSECT_ALIGN SEPARATOR \
253aliasname: \
254 .vbyte VBYTE_LEN, .name[PR] SEPARATOR \
255 .vbyte VBYTE_LEN, TOC[TC0] SEPARATOR \
256 .vbyte VBYTE_LEN, 0 SEPARATOR \
257 .weak aliasname SEPARATOR \
258 .weak .aliasname SEPARATOR \
259 .csect .name[PR], 2 SEPARATOR \
260.aliasname: \
261
262#define WEAK_ALIAS(name, aliasname)
263#define NO_EXEC_STACK_DIRECTIVE
264
265// clang-format on
266#else
267
268#error Unsupported target
269
270#endif
271
272#if defined(_AIX)
273 // clang-format off
274#define DEFINE_LIBUNWIND_FUNCTION(name) \
275 .globl name[DS] SEPARATOR \
276 .globl .name SEPARATOR \
277 .align 4 SEPARATOR \
278 .csect name[DS], CSECT_ALIGN SEPARATOR \
279 .vbyte VBYTE_LEN, .name SEPARATOR \
280 .vbyte VBYTE_LEN, TOC[TC0] SEPARATOR \
281 .vbyte VBYTE_LEN, 0 SEPARATOR \
282 .csect .text[PR], 2 SEPARATOR \
283.name:
284 // clang-format on
285#else
286#define DEFINE_LIBUNWIND_FUNCTION(name) \
287 .globl SYMBOL_NAME(name) SEPARATOR \
288 HIDDEN_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
289 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
290 PPC64_OPD1 \
291 SYMBOL_NAME(name): \
292 PPC64_OPD2 \
293 AARCH64_BTI
294#endif
295
296#if defined(__arm__)
297#if !defined(__ARM_ARCH)
298#define __ARM_ARCH 4
299#endif
300
301#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
302#define ARM_HAS_BX
303#endif
304
305#ifdef ARM_HAS_BX
306#define JMP(r) bx r
307#else
308#define JMP(r) mov pc, r
309#endif
310#endif /* __arm__ */
311
312#if defined(__powerpc__)
313#define PPC_LEFT_SHIFT(index) << (index)
314#endif
315
316#endif /* UNWIND_ASSEMBLY_H */
317