2
0

make_i386_ms_pe_gas.asm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_strg |fc_deallo| limit | base | fc_seh | EDI | ESI | EBX | *
  15. * --------------------------------------------------------------------------------- *
  16. * --------------------------------------------------------------------------------- *
  17. * | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
  18. * --------------------------------------------------------------------------------- *
  19. * | 020h | 024h | 028h | 02ch | 030h | 034h | 038h | 03ch | *
  20. * --------------------------------------------------------------------------------- *
  21. * | 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 -0x08(%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 -0x48(%eax), %eax
  42. /* first arg of make_fcontext() == top of context-stack */
  43. movl 0x04(%esp), %ecx
  44. /* save top address of context stack as 'base' */
  45. movl %ecx, 0xc(%eax)
  46. /* second arg of make_fcontext() == size of context-stack */
  47. movl 0x08(%esp), %edx
  48. /* negate stack size for LEA instruction (== substraction) */
  49. negl %edx
  50. /* compute bottom address of context stack (limit) */
  51. leal (%ecx,%edx), %ecx
  52. /* save bottom address of context-stack as 'limit' */
  53. movl %ecx, 0x8(%eax)
  54. /* save bottom address of context-stack as 'dealloction stack' */
  55. movl %ecx, 0x4(%eax)
  56. /* set fiber-storage to zero */
  57. xorl %ecx, %ecx
  58. movl %ecx, (%eax)
  59. /* third arg of make_fcontext() == address of context-function */
  60. /* stored in EBX */
  61. movl 0xc(%esp), %ecx
  62. movl %ecx, 0x1c(%eax)
  63. /* compute abs address of label trampoline */
  64. movl $trampoline, %ecx
  65. /* save address of trampoline as return-address for context-function */
  66. /* will be entered after calling jump_fcontext() first time */
  67. movl %ecx, 0x24(%eax)
  68. /* compute abs address of label finish */
  69. movl $finish, %ecx
  70. /* save address of finish as return-address for context-function */
  71. /* will be entered after context-function returns */
  72. movl %ecx, 0x20(%eax)
  73. /* traverse current seh chain to get the last exception handler installed by Windows */
  74. /* note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default */
  75. /* the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler */
  76. /* at its end by RaiseException all seh andlers are disregarded if not present and the */
  77. /* program is aborted */
  78. /* load NT_TIB into ECX */
  79. movl %fs:(0x0), %ecx
  80. walk:
  81. /* load 'next' member of current SEH into EDX */
  82. movl (%ecx), %edx
  83. /* test if 'next' of current SEH is last (== 0xffffffff) */
  84. incl %edx
  85. jz found
  86. decl %edx
  87. /* exchange content; ECX contains address of next SEH */
  88. xchgl %ecx, %edx
  89. /* inspect next SEH */
  90. jmp walk
  91. found:
  92. /* load 'handler' member of SEH == address of last SEH handler installed by Windows */
  93. movl 0x04(%ecx), %ecx
  94. /* save address in ECX as SEH handler for context */
  95. movl %ecx, 0x38(%eax)
  96. /* set ECX to -1 */
  97. movl $0xffffffff, %ecx
  98. /* save ECX as next SEH item */
  99. movl %ecx, 0x34(%eax)
  100. /* load address of next SEH item */
  101. leal 0x34(%eax), %ecx
  102. /* save next SEH */
  103. movl %ecx, 0x10(%eax)
  104. /* return pointer to context-data */
  105. ret
  106. trampoline:
  107. /* move transport_t for entering context-function */
  108. /* FCTX == EAX, DATA == EDX */
  109. movl %eax, (%esp)
  110. movl %edx, 0x4(%esp)
  111. /* label finish as return-address */
  112. pushl %ebp
  113. /* jump to context-function */
  114. jmp *%ebx
  115. finish:
  116. /* ESP points to same address as ESP on entry of context function + 0x4 */
  117. xorl %eax, %eax
  118. /* exit code is zero */
  119. movl %eax, (%esp)
  120. /* exit application */
  121. call __exit
  122. hlt
  123. .def __exit; .scl 2; .type 32; .endef /* standard C library function */
  124. .section .drectve
  125. .ascii " -export:\"make_fcontext\""