This has come up before, but the questions are slightly different, and the answers were all quite unhelpful, so I'll try one more time.
I need 2 pieces information from the compiler which seem to be difficult to extract.
I want to find the vtable pointer for a given class without an instance of the class. The only reference I can find to the vtable symbol anywhere in the binary is in the constructor where it's assigned to new instances, and it's really awkward to get the pointer out of the constructor without calling it... I'm wondering if anyone can point to the vtable name mangling spec for common compilers (msvc, gcc, clang) so I can extern to the symbol explicitly? (I haven't been able to find this). My concern with this is that I suspect (at least on VC) that the symbol name has some characters that are illegal in C++ identifiers, so I'm not sure how to create a variable that links to it...
I need the actual function pointers for methods. The only syntax that seems to be available to approach member function pointers is the pointer-to-member operator, and that results in very compiler-specific output.
I have observed, GCC/Clang produce a nice little struct; { void *ptr_or_offset; size_t suspected_vtable; }. From that, it's easy enough to find the actual function pointer (assuming I have the vtable pointer! see #1).
MSVC is a little harder; pointer-to-members for virtuals are pointers to thunk functions that perform the virtual lookup. It seems the thunk's correlate with the vtable offset, so there is one thunk for each vtable offset. This strategy makes it very hard to; identify if the method is virtual or not, and if it is, get the vtable offset (and therefore the actual function pointer). I'm thinking maybe I can fabricate a table of thunk pointers for each vtable offset up to some N, then when I take a pointer-to-member, I can compare it with each item in the thunk table; if it is among them, I know it is virtual, and the vtable offset, so I can get the pointer.
So, this all sounds horrible, but it is what it is since C++ doesn't feel like syntax should be available to get these fundamental language primitives, and doesn't have proper delegates for some unknown reason!
Can anyone think of a better or more-direct mechanism to capture those pieces of data that I seek? Or any alternative solutions which would improve on portability would also be cool!
Cheers!
Edit: Considering there are other posts similar to this already filled with people saying that 'it's not portable', and 'don't do it', I'd like to request that you refrain from polluting this thread with more of the same. They are worthless comments that don't address the problem. This problem requires some creative thinking, impress us with the quality of your solution.
Edit 2: Not sure why I'm being down-voted. This is an interesting and largely unsolved problem. There is very little topical discussion on the internets.