1 /* ======== SourceHook ========
3 * No warranties of any kind
5 * This file provides a way for getting information about a member function.
6 * ============================
11 #ifndef __SHINT_MEMFUNC_INFO_H__
12 #define __SHINT_MEMFUNC_INFO_H__
19 // I believe this was originally going to be in the C++ standard but
20 // was left out by accident. It's even milder than static_cast.
21 // I use it instead of static_cast<> to emphasize that I'm not doing
23 // Usage is identical to static_cast<>
24 template <class OutputClass, class InputClass>
25 inline OutputClass implicit_cast(InputClass input){
32 bool isVirtual; // Is the function virtual?
33 int thisptroffs; // The this pointer the function expects to be called with
34 // If -1, you need to call the GetFuncInfo_GetThisPtr function
35 int vtblindex; // The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
36 int vtbloffs; // The vtable pointer
39 // Ideas by Don Clugston.
40 // Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
42 template<int N> struct MFI_Impl
44 template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
46 static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
50 # if SH_COMP == SH_COMP_GCC
52 template<> struct MFI_Impl<8> // All of these have size==8
58 void *funcadr; // always even
59 int vtable_index_plus1; // = vindex+1, always odd
63 template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
65 GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)mfp;
66 out.thisptroffs = mfp_detail->delta;
67 if (mfp_detail->vtable_index_plus1 & 1)
69 out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4;
74 out.isVirtual = false;
78 # elif SH_COMP == SH_COMP_MSVC
82 int MFI_GetVtblOffset(void *mfp)
84 unsigned char *addr = (unsigned char*)mfp;
85 if (*addr == 0xE9) // Jmp
88 // Check where it'd jump
89 addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
92 // Check whether it's a virtual function call
93 // They look like this:
94 // 004125A0 8B 01 mov eax,dword ptr [ecx]
95 // 004125A2 FF 60 04 jmp dword ptr [eax+4]
97 // 00411B80 8B 01 mov eax,dword ptr [ecx]
98 // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
99 if (*addr++ == 0x8B && *addr++ == 0x01 && *addr++ == 0xFF)
105 else if (*addr == 0xA0)
107 return *((unsigned int*)++addr) / 4;
109 else if (*addr == 0x20)
118 template<> struct MFI_Impl<4> // simple ones
120 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
122 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
123 out.isVirtual = out.vtblindex >= 0 ? true : false;
129 template<> struct MFI_Impl<8> // more complicated ones!
131 struct MSVC_MemFunPtr2
136 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
138 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
139 out.isVirtual = out.vtblindex >= 0 ? true : false;
140 out.thisptr = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
145 // By Don Clugston, adapted
146 template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG
148 class __single_inheritance GenericClass;
149 class GenericClass {};
151 struct MicrosoftVirtualMFP {
152 void (GenericClass::*codeptr)(); // points to the actual member function
153 int delta; // #bytes to be added to the 'this' pointer
154 int vtable_index; // or 0 if no virtual inheritance
157 struct GenericVirtualClass : virtual public GenericClass
159 typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
160 GenericVirtualClass * GetThis() { return this; }
163 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
165 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
166 out.isVirtual = out.vtblindex >= 0 ? true : false;
170 GenericClass* (T::*ProbeFunc)();
171 MicrosoftVirtualMFP s;
175 GenericVirtualClass::ProbePtrType virtfunc;
176 MicrosoftVirtualMFP s;
179 printf("%d %d", sizeof(mfp), sizeof(u.ProbeFunc));
180 // Check that the horrible_cast<>s will work
181 typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
182 && sizeof(mfp)==sizeof(u.ProbeFunc)
183 && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
184 // Unfortunately, taking the address of a MF prevents it from being inlined, so
185 // this next line can't be completely optimised away by the compiler.
186 u2.virtfunc = &GenericVirtualClass::GetThis;
187 u.s.codeptr = u2.s.codeptr;
188 out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
193 // Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
194 // unknown_inheritance classes go here
195 // This is probably the ugliest bit of code I've ever written. Look at the casts!
196 // There is a compiler bug in MSVC6 which prevents it from using this code.
197 template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN
199 template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
201 out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
202 out.isVirtual = out.vtblindex >= 0 ? true : false;
204 // The member function pointer is 16 bytes long. We can't use a normal cast, but
205 // we can use a union to do the conversion.
208 // In VC++ and ICL, an unknown_inheritance member pointer
209 // is internally defined as:
211 void *m_funcaddress; // points to the actual member function
212 int delta; // #bytes to be added to the 'this' pointer
213 int vtordisp; // #bytes to add to 'this' to find the vtable
214 int vtable_index; // or 0 if no virtual inheritance
217 // Check that the horrible_cast will work
218 typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
220 int virtual_delta = 0;
221 if (u.s.vtable_index) { // Virtual inheritance is used
223 // First, get to the vtable.
224 // It is 'vtordisp' bytes from the start of the class.
225 int * vtable = *reinterpret_cast<int **>(
226 reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
228 // 'vtable_index' tells us where in the table we should be looking.
229 virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
230 reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
231 // The int at 'virtual_delta' gives us the amount to add to 'this'.
232 // Finally we can add the three components together. Phew!
233 out.thisptr = reinterpret_cast<void *>(
234 reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
236 out.vtbloffs = u.s.vtordisp;
237 out.thisptroffs = -1;
241 out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
242 out.thisptroffs = u.s.delta;
247 # error Unsupported compiler
250 template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
252 MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);