Sunday, October 30, 2022

Digest for comp.lang.c++@googlegroups.com - 2 updates in 1 topic

Frederick Virchanza Gotham <cauldwell.thomas@gmail.com>: Oct 29 06:00PM -0700

I intend to manipulate the functionality of 'dynamic_cast' on x86_64 with the GNU compiler 'g++'.
 
But first I need a solid understanding of how the v-table is laid out with RTTI and so forth. I searched and searched and searched the web but I can't get a simple explanation so I've decided to take the machine code for dynamic_cast and run it through a C decompiler. If I take the assembler for 'dynamic_cast' and run it through the IDA decompiler, it gives me the following:
 
void *_dynamic_cast(void ****p, const void *tiBase, const void *tiDerived, ptrdiff_t s2d)
{
if ( !p ) return nullptr;
 
void ****q = (void****)( p + p[0][-2] / 8u );
 
void **value = p[0][-1];
 
if ( q[0][-1] == value )
{
typedef void (*FuncPtr)(void **, ptrdiff_t, uint64_t, const void *, void ****, const void *, void ****) __attribute__((fastcall));
 
FuncPtr *const pf = static_cast<FuncPtr*>(*value);
 
pf[7u](value,s2d,6LL,tiDerived,q,tiBase,p);
}
 
return nullptr;
}
 
So the first thing I notice here is that this function always returns a null pointer -- but obviously that would be useless.
 
The only thing I can imagine here is that the function call "pf[7u](. . .)" is doing some sort of long jump return. Is that what's happening?
 
Also I imagine that "pf[7u]" is the address of a thunk.
 
My manipulation will involve writing a new thunk and setting "pf[7u]" to my new thunk.
David Brown <david.brown@hesbynett.no>: Oct 30 10:09AM +0100

On 30/10/2022 02:00, Frederick Virchanza Gotham wrote:
 
> I intend to manipulate the functionality of 'dynamic_cast' on x86_64
> with the GNU compiler 'g++'.
 
I don't know what you are trying to do here, but it certainly sounds
like a bad idea...
 
> machine code for dynamic_cast and run it through a C decompiler. If I
> take the assembler for 'dynamic_cast' and run it through the IDA
> decompiler, it gives me the following:
 
Surely the obvious way to proceed is to download the gcc sources and
look at how dynamic_cast is actually implemented there? It seems crazy
to use a "decompiler" when the original source code is available. I am
not suggesting that the gcc sources are likely to be simple and clear in
this area, but they must surely be a better choice.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: