make_i386_ms_pe_gas.asm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  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)
  7. */
  8. /*************************************************************************************
  9. * --------------------------------------------------------------------------------- *
  10. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | *
  11. * --------------------------------------------------------------------------------- *
  12. * | 0h | 04h | 08h | 0ch | 010h | 014h | 018h | 01ch | *
  13. * --------------------------------------------------------------------------------- *
  14. * | fc_mxcsr|fc_x87_cw| fc_strg |fc_deallo| limit | base | fc_seh | EDI | *
  15. * --------------------------------------------------------------------------------- *
  16. * --------------------------------------------------------------------------------- *
  17. * | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
  18. * --------------------------------------------------------------------------------- *
  19. * | 020h | 024h | 028h | 02ch | 030h | 034h | 038h | 03ch | *
  20. * --------------------------------------------------------------------------------- *
  21. * | ESI | EBX | EBP | EIP | to | data | EH NXT |SEH HNDLR| *
  22. * --------------------------------------------------------------------------------- *
  23. **************************************************************************************/
  24. .file "make_i386_ms_pe_gas.asm"
  25. .text
  26. .p2align 4,,15
  27. .globl _make_fcontext
  28. .def _make_fcontext; .scl 2; .type 32; .endef
  29. _make_fcontext:
  30. /* first arg of make_fcontext() == top of context-stack */
  31. movl 0x04(%esp), %eax
  32. /* reserve space for first argument of context-function */
  33. /* EAX might already point to a 16byte border */
  34. leal -0x8(%eax), %eax
  35. /* shift address in EAX to lower 16 byte boundary */
  36. andl $-16, %eax
  37. /* reserve space for context-data on context-stack */
  38. /* size for fc_mxcsr .. EIP + return-address for context-function */
  39. /* on context-function entry: (ESP -0x4) % 8 == 0 */
  40. /* additional space is required for SEH */
  41. leal -0x40(%eax), %eax
  42. /* save MMX control- and status-word */
  43. stmxcsr (%eax)
  44. /* save x87 control-word */
  45. fnstcw 0x4(%eax)
  46. /* first arg of make_fcontext() == top of context-stack */
  47. movl 0x4(%esp), %ecx
  48. /* save top address of context stack as 'base' */
  49. movl %ecx, 0x14(%eax)
  50. /* second arg of make_fcontext() == size of context-stack */
  51. movl 0x8(%esp), %edx
  52. /* negate stack size for LEA instruction (== substraction) */
  53. negl %edx
  54. /* compute bottom address of context stack (limit) */
  55. leal (%ecx,%edx), %ecx
  56. /* save bottom address of context-stack as 'limit' */
  57. movl %ecx, 0x10(%eax)
  58. /* save bottom address of context-stack as 'dealloction stack' */
  59. movl %ecx, 0xc(%eax)
  60. /* set fiber-storage to zero */
  61. xorl %ecx, %ecx
  62. movl %ecx, 0x8(%eax)
  63. /* third arg of make_fcontext() == address of context-function */
  64. /* stored in EBX */
  65. movl 0xc(%esp), %ecx
  66. movl %ecx, 0x24(%eax)
  67. /* compute abs address of label trampoline */
  68. movl $trampoline, %ecx
  69. /* save address of trampoline as return-address for context-function */
  70. /* will be entered after calling jump_fcontext() first time */
  71. movl %ecx, 0x2c(%eax)
  72. /* compute abs address of label finish */
  73. movl $finish, %ecx
  74. /* save address of finish as return-address for context-function */
  75. /* will be entered after context-function returns */
  76. movl %ecx, 0x28(%eax)
  77. /* traverse current seh chain to get the last exception handler installed by Windows */
  78. /* note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default */
  79. /* the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler */
  80. /* at its end by RaiseException all seh andlers are disregarded if not present and the */
  81. /* program is aborted */
  82. /* load NT_TIB into ECX */
  83. movl %fs:(0x0), %ecx
  84. walk:
  85. /* load 'next' member of current SEH into EDX */
  86. movl (%ecx), %edx
  87. /* test if 'next' of current SEH is last (== 0xffffffff) */
  88. incl %edx
  89. jz found
  90. decl %edx
  91. /* exchange content; ECX contains address of next SEH */
  92. xchgl %ecx, %edx
  93. /* inspect next SEH */
  94. jmp walk
  95. found:
  96. /* load 'handler' member of SEH == address of last SEH handler installed by Windows */
  97. movl 0x04(%ecx), %ecx
  98. /* save address in ECX as SEH handler for context */
  99. movl %ecx, 0x3c(%eax)
  100. /* set ECX to -1 */
  101. movl $0xffffffff, %ecx
  102. /* save ECX as next SEH item */
  103. movl %ecx, 0x38(%eax)
  104. /* load address of next SEH item */
  105. leal 0x38(%eax), %ecx
  106. /* save next SEH */
  107. movl %ecx, 0x18(%eax)
  108. /* return pointer to context-data */
  109. ret
  110. trampoline:
  111. /* move transport_t for entering context-function */
  112. /* FCTX == EAX, DATA == EDX */
  113. movl %eax, (%esp)
  114. movl %edx, 0x4(%esp)
  115. /* label finish as return-address */
  116. pushl %ebp
  117. /* jump to context-function */
  118. jmp *%ebx
  119. finish:
  120. /* ESP points to same address as ESP on entry of context function + 0x4 */
  121. xorl %eax, %eax
  122. /* exit code is zero */
  123. movl %eax, (%esp)
  124. /* exit application */
  125. call __exit
  126. hlt
  127. .def __exit; .scl 2; .type 32; .endef /* standard C library function */
  128. .section .drectve
  129. .ascii " -export:\"make_fcontext\""