2

I checked the assembly code generated by GCC for following C++ code snippet.

class Interface {
public:
 virtual int f() = 0;
 virtual int g() = 0;
};

class Concrete : public Interface {
public:
 virtual int f();
 virtual int g();
};

Seams there is two long in each vtable, and the assembly code seams doesn't use it, so what is the purpose of those two viable?

          .globl __ZTV8Concrete
          .const_data
          .align 3
  __ZTV8Concrete:
          .long   0   <<<< THOS TWO LONG DOESN"T USED.
          .long   0 
          .long   __ZN8Concrete1gEv
          .long   __ZN8Concrete1fEv
          .globl __ZTV9Interface
          .weak_definition __ZTV9Interface
          .section __DATA,__const_coal,coalesced
          .align 3
  __ZTV9Interface:
          .long   0    <<<< THOS TWO LONG DOESN"T USED ALSO.
          .long   0 
          .long   ___cxa_pure_virtual
          .long   ___cxa_pure_virtual
          .section __TEXT,__textcoal_nt,coalesced,pure_instructions
          .weak_definition        ___x86.get_pc_thunk.cx
          .private_extern ___x86.get_pc_thunk.

The constructor:

  __ZN8ConcreteC1Ev:
  LFB8:
          pushl   %ebp
  LCFI9:
          movl    %esp, %ebp
  LCFI10:
          pushl   %ebx
          subl    $20, %esp
  LCFI11:
          call    ___x86.get_pc_thunk.bx
  L2$pb:
          movl    8(%ebp), %eax
          movl    %eax, (%esp)
          call    __ZN9InterfaceC2Ev
          movl    8(%ebp), %edx
2>        leal    __ZTV8Concrete-L2$pb(%ebx), %eax
          leal    8(%eax), %eax    <<<< SKIP THE TWO 2 LONG for VTABLE
          movl    %eax, (%edx)
          addl    $20, %esp
          popl    %ebx
ZijingWu
  • 3,350
  • 3
  • 25
  • 40
  • 1
    Is the assembly for the source? I see a `__ZN8Concrete1gEv`... a `g()` function? – xanatos Sep 04 '13 at 10:19
  • I don't know the gory details of the GCC ABI, but you might want to compare with virtual inheritance. There's a chance those fields would be used then. – syam Sep 04 '13 at 10:28
  • Why do you care? I take it it's not causing a problem? – Mats Petersson Sep 04 '13 at 10:31
  • @xanatos, sorry, I put an incorrect version of the source here. I will edit it, there is an g() after f() declaration. – ZijingWu Sep 04 '13 at 10:35

1 Answers1

3

One of the two "spaces" should be for RTTI (the second one), the other for multiple inheritance (the first one). See for example http://tinydrblog.appspot.com/?p=89001, or even better https://stackoverflow.com/a/5712953/613130

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280