Wrapping C++ in C.

Mike Stump mrs@windriver.com
Fri Feb 4 15:27:00 GMT 2000


> Date: Wed, 2 Feb 2000 23:31:22 -0500 (EST)
> From: Kevin Atkinson <kevinatk@home.com>

> 2) Is it possible to get a function pointer out of a C++ member function?

Yes, of course.  .__pfn_or_delta2.__pfn will get the pointer to member
function.  Other fields that will interest you, .__index, .__delta and
.__pfn_or_delta2.__delta2.  If your system uses thunks, quite a few of
these go away, and you're reduced to having just the pointer to
function.  The information is then buried in the thunk.  The name of
the thunk contains the information that would otherwise be in the
table.

> Now is it possible to set the function pointers directly equal to the
> member functions to avoid the cost of an extra function call?

Yes.

> What if the C++ methods were virtual?

Yes.

> 2b) What about the other way around.

Yes.

> If a C function implements Emulation is it even remotely possible to
> directly turn it into a C++ Emulation.  IE, manipulate the virtual
> table so that e->next() will really be calling a C function and not
> a C++ member function.

If you want to change vtables from real classes, to poke in addresses
of C functions, you can do that, but you'll need to define
WRITABLE_VTABLES in your compiler, see:

#ifndef WRITABLE_VTABLES

in the code.  Otherwise, you can just build up your own vtable, and
use it.  Examine the output of the compiler and mirror it.  You can
ignore rtti stuff (if you don't do EH).

> I release 2b is far fetched so please don't call me crazy.

The way functions/member functions/dtors/ctors are done is identical.

I understand that this is only a hint to getting started that that
using these techniques completely, you'll need more information.  The
rest of it you can get from checking out the compiler and the
generated code.  I don't want to make it too easy for you.  :-)
Or, I might entertain more questions...


Also, a disclaimer.  The information provided is undocumented compiler
internal details that change from time to time.  Using it is extremely
unportable and bad form.  The expression, `here's the loaded weapon,
don't point it at your head', comes to mind here.  With that said, go
play and have fun with it.  :-)


More information about the Gcc mailing list