1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
12 * @brief This file provides a way for getting information about a member function.
13 * @file sh_memfuncinfo.h
16 #ifndef __SHINT_MEMFUNC_INFO_H__
17 #define __SHINT_MEMFUNC_INFO_H__
24 // I believe this was originally going to be in the C++ standard but
25 // was left out by accident. It's even milder than static_cast.
26 // I use it instead of static_cast<> to emphasize that I'm not doing
28 // Usage is identical to static_cast<>
29 template <class OutputClass, class InputClass>
30 inline OutputClass implicit_cast(InputClass input){
37 bool isVirtual; // Is the function virtual?
38 int thisptroffs; // The this pointer the function expects to be called with
39 // If -1, you need to call the GetFuncInfo_GetThisPtr function
40 int vtblindex; // The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
41 int vtbloffs; // The vtable pointer
44 // Ideas by Don Clugston.
45 // Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
47 template<int N> struct MFI_Impl
49 template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
51 static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
55 # if SH_COMP == SH_COMP_GCC
57 template<> struct MFI_Impl<8> // All of these have size==8
63 void *funcadr; // always even
64 int vtable_index_plus1; // = vindex+1, always odd
68 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
70 GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)&mfp;
71 out.thisptroffs = mfp_detail->delta;
72 if (mfp_detail->vtable_index_plus1 & 1)
74 out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4;
79 out.isVirtual = false;
83 # elif SH_COMP == SH_COMP_MSVC
87 int MFI_GetVtblOffset(void *mfp)
89 unsigned char *addr = (unsigned char*)mfp;
90 if (*addr == 0xE9) // Jmp
93 // Check where it'd jump
94 addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
97 // Check whether it's a virtual function call
98 // They look like this:
99 // 004125A0 8B 01 mov eax,dword ptr [ecx]
100 // 004125A2 FF 60 04 jmp dword ptr [eax+4]
102 // 00411B80 8B 01 mov eax,dword ptr [ecx]
103 // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
105 // However, for vararg functions, they look like this:
106 // 0048F0B0 8B 44 24 04 mov eax,dword ptr [esp+4]
107 // 0048F0B4 8B 00 mov eax,dword ptr [eax]
108 // 0048F0B6 FF 60 08 jmp dword ptr [eax+8]
110 // 0048F0B0 8B 44 24 04 mov eax,dword ptr [esp+4]
111 // 0048F0B4 8B 00 mov eax,dword ptr [eax]
112 // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
114 // With varargs, the this pointer is passed as if it was the first argument
117 if (addr[0] == 0x8B && addr[1] == 0x44 && addr[2] == 0x24 && addr[3] == 0x04 &&
118 addr[4] == 0x8B && addr[5] == 0x00)
123 else if (addr[0] == 0x8B && addr[1] == 0x01)
137 else if (*addr == 0xA0)
139 return *((unsigned int*)++addr) / 4;
141 else if (*addr == 0x20)
150 template<> struct MFI_Impl<4> // simple ones
152 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
154 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
155 out.isVirtual = out.vtblindex >= 0 ? true : false;
161 template<> struct MFI_Impl<8> // more complicated ones!
163 struct MSVC_MemFunPtr2
168 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
170 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
171 out.isVirtual = out.vtblindex >= 0 ? true : false;
172 out.thisptroffs = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
177 // By Don Clugston, adapted
178 template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG
180 class __single_inheritance GenericClass;
181 class GenericClass {};
183 struct MicrosoftVirtualMFP {
184 void (GenericClass::*codeptr)(); // points to the actual member function
185 int delta; // #bytes to be added to the 'this' pointer
186 int vtable_index; // or 0 if no virtual inheritance
189 struct GenericVirtualClass : virtual public GenericClass
191 typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
192 GenericVirtualClass * GetThis() { return this; }
195 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
197 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
198 out.isVirtual = out.vtblindex >= 0 ? true : false;
203 GenericClass* (T::*ProbeFunc)();
204 MicrosoftVirtualMFP s;
208 GenericVirtualClass::ProbePtrType virtfunc;
209 MicrosoftVirtualMFP s;
212 // Check that the horrible_cast<>s will work
213 typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
214 && sizeof(mfp)==sizeof(u.ProbeFunc)
215 && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
216 // Unfortunately, taking the address of a MF prevents it from being inlined, so
217 // this next line can't be completely optimised away by the compiler.
218 u2.virtfunc = &GenericVirtualClass::GetThis;
219 u.s.codeptr = u2.s.codeptr;
220 out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
222 out.thisptroffs = -1;
227 // Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
228 // unknown_inheritance classes go here
229 // This is probably the ugliest bit of code I've ever written. Look at the casts!
230 // There is a compiler bug in MSVC6 which prevents it from using this code.
231 template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN
233 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
235 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
236 out.isVirtual = out.vtblindex >= 0 ? true : false;
238 // The member function pointer is 16 bytes long. We can't use a normal cast, but
239 // we can use a union to do the conversion.
242 // In VC++ and ICL, an unknown_inheritance member pointer
243 // is internally defined as:
245 void *m_funcaddress; // points to the actual member function
246 int delta; // #bytes to be added to the 'this' pointer
247 int vtordisp; // #bytes to add to 'this' to find the vtable
248 int vtable_index; // or 0 if no virtual inheritance
251 // Check that the horrible_cast will work
252 typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
254 int virtual_delta = 0;
255 if (u.s.vtable_index) { // Virtual inheritance is used
257 // First, get to the vtable.
258 // It is 'vtordisp' bytes from the start of the class.
259 int * vtable = *reinterpret_cast<int **>(
260 reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
262 // 'vtable_index' tells us where in the table we should be looking.
263 virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
264 reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
265 // The int at 'virtual_delta' gives us the amount to add to 'this'.
266 // Finally we can add the three components together. Phew!
267 out.thisptr = reinterpret_cast<void *>(
268 reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
270 out.vtbloffs = u.s.vtordisp;
271 out.thisptroffs = -1;
275 out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
276 out.thisptroffs = u.s.delta;
281 # error Unsupported compiler
284 template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
286 MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);