diff --git a/rtl/win/systhrd.inc b/rtl/win/systhrd.inc index 0f96a934c8..60a52273c2 100644 --- a/rtl/win/systhrd.inc +++ b/rtl/win/systhrd.inc @@ -225,7 +225,12 @@ var {$ifdef DEBUG_MT} writeln('Jumping to thread function of thread ',Win32GetCurrentThreadId); {$endif DEBUG_MT} +{$ifdef FPC_USE_WIN64_SEH} + { use special 'top-level' exception handler around the thread function } + ThreadMain:=main_wrapper(ti.p,pointer(ti.f)); +{$else FPC_USE_WIN64_SEH} ThreadMain:=ti.f(ti.p); +{$endif FPC_USE_WIN64_SEH} end; diff --git a/rtl/win64/system.pp b/rtl/win64/system.pp index 5ec9a9d07a..b9255babcd 100644 --- a/rtl/win64/system.pp +++ b/rtl/win64/system.pp @@ -125,6 +125,19 @@ implementation var SysInstance : qword;public; +{$ifdef FPC_USE_WIN64_SEH} +function main_wrapper(arg: Pointer; proc: Pointer): ptrint; assembler; nostackframe; +asm + subq $40, %rsp +.seh_stackalloc 40 +.seh_endprologue + call %rdx { "arg" is passed in %rcx } + nop { this nop is critical for exception handling } + addq $40, %rsp +.seh_handler __FPC_default_handler,@except,@unwind +end; +{$endif FPC_USE_WIN64_SEH} + { include system independent routines } {$I system.inc} @@ -179,18 +192,6 @@ var to check if the call stack can be written on exceptions } _SS : Cardinal; -{$ifdef FPC_USE_WIN64_SEH} -procedure main_wrapper(p: TProcedure); assembler; nostackframe; -asm - subq $40, %rsp -.seh_stackalloc 40 -.seh_endprologue - call %rcx - nop { this nop is critical for exception handling } - addq $40, %rsp -.seh_handler __FPC_default_handler,@except,@unwind -end; -{$endif FPC_USE_WIN64_SEH} procedure Exe_entry;[public,alias:'_FPC_EXE_Entry']; @@ -209,7 +210,8 @@ procedure Exe_entry;[public,alias:'_FPC_EXE_Entry']; movq %rbp,%rsi xorq %rbp,%rbp {$ifdef FPC_USE_WIN64_SEH} - lea PASCALMAIN(%rip),%rcx + xor %rcx,%rcx + lea PASCALMAIN(%rip),%rdx call main_wrapper {$else FPC_USE_WIN64_SEH} call PASCALMAIN