sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 463 7a413a63426f
parent 461 2fa049f36721
child 464 b340e8665151
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Sat Nov 17 09:53:58 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp	Sun Nov 18 11:58:07 2007 +0000
     1.3 @@ -848,7 +848,7 @@
     1.4  		}
     1.5  
     1.6  		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
     1.7 -			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
     1.8 +			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
     1.9  		{
    1.10  			jitoffs_t counter, tmppos;
    1.11  			jitoffs_t counter2, tmppos2;
    1.12 @@ -894,8 +894,17 @@
    1.13  			//   eax = [eax+2*SIZE_PTR]
    1.14  			//   call eax
    1.15  			//   gcc: clean up 
    1.16 +			
    1.17 +			jit_int32_t gcc_clean_bytes = 0;
    1.18 +			// vafmt: push va_buf
    1.19 +			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
    1.20 +			{
    1.21 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
    1.22 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.23 +				gcc_clean_bytes += SIZE_PTR;
    1.24 +			}
    1.25  
    1.26 -			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
    1.27 +			gcc_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 @@ -953,7 +962,7 @@
    1.32  		}
    1.33  
    1.34  		void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
    1.35 -			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
    1.36 +			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
    1.37  		{
    1.38  			jitoffs_t counter, tmppos;
    1.39  			jitoffs_t counter2, tmppos2;
    1.40 @@ -1005,8 +1014,19 @@
    1.41  			tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
    1.42  			m_HookFunc.start_count(counter2);
    1.43  
    1.44 +			jit_int32_t gcc_clean_bytes = 0;
    1.45 +			
    1.46 +			// vafmt: push va_buf, then "%s"
    1.47 +			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
    1.48 +			{
    1.49 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
    1.50 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
    1.51 +				IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
    1.52 +				gcc_clean_bytes += 2*SIZE_PTR;
    1.53 +			}
    1.54 +
    1.55  			// push params
    1.56 -			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
    1.57 +			gcc_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
    1.58  
    1.59  			// thisptr
    1.60  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
    1.61 @@ -1248,21 +1268,22 @@
    1.62  			//   IHookContext *pContext					ebp - 24		-4
    1.63  			//  == 3 ptrs + 3 enums = 24 bytes
    1.64  			//
    1.65 -			// varargs:
    1.66 -			//   va_list argptr							ebp - 28		-4
    1.67 -			//
    1.68  			// non-void: add:
    1.69 -			//   my_rettype *ret_ptr					ebp - 28 (va: -4)		-4
    1.70 -			//   my_rettype orig_ret					ebp - 28 (va: -4) - sizeof(my_rettype)			-4
    1.71 -			//   my_rettype override_ret				ebp - 28 (va: -4) - sizeof(my_rettype)*2			-4
    1.72 -			//   my_rettype plugin_ret					ebp - 28 (va: -4) - sizeof(my_rettype)*3			-4
    1.73 +			//   my_rettype *ret_ptr					ebp - 28 		-4
    1.74 +			//   my_rettype orig_ret					ebp - 28 - sizeof(my_rettype)			-4
    1.75 +			//   my_rettype override_ret				ebp - 28 - sizeof(my_rettype)*2			-4
    1.76 +			//   my_rettype plugin_ret					ebp - 28 - sizeof(my_rettype)*3			-4
    1.77  			//  == + 3 * sizeof(my_rettype) bytes
    1.78  
    1.79  			// if required:
    1.80 -			//   my_rettype place_for_memret			ebp - 28 (va: -4) - sizeof(my_rettype)*4			-4
    1.81 +			//   my_rettype place_for_memret			ebp - 28 - sizeof(my_rettype)*4			-4
    1.82  
    1.83  			// gcc only: if required:
    1.84 -			//   place forced byref params              ebp - 28 (va: -4) - sizeof(my_rettype)*{4 or 5}
    1.85 +			//   place forced byref params              ebp - 28 - sizeof(my_rettype)*{4 or 5}
    1.86 +			//
    1.87 +			// varargs:
    1.88 +			//   va_list argptr
    1.89 +			//   char va_buf[something];
    1.90  			
    1.91  
    1.92  			const jit_int8_t v_vfnptr_origentry =	AddVarToFrame(SIZE_PTR);
    1.93 @@ -1271,10 +1292,6 @@
    1.94  			const jit_int8_t v_cur_res =			AddVarToFrame(sizeof(META_RES));
    1.95  			const jit_int8_t v_iter =				AddVarToFrame(SIZE_PTR);
    1.96  			const jit_int8_t v_pContext =			AddVarToFrame(SIZE_PTR);
    1.97 -			// Only exists for varargs functions
    1.98 -			const jit_int8_t v_va_argptr =			(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) ?
    1.99 -														AddVarToFrame(sizeof(va_list)) :
   1.100 -														0;
   1.101  			
   1.102  #if SH_COMP == SH_COMP_GCC
   1.103  			jit_int32_t param_base_offs =		16;
   1.104 @@ -1319,25 +1336,60 @@
   1.105  			}
   1.106  
   1.107  			jit_int32_t v_place_fbrr_base  = 0;
   1.108 -			if (GetForcedByRefParamsSize())
   1.109 +			if (SH_COMP == SH_COMP_GCC && GetForcedByRefParamsSize())
   1.110  			{
   1.111  				v_place_fbrr_base =		AddVarToFrame(GetForcedByRefParamsSize());
   1.112  			}
   1.113  
   1.114 +			// Only exists for varargs functions
   1.115 +			jit_int32_t v_va_argptr = 0;
   1.116 +			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
   1.117 +			{
   1.118 +				v_va_argptr = AddVarToFrame(SIZE_PTR);
   1.119 +			}
   1.120 +			
   1.121 +			jit_int32_t v_va_buf = 0;
   1.122 +			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
   1.123 +			{
   1.124 +				v_va_buf = AddVarToFrame(SourceHook::STRBUF_LEN);
   1.125 +			}
   1.126 +			
   1.127  			IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
   1.128  
   1.129  			// init status localvar
   1.130  			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
   1.131  
   1.132 -			// VarArgs: init argptr
   1.133 +			// VarArgs: init argptr & format
   1.134  			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
   1.135  			{
   1.136  				// argptr = first vararg param
   1.137 -				// lea eax, [esp + param_base_offs + paramssize]
   1.138 +				// lea eax, [ebp + param_base_offs + paramssize]
   1.139  				// mov argptr, eax
   1.140  
   1.141 -				IA32_Lea_Reg_DispRegMultImm32(&m_HookFunc, REG_EAX, REG_NOIDX, REG_ESP, NOSCALE, param_base_offs + GetParamsStackSize());
   1.142 -				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, v_va_argptr, REG_EAX);
   1.143 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_base_offs + GetParamsStackSize() + SIZE_PTR);	// +SIZE_PTR: last const char * is not in protoinfo
   1.144 +				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_va_argptr);
   1.145 +			}
   1.146 +			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
   1.147 +			{
   1.148 +				// vsnprintf
   1.149 +				
   1.150 +				// push valist, fmt param, maxsize, buffer
   1.151 +				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   1.152 +				IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_base_offs + GetParamsStackSize());		// last given param (+4-4, see above)
   1.153 +				IA32_Push_Imm32(&m_HookFunc, SourceHook::STRBUF_LEN - 1);
   1.154 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
   1.155 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.156 +				
   1.157 +				// call
   1.158 +				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(&vsnprintf));
   1.159 +				IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.160 +
   1.161 +				// Clean up (cdecl)
   1.162 +				IA32_Add_Rm_Imm32(&m_HookFunc, REG_ESP, 0x10, MOD_REG);
   1.163 +
   1.164 +				// Set trailing zero
   1.165 +				IA32_Xor_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_REG);
   1.166 +				IA32_Mov_Rm8_Reg8_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_va_buf + SourceHook::STRBUF_LEN - 1);
   1.167  			}
   1.168  			
   1.169  			// Call constructors for ret vars if required
   1.170 @@ -1374,15 +1426,15 @@
   1.171  
   1.172  			// ********************** call pre hooks **********************
   1.173  			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
   1.174 -				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   1.175 +				v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
   1.176  
   1.177  			// ********************** call orig func **********************
   1.178  			GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
   1.179 -				v_override_ret, v_place_for_memret, v_place_fbrr_base);
   1.180 +				v_override_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
   1.181  
   1.182  			// ********************** call post hooks **********************
   1.183  			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
   1.184 -				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   1.185 +				v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
   1.186  
   1.187  			// ********************** end context and return **********************
   1.188