《coredump问题原理探究》Linux x86版6.7节多继承

类的多继承大致可以分为两种情况.一种是无共同基类的.一种是有共同基类的.

先看一下第一种情况:

1 #include <stdio.h> 2 class xuzhina_dump_c06_s5_mother 3 { 4private: 5int m_age; 6int m_beauty; 7public: 8virtual void print() 9{ 10printf( "mother\n" ); 11} 12 13virtual void setBeauty( int age, int beauty ) 14{ 15m_age = age – 5; 16m_beauty = beauty; 17} 18 };19 20 class xuzhina_dump_c06_s5_father 21 { 22private: 23int m_strong; 24int m_age; 25public: 26virtual void print() 27{ 28printf( "father\n" ); 29} 30virtual void setStrong( int strong, int age ) 31{ 32m_strong = strong; 33m_age = age; 34} 35 }; 3637 class xuzhina_dump_c06_s5_child: public xuzhina_dump_c06_s5_father, 38public xuzhina_dump_c06_s5_mother 39 40 { 41private: 42bool m_newMind; 43public: 44virtual void print() 45{ 46printf( "child\n" ); 47} 48 49virtual void setGender( bool gender ) 50{ 51m_newMind = true; 52if ( gender ) 53{ 54setBeauty( 10, 10 ); 55} 56else 57{ 58setStrong( 20,20 ); 59} 60} 61 }; 6263 int main() 64 { 65xuzhina_dump_c06_s5_child* child = new xuzhina_dump_c06_s5_child; 66child->setGender( false ); 67child->print(); 68 69xuzhina_dump_c06_s5_father* f = child; 70f->print(); 71 72xuzhina_dump_c06_s5_mother* m = child; 73m->print(); 74 75return 0; 76 }

看一下main函数的汇编:

(gdb) disassemble mainDump of assembler code for function main: 0x080485b0 <+0>:push %ebp 0x080485b1 <+1>:mov %esp,%ebp 0x080485b3 <+3>:push %ebx 0x080485b4 <+4>:and $0xfffffff0,%esp 0x080485b7 <+7>:sub $0x20,%esp 0x080485ba <+10>: movl $0x1c,(%esp) 0x080485c1 <+17>: call 0x8048490 <_Znwj@plt> 0x080485c6 <+22>: mov %eax,%ebx 0x080485c8 <+24>: mov %ebx,(%esp) 0x080485cb <+27>: call 0x8048746 <_ZN25xuzhina_dump_c06_s5_childC2Ev> 0x080485d0 <+32>: mov %ebx,0x1c(%esp) 0x080485d4 <+36>: mov 0x1c(%esp),%eax 0x080485d8 <+40>: mov (%eax),%eax 0x080485da <+42>: add $0x8,%eax 0x080485dd <+45>: mov (%eax),%eax 0x080485df <+47>: movl $0x0,0x4(%esp) 0x080485e7 <+55>: mov 0x1c(%esp),%edx 0x080485eb <+59>: mov %edx,(%esp) 0x080485ee <+62>: call *%eax 0x080485f0 <+64>: mov 0x1c(%esp),%eax 0x080485f4 <+68>: mov (%eax),%eax 0x080485f6 <+70>: mov (%eax),%eax 0x080485f8 <+72>: mov 0x1c(%esp),%edx 0x080485fc <+76>: mov %edx,(%esp) 0x080485ff <+79>: call *%eax 0x08048601 <+81>: mov 0x1c(%esp),%eax 0x08048605 <+85>: mov %eax,0x18(%esp) 0x08048609 <+89>: mov 0x18(%esp),%eax 0x0804860d <+93>: mov (%eax),%eax 0x0804860f <+95>: mov (%eax),%eax 0x08048611 <+97>: mov 0x18(%esp),%edx 0x08048615 <+101>: mov %edx,(%esp) 0x08048618 <+104>: call *%eax 0x0804861a <+106>: cmpl $0x0,0x1c(%esp) 0x0804861f <+111>: je0x804862a <main+122> 0x08048621 <+113>: mov 0x1c(%esp),%eax 0x08048625 <+117>: add $0xc,%eax 0x08048628 <+120>: jmp 0x804862f <main+127> 0x0804862a <+122>: mov $0x0,%eax 0x0804862f <+127>: mov %eax,0x14(%esp) 0x08048633 <+131>: mov 0x14(%esp),%eax 0x08048637 <+135>: mov (%eax),%eax 0x08048639 <+137>: mov (%eax),%eax 0x0804863b <+139>: mov 0x14(%esp),%edx 0x0804863f <+143>: mov %edx,(%esp) 0x08048642 <+146>: call *%eax 0x08048644 <+148>: mov $0x0,%eax 0x08048649 <+153>: mov -0x4(%ebp),%ebx 0x0804864c <+156>: leave0x0804864d <+157>: retEnd of assembler dump.

由上面的汇编,可以看到,对象child的地址存放在esp+0x1c

而下面这几条指令

0x08048601 <+81>: mov 0x1c(%esp),%eax 0x08048605 <+85>: mov %eax,0x18(%esp) 0x08048609 <+89>: mov 0x18(%esp),%eax 0x0804860d <+93>: mov (%eax),%eax 0x0804860f <+95>: mov (%eax),%eax 0x08048611 <+97>: mov 0x18(%esp),%edx 0x08048615 <+101>: mov %edx,(%esp) 0x08048618 <+104>: call *%eax0x08048621 <+113>: mov 0x1c(%esp),%eax 0x08048625 <+117>: add $0xc,%eax 0x0804862f <+127>: mov %eax,0x14(%esp) 0x08048633 <+131>: mov 0x14(%esp),%eax 0x08048637 <+135>: mov (%eax),%eax 0x08048639 <+137>: mov (%eax),%eax 0x0804863b <+139>: mov 0x14(%esp),%edx 0x0804863f <+143>: mov %edx,(%esp) 0x08048642 <+146>: call *%eax

由于是和代码

69xuzhina_dump_c06_s5_father* f = child; 70f->print(); 71 72xuzhina_dump_c06_s5_mother* m = child; 73m->print();

相对应的.

0x08048625 <+117>: add $0xc,%eax

可以看到非常奇怪的现象,当类xuzhina_dump_c06_s5_child的指针转换成类xuzhina_dump_c06_s5_mother的指针时,并不是直接赋值过去,而是比预料的地址加了一个偏移值.

看不见我将要去的地方,记不得我已经去过的地方。

《coredump问题原理探究》Linux x86版6.7节多继承

相关文章:

你感兴趣的文章:

标签云: