Use -fbuilding-libgcc for more target macros used in libgcc.
[official-gcc.git] / libgcc / config / sparc / sol2-unwind.h
blob6638464997c6f40356db01213ffa97f0b6fd17e8
1 /* DWARF2 EH unwinding support for SPARC Solaris.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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.
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/>. */
25 /* Do code reading to identify a signal frame, and set the frame
26 state data appropriately. See unwind-dw2.c for the structs. */
28 #include <ucontext.h>
29 #include <sys/frame.h>
30 #include <sys/stack.h>
32 #ifdef __arch64__
34 #define IS_SIGHANDLER sparc64_is_sighandler
36 static int
37 sparc64_is_sighandler (unsigned int *pc, void *cfa, int *nframes)
39 if (/* Solaris 8+ - multi-threaded
40 ----------------------------
41 <__sighndlr>: save %sp, -176, %sp
42 <__sighndlr+4>: mov %i0, %o0
43 <__sighndlr+8>: mov %i1, %o1
44 <__sighndlr+12>: call %i3
45 <__sighndlr+16>: mov %i2, %o2
46 <__sighndlr+20>: ret <--- PC
47 <__sighndlr+24>: restore */
48 pc[-5] == 0x9de3bf50
49 && pc[-4] == 0x90100018
50 && pc[-3] == 0x92100019
51 && pc[-2] == 0x9fc6c000
52 && pc[-1] == 0x9410001a
53 && pc[ 0] == 0x81c7e008
54 && pc[ 1] == 0x81e80000)
56 /* We have observed different calling frames among different
57 versions of the operating system, so that we need to
58 discriminate using the upper frame. We look for the return
59 address of the caller frame (there is an offset of 15 double
60 words between the frame address and the place where this return
61 address is stored) in order to do some more pattern matching. */
62 unsigned int cuh_pattern
63 = *(unsigned int *)(*(unsigned long *)(cfa + 15*8) - 4);
65 if (cuh_pattern == 0x92100019)
66 /* This matches the call_user_handler pattern in Solaris 11
67 libc.so.1:
69 <call_user_handler+864>: mov %i1, %o1
70 <call_user_handler+868>: call __sighndlr
72 This is the same setup as for Solaris 10, see below. */
73 *nframes = 3;
75 else if (cuh_pattern == 0xd25fa7ef)
77 /* This matches the call_user_handler pattern in Solaris 10
78 libc.so.1:
80 <call_user_handler+988>: ldx [ %fp + 0x7ef ], %o1
81 <call_user_handler+992>: call __sighndlr
83 There are 2 cases so we look for the return address of the
84 caller's caller frame in order to do more pattern matching. */
85 unsigned long sah_address = *(unsigned long *)(cfa + 176 + 15*8);
87 if (sah_address && *(unsigned int *)(sah_address - 4) == 0x92100019)
88 /* We need to move up three frames:
90 <signal handler> <-- context->cfa
91 __sighndlr
92 call_user_handler
93 sigacthandler
94 <kernel> */
95 *nframes = 3;
96 else
97 /* The sigacthandler frame isn't present in the chain.
98 We need to move up two frames:
100 <signal handler> <-- context->cfa
101 __sighndlr
102 call_user_handler
103 <kernel> */
104 *nframes = 2;
107 else if (cuh_pattern == 0x9410001a || cuh_pattern == 0x9410001b)
108 /* This matches the call_user_handler pattern in Solaris 9
109 libthread.so.1:
111 <call_user_handler+600>: mov %i2, %o2
112 <call_user_handler+604>: call __sighndlr
114 This is the same setup as for Solaris 10, see above. */
115 *nframes = 3;
117 return 1;
120 return 0;
123 #define MD_FALLBACK_FRAME_STATE_FOR sparc64_fallback_frame_state
125 #define MD_FROB_UPDATE_CONTEXT sparc64_frob_update_context
127 static void
128 sparc64_frob_update_context (struct _Unwind_Context *context,
129 _Unwind_FrameState *fs)
131 /* The column of %sp contains the old CFA, not the old value of %sp.
132 The CFA offset already comprises the stack bias so, when %sp is the
133 CFA register, we must avoid counting the stack bias twice. */
134 if (fs->regs.cfa_reg == __builtin_dwarf_sp_column ()
135 && fs->regs.cfa_how == CFA_REG_OFFSET
136 && fs->regs.cfa_offset != 0)
138 long i;
140 context->cfa -= STACK_BIAS;
142 for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__ + 1; ++i)
143 if (fs->regs.reg[i].how == REG_SAVED_OFFSET)
144 _Unwind_SetGRPtr (context, i,
145 _Unwind_GetGRPtr (context, i) - STACK_BIAS);
149 #else
151 #define IS_SIGHANDLER sparc_is_sighandler
153 static int
154 sparc_is_sighandler (unsigned int *pc, void *cfa, int *nframes)
156 if(/* Solaris 8+ - multi-threaded
157 ----------------------------
158 <__sighndlr>: save %sp, -96, %sp
159 <__sighndlr+4>: mov %i0, %o0
160 <__sighndlr+8>: mov %i1, %o1
161 <__sighndlr+12>: call %i3
162 <__sighndlr+16>: mov %i2, %o2
163 <__sighndlr+20>: ret <--- PC
164 <__sighndlr+24>: restore */
165 pc[-5] == 0x9de3bfa0
166 && pc[-4] == 0x90100018
167 && pc[-3] == 0x92100019
168 && pc[-2] == 0x9fc6c000
169 && pc[-1] == 0x9410001a
170 && pc[ 0] == 0x81c7e008
171 && pc[ 1] == 0x81e80000)
173 /* We have observed different calling frames among different
174 versions of the operating system, so that we need to
175 discriminate using the upper frame. We look for the return
176 address of the caller frame (there is an offset of 15 words
177 between the frame address and the place where this return
178 address is stored) in order to do some more pattern matching. */
179 unsigned int cuh_pattern
180 = *(unsigned int *)(*(unsigned int *)(cfa + 15*4) - 4);
182 if (cuh_pattern == 0x92100019)
183 /* This matches the call_user_handler pattern in Solaris 11
184 libc.so.1:
186 <call_user_handler+876>: mov %i1, %o1
187 <call_user_handler+880>: call __sighndlr
189 This is the same setup as for Solaris 10, see below. */
190 *nframes = 3;
192 else if (cuh_pattern == 0xd407a04c)
194 /* This matches the call_user_handler pattern in Solaris 10
195 libc.so.1:
197 <call_user_handler+948>: ld [ %fp + 0x4c ], %o2
198 <call_user_handler+952>: call __sighndlr
200 There are 2 cases so we look for the return address of the
201 caller's caller frame in order to do more pattern matching. */
202 unsigned int sah_address = *(unsigned int *)(cfa + 96 + 15*4);
204 if (sah_address && *(unsigned int *)(sah_address - 4) == 0x92100019)
205 /* We need to move up three frames:
207 <signal handler> <-- context->cfa
208 __sighndlr
209 call_user_handler
210 sigacthandler
211 <kernel> */
212 *nframes = 3;
213 else
214 /* The sigacthandler frame isn't present in the chain.
215 We need to move up two frames:
217 <signal handler> <-- context->cfa
218 __sighndlr
219 call_user_handler
220 <kernel> */
221 *nframes = 2;
224 else if (cuh_pattern == 0x9410001a || cuh_pattern == 0x9410001b)
225 /* This matches the call_user_handler pattern in Solaris 9
226 libthread.so.1:
228 <call_user_handler+560>: mov %i2, %o2
229 <call_user_handler+564>: call __sighndlr
231 This is the same setup as for Solaris 10, see above. */
232 *nframes = 3;
234 return 1;
237 return 0;
240 #define MD_FALLBACK_FRAME_STATE_FOR sparc_fallback_frame_state
242 #endif
244 static _Unwind_Reason_Code
245 MD_FALLBACK_FRAME_STATE_FOR (struct _Unwind_Context *context,
246 _Unwind_FrameState *fs)
248 void *pc = context->ra;
249 void *this_cfa = context->cfa;
250 int nframes = 0;
251 long new_cfa;
252 void *ra_location, *shifted_ra_location;
253 mcontext_t *mctx;
254 int i;
256 /* Deal with frame-less function from which a signal was raised. */
257 if (_Unwind_IsSignalFrame (context))
259 /* The CFA is by definition unmodified in this case. */
260 fs->regs.cfa_how = CFA_REG_OFFSET;
261 fs->regs.cfa_reg = __builtin_dwarf_sp_column ();
262 fs->regs.cfa_offset = 0;
264 /* This is the canonical RA column. */
265 fs->retaddr_column = 15;
267 return _URC_NO_REASON;
270 /* Do some pattern matching at the return address. */
271 if (IS_SIGHANDLER (pc, this_cfa, &nframes))
273 struct frame *fp = (struct frame *) this_cfa;
274 struct handler_args {
275 struct frame frwin;
276 ucontext_t ucontext;
277 } *handler_args;
278 ucontext_t *ucp;
280 /* this_cfa points into the frame after the saved frame pointer and
281 saved pc (struct frame).
283 The ucontext_t structure is in the kernel frame after a struct
284 frame. Since the frame sizes vary even within OS releases, we
285 need to walk the stack to get there. */
286 for (i = 0; i < nframes; i++)
287 fp = (struct frame *) ((char *)fp->fr_savfp + STACK_BIAS);
289 handler_args = (struct handler_args *) fp;
290 ucp = &handler_args->ucontext;
291 mctx = &ucp->uc_mcontext;
293 else
294 return _URC_END_OF_STACK;
296 /* The frame address is %sp + STACK_BIAS in 64-bit mode. */
297 new_cfa = mctx->gregs[REG_SP] + STACK_BIAS;
299 fs->regs.cfa_how = CFA_REG_OFFSET;
300 fs->regs.cfa_reg = __builtin_dwarf_sp_column ();
301 fs->regs.cfa_offset = new_cfa - (long) this_cfa + STACK_BIAS;
303 /* Restore global and out registers (in this order) from the
304 ucontext_t structure, uc_mcontext.gregs field. */
305 for (i = 1; i < 16; i++)
307 /* We never restore %sp as everything is purely CFA-based. */
308 if ((unsigned int) i == __builtin_dwarf_sp_column ())
309 continue;
311 /* First the global registers and then the out registers. */
312 fs->regs.reg[i].how = REG_SAVED_OFFSET;
313 fs->regs.reg[i].loc.offset = (long)&mctx->gregs[REG_Y + i] - new_cfa;
316 /* Just above the stack pointer there are 16 extended words in which
317 the register window (in and local registers) was saved. */
318 for (i = 0; i < 16; i++)
320 fs->regs.reg[i + 16].how = REG_SAVED_OFFSET;
321 fs->regs.reg[i + 16].loc.offset = i * sizeof(long);
324 /* Check whether we need to restore FPU registers. */
325 if (mctx->fpregs.fpu_qcnt)
327 for (i = 0; i < 32; i++)
329 fs->regs.reg[i + 32].how = REG_SAVED_OFFSET;
330 fs->regs.reg[i + 32].loc.offset
331 = (long)&mctx->fpregs.fpu_fr.fpu_regs[i] - new_cfa;
334 #ifdef __arch64__
335 /* For 64-bit, fpu_fr.fpu_dregs contains 32 instead of 16 doubles. */
336 for (i = 32; i < 64; i++)
338 if (i > 32 && (i & 1))
339 continue;
341 fs->regs.reg[i + 32].how = REG_SAVED_OFFSET;
342 fs->regs.reg[i + 32].loc.offset
343 = (long)&mctx->fpregs.fpu_fr.fpu_dregs[i/2] - new_cfa;
345 #endif
348 /* State the rules to find the kernel's code "return address", which is
349 the address of the active instruction when the signal was caught.
350 On the SPARC, since RETURN_ADDR_OFFSET (essentially 8) is defined, we
351 need to preventively subtract it from the purported return address. */
352 ra_location = &mctx->gregs[REG_PC];
353 shifted_ra_location = &mctx->gregs[REG_Y];
354 *(void **)shifted_ra_location = *(void **)ra_location - 8;
355 fs->retaddr_column = 0;
356 fs->regs.reg[0].how = REG_SAVED_OFFSET;
357 fs->regs.reg[0].loc.offset = (long)shifted_ra_location - new_cfa;
359 /* SIGFPE for IEEE-754 exceptions is delivered after the faulting insn
360 rather than before it, so don't set fs->signal_frame in that case.
361 We test whether the cexc field of the FSR is zero. */
362 if ((mctx->fpregs.fpu_fsr & 0x1f) == 0)
363 fs->signal_frame = 1;
365 return _URC_NO_REASON;