Debian Codegraph @

libgcc/config/i386/gnu-unwind.h

raw
1 /* DWARF2 EH unwinding support for GNU Hurd: x86.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 Contributed by Samuel Thibault <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC 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 General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 /* Do code reading to identify a signal frame, and set the frame
27 state data appropriately. See unwind-dw2.c for the structs. */
28
29 #ifndef inhibit_libc
30
31 #include <signal.h>
32
33 #ifdef __x86_64__
34
35 #define MD_FALLBACK_FRAME_STATE_FOR x86_gnu_fallback_frame_state
36
37 static _Unwind_Reason_Code
38 x86_gnu_fallback_frame_state
39 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
40 {
41 static const unsigned char gnu_sigtramp_code[] =
42 {
43 /* rpc_wait_trampoline: */
44 0x48, 0xc7, 0xc0, 0xe7, 0xff, 0xff, 0xff, /* mov $-25,%rax */
45 0x0f, 0x05, /* syscall */
46 0x49, 0x89, 0x04, 0x24, /* mov %rax,(%r12) */
47 0x48, 0x89, 0xdc, /* mov %rbx,%rsp */
48
49 /* trampoline: */
50 0x5f, /* pop %rdi */
51 0x5e, /* pop %rsi */
52 0x5a, /* pop %rdx */
53 0x48, 0x83, 0xc4, 0x08, /* add $0x8,%rsp */
54 0x41, 0xff, 0xd5, /* call *%r13 */
55
56 /* RA HERE */
57 0x48, 0x8b, 0x7c, 0x24, 0x10, /* mov 0x10(%rsp),%rdi */
58 0xc3, /* ret */
59
60 /* firewall: */
61 0xf4, /* hlt */
62 };
63
64 const size_t gnu_sigtramp_len = sizeof gnu_sigtramp_code;
65 const size_t gnu_sigtramp_tail = 7; /* length of tail after RA */
66
67 struct stack_contents {
68 void *sigreturn_addr;
69 void *sigreturn_returns_here;
70 struct sigcontext *return_scp;
71 } *stack_contents;
72 struct sigcontext *scp;
73 unsigned long usp;
74
75 unsigned char *adjusted_pc = (unsigned char*)(context->ra) +
76 gnu_sigtramp_tail - gnu_sigtramp_len;
77 if (memcmp (adjusted_pc, gnu_sigtramp_code, gnu_sigtramp_len))
78 return _URC_END_OF_STACK;
79
80 stack_contents = context->cfa;
81
82 scp = stack_contents->return_scp;
83 usp = scp->sc_ursp;
84
85 fs->regs.reg[0].loc.offset = (unsigned long)&scp->sc_rax - usp;
86 fs->regs.reg[1].loc.offset = (unsigned long)&scp->sc_rdx - usp;
87 fs->regs.reg[2].loc.offset = (unsigned long)&scp->sc_rcx - usp;
88 fs->regs.reg[3].loc.offset = (unsigned long)&scp->sc_rbx - usp;
89 fs->regs.reg[4].loc.offset = (unsigned long)&scp->sc_rsi - usp;
90 fs->regs.reg[5].loc.offset = (unsigned long)&scp->sc_rdi - usp;
91 fs->regs.reg[6].loc.offset = (unsigned long)&scp->sc_rbp - usp;
92 fs->regs.reg[8].loc.offset = (unsigned long)&scp->sc_r8 - usp;
93 fs->regs.reg[9].loc.offset = (unsigned long)&scp->sc_r9 - usp;
94 fs->regs.reg[10].loc.offset = (unsigned long)&scp->sc_r10 - usp;
95 fs->regs.reg[11].loc.offset = (unsigned long)&scp->sc_r11 - usp;
96 fs->regs.reg[12].loc.offset = (unsigned long)&scp->sc_r12 - usp;
97 fs->regs.reg[13].loc.offset = (unsigned long)&scp->sc_r13 - usp;
98 fs->regs.reg[14].loc.offset = (unsigned long)&scp->sc_r14 - usp;
99 fs->regs.reg[15].loc.offset = (unsigned long)&scp->sc_r15 - usp;
100 fs->regs.reg[16].loc.offset = (unsigned long)&scp->sc_rip - usp;
101
102 /* Register 7 is rsp */
103 fs->regs.cfa_how = CFA_REG_OFFSET;
104 fs->regs.cfa_reg = 7;
105 fs->regs.cfa_offset = usp - (unsigned long) context->cfa;
106
107 fs->regs.how[0] = REG_SAVED_OFFSET;
108 fs->regs.how[1] = REG_SAVED_OFFSET;
109 fs->regs.how[2] = REG_SAVED_OFFSET;
110 fs->regs.how[3] = REG_SAVED_OFFSET;
111 fs->regs.how[4] = REG_SAVED_OFFSET;
112 fs->regs.how[5] = REG_SAVED_OFFSET;
113 fs->regs.how[6] = REG_SAVED_OFFSET;
114 fs->regs.how[8] = REG_SAVED_OFFSET;
115 fs->regs.how[9] = REG_SAVED_OFFSET;
116 fs->regs.how[10] = REG_SAVED_OFFSET;
117 fs->regs.how[11] = REG_SAVED_OFFSET;
118 fs->regs.how[12] = REG_SAVED_OFFSET;
119 fs->regs.how[13] = REG_SAVED_OFFSET;
120 fs->regs.how[14] = REG_SAVED_OFFSET;
121 fs->regs.how[15] = REG_SAVED_OFFSET;
122 fs->regs.how[16] = REG_SAVED_OFFSET;
123
124 fs->retaddr_column = 16;
125 fs->signal_frame = 1;
126
127 return _URC_NO_REASON;
128 }
129
130 #else /* ifdef __x86_64__ */
131
132 #define MD_FALLBACK_FRAME_STATE_FOR x86_gnu_fallback_frame_state
133
134 static _Unwind_Reason_Code
135 x86_gnu_fallback_frame_state
136 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
137 {
138 struct handler_args {
139 int signo;
140 union
141 {
142 struct
143 {
144 long int sigcode;
145 struct sigcontext *scp;
146 } legacy;
147 struct
148 {
149 siginfo_t *siginfop;
150 ucontext_t *uctxp;
151 } posix;
152 };
153 } *handler_args;
154 long int sigcode;
155 unsigned long usp;
156
157 /*
158 * i386 sigtramp frame we are looking for follows.
159 * (see glibc/sysdeps/mach/hurd/i386/trampoline.c assembly)
160 *
161 * rpc_wait_trampoline:
162 * 0: b8 e7 ff ff ff mov $-25,%eax mach_msg_trap
163 * 5: 9a 00 00 00 00 07 00 lcall $7,$0
164 * 12: 89 01 movl %eax, (%ecx)
165 * 14: 89 dc movl %ebx, %esp switch to signal stack
166 *
167 * trampoline:
168 * 16: ff d2 call *%edx call the handler function
169 * RA HERE
170 * 18: 83 c4 0c addl $12, %esp pop its args
171 * 21: c3 ret return to sigreturn
172 *
173 * firewall:
174 * 22: f4 hlt
175 */
176
177 if (!( *(unsigned int *)(context->ra ) == 0xc30cc483
178 && *(unsigned char *)(context->ra + 4) == 0xf4
179
180 && *(unsigned int *)(context->ra - 4) == 0xd2ffdc89
181 && *(unsigned int *)(context->ra - 8) == 0x01890007
182 && *(unsigned int *)(context->ra - 12) == 0x00000000
183 && *(unsigned int *)(context->ra - 16) == 0x9affffff
184 && *(unsigned short *)(context->ra - 18) == 0xe7b8))
185 return _URC_END_OF_STACK;
186
187 handler_args = context->cfa;
188 sigcode = handler_args->legacy.sigcode;
189 if (sigcode >= -16 && sigcode < 4096)
190 {
191 /* This cannot be a SIGINFO pointer, assume legacy. */
192 struct sigcontext *scp = handler_args->legacy.scp;
193 usp = scp->sc_uesp;
194
195 fs->regs.reg[0].loc.offset = (unsigned long)&scp->sc_eax - usp;
196 fs->regs.reg[1].loc.offset = (unsigned long)&scp->sc_ecx - usp;
197 fs->regs.reg[2].loc.offset = (unsigned long)&scp->sc_edx - usp;
198 fs->regs.reg[3].loc.offset = (unsigned long)&scp->sc_ebx - usp;
199 fs->regs.reg[5].loc.offset = (unsigned long)&scp->sc_ebp - usp;
200 fs->regs.reg[6].loc.offset = (unsigned long)&scp->sc_esi - usp;
201 fs->regs.reg[7].loc.offset = (unsigned long)&scp->sc_edi - usp;
202 fs->regs.reg[8].loc.offset = (unsigned long)&scp->sc_eip - usp;
203 }
204 else
205 {
206 /* This is not a valid sigcode, assume SIGINFO. */
207 ucontext_t *uctxp = handler_args->posix.uctxp;
208 gregset_t *gregset = &uctxp->uc_mcontext.gregs;
209 usp = (*gregset)[REG_UESP];
210
211 fs->regs.reg[0].loc.offset = (unsigned long)&(*gregset)[REG_EAX] - usp;
212 fs->regs.reg[1].loc.offset = (unsigned long)&(*gregset)[REG_ECX] - usp;
213 fs->regs.reg[2].loc.offset = (unsigned long)&(*gregset)[REG_EDX] - usp;
214 fs->regs.reg[3].loc.offset = (unsigned long)&(*gregset)[REG_EBX] - usp;
215 fs->regs.reg[5].loc.offset = (unsigned long)&(*gregset)[REG_EBP] - usp;
216 fs->regs.reg[6].loc.offset = (unsigned long)&(*gregset)[REG_ESI] - usp;
217 fs->regs.reg[7].loc.offset = (unsigned long)&(*gregset)[REG_EDI] - usp;
218 fs->regs.reg[8].loc.offset = (unsigned long)&(*gregset)[REG_EIP] - usp;
219 }
220
221 fs->regs.cfa_how = CFA_REG_OFFSET;
222 fs->regs.cfa_reg = 4;
223 fs->regs.cfa_offset = usp - (unsigned long) context->cfa;
224
225 fs->regs.how[0] = REG_SAVED_OFFSET;
226 fs->regs.how[1] = REG_SAVED_OFFSET;
227 fs->regs.how[2] = REG_SAVED_OFFSET;
228 fs->regs.how[3] = REG_SAVED_OFFSET;
229 fs->regs.how[5] = REG_SAVED_OFFSET;
230 fs->regs.how[6] = REG_SAVED_OFFSET;
231 fs->regs.how[7] = REG_SAVED_OFFSET;
232 fs->regs.how[8] = REG_SAVED_OFFSET;
233
234 fs->retaddr_column = 8;
235 fs->signal_frame = 1;
236
237 return _URC_NO_REASON;
238 }
239
240 #endif /* ifdef __x86_64__ */
241
242 #endif /* ifndef inhibit_libc */