1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * Contributor(s): Borja "faluco" Ferav (many thanks for assitance!)
9 * David "BAILOPAN" Anderson
10 * ============================
13 // recommended literature:
14 // http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml
15 // http://www.angelcode.com/dev/callconv/callconv.html
16 // http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
18 #include "sourcehook_hookmangen.h"
19 #include "sourcehook_hookmangen_x86.h"
20 #include "sh_memory.h"
22 #if SH_COMP == SH_COMP_MSVC
24 # define MSVC_ONLY(x) x
25 #elif SH_COMP == SH_COMP_GCC
26 # define GCC_ONLY(x) x
30 // :TODO: test BIG vtable indices
36 CPageAlloc GenBuffer::ms_Allocator;
39 jit_int32_t DownCastPtr(T ptr)
41 return reinterpret_cast<jit_int32_t>(ptr);
44 jit_uint32_t DownCastSize(size_t size)
46 return static_cast<jit_uint32_t>(size);
49 GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
50 : m_Proto(proto), m_VtblOffs(vtbl_offs), m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr),
51 m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
54 m_HookfuncVfnptr = new void*;
55 m_BuiltPI = new ProtoInfo;
56 m_BuiltPI_Params = NULL;
57 m_BuiltPI_Params2 = NULL;
60 GenContext::~GenContext()
64 delete m_HookfuncVfnptr;
68 void GenContext::Clear()
74 delete m_BuiltPI_Params;
75 m_BuiltPI_Params = NULL;
77 if (m_BuiltPI_Params2)
79 delete m_BuiltPI_Params2;
80 m_BuiltPI_Params2 = NULL;
84 void GenContext::BuildProtoInfo()
86 m_BuiltPI->convention = m_Proto.GetConvention();
87 m_BuiltPI->numOfParams = m_Proto.GetNumOfParams();
89 m_BuiltPI->retPassInfo.size = m_Proto.GetRet().size;
90 m_BuiltPI->retPassInfo.type = m_Proto.GetRet().type;
91 m_BuiltPI->retPassInfo.flags = m_Proto.GetRet().flags;
92 m_BuiltPI->retPassInfo2.pNormalCtor = m_Proto.GetRet().pNormalCtor;
93 m_BuiltPI->retPassInfo2.pCopyCtor = m_Proto.GetRet().pCopyCtor;
94 m_BuiltPI->retPassInfo2.pDtor = m_Proto.GetRet().pDtor;
95 m_BuiltPI->retPassInfo2.pAssignOperator = m_Proto.GetRet().pAssignOperator;
98 delete m_BuiltPI_Params;
99 m_BuiltPI_Params = new PassInfo[m_BuiltPI->numOfParams + 1];
100 if (m_BuiltPI_Params2)
101 delete m_BuiltPI_Params2;
102 m_BuiltPI_Params2 = new PassInfo::V2Info[m_BuiltPI->numOfParams + 1];
104 m_BuiltPI_Params[0].size = 1; // Version 1
105 m_BuiltPI_Params[0].type = 0;
106 m_BuiltPI_Params[0].flags = 0;
108 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
110 m_BuiltPI_Params[i+1].size = m_Proto.GetParam(i).size;
111 m_BuiltPI_Params[i+1].type = m_Proto.GetParam(i).type;
112 m_BuiltPI_Params[i+1].flags = m_Proto.GetParam(i).flags;
114 m_BuiltPI_Params2[i+1].pNormalCtor = m_Proto.GetParam(i).pNormalCtor;
115 m_BuiltPI_Params2[i+1].pCopyCtor = m_Proto.GetParam(i).pCopyCtor;
116 m_BuiltPI_Params2[i+1].pDtor = m_Proto.GetParam(i).pDtor;
117 m_BuiltPI_Params2[i+1].pAssignOperator = m_Proto.GetParam(i).pAssignOperator;
120 m_BuiltPI->paramsPassInfo = m_BuiltPI_Params;
121 m_BuiltPI->paramsPassInfo2 = m_BuiltPI_Params2;
124 jit_int32_t GenContext::GetRealSize(const IntPassInfo &info)
126 if (info.flags & PassInfo::PassFlag_ByRef)
130 return static_cast<jit_int32_t>(info.size);
133 // Computes size on the stack
134 jit_int32_t GenContext::GetStackSize(const IntPassInfo &info)
136 // Align up to 4 byte boundaries
137 jit_int32_t rs = GetRealSize(info);
139 rs = (rs & ~(3)) + 4;
143 jit_int8_t GenContext::NextRegEBX_ECX_EDX()
145 switch ((m_RegCounter++) % 3)
158 void GenContext::BitwiseCopy_Setup()
164 IA32_Cld(&m_HookFunc);
165 IA32_Push_Reg(&m_HookFunc, REG_EDI);
166 IA32_Push_Reg(&m_HookFunc, REG_ESI);
169 void GenContext::BitwiseCopy_Do(size_t size)
171 jit_uint32_t dwords = DownCastSize(size) / 4;
172 jit_uint32_t bytes = DownCastSize(size) % 4;
185 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
186 IA32_Rep(&m_HookFunc);
187 IA32_Movsd(&m_HookFunc);
191 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
192 IA32_Rep(&m_HookFunc);
193 IA32_Movsb(&m_HookFunc);
195 IA32_Pop_Reg(&m_HookFunc, REG_ESI);
196 IA32_Pop_Reg(&m_HookFunc, REG_EDI);
199 short GenContext::GetParamsStackSize()
202 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
204 acc += GetStackSize(m_Proto.GetParam(i));
207 // Memory return: address is first param
208 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
211 // :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
216 short GenContext::GetForcedByRefParamOffset(int p)
219 for (int i = 0; i < p; ++i)
221 if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
222 off += GetStackSize(m_Proto.GetParam(i));
227 short GenContext::GetForcedByRefParamsSize()
229 return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
232 jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
234 // push [ebp+<offset>]
235 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
240 jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
248 SH_ASSERT(0, ("Unsupported!"));
251 reg = NextRegEBX_ECX_EDX();
252 //movzx reg, BYTE PTR [ebp+<offset>]
254 IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
255 IA32_Push_Reg(&m_HookFunc, reg);
259 reg = NextRegEBX_ECX_EDX();
260 //movzx reg, WORD PTR [ebp+<offset>]
262 m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
263 IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
264 IA32_Push_Reg(&m_HookFunc, reg);
268 reg = NextRegEBX_ECX_EDX();
269 //mov reg, DWORD PTR [ebp+<offset>]
271 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
272 IA32_Push_Reg(&m_HookFunc, reg);
276 reg = NextRegEBX_ECX_EDX();
277 reg2 = NextRegEBX_ECX_EDX();
278 //mov reg, DWORD PTR [ebp+<offset>+4]
279 //mov reg2, DWORD PTR [ebp+<offset>]
283 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
284 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
285 IA32_Push_Reg(&m_HookFunc, reg);
286 IA32_Push_Reg(&m_HookFunc, reg2);
292 jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
297 SH_ASSERT(0, ("Unsupported!"));
300 //fld DWORD PTR [ebp+<offset>]
302 //fstp DWORD PTR [esp]
303 IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
304 IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
305 IA32_Fstp_Mem32_ESP(&m_HookFunc);
308 //fld QWORD PTR [ebp+<offset>]
310 //fstp QWORD PTR [esp]
311 IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
312 IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
313 IA32_Fstp_Mem64_ESP(&m_HookFunc);
318 jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
320 if ((pi.flags & PassFlag_ForcedByRef) == 0)
322 // make room on the stack
324 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetStackSize(pi), MOD_REG);
327 // if there is a copy constructor..
332 // this= target addr = forcedbyref ? ebp+place_fbrr : esp+12
333 // call copy constructor
336 IA32_Push_Reg(&m_HookFunc, REG_EAX);
338 if (pi.flags & PassFlag_ForcedByRef)
339 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
341 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
343 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
344 IA32_Push_Reg(&m_HookFunc, REG_EAX);
345 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
346 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
347 IA32_Call_Reg(&m_HookFunc, REG_EDX);
348 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
349 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
359 // lea edi, [ebp_place_fbrr]
361 // lea edi, [esp+8] - bc_setup pushed two regs onto the stack!
362 //lea esi, [ebp+<offs>]
363 if (pi.flags & PassFlag_ForcedByRef)
364 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
366 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
368 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
370 BitwiseCopy_Do(pi.size);
373 // forcedref: push reference to ebp+place_fbrr
374 if (pi.flags & PassFlag_ForcedByRef)
376 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
377 IA32_Push_Reg(&m_HookFunc, REG_ECX);
381 return DownCastSize(pi.size);
384 void GenContext::DestroyParams(jit_int32_t fbrr_base)
386 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
388 const IntPassInfo &pi = m_Proto.GetParam(i);
389 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
390 (pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
392 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
393 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
394 IA32_Call_Reg(&m_HookFunc, REG_EAX);
399 // May not touch eax!
400 jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
401 jit_int32_t v_place_fbrr_base)
403 jit_int32_t added_to_stack = 0;
406 // compute the offset _after_ the last parameter
407 jit_int32_t cur_offset = param_base_offset;
408 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
410 cur_offset += GetStackSize(m_Proto.GetParam(i));
413 // push parameters in reverse order
414 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
416 const IntPassInfo &pi = m_Proto.GetParam(i);
417 cur_offset -= GetStackSize(pi);
418 if (pi.flags & PassInfo::PassFlag_ByVal)
422 case PassInfo::PassType_Basic:
423 ret = PushBasic(cur_offset, pi);
425 case PassInfo::PassType_Float:
426 ret = PushFloat(cur_offset, pi);
428 case PassInfo::PassType_Object:
429 ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
433 else if (pi.flags & PassInfo::PassFlag_ByRef)
435 PushRef(cur_offset, pi);
439 SH_ASSERT(0, ("Unsupported!"));
441 added_to_stack += ret;
444 return added_to_stack;
447 // It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
448 jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
450 // Memory return support
451 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
453 // push address where to save it!
455 IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
456 MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
457 IA32_Push_Reg(&m_HookFunc, reg);
459 return (SH_COMP==SH_COMP_MSVC) ? 4 : 0; // varargs funcs on msvc might need this.
460 // gcc doesn't: callee cleans the memret ptr, caller the other params :s
465 void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
467 size_t size = GetRealSize(m_Proto.GetRet());
470 // No return value -> nothing
474 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
476 // mov [ebp + v_plugin_ret], eax
477 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
484 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
486 if (MemRetWithTempObj())
488 // *v_where = *v_place_for_memret
490 // if we have an assign operator, use that
491 if (m_Proto.GetRet().pAssignOperator)
493 // lea edx, [ebp + v_place_for_memret] <-- src addr
494 // lea ecx, [ebp + v_where] <-- dest addr
500 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
501 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
502 IA32_Push_Reg(&m_HookFunc, REG_EDX);
503 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
505 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
506 IA32_Call_Reg(&m_HookFunc, REG_EAX);
507 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
514 //lea edi, [evp+v_where] <-- destination
515 //lea esi, [ebp+v_place_for_memret] <-- src
516 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
517 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
519 BitwiseCopy_Do(m_Proto.GetRet().size);
522 // Then: destruct *v_place_for_memret if required
523 if (m_Proto.GetRet().pDtor)
525 //lea ecx, [ebp+v_place_for_memret]
530 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
531 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
532 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
533 IA32_Call_Reg(&m_HookFunc, REG_EAX);
534 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
539 // Already copied to correct address -> we're done
544 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
547 IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
549 IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
551 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
555 // size <= 4: return in EAX
556 // We align <4 sizes up to 4
559 // mov [ebp + v_plugin_ret], eax
560 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
564 // size <= 4: return in EAX:EDX
565 // We align 4<x<8 sizes up to 8
567 // mov [ebp + v_plugin_ret], eax
568 // mov [ebp + v_plugin_ret + 4], edx
569 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
570 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
573 else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
575 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
579 // size <= 4: return in EAX
580 // We align <4 sizes up to 4
582 // mov [ebp + v_plugin_ret], eax
583 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
587 // size <= 4: return in EAX:EDX
588 // We align 4<x<8 sizes up to 8
590 // mov [ebp + v_plugin_ret], eax
591 // mov [ebp + v_plugin_ret + 4], edx
592 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
593 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
597 SH_ASSERT(0, ("RetReg and size > 8 !"));
603 bool GenContext::MemRetWithTempObj()
605 // Memory return AND (has destructor OR has assign operator)
606 return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
607 && (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
610 void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
612 // only for non-void functions!
613 if (m_Proto.GetRet().size == 0)
616 // if (cur_res >= MRES_OVERRIDE)
617 // *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
620 // cmp eax,MRES_OVERRIDE
622 // pContext->GetOverrideRetPtr() --> overrideretptr in eax
627 jitoffs_t tmppos, counter;
629 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
631 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
632 tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
633 m_HookFunc.start_count(counter);
635 // eax = pContext->GetOverrideRetPtr()
642 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
643 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
645 // vtbloffs=0, vtblidx=1
646 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
647 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
648 IA32_Call_Reg(&m_HookFunc, REG_EAX);
649 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
653 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
655 // mov ecx, [ebp+v_plugin_ret]
657 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
658 IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
662 if (m_Proto.GetRet().pAssignOperator)
664 // lea edx, [ebp + v_plugin_ret]
665 // push edx <-- src addr
666 // msvc: ecx = eax <-- dest addr
667 // gcc: push eax <-- dest addr
671 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
672 IA32_Push_Reg(&m_HookFunc, REG_EDX);
673 #if SH_COMP == SH_COMP_MSVC
674 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
675 #elif SH_COMP == SH_COMP_GCC
676 IA32_Push_Reg(&m_HookFunc, REG_EAX);
679 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
680 IA32_Call_Reg(&m_HookFunc, REG_EAX);
681 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
688 //mov edi, eax <-- destination
689 //lea esi, [ebp+v_plugin_ret] <-- src
690 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
691 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
693 BitwiseCopy_Do(m_Proto.GetRet().size);
697 m_HookFunc.end_count(counter);
698 m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
701 void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
703 // only for non-void functions!
704 if (m_Proto.GetRet().size == 0)
707 // retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
709 // OverrideRetPtr: vtblidx = 1
710 // OrigRetPtr: vtbldix = 2
711 // vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
714 // eax = pContext->GetOverrideRetPtr()
718 // eax = (status < MRES_OVERRIDE) ? 1 : 0
720 // cmp [ebp + v_status], MRES_OVERRIDE
721 // setl al <-- setcc optimization for ternary operators,
724 // lea eax, [4*eax + 0x4]
733 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
734 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
735 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
736 IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
737 IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
739 IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
741 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
742 IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
744 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
745 IA32_Call_Reg(&m_HookFunc, REG_EDX);
746 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
748 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
751 void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
753 size_t size = m_Proto.GetRet().size;
757 // Get real ret pointer into ecx
758 // mov ecx, [ebp + v_ret_ptr]
759 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
761 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
764 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
769 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
772 IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
774 IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
776 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic ||
777 ((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
781 // size <= 4: return in EAX
782 // We align <4 sizes up to 4
785 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
789 // size <= 4: return in EAX:EDX
790 // We align 4<x<8 sizes up to 8
794 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
795 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
799 // size >8: return in memory
804 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
806 // *memret_outaddr = plugin_ret
807 if (m_Proto.GetRet().pCopyCtor)
809 // mov edx, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
810 // push edx <-- src addr
811 // msvc: ecx = [ebp + v_memret_outaddr] <-- dest addr
812 // gcc: push [ebp + v_memret_outaddr] <-- dest addr
816 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
817 IA32_Push_Reg(&m_HookFunc, REG_EDX);
819 #if SH_COMP == SH_COMP_MSVC
820 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
821 #elif SH_COMP == SH_COMP_GCC
822 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
825 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
826 IA32_Call_Reg(&m_HookFunc, REG_EAX);
827 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
834 //mov edi, [ebp+v_memret_outaddr] <-- destination
835 //mov esi, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
836 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
837 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
839 BitwiseCopy_Do(m_Proto.GetRet().size);
842 // In both cases: return the pointer in EAX
843 // mov eax, [ebp + v_memret_outaddr]
844 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
848 void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
849 int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
851 jitoffs_t counter, tmppos;
852 jitoffs_t counter2, tmppos2;
854 jitoffs_t loop_begin_counter;
856 // prev_res = MRES_IGNORED
857 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
859 m_HookFunc.start_count(loop_begin_counter);
861 // eax = pContext->GetNext()
868 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
869 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
871 // vtbloffs=0, vtblidx=0
872 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
873 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
874 IA32_Call_Reg(&m_HookFunc, REG_EAX);
875 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
880 IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
881 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
882 m_HookFunc.start_count(counter);
884 // prev_res = MRES_IGNORED
885 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res);
892 // eax = [eax+2*SIZE_PTR]
896 jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
898 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
899 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
900 gcc_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
901 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
902 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
903 IA32_Call_Reg(&m_HookFunc, REG_EAX);
905 DestroyParams(v_place_fbrr_base);
907 SaveRetVal(v_plugin_ret, v_place_for_memret);
910 #if SH_COMP == SH_COMP_GCC
911 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
916 // process meta return:
917 // prev_res = cur_res
918 // if (cur_res > status) status = cur_res;
928 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
929 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
930 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
932 IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
933 tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
934 m_HookFunc.start_count(counter2);
936 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
938 m_HookFunc.end_count(counter2);
939 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
941 // process retval for non-void functions
942 ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
944 // jump back to loop begin
945 tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
946 m_HookFunc.end_count(loop_begin_counter);
947 m_HookFunc.rewrite(tmppos2, -static_cast<jit_int32_t>(loop_begin_counter));
949 m_HookFunc.end_count(counter);
950 m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
953 void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
954 int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
956 jitoffs_t counter, tmppos;
957 jitoffs_t counter2, tmppos2;
958 jitoffs_t counter3, tmppos3;
960 // if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
961 // *v_orig_ret = orig_call()
963 // *v_orig_ret = *v_override_ret
966 // cmp eax, MRES_SUPERCEDE
968 // call pContext->ShouldCallOrig()
969 // test al, al !! important: al, not eax! bool is only stored in the LSbyte
973 // SaveRet(v_orig_ret)
974 // jmp skip_dont_call:
977 // *v_orig_ret = *v_override_ret
980 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
981 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
983 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
984 m_HookFunc.start_count(counter);
986 // eax = pContext->ShouldCallOrig()
990 // eax = [eax + 3*PTR_SIZE]
993 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
994 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
996 // vtbloffs=0, vtblidx=3
997 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
998 IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
999 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1000 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1002 IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1003 tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1004 m_HookFunc.start_count(counter2);
1007 jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
1010 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1011 #if SH_COMP == SH_COMP_GCC
1012 // on gcc/mingw, this is the first parameter
1013 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1014 // on msvc, simply leave it in ecx
1016 gcc_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1019 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1020 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1023 #if SH_COMP == SH_COMP_GCC
1024 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
1027 DestroyParams(v_place_fbrr_base);
1030 SaveRetVal(v_orig_ret, v_place_for_memret);
1032 // Skip don't call variant
1033 tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
1034 m_HookFunc.start_count(counter3);
1038 m_HookFunc.end_count(counter);
1039 m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
1041 m_HookFunc.end_count(counter2);
1042 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
1044 // *v_orig_ret = *v_override_ret
1045 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
1047 // mov ecx, [ebp + v_override_ret]
1048 // mov [ebp + v_orig_ret], ecx
1050 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1051 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
1055 if (m_Proto.GetRet().pAssignOperator)
1057 // lea edx, [ebp + v_override_ret] <-- src addr
1058 // lea ecx, [ebp + v_orig_ret] <-- dest addr
1059 // push edx <-- src addr
1064 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
1065 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1066 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1067 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1069 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1070 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1071 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1076 BitwiseCopy_Setup();
1078 //lea edi, [ebp+v_orig_ret] <-- destination
1079 //lea esi, [ebp+v_override_ret] <-- src
1080 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
1081 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
1083 BitwiseCopy_Do(m_Proto.GetRet().size);
1087 // skip don't call label target:
1088 m_HookFunc.end_count(counter3);
1089 m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
1092 // Sets *v_pContext to return value
1093 void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1094 int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
1095 int v_this, int v_pContext)
1097 // call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
1098 // &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
1099 // The last two params are null for void funcs.
1101 if (m_Proto.GetRet().size == 0)
1104 IA32_Push_Imm8(&m_HookFunc, 0); // orig_ret
1105 IA32_Push_Imm8(&m_HookFunc, 0); // override_ret
1109 // orig_ret and override_ret
1110 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
1111 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
1112 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1113 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1116 // cur_res and prev_res
1117 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1118 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
1119 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1120 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1122 // status and vfnptr_origentry
1123 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1124 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
1125 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1126 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1129 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
1130 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1133 // *(this + vtbloffs) + SIZE_PTR*vtblidx
1134 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this); // get this into ecx (gcc!)
1135 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
1136 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
1137 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1140 IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1141 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1142 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1145 #if SH_COMP == SH_COMP_GCC
1146 // on gcc/mingw, this is the first parameter
1147 GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
1148 #elif SH_COMP == SH_COMP_MSVC
1149 // on msvc, it's ecx
1150 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1153 // call the function. vtbloffs = 0, vtblidx = 19
1154 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1155 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1156 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
1158 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1160 // on gcc/mingw, we have to clean up after the call
1161 // 9 params + hidden thisptr param
1162 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
1164 // store return value
1165 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
1168 void GenContext::CallEndContext(int v_pContext)
1171 // shptr->EndContext(pContex)
1172 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
1173 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1176 #if SH_COMP == SH_COMP_GCC
1177 // on gcc/mingw, this is the first parameter
1178 IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1179 #elif SH_COMP == SH_COMP_MSVC
1180 // on msvc, it's ecx
1181 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1184 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1185 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1186 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
1188 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1190 // on gcc/mingw, we have to clean up after the call
1191 // 1 param + hidden thisptr param
1192 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1195 void * GenContext::GenerateHookFunc()
1198 IA32_Push_Reg(&m_HookFunc, REG_EBP);
1199 IA32_Push_Reg(&m_HookFunc, REG_EBX);
1200 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1202 // on msvc, save thisptr
1203 #if SH_COMP == SH_COMP_MSVC
1204 jit_int8_t v_this = -4;
1205 const int addstackoffset = -4;
1206 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1207 #elif SH_COMP == SH_COMP_GCC
1208 jit_int8_t v_this = 12; // first param
1209 const int addstackoffset = 0;
1213 // ********************** stack frame **********************
1215 // second param (gcc: first real param) ebp + 16
1216 // first param (gcc: thisptr) ebp + 12
1217 // ret address: ebp + 8
1218 // caller's ebp ebp + 4
1220 // MSVC ONLY: current this ebp - 4
1221 // void *vfnptr_origentry ebp - 4 -4
1222 // META_RES status = MRES_IGNORED ebp - 8 -4
1223 // META_RES prev_res ebp - 12 -4
1224 // META_RES cur_res ebp - 16 -4
1225 // IMyDelegate *iter ebp - 20 -4
1226 // IHookContext *pContext ebp - 24 -4
1227 // == 3 ptrs + 3 enums = 24 bytes
1230 // my_rettype *ret_ptr ebp - 28 -4
1231 // my_rettype orig_ret ebp - 28 - sizeof(my_rettype) -4
1232 // my_rettype override_ret ebp - 28 - sizeof(my_rettype)*2 -4
1233 // my_rettype plugin_ret ebp - 28 - sizeof(my_rettype)*3 -4
1234 // == + 3 * sizeof(my_rettype) bytes
1237 // my_rettype place_for_memret ebp - 28 - sizeof(my_rettype)*4 -4
1239 // gcc only: if required:
1240 // place forced byref params ebp - 28 - sizeof(my_rettype)*{4 or 5}
1243 const jit_int8_t v_vfnptr_origentry = -4 + addstackoffset;
1244 const jit_int8_t v_status = -8 + addstackoffset;
1245 const jit_int8_t v_prev_res = -12 + addstackoffset;
1246 const jit_int8_t v_cur_res = -16 + addstackoffset;
1247 const jit_int8_t v_iter = -20 + addstackoffset;
1248 const jit_int8_t v_pContext = -24 + addstackoffset;
1250 #if SH_COMP == SH_COMP_GCC
1251 jit_int32_t param_base_offs = 16;
1252 #elif SH_COMP == SH_COMP_MSVC
1253 jit_int32_t param_base_offs = 12;
1256 // Memory return: first param is the address
1257 jit_int32_t v_memret_addr = 0;
1258 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1260 #if SH_COMP == SH_COMP_GCC
1261 // gcc: now: first param = mem ret addr
1262 // second param = this pointer
1263 // third param = actual first param
1266 param_base_offs += SIZE_PTR;
1267 #elif SH_COMP == SH_COMP_MSVC
1268 v_memret_addr = param_base_offs;
1269 param_base_offs += SIZE_PTR;
1273 jit_int32_t v_ret_ptr = -28 + addstackoffset;
1274 jit_int32_t v_orig_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 1;
1275 jit_int32_t v_override_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 2;
1276 jit_int32_t v_plugin_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 3;
1278 jit_int32_t v_place_for_memret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 4;
1280 jit_int32_t v_place_fbrr_base = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * (MemRetWithTempObj() ? 5 : 4);
1282 // Hash for temporary storage for byval params with copy constructors
1283 // (param, offset into stack)
1284 short usedStackBytes = 3*SIZE_MWORD + 3*SIZE_PTR // vfnptr_origentry, status, prev_res, cur_res, iter, pContext
1285 + 3 * GetStackSize(m_Proto.GetRet()) + (m_Proto.GetRet().size == 0 ? 0 : SIZE_PTR) // ret_ptr, orig_ret, override_ret, plugin_ret
1286 + (MemRetWithTempObj() ? GetStackSize(m_Proto.GetRet()) : 0)
1287 + GetForcedByRefParamsSize()
1288 - addstackoffset; // msvc: current thisptr
1290 IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, usedStackBytes, MOD_REG);
1292 // init status localvar
1293 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
1295 // Call constructors for ret vars if required
1296 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1297 m_Proto.GetRet().pNormalCtor)
1299 // :TODO: Gcc version
1302 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1303 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1304 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1305 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1306 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1309 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1310 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1311 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1312 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1313 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1316 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1317 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1318 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1319 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1320 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1322 // _don't_ call a constructor for v_place_for_memret !
1325 // ********************** SetupHookLoop **********************
1326 CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
1327 v_this, v_pContext);
1329 // ********************** call pre hooks **********************
1330 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1331 v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
1333 // ********************** call orig func **********************
1334 GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
1335 v_override_ret, v_place_for_memret, v_place_fbrr_base);
1337 // ********************** call post hooks **********************
1338 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1339 v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
1341 // ********************** end context and return **********************
1343 PrepareReturn(v_status, v_pContext, v_ret_ptr);
1345 CallEndContext(v_pContext);
1347 // Call destructors of byval object params which have a destructor
1348 jit_int32_t cur_param_pos = param_base_offs;
1349 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1351 const IntPassInfo &pi = m_Proto.GetParam(i);
1352 // GCC: NOT of forced byref params. the caller destructs those.
1353 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
1354 (pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
1356 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
1357 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
1358 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1360 cur_param_pos += GetStackSize(pi);
1363 DoReturn(v_ret_ptr, v_memret_addr);
1365 // Call destructors of orig_ret/ ...
1366 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1367 m_Proto.GetRet().pDtor)
1369 // Preserve return value in EAX(:EDX)
1370 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1371 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1373 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1374 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1375 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1376 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1377 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1379 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1380 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1381 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1382 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1383 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1385 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1386 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1387 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1388 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1389 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1391 IA32_Pop_Reg(&m_HookFunc, REG_EDX);
1392 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
1396 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
1397 IA32_Pop_Reg(&m_HookFunc, REG_EBX);
1398 IA32_Pop_Reg(&m_HookFunc, REG_EBP);
1399 MSVC_ONLY(IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize()));
1400 // gcc: Remove 4 bytes from stack on memory return
1401 #if SH_COMP == SH_COMP_GCC
1402 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1403 IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
1405 IA32_Return(&m_HookFunc);
1408 // Store pointer for later use
1409 // m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
1410 // into the hookman's vtable
1411 *m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
1415 return m_HookFunc.GetData();
1418 // Pre-condition: GenerateHookFunc() has been called!
1419 void * GenContext::GeneratePubFunc()
1421 jitoffs_t counter, tmppos;
1423 // The pubfunc is a static cdecl function.
1425 // int HookManPubFunc(
1426 // bool store, ebp + 8
1427 // IHookManagerInfo *hi ebp + 12
1433 // hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
1437 IA32_Push_Reg(&m_PubFunc, REG_EBP);
1438 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
1441 // save store in eax, hi in ecx
1442 IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
1443 IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
1445 // Check for store == 0
1446 IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1447 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1448 m_PubFunc.start_count(counter);
1450 // nonzero -> store hi
1451 IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1452 IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1455 m_PubFunc.end_count(counter);
1456 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1458 // check for hi == 0
1459 IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
1460 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1461 m_PubFunc.start_count(counter);
1463 // nonzero -> call vfunc
1464 // push params in reverse order
1465 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
1466 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
1467 IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
1468 IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
1469 IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
1471 // hi == this is in ecx
1472 // on gcc/mingw, ecx is the first parameter
1473 #if SH_COMP == SH_COMP_GCC
1474 IA32_Push_Reg(&m_PubFunc, REG_ECX);
1477 // call the function. vtbloffs = 0, vtblidx = 0
1478 // get vtptr into edx
1479 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1480 // get funcptr into eax
1481 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1483 IA32_Call_Reg(&m_PubFunc, REG_EAX);
1485 // on gcc/mingw, we have to clean up after the call
1486 #if SH_COMP == SH_COMP_GCC
1487 // 5 params + hidden thisptr param
1488 IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
1492 m_PubFunc.end_count(counter);
1493 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1496 IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1499 IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
1500 IA32_Pop_Reg(&m_PubFunc, REG_EBP);
1501 IA32_Return(&m_PubFunc);
1508 bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
1510 if (pi.type != PassInfo::PassType_Basic &&
1511 pi.type != PassInfo::PassType_Float &&
1512 pi.type != PassInfo::PassType_Object)
1518 if (pi.type == PassInfo::PassType_Object &&
1519 (pi.flags & PassInfo::PassFlag_ByVal))
1521 if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
1527 if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
1533 if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
1539 if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
1546 if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
1548 return false; // Neither byval nor byref!
1553 void GenContext::AutoDetectRetType()
1555 IntPassInfo &pi = m_Proto.GetRet();
1557 // Only relevant for byval types
1558 if (pi.flags & PassInfo::PassFlag_ByVal)
1561 if (pi.type == PassInfo::PassType_Basic ||
1562 pi.type == PassInfo::PassType_Float)
1565 // _always_ in registers, no matter what the user says
1568 pi.flags &= ~PassInfo::PassFlag_RetMem;
1569 pi.flags |= PassInfo::PassFlag_RetReg;
1573 // Does this even exist? No idea, if it does: in memory!
1574 pi.flags &= ~PassInfo::PassFlag_RetReg;
1575 pi.flags |= PassInfo::PassFlag_RetMem;
1579 else if (pi.type == PassInfo::PassType_Object)
1581 // If the user says nothing, auto-detect
1582 if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
1584 #if SH_COMP == SH_COMP_MSVC
1585 // MSVC seems to return _all_ structs, classes, unions in memory
1586 pi.flags |= PassInfo::PassFlag_RetMem;
1587 #elif SH_COMP == SH_COMP_GCC
1588 // Same goes for GCC :)
1589 pi.flags |= PassInfo::PassFlag_RetMem;
1596 // byref: make sure that the flag is _not_ set
1597 pi.flags &= ~PassInfo::PassFlag_RetMem;
1598 pi.flags |= PassInfo::PassFlag_RetReg;
1602 void GenContext::AutoDetectParamFlags()
1604 #if SH_COMP == SH_COMP_GCC
1605 // On GCC, all objects are passed by reference if they have a destructor
1606 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1608 IntPassInfo &pi = m_Proto.GetParam(i);
1609 if (pi.type == PassInfo::PassType_Object &&
1610 (pi.flags & PassInfo::PassFlag_ODtor))
1612 pi.flags |= PassFlag_ForcedByRef;
1618 HookManagerPubFunc GenContext::Generate()
1622 // Check conditions:
1623 // -1) good proto version
1624 // 0) we don't support unknown passtypes, convention, ...
1625 // 1) we don't support functions which return objects by value or take parameters by value
1626 // that have a constructor, a destructor or an overloaded assignment op
1627 // (we wouldn't know how to call it!)
1629 if (m_Proto.GetVersion() < 1)
1634 AutoDetectRetType();
1635 AutoDetectParamFlags();
1637 if (m_Proto.GetConvention() != ProtoInfo::CallConv_Cdecl &&
1638 m_Proto.GetConvention() != ProtoInfo::CallConv_ThisCall)
1644 if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
1649 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1651 if (!PassInfoSupported(m_Proto.GetParam(i), false))
1657 return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());