2 * PROJECT: ReactOS system libraries
3 * LICENSE: GNU GPL - See COPYING in the top level directory
4 * PURPOSE: Header for PSEH3
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
8 /* For additional information see pseh3.c in the related library. */
15 typedef struct _SEH3$_SCOPE_TABLE
19 } SEH3$_SCOPE_TABLE
, *PSEH3$_SCOPE_TABLE
;
21 typedef struct _SEH3$_EXCEPTION_POINTERS
23 struct _EXCEPTION_RECORD
*ExceptionRecord
;
24 struct _CONTEXT
*ContextRecord
;
25 } SEH3$_EXCEPTION_POINTERS
, *PSEH3$_EXCEPTION_POINTERS
;
27 typedef struct _SEH3$_REGISTRATION_FRAME
29 /* First the Windows base record. Don't move this! */
30 struct _SEH3$_REGISTRATION_FRAME
*Next
;
33 /* Points to the end of the internal registration chain */
34 struct _SEH3$_REGISTRATION_FRAME
*EndOfChain
;
36 /* Pointer to the static scope table */
37 PSEH3$_SCOPE_TABLE ScopeTable
;
39 /* Except handler stores pointer to exception pointers here */
40 PSEH3$_EXCEPTION_POINTERS
volatile ExceptionPointers
;
42 /* Registers that we need to save */
46 } SEH3$_REGISTRATION_FRAME
,*PSEH3$_REGISTRATION_FRAME
;
48 /* Prevent gcc from inlining functions that use SEH. */
49 static inline __attribute__((always_inline
)) __attribute__((returns_twice
)) void _SEH3$
_PreventInlining() {}
51 extern inline __attribute__((always_inline
,gnu_inline
))
52 void _SEH3$
_UnregisterFrame(volatile SEH3$_REGISTRATION_FRAME
*RegistrationFrame
)
54 asm volatile ("movl %k[NewHead], %%fs:0"
55 : : [NewHead
] "ir" (RegistrationFrame
->Next
) : "memory");
58 extern inline __attribute__((always_inline
,gnu_inline
))
59 void _SEH3$
_UnregisterTryLevel(
60 volatile SEH3$_REGISTRATION_FRAME
*TrylevelFrame
)
62 volatile SEH3$_REGISTRATION_FRAME
*RegistrationFrame
;
63 asm volatile ("movl %%fs:0, %k[RegistrationFrame]"
64 : [RegistrationFrame
] "=r" (RegistrationFrame
) : );
65 RegistrationFrame
->EndOfChain
= TrylevelFrame
->Next
;
73 /* These are global dummy definitions, that get overwritten in the local context of __finally / __except blocks */
74 int __cdecl
__attribute__((error ("Can only be used inside a __finally block."))) _abnormal_termination(void);
75 unsigned long __cdecl
__attribute__((error("Can only be used inside an exception filter or __except block."))) _exception_code(void);
76 void * __cdecl
__attribute__((error("Can only be used inside an exception filter."))) _exception_info(void);
78 /* Define the registers that get clobbered, when reaching the __except block.
79 We specify ebp on optimized builds without frame pointer, since it will be
80 used by GCC as a general purpose register then. */
81 #if defined(__OPTIMIZE__) && defined(_ALLOW_OMIT_FRAME_POINTER)
82 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebp", "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
84 #define _SEH3$_CLOBBER_ON_EXCEPTION "ebx", "ecx", "edx", "esi", "edi", "flags", "memory"
87 /* This attribute allows automatic cleanup of the registered frames */
88 #define _SEH3$_AUTO_CLEANUP __attribute__((cleanup(_SEH3$_AutoCleanup)))
90 #define _SEH3$_ASM_GOTO(_Asm, _Label, ...) asm goto (_Asm : : : "memory", ## __VA_ARGS__ : _Label)
92 #define _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
93 inline __attribute__((always_inline, gnu_inline)) \
94 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; }
96 /* This is an asm wrapper around _SEH3$_RegisterFrame */
97 #define _SEH3$_RegisterFrame(_TrylevelFrame, _DataTable, _Target) \
98 asm goto ("leal %0, %%ecx\n" \
99 "call __SEH3$_RegisterFrame\n" \
101 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
102 : "ecx", "edx", "memory" \
105 /* This is an asm wrapper around _SEH3$_EnterTryLevel */
106 #define _SEH3$_RegisterTryLevel(_TrylevelFrame, _DataTable, _Target) \
107 asm goto ("leal %0, %%ecx\n" \
108 "call __SEH3$_RegisterTryLevel\n" \
110 : "m" (*(_TrylevelFrame)), "a" (_DataTable) \
111 : "ecx", "edx", "memory" \
114 /* On GCC the filter function is a nested function with __fastcall calling
115 convention. The eax register contains a base address the function uses
116 to address the callers stack frame. __fastcall is chosen, because it gives
117 us an effective was of passing one argument to the function, that we need
118 to tell the function in a first pass to return informtion about the frame
119 base address. Since this is something GCC chooses arbitrarily, we call
120 the function with an arbitrary base address in eax first and then use the
121 result to calculate the correct address for a second call to the function. */
122 #define _SEH3$_DECLARE_FILTER_FUNC(_Name) \
123 auto int __fastcall _Name(int Action)
125 #define _SEH3$_NESTED_FUNC_OPEN(_Name) \
126 int __fastcall _Name(int Action) \
128 /* This is a fancy way to get information about the frame layout */ \
129 if (Action == 0) return (int)&_SEH3$_TrylevelFrame;
131 #define _SEH3$_DEFINE_FILTER_FUNC(_Name, expression) \
132 _SEH3$_NESTED_FUNC_OPEN(_Name) \
133 /* Declare the intrinsics for exception filters */ \
134 inline __attribute__((always_inline, gnu_inline)) \
135 unsigned long _exception_code() { return _SEH3$_TrylevelFrame.ExceptionPointers->ExceptionRecord->ExceptionCode; } \
136 inline __attribute__((always_inline, gnu_inline)) \
137 void * _exception_info() { return _SEH3$_TrylevelFrame.ExceptionPointers; } \
139 /* Now handle the actual filter expression */ \
140 return (expression); \
143 #define _SEH3$_FINALLY_FUNC_OPEN(_Name) \
144 _SEH3$_NESTED_FUNC_OPEN(_Name) \
145 /* Declare the intrinsics for the finally function */ \
146 inline __attribute__((always_inline, gnu_inline)) \
147 int _abnormal_termination() { return (_SEH3$_TrylevelFrame.ScopeTable != 0); }
149 #define _SEH3$_FILTER(_Filter, _FilterExpression) \
150 (__builtin_constant_p(_FilterExpression) ? (void*)(unsigned long)(unsigned char)(unsigned long)(_FilterExpression) : _Filter)
152 #define _SEH3$_DEFINE_DUMMY_FINALLY(_Name) \
153 auto inline __attribute__((always_inline,gnu_inline)) int _Name(int Action) { (void)Action; return 0; }
155 #define _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
156 auto inline __attribute__((always_inline,gnu_inline)) void _Name(volatile SEH3$_REGISTRATION_FRAME *p)
158 #define _SEH3$_DEFINE_CLEANUP_FUNC(_Name) \
159 _SEH3$_DECLARE_CLEANUP_FUNC(_Name) \
162 /* Unregister the frame */ \
163 if (_SEH3$_TryLevel == 1) _SEH3$_UnregisterFrame(&_SEH3$_TrylevelFrame); \
164 else _SEH3$_UnregisterTryLevel(&_SEH3$_TrylevelFrame); \
166 /* Invoke the finally function (an inline dummy in the __except case) */ \
167 _SEH3$_FinallyFunction(1); \
170 /* This construct scares GCC so much, that it will stop moving code
171 around into places that are never executed. */
172 #define _SEH3$_SCARE_GCC() \
174 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_BeforeTry); \
175 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_HandlerTarget); \
176 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
177 asm volatile ("#" : "=a"(plabel) : "p"(&&_SEH3$_l_BeforeTry), "p"(&&_SEH3$_l_HandlerTarget), "p"(&&_SEH3$_l_OnException) \
178 : _SEH3$_CLOBBER_ON_EXCEPTION ); \
179 goto _SEH3$_l_OnException;
183 _SEH3$_PreventInlining(); \
184 /* Enter the outer scope */ \
186 /* Declare local labels */ \
187 __label__ _SEH3$_l_BeforeTry; \
188 __label__ _SEH3$_l_DoTry; \
189 __label__ _SEH3$_l_AfterTry; \
190 __label__ _SEH3$_l_EndTry; \
191 __label__ _SEH3$_l_HandlerTarget; \
192 __label__ _SEH3$_l_OnException; \
194 /* Count the try level. Outside of any __try, _SEH3$_TryLevel is 0 */ \
196 _SEH3$_PreviousTryLevel = _SEH3$_TryLevel, \
197 _SEH3$_TryLevel = _SEH3$_PreviousTryLevel + 1, \
200 /* Forward declaration of the auto cleanup function */ \
201 _SEH3$_DECLARE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
203 /* Allocate a registration frame */ \
204 volatile SEH3$_REGISTRATION_FRAME _SEH3$_AUTO_CLEANUP _SEH3$_TrylevelFrame; \
206 goto _SEH3$_l_BeforeTry; \
207 /* Silence warning */ goto _SEH3$_l_AfterTry; \
213 #define _SEH3_EXCEPT(...) \
214 /* End the try block */ \
216 _SEH3$_l_AfterTry: (void)0; \
217 goto _SEH3$_l_EndTry; \
219 _SEH3$_l_BeforeTry: (void)0; \
220 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
222 /* Forward declaration of the filter function */ \
223 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FilterFunction); \
225 /* Create a static data table that contains the jump target and filter function */ \
226 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { &&_SEH3$_l_HandlerTarget, _SEH3$_FILTER(&_SEH3$_FilterFunction, (__VA_ARGS__)) }; \
228 /* Register the registration record. */ \
229 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
230 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
232 /* Emit the filter function */ \
233 _SEH3$_DEFINE_FILTER_FUNC(_SEH3$_FilterFunction, (__VA_ARGS__)) \
235 /* Define an empty inline finally function */ \
236 _SEH3$_DEFINE_DUMMY_FINALLY(_SEH3$_FinallyFunction) \
238 /* Allow intrinsics for __except to be used */ \
239 _SEH3$_DECLARE_EXCEPT_INTRINSICS() \
241 goto _SEH3$_l_DoTry; \
243 _SEH3$_l_HandlerTarget: (void)0; \
247 /* Prevent this block from being optimized away */ \
248 asm volatile ("#\n"); \
252 #define _SEH3_FINALLY \
253 /* End the try block */ \
255 _SEH3$_l_AfterTry: (void)0; \
256 /* Set ScopeTable to 0, this is used by _abnormal_termination() */ \
257 _SEH3$_TrylevelFrame.ScopeTable = 0; \
259 goto _SEH3$_l_EndTry; \
261 _SEH3$_l_BeforeTry: (void)0; \
262 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
264 /* Forward declaration of the finally function */ \
265 _SEH3$_DECLARE_FILTER_FUNC(_SEH3$_FinallyFunction); \
267 /* Create a static data table that contains the finally function */ \
268 static const SEH3$_SCOPE_TABLE _SEH3$_ScopeTable = { 0, &_SEH3$_FinallyFunction }; \
270 /* Register the registration record. */ \
271 if (_SEH3$_TryLevel == 1) _SEH3$_RegisterFrame(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
272 else _SEH3$_RegisterTryLevel(&_SEH3$_TrylevelFrame, &_SEH3$_ScopeTable, _SEH3$_l_HandlerTarget); \
274 goto _SEH3$_l_DoTry; \
276 _SEH3$_l_HandlerTarget: (void)0; \
278 _SEH3$_FINALLY_FUNC_OPEN(_SEH3$_FinallyFunction) \
279 /* This construct makes sure that the finally function returns */ \
280 /* a proper value at the end */ \
281 for (; ; (void)({return 0; 0;}))
287 goto _SEH3$_l_EndTry; \
289 _SEH3$_l_OnException: (void)0; \
290 /* Force GCC to create proper code pathes */ \
293 _SEH3$_l_EndTry:(void)0; \
294 _SEH3$_ASM_GOTO("#\n", _SEH3$_l_OnException); \
296 /* Implementation of the auto cleanup function */ \
297 _SEH3$_DEFINE_CLEANUP_FUNC(_SEH3$_AutoCleanup); \
299 /* Close the outer scope */ \
302 #define _SEH3_LEAVE goto _SEH3$_l_AfterTry
304 #define _SEH3_VOLATILE volatile