Reputation: 10658
I am currently trying to understand how G++ generates assembly from a small example C++ program. So I am testing the following programm with various optimizations:
#define noinline __attribute__ ((noinline))
class Test_Base {
public:
noinline Test_Base() {}
virtual noinline ~Test_Base() {}
void noinline method() { v_method(); }
private:
virtual void v_method()=0;
};
class Test1
: public Test_Base
{
public:
noinline Test1() {}
noinline ~Test1() {}
private:
void noinline v_method() {}
};
class Test2
: public Test_Base
{
public:
noinline Test2() {}
~Test2() {}
private:
void noinline v_method() {}
};
int main() {
volatile int x = 0;
Test_Base * b;
Test1 t1;
Test2 t2;
if( x )
b = &t1;
else
b = &t2;
b->method();
return 0;
}
However looking at this code (compiled using -Os
for the ARMv7 plattform), I found that all definitions of the constructors and destructors have been included multiple times. Here are the relevant parts of the symbol table for Test1:
00008730 w F .text 00000004 Test1::v_method()
000088d8 w O .rodata 00000014 vtable for Test1
000087d0 w F .text 00000020 Test1::Test1()
00008774 w F .text 0000001c Test1::~Test1()
00008710 w F .text 00000020 Test1::~Test1()
000088a4 w O .rodata 00000007 typeinfo name for Test1
00008898 w O .rodata 0000000c typeinfo for Test1
000087d0 w F .text 00000020 Test1::Test1()
00008710 w F .text 00000020 Test1::~Test1()
So I have one constructor and two destructors (the last two calls are just duplicates tat the same positions as before). Looking at the assembly I observe the following:
First the constructor
000087d0 <Test1::Test1()>:
87d0: e92d4010 push {r4, lr}
87d4: e1a04000 mov r4, r0
87d8: ebfffff3 bl 87ac <Test_Base::Test_Base()>
87dc: e1a00004 mov r0, r4
87e0: e59f3004 ldr r3, [pc, #4] ; 87ec <Test1::Test1()+0x1c>
87e4: e5843000 str r3, [r4]
87e8: e8bd8010 pop {r4, pc}
87ec: 000088e0 .word 0x000088e0
I guess this does what I told it to do.
Now the destructor at 0x8710:
00008710 <Test1::~Test1()>:
8710: e59f3014 ldr r3, [pc, #20] ; 872c <Test1::~Test1()+0x1c>
8714: e92d4010 push {r4, lr}
8718: e1a04000 mov r4, r0
871c: e5803000 str r3, [r0]
8720: ebfffff6 bl 8700 <Test_Base::~Test_Base()>
8724: e1a00004 mov r0, r4
8728: e8bd8010 pop {r4, pc}
872c: 000088e0 .word 0x000088e0
Again there is nothing suspicious here.
Now the destructor at 0x8774:
00008774 <Test1::~Test1()>:
8774: e92d4010 push {r4, lr}
8778: e1a04000 mov r4, r0
877c: ebffffe3 bl 8710 <Test1::~Test1()>
8780: e1a00004 mov r0, r4
8784: ebffff69 bl 8530 <_init+0x44>
8788: e1a00004 mov r0, r4
878c: e8bd8010 pop {r4, pc}
I cannot realy tell what this does, as I am not really familiar with the ABI. I am guessing it has something to do with static initialization.
What is the purpose of this additional destructor?
If I compile the same for x86_64 I also get duplicated destructors, so this does not seem system specific.
Upvotes: 2
Views: 172
Reputation: 254501
The first is a non-virtual destructor, used for destroying automatic or static objects when the dynamic type is known at compile time.
The second is a virtual "thunk" used for polymorphic deletion. It calls the non-virtual destructor to destoy the object, then calls operator delete
to free the memory.
Upvotes: 2