make_i386_ms_pe_masm.asm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ; Copyright Oliver Kowalke 2009.
  2. ; Distributed under the Boost Software License, Version 1.0.
  3. ; (See accompanying file LICENSE_1_0.txt or copy at
  4. ; http://www.boost.org/LICENSE_1_0.txt)
  5. ; ---------------------------------------------------------------------------------
  6. ; | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  7. ; ---------------------------------------------------------------------------------
  8. ; | 0h | 04h | 08h | 0ch | 010h | 014h | 018h | 01ch |
  9. ; ---------------------------------------------------------------------------------
  10. ; | fc_strg |fc_deallo| limit | base | fc_seh | EDI | ESI | EBX |
  11. ; ---------------------------------------------------------------------------------
  12. ; ---------------------------------------------------------------------------------
  13. ; | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
  14. ; ---------------------------------------------------------------------------------
  15. ; | 020h | 024h | 028h | 02ch | 030h | 034h | 038h | 03ch |
  16. ; ---------------------------------------------------------------------------------
  17. ; | EBP | EIP | to | data | | EH NXT |SEH HNDLR| |
  18. ; ---------------------------------------------------------------------------------
  19. .386
  20. .XMM
  21. .model flat, c
  22. ; standard C library function
  23. _exit PROTO, value:SDWORD
  24. .code
  25. make_fcontext PROC BOOST_CONTEXT_EXPORT
  26. ; first arg of make_fcontext() == top of context-stack
  27. mov eax, [esp+04h]
  28. ; reserve space for first argument of context-function
  29. ; EAX might already point to a 16byte border
  30. lea eax, [eax-08h]
  31. ; shift address in EAX to lower 16 byte boundary
  32. and eax, -16
  33. ; reserve space for context-data on context-stack
  34. ; on context-function entry: (ESP -0x4) % 8 == 0
  35. ; additional space is required for SEH
  36. lea eax, [eax-048h]
  37. ; first arg of make_fcontext() == top of context-stack
  38. mov ecx, [esp+04h]
  39. ; save top address of context stack as 'base'
  40. mov [eax+0ch], ecx
  41. ; second arg of make_fcontext() == size of context-stack
  42. mov edx, [esp+08h]
  43. ; negate stack size for LEA instruction (== substraction)
  44. neg edx
  45. ; compute bottom address of context stack (limit)
  46. lea ecx, [ecx+edx]
  47. ; save bottom address of context-stack as 'limit'
  48. mov [eax+08h], ecx
  49. ; save bottom address of context-stack as 'dealloction stack'
  50. mov [eax+04h], ecx
  51. ; set fiber-storage to zero
  52. xor ecx, ecx
  53. mov [eax], ecx
  54. ; third arg of make_fcontext() == address of context-function
  55. ; stored in EBX
  56. mov ecx, [esp+0ch]
  57. mov [eax+01ch], ecx
  58. ; compute abs address of label trampoline
  59. mov ecx, trampoline
  60. ; save address of trampoline as return-address for context-function
  61. ; will be entered after calling jump_fcontext() first time
  62. mov [eax+024h], ecx
  63. ; compute abs address of label finish
  64. mov ecx, finish
  65. ; save address of finish as return-address for context-function
  66. ; will be entered after context-function returns
  67. mov [eax+020h], ecx
  68. ; traverse current seh chain to get the last exception handler installed by Windows
  69. ; note that on Windows Server 2008 and 2008 R2, SEHOP is activated by default
  70. ; the exception handler chain is tested for the presence of ntdll.dll!FinalExceptionHandler
  71. ; at its end by RaiseException all seh-handlers are disregarded if not present and the
  72. ; program is aborted
  73. assume fs:nothing
  74. ; load NT_TIB into ECX
  75. mov ecx, fs:[0h]
  76. assume fs:error
  77. walk:
  78. ; load 'next' member of current SEH into EDX
  79. mov edx, [ecx]
  80. ; test if 'next' of current SEH is last (== 0xffffffff)
  81. inc edx
  82. jz found
  83. dec edx
  84. ; exchange content; ECX contains address of next SEH
  85. xchg edx, ecx
  86. ; inspect next SEH
  87. jmp walk
  88. found:
  89. ; load 'handler' member of SEH == address of last SEH handler installed by Windows
  90. mov ecx, [ecx+04h]
  91. ; save address in ECX as SEH handler for context
  92. mov [eax+038h], ecx
  93. ; set ECX to -1
  94. mov ecx, 0ffffffffh
  95. ; save ECX as next SEH item
  96. mov [eax+034h], ecx
  97. ; load address of next SEH item
  98. lea ecx, [eax+034h]
  99. ; save next SEH
  100. mov [eax+010h], ecx
  101. ret ; return pointer to context-data
  102. trampoline:
  103. ; move transport_t for entering context-function
  104. ; FCTX == EAX, DATA == EDX
  105. mov [esp], eax
  106. mov [esp+04h], edx
  107. push ebp
  108. ; jump to context-function
  109. jmp ebx
  110. finish:
  111. ; exit code is zero
  112. xor eax, eax
  113. mov [esp], eax
  114. ; exit application
  115. call _exit
  116. hlt
  117. make_fcontext ENDP
  118. END