xref: /freebsd/contrib/llvm-project/libunwind/src/assembly.h (revision d56accc7c3dcc897489b6a07834763a03b9f3d68)
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(__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 
25 #if defined(__powerpc64__)
26 #define SEPARATOR ;
27 #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
35 #elif defined(__APPLE__) && defined(__aarch64__)
36 #define SEPARATOR %%
37 #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 ;
66 #else
67 #define SEPARATOR ;
68 #endif
69 
70 #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 
84 #if defined(__aarch64__) && 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 
102 #if !defined(__aarch64__)
103 #ifdef __ARM_FEATURE_PAC_DEFAULT
104   .eabi_attribute Tag_PAC_extension, 2
105   .eabi_attribute Tag_PACRET_use, 1
106 #endif
107 #ifdef __ARM_FEATURE_BTI_DEFAULT
108   .eabi_attribute Tag_BTI_extension, 1
109   .eabi_attribute Tag_BTI_use, 1
110 #endif
111 #endif
112 
113 #define GLUE2(a, b) a ## b
114 #define GLUE(a, b) GLUE2(a, b)
115 #define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
116 
117 #if defined(__APPLE__)
118 
119 #define SYMBOL_IS_FUNC(name)
120 #define HIDDEN_SYMBOL(name) .private_extern name
121 #if defined(_LIBUNWIND_HIDE_SYMBOLS)
122 #define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
123 #else
124 #define EXPORT_SYMBOL(name)
125 #endif
126 #define WEAK_ALIAS(name, aliasname)                                            \
127   .globl SYMBOL_NAME(aliasname) SEPARATOR                                      \
128   EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                              \
129   SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
130 
131 #define NO_EXEC_STACK_DIRECTIVE
132 
133 #elif defined(__ELF__)
134 
135 #if defined(__arm__)
136 #define SYMBOL_IS_FUNC(name) .type name,%function
137 #else
138 #define SYMBOL_IS_FUNC(name) .type name,@function
139 #endif
140 #define HIDDEN_SYMBOL(name) .hidden name
141 #if defined(_LIBUNWIND_HIDE_SYMBOLS)
142 #define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
143 #else
144 #define EXPORT_SYMBOL(name)
145 #endif
146 #define WEAK_SYMBOL(name) .weak name
147 
148 #if defined(__hexagon__)
149 #define WEAK_ALIAS(name, aliasname)                                            \
150   EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                              \
151   WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                                \
152   .equiv SYMBOL_NAME(aliasname), SYMBOL_NAME(name)
153 #else
154 #define WEAK_ALIAS(name, aliasname)                                            \
155   EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                              \
156   WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                                \
157   SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
158 #endif
159 
160 #if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
161     defined(__linux__)
162 #define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
163 #else
164 #define NO_EXEC_STACK_DIRECTIVE
165 #endif
166 
167 #elif defined(_WIN32)
168 
169 #define SYMBOL_IS_FUNC(name)                                                   \
170   .def name SEPARATOR                                                          \
171     .scl 2 SEPARATOR                                                           \
172     .type 32 SEPARATOR                                                         \
173   .endef
174 #define EXPORT_SYMBOL2(name)                                                   \
175   .section .drectve,"yn" SEPARATOR                                             \
176   .ascii "-export:", #name, "\0" SEPARATOR                                     \
177   .text
178 #if defined(_LIBUNWIND_HIDE_SYMBOLS)
179 #define EXPORT_SYMBOL(name)
180 #else
181 #define EXPORT_SYMBOL(name) EXPORT_SYMBOL2(name)
182 #endif
183 #define HIDDEN_SYMBOL(name)
184 
185 #if defined(__MINGW32__)
186 #define WEAK_ALIAS(name, aliasname)                                            \
187   .globl SYMBOL_NAME(aliasname) SEPARATOR                                      \
188   EXPORT_SYMBOL(aliasname) SEPARATOR                                           \
189   SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
190 #else
191 #define WEAK_ALIAS3(name, aliasname)                                           \
192   .section .drectve,"yn" SEPARATOR                                             \
193   .ascii "-alternatename:", #aliasname, "=", #name, "\0" SEPARATOR             \
194   .text
195 #define WEAK_ALIAS2(name, aliasname)                                           \
196   WEAK_ALIAS3(name, aliasname)
197 #define WEAK_ALIAS(name, aliasname)                                            \
198   EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR                              \
199   WEAK_ALIAS2(SYMBOL_NAME(name), SYMBOL_NAME(aliasname))
200 #endif
201 
202 #define NO_EXEC_STACK_DIRECTIVE
203 
204 #elif defined(__sparc__)
205 
206 #else
207 
208 #error Unsupported target
209 
210 #endif
211 
212 #define DEFINE_LIBUNWIND_FUNCTION(name)                                        \
213   .globl SYMBOL_NAME(name) SEPARATOR                                           \
214   HIDDEN_SYMBOL(SYMBOL_NAME(name)) SEPARATOR                                   \
215   SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR                                  \
216   PPC64_OPD1                                                                   \
217   SYMBOL_NAME(name):                                                           \
218   PPC64_OPD2                                                                   \
219   AARCH64_BTI
220 
221 #if defined(__arm__)
222 #if !defined(__ARM_ARCH)
223 #define __ARM_ARCH 4
224 #endif
225 
226 #if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
227 #define ARM_HAS_BX
228 #endif
229 
230 #ifdef ARM_HAS_BX
231 #define JMP(r) bx r
232 #else
233 #define JMP(r) mov pc, r
234 #endif
235 #endif /* __arm__ */
236 
237 #if defined(__powerpc__)
238 #define PPC_LEFT_SHIFT(index) << (index)
239 #endif
240 
241 #endif /* UNWIND_ASSEMBLY_H */
242