2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010-2013, 2015 André Hentschel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #define WIN32_NO_STATUS
31 #include "wine/exception.h"
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
37 WINE_DECLARE_DEBUG_CHANNEL(relay
);
38 WINE_DECLARE_DEBUG_CHANNEL(threadname
);
40 typedef struct _SCOPE_TABLE
50 } SCOPE_TABLE
, *PSCOPE_TABLE
;
53 /* layering violation: the setjmp buffer is defined in msvcrt, but used by RtlUnwindEx */
54 struct MSVCRT_JUMP_BUFFER
68 unsigned long long D
[8];
72 static void dump_scope_table( ULONG base
, const SCOPE_TABLE
*table
)
76 TRACE( "scope table at %p\n", table
);
77 for (i
= 0; i
< table
->Count
; i
++)
78 TRACE( " %u: %lx-%lx handler %lx target %lx\n", i
,
79 base
+ table
->ScopeRecord
[i
].BeginAddress
,
80 base
+ table
->ScopeRecord
[i
].EndAddress
,
81 base
+ table
->ScopeRecord
[i
].HandlerAddress
,
82 base
+ table
->ScopeRecord
[i
].JumpTarget
);
85 /*******************************************************************
88 static inline BOOL
is_valid_frame( ULONG_PTR frame
)
90 if (frame
& 3) return FALSE
;
91 return ((void *)frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
92 (void *)frame
<= NtCurrentTeb()->Tib
.StackBase
);
96 /**************************************************************************
99 * Incoming r4 contains words to allocate, converting to bytes then return
101 __ASM_GLOBAL_FUNC( __chkstk
, "lsl r4, r4, #2\n\t"
104 /***********************************************************************
105 * RtlCaptureContext (NTDLL.@)
107 __ASM_GLOBAL_FUNC( RtlCaptureContext
,
108 "str r1, [r0, #0x8]\n\t" /* context->R1 */
109 "mov r1, #0x0200000\n\t" /* CONTEXT_ARM */
110 "add r1, r1, #0x7\n\t" /* CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT */
111 "str r1, [r0]\n\t" /* context->ContextFlags */
112 "str SP, [r0, #0x38]\n\t" /* context->Sp */
113 "str LR, [r0, #0x40]\n\t" /* context->Pc */
115 "bfi r1, lr, #5, #1\n\t" /* Thumb bit */
116 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
118 "str r1, [r0, #0x4]\n\t" /* context->R0 */
119 "str r1, [r0, #0x3c]\n\t" /* context->Lr */
121 "stm r0, {r2-r12}\n\t" /* context->R2..R12 */
123 "add r0, #0x44\n\t" /* 0x50 - 0x0c */
124 "vstm r0, {d0-d15}\n\t" /* context->D0-D15 */
129 /**********************************************************************
132 static NTSTATUS
virtual_unwind( ULONG type
, DISPATCHER_CONTEXT
*dispatch
, CONTEXT
*context
)
134 LDR_DATA_TABLE_ENTRY
*module
;
138 dispatch
->ImageBase
= 0;
139 dispatch
->ScopeIndex
= 0;
140 dispatch
->EstablisherFrame
= 0;
141 dispatch
->ControlPc
= context
->Pc
;
143 * TODO: CONTEXT_UNWOUND_TO_CALL should be cleared if unwound past a
146 dispatch
->ControlPcIsUnwound
= (context
->ContextFlags
& CONTEXT_UNWOUND_TO_CALL
) != 0;
147 pc
= context
->Pc
- (dispatch
->ControlPcIsUnwound
? 2 : 0);
149 /* first look for PE exception information */
151 if ((dispatch
->FunctionEntry
= lookup_function_info(pc
,
152 (ULONG_PTR
*)&dispatch
->ImageBase
, &module
)))
154 dispatch
->LanguageHandler
= RtlVirtualUnwind( type
, dispatch
->ImageBase
, pc
,
155 dispatch
->FunctionEntry
, context
,
156 &dispatch
->HandlerData
, (ULONG_PTR
*)&dispatch
->EstablisherFrame
,
158 return STATUS_SUCCESS
;
161 /* then look for host system exception information */
163 if (!module
|| (module
->Flags
& LDR_WINE_INTERNAL
))
165 struct unwind_builtin_dll_params params
= { type
, dispatch
, context
};
167 status
= WINE_UNIX_CALL( unix_unwind_builtin_dll
, ¶ms
);
168 if (status
!= STATUS_SUCCESS
) return status
;
170 if (dispatch
->EstablisherFrame
)
172 dispatch
->FunctionEntry
= NULL
;
173 if (dispatch
->LanguageHandler
&& !module
)
175 FIXME( "calling personality routine in system library not supported yet\n" );
176 dispatch
->LanguageHandler
= NULL
;
178 return STATUS_SUCCESS
;
183 status
= context
->Pc
!= context
->Lr
?
184 STATUS_SUCCESS
: STATUS_INVALID_DISPOSITION
;
185 WARN( "exception data not found in %s for %p, LR %p, status %lx\n",
186 debugstr_w(module
->BaseDllName
.Buffer
), (void*) context
->Pc
,
187 (void*) context
->Lr
, status
);
188 dispatch
->EstablisherFrame
= context
->Sp
;
189 dispatch
->LanguageHandler
= NULL
;
190 context
->Pc
= context
->Lr
;
191 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
195 dispatch
->EstablisherFrame
= context
->Sp
;
196 dispatch
->LanguageHandler
= NULL
;
197 context
->Pc
= context
->Lr
;
198 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
199 return STATUS_SUCCESS
;
203 struct unwind_exception_frame
205 EXCEPTION_REGISTRATION_RECORD frame
;
206 DISPATCHER_CONTEXT
*dispatch
;
209 /**********************************************************************
210 * unwind_exception_handler
212 * Handler for exceptions happening while calling an unwind handler.
214 static DWORD __cdecl
unwind_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
215 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
217 struct unwind_exception_frame
*unwind_frame
= (struct unwind_exception_frame
*)frame
;
218 DISPATCHER_CONTEXT
*dispatch
= (DISPATCHER_CONTEXT
*)dispatcher
;
220 /* copy the original dispatcher into the current one, except for the TargetIp */
221 dispatch
->ControlPc
= unwind_frame
->dispatch
->ControlPc
;
222 dispatch
->ImageBase
= unwind_frame
->dispatch
->ImageBase
;
223 dispatch
->FunctionEntry
= unwind_frame
->dispatch
->FunctionEntry
;
224 dispatch
->EstablisherFrame
= unwind_frame
->dispatch
->EstablisherFrame
;
225 dispatch
->ContextRecord
= unwind_frame
->dispatch
->ContextRecord
;
226 dispatch
->LanguageHandler
= unwind_frame
->dispatch
->LanguageHandler
;
227 dispatch
->HandlerData
= unwind_frame
->dispatch
->HandlerData
;
228 dispatch
->HistoryTable
= unwind_frame
->dispatch
->HistoryTable
;
229 dispatch
->ScopeIndex
= unwind_frame
->dispatch
->ScopeIndex
;
230 TRACE( "detected collided unwind\n" );
231 return ExceptionCollidedUnwind
;
234 /**********************************************************************
235 * call_unwind_handler
237 * Call a single unwind handler.
239 static DWORD
call_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
)
241 struct unwind_exception_frame frame
;
244 frame
.frame
.Handler
= unwind_exception_handler
;
245 frame
.dispatch
= dispatch
;
246 __wine_push_frame( &frame
.frame
);
248 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
249 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
250 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
251 TRACE( "handler %p returned %lx\n", dispatch
->LanguageHandler
, res
);
253 __wine_pop_frame( &frame
.frame
);
257 case ExceptionContinueSearch
:
258 case ExceptionCollidedUnwind
:
261 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
269 /**********************************************************************
270 * call_teb_unwind_handler
272 * Call a single unwind handler from the TEB chain.
274 static DWORD
call_teb_unwind_handler( EXCEPTION_RECORD
*rec
, DISPATCHER_CONTEXT
*dispatch
,
275 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
279 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
280 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
281 res
= teb_frame
->Handler( rec
, teb_frame
, dispatch
->ContextRecord
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
282 TRACE( "handler at %p returned %lu\n", teb_frame
->Handler
, res
);
286 case ExceptionContinueSearch
:
287 case ExceptionCollidedUnwind
:
290 raise_status( STATUS_INVALID_DISPOSITION
, rec
);
298 static DWORD __cdecl
nested_exception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
299 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
301 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
302 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
304 return ExceptionContinueSearch
;
308 /**********************************************************************
311 * Call a single exception handler.
312 * FIXME: Handle nested exceptions.
314 static DWORD
call_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
)
316 EXCEPTION_REGISTRATION_RECORD frame
;
319 frame
.Handler
= nested_exception_handler
;
320 __wine_push_frame( &frame
);
322 TRACE( "calling handler %p (rec=%p, frame=0x%lx context=%p, dispatch=%p)\n",
323 dispatch
->LanguageHandler
, rec
, dispatch
->EstablisherFrame
, dispatch
->ContextRecord
, dispatch
);
324 res
= dispatch
->LanguageHandler( rec
, (void *)dispatch
->EstablisherFrame
, context
, dispatch
);
325 TRACE( "handler at %p returned %lu\n", dispatch
->LanguageHandler
, res
);
327 rec
->ExceptionFlags
&= EH_NONCONTINUABLE
;
328 __wine_pop_frame( &frame
);
333 /**********************************************************************
336 * Call a single exception handler from the TEB chain.
337 * FIXME: Handle nested exceptions.
339 static DWORD
call_teb_handler( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, DISPATCHER_CONTEXT
*dispatch
,
340 EXCEPTION_REGISTRATION_RECORD
*teb_frame
)
344 TRACE( "calling TEB handler %p (rec=%p, frame=%p context=%p, dispatch=%p)\n",
345 teb_frame
->Handler
, rec
, teb_frame
, dispatch
->ContextRecord
, dispatch
);
346 res
= teb_frame
->Handler( rec
, teb_frame
, context
, (EXCEPTION_REGISTRATION_RECORD
**)dispatch
);
347 TRACE( "handler at %p returned %lu\n", teb_frame
->Handler
, res
);
352 /**********************************************************************
353 * call_function_handlers
355 * Call the per-function handlers.
357 static NTSTATUS
call_function_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*orig_context
)
359 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
360 UNWIND_HISTORY_TABLE table
;
361 DISPATCHER_CONTEXT dispatch
;
362 CONTEXT context
, prev_context
;
365 context
= *orig_context
;
366 dispatch
.TargetPc
= 0;
367 dispatch
.ContextRecord
= &context
;
368 dispatch
.HistoryTable
= &table
;
369 prev_context
= context
;
370 dispatch
.NonVolatileRegisters
= (BYTE
*)&prev_context
.R4
;
374 status
= virtual_unwind( UNW_FLAG_EHANDLER
, &dispatch
, &context
);
375 if (status
!= STATUS_SUCCESS
) return status
;
378 if (!dispatch
.EstablisherFrame
) break;
380 if (!is_valid_frame( dispatch
.EstablisherFrame
))
382 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
383 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
384 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
388 if (dispatch
.LanguageHandler
)
390 switch (call_handler( rec
, orig_context
, &dispatch
))
392 case ExceptionContinueExecution
:
393 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
394 return STATUS_SUCCESS
;
395 case ExceptionContinueSearch
:
397 case ExceptionNestedException
:
398 FIXME( "nested exception\n" );
400 case ExceptionCollidedUnwind
: {
403 context
= *dispatch
.ContextRecord
;
404 dispatch
.ContextRecord
= &context
;
405 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
406 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
407 &context
, (PVOID
*)&dispatch
.HandlerData
, &frame
, NULL
);
411 return STATUS_INVALID_DISPOSITION
;
414 /* hack: call wine handlers registered in the tib list */
415 else while ((DWORD
)teb_frame
< context
.Sp
)
417 TRACE( "found wine frame %p rsp %lx handler %p\n",
418 teb_frame
, context
.Sp
, teb_frame
->Handler
);
419 dispatch
.EstablisherFrame
= (DWORD
)teb_frame
;
420 switch (call_teb_handler( rec
, orig_context
, &dispatch
, teb_frame
))
422 case ExceptionContinueExecution
:
423 if (rec
->ExceptionFlags
& EH_NONCONTINUABLE
) return STATUS_NONCONTINUABLE_EXCEPTION
;
424 return STATUS_SUCCESS
;
425 case ExceptionContinueSearch
:
427 case ExceptionNestedException
:
428 FIXME( "nested exception\n" );
430 case ExceptionCollidedUnwind
: {
433 context
= *dispatch
.ContextRecord
;
434 dispatch
.ContextRecord
= &context
;
435 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
436 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
437 &context
, (PVOID
*)&dispatch
.HandlerData
, &frame
, NULL
);
438 teb_frame
= teb_frame
->Prev
;
442 return STATUS_INVALID_DISPOSITION
;
444 teb_frame
= teb_frame
->Prev
;
447 if (context
.Sp
== (DWORD
)NtCurrentTeb()->Tib
.StackBase
) break;
448 prev_context
= context
;
450 return STATUS_UNHANDLED_EXCEPTION
;
454 /*******************************************************************
455 * KiUserExceptionDispatcher (NTDLL.@)
457 NTSTATUS WINAPI
KiUserExceptionDispatcher( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
462 TRACE( "code=%lx flags=%lx addr=%p pc=%08lx\n",
463 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
, context
->Pc
);
464 for (c
= 0; c
< rec
->NumberParameters
; c
++)
465 TRACE( " info[%ld]=%08Ix\n", c
, rec
->ExceptionInformation
[c
] );
467 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
469 if (rec
->ExceptionInformation
[1] >> 16)
470 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
471 rec
->ExceptionAddress
,
472 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
474 MESSAGE( "wine: Call from %p to unimplemented function %s.%Id, aborting\n",
475 rec
->ExceptionAddress
,
476 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
478 else if (rec
->ExceptionCode
== EXCEPTION_WINE_NAME_THREAD
&& rec
->ExceptionInformation
[0] == 0x1000)
480 if ((DWORD
)rec
->ExceptionInformation
[2] == -1 || (DWORD
)rec
->ExceptionInformation
[2] == GetCurrentThreadId())
481 WARN_(threadname
)( "Thread renamed to %s\n", debugstr_a((char *)rec
->ExceptionInformation
[1]) );
483 WARN_(threadname
)( "Thread ID %04lx renamed to %s\n", (DWORD
)rec
->ExceptionInformation
[2],
484 debugstr_a((char *)rec
->ExceptionInformation
[1]) );
486 set_native_thread_name((DWORD
)rec
->ExceptionInformation
[2], (char *)rec
->ExceptionInformation
[1]);
488 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_C
)
490 WARN( "%s\n", debugstr_an((char *)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
492 else if (rec
->ExceptionCode
== DBG_PRINTEXCEPTION_WIDE_C
)
494 WARN( "%s\n", debugstr_wn((WCHAR
*)rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[0] - 1) );
498 if (rec
->ExceptionCode
== STATUS_ASSERTION_FAILURE
)
499 ERR( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
501 WARN( "%s exception (code=%lx) raised\n", debugstr_exception_code(rec
->ExceptionCode
), rec
->ExceptionCode
);
503 TRACE( " r0=%08lx r1=%08lx r2=%08lx r3=%08lx r4=%08lx r5=%08lx\n",
504 context
->R0
, context
->R1
, context
->R2
, context
->R3
, context
->R4
, context
->R5
);
505 TRACE( " r6=%08lx r7=%08lx r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
506 context
->R6
, context
->R7
, context
->R8
, context
->R9
, context
->R10
, context
->R11
);
507 TRACE( " r12=%08lx sp=%08lx lr=%08lx pc=%08lx cpsr=%08lx\n",
508 context
->R12
, context
->Sp
, context
->Lr
, context
->Pc
, context
->Cpsr
);
511 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
512 NtContinue( context
, FALSE
);
514 if ((status
= call_function_handlers( rec
, context
)) == STATUS_SUCCESS
)
515 NtContinue( context
, FALSE
);
517 if (status
!= STATUS_UNHANDLED_EXCEPTION
) RtlRaiseStatus( status
);
518 return NtRaiseException( rec
, context
, FALSE
);
522 /*******************************************************************
523 * KiUserApcDispatcher (NTDLL.@)
525 void WINAPI
KiUserApcDispatcher( CONTEXT
*context
, ULONG_PTR ctx
, ULONG_PTR arg1
, ULONG_PTR arg2
,
528 func( ctx
, arg1
, arg2
);
529 NtContinue( context
, TRUE
);
533 /*******************************************************************
534 * KiUserCallbackDispatcher (NTDLL.@)
536 void WINAPI
KiUserCallbackDispatcher( ULONG id
, void *args
, ULONG len
)
542 NTSTATUS (WINAPI
*func
)(void *, ULONG
) = ((void **)NtCurrentTeb()->Peb
->KernelCallbackTable
)[id
];
543 status
= NtCallbackReturn( NULL
, 0, func( args
, len
));
547 ERR_(seh
)( "ignoring exception\n" );
548 status
= NtCallbackReturn( 0, 0, 0 );
552 RtlRaiseStatus( status
);
556 /***********************************************************************
557 * Definitions for Win32 unwind tables
562 DWORD function_length
: 18;
571 struct unwind_info_ext
578 struct unwind_info_epilog
586 static const BYTE unwind_code_len
[256] =
588 /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
589 /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
590 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
591 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
592 /* 80 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
593 /* a0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
594 /* c0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
595 /* e0 */ 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,3,4,1,1,1,1,1
598 static const BYTE unwind_instr_len
[256] =
600 /* 00 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
601 /* 20 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
602 /* 40 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
603 /* 60 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
604 /* 80 */ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
605 /* a0 */ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
606 /* c0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,
607 /* e0 */ 4,4,4,4,4,4,4,4,4,4,4,4,2,2,2,4,0,0,0,0,0,4,4,2,2,4,4,2,4,2,4,0
610 /***********************************************************************
613 static unsigned int get_sequence_len( BYTE
*ptr
, BYTE
*end
, int include_end
)
615 unsigned int ret
= 0;
621 if (*ptr
<= 0xfe && include_end
)
622 ret
+= unwind_instr_len
[*ptr
];
625 ret
+= unwind_instr_len
[*ptr
];
626 ptr
+= unwind_code_len
[*ptr
];
632 /***********************************************************************
635 static void pop_regs_mask( int mask
, CONTEXT
*context
,
636 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
639 for (i
= 0; i
<= 12; i
++)
641 if (!(mask
& (1 << i
))) continue;
642 if (ptrs
&& i
>= 4 && i
<= 11) (&ptrs
->R4
)[i
- 4] = (DWORD
*)context
->Sp
;
643 if (i
>= 4) (&context
->R0
)[i
] = *(DWORD
*)context
->Sp
;
649 /***********************************************************************
652 static void pop_regs_range( int last
, CONTEXT
*context
,
653 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
656 for (i
= 4; i
<= last
; i
++)
658 if (ptrs
) (&ptrs
->R4
)[i
- 4] = (DWORD
*)context
->Sp
;
659 (&context
->R0
)[i
] = *(DWORD
*)context
->Sp
;
665 /***********************************************************************
668 static void pop_lr( int increment
, CONTEXT
*context
,
669 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
671 if (ptrs
) ptrs
->Lr
= (DWORD
*)context
->Sp
;
672 context
->Lr
= *(DWORD
*)context
->Sp
;
673 context
->Sp
+= increment
;
677 /***********************************************************************
680 static void pop_fpregs_range( int first
, int last
, CONTEXT
*context
,
681 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
684 for (i
= first
; i
<= last
; i
++)
686 if (ptrs
&& i
>= 8 && i
<= 15) (&ptrs
->D8
)[i
- 8] = (ULONGLONG
*)context
->Sp
;
687 context
->D
[i
] = *(ULONGLONG
*)context
->Sp
;
693 /***********************************************************************
694 * process_unwind_codes
696 static void process_unwind_codes( BYTE
*ptr
, BYTE
*end
, CONTEXT
*context
,
697 KNONVOLATILE_CONTEXT_POINTERS
*ptrs
, int skip
)
699 unsigned int val
, len
;
703 while (ptr
< end
&& skip
)
705 if (*ptr
>= 0xfd) break;
706 skip
-= unwind_instr_len
[*ptr
];
707 ptr
+= unwind_code_len
[*ptr
];
712 len
= unwind_code_len
[*ptr
];
713 if (ptr
+ len
> end
) break;
715 for (i
= 0; i
< len
; i
++)
716 val
= (val
<< 8) | ptr
[i
];
718 if (*ptr
<= 0x7f) /* add sp, sp, #x */
719 context
->Sp
+= 4 * (val
& 0x7f);
720 else if (*ptr
<= 0xbf) /* pop {r0-r12,lr} */
722 pop_regs_mask( val
& 0x1fff, context
, ptrs
);
724 pop_lr( 4, context
, ptrs
);
726 else if (*ptr
<= 0xcf) /* mov sp, rX */
727 context
->Sp
= (&context
->R0
)[val
& 0x0f];
728 else if (*ptr
<= 0xd7) /* pop {r4-rX,lr} */
730 pop_regs_range( (val
& 0x03) + 4, context
, ptrs
);
732 pop_lr( 4, context
, ptrs
);
734 else if (*ptr
<= 0xdf) /* pop {r4-rX,lr} */
736 pop_regs_range( (val
& 0x03) + 8, context
, ptrs
);
738 pop_lr( 4, context
, ptrs
);
740 else if (*ptr
<= 0xe7) /* vpop {d8-dX} */
741 pop_fpregs_range( 8, (val
& 0x07) + 8, context
, ptrs
);
742 else if (*ptr
<= 0xeb) /* add sp, sp, #x */
743 context
->Sp
+= 4 * (val
& 0x3ff);
744 else if (*ptr
<= 0xed) /* pop {r0-r12,lr} */
746 pop_regs_mask( val
& 0xff, context
, ptrs
);
748 pop_lr( 4, context
, ptrs
);
750 else if (*ptr
<= 0xee) /* Microsoft-specific 0x00-0x0f, Available 0x10-0xff */
751 WARN( "unsupported code %02x\n", *ptr
);
752 else if (*ptr
<= 0xef && ((val
& 0xff) <= 0x0f)) /* ldr lr, [sp], #x */
753 pop_lr( 4 * (val
& 0x0f), context
, ptrs
);
754 else if (*ptr
== 0xf4) /* Custom private (unallocated) opcode, saved a full CONTEXT on the stack */
755 memcpy( context
, (DWORD
*)context
->Sp
, sizeof(CONTEXT
) );
756 else if (*ptr
<= 0xf4) /* Available */
757 WARN( "unsupported code %02x\n", *ptr
);
758 else if (*ptr
<= 0xf5) /* vpop {dS-dE} */
759 pop_fpregs_range( (val
& 0xf0) >> 4, (val
& 0x0f), context
, ptrs
);
760 else if (*ptr
<= 0xf6) /* vpop {dS-dE} */
761 pop_fpregs_range( ((val
& 0xf0) >> 4) + 16, (val
& 0x0f) + 16, context
, ptrs
);
762 else if (*ptr
== 0xf7 || *ptr
== 0xf9) /* add sp, sp, #x */
763 context
->Sp
+= 4 * (val
& 0xffff);
764 else if (*ptr
== 0xf8 || *ptr
== 0xfa) /* add sp, sp, #x */
765 context
->Sp
+= 4 * (val
& 0xffffff);
766 else if (*ptr
<= 0xfc) /* nop */
776 /***********************************************************************
779 static void *unwind_packed_data( ULONG_PTR base
, ULONG_PTR pc
, RUNTIME_FUNCTION
*func
,
780 CONTEXT
*context
, KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
783 int pf
= 0, ef
= 0, fpoffset
= 0, stack
= func
->StackAdjust
;
784 int prologue_regmask
= 0;
785 int epilogue_regmask
= 0;
786 unsigned int offset
, len
;
787 BYTE prologue
[10], *prologue_end
, epilogue
[20], *epilogue_end
;
789 TRACE( "function %lx-%lx: len=%#x flag=%x ret=%u H=%u reg=%u R=%u L=%u C=%u stackadjust=%x\n",
790 base
+ func
->BeginAddress
, base
+ func
->BeginAddress
+ func
->FunctionLength
* 2,
791 func
->FunctionLength
, func
->Flag
, func
->Ret
,
792 func
->H
, func
->Reg
, func
->R
, func
->L
, func
->C
, func
->StackAdjust
);
794 offset
= (pc
- base
) - func
->BeginAddress
;
795 if (func
->StackAdjust
>= 0x03f4)
797 pf
= func
->StackAdjust
& 0x04;
798 ef
= func
->StackAdjust
& 0x08;
799 stack
= (func
->StackAdjust
& 3) + 1;
804 int first
= 4, last
= func
->Reg
+ 4;
807 first
= (~func
->StackAdjust
) & 3;
811 for (i
= first
; i
<= last
; i
++)
812 prologue_regmask
|= 1 << i
;
813 fpoffset
= last
+ 1 - first
;
818 int first
= 4, last
= func
->Reg
+ 4;
821 first
= (~func
->StackAdjust
) & 3;
825 for (i
= first
; i
<= last
; i
++)
826 epilogue_regmask
|= 1 << i
;
831 prologue_regmask
|= 1 << 11;
832 epilogue_regmask
|= 1 << 11;
837 prologue_regmask
|= 1 << 14; /* lr */
839 epilogue_regmask
|= 1 << 14; /* lr */
841 epilogue_regmask
|= 1 << 15; /* pc */
844 /* Synthesize prologue opcodes */
849 prologue
[pos
++] = stack
; /* sub sp, sp, #x */
853 prologue
[pos
++] = 0xe8 | (stack
>> 8); /* sub.w sp, sp, #x */
854 prologue
[pos
++] = stack
& 0xff;
858 if (func
->R
&& func
->Reg
!= 7)
859 prologue
[pos
++] = 0xe0 | func
->Reg
; /* vpush {d8-dX} */
861 if (func
->C
&& fpoffset
== 0)
862 prologue
[pos
++] = 0xfb; /* mov r11, sp - handled as nop16 */
864 prologue
[pos
++] = 0xfc; /* add r11, sp, #x - handled as nop32 */
866 if (prologue_regmask
& 0xf00) /* r8-r11 set */
868 int bitmask
= prologue_regmask
& 0x1fff;
869 if (prologue_regmask
& (1 << 14)) /* lr */
871 prologue
[pos
++] = 0x80 | (bitmask
>> 8); /* push.w {r0-r12,lr} */
872 prologue
[pos
++] = bitmask
& 0xff;
874 else if (prologue_regmask
) /* r0-r7, lr set */
876 int bitmask
= prologue_regmask
& 0xff;
877 if (prologue_regmask
& (1 << 14)) /* lr */
879 prologue
[pos
++] = 0xec | (bitmask
>> 8); /* push {r0-r7,lr} */
880 prologue
[pos
++] = bitmask
& 0xff;
884 prologue
[pos
++] = 0x04; /* push {r0-r3} - handled as sub sp, sp, #16 */
886 prologue
[pos
++] = 0xff; /* end */
887 prologue_end
= &prologue
[pos
];
889 /* Synthesize epilogue opcodes */
895 epilogue
[pos
++] = stack
; /* sub sp, sp, #x */
899 epilogue
[pos
++] = 0xe8 | (stack
>> 8); /* sub.w sp, sp, #x */
900 epilogue
[pos
++] = stack
& 0xff;
904 if (func
->R
&& func
->Reg
!= 7)
905 epilogue
[pos
++] = 0xe0 | func
->Reg
; /* vpush {d8-dX} */
907 if (epilogue_regmask
& 0x7f00) /* r8-r11, lr set */
909 int bitmask
= epilogue_regmask
& 0x1fff;
910 if (epilogue_regmask
& (3 << 14)) /* lr or pc */
912 epilogue
[pos
++] = 0x80 | (bitmask
>> 8); /* push.w {r0-r12,lr} */
913 epilogue
[pos
++] = bitmask
& 0xff;
915 else if (epilogue_regmask
) /* r0-r7, pc set */
917 int bitmask
= epilogue_regmask
& 0xff;
918 if (epilogue_regmask
& (1 << 15)) /* pc */
919 bitmask
|= 0x100; /* lr */
920 epilogue
[pos
++] = 0xec | (bitmask
>> 8); /* push {r0-r7,lr} */
921 epilogue
[pos
++] = bitmask
& 0xff;
924 if (func
->H
&& !(func
->L
&& func
->Ret
== 0))
925 epilogue
[pos
++] = 0x04; /* add sp, sp, #16 */
926 else if (func
->H
&& (func
->L
&& func
->Ret
== 0))
928 epilogue
[pos
++] = 0xef; /* ldr lr, [sp], #20 */
933 epilogue
[pos
++] = 0xfd; /* bx lr */
934 else if (func
->Ret
== 2)
935 epilogue
[pos
++] = 0xfe; /* b address */
937 epilogue
[pos
++] = 0xff; /* end */
938 epilogue_end
= &epilogue
[pos
];
940 if (func
->Flag
== 1 && offset
< 4 * (prologue_end
- prologue
)) {
942 len
= get_sequence_len( prologue
, prologue_end
, 0 );
945 process_unwind_codes( prologue
, prologue_end
, context
, ptrs
, len
- offset
);
950 if (func
->Ret
!= 3 && 2 * func
->FunctionLength
- offset
<= 4 * (epilogue_end
- epilogue
)) {
952 len
= get_sequence_len( epilogue
, epilogue_end
, 1 );
953 if (offset
>= 2 * func
->FunctionLength
- len
)
955 process_unwind_codes( epilogue
, epilogue_end
, context
, ptrs
, offset
- (2 * func
->FunctionLength
- len
) );
960 /* Execute full prologue */
961 process_unwind_codes( prologue
, prologue_end
, context
, ptrs
, 0 );
967 /***********************************************************************
970 static void *unwind_full_data( ULONG_PTR base
, ULONG_PTR pc
, RUNTIME_FUNCTION
*func
,
971 CONTEXT
*context
, PVOID
*handler_data
, KNONVOLATILE_CONTEXT_POINTERS
*ptrs
)
973 struct unwind_info
*info
;
974 struct unwind_info_epilog
*info_epilog
;
975 unsigned int i
, codes
, epilogs
, len
, offset
;
979 info
= (struct unwind_info
*)((char *)base
+ func
->UnwindData
);
981 epilogs
= info
->epilog
;
983 if (!codes
&& !epilogs
)
985 struct unwind_info_ext
*infoex
= data
;
986 codes
= infoex
->codes
;
987 epilogs
= infoex
->epilog
;
991 if (!info
->e
) data
= info_epilog
+ epilogs
;
993 offset
= (pc
- base
) - func
->BeginAddress
;
994 end
= (BYTE
*)data
+ codes
* 4;
996 TRACE( "function %lx-%lx: len=%#x ver=%u X=%u E=%u F=%u epilogs=%u codes=%u\n",
997 base
+ func
->BeginAddress
, base
+ func
->BeginAddress
+ info
->function_length
* 2,
998 info
->function_length
, info
->version
, info
->x
, info
->e
, info
->f
, epilogs
, codes
* 4 );
1000 /* check for prolog */
1001 if (offset
< codes
* 4 * 4 && !info
->f
)
1003 len
= get_sequence_len( data
, end
, 0 );
1006 process_unwind_codes( data
, end
, context
, ptrs
, len
- offset
);
1011 /* check for epilog */
1014 for (i
= 0; i
< epilogs
; i
++)
1016 /* TODO: Currently not checking epilogue conditions. */
1017 if (offset
< 2 * info_epilog
[i
].offset
) break;
1018 if (offset
- 2 * info_epilog
[i
].offset
< (codes
* 4 - info_epilog
[i
].index
) * 4)
1020 BYTE
*ptr
= (BYTE
*)data
+ info_epilog
[i
].index
;
1021 len
= get_sequence_len( ptr
, end
, 1 );
1022 if (offset
<= 2 * info_epilog
[i
].offset
+ len
)
1024 process_unwind_codes( ptr
, end
, context
, ptrs
, offset
- 2 * info_epilog
[i
].offset
);
1030 else if (2 * info
->function_length
- offset
<= (codes
* 4 - epilogs
) * 4)
1032 BYTE
*ptr
= (BYTE
*)data
+ epilogs
;
1033 len
= get_sequence_len( ptr
, end
, 1 );
1034 if (offset
>= 2 * info
->function_length
- len
)
1036 process_unwind_codes( ptr
, end
, context
, ptrs
, offset
- (2 * info
->function_length
- len
) );
1041 process_unwind_codes( data
, end
, context
, ptrs
, 0 );
1043 /* get handler since we are inside the main code */
1046 DWORD
*handler_rva
= (DWORD
*)data
+ codes
;
1047 *handler_data
= handler_rva
+ 1;
1048 return (char *)base
+ *handler_rva
;
1053 /***********************************************************************
1054 * RtlVirtualUnwind (NTDLL.@)
1056 PVOID WINAPI
RtlVirtualUnwind( ULONG type
, ULONG_PTR base
, ULONG_PTR pc
,
1057 RUNTIME_FUNCTION
*func
, CONTEXT
*context
,
1058 PVOID
*handler_data
, ULONG_PTR
*frame_ret
,
1059 KNONVOLATILE_CONTEXT_POINTERS
*ctx_ptr
)
1063 TRACE( "type %lx pc %Ix sp %lx func %lx\n", type
, pc
, context
->Sp
, base
+ func
->BeginAddress
);
1065 *handler_data
= NULL
;
1069 handler
= unwind_packed_data( base
, pc
, func
, context
, ctx_ptr
);
1071 handler
= unwind_full_data( base
, pc
, func
, context
, handler_data
, ctx_ptr
);
1073 TRACE( "ret: lr=%lx sp=%lx handler=%p\n", context
->Lr
, context
->Sp
, handler
);
1075 context
->Pc
= context
->Lr
;
1076 context
->ContextFlags
|= CONTEXT_UNWOUND_TO_CALL
;
1077 *frame_ret
= context
->Sp
;
1082 /**********************************************************************
1083 * call_consolidate_callback
1085 * Wrapper function to call a consolidate callback from a fake frame.
1086 * If the callback executes RtlUnwindEx (like for example done in C++ handlers),
1087 * we have to skip all frames which were already processed. To do that we
1088 * trick the unwinding functions into thinking the call came from somewhere
1089 * else. All CFI instructions are either DW_CFA_def_cfa_expression or
1090 * DW_CFA_expression, and the expressions have the following format:
1092 * DW_OP_breg13; sleb128 <OFFSET> | Load SP + struct member offset
1093 * [DW_OP_deref] | Dereference, only for CFA
1095 extern void * WINAPI
call_consolidate_callback( CONTEXT
*context
,
1096 void *(CALLBACK
*callback
)(EXCEPTION_RECORD
*),
1097 EXCEPTION_RECORD
*rec
);
1098 __ASM_GLOBAL_FUNC( call_consolidate_callback
,
1099 "push {r0-r2,lr}\n\t"
1100 __ASM_SEH(".seh_nop\n\t")
1101 "sub sp, sp, #0x1a0\n\t"
1102 __ASM_SEH(".seh_nop\n\t")
1104 __ASM_SEH(".seh_nop\n\t")
1106 __ASM_SEH(".seh_nop\n\t")
1107 "mov r2, #0x1a0\n\t"
1108 __ASM_SEH(".seh_nop_w\n\t")
1109 "bl " __ASM_NAME("memcpy") "\n\t"
1110 __ASM_SEH(".seh_custom 0xf4\n\t") /* A custom (unallocated) SEH opcode for CONTEXT on stack */
1111 __ASM_SEH(".seh_endprologue\n\t")
1112 __ASM_CFI(".cfi_def_cfa 13, 0\n\t")
1113 __ASM_CFI(".cfi_escape 0x0f,0x04,0x7d,0xb8,0x00,0x06\n\t") /* DW_CFA_def_cfa_expression: DW_OP_breg13 + 56, DW_OP_deref */
1114 __ASM_CFI(".cfi_escape 0x10,0x04,0x02,0x7d,0x14\n\t") /* DW_CFA_expression: R4 DW_OP_breg13 + 20 */
1115 __ASM_CFI(".cfi_escape 0x10,0x05,0x02,0x7d,0x18\n\t") /* DW_CFA_expression: R5 DW_OP_breg13 + 24 */
1116 __ASM_CFI(".cfi_escape 0x10,0x06,0x02,0x7d,0x1c\n\t") /* DW_CFA_expression: R6 DW_OP_breg13 + 28 */
1117 __ASM_CFI(".cfi_escape 0x10,0x07,0x02,0x7d,0x20\n\t") /* DW_CFA_expression: R7 DW_OP_breg13 + 32 */
1118 __ASM_CFI(".cfi_escape 0x10,0x08,0x02,0x7d,0x24\n\t") /* DW_CFA_expression: R8 DW_OP_breg13 + 36 */
1119 __ASM_CFI(".cfi_escape 0x10,0x09,0x02,0x7d,0x28\n\t") /* DW_CFA_expression: R9 DW_OP_breg13 + 40 */
1120 __ASM_CFI(".cfi_escape 0x10,0x0a,0x02,0x7d,0x2c\n\t") /* DW_CFA_expression: R10 DW_OP_breg13 + 44 */
1121 __ASM_CFI(".cfi_escape 0x10,0x0b,0x02,0x7d,0x30\n\t") /* DW_CFA_expression: R11 DW_OP_breg13 + 48 */
1122 __ASM_CFI(".cfi_escape 0x10,0x0e,0x03,0x7d,0xc0,0x00\n\t") /* DW_CFA_expression: LR DW_OP_breg13 + 64 (PC) */
1123 /* Libunwind doesn't support the registers D8-D15 like this */
1125 __ASM_CFI(".cfi_escape 0x10,0x88,0x02,0x03,0x7d,0x90,0x01\n\t") /* DW_CFA_expression: D8 DW_OP_breg13 + 144 */
1126 __ASM_CFI(".cfi_escape 0x10,0x89,0x02,0x03,0x7d,0x98,0x01\n\t") /* DW_CFA_expression: D9 DW_OP_breg13 + 152 */
1127 __ASM_CFI(".cfi_escape 0x10,0x8a,0x02,0x03,0x7d,0xa0,0x01\n\t") /* DW_CFA_expression: D10 DW_OP_breg13 + 160 */
1128 __ASM_CFI(".cfi_escape 0x10,0x8b,0x02,0x03,0x7d,0xa8,0x01\n\t") /* DW_CFA_expression: D11 DW_OP_breg13 + 168 */
1129 __ASM_CFI(".cfi_escape 0x10,0x8c,0x02,0x03,0x7d,0xb0,0x01\n\t") /* DW_CFA_expression: D12 DW_OP_breg13 + 176 */
1130 __ASM_CFI(".cfi_escape 0x10,0x8d,0x02,0x03,0x7d,0xb8,0x01\n\t") /* DW_CFA_expression: D13 DW_OP_breg13 + 184 */
1131 __ASM_CFI(".cfi_escape 0x10,0x8e,0x02,0x03,0x7d,0xc0,0x01\n\t") /* DW_CFA_expression: D14 DW_OP_breg13 + 192 */
1132 __ASM_CFI(".cfi_escape 0x10,0x8f,0x02,0x03,0x7d,0xc8,0x01\n\t") /* DW_CFA_expression: D15 DW_OP_breg13 + 200 */
1134 /* These EHABI opcodes are to be read bottom up - they
1135 * restore relevant registers from the CONTEXT. */
1136 __ASM_EHABI(".save {sp}\n\t") /* Restore Sp last */
1137 __ASM_EHABI(".pad #-(0x80 + 0x0c + 0x0c)\n\t") /* Move back across D0-D15, Cpsr, Fpscr, Padding, Pc, Lr and Sp */
1138 __ASM_EHABI(".vsave {d8-d15}\n\t")
1139 __ASM_EHABI(".pad #0x40\n\t") /* Skip past D0-D7 */
1140 __ASM_EHABI(".pad #0x0c\n\t") /* Skip past Cpsr, Fpscr and Padding */
1141 __ASM_EHABI(".save {lr, pc}\n\t")
1142 __ASM_EHABI(".pad #0x08\n\t") /* Skip past R12 and Sp - Sp is restored last */
1143 __ASM_EHABI(".save {r4-r11}\n\t")
1144 __ASM_EHABI(".pad #0x14\n\t") /* Skip past ContextFlags and R0-R3 */
1146 "ldrd r1, r2, [sp, #0x1a4]\n\t"
1149 "add sp, sp, #0x1ac\n\t"
1154 /*******************************************************************
1155 * RtlRestoreContext (NTDLL.@)
1157 void CDECL
RtlRestoreContext( CONTEXT
*context
, EXCEPTION_RECORD
*rec
)
1159 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1161 if (rec
&& rec
->ExceptionCode
== STATUS_LONGJUMP
&& rec
->NumberParameters
>= 1)
1163 struct MSVCRT_JUMP_BUFFER
*jmp
= (struct MSVCRT_JUMP_BUFFER
*)rec
->ExceptionInformation
[0];
1166 for (i
= 4; i
<= 11; i
++)
1167 (&context
->R4
)[i
-4] = (&jmp
->R4
)[i
-4];
1168 context
->Lr
= jmp
->Pc
;
1169 context
->Sp
= jmp
->Sp
;
1170 context
->Fpscr
= jmp
->Fpscr
;
1172 for (i
= 0; i
< 8; i
++)
1173 context
->D
[8+i
] = jmp
->D
[i
];
1175 else if (rec
&& rec
->ExceptionCode
== STATUS_UNWIND_CONSOLIDATE
&& rec
->NumberParameters
>= 1)
1177 PVOID (CALLBACK
*consolidate
)(EXCEPTION_RECORD
*) = (void *)rec
->ExceptionInformation
[0];
1178 TRACE( "calling consolidate callback %p (rec=%p)\n", consolidate
, rec
);
1179 rec
->ExceptionInformation
[10] = (ULONG_PTR
)&context
->R4
;
1181 context
->Pc
= (DWORD
)call_consolidate_callback( context
, consolidate
, rec
);
1184 /* hack: remove no longer accessible TEB frames */
1185 while ((DWORD
)teb_frame
< context
->Sp
)
1187 TRACE( "removing TEB frame: %p\n", teb_frame
);
1188 teb_frame
= __wine_pop_frame( teb_frame
);
1191 TRACE( "returning to %lx stack %lx\n", context
->Pc
, context
->Sp
);
1192 NtContinue( context
, FALSE
);
1196 /***********************************************************************
1197 * RtlUnwindEx (NTDLL.@)
1199 void WINAPI
RtlUnwindEx( PVOID end_frame
, PVOID target_ip
, EXCEPTION_RECORD
*rec
,
1200 PVOID retval
, CONTEXT
*context
, UNWIND_HISTORY_TABLE
*table
)
1202 EXCEPTION_REGISTRATION_RECORD
*teb_frame
= NtCurrentTeb()->Tib
.ExceptionList
;
1203 EXCEPTION_RECORD record
;
1204 DISPATCHER_CONTEXT dispatch
;
1205 CONTEXT new_context
;
1209 RtlCaptureContext( context
);
1210 new_context
= *context
;
1212 /* build an exception record, if we do not have one */
1215 record
.ExceptionCode
= STATUS_UNWIND
;
1216 record
.ExceptionFlags
= 0;
1217 record
.ExceptionRecord
= NULL
;
1218 record
.ExceptionAddress
= (void *)context
->Pc
;
1219 record
.NumberParameters
= 0;
1223 rec
->ExceptionFlags
|= EH_UNWINDING
| (end_frame
? 0 : EH_EXIT_UNWIND
);
1225 TRACE( "code=%lx flags=%lx end_frame=%p target_ip=%p pc=%08lx\n",
1226 rec
->ExceptionCode
, rec
->ExceptionFlags
, end_frame
, target_ip
, context
->Pc
);
1227 for (i
= 0; i
< min( EXCEPTION_MAXIMUM_PARAMETERS
, rec
->NumberParameters
); i
++)
1228 TRACE( " info[%ld]=%08Ix\n", i
, rec
->ExceptionInformation
[i
] );
1229 TRACE(" r0=%08lx r1=%08lx r2=%08lx r3=%08lx\n",
1230 context
->R0
, context
->R1
, context
->R2
, context
->R3
);
1231 TRACE(" r4=%08lx r5=%08lx r6=%08lx r7=%08lx\n",
1232 context
->R4
, context
->R5
, context
->R6
, context
->R7
);
1233 TRACE(" r8=%08lx r9=%08lx r10=%08lx r11=%08lx\n",
1234 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
1235 TRACE(" r12=%08lx sp=%08lx lr=%08lx pc=%08lx\n",
1236 context
->R12
, context
->Sp
, context
->Lr
, context
->Pc
);
1238 dispatch
.TargetPc
= (ULONG_PTR
)target_ip
;
1239 dispatch
.ContextRecord
= context
;
1240 dispatch
.HistoryTable
= table
;
1241 dispatch
.NonVolatileRegisters
= (BYTE
*)&context
->R4
;
1245 status
= virtual_unwind( UNW_FLAG_UHANDLER
, &dispatch
, &new_context
);
1246 if (status
!= STATUS_SUCCESS
) raise_status( status
, rec
);
1249 if (!dispatch
.EstablisherFrame
) break;
1251 if (!is_valid_frame( dispatch
.EstablisherFrame
))
1253 ERR( "invalid frame %lx (%p-%p)\n", dispatch
.EstablisherFrame
,
1254 NtCurrentTeb()->Tib
.StackLimit
, NtCurrentTeb()->Tib
.StackBase
);
1255 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
1259 if (dispatch
.LanguageHandler
)
1261 if (end_frame
&& (dispatch
.EstablisherFrame
> (DWORD
)end_frame
))
1263 ERR( "invalid end frame %lx/%p\n", dispatch
.EstablisherFrame
, end_frame
);
1264 raise_status( STATUS_INVALID_UNWIND_TARGET
, rec
);
1266 if (dispatch
.EstablisherFrame
== (DWORD
)end_frame
) rec
->ExceptionFlags
|= EH_TARGET_UNWIND
;
1267 if (call_unwind_handler( rec
, &dispatch
) == ExceptionCollidedUnwind
)
1271 *context
= new_context
= *dispatch
.ContextRecord
;
1272 dispatch
.ContextRecord
= context
;
1273 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1274 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1275 &new_context
, &dispatch
.HandlerData
, &frame
,
1277 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1280 rec
->ExceptionFlags
&= ~EH_COLLIDED_UNWIND
;
1282 else /* hack: call builtin handlers registered in the tib list */
1284 DWORD backup_frame
= dispatch
.EstablisherFrame
;
1285 while ((DWORD
)teb_frame
< new_context
.Sp
&& (DWORD
)teb_frame
< (DWORD
)end_frame
)
1287 TRACE( "found builtin frame %p handler %p\n", teb_frame
, teb_frame
->Handler
);
1288 dispatch
.EstablisherFrame
= (DWORD
)teb_frame
;
1289 if (call_teb_unwind_handler( rec
, &dispatch
, teb_frame
) == ExceptionCollidedUnwind
)
1293 teb_frame
= __wine_pop_frame( teb_frame
);
1295 *context
= new_context
= *dispatch
.ContextRecord
;
1296 dispatch
.ContextRecord
= context
;
1297 RtlVirtualUnwind( UNW_FLAG_NHANDLER
, dispatch
.ImageBase
,
1298 dispatch
.ControlPc
, dispatch
.FunctionEntry
,
1299 &new_context
, &dispatch
.HandlerData
,
1301 rec
->ExceptionFlags
|= EH_COLLIDED_UNWIND
;
1304 teb_frame
= __wine_pop_frame( teb_frame
);
1306 if ((DWORD
)teb_frame
== (DWORD
)end_frame
&& (DWORD
)end_frame
< new_context
.Sp
) break;
1307 dispatch
.EstablisherFrame
= backup_frame
;
1310 if (dispatch
.EstablisherFrame
== (DWORD
)end_frame
) break;
1311 *context
= new_context
;
1314 context
->R0
= (DWORD
)retval
;
1315 context
->Pc
= (DWORD
)target_ip
;
1316 RtlRestoreContext(context
, rec
);
1320 /***********************************************************************
1321 * RtlUnwind (NTDLL.@)
1323 void WINAPI
RtlUnwind( void *frame
, void *target_ip
, EXCEPTION_RECORD
*rec
, void *retval
)
1326 RtlUnwindEx( frame
, target_ip
, rec
, retval
, &context
, NULL
);
1330 /*******************************************************************
1331 * __jump_unwind (NTDLL.@)
1333 void WINAPI
__jump_unwind( void *frame
, void *target_ip
)
1336 RtlUnwindEx( frame
, target_ip
, NULL
, NULL
, &context
, NULL
);
1339 extern LONG
__C_ExecuteExceptionFilter(PEXCEPTION_POINTERS ptrs
, PVOID frame
,
1340 PEXCEPTION_FILTER filter
,
1341 PUCHAR nonvolatile
);
1342 __ASM_GLOBAL_FUNC( __C_ExecuteExceptionFilter
,
1343 "push {r4-r11,lr}\n\t"
1344 __ASM_EHABI(".save {r4-r11,lr}\n\t")
1345 __ASM_SEH(".seh_save_regs_w {r4-r11,lr}\n\t")
1346 __ASM_SEH(".seh_endprologue\n\t")
1348 __ASM_CFI(".cfi_def_cfa 13, 36\n\t")
1349 __ASM_CFI(".cfi_offset r4, -36\n\t")
1350 __ASM_CFI(".cfi_offset r5, -32\n\t")
1351 __ASM_CFI(".cfi_offset r6, -28\n\t")
1352 __ASM_CFI(".cfi_offset r7, -24\n\t")
1353 __ASM_CFI(".cfi_offset r8, -20\n\t")
1354 __ASM_CFI(".cfi_offset r9, -16\n\t")
1355 __ASM_CFI(".cfi_offset r10, -12\n\t")
1356 __ASM_CFI(".cfi_offset r11, -8\n\t")
1357 __ASM_CFI(".cfi_offset lr, -4\n\t")
1359 "ldm r3, {r4-r11,lr}\n\t"
1361 "pop {r4-r11,pc}\n\t" )
1363 extern void __C_ExecuteTerminationHandler(BOOL abnormal
, PVOID frame
,
1364 PTERMINATION_HANDLER handler
,
1365 PUCHAR nonvolatile
);
1366 /* This is, implementation wise, identical to __C_ExecuteExceptionFilter. */
1367 __ASM_GLOBAL_FUNC( __C_ExecuteTerminationHandler
,
1368 "b " __ASM_NAME("__C_ExecuteExceptionFilter") "\n\t");
1370 /*******************************************************************
1371 * __C_specific_handler (NTDLL.@)
1373 EXCEPTION_DISPOSITION WINAPI
__C_specific_handler( EXCEPTION_RECORD
*rec
,
1376 struct _DISPATCHER_CONTEXT
*dispatch
)
1378 SCOPE_TABLE
*table
= dispatch
->HandlerData
;
1380 DWORD ControlPc
= dispatch
->ControlPc
;
1382 TRACE( "%p %p %p %p\n", rec
, frame
, context
, dispatch
);
1383 if (TRACE_ON(seh
)) dump_scope_table( dispatch
->ImageBase
, table
);
1385 if (dispatch
->ControlPcIsUnwound
)
1388 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
1390 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1392 if (ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1393 ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1395 PTERMINATION_HANDLER handler
;
1397 if (table
->ScopeRecord
[i
].JumpTarget
) continue;
1399 if (rec
->ExceptionFlags
& EH_TARGET_UNWIND
&&
1400 dispatch
->TargetPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1401 dispatch
->TargetPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1406 handler
= (PTERMINATION_HANDLER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1407 dispatch
->ScopeIndex
= i
+1;
1409 TRACE( "calling __finally %p frame %p\n", handler
, frame
);
1410 __C_ExecuteTerminationHandler( TRUE
, frame
, handler
,
1411 dispatch
->NonVolatileRegisters
);
1414 return ExceptionContinueSearch
;
1417 for (i
= dispatch
->ScopeIndex
; i
< table
->Count
; i
++)
1419 if (ControlPc
>= dispatch
->ImageBase
+ table
->ScopeRecord
[i
].BeginAddress
&&
1420 ControlPc
< dispatch
->ImageBase
+ table
->ScopeRecord
[i
].EndAddress
)
1422 if (!table
->ScopeRecord
[i
].JumpTarget
) continue;
1423 if (table
->ScopeRecord
[i
].HandlerAddress
!= EXCEPTION_EXECUTE_HANDLER
)
1425 EXCEPTION_POINTERS ptrs
;
1426 PEXCEPTION_FILTER filter
;
1428 filter
= (PEXCEPTION_FILTER
)(dispatch
->ImageBase
+ table
->ScopeRecord
[i
].HandlerAddress
);
1429 ptrs
.ExceptionRecord
= rec
;
1430 ptrs
.ContextRecord
= context
;
1431 TRACE( "calling filter %p ptrs %p frame %p\n", filter
, &ptrs
, frame
);
1432 switch (__C_ExecuteExceptionFilter( &ptrs
, frame
, filter
,
1433 dispatch
->NonVolatileRegisters
))
1435 case EXCEPTION_EXECUTE_HANDLER
:
1437 case EXCEPTION_CONTINUE_SEARCH
:
1439 case EXCEPTION_CONTINUE_EXECUTION
:
1440 return ExceptionContinueExecution
;
1443 TRACE( "unwinding to target %lx\n", dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
);
1444 RtlUnwindEx( frame
, (char *)dispatch
->ImageBase
+ table
->ScopeRecord
[i
].JumpTarget
,
1445 rec
, 0, dispatch
->ContextRecord
, dispatch
->HistoryTable
);
1448 return ExceptionContinueSearch
;
1452 /***********************************************************************
1453 * RtlRaiseException (NTDLL.@)
1455 __ASM_GLOBAL_FUNC( RtlRaiseException
,
1457 __ASM_EHABI(".save {r0, lr}\n\t")
1458 __ASM_SEH(".seh_save_regs {r0, lr}\n\t")
1459 "sub sp, sp, #0x1a0\n\t" /* sizeof(CONTEXT) */
1460 __ASM_EHABI(".pad #0x1a0\n\t")
1461 __ASM_SEH(".seh_stackalloc 0x1a0\n\t")
1462 __ASM_SEH(".seh_endprologue\n\t")
1463 __ASM_CFI(".cfi_adjust_cfa_offset 424\n\t")
1464 __ASM_CFI(".cfi_offset lr, -4\n\t")
1465 "mov r0, sp\n\t" /* context */
1466 "bl " __ASM_NAME("RtlCaptureContext") "\n\t"
1467 "ldr r0, [sp, #0x1a0]\n\t" /* rec */
1468 "ldr r1, [sp, #0x1a4]\n\t"
1469 "str r1, [sp, #0x3c]\n\t" /* context->Lr */
1470 "str r1, [sp, #0x40]\n\t" /* context->Pc */
1472 "bfi r2, r1, #5, #1\n\t" /* Thumb bit */
1473 "str r2, [sp, #0x44]\n\t" /* context->Cpsr */
1474 "str r1, [r0, #12]\n\t" /* rec->ExceptionAddress */
1475 "add r1, sp, #0x1a8\n\t"
1476 "str r1, [sp, #0x38]\n\t" /* context->Sp */
1479 "bl " __ASM_NAME("NtRaiseException") "\n\t"
1480 "bl " __ASM_NAME("RtlRaiseStatus") )
1482 /*************************************************************************
1483 * RtlCaptureStackBackTrace (NTDLL.@)
1485 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1487 FIXME( "(%ld, %ld, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1491 /***********************************************************************
1492 * RtlUserThreadStart (NTDLL.@)
1494 void WINAPI
RtlUserThreadStart( PRTL_THREAD_START_ROUTINE entry
, void *arg
)
1498 pBaseThreadInitThunk( 0, (LPTHREAD_START_ROUTINE
)entry
, arg
);
1500 __EXCEPT(call_unhandled_exception_filter
)
1502 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
1507 /******************************************************************
1508 * LdrInitializeThunk (NTDLL.@)
1510 void WINAPI
LdrInitializeThunk( CONTEXT
*context
, ULONG_PTR unk2
, ULONG_PTR unk3
, ULONG_PTR unk4
)
1512 loader_init( context
, (void **)&context
->R0
);
1513 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", (void *)context
->R0
, (void *)context
->R1
);
1514 NtContinue( context
, TRUE
);
1517 /**********************************************************************
1518 * DbgBreakPoint (NTDLL.@)
1520 __ASM_GLOBAL_FUNC( DbgBreakPoint
, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1522 /**********************************************************************
1523 * DbgUserBreakPoint (NTDLL.@)
1525 __ASM_GLOBAL_FUNC( DbgUserBreakPoint
, "udf #0xfe; bx lr; nop; nop; nop; nop" );
1527 #endif /* __arm__ */