1 /* asm-common-aarch64.h  -  Common macros for AArch64 assembly
2  *
3  * Copyright (C) 2018 Martin Storsjö <martin@martin.st>
4  *
5  * This file is part of Libgcrypt.
6  *
7  * Libgcrypt is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * Libgcrypt is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef GCRY_ASM_COMMON_AARCH64_H
22 #define GCRY_ASM_COMMON_AARCH64_H
23 
24 #include <config.h>
25 
26 #ifdef HAVE_GCC_ASM_ELF_DIRECTIVES
27 # define ELF(...) __VA_ARGS__
28 #else
29 # define ELF(...) /*_*/
30 #endif
31 
32 #ifdef __APPLE__
33 #define GET_DATA_POINTER(reg, name) \
34 	adrp    reg, name@GOTPAGE ; \
35 	add     reg, reg, name@GOTPAGEOFF ;
36 #elif defined(_WIN32)
37 #define GET_DATA_POINTER(reg, name) \
38 	adrp    reg, name ; \
39 	add     reg, reg, #:lo12:name ;
40 #else
41 #define GET_DATA_POINTER(reg, name) \
42 	adrp    reg, :got:name ; \
43 	ldr     reg, [reg, #:got_lo12:name] ;
44 #endif
45 
46 #ifdef HAVE_GCC_ASM_CFI_DIRECTIVES
47 /* CFI directives to emit DWARF stack unwinding information. */
48 # define CFI_STARTPROC()            .cfi_startproc
49 # define CFI_ENDPROC()              .cfi_endproc
50 # define CFI_REMEMBER_STATE()       .cfi_remember_state
51 # define CFI_RESTORE_STATE()        .cfi_restore_state
52 # define CFI_ADJUST_CFA_OFFSET(off) .cfi_adjust_cfa_offset off
53 # define CFI_REL_OFFSET(reg,off)    .cfi_rel_offset reg, off
54 # define CFI_DEF_CFA_REGISTER(reg)  .cfi_def_cfa_register reg
55 # define CFI_REGISTER(ro,rn)        .cfi_register ro, rn
56 # define CFI_RESTORE(reg)           .cfi_restore reg
57 
58 /* CFA expressions are used for pointing CFA and registers to
59  * SP relative offsets. */
60 # define DW_REGNO_SP 31
61 
62 /* Fixed length encoding used for integers for now. */
63 # define DW_SLEB128_7BIT(value) \
64 	0x00|((value) & 0x7f)
65 # define DW_SLEB128_28BIT(value) \
66 	0x80|((value)&0x7f), \
67 	0x80|(((value)>>7)&0x7f), \
68 	0x80|(((value)>>14)&0x7f), \
69 	0x00|(((value)>>21)&0x7f)
70 
71 # define CFI_CFA_ON_STACK(rsp_offs,cfa_depth) \
72 	.cfi_escape \
73 	  0x0f, /* DW_CFA_def_cfa_expression */ \
74 	    DW_SLEB128_7BIT(11), /* length */ \
75 	  0x8f, /* DW_OP_breg31, rsp + constant */ \
76 	    DW_SLEB128_28BIT(rsp_offs), \
77 	  0x06, /* DW_OP_deref */ \
78 	  0x23, /* DW_OP_plus_constu */ \
79 	    DW_SLEB128_28BIT((cfa_depth)+8)
80 
81 # define CFI_REG_ON_STACK(regno,rsp_offs) \
82 	.cfi_escape \
83 	  0x10, /* DW_CFA_expression */ \
84 	    DW_SLEB128_7BIT(regno), \
85 	    DW_SLEB128_7BIT(5), /* length */ \
86 	  0x8f, /* DW_OP_breg31, rsp + constant */ \
87 	    DW_SLEB128_28BIT(rsp_offs)
88 
89 #else
90 # define CFI_STARTPROC()
91 # define CFI_ENDPROC()
92 # define CFI_REMEMBER_STATE()
93 # define CFI_RESTORE_STATE()
94 # define CFI_ADJUST_CFA_OFFSET(off)
95 # define CFI_REL_OFFSET(reg,off)
96 # define CFI_DEF_CFA_REGISTER(reg)
97 # define CFI_REGISTER(ro,rn)
98 # define CFI_RESTORE(reg)
99 
100 # define CFI_CFA_ON_STACK(rsp_offs,cfa_depth)
101 # define CFI_REG_ON_STACK(reg,rsp_offs)
102 #endif
103 
104 #endif /* GCRY_ASM_COMMON_AARCH64_H */
105