-1

I'm writing a C++ state machine for Cortex-M4.
I use ARM GCC 11.2.1 (none).
I'm making a comparison between C and C++ output assembly.

I have the following C++ code godbolt link

struct State
{
    virtual void entry(void) = 0;
    virtual void exit(void) = 0;
    virtual void run(void) = 0;
};

struct S1 : public State
{
    void entry(void)
    {
    }

    void exit(void)
    {
    }

    void run(void)
    {
    }
};

S1 s1;

The assembly output is:

S1::entry():
  bx lr
S1::exit():
  bx lr
S1::run():
  bx lr
vtable for S1:
  .word 0
  .word 0
  .word S1::entry()
  .word S1::exit()
  .word S1::run()
s1:
  .word vtable for S1+8

The only difference from C version of this code is the 2 lines .word 0:

vtable for S1:
      .word 0
      .word 0

What does that mean and what does it do?

Here's the C version of the code above I wrote. godbolt link

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    Why are you trying to compare a C struct with function pointers to a C++ object with virtual members? – Botje Aug 12 '22 at 08:14
  • 1
    These are two additional pointers. Why are you interested in such internals? – the busybee Aug 12 '22 at 08:18
  • @Botje because I like learning the differences between programming tools and languages. – blinkbetter Aug 12 '22 at 08:18
  • @thebusybee Because I wanna broaden my knowledge. – blinkbetter Aug 12 '22 at 08:19
  • 1
    @blinkbetter you might find this interesting, if you are learning, https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/ – Fra93 Aug 12 '22 at 08:29
  • `.word 0` emits 4 bytes of zeros into the current section. Check the assembler manual to answer the title part of your question. https://sourceware.org/binutils/docs/as/Word.html . As for vtables, near duplicate of [How do objects work in x86 at the assembly level?](https://stackoverflow.com/q/33556511) which shows code that uses that data (for another ISA, but same design of pointers to tables of function pointers.) – Peter Cordes Aug 12 '22 at 08:36
  • A difference from C structs is that C++ also supports `typeid`, `dynamic_cast`, and virtual inheritance. A couple of extra pointers can be handy when implementing that. – BoP Aug 12 '22 at 08:46
  • @BoP I have disabled RTTI – blinkbetter Aug 12 '22 at 08:58

2 Answers2

2

The C++ ABI for ARM and the GNU C++ ABI define which entries must appear in the virtual table.

In the case of your code, the first two entries are the offset to the top of the vtable and the typeinfo pointer. These are zero for now, but may be overwritten if required (eg: if a further derived class is made).

Tom V
  • 4,827
  • 2
  • 5
  • 22
0

It means the assembler should output the word number 0.

The assembler basically goes through the file from top to bottom, and for each instruction, it calculates the bytes for that instruction (always 4 bytes on ARM), and writes it into the output file.

.word just tells it to output a particular number as 4 bytes. In this case 0.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    :-D While this is technically correct, and I was temped to write the same, it does not answer what these zeroed fields are for. Most probably this is what the OP wants to know. – the busybee Aug 12 '22 at 09:20
  • 1
    Most instructions on this platform (ARMv7M) are 2 bytes, only a few are 4. – Tom V Aug 13 '22 at 07:11