sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 464 b340e8665151
parent 463 7a413a63426f
child 467 7b0d972632af
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Sun Nov 18 11:58:07 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp	Sun Nov 18 13:12:51 2007 +0000
     1.3 @@ -20,6 +20,7 @@
     1.4  #include "sourcehook_hookmangen_x86.h"
     1.5  #include "sh_memory.h"
     1.6  #include <stdarg.h>							// we might need the address of vsnprintf
     1.7 +#include <stdio.h>
     1.8  
     1.9  #if SH_COMP == SH_COMP_MSVC
    1.10  # define GCC_ONLY(x)
    1.11 @@ -895,20 +896,21 @@
    1.12  			//   call eax
    1.13  			//   gcc: clean up 
    1.14  			
    1.15 -			jit_int32_t gcc_clean_bytes = 0;
    1.16 +			jit_int32_t caller_clean_bytes = 0;			// gcc always, msvc never (hooks never have varargs!)
    1.17  			// vafmt: push va_buf
    1.18  			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
    1.19  			{
    1.20  				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
    1.21  				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.22 -				gcc_clean_bytes += SIZE_PTR;
    1.23 +				caller_clean_bytes += SIZE_PTR;
    1.24  			}
    1.25  
    1.26 -			gcc_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
    1.27 +			caller_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
    1.28  
    1.29  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
    1.30 -			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
    1.31 -			gcc_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
    1.32 +			if (SH_COMP == SH_COMP_GCC)
    1.33 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.34 +			caller_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
    1.35  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
    1.36  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
    1.37  			IA32_Call_Reg(&m_HookFunc, REG_EAX);
    1.38 @@ -917,12 +919,10 @@
    1.39  
    1.40  			SaveRetVal(v_plugin_ret, v_place_for_memret);
    1.41  
    1.42 -			// cleanup
    1.43 -#if SH_COMP == SH_COMP_GCC
    1.44 -			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
    1.45 -#endif
    1.46 -
    1.47 -			// params + thisptr
    1.48 +			// cleanup (gcc only)
    1.49 +			//   params + thisptr
    1.50 +			if (SH_COMP == SH_COMP_GCC)
    1.51 +				IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
    1.52  
    1.53  			// process meta return:
    1.54  			//  prev_res = cur_res
    1.55 @@ -1014,7 +1014,7 @@
    1.56  			tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
    1.57  			m_HookFunc.start_count(counter2);
    1.58  
    1.59 -			jit_int32_t gcc_clean_bytes = 0;
    1.60 +			jit_int32_t caller_clean_bytes = 0;			// gcc always, msvc when cdecl-like (varargs)
    1.61  			
    1.62  			// vafmt: push va_buf, then "%s"
    1.63  			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
    1.64 @@ -1022,29 +1022,29 @@
    1.65  				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
    1.66  				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.67  				IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
    1.68 -				gcc_clean_bytes += 2*SIZE_PTR;
    1.69 +				caller_clean_bytes += 2*SIZE_PTR;
    1.70  			}
    1.71  
    1.72  			// push params
    1.73 -			gcc_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
    1.74 +			caller_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
    1.75  
    1.76  			// thisptr
    1.77  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
    1.78 -#if SH_COMP == SH_COMP_GCC
    1.79 -			//  on gcc/mingw, this is the first parameter
    1.80 -			IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.81 -			//  on msvc, simply leave it in ecx
    1.82 -#endif
    1.83 -			gcc_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
    1.84 +			if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
    1.85 +			{
    1.86 +				//  on gcc/mingw or msvc with varargs, this is the first parameter
    1.87 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.88 +				//  on msvc without varargs, simply leave it in ecx
    1.89 +			}
    1.90 +			caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
    1.91  
    1.92  			// call
    1.93  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
    1.94  			IA32_Call_Reg(&m_HookFunc, REG_EAX);
    1.95  
    1.96  			// cleanup
    1.97 -#if SH_COMP == SH_COMP_GCC
    1.98 -			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
    1.99 -#endif
   1.100 +			if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
   1.101 +				IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
   1.102  
   1.103  			DestroyParams(v_place_fbrr_base);
   1.104  
   1.105 @@ -1239,21 +1239,27 @@
   1.106  			IA32_Push_Reg(&m_HookFunc, REG_EBX);
   1.107  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
   1.108  
   1.109 -
   1.110 -			// on msvc, save thisptr
   1.111 -#if SH_COMP == SH_COMP_MSVC
   1.112 -			jit_int32_t v_this = -4;
   1.113 -			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.114 -#elif SH_COMP == SH_COMP_GCC
   1.115 -			jit_int32_t v_this = 12;		// first param
   1.116 -#endif
   1.117 -
   1.118 -			ResetFrame(
   1.119 -					(SH_COMP==SH_COMP_MSVC) ? -4 : 0
   1.120 -					);			// on msvc, start on offset -4 because there already is the thisptr variable
   1.121 +			jit_int32_t v_this = 0;
   1.122 +			jit_int32_t param_base_offs = 0;
   1.123 +			if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
   1.124 +			{
   1.125 +				// gcc or msvc with varargs:
   1.126 +				v_this = 12;		// first param
   1.127 +				param_base_offs = 16;
   1.128 +				ResetFrame(0);
   1.129 +			}
   1.130 +			else
   1.131 +			{
   1.132 +				// on msvc without varargs, save thisptr
   1.133 +				v_this = -4;
   1.134 +				param_base_offs = 12;
   1.135 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.136 +				ResetFrame(-4);			// start placing local vars on offset -4
   1.137 +										// because there already is the thisptr variable
   1.138 +			}
   1.139  
   1.140  			// ********************** stack frame **********************
   1.141 -			//															MSVC
   1.142 +			//															MSVC without varargs
   1.143  			//   second param (gcc: first real param)	ebp + 16
   1.144  			//   first param (gcc: thisptr)				ebp + 12
   1.145  			//   ret address:							ebp + 8
   1.146 @@ -1292,28 +1298,25 @@
   1.147  			const jit_int8_t v_cur_res =			AddVarToFrame(sizeof(META_RES));
   1.148  			const jit_int8_t v_iter =				AddVarToFrame(SIZE_PTR);
   1.149  			const jit_int8_t v_pContext =			AddVarToFrame(SIZE_PTR);
   1.150 -			
   1.151 -#if SH_COMP == SH_COMP_GCC
   1.152 -			jit_int32_t param_base_offs =		16;
   1.153 -#elif SH_COMP == SH_COMP_MSVC
   1.154 -			jit_int32_t param_base_offs =		12;
   1.155 -#endif
   1.156  
   1.157  			// Memory return: first param is the address
   1.158  			jit_int32_t v_memret_addr = 0;
   1.159  			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.160  			{
   1.161 -#if SH_COMP == SH_COMP_GCC
   1.162 -				// gcc: now:	first param = mem ret addr
   1.163 -				//				second param = this pointer
   1.164 -				//				third param = actual first param
   1.165 -				v_memret_addr = 12;
   1.166 -				v_this += 4;
   1.167 -				param_base_offs += SIZE_PTR;
   1.168 -#elif SH_COMP == SH_COMP_MSVC
   1.169 -				v_memret_addr = param_base_offs;
   1.170 -				param_base_offs += SIZE_PTR;
   1.171 -#endif
   1.172 +				if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
   1.173 +				{
   1.174 +					// gcc: now:	first param = mem ret addr
   1.175 +					//				second param = this pointer
   1.176 +					//				third param = actual first param
   1.177 +					v_memret_addr = 12;
   1.178 +					v_this += 4;
   1.179 +					param_base_offs += SIZE_PTR;
   1.180 +				}
   1.181 +				else
   1.182 +				{
   1.183 +					v_memret_addr = param_base_offs;
   1.184 +					param_base_offs += SIZE_PTR;
   1.185 +				}
   1.186  			}
   1.187  
   1.188  			jit_int32_t v_ret_ptr = 0;
   1.189 @@ -1494,14 +1497,23 @@
   1.190  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
   1.191  			IA32_Pop_Reg(&m_HookFunc, REG_EBX);
   1.192  			IA32_Pop_Reg(&m_HookFunc, REG_EBP);
   1.193 -			MSVC_ONLY(IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize()));
   1.194 -			// gcc: Remove 4 bytes from stack on memory return
   1.195 -#if SH_COMP == SH_COMP_GCC
   1.196 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.197 -				IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
   1.198 +
   1.199 +			if (SH_COMP == SH_COMP_MSVC && !(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
   1.200 +			{
   1.201 +				// msvc without varargs:
   1.202 +				//  callee cleans the stack
   1.203 +				IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize());
   1.204 +			}
   1.205  			else
   1.206 -				IA32_Return(&m_HookFunc);
   1.207 -#endif
   1.208 +			{
   1.209 +				// gcc or msvc with varargs: caller cleans the stack
   1.210 +				//  exception: gcc removes the memret addr on memret: 
   1.211 +				if (SH_COMP == SH_COMP_GCC && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem))
   1.212 +					IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
   1.213 +				else
   1.214 +					IA32_Return(&m_HookFunc);
   1.215 +			}
   1.216 +			
   1.217  			
   1.218  			// Store pointer for later use
   1.219  			// m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer