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]
104 if (*addr++ == 0x8B && *addr++ == 0x01 && *addr++ == 0xFF)
110 else if (*addr == 0xA0)
112 return *((unsigned int*)++addr) / 4;
114 else if (*addr == 0x20)
123 template<> struct MFI_Impl<4> // simple ones
125 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
127 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
128 out.isVirtual = out.vtblindex >= 0 ? true : false;
134 template<> struct MFI_Impl<8> // more complicated ones!
136 struct MSVC_MemFunPtr2
141 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
143 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
144 out.isVirtual = out.vtblindex >= 0 ? true : false;
145 out.thisptroffs = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
150 // By Don Clugston, adapted
151 template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG
153 class __single_inheritance GenericClass;
154 class GenericClass {};
156 struct MicrosoftVirtualMFP {
157 void (GenericClass::*codeptr)(); // points to the actual member function
158 int delta; // #bytes to be added to the 'this' pointer
159 int vtable_index; // or 0 if no virtual inheritance
162 struct GenericVirtualClass : virtual public GenericClass
164 typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
165 GenericVirtualClass * GetThis() { return this; }
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;
176 GenericClass* (T::*ProbeFunc)();
177 MicrosoftVirtualMFP s;
181 GenericVirtualClass::ProbePtrType virtfunc;
182 MicrosoftVirtualMFP s;
185 // Check that the horrible_cast<>s will work
186 typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
187 && sizeof(mfp)==sizeof(u.ProbeFunc)
188 && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
189 // Unfortunately, taking the address of a MF prevents it from being inlined, so
190 // this next line can't be completely optimised away by the compiler.
191 u2.virtfunc = &GenericVirtualClass::GetThis;
192 u.s.codeptr = u2.s.codeptr;
193 out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
195 out.thisptroffs = -1;
200 // Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
201 // unknown_inheritance classes go here
202 // This is probably the ugliest bit of code I've ever written. Look at the casts!
203 // There is a compiler bug in MSVC6 which prevents it from using this code.
204 template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN
206 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
208 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
209 out.isVirtual = out.vtblindex >= 0 ? true : false;
211 // The member function pointer is 16 bytes long. We can't use a normal cast, but
212 // we can use a union to do the conversion.
215 // In VC++ and ICL, an unknown_inheritance member pointer
216 // is internally defined as:
218 void *m_funcaddress; // points to the actual member function
219 int delta; // #bytes to be added to the 'this' pointer
220 int vtordisp; // #bytes to add to 'this' to find the vtable
221 int vtable_index; // or 0 if no virtual inheritance
224 // Check that the horrible_cast will work
225 typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
227 int virtual_delta = 0;
228 if (u.s.vtable_index) { // Virtual inheritance is used
230 // First, get to the vtable.
231 // It is 'vtordisp' bytes from the start of the class.
232 int * vtable = *reinterpret_cast<int **>(
233 reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
235 // 'vtable_index' tells us where in the table we should be looking.
236 virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
237 reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
238 // The int at 'virtual_delta' gives us the amount to add to 'this'.
239 // Finally we can add the three components together. Phew!
240 out.thisptr = reinterpret_cast<void *>(
241 reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
243 out.vtbloffs = u.s.vtordisp;
244 out.thisptroffs = -1;
248 out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
249 out.thisptroffs = u.s.delta;
254 # error Unsupported compiler
257 template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
259 MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);