程序本身用VC编译:
// virtualandoverloadding.cpp : Defines the entry point for the console application.
//
#include \"stdafx.h\"
class parent
{
public:
int int1()
{
return 1;
}
virtual int int1(int x)
{
return x;
}
int int1(char c)
{
return c;
}
};
class child:public parent
{
public:
int int1(int x)
{
return x*2;
}
};
int main(int argc, char* argv[])
{
parent *p=new child();
p->int1();
p->int1(2);
p->int1(’c’);
return 0;
}
分析:
进入MAIN后
下面是动态的堆栈跟踪分析
PUSH EBP
EBP
MOV EBP,ESP
EBP,ESP->EBP
PUSH 0FFH
ESP ->0FFH
0FFH
0FFH
0FFH
EBP ->EBP
PUSH OFFSET _ehandler$_main(004109bb);SEH的构造,从堆栈图中特别清楚的.
ESP ->_ehandler$_main
0FFH
0FFH
0FFH
0FFH
EBP ->EBP
mov eax,fs:[00000000]
push eax
ESP ->fs:[00000000]
_ehandler$_main
0FFH
0FFH
0FFH
0FFH
EBP ->EBP
mov dword ptr fs:[0],esp
sub esp,50h
esp ->...
...
...
...
50H bytes
...
fs:[00000000];这个结构已经呈现在我们面前
_ehandler$_main
0FFH
0FFH
0FFH
0FFH
EBP ->EBP
push ebx
push esi
push edi
esp ->edi
esi
ebx
...
...
...
...
50H bytes
...
fs:[00000000]
_ehandler$_main
0FFH
0FFH
0FFH
0FFH
EBP ->EBP
lea edi,[ebp-5Ch]
mov ecx,14h
mov eax,0CCCCCCCCh
rep stos dword ptr [edi]
;注意这里的算法,开始分配的空间一共50H个字节共有80BYTE,所以这里用14H就是;20次循环 [Page]
push 4
esp ->
04h
00h
00h
00h
edi
esi
ebx
...
...
...
...
50H bytes
...
fs:[00000000]
_ehandler$_main
0FFH
0FFH
0FFH
0FFH
EBP ->EBP
call operator new(00401360)
add esp 4;调用者恢复堆栈
进入operator new
operator new:
push ebp
mov ebp,esp
push 1
mov eax,dword ptr [cb]
push eax
call _nh_malloc (00401a70);malloc的参数是上面PUSH 4来的
add esp,8;临时空间的回收还是函数本身来负责的
pop ebp
ret
回到MAIN
mov dword ptr [ebp-18h],eax
mov dword ptr [ebp-4],0
cmp dword ptr [ebp-18h],0
je main+54h (004010a4);这里用到了一个刚才忽视的单元EBP-4不知道为啥了,兄弟们知道的上啊...
mov ecx,dword ptr [ebp-18h]
call @ILT+15(child::child) (00401014)
终于进入了构造函数
child::child:
push ebp
mov ebp,esp
sub esp,44h
push ebx
push esi
push edi
push ecx;其实这里使用ecx来传递参数有些fastcall的意思了
lea edi,[ebp-44h]
mov ecx,11h
mov eax,0CCCCCCCCh
rep stos dword ptr [edi];故技重施?唉...
pop ecx
mov dword ptr [ebp-4],ecx
mov ecx,dword ptr [ebp-4];这里编译器犯傻了,呵呵,倒腾了一次ECX
call @ILT+5(parent::parent) (0040100a);调用了父类构造器
;不怕累的继续跟我来
parent::parent:
push ebp
mov ebp,esp
sub esp,44h
push ebx
push esi
push edi
push ecx
lea edi,[ebp-44h]
mov ecx,11h