1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/sh_memfuncinfo.h Thu Apr 14 12:54:13 2005 +0000
1.3 @@ -0,0 +1,257 @@
1.4 +/* ======== SourceHook ========
1.5 +* By PM
1.6 +* No warranties of any kind
1.7 +*
1.8 +* This file provides a way for getting information about a member function.
1.9 +* ============================
1.10 +*/
1.11 +
1.12 +
1.13 +
1.14 +#ifndef __SHINT_MEMFUNC_INFO_H__
1.15 +#define __SHINT_MEMFUNC_INFO_H__
1.16 +
1.17 +namespace SourceHook
1.18 +{
1.19 +
1.20 + // Don Clugston:
1.21 + // implicit_cast< >
1.22 + // I believe this was originally going to be in the C++ standard but
1.23 + // was left out by accident. It's even milder than static_cast.
1.24 + // I use it instead of static_cast<> to emphasize that I'm not doing
1.25 + // anything nasty.
1.26 + // Usage is identical to static_cast<>
1.27 + template <class OutputClass, class InputClass>
1.28 + inline OutputClass implicit_cast(InputClass input){
1.29 + return input;
1.30 + }
1.31 +
1.32 +
1.33 + struct MemFuncInfo
1.34 + {
1.35 + bool isVirtual; // Is the function virtual?
1.36 + int thisptroffs; // The this pointer the function expects to be called with
1.37 + // If -1, you need to call the GetFuncInfo_GetThisPtr function
1.38 + int vtblindex; // The function's index in the vtable (0-based, 1=second entry, 2=third entry, ...)
1.39 + int vtbloffs; // The vtable pointer
1.40 + };
1.41 +
1.42 + // Ideas by Don Clugston.
1.43 + // Check out his excellent paper: http://www.codeproject.com/cpp/FastDelegate.asp
1.44 +
1.45 + template<int N> struct MFI_Impl
1.46 + {
1.47 + template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
1.48 + {
1.49 + static char weird_memfunc_pointer_exclamation_mark_arrow_error[N-1000];
1.50 + }
1.51 + };
1.52 +
1.53 +# if SH_COMP == SH_COMP_GCC
1.54 +
1.55 + template<> struct MFI_Impl<8> // All of these have size==8
1.56 + {
1.57 + struct GCC_MemFunPtr
1.58 + {
1.59 + union
1.60 + {
1.61 + void *funcadr; // always even
1.62 + int vtable_index_plus1; // = vindex+1, always odd
1.63 + };
1.64 + int delta;
1.65 + };
1.66 + template<class MFP> static inline void GetFuncInfo(MFP *mfp, MemFuncInfo &out)
1.67 + {
1.68 + GCC_MemFunPtr *mfp_detail = (GCC_MemFunPtr*)mfp;
1.69 + out.thisptroffs = mfp_detail->delta;
1.70 + if (mfp_detail->vtable_index_plus1 & 1)
1.71 + {
1.72 + out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4;
1.73 + out.vtbloffs = 0;
1.74 + out.isVirtual = true;
1.75 + }
1.76 + else
1.77 + out.isVirtual = false;
1.78 + }
1.79 + };
1.80 +
1.81 +# elif SH_COMP == SH_COMP_MSVC
1.82 +
1.83 + namespace
1.84 + {
1.85 + int MFI_GetVtblOffset(void *mfp)
1.86 + {
1.87 + unsigned char *addr = (unsigned char*)mfp;
1.88 + if (*addr == 0xE9) // Jmp
1.89 + {
1.90 + // May or may not be!
1.91 + // Check where it'd jump
1.92 + addr += 5 /*size of the instruction*/ + *(unsigned long*)(addr + 1);
1.93 + }
1.94 +
1.95 + // Check whether it's a virtual function call
1.96 + // They look like this:
1.97 + // 004125A0 8B 01 mov eax,dword ptr [ecx]
1.98 + // 004125A2 FF 60 04 jmp dword ptr [eax+4]
1.99 + // ==OR==
1.100 + // 00411B80 8B 01 mov eax,dword ptr [ecx]
1.101 + // 00411B82 FF A0 18 03 00 00 jmp dword ptr [eax+318h]
1.102 + if (*addr++ == 0x8B && *addr++ == 0x01 && *addr++ == 0xFF)
1.103 + {
1.104 + if (*addr == 0x60)
1.105 + {
1.106 + return *++addr / 4;
1.107 + }
1.108 + else if (*addr == 0xA0)
1.109 + {
1.110 + return *((unsigned int*)++addr) / 4;
1.111 + }
1.112 + else if (*addr == 0x20)
1.113 + return 0;
1.114 + else
1.115 + return -1;
1.116 + }
1.117 + return -1;
1.118 + }
1.119 + }
1.120 +
1.121 + template<> struct MFI_Impl<4> // simple ones
1.122 + {
1.123 + template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.124 + {
1.125 + out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.126 + out.isVirtual = out.vtblindex >= 0 ? true : false;
1.127 + out.thisptroffs = 0;
1.128 + out.vtbloffs = 0;
1.129 + }
1.130 + };
1.131 +
1.132 + template<> struct MFI_Impl<8> // more complicated ones!
1.133 + {
1.134 + struct MSVC_MemFunPtr2
1.135 + {
1.136 + void *funcadr;
1.137 + int delta;
1.138 + };
1.139 + template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.140 + {
1.141 + out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.142 + out.isVirtual = out.vtblindex >= 0 ? true : false;
1.143 + out.thisptr = reinterpret_cast<MSVC_MemFunPtr2*>(&mfp)->delta;
1.144 + out.vtbloffs = 0;
1.145 + }
1.146 + };
1.147 +
1.148 + // By Don Clugston, adapted
1.149 + template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG
1.150 + {
1.151 + class __single_inheritance GenericClass;
1.152 + class GenericClass {};
1.153 +
1.154 + struct MicrosoftVirtualMFP {
1.155 + void (GenericClass::*codeptr)(); // points to the actual member function
1.156 + int delta; // #bytes to be added to the 'this' pointer
1.157 + int vtable_index; // or 0 if no virtual inheritance
1.158 + };
1.159 +
1.160 + struct GenericVirtualClass : virtual public GenericClass
1.161 + {
1.162 + typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
1.163 + GenericVirtualClass * GetThis() { return this; }
1.164 + };
1.165 +
1.166 + template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.167 + {
1.168 + out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.169 + out.isVirtual = out.vtblindex >= 0 ? true : false;
1.170 + // This pointer
1.171 + union {
1.172 + MFP func;
1.173 + GenericClass* (T::*ProbeFunc)();
1.174 + MicrosoftVirtualMFP s;
1.175 + } u;
1.176 + u.func = mfp;
1.177 + union {
1.178 + GenericVirtualClass::ProbePtrType virtfunc;
1.179 + MicrosoftVirtualMFP s;
1.180 + } u2;
1.181 +
1.182 + printf("%d %d", sizeof(mfp), sizeof(u.ProbeFunc));
1.183 + // Check that the horrible_cast<>s will work
1.184 + typedef int ERROR_CantUsehorrible_cast[sizeof(mfp)==sizeof(u.s)
1.185 + && sizeof(mfp)==sizeof(u.ProbeFunc)
1.186 + && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
1.187 + // Unfortunately, taking the address of a MF prevents it from being inlined, so
1.188 + // this next line can't be completely optimised away by the compiler.
1.189 + u2.virtfunc = &GenericVirtualClass::GetThis;
1.190 + u.s.codeptr = u2.s.codeptr;
1.191 + out.thisptroffs = (reinterpret_cast<T*>(NULL)->*u.ProbeFunc)();
1.192 + out.vtbloffs = 0;
1.193 + }
1.194 + };
1.195 +
1.196 + // Don: Nasty hack for Microsoft and Intel (IA32 and Itanium)
1.197 + // unknown_inheritance classes go here
1.198 + // This is probably the ugliest bit of code I've ever written. Look at the casts!
1.199 + // There is a compiler bug in MSVC6 which prevents it from using this code.
1.200 + template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN
1.201 + {
1.202 + template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
1.203 + {
1.204 + out.vtblindex = MFI_GetVtblOffset(*(void**)&mfp);
1.205 + out.isVirtual = out.vtblindex >= 0 ? true : false;
1.206 +
1.207 + // The member function pointer is 16 bytes long. We can't use a normal cast, but
1.208 + // we can use a union to do the conversion.
1.209 + union {
1.210 + MFP func;
1.211 + // In VC++ and ICL, an unknown_inheritance member pointer
1.212 + // is internally defined as:
1.213 + struct {
1.214 + void *m_funcaddress; // points to the actual member function
1.215 + int delta; // #bytes to be added to the 'this' pointer
1.216 + int vtordisp; // #bytes to add to 'this' to find the vtable
1.217 + int vtable_index; // or 0 if no virtual inheritance
1.218 + } s;
1.219 + } u;
1.220 + // Check that the horrible_cast will work
1.221 + typedef int ERROR_CantUsehorrible_cast[sizeof(u.func)==sizeof(u.s)? 1 : -1];
1.222 + u.func = mfp;
1.223 + int virtual_delta = 0;
1.224 + if (u.s.vtable_index) { // Virtual inheritance is used
1.225 + /*
1.226 + // First, get to the vtable.
1.227 + // It is 'vtordisp' bytes from the start of the class.
1.228 + int * vtable = *reinterpret_cast<int **>(
1.229 + reinterpret_cast<char *>(thisptr) + u.s.vtordisp );
1.230 +
1.231 + // 'vtable_index' tells us where in the table we should be looking.
1.232 + virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
1.233 + reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
1.234 + // The int at 'virtual_delta' gives us the amount to add to 'this'.
1.235 + // Finally we can add the three components together. Phew!
1.236 + out.thisptr = reinterpret_cast<void *>(
1.237 + reinterpret_cast<char *>(thisptr) + u.s.delta + virtual_delta);
1.238 + */
1.239 + out.vtbloffs = u.s.vtordisp;
1.240 + out.thisptroffs = -1;
1.241 + }
1.242 + else
1.243 + {
1.244 + out.vtbloffs = out.vtblindex < 0 ? 0 : u.s.delta;
1.245 + out.thisptroffs = u.s.delta;
1.246 + }
1.247 + };
1.248 + };
1.249 +# else
1.250 +# error Unsupported compiler
1.251 +# endif
1.252 +
1.253 + template<class X> inline void GetFuncInfo(X mfp, MemFuncInfo &out)
1.254 + {
1.255 + MFI_Impl<sizeof(mfp)>::GetFuncInfo(mfp, out);
1.256 + }
1.257 +
1.258 +}
1.259 +
1.260 +#endif
1.261 \ No newline at end of file