[cxx-abi-dev] Function descriptors in vtable

Cary Coutant cary at cup.hp.com
Wed Jan 22 01:22:58 UTC 2003


> You could, however, use the GP/Address pair like this:
>
>  GP: Real function pointer
>  Address: Pointer to stub code
>
> The stub code would simply look like:
>
> (*GP)();
>
> All virtual functions would use this same stub.

Noam had already contacted us at HP separately, and we were prepared to 
offer a C language extension that would allow them to declare the 
vtable structure in C, but Mark's suggestion here is simple and elegant 
(thanks, Mark!), and we like it much better (not even considering the 
fact that it means we don't have to add a compiler extension).

I'll carry this a bit further, and demonstrate how this proposal could 
be done in the context of Noam's example:

struct bbb;

typedef struct {
#ifdef ILP32
    uint64_t _stub_f;
    uint32_t _pad_f;
    int (*f)(void *b, int x);
#else /* LP64 */
    uint64_t _stub_f;
    int (*f)(void *b, int x);
#endif
} bbb_vtable;

typedef struct {
    bbb_vtable *vt;
} bbb;

As Mark said, every vtable entry can point to the same stub, which can 
be somewhere in MainWin's runtime library. The stub could look 
something like this:

_mainwin_com_stub:
#ifdef ILP32
         addp4   gp = r0, gp
         ;;
#endif
         ld8     r31 = [gp], 8
         ;;
         ld8     gp = [gp]
         mov     b6 = r31
         ;;
         br.call.sptk.many [b6]
         ;;

You can initialize the vtable statically using assembly code:

bbb_vtable:
         data8   _mainwin_com_stub
         data8   @fptr(bbb_f)

Or dynamically in C:

    extern int _mainwin_com_stub;
    extern int bbb_f(void *b, int x);

    bbb_vtable._stub_f = &_mainwin_com_stub;
    bbb_vtable.f = bbb_f;

(Notice we lie and declare the stub as a data symbol. This keeps the 
compiler from generating the FPTR relocation that would create a 
function pointer rather than the simple code pointer that we want.)

-cary




More information about the cxx-abi-dev mailing list