blob: e38d32336929bcecca85b52eb9d6237603d1f855 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001/* ===-- assembly.h - libUnwind assembler support macros -------------------===
2 *
Chandler Carruth61860a52019-01-19 10:56:40 +00003 * 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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006 *
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
gejin7f493162021-08-26 16:20:38 +080018#if (defined(__i386__) || defined(__x86_64__)) && defined(__linux__)
19#include <cet.h>
20#define _LIBUNWIND_CET_ENDBR _CET_ENDBR
21#else
22#define _LIBUNWIND_CET_ENDBR
23#endif
24
Martin Storsjo8338b0a2018-01-02 22:11:30 +000025#if defined(__powerpc64__)
26#define SEPARATOR ;
Martin Storsjo6518fcb2018-01-16 20:54:10 +000027#define PPC64_OFFS_SRR0 0
28#define PPC64_OFFS_CR 272
29#define PPC64_OFFS_XER 280
30#define PPC64_OFFS_LR 288
31#define PPC64_OFFS_CTR 296
32#define PPC64_OFFS_VRSAVE 304
33#define PPC64_OFFS_FP 312
34#define PPC64_OFFS_V 824
Fangrui Song1b5cb742020-04-09 14:09:43 -070035#elif defined(__APPLE__) && defined(__aarch64__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000036#define SEPARATOR %%
Kamlesh Kumarf6ac3de2021-03-02 06:57:54 +053037#elif defined(__riscv)
38# define RISCV_ISIZE (__riscv_xlen / 8)
39# define RISCV_FOFFSET (RISCV_ISIZE * 32)
40# if defined(__riscv_flen)
41# define RISCV_FSIZE (__riscv_flen / 8)
42# endif
43
44# if __riscv_xlen == 64
45# define ILOAD ld
46# define ISTORE sd
47# elif __riscv_xlen == 32
48# define ILOAD lw
49# define ISTORE sw
50# else
51# error "Unsupported __riscv_xlen"
52# endif
53
54# if defined(__riscv_flen)
55# if __riscv_flen == 64
56# define FLOAD fld
57# define FSTORE fsd
58# elif __riscv_flen == 32
59# define FLOAD flw
60# define FSTORE fsw
61# else
62# error "Unsupported __riscv_flen"
63# endif
64# endif
65# define SEPARATOR ;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000066#else
67#define SEPARATOR ;
68#endif
69
Martin Storsjo8a6fc692019-05-16 06:49:13 +000070#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
71#define PPC64_OPD1 .section .opd,"aw",@progbits SEPARATOR
72#define PPC64_OPD2 SEPARATOR \
73 .p2align 3 SEPARATOR \
74 .quad .Lfunc_begin0 SEPARATOR \
75 .quad .TOC.@tocbase SEPARATOR \
76 .quad 0 SEPARATOR \
77 .text SEPARATOR \
78.Lfunc_begin0:
79#else
80#define PPC64_OPD1
81#define PPC64_OPD2
82#endif
83
Daniel Kissb4416852020-09-29 15:50:19 +020084#if defined(__ARM_FEATURE_BTI_DEFAULT)
85 .pushsection ".note.gnu.property", "a" SEPARATOR \
86 .balign 8 SEPARATOR \
87 .long 4 SEPARATOR \
88 .long 0x10 SEPARATOR \
89 .long 0x5 SEPARATOR \
90 .asciz "GNU" SEPARATOR \
91 .long 0xc0000000 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \
92 .long 4 SEPARATOR \
93 .long 3 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_BTI AND */ \
94 /* GNU_PROPERTY_AARCH64_FEATURE_1_PAC */ \
95 .long 0 SEPARATOR \
96 .popsection SEPARATOR
97#define AARCH64_BTI bti c
98#else
99#define AARCH64_BTI
100#endif
101
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102#define GLUE2(a, b) a ## b
103#define GLUE(a, b) GLUE2(a, b)
104#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
105
106#if defined(__APPLE__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000107
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000108#define SYMBOL_IS_FUNC(name)
Martin Storsjo6038ed32017-10-22 19:39:26 +0000109#define HIDDEN_SYMBOL(name) .private_extern name
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800110#if defined(_LIBUNWIND_HIDE_SYMBOLS)
111#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
112#else
113#define EXPORT_SYMBOL(name)
114#endif
Petr Hosek9bbfad52019-04-03 21:50:03 +0000115#define WEAK_ALIAS(name, aliasname) \
Petr Hosek2ec9ffc2019-04-04 03:36:35 +0000116 .globl SYMBOL_NAME(aliasname) SEPARATOR \
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800117 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Petr Hosek9bbfad52019-04-03 21:50:03 +0000118 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
119
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000120#define NO_EXEC_STACK_DIRECTIVE
121
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000122#elif defined(__ELF__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000123
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000124#if defined(__arm__)
125#define SYMBOL_IS_FUNC(name) .type name,%function
126#else
127#define SYMBOL_IS_FUNC(name) .type name,@function
128#endif
Martin Storsjo6038ed32017-10-22 19:39:26 +0000129#define HIDDEN_SYMBOL(name) .hidden name
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800130#if defined(_LIBUNWIND_HIDE_SYMBOLS)
131#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
132#else
133#define EXPORT_SYMBOL(name)
134#endif
Petr Hosek9bbfad52019-04-03 21:50:03 +0000135#define WEAK_SYMBOL(name) .weak name
Brian Cainb432c472020-04-09 00:14:02 -0500136
137#if defined(__hexagon__)
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800138#define WEAK_ALIAS(name, aliasname) \
139 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
140 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Brian Cainb432c472020-04-09 00:14:02 -0500141 .equiv SYMBOL_NAME(aliasname), SYMBOL_NAME(name)
142#else
Petr Hosek9bbfad52019-04-03 21:50:03 +0000143#define WEAK_ALIAS(name, aliasname) \
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800144 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
145 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Petr Hosek9bbfad52019-04-03 21:50:03 +0000146 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
Brian Cainb432c472020-04-09 00:14:02 -0500147#endif
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000148
Manoj Guptad8b79152017-05-16 20:18:57 +0000149#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
150 defined(__linux__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000151#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000152#else
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000153#define NO_EXEC_STACK_DIRECTIVE
154#endif
155
Martin Storsjo6038ed32017-10-22 19:39:26 +0000156#elif defined(_WIN32)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000157
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000158#define SYMBOL_IS_FUNC(name) \
159 .def name SEPARATOR \
160 .scl 2 SEPARATOR \
161 .type 32 SEPARATOR \
162 .endef
Petr Hosek9bbfad52019-04-03 21:50:03 +0000163#define EXPORT_SYMBOL2(name) \
164 .section .drectve,"yn" SEPARATOR \
165 .ascii "-export:", #name, "\0" SEPARATOR \
Charles Davisc5094702018-08-31 18:11:48 +0000166 .text
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800167#if defined(_LIBUNWIND_HIDE_SYMBOLS)
Martin Storsjocf47ba62018-12-11 07:34:14 +0000168#define EXPORT_SYMBOL(name)
169#else
Charles Davisc5094702018-08-31 18:11:48 +0000170#define EXPORT_SYMBOL(name) EXPORT_SYMBOL2(name)
Martin Storsjocf47ba62018-12-11 07:34:14 +0000171#endif
Martin Storsjo6038ed32017-10-22 19:39:26 +0000172#define HIDDEN_SYMBOL(name)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000173
Petr Hosek9bbfad52019-04-03 21:50:03 +0000174#if defined(__MINGW32__)
175#define WEAK_ALIAS(name, aliasname) \
176 .globl SYMBOL_NAME(aliasname) SEPARATOR \
177 EXPORT_SYMBOL(aliasname) SEPARATOR \
178 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
179#else
180#define WEAK_ALIAS3(name, aliasname) \
181 .section .drectve,"yn" SEPARATOR \
182 .ascii "-alternatename:", #aliasname, "=", #name, "\0" SEPARATOR \
183 .text
184#define WEAK_ALIAS2(name, aliasname) \
185 WEAK_ALIAS3(name, aliasname)
186#define WEAK_ALIAS(name, aliasname) \
187 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
188 WEAK_ALIAS2(SYMBOL_NAME(name), SYMBOL_NAME(aliasname))
189#endif
190
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000191#define NO_EXEC_STACK_DIRECTIVE
192
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000193#elif defined(__sparc__)
194
Martin Storsjo6038ed32017-10-22 19:39:26 +0000195#else
196
197#error Unsupported target
198
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199#endif
200
Petr Hosek9bbfad52019-04-03 21:50:03 +0000201#define DEFINE_LIBUNWIND_FUNCTION(name) \
202 .globl SYMBOL_NAME(name) SEPARATOR \
203 HIDDEN_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
204 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
Martin Storsjo8a6fc692019-05-16 06:49:13 +0000205 PPC64_OPD1 \
206 SYMBOL_NAME(name): \
Daniel Kissb4416852020-09-29 15:50:19 +0200207 PPC64_OPD2 \
208 AARCH64_BTI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000209
210#if defined(__arm__)
211#if !defined(__ARM_ARCH)
212#define __ARM_ARCH 4
213#endif
214
215#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
216#define ARM_HAS_BX
217#endif
218
219#ifdef ARM_HAS_BX
220#define JMP(r) bx r
221#else
222#define JMP(r) mov pc, r
223#endif
224#endif /* __arm__ */
225
Xing Xued6f21e02021-05-06 14:33:38 -0400226#if defined(__ppc__) || defined(__powerpc64__)
227#define PPC_LEFT_SHIFT(index) << (index)
228#endif
229
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000230#endif /* UNWIND_ASSEMBLY_H */