Debian Codegraph @

libgcc/config/aarch64/aarch64-unwind.h

raw
1 /* Copyright (C) 2017-2025 Free Software Foundation, Inc.
2 Contributed by ARM Ltd.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #if !defined (AARCH64_UNWIND_H) && !defined (__ILP32__)
26 #define AARCH64_UNWIND_H
27
28 #include "config/aarch64/aarch64-unwind-def.h"
29
30 #include "ansidecl.h"
31 #include <stdbool.h>
32
33 #define AARCH64_DWARF_REGNUM_RA_STATE 34
34 #define AARCH64_DWARF_RA_STATE_MASK 0x1
35
36 /* The diversifiers used to sign a function's return address. */
37 typedef enum
38 {
39 aarch64_ra_no_signing = 0x0,
40 aarch64_ra_signing_sp = 0x1,
41 } __attribute__((packed)) aarch64_ra_signing_method_t;
42
43 #define MD_ARCH_EXTENSION_CIE_AUG_HANDLER(fs, aug) \
44 aarch64_cie_aug_handler (fs, aug)
45
46 #define MD_ARCH_EXTENSION_FRAME_INIT(context, fs) \
47 aarch64_arch_extension_frame_init (context, fs)
48
49 #define MD_DEMANGLE_RETURN_ADDR(context, fs, addr) \
50 aarch64_demangle_return_addr (context, fs, addr)
51
52 #define MD_FRAME_LOCAL_REGISTER_P(reg) \
53 aarch64_frame_local_register (reg)
54
55 static inline aarch64_ra_signing_method_t
56 aarch64_context_ra_state_get (struct _Unwind_Context *context)
57 {
58 const int index = AARCH64_DWARF_REGNUM_RA_STATE;
59 return _Unwind_GetGR (context, index) & AARCH64_DWARF_RA_STATE_MASK;
60 }
61
62 static inline aarch64_ra_signing_method_t
63 aarch64_fs_ra_state_get (_Unwind_FrameState const *fs)
64 {
65 const int index = AARCH64_DWARF_REGNUM_RA_STATE;
66 return fs->regs.reg[index].loc.offset & AARCH64_DWARF_RA_STATE_MASK;
67 }
68
69 static inline void
70 aarch64_fs_ra_state_set (_Unwind_FrameState *fs,
71 aarch64_ra_signing_method_t signing_method)
72 {
73 fs->regs.reg[AARCH64_DWARF_REGNUM_RA_STATE].loc.offset = signing_method;
74 }
75
76 static inline void
77 aarch64_fs_ra_state_toggle (_Unwind_FrameState *fs)
78 {
79 /* /!\ Mixing DW_CFA_val_expression with DW_CFA_AARCH64_negate_ra_state will
80 result in undefined behavior (likely an unwinding failure), as the
81 chronology of the DWARF directives will be broken. */
82 gcc_assert (fs->regs.how[AARCH64_DWARF_REGNUM_RA_STATE] == REG_ARCHEXT);
83
84 aarch64_ra_signing_method_t signing_method = aarch64_fs_ra_state_get (fs);
85 gcc_assert (signing_method == aarch64_ra_no_signing
86 || signing_method == aarch64_ra_signing_sp);
87 aarch64_fs_ra_state_set (fs, (signing_method == aarch64_ra_no_signing)
88 ? aarch64_ra_signing_sp
89 : aarch64_ra_no_signing);
90 }
91
92 /* CIE handler for custom augmentation string. */
93 static inline bool
94 aarch64_cie_aug_handler (_Unwind_FrameState *fs, unsigned char aug)
95 {
96 /* AArch64 B-key pointer authentication. */
97 if (aug == 'B')
98 {
99 fs->regs.arch_fs.signing_key = AARCH64_PAUTH_KEY_B;
100 return true;
101 }
102 return false;
103 }
104
105 /* At the entrance of a new frame, some cached information from the CIE/FDE,
106 and registers values related to architectural extensions require a default
107 initialization.
108 If any of those values related to architecture extensions had to be saved
109 for the next frame, it should be done via the architecture extensions handler
110 MD_FROB_UPDATE_CONTEXT in uw_update_context_1 (libgcc/unwind-dw2.c). */
111 static inline void
112 aarch64_arch_extension_frame_init (struct _Unwind_Context *context ATTRIBUTE_UNUSED,
113 _Unwind_FrameState *fs)
114 {
115 /* By default, DW_CFA_AARCH64_negate_ra_state assumes key A is being used
116 for signing. This can be overridden by adding 'B' to the augmentation
117 string. */
118 fs->regs.arch_fs.signing_key = AARCH64_PAUTH_KEY_A;
119
120 /* All registers are initially in state REG_UNSAVED, which indicates that
121 they inherit register values from the previous frame. However, the
122 return address starts every frame in the "unsigned" state. It also
123 starts every frame in a state that supports the original toggle-based
124 DW_CFA_AARCH64_negate_ra_state method of controlling RA signing. */
125 fs->regs.how[AARCH64_DWARF_REGNUM_RA_STATE] = REG_ARCHEXT;
126 aarch64_fs_ra_state_set (fs, aarch64_ra_no_signing);
127 }
128
129 /* Before copying the current context to the target context, check whether
130 the register is local to this context and should not be forwarded. */
131 static inline bool
132 aarch64_frame_local_register(long reg)
133 {
134 return (reg == AARCH64_DWARF_REGNUM_RA_STATE);
135 }
136
137 /* Do AArch64 private extraction on ADDR_WORD based on context info CONTEXT and
138 unwind frame info FS. If ADDR_WORD is signed, we do address authentication
139 on it using CFA of current frame.
140
141 Note: when DW_CFA_val_expression is used, FS only records the location of the
142 associated CFI program, rather than the value of the expression itself.
143 The CFI program is executed by uw_update_context when updating the context,
144 so the value of the expression must be taken from CONTEXT rather than FS. */
145 static inline void *
146 aarch64_demangle_return_addr (struct _Unwind_Context *context,
147 _Unwind_FrameState *fs,
148 _Unwind_Word addr_word)
149 {
150 void *addr = (void *)addr_word;
151 const int reg = AARCH64_DWARF_REGNUM_RA_STATE;
152
153 /* In libgcc, REG_ARCHEXT means that the RA state register was set by an
154 AArch64 DWARF instruction and contains a valid value, or is used to
155 describe the initial state set in aarch64_arch_extension_frame_init.
156 Return-address signing state is normally toggled by DW_CFA_AARCH64_negate
157 _ra_state (also knwon by its alias as DW_CFA_GNU_window_save).
158 However, RA state register can be set directly via DW_CFA_val_expression
159 too. GCC does not generate such CFI but some other compilers reportedly
160 do (see PR104689 for more details).
161 Any other value than REG_ARCHEXT should be interpreted as if the RA state
162 register is set by another DWARF instruction, and the value is fetchable
163 via _Unwind_GetGR. */
164 aarch64_ra_signing_method_t signing_method = aarch64_ra_no_signing;
165 if (fs->regs.how[reg] == REG_ARCHEXT)
166 signing_method = aarch64_fs_ra_state_get (fs);
167 else if (fs->regs.how[reg] != REG_UNSAVED)
168 signing_method = aarch64_context_ra_state_get (context);
169
170 if (signing_method == aarch64_ra_signing_sp)
171 {
172 _Unwind_Word salt = (_Unwind_Word) context->cfa;
173 if (fs->regs.arch_fs.signing_key == AARCH64_PAUTH_KEY_B)
174 return __builtin_aarch64_autib1716 (addr, salt);
175 return __builtin_aarch64_autia1716 (addr, salt);
176 }
177
178 return addr;
179 }
180
181 /* GCS enable flag for chkfeat instruction. */
182 #define _CHKFEAT_GCS 1
183 /* SME runtime function local to libgcc, streaming compatible
184 and preserves more registers than the base PCS requires, but
185 we don't rely on that here. */
186 __attribute__ ((visibility ("hidden")))
187 void __libgcc_arm_za_disable (void);
188
189 /* Disable the SME ZA state in case an unwound frame used the ZA
190 lazy saving scheme. And unwind the GCS for EH. */
191 #undef _Unwind_Frames_Extra
192 #define _Unwind_Frames_Extra(x) \
193 do \
194 { \
195 __libgcc_arm_za_disable (); \
196 if (__builtin_aarch64_chkfeat (_CHKFEAT_GCS) == 0) \
197 { \
198 for (_Unwind_Word n = (x); n != 0; n--) \
199 __builtin_aarch64_gcspopm (); \
200 } \
201 } \
202 while (0)
203
204 /* On signal entry the OS places a token on the GCS that can be used to
205 verify the integrity of the GCS pointer on signal return. It also
206 places the signal handler return address (the restorer that calls the
207 signal return syscall) on the GCS so the handler can return.
208 Because of this token, each stack frame visited during unwinding has
209 exactly one corresponding entry on the GCS, so the frame count is
210 the number of entries that will have to be popped at EH return time.
211
212 Note: This depends on the GCS signal ABI of the OS.
213
214 When unwinding across a stack frame for each frame the corresponding
215 entry is checked on the GCS against the computed return address from
216 the normal stack. If they don't match then _URC_FATAL_PHASE2_ERROR
217 is returned. This check is omitted if
218
219 1. GCS is disabled. Note: asynchronous GCS disable is supported here
220 if GCSPR and the GCS remains readable.
221 2. Non-catchable exception where exception_class == 0. Note: the
222 pthread cancellation implementation in glibc sets exception_class
223 to 0 when the unwinder is used for cancellation cleanup handling,
224 so this allows the GCS to get out of sync during cancellation.
225 This weakens security but avoids an ABI break in glibc.
226 3. Zero return address which marks the outermost stack frame.
227 4. Signal stack frame, the GCS entry is an OS specific token then
228 with the top bit set.
229 */
230 #undef _Unwind_Frames_Increment
231 #define _Unwind_Frames_Increment(exc, context, frames) \
232 do \
233 { \
234 frames++; \
235 if (__builtin_aarch64_chkfeat (_CHKFEAT_GCS) != 0 \
236 || exc->exception_class == 0 \
237 || _Unwind_GetIP (context) == 0) \
238 break; \
239 const _Unwind_Word *gcs = __builtin_aarch64_gcspr (); \
240 if (_Unwind_IsSignalFrame (context)) \
241 { \
242 if (gcs[frames] >> 63 == 0) \
243 return _URC_FATAL_PHASE2_ERROR; \
244 } \
245 else \
246 { \
247 if (gcs[frames] != _Unwind_GetIP (context)) \
248 return _URC_FATAL_PHASE2_ERROR; \
249 } \
250 } \
251 while (0)
252
253 #endif /* defined AARCH64_UNWIND_H && defined __ILP32__ */