[4] | 1 | /*
|
---|
[21381] | 2 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
[4] | 3 | *
|
---|
[21381] | 4 | * Compiler-level Win32 SEH support for OS/2
|
---|
[4] | 5 | *
|
---|
[21605] | 6 | * Copyright 2010 Dmitriy Kuminov
|
---|
[4] | 7 | */
|
---|
| 8 |
|
---|
[21448] | 9 | /*
|
---|
| 10 | * NOTE: This __try/__except and __try/__finally/__leave implementation is not
|
---|
| 11 | * backed up by the low level compiler support and therefore the following
|
---|
| 12 | * limitations exist comparing to the MSVC implementation (breaking them will
|
---|
| 13 | * crash the application):
|
---|
| 14 | *
|
---|
[21474] | 15 | * 1. You cannot use the return statement within __try or __except or __finally
|
---|
| 16 | * blocks.
|
---|
[21448] | 17 | *
|
---|
[21474] | 18 | * 2. You cannot use the goto statement or the longjmp() function within __try
|
---|
| 19 | * or __except or __finally blocks if it passes control outside these blocks.
|
---|
| 20 | *
|
---|
[21448] | 21 | * 2. If you use __try and friends inside a do/while/for/switch block, you will
|
---|
| 22 | * lose the meaning of break and continue statements and must not use them.
|
---|
| 23 | *
|
---|
| 24 | * 3. The scopes of C and C++ exception blocks may not overlap (i.e. you cannot
|
---|
| 25 | * use try/catch inside __try/__except and vice versa).
|
---|
| 26 | *
|
---|
| 27 | * 4. There may be some other (yet unknown) limitations.
|
---|
| 28 | *
|
---|
| 29 | * Fortunately, in most cases, these limitations may be worked around by
|
---|
| 30 | * slightly changing the original source code.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[21381] | 33 | #ifndef __EXCPT_H__
|
---|
| 34 | #define __EXCPT_H__
|
---|
| 35 |
|
---|
[21387] | 36 | #include <windows.h>
|
---|
[21381] | 37 |
|
---|
| 38 | #ifdef __cplusplus
|
---|
| 39 | extern "C" {
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | #if defined(__GNUC__)
|
---|
| 43 |
|
---|
[21474] | 44 | struct ___seh_EXCEPTION_FRAME;
|
---|
[21381] | 45 |
|
---|
[21999] | 46 | #ifdef ODIN_FORCE_WIN32_TIB
|
---|
| 47 |
|
---|
| 48 | typedef int (*__seh_PEXCEPTION_HANDLER_WIN32)(PEXCEPTION_RECORD,
|
---|
| 49 | struct ___seh_EXCEPTION_FRAME *,
|
---|
| 50 | PCONTEXT, PVOID);
|
---|
| 51 |
|
---|
[21474] | 52 | #pragma pack(1)
|
---|
| 53 |
|
---|
| 54 | typedef struct ___seh_EXCEPTION_FRAME
|
---|
[21381] | 55 | {
|
---|
[21474] | 56 | /* + 0 */ struct ___seh_EXCEPTION_FRAME *pPrev;
|
---|
[21999] | 57 | /* + 4 */ __seh_PEXCEPTION_HANDLER_WIN32 pHandler;
|
---|
[21474] | 58 | /* + 8 */ void *pFilterCallback;
|
---|
| 59 | /* +12 */ void *pHandlerCallback;
|
---|
| 60 | /* +16 */ void *pHandlerContext;
|
---|
| 61 | /* +20 */ int filterResult;
|
---|
| 62 | /* +24 */ DWORD EBX;
|
---|
| 63 | /* +28 */ DWORD ESI;
|
---|
| 64 | /* +32 */ DWORD EDI;
|
---|
| 65 | /* +36 */ DWORD EBP;
|
---|
| 66 | /* +40 */ DWORD ESP;
|
---|
| 67 | /* +44 */ DWORD pPrevFrameOS2;
|
---|
| 68 | /* +48 */ EXCEPTION_POINTERS Pointers;
|
---|
| 69 | /* +56 */ int state;
|
---|
| 70 | /* +60 */ DWORD pPrevFrameWin32;
|
---|
[21633] | 71 | /* +64 */ DWORD Win32FS;
|
---|
[21381] | 72 | }
|
---|
[21474] | 73 | __seh_EXCEPTION_FRAME;
|
---|
[21381] | 74 |
|
---|
[21474] | 75 | #pragma pack()
|
---|
[21381] | 76 |
|
---|
[21999] | 77 | extern int __seh_handler_win32(PEXCEPTION_RECORD pRec,
|
---|
| 78 | struct ___seh_EXCEPTION_FRAME *pFrame,
|
---|
| 79 | PCONTEXT pContext, PVOID pVoid);
|
---|
| 80 |
|
---|
| 81 | #else /* ODIN_FORCE_WIN32_TIB */
|
---|
| 82 |
|
---|
| 83 | typedef int (*__seh_PEXCEPTION_HANDLER)(PVOID,
|
---|
| 84 | struct ___seh_EXCEPTION_FRAME *,
|
---|
| 85 | PVOID, PVOID);
|
---|
| 86 |
|
---|
| 87 | #pragma pack(1)
|
---|
| 88 |
|
---|
| 89 | typedef struct ___seh_EXCEPTION_FRAME
|
---|
| 90 | {
|
---|
| 91 | /* + 0 */ struct ___seh_EXCEPTION_FRAME *pPrev;
|
---|
| 92 | /* + 4 */ __seh_PEXCEPTION_HANDLER pHandler;
|
---|
| 93 | /* + 8 */ void *pFilterCallback;
|
---|
| 94 | /* +12 */ void *pHandlerCallback;
|
---|
| 95 | /* +16 */ void *pHandlerContext;
|
---|
| 96 | /* +20 */ int filterResult;
|
---|
| 97 | /* +24 */ DWORD EBX;
|
---|
| 98 | /* +28 */ DWORD ESI;
|
---|
| 99 | /* +32 */ DWORD EDI;
|
---|
| 100 | /* +36 */ DWORD EBP;
|
---|
| 101 | /* +40 */ DWORD ESP;
|
---|
| 102 | /* +44 */ EXCEPTION_POINTERS Pointers;
|
---|
| 103 | /* +52 */ int state;
|
---|
| 104 | }
|
---|
| 105 | __seh_EXCEPTION_FRAME;
|
---|
| 106 |
|
---|
| 107 | #pragma pack()
|
---|
| 108 |
|
---|
| 109 | extern int __seh_handler(PVOID pRec,
|
---|
[21474] | 110 | struct ___seh_EXCEPTION_FRAME *pFrame,
|
---|
[21999] | 111 | PVOID pContext, PVOID pVoid);
|
---|
[21474] | 112 |
|
---|
[21999] | 113 | #endif /* ODIN_FORCE_WIN32_TIB */
|
---|
| 114 |
|
---|
[21387] | 115 | #define _exception_code() (__seh_frame.Pointers.ExceptionRecord->ExceptionCode)
|
---|
[21474] | 116 | #define _exception_info() ((void *)&__seh_frame.Pointers)
|
---|
[21381] | 117 |
|
---|
[21474] | 118 | #define exception_code _exception_code
|
---|
| 119 | #define exception_info (PEXCEPTION_POINTERS)_exception_info
|
---|
| 120 |
|
---|
[21387] | 121 | #define GetExceptionCode _exception_code
|
---|
[21474] | 122 | #define GetExceptionInformation (PEXCEPTION_POINTERS)_exception_info
|
---|
[21387] | 123 |
|
---|
[21999] | 124 | #ifdef ODIN_FORCE_WIN32_TIB
|
---|
| 125 |
|
---|
[21381] | 126 | #define __try \
|
---|
[21474] | 127 | volatile __seh_EXCEPTION_FRAME __seh_frame; \
|
---|
[21999] | 128 | __seh_frame.pHandler = __seh_handler_win32; \
|
---|
[21427] | 129 | __seh_frame.Pointers.ExceptionRecord = NULL; \
|
---|
| 130 | __seh_frame.Pointers.ContextRecord = NULL; \
|
---|
[21382] | 131 | __seh_frame.state = 0; \
|
---|
[21636] | 132 | /* install exception handler, both Win32 and OS/2 chains (note: EDX is in \
|
---|
| 133 | * clobber too since it may carry any value when we jump back to \
|
---|
| 134 | * pFilterCallback from the handler */ \
|
---|
| 135 | __asm__ ("leal %0, %%ecx; " \
|
---|
| 136 | "movl %%fs, %%eax; " \
|
---|
| 137 | "andl $0x0000FFFF, %%eax; " \
|
---|
| 138 | "movl %%eax, 64(%%ecx); " \
|
---|
| 139 | "movl %%fs:0, %%eax; " \
|
---|
| 140 | "movl %%eax, 0(%%ecx); " \
|
---|
| 141 | "movl %%eax, 60(%%ecx); " \
|
---|
| 142 | "movl $0f, 8(%%ecx); " \
|
---|
| 143 | \
|
---|
| 144 | "movl %%ebx, 24(%%ecx); " \
|
---|
| 145 | "movl %%esi, 28(%%ecx); " \
|
---|
| 146 | "movl %%edi, 32(%%ecx); " \
|
---|
| 147 | "movl %%ebp, 36(%%ecx); " \
|
---|
| 148 | "movl %%esp, 40(%%ecx); " \
|
---|
| 149 | \
|
---|
| 150 | "pushl %%fs; " \
|
---|
| 151 | "pushl $Dos32TIB; " \
|
---|
| 152 | "popl %%fs; " \
|
---|
| 153 | "movl %%fs:0, %%eax; " \
|
---|
| 154 | "movl %%eax, 44(%%ecx); " \
|
---|
| 155 | "movl %%ecx, %%fs:0; " \
|
---|
| 156 | "popl %%fs; " \
|
---|
| 157 | \
|
---|
| 158 | "movl %%ecx, %%fs:0; " \
|
---|
| 159 | \
|
---|
| 160 | "\n0: /* pFilterCallback */ \n" \
|
---|
| 161 | \
|
---|
| 162 | : : "m" (__seh_frame) \
|
---|
| 163 | : "%eax", "%ecx", "%edx"); \
|
---|
[21382] | 164 | for (; __seh_frame.state <= 3; ++__seh_frame.state) \
|
---|
[21381] | 165 | if (__seh_frame.state == 0) \
|
---|
| 166 | { \
|
---|
| 167 | {
|
---|
| 168 |
|
---|
| 169 | #define __except(filter_expr) \
|
---|
| 170 | } \
|
---|
[21448] | 171 | /* cause the next state to be 3 */ \
|
---|
[21381] | 172 | __seh_frame.state = 2; \
|
---|
| 173 | } \
|
---|
| 174 | else if (__seh_frame.state == 1) { \
|
---|
[21448] | 175 | /* execption caught, call filter expression */ \
|
---|
[21381] | 176 | __seh_frame.filterResult = (filter_expr); \
|
---|
| 177 | __asm__("leal %0, %%ebx; jmp *%1" \
|
---|
| 178 | : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
|
---|
| 179 | : "%ebx"); \
|
---|
| 180 | } \
|
---|
| 181 | else if (__seh_frame.state == 3) \
|
---|
[21633] | 182 | /* remove exception handler (note that for some reason SMP kernel \
|
---|
| 183 | * seems to garbage the Win32FS:[0] cell with the OS/2 exception \
|
---|
| 184 | * registration record, so use the original __seh_frame value) */ \
|
---|
| 185 | __asm__ ("leal %0, %%ecx; " \
|
---|
[21636] | 186 | \
|
---|
[21633] | 187 | "movl 64(%%ecx), %%eax; " \
|
---|
| 188 | "movl %%eax, %%fs; " \
|
---|
[21636] | 189 | \
|
---|
[21474] | 190 | "movl 60(%%ecx), %%eax; " \
|
---|
[21381] | 191 | "movl %%eax, %%fs:0; " \
|
---|
[21636] | 192 | \
|
---|
[21474] | 193 | "pushl %%fs; " \
|
---|
| 194 | "pushl $Dos32TIB; " \
|
---|
| 195 | "popl %%fs; " \
|
---|
| 196 | "movl 44(%%ecx), %%eax; " \
|
---|
| 197 | "movl %%eax, %%fs:0; " \
|
---|
| 198 | "popl %%fs; " \
|
---|
[21633] | 199 | : : "m"(__seh_frame) \
|
---|
[21474] | 200 | : "%eax", "%ecx"); \
|
---|
[21448] | 201 | else /* __seh_frame.state == 2 -> execute except block */
|
---|
[21381] | 202 |
|
---|
[21448] | 203 | #define __finally \
|
---|
| 204 | } \
|
---|
| 205 | /* cause the next state to be 2 */ \
|
---|
| 206 | __seh_frame.state = 1; \
|
---|
| 207 | } \
|
---|
| 208 | else if (__seh_frame.state == 1) { \
|
---|
| 209 | /* execption caught, handle and proceed to the filally block */ \
|
---|
| 210 | __seh_frame.filterResult = EXCEPTION_EXECUTE_HANDLER; \
|
---|
| 211 | __asm__("leal %0, %%ebx; jmp *%1" \
|
---|
| 212 | : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
|
---|
| 213 | : "%ebx"); \
|
---|
| 214 | } \
|
---|
| 215 | else if (__seh_frame.state == 3) \
|
---|
[21633] | 216 | /* remove exception handler (note that for some reason SMP kernel \
|
---|
| 217 | * seems to garbage the Win32FS:[0] cell with the OS/2 exception \
|
---|
| 218 | * registration record, so use the original __seh_frame value) */ \
|
---|
| 219 | __asm__ ("leal %0, %%ecx; " \
|
---|
[21636] | 220 | \
|
---|
[21633] | 221 | "movl 64(%%ecx), %%eax; " \
|
---|
| 222 | "movl %%eax, %%fs; " \
|
---|
[21636] | 223 | \
|
---|
[21474] | 224 | "movl 60(%%ecx), %%eax; " \
|
---|
[21448] | 225 | "movl %%eax, %%fs:0; " \
|
---|
[21636] | 226 | \
|
---|
[21474] | 227 | "pushl %%fs; " \
|
---|
| 228 | "pushl $Dos32TIB; " \
|
---|
| 229 | "popl %%fs; " \
|
---|
| 230 | "movl 44(%%ecx), %%eax; " \
|
---|
| 231 | "movl %%eax, %%fs:0; " \
|
---|
| 232 | "popl %%fs; " \
|
---|
[21633] | 233 | : : "m"(__seh_frame) \
|
---|
[21474] | 234 | : "%eax", "%ecx"); \
|
---|
[21448] | 235 | else /* __seh_frame.state == 2 -> execute finally block */
|
---|
| 236 |
|
---|
| 237 | #define __leave \
|
---|
| 238 | /* cause the next state to be 2 */ \
|
---|
| 239 | __seh_frame.state = 1; \
|
---|
| 240 | continue;
|
---|
| 241 |
|
---|
[21999] | 242 | #else /* ODIN_FORCE_WIN32_TIB */
|
---|
| 243 |
|
---|
| 244 | #define __try \
|
---|
| 245 | volatile __seh_EXCEPTION_FRAME __seh_frame; \
|
---|
| 246 | __seh_frame.pHandler = __seh_handler; \
|
---|
| 247 | __seh_frame.Pointers.ExceptionRecord = NULL; \
|
---|
| 248 | __seh_frame.Pointers.ContextRecord = NULL; \
|
---|
| 249 | __seh_frame.state = 0; \
|
---|
| 250 | /* install OS/2 exception handler (note: EDX is in clobber too since it \
|
---|
| 251 | * may carry any value when we jump back to pFilterCallback from the \
|
---|
| 252 | * handler */ \
|
---|
| 253 | __asm__ ("leal %0, %%ecx; " \
|
---|
| 254 | "pushl %%fs; " \
|
---|
| 255 | "pushl $Dos32TIB; " \
|
---|
| 256 | "popl %%fs; " \
|
---|
| 257 | "movl %%fs:0, %%eax; " \
|
---|
| 258 | "movl %%eax, 0(%%ecx); " \
|
---|
| 259 | "movl $0f, 8(%%ecx); " \
|
---|
| 260 | "movl %%ebx, 24(%%ecx); " \
|
---|
| 261 | "movl %%esi, 28(%%ecx); " \
|
---|
| 262 | "movl %%edi, 32(%%ecx); " \
|
---|
| 263 | "movl %%ebp, 36(%%ecx); " \
|
---|
| 264 | "movl %%esp, 40(%%ecx); " \
|
---|
| 265 | "movl %%ecx, %%fs:0; " \
|
---|
| 266 | "popl %%fs; " \
|
---|
| 267 | \
|
---|
| 268 | "\n0: /* pFilterCallback */ \n" \
|
---|
| 269 | \
|
---|
| 270 | : : "m" (__seh_frame) \
|
---|
| 271 | : "%eax", "%ecx", "%edx"); \
|
---|
| 272 | for (; __seh_frame.state <= 3; ++__seh_frame.state) \
|
---|
| 273 | if (__seh_frame.state == 0) \
|
---|
| 274 | { \
|
---|
| 275 | {
|
---|
| 276 |
|
---|
| 277 | #define __except(filter_expr) \
|
---|
| 278 | } \
|
---|
| 279 | /* cause the next state to be 3 */ \
|
---|
| 280 | __seh_frame.state = 2; \
|
---|
| 281 | } \
|
---|
| 282 | else if (__seh_frame.state == 1) { \
|
---|
| 283 | /* execption caught, call filter expression */ \
|
---|
| 284 | __seh_frame.filterResult = (filter_expr); \
|
---|
| 285 | __asm__("leal %0, %%ebx; jmp *%1" \
|
---|
| 286 | : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
|
---|
| 287 | : "%ebx"); \
|
---|
| 288 | } \
|
---|
| 289 | else if (__seh_frame.state == 3) \
|
---|
| 290 | /* remove exception handler */ \
|
---|
| 291 | __asm__ ("leal %0, %%ecx; " \
|
---|
| 292 | "pushl %%fs; " \
|
---|
| 293 | "pushl $Dos32TIB; " \
|
---|
| 294 | "popl %%fs; " \
|
---|
| 295 | "movl 0(%%ecx), %%eax; " \
|
---|
| 296 | "movl %%eax, %%fs:0; " \
|
---|
| 297 | "popl %%fs; " \
|
---|
| 298 | : : "m"(__seh_frame) \
|
---|
| 299 | : "%eax", "%ecx"); \
|
---|
| 300 | else /* __seh_frame.state == 2 -> execute except block */
|
---|
| 301 |
|
---|
| 302 | #define __finally \
|
---|
| 303 | } \
|
---|
| 304 | /* cause the next state to be 2 */ \
|
---|
| 305 | __seh_frame.state = 1; \
|
---|
| 306 | } \
|
---|
| 307 | else if (__seh_frame.state == 1) { \
|
---|
| 308 | /* execption caught, handle and proceed to the filally block */ \
|
---|
| 309 | __seh_frame.filterResult = EXCEPTION_EXECUTE_HANDLER; \
|
---|
| 310 | __asm__("leal %0, %%ebx; jmp *%1" \
|
---|
| 311 | : : "m"(__seh_frame), "m"(__seh_frame.pHandlerCallback) \
|
---|
| 312 | : "%ebx"); \
|
---|
| 313 | } \
|
---|
| 314 | else if (__seh_frame.state == 3) \
|
---|
| 315 | /* remove exception handler */ \
|
---|
| 316 | __asm__ ("leal %0, %%ecx; " \
|
---|
| 317 | "pushl %%fs; " \
|
---|
| 318 | "pushl $Dos32TIB; " \
|
---|
| 319 | "popl %%fs; " \
|
---|
| 320 | "movl 0(%%ecx), %%eax; " \
|
---|
| 321 | "movl %%eax, %%fs:0; " \
|
---|
| 322 | "popl %%fs; " \
|
---|
| 323 | : : "m"(__seh_frame) \
|
---|
| 324 | : "%eax", "%ecx"); \
|
---|
| 325 | else /* __seh_frame.state == 2 -> execute finally block */
|
---|
| 326 |
|
---|
| 327 | #define __leave \
|
---|
| 328 | /* cause the next state to be 2 */ \
|
---|
| 329 | __seh_frame.state = 1; \
|
---|
| 330 | continue;
|
---|
| 331 |
|
---|
| 332 | #endif /* ODIN_FORCE_WIN32_TIB */
|
---|
| 333 |
|
---|
[21381] | 334 | #else /* defined(__GNUC__) */
|
---|
| 335 |
|
---|
| 336 | #warning "Structured exception handling is not supported for this compiler!"
|
---|
| 337 |
|
---|
| 338 | #endif /* defined(__GNUC__) */
|
---|
| 339 |
|
---|
| 340 | #ifdef __cplusplus
|
---|
| 341 | }
|
---|
| 342 | #endif
|
---|
| 343 |
|
---|
| 344 | #endif /* __EXCPT_H__ */
|
---|
[21387] | 345 |
|
---|