2 * Support functions for Wine exception handling
4 * Copyright (c) 1999, 2010 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/exception.h"
26 #if defined(__GNUC__) || defined(__clang__)
30 __ASM_GLOBAL_FUNC( __wine_rtl_unwind
,
32 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
33 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
35 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
37 "pushl $0\n\t" /* retval */
38 "pushl 12(%ebp)\n\t" /* record */
39 "pushl 16(%ebp)\n\t" /* target */
40 "pushl 8(%ebp)\n\t" /* frame */
41 "call " __ASM_STDCALL("RtlUnwind",16) "\n\t"
44 #elif defined(__x86_64__) && !defined(__arm64ec__)
46 __ASM_GLOBAL_FUNC( __wine_rtl_unwind
,
48 __ASM_SEH(".seh_pushreg %rbp\n\t")
49 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
50 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
52 __ASM_SEH(".seh_setframe %rbp,0\n\t")
53 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
55 __ASM_SEH(".seh_stackalloc 0x20\n\t")
56 __ASM_SEH(".seh_endprologue\n\t")
57 "movq %r8,%r9\n\t" /* retval = final target */
58 "movq %rdx,%r8\n\t" /* record */
59 "leaq __wine_unwind_trampoline(%rip),%rdx\n\t" /* target = trampoline */
60 "call " __ASM_NAME("RtlUnwind") "\n"
61 "__wine_unwind_trampoline:\n\t"
62 /* we need an extra call to make sure the stack is correctly aligned */
67 void __cdecl
__wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD
* frame
, EXCEPTION_RECORD
*record
,
68 void (*target
)(void) )
70 RtlUnwind( frame
, target
, record
, 0 );
76 static void DECLSPEC_NORETURN
unwind_target(void)
78 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)__wine_get_frame();
79 __wine_pop_frame( &wine_frame
->frame
);
80 for (;;) __wine_longjmp( &wine_frame
->jmp
, 1 );
83 static void DECLSPEC_NORETURN
unwind_frame( EXCEPTION_RECORD
*record
,
84 EXCEPTION_REGISTRATION_RECORD
*frame
)
86 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
88 /* hack to make GetExceptionCode() work in handler */
89 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
90 wine_frame
->ExceptionRecord
= wine_frame
;
92 __wine_rtl_unwind( frame
, record
, unwind_target
);
95 DWORD __cdecl
__wine_exception_handler( EXCEPTION_RECORD
*record
,
96 EXCEPTION_REGISTRATION_RECORD
*frame
,
98 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
100 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
101 EXCEPTION_POINTERS ptrs
;
103 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
| EXCEPTION_NESTED_CALL
))
104 return ExceptionContinueSearch
;
106 ptrs
.ExceptionRecord
= record
;
107 ptrs
.ContextRecord
= context
;
108 switch(wine_frame
->u
.filter( &ptrs
))
110 case EXCEPTION_CONTINUE_SEARCH
:
111 return ExceptionContinueSearch
;
112 case EXCEPTION_CONTINUE_EXECUTION
:
113 return ExceptionContinueExecution
;
114 case EXCEPTION_EXECUTE_HANDLER
:
117 unwind_frame( record
, frame
);
120 DWORD __cdecl
__wine_exception_ctx_handler( EXCEPTION_RECORD
*record
,
121 EXCEPTION_REGISTRATION_RECORD
*frame
,
123 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
125 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
126 EXCEPTION_POINTERS ptrs
;
128 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
| EXCEPTION_NESTED_CALL
))
129 return ExceptionContinueSearch
;
131 ptrs
.ExceptionRecord
= record
;
132 ptrs
.ContextRecord
= context
;
133 switch(wine_frame
->u
.filter_ctx( &ptrs
, wine_frame
->ctx
))
135 case EXCEPTION_CONTINUE_SEARCH
:
136 return ExceptionContinueSearch
;
137 case EXCEPTION_CONTINUE_EXECUTION
:
138 return ExceptionContinueExecution
;
139 case EXCEPTION_EXECUTE_HANDLER
:
142 unwind_frame( record
, frame
);
145 DWORD __cdecl
__wine_exception_handler_page_fault( EXCEPTION_RECORD
*record
,
146 EXCEPTION_REGISTRATION_RECORD
*frame
,
148 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
150 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
| EXCEPTION_NESTED_CALL
))
151 return ExceptionContinueSearch
;
152 if (record
->ExceptionCode
!= STATUS_ACCESS_VIOLATION
)
153 return ExceptionContinueSearch
;
154 unwind_frame( record
, frame
);
157 DWORD __cdecl
__wine_exception_handler_all( EXCEPTION_RECORD
*record
,
158 EXCEPTION_REGISTRATION_RECORD
*frame
,
160 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
162 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
| EXCEPTION_NESTED_CALL
))
163 return ExceptionContinueSearch
;
164 unwind_frame( record
, frame
);
167 DWORD __cdecl
__wine_finally_handler( EXCEPTION_RECORD
*record
,
168 EXCEPTION_REGISTRATION_RECORD
*frame
,
170 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
172 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
))
174 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
175 wine_frame
->u
.finally_func( FALSE
);
177 return ExceptionContinueSearch
;
180 DWORD __cdecl
__wine_finally_ctx_handler( EXCEPTION_RECORD
*record
,
181 EXCEPTION_REGISTRATION_RECORD
*frame
,
183 EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
185 if (record
->ExceptionFlags
& (EXCEPTION_UNWINDING
| EXCEPTION_EXIT_UNWIND
))
187 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
188 wine_frame
->u
.finally_func_ctx( FALSE
, wine_frame
->ctx
);
190 return ExceptionContinueSearch
;
193 #endif /* __GNUC__ || __clang__ */