Update FSF address.
[official-gcc.git] / gcc / config / s390 / tpf-unwind.h
blob600600638b17772d0608cb7c8e60614390857e19
1 /* DWARF2 EH unwinding support for TPF OS.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by P.J. Darcy (darcypj@us.ibm.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
31 #define __USE_GNU 1
32 #define _GNU_SOURCE
33 #include <dlfcn.h>
34 #undef __USE_GNU
35 #undef _GNU_SOURCE
37 /* Function Name: __isPATrange
38 Parameters passed into it: address to check
39 Return Value: A 1 if address is in pat code "range", 0 if not
40 Description: This function simply checks to see if the address
41 passed to it is in the CP pat code range. */
43 #define MIN_PATRANGE 0x10000
44 #define MAX_PATRANGE 0x800000
46 static inline unsigned int
47 __isPATrange (void *addr)
49 if (addr > (void *)MIN_PATRANGE && addr < (void *)MAX_PATRANGE)
50 return 1;
51 else
52 return 0;
55 /* TPF return address offset from start of stack frame. */
56 #define TPFRA_OFFSET 168
58 /* Exceptions macro defined for TPF so that functions without
59 dwarf frame information can be used with exceptions. */
60 #define MD_FALLBACK_FRAME_STATE_FOR s390_fallback_frame_state
62 static _Unwind_Reason_Code
63 s390_fallback_frame_state (struct _Unwind_Context *context,
64 _Unwind_FrameState *fs)
66 unsigned long int regs;
67 unsigned long int new_cfa;
68 int i;
70 regs = *((unsigned long int *)
71 (((unsigned long int) context->cfa) - STACK_POINTER_OFFSET));
73 /* Are we going through special linkage code? */
74 if (__isPATrange (context->ra))
77 /* Our return register isn't zero for end of stack, so
78 check backward stackpointer to see if it is zero. */
79 if (regs == NULL)
80 return _URC_END_OF_STACK;
82 /* No stack frame. */
83 fs->cfa_how = CFA_REG_OFFSET;
84 fs->cfa_reg = 15;
85 fs->cfa_offset = STACK_POINTER_OFFSET;
87 /* All registers remain unchanged ... */
88 for (i = 0; i < 32; i++)
90 fs->regs.reg[i].how = REG_SAVED_REG;
91 fs->regs.reg[i].loc.reg = i;
94 /* ... except for %r14, which is stored at CFA-112
95 and used as return address. */
96 fs->regs.reg[14].how = REG_SAVED_OFFSET;
97 fs->regs.reg[14].loc.offset = TPFRA_OFFSET - STACK_POINTER_OFFSET;
98 fs->retaddr_column = 14;
100 return _URC_NO_REASON;
103 regs = *((unsigned long int *)
104 (((unsigned long int) context->cfa) - STACK_POINTER_OFFSET));
105 new_cfa = regs + STACK_POINTER_OFFSET;
107 fs->cfa_how = CFA_REG_OFFSET;
108 fs->cfa_reg = 15;
109 fs->cfa_offset = new_cfa -
110 (unsigned long int) context->cfa + STACK_POINTER_OFFSET;
112 for (i = 0; i < 16; i++)
114 fs->regs.reg[i].how = REG_SAVED_OFFSET;
115 fs->regs.reg[i].loc.offset = regs + i*8 - new_cfa;
118 for (i = 0; i < 4; i++)
120 fs->regs.reg[16 + i].how = REG_SAVED_OFFSET;
121 fs->regs.reg[16 + i].loc.offset = regs + 16*8 + i*8 - new_cfa;
124 fs->retaddr_column = 14;
126 return _URC_NO_REASON;
129 /* Function Name: __tpf_eh_return
130 Parameters passed into it: Destination address to jump to.
131 Return Value: Converted Destination address if a Pat Stub exists.
132 Description: This function swaps the unwinding return address
133 with the cp stub code. The original target return address is
134 then stored into the tpf return address field. The cp stub
135 code is searched for by climbing back up the stack and
136 comparing the tpf stored return address object address to
137 that of the targets object address. */
139 #define CURRENT_STACK_PTR() \
140 ({ register unsigned long int *stack_ptr asm ("%r15"); stack_ptr; })
142 #define PREVIOUS_STACK_PTR() \
143 ((unsigned long int *)(*(CURRENT_STACK_PTR())))
145 #define RA_OFFSET 112
146 #define R15_OFFSET 120
147 #define TPFAREA_OFFSET 160
148 #define TPFAREA_SIZE STACK_POINTER_OFFSET-TPFAREA_OFFSET
149 #define INVALID_RETURN 0
151 void * __tpf_eh_return (void *target);
153 void *
154 __tpf_eh_return (void *target)
156 Dl_info targetcodeInfo, currentcodeInfo;
157 int retval;
158 void *current, *stackptr, *destination_frame;
159 unsigned long int shifter, is_a_stub;
161 is_a_stub = 0;
163 /* Get code info for target return's address. */
164 retval = dladdr (target, &targetcodeInfo);
166 /* Ensure the code info is valid (for target). */
167 if (retval != INVALID_RETURN)
170 /* Get the stack pointer of the stack frame to be modified by
171 the exception unwinder. So that we can begin our climb
172 there. */
173 stackptr = (void *) *((unsigned long int *) (*(PREVIOUS_STACK_PTR())));
175 /* Begin looping through stack frames. Stop if invalid
176 code information is retrieved or if a match between the
177 current stack frame iteration shared object's address
178 matches that of the target, calculated above. */
181 /* Get return address based on our stackptr iterator. */
182 current = (void *) *((unsigned long int *)
183 (stackptr+RA_OFFSET));
185 /* Is it a Pat Stub? */
186 if (__isPATrange (current))
188 /* Yes it was, get real return address
189 in TPF stack area. */
190 current = (void *) *((unsigned long int *)
191 (stackptr+TPFRA_OFFSET));
192 is_a_stub = 1;
195 /* Get codeinfo on RA so that we can figure out
196 the module address. */
197 retval = dladdr (current, &currentcodeInfo);
199 /* Check that codeinfo for current stack frame is valid.
200 Then compare the module address of current stack frame
201 to target stack frame to determine if we have the pat
202 stub address we want. Also ensure we are dealing
203 with a module crossing, stub return address. */
204 if (is_a_stub && retval != INVALID_RETURN
205 && targetcodeInfo.dli_fbase == currentcodeInfo.dli_fbase)
207 /* Yes! They are in the same module.
208 Force copy of TPF private stack area to
209 destination stack frame TPF private area. */
210 destination_frame = (void *) *((unsigned long int *)
211 (*PREVIOUS_STACK_PTR() + R15_OFFSET));
213 /* Copy TPF linkage area from current frame to
214 destination frame. */
215 memcpy((void *) (destination_frame + TPFAREA_OFFSET),
216 (void *) (stackptr + TPFAREA_OFFSET), TPFAREA_SIZE);
218 /* Now overlay the
219 real target address into the TPF stack area of
220 the target frame we are jumping to. */
221 *((unsigned long int *) (destination_frame +
222 TPFRA_OFFSET)) = (unsigned long int) target;
224 /* Before returning the desired pat stub address to
225 the exception handling unwinder so that it can
226 actually do the "leap" shift out the low order
227 bit designated to determine if we are in 64BIT mode.
228 This is necessary for CTOA stubs.
229 Otherwise we leap one byte past where we want to
230 go to in the TPF pat stub linkage code. */
231 shifter = *((unsigned long int *)
232 (stackptr + RA_OFFSET));
234 shifter &= ~1ul;
236 /* Store Pat Stub Address in destination Stack Frame. */
237 *((unsigned long int *) (destination_frame +
238 RA_OFFSET)) = shifter;
240 /* Re-adjust pat stub address to go to correct place
241 in linkage. */
242 shifter = shifter - 4;
244 return (void *) shifter;
247 /* Desired module pat stub not found ...
248 Bump stack frame iterator. */
249 stackptr = (void *) *(unsigned long int *) stackptr;
251 is_a_stub = 0;
253 } while (stackptr && retval != INVALID_RETURN
254 && targetcodeInfo.dli_fbase != currentcodeInfo.dli_fbase);
257 /* No pat stub found, could be a problem? Simply return unmodified
258 target address. */
259 return target;