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_strg |fc_deallo| limit | base | fc_seh | EDI | ESI | EBX | *
16 * --------------------------------------------------------------------------------- *
17 * --------------------------------------------------------------------------------- *
18 * | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
19 * --------------------------------------------------------------------------------- *
20 * | 020h | 024h | 028h | 02ch | 030h | 034h | 038h | 03ch | *
21 * --------------------------------------------------------------------------------- *
22 * | 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 */
37 leal -0x08(%eax), %eax
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 -0x48(%eax), %eax
48 /* first arg of make_fcontext() == top of context-stack */
50 /* save top address of context stack as 'base' */
52 /* second arg of make_fcontext() == size of context-stack */
54 /* negate stack size for LEA instruction (== substraction) */
56 /* compute bottom address of context stack (limit) */
57 leal (%ecx,%edx), %ecx
58 /* save bottom address of context-stack as 'limit' */
60 /* save bottom address of context-stack as 'dealloction stack' */
62 /* set fiber-storage to zero */
66 /* third arg of make_fcontext() == address of context-function */
71 /* compute abs address of label trampoline */
72 movl $trampoline, %ecx
73 /* save address of trampoline as return-address for context-function */
74 /* will be entered after calling jump_fcontext() first time */
77 /* compute abs address of label finish */
79 /* save address of finish as return-address for context-function */
80 /* will be entered after context-function returns */
83 /* traverse current seh chain to get the last exception handler installed by Windows */
84 /* note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default */
85 /* the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler */
86 /* at its end by RaiseException all seh andlers are disregarded if not present and the */
87 /* program is aborted */
88 /* load NT_TIB into ECX */
92 /* load 'next' member of current SEH into EDX */
94 /* test if 'next' of current SEH is last (== 0xffffffff) */
98 /* exchange content; ECX contains address of next SEH */
100 /* inspect next SEH */
104 /* load 'handler' member of SEH == address of last SEH handler installed by Windows */
105 movl 0x04(%ecx), %ecx
106 /* save address in ECX as SEH handler for context */
107 movl %ecx, 0x38(%eax)
109 movl $0xffffffff, %ecx
110 /* save ECX as next SEH item */
111 movl %ecx, 0x34(%eax)
112 /* load address of next SEH item */
113 leal 0x34(%eax), %ecx
115 movl %ecx, 0x10(%eax)
117 /* return pointer to context-data */
121 /* move transport_t for entering context-function */
122 /* FCTX == EAX, DATA == EDX */
125 /* label finish as return-address */
127 /* jump to context-function */
131 /* ESP points to same address as ESP on entry of context function + 0x4 */
133 /* exit code is zero */
135 /* exit application */
139 .def __exit; .scl 2; .type 32; .endef /* standard C library function */
142 .ascii " -export:\"make_fcontext\""