blob: f5ca35c0c1893d01a145174722806a24997a3a88 [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
Martin Storsjo8338b0a2018-01-02 22:11:30 +000018#if defined(__powerpc64__)
19#define SEPARATOR ;
Martin Storsjo6518fcb2018-01-16 20:54:10 +000020#define PPC64_OFFS_SRR0 0
21#define PPC64_OFFS_CR 272
22#define PPC64_OFFS_XER 280
23#define PPC64_OFFS_LR 288
24#define PPC64_OFFS_CTR 296
25#define PPC64_OFFS_VRSAVE 304
26#define PPC64_OFFS_FP 312
27#define PPC64_OFFS_V 824
Fangrui Song1b5cb742020-04-09 14:09:43 -070028#elif defined(__APPLE__) && defined(__aarch64__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000029#define SEPARATOR %%
Kamlesh Kumarf6ac3de2021-03-02 06:57:54 +053030#elif defined(__riscv)
31# define RISCV_ISIZE (__riscv_xlen / 8)
32# define RISCV_FOFFSET (RISCV_ISIZE * 32)
33# if defined(__riscv_flen)
34# define RISCV_FSIZE (__riscv_flen / 8)
35# endif
36
37# if __riscv_xlen == 64
38# define ILOAD ld
39# define ISTORE sd
40# elif __riscv_xlen == 32
41# define ILOAD lw
42# define ISTORE sw
43# else
44# error "Unsupported __riscv_xlen"
45# endif
46
47# if defined(__riscv_flen)
48# if __riscv_flen == 64
49# define FLOAD fld
50# define FSTORE fsd
51# elif __riscv_flen == 32
52# define FLOAD flw
53# define FSTORE fsw
54# else
55# error "Unsupported __riscv_flen"
56# endif
57# endif
58# define SEPARATOR ;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000059#else
60#define SEPARATOR ;
61#endif
62
Martin Storsjo8a6fc692019-05-16 06:49:13 +000063#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
64#define PPC64_OPD1 .section .opd,"aw",@progbits SEPARATOR
65#define PPC64_OPD2 SEPARATOR \
66 .p2align 3 SEPARATOR \
67 .quad .Lfunc_begin0 SEPARATOR \
68 .quad .TOC.@tocbase SEPARATOR \
69 .quad 0 SEPARATOR \
70 .text SEPARATOR \
71.Lfunc_begin0:
72#else
73#define PPC64_OPD1
74#define PPC64_OPD2
75#endif
76
Daniel Kissb4416852020-09-29 15:50:19 +020077#if defined(__ARM_FEATURE_BTI_DEFAULT)
78 .pushsection ".note.gnu.property", "a" SEPARATOR \
79 .balign 8 SEPARATOR \
80 .long 4 SEPARATOR \
81 .long 0x10 SEPARATOR \
82 .long 0x5 SEPARATOR \
83 .asciz "GNU" SEPARATOR \
84 .long 0xc0000000 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \
85 .long 4 SEPARATOR \
86 .long 3 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_BTI AND */ \
87 /* GNU_PROPERTY_AARCH64_FEATURE_1_PAC */ \
88 .long 0 SEPARATOR \
89 .popsection SEPARATOR
90#define AARCH64_BTI bti c
91#else
92#define AARCH64_BTI
93#endif
94
Saleem Abdulrasool17552662015-04-24 19:39:17 +000095#define GLUE2(a, b) a ## b
96#define GLUE(a, b) GLUE2(a, b)
97#define SYMBOL_NAME(name) GLUE(__USER_LABEL_PREFIX__, name)
98
99#if defined(__APPLE__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000100
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000101#define SYMBOL_IS_FUNC(name)
Martin Storsjo6038ed32017-10-22 19:39:26 +0000102#define HIDDEN_SYMBOL(name) .private_extern name
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800103#if defined(_LIBUNWIND_HIDE_SYMBOLS)
104#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
105#else
106#define EXPORT_SYMBOL(name)
107#endif
Petr Hosek9bbfad52019-04-03 21:50:03 +0000108#define WEAK_ALIAS(name, aliasname) \
Petr Hosek2ec9ffc2019-04-04 03:36:35 +0000109 .globl SYMBOL_NAME(aliasname) SEPARATOR \
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800110 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Petr Hosek9bbfad52019-04-03 21:50:03 +0000111 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
112
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000113#define NO_EXEC_STACK_DIRECTIVE
114
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000115#elif defined(__ELF__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000116
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000117#if defined(__arm__)
118#define SYMBOL_IS_FUNC(name) .type name,%function
119#else
120#define SYMBOL_IS_FUNC(name) .type name,@function
121#endif
Martin Storsjo6038ed32017-10-22 19:39:26 +0000122#define HIDDEN_SYMBOL(name) .hidden name
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800123#if defined(_LIBUNWIND_HIDE_SYMBOLS)
124#define EXPORT_SYMBOL(name) HIDDEN_SYMBOL(name)
125#else
126#define EXPORT_SYMBOL(name)
127#endif
Petr Hosek9bbfad52019-04-03 21:50:03 +0000128#define WEAK_SYMBOL(name) .weak name
Brian Cainb432c472020-04-09 00:14:02 -0500129
130#if defined(__hexagon__)
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800131#define WEAK_ALIAS(name, aliasname) \
132 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
133 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Brian Cainb432c472020-04-09 00:14:02 -0500134 .equiv SYMBOL_NAME(aliasname), SYMBOL_NAME(name)
135#else
Petr Hosek9bbfad52019-04-03 21:50:03 +0000136#define WEAK_ALIAS(name, aliasname) \
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800137 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
138 WEAK_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
Petr Hosek9bbfad52019-04-03 21:50:03 +0000139 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
Brian Cainb432c472020-04-09 00:14:02 -0500140#endif
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000141
Manoj Guptad8b79152017-05-16 20:18:57 +0000142#if defined(__GNU__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
143 defined(__linux__)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000144#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000145#else
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000146#define NO_EXEC_STACK_DIRECTIVE
147#endif
148
Martin Storsjo6038ed32017-10-22 19:39:26 +0000149#elif defined(_WIN32)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000150
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000151#define SYMBOL_IS_FUNC(name) \
152 .def name SEPARATOR \
153 .scl 2 SEPARATOR \
154 .type 32 SEPARATOR \
155 .endef
Petr Hosek9bbfad52019-04-03 21:50:03 +0000156#define EXPORT_SYMBOL2(name) \
157 .section .drectve,"yn" SEPARATOR \
158 .ascii "-export:", #name, "\0" SEPARATOR \
Charles Davisc5094702018-08-31 18:11:48 +0000159 .text
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800160#if defined(_LIBUNWIND_HIDE_SYMBOLS)
Martin Storsjocf47ba62018-12-11 07:34:14 +0000161#define EXPORT_SYMBOL(name)
162#else
Charles Davisc5094702018-08-31 18:11:48 +0000163#define EXPORT_SYMBOL(name) EXPORT_SYMBOL2(name)
Martin Storsjocf47ba62018-12-11 07:34:14 +0000164#endif
Martin Storsjo6038ed32017-10-22 19:39:26 +0000165#define HIDDEN_SYMBOL(name)
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000166
Petr Hosek9bbfad52019-04-03 21:50:03 +0000167#if defined(__MINGW32__)
168#define WEAK_ALIAS(name, aliasname) \
169 .globl SYMBOL_NAME(aliasname) SEPARATOR \
170 EXPORT_SYMBOL(aliasname) SEPARATOR \
171 SYMBOL_NAME(aliasname) = SYMBOL_NAME(name)
172#else
173#define WEAK_ALIAS3(name, aliasname) \
174 .section .drectve,"yn" SEPARATOR \
175 .ascii "-alternatename:", #aliasname, "=", #name, "\0" SEPARATOR \
176 .text
177#define WEAK_ALIAS2(name, aliasname) \
178 WEAK_ALIAS3(name, aliasname)
179#define WEAK_ALIAS(name, aliasname) \
180 EXPORT_SYMBOL(SYMBOL_NAME(aliasname)) SEPARATOR \
181 WEAK_ALIAS2(SYMBOL_NAME(name), SYMBOL_NAME(aliasname))
182#endif
183
Saleem Abdulrasool85e5b232016-08-05 21:35:28 +0000184#define NO_EXEC_STACK_DIRECTIVE
185
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000186#elif defined(__sparc__)
187
Martin Storsjo6038ed32017-10-22 19:39:26 +0000188#else
189
190#error Unsupported target
191
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000192#endif
193
Petr Hosek9bbfad52019-04-03 21:50:03 +0000194#define DEFINE_LIBUNWIND_FUNCTION(name) \
195 .globl SYMBOL_NAME(name) SEPARATOR \
196 HIDDEN_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
197 SYMBOL_IS_FUNC(SYMBOL_NAME(name)) SEPARATOR \
Martin Storsjo8a6fc692019-05-16 06:49:13 +0000198 PPC64_OPD1 \
199 SYMBOL_NAME(name): \
Daniel Kissb4416852020-09-29 15:50:19 +0200200 PPC64_OPD2 \
201 AARCH64_BTI
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000202
203#if defined(__arm__)
204#if !defined(__ARM_ARCH)
205#define __ARM_ARCH 4
206#endif
207
208#if defined(__ARM_ARCH_4T__) || __ARM_ARCH >= 5
209#define ARM_HAS_BX
210#endif
211
212#ifdef ARM_HAS_BX
213#define JMP(r) bx r
214#else
215#define JMP(r) mov pc, r
216#endif
217#endif /* __arm__ */
218
219#endif /* UNWIND_ASSEMBLY_H */