2 Copyright Oliver Kowalke 2009.
3 Copyright Thomas Sailer 2013.
4 Distributed under the Boost Software License, Version 1.0.
5 (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
9 /*************************************************************************************
10 * --------------------------------------------------------------------------------- *
11 * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | *
12 * --------------------------------------------------------------------------------- *
13 * | 0h | 04h | 08h | 0ch | 010h | 014h | 018h | 01ch | *
14 * --------------------------------------------------------------------------------- *
15 * | fc_mxcsr|fc_x87_cw| fc_strg |fc_deallo| limit | base | fc_seh | EDI | *
16 * --------------------------------------------------------------------------------- *
17 * --------------------------------------------------------------------------------- *
18 * | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
19 * --------------------------------------------------------------------------------- *
20 * | 020h | 024h | 028h | 02ch | 030h | 034h | 038h | 03ch | *
21 * --------------------------------------------------------------------------------- *
22 * | ESI | EBX | EBP | EIP | to | data | EH NXT |SEH HNDLR| *
23 * --------------------------------------------------------------------------------- *
24 **************************************************************************************/
26 .file "make_i386_ms_pe_gas.asm"
30 .def _make_fcontext; .scl 2; .type 32; .endef
32 /* first arg of make_fcontext() == top of context-stack */
35 /* reserve space for first argument of context-function */
36 /* EAX might already point to a 16byte border */
39 /* shift address in EAX to lower 16 byte boundary */
42 /* reserve space for context-data on context-stack */
43 /* size for fc_mxcsr .. EIP + return-address for context-function */
44 /* on context-function entry: (ESP -0x4) % 8 == 0 */
45 /* additional space is required for SEH */
46 leal -0x40(%eax), %eax
48 /* save MMX control- and status-word */
50 /* save x87 control-word */
53 /* first arg of make_fcontext() == top of context-stack */
55 /* save top address of context stack as 'base' */
57 /* second arg of make_fcontext() == size of context-stack */
59 /* negate stack size for LEA instruction (== substraction) */
61 /* compute bottom address of context stack (limit) */
62 leal (%ecx,%edx), %ecx
63 /* save bottom address of context-stack as 'limit' */
65 /* save bottom address of context-stack as 'dealloction stack' */
67 /* set fiber-storage to zero */
71 /* third arg of make_fcontext() == address of context-function */
76 /* compute abs address of label trampoline */
77 movl $trampoline, %ecx
78 /* save address of trampoline as return-address for context-function */
79 /* will be entered after calling jump_fcontext() first time */
82 /* compute abs address of label finish */
84 /* save address of finish as return-address for context-function */
85 /* will be entered after context-function returns */
88 /* traverse current seh chain to get the last exception handler installed by Windows */
89 /* note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default */
90 /* the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler */
91 /* at its end by RaiseException all seh andlers are disregarded if not present and the */
92 /* program is aborted */
93 /* load NT_TIB into ECX */
97 /* load 'next' member of current SEH into EDX */
99 /* test if 'next' of current SEH is last (== 0xffffffff) */
103 /* exchange content; ECX contains address of next SEH */
105 /* inspect next SEH */
109 /* load 'handler' member of SEH == address of last SEH handler installed by Windows */
110 movl 0x04(%ecx), %ecx
111 /* save address in ECX as SEH handler for context */
112 movl %ecx, 0x3c(%eax)
114 movl $0xffffffff, %ecx
115 /* save ECX as next SEH item */
116 movl %ecx, 0x38(%eax)
117 /* load address of next SEH item */
118 leal 0x38(%eax), %ecx
120 movl %ecx, 0x18(%eax)
122 /* return pointer to context-data */
126 /* move transport_t for entering context-function */
127 /* FCTX == EAX, DATA == EDX */
130 /* label finish as return-address */
132 /* jump to context-function */
136 /* ESP points to same address as ESP on entry of context function + 0x4 */
138 /* exit code is zero */
140 /* exit application */
144 .def __exit; .scl 2; .type 32; .endef /* standard C library function */
147 .ascii " -export:\"make_fcontext\""