(转)函数调用栈 剖析+图解

来源:互联网 发布:电脑内部录音软件 编辑:程序博客网 时间:2024/05/20 08:25





原文链接:http://blog.csdn.net/wangyezi19930928/article/details/16921927


                <li><a href="http://so.csdn.net/so/search/s.do?q=汇编&amp;t=blog" target="_blank">汇编</a> <span>/</span></li>                <li><a href="http://so.csdn.net/so/search/s.do?q=栈&amp;t=blog" target="_blank">栈</a> <span>/</span></li>                <li><a href="http://so.csdn.net/so/search/s.do?q=布局&amp;t=blog" target="_blank">布局</a> <span style="display: none;">/</span></li>            </ul>            <ul class="right_bar">                <li><button class="btn-noborder"><i class="icon iconfont icon-read"></i><span class="txt">15517</span></button></li>                <!--<li><button class="btn-noborder"><i class="icon iconfont icon-dashang-"></i><span class="txt">0</span></button></li>-->                <li class="edit" style="display: none;">                    <a class="btn-noborder" href="http://write.blog.csdn.net/postedit/16921927">                        <i class="icon iconfont icon-bianji"></i><span class="txt">编辑</span>                    </a>                </li>                <li class="del" style="display: none;">                    <a class="btn-noborder" onclick="javascript:deleteArticle(fileName);return false;">                        <i class="icon iconfont icon-shanchu"></i><span class="txt">删除</span>                    </a>                </li>            </ul>        </div>        <div id="article_content" class="article_content csdn-tracking-statistics" data-mod="popu_519" data-dsm="post" style="overflow: hidden;">

栈: 在函数调用时,第一个进栈的是主函数中函数调用后的下一条指令(函数调用语句的下一条可执行语句)的地址,然后是函数的各个参数,在大多数的C编译器中,参数是由右往左入栈的,然后是函数中的局部变量。注意静态变量是不入栈的。

当本次函数调用结束后,局部变量先出栈,然后是参数,最后栈顶指针指向最开始存的地址,也就是主函数中的下一条指令,程序由该点继续运行。函数调用栈 - kom118 - Just do IT!!

当发生函数调用的时候,栈空间中存放的数据是这样的:
1、调用者函数把被调函数所需要的参数按照与被调函数的形参顺序相反的顺序压入栈中,即:从右向左依次把被调函数所需要的参数压入栈;
2、调用者函数使用call指令调用被调函数,并把call指令的下一条指令的地址当成返回地址压入栈中(这个压栈操作隐含在call指令中);
3、在被调函数中,被调函数会先保存调用者函数的栈底地址(push ebp),然后再保存调用者函数的栈顶地址,即:当前被调函数的栈底地址(mov ebp,esp);
4、在被调函数中,从ebp的位置处开始存放被调函数中的局部变量和临时变量,并且这些变量的地址按照定义时的顺序依次减小,即:这些变量的地址是按照栈的延伸方向排列的,先定义的变量先入栈,后定义的变量后入栈;
所以,发生函数调用时,入栈的顺序为:
参数N
参数N-1
参数N-2
…..
参数3
参数2
参数1
函数返回地址
上一层调用函数的EBP/BP
局部变量1
局部变量2
….
局部变量N
函数调用栈如下图所示:


解释:  //EBP 基址指针,是保存调用者函数的地址,总是指向函数栈栈底,ESP被调函数的指针,总是指向函数栈栈顶。
首 先,将调用者函数的EBP入栈(pushebp),然后将调用者函数的栈顶指针ESP赋值给被调函数的EBP(作为被调函数的栈底,movebp,esp),此时,EBP寄存器处于一个非常重要的位置,该寄存器中存放着一个地址(原EBP入栈后的栈顶),以该地址为基准,向上(栈底方向)能获取返回地址、参数值,向下(栈顶方向)能获取函数的局部变量值,而该地址处又存放着上一层函数调用时的EBP值;


一般规律,SS:[ebp+4]处为被调函数的返回地址,SS:[EBP+8]处为传递给被调函数的第一个参数(最后一个入栈的参数,此处假设其占用4字节内存)的值,SS:[EBP-4]处为被调函数中的第一个局部变量,SS:[EBP]处为上一层EBP值;由于EBP中的地址处总是”上一层函数调用时的EBP值”,而在每一层函数调用中,都能通过当时的EBP值”向上(栈底方向)能获取返回地址、参数值,向下(栈顶方向)能获取被调函数的局部变量值”;

如此递归,就形成了函数调用栈;


Eg函数内局部变量布局示例:

[plain] view plain copy
print?
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. struct C  
  4. {  
  5.   int a;  
  6.   int b;  
  7.   int c;  
  8. };  
  9. int test2(int x, int y, int z)  
  10. {  
  11.   printf(“hello,test2\n”);  
  12.   return 0;  
  13. }  
  14. int test(int x, int y, int z)  
  15. {  
  16.   int a = 1;  
  17.   int b = 2;  
  18.   int c = 3;  
  19.   struct C st;  
  20.   printf(“addr x = %u\n”,(unsigned int)(&x));  
  21.   printf(“addr y = %u\n”,(unsigned int)(&y));  
  22.   printf(“addr z = %u\n”,(unsigned int)(&z));  
  23.   printf(“addr a = %u\n”,(unsigned int)(&a));  
  24.   printf(“addr b = %u\n”,(unsigned int)(&b));  
  25.   printf(“addr c = %u\n”,(unsigned int)(&c));  
  26.   printf(“addr st = %u\n”,(unsigned int)(&st));  
  27.   printf(“addr st.a = %u\n”,(unsigned int)(&st.a));  
  28.   printf(“addr st.b = %u\n”,(unsigned int)(&st.b));  
  29.   printf(“addr st.c = %u\n”,(unsigned int)(&st.c));  
  30.   return 0;  
  31. } int main(int argc, char** argv)  
  32. {  
  33.   int x = 1;  
  34.   int y = 2;  
  35.   int z = 3;  
  36.   test(x,y,z);  
  37.   printf(“x = %d; y = %d; z = %d;\n”, x,y,z);  
  38.   memset(&y, 0, 8);  
  39.   printf(“x = %d; y = %d; z = %d;\n”, x,y,z);  
  40.   return 0;  
  41. }  
#include <stdio.h>

#include <string.h>struct C{ int a; int b; int c;};int test2(int x, int y, int z){ printf("hello,test2\n"); return 0;}int test(int x, int y, int z){ int a = 1; int b = 2; int c = 3; struct C st; printf("addr x = %u\n",(unsigned int)(&x)); printf("addr y = %u\n",(unsigned int)(&y)); printf("addr z = %u\n",(unsigned int)(&z)); printf("addr a = %u\n",(unsigned int)(&a)); printf("addr b = %u\n",(unsigned int)(&b)); printf("addr c = %u\n",(unsigned int)(&c)); printf("addr st = %u\n",(unsigned int)(&st)); printf("addr st.a = %u\n",(unsigned int)(&st.a)); printf("addr st.b = %u\n",(unsigned int)(&st.b)); printf("addr st.c = %u\n",(unsigned int)(&st.c)); return 0;} int main(int argc, char** argv){ int x = 1; int y = 2; int z = 3; test(x,y,z); printf("x = %d; y = %d; z = %d;\n", x,y,z); memset(&y, 0, 8); printf("x = %d; y = %d; z = %d;\n", x,y,z); return 0;}
打印输出如下:

[plain] view plain copy
print?
  1. addr x = 3220024704  
  2. addr y = 3220024708  
  3. addr z = 3220024712  
  4. addr a = 3220024684  
  5. addr b = 3220024680  
  6. addr c = 3220024676  
  7. addr st = 3220024664  
  8. addr st.a = 3220024664  
  9. addr st.b = 3220024668  
  10. addr st.c = 3220024672  
  11. x = 1; y = 2; z = 3;  
  12. x = 0; y = 0; z = 3;  
addr x = 3220024704addr y = 3220024708addr z = 3220024712addr a = 3220024684addr b = 3220024680addr c = 3220024676addr st = 3220024664addr st.a = 3220024664addr st.b = 3220024668addr st.c = 3220024672x = 1; y = 2; z = 3;x = 0; y = 0; z = 3;
局部变量在栈中布局示意图:



该图中的局部变量都是在该示例中定义的:


 这个图片中反映的是一个典型的函数调用栈的内存布局;
访问函数的局部变量和访问函数参数的区别:
局部变量总是通过将ebp减去偏移量来访问,函数参数总是通过将ebp加上偏移量来访问。对于32位变量而言,第一个局部变量位于ebp-4,第二个位于ebp-8,以此类推,32位局部变量在栈中形成一个逆序数组;第一个函数参数位于ebp+8,第二个位于ebp+12,以此类推,32位函数参数在栈中形成一个正序数组。




Eg、研究函数调用过程:

[plain] view plain copy
print?
  1. #include <stdio.h>  
  2.   
  3. int bar(int c,int d)  
  4. {  
  5.         int e=c+d;  
  6.         return e;  
  7. }  
  8.   
  9. int foo(int a,int b)  
  10. {  
  11.         return bar(a,b);  
  12. }  
  13.   
  14. int main(int argc,int argv)  
  15. {  
  16.         foo(2,3);  
  17.         return 0;  
  18. }  
#include <stdio.h>int bar(int c,int d){        int e=c+d;        return e;}int foo(int a,int b){        return bar(a,b);}int main(int argc,int argv){        foo(2,3);        return 0;}

上面是一个很简单的函数调用过程,整个程序的执行过程是main调用foofoo调用bar


//查看反汇编文件(要查看编译后的汇编代码,其实还有一种办法是gcc -S text_stack.c,这样只生成汇编代码text_stack.s,而不生成二进制的目标文件。)

[plain] view plain copy
print?
  1. root@wangye:/home/wangye# gcc text_stack.c -g  
  2. root@wangye:/home/wangye# objdump -dS a.out   
root@wangye:/home/wangye# gcc text_stack.c -groot@wangye:/home/wangye# objdump -dS a.out 

反汇编结果很长,下面只列出我们关心的部分。

[plain] view plain copy
print?
  1. 08048394 <bar>:  
  2. #include <stdio.h>  
  3.   
  4. int bar(int c,int d)  
  5. {  
  6.  8048394:   55                      push   %ebp  
  7.  8048395:   89 e5                   mov    %esp,%ebp  
  8.  8048397:   83 ec 10                sub    0x10,0x8,%esp&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;bar(a,b);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483b1:&nbsp;&nbsp;&nbsp;8b&nbsp;45&nbsp;0c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;0xc(%ebp),%eax&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483b4:&nbsp;&nbsp;&nbsp;89&nbsp;44&nbsp;24&nbsp;04&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;%eax,0x4(%esp)&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483b8:&nbsp;&nbsp;&nbsp;8b&nbsp;45&nbsp;08&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;0x8(%ebp),%eax&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483bb:&nbsp;&nbsp;&nbsp;89&nbsp;04&nbsp;24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;%eax,(%esp)&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483be:&nbsp;&nbsp;&nbsp;e8&nbsp;d1&nbsp;ff&nbsp;ff&nbsp;ff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call&nbsp;&nbsp;&nbsp;8048394&nbsp;&lt;bar&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483c3:&nbsp;&nbsp;&nbsp;c9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leave&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483c4:&nbsp;&nbsp;&nbsp;c3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>080483c5&nbsp;&lt;main&gt;:&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>int&nbsp;main(int&nbsp;argc,int&nbsp;argv)&nbsp;&nbsp;</span></li><li class=""><span>{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483c5:&nbsp;&nbsp;&nbsp;55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;%ebp&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483c6:&nbsp;&nbsp;&nbsp;89&nbsp;e5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;%esp,%ebp&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483c8:&nbsp;&nbsp;&nbsp;83&nbsp;ec&nbsp;08&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub&nbsp;&nbsp;&nbsp;&nbsp;0x8,0x3,0x4(%esp)&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;80483d2:&nbsp;&nbsp;&nbsp;00&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;80483d3:&nbsp;&nbsp;&nbsp;c7&nbsp;04&nbsp;24&nbsp;02&nbsp;00&nbsp;00&nbsp;00&nbsp;&nbsp;&nbsp;&nbsp;movl&nbsp;&nbsp;&nbsp;0x2,(0x0,%eax  
  9. }  
08048394 <bar>:
#include <stdio.h>int bar(int c,int d){ 8048394: 55 push %ebp 8048395: 89 e5 mov %esp,%ebp 8048397: 83 ec 10 sub 0x10,%esp
    int e=c+d;
 804839a:   8b 45 0c                mov    0xc(%ebp),%eax
 804839d:   8b 55 08                mov    0x8(%ebp),%edx
 80483a0:   8d 04 02                lea    (%edx,%eax,1),%eax
 80483a3:   89 45 fc                mov    %eax,-0x4(%ebp)
    return e;
 80483a6:   8b 45 fc                mov    -0x4(%ebp),%eax
}
 80483a9:   c9                      leave  
 80483aa:   c3                      ret    

080483ab <foo>:

int foo(int a,int b)
{
 80483ab:   55                      push   %ebp
 80483ac:   89 e5                   mov    %esp,%ebp
 80483ae:   83 ec 08                sub
0x8,%esp return bar(a,b); 80483b1: 8b 45 0c mov 0xc(%ebp),%eax 80483b4: 89 44 24 04 mov %eax,0x4(%esp) 80483b8: 8b 45 08 mov 0x8(%ebp),%eax 80483bb: 89 04 24 mov %eax,(%esp) 80483be: e8 d1 ff ff ff call 8048394 <bar>} 80483c3: c9 leave 80483c4: c3 ret 080483c5 <main>:int main(int argc,int argv){ 80483c5: 55 push %ebp 80483c6: 89 e5 mov %esp,%ebp 80483c8: 83 ec 08 sub 0x8,foo(2,3);80483cb:c7442404030000movl0x3,0x4(%esp) 80483d2: 00 80483d3: c7 04 24 02 00 00 00 movl 0x2,(80483da:e8ccffffffcall80483ab<foo>return0;80483df:b800000000mov0x0,%eax}

//我们用gdb跟踪程序的执行,直到bar函数中的int e = c + d;语句执行完毕准备返回时,这时在gdb中打印函数栈帧。

[plain] view plain copy
print?
  1. wangye@wangye:~&nbsp;gdb&nbsp;text_stack&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>GNU&nbsp;gdb&nbsp;(GDB)&nbsp;7.0.1-debian&nbsp;&nbsp;</span></li><li class="alt"><span>Copyright&nbsp;(C)&nbsp;2009&nbsp;Free&nbsp;Software&nbsp;Foundation,&nbsp;Inc.&nbsp;&nbsp;</span></li><li class=""><span>License&nbsp;GPLv3+:&nbsp;GNU&nbsp;GPL&nbsp;version&nbsp;3&nbsp;or&nbsp;later&nbsp;&lt;http://gnu.org/licenses/gpl.html&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>This&nbsp;is&nbsp;free&nbsp;software:&nbsp;you&nbsp;are&nbsp;free&nbsp;to&nbsp;change&nbsp;and&nbsp;redistribute&nbsp;it.&nbsp;&nbsp;</span></li><li class=""><span>There&nbsp;is&nbsp;NO&nbsp;WARRANTY,&nbsp;to&nbsp;the&nbsp;extent&nbsp;permitted&nbsp;by&nbsp;law.&nbsp;&nbsp;Type&nbsp;"show&nbsp;copying"&nbsp;&nbsp;</span></li><li class="alt"><span>and&nbsp;"show&nbsp;warranty"&nbsp;for&nbsp;details.&nbsp;&nbsp;</span></li><li class=""><span>This&nbsp;GDB&nbsp;was&nbsp;configured&nbsp;as&nbsp;"i486-linux-gnu".&nbsp;&nbsp;</span></li><li class="alt"><span>For&nbsp;bug&nbsp;reporting&nbsp;instructions,&nbsp;please&nbsp;see:&nbsp;&nbsp;</span></li><li class=""><span>&lt;http://www.gnu.org/software/gdb/bugs/&gt;...&nbsp;&nbsp;</span></li><li class="alt"><span>Reading&nbsp;symbols&nbsp;from&nbsp;/home/wangye/text_stack...done.&nbsp;&nbsp;</span></li><li class=""><span>(gdb)&nbsp;start&nbsp;&nbsp;</span></li><li class="alt"><span>Temporary&nbsp;breakpoint&nbsp;1&nbsp;at&nbsp;0x80483cb:&nbsp;file&nbsp;text_stack.c,&nbsp;line&nbsp;16.&nbsp;&nbsp;</span></li><li class=""><span>Starting&nbsp;program:&nbsp;/home/wangye/text_stack&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>Temporary&nbsp;breakpoint&nbsp;1,&nbsp;main&nbsp;(argc=1,&nbsp;argv=-1073744732)&nbsp;at&nbsp;text_stack.c:16&nbsp;&nbsp;</span></li><li class="alt"><span>16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo(2,3);&nbsp;&nbsp;</span></li><li class=""><span>(gdb)&nbsp;s&nbsp;&nbsp;</span></li><li class="alt"><span>foo&nbsp;(a=2,&nbsp;b=3)&nbsp;at&nbsp;text_stack.c:11&nbsp;&nbsp;</span></li><li class=""><span>11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;bar(a,b);&nbsp;&nbsp;</span></li><li class="alt"><span>(gdb)&nbsp;s&nbsp;&nbsp;</span></li><li class=""><span>bar&nbsp;(c=2,&nbsp;d=3)&nbsp;at&nbsp;text_stack.c:5&nbsp;&nbsp;</span></li><li class="alt"><span>5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;e=c+d;&nbsp;&nbsp;</span></li><li class=""><span>(gdb)&nbsp;disassemble&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>Dump&nbsp;of&nbsp;assembler&nbsp;code&nbsp;for&nbsp;function&nbsp;bar:&nbsp;&nbsp;</span></li><li class=""><span>0x08048394&nbsp;&lt;bar+0&gt;:&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;%ebp&nbsp;&nbsp;</span></li><li class="alt"><span>0x08048395&nbsp;&lt;bar+1&gt;:&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;%esp,%ebp&nbsp;&nbsp;</span></li><li class=""><span>0x08048397&nbsp;&lt;bar+3&gt;:&nbsp;&nbsp;&nbsp;sub&nbsp;&nbsp;&nbsp;&nbsp;0x10,%esp  
  2. 0x0804839a <bar+6>:   mov    0xc(%ebp),%eax  
  3. 0x0804839d <bar+9>:   mov    0x8(%ebp),%edx  
  4. 0x080483a0 <bar+12>:  lea    (%edx,%eax,1),%eax  
  5. 0x080483a3 <bar+15>:  mov    %eax,-0x4(%ebp)  
  6. 0x080483a6 <bar+18>:  mov    -0x4(%ebp),%eax  
  7. 0x080483a9 <bar+21>:  leave    
  8. 0x080483aa <bar+22>:  ret      
  9. End of assembler dump.  
  10. (gdb) si  
  11. 0x0804839d  5       int e=c+d;  
  12. (gdb) si  
  13. 0x080483a0  5       int e=c+d;  
  14. (gdb) si  
  15. 0x080483a3  5       int e=c+d;  
  16. (gdb) si  
  17. 6       return e;  
  18. (gdb) si  
  19. 7   }  
  20. (gdb) bt  
  21. #0  bar (c=2, d=3) at text_stack.c:7  
  22. #1  0x080483c3 in foo (a=2, b=3) at text_stack.c:11  
  23. #2  0x080483df in main (argc=1, argv=-1073744732) at text_stack.c:16  
  24. (gdb) info re  
  25. record     registers    
  26. (gdb) info regi  
  27. eax            0x5  5  
  28. ecx            0x4c2f5d43   1278172483  
  29. edx            0x2  2  
  30. ebx            0xb7fcaff4   -1208176652  
  31. esp            0xbffff3c8   0xbffff3c8  
  32. ebp            0xbffff3d8   0xbffff3d8  
  33. esi            0x0  0  
  34. edi            0x0  0  
  35. eip            0x80483a9    0x80483a9 <bar+21>  
  36. eflags         0x282    [ SF IF ]  
  37. cs             0x73 115  
  38. ss             0x7b 123  
  39. ds             0x7b 123  
  40. es             0x7b 123  
  41. fs             0x0  0  
  42. gs             0x33 51  
  43. (gdb) info regi  
  44. eax            0x5  5  
  45. ecx            0x4c2f5d43   1278172483  
  46. edx            0x2  2  
  47. ebx            0xb7fcaff4   -1208176652  
  48. esp            0xbffff3c8   0xbffff3c8  
  49. ebp            0xbffff3d8   0xbffff3d8  
  50. esi            0x0  0  
  51. edi            0x0  0  
  52. eip            0x80483a9    0x80483a9 <bar+21>  
  53. eflags         0x282    [ SF IF ]  
  54. cs             0x73 115  
  55. ss             0x7b 123  
  56. ds             0x7b 123  
  57. es             0x7b 123  
  58. fs             0x0  0  
  59. gs             0x33 51  
  60. (gdb) x/20 esp&nbsp;&nbsp;</span></li><li class=""><span>0xbffff3c8:&nbsp;-1073744904&nbsp;134513689&nbsp;&nbsp;&nbsp;-1208175868&nbsp;5&nbsp;&nbsp;</span></li><li class="alt"><span>0xbffff3d8:&nbsp;-1073744920&nbsp;134513603&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;</span></li><li class=""><span>0xbffff3e8:&nbsp;-1073744904&nbsp;134513631&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;</span></li><li class="alt"><span>0xbffff3f8:&nbsp;-1073744776&nbsp;-1209406298&nbsp;1&nbsp;&nbsp;&nbsp;-1073744732&nbsp;&nbsp;</span></li><li class=""><span>0xbffff408:&nbsp;-1073744724&nbsp;-1208084392&nbsp;-1073744800&nbsp;-1&nbsp;&nbsp;</span></li></ol></div><pre code_snippet_id="81037" snippet_file_name="blog_20131125_6_4669862" name="code" class="plain" style="display: none;">wangye@wangye:~ gdb text_stack GNU gdb (GDB) 7.0.1-debianCopyright (C) 2009 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. Type “show copying”and “show warranty” for details.This GDB was configured as “i486-linux-gnu”.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>…Reading symbols from /home/wangye/text_stack…done.(gdb) startTemporary breakpoint 1 at 0x80483cb: file text_stack.c, line 16.Starting program: /home/wangye/text_stack Temporary breakpoint 1, main (argc=1, argv=-1073744732) at text_stack.c:1616 foo(2,3);(gdb) sfoo (a=2, b=3) at text_stack.c:1111 return bar(a,b);(gdb) sbar (c=2, d=3) at text_stack.c:55 int e=c+d;(gdb) disassemble Dump of assembler code for function bar:0x08048394 <bar+0>: push %ebp0x08048395 <bar+1>: mov %esp,%ebp0x08048397 <bar+3>: sub $0x10,%esp0x0804839a <bar+6>: mov 0xc(%ebp),%eax0x0804839d <bar+9>: mov 0x8(%ebp),%edx0x080483a0 <bar+12>: lea (%edx,%eax,1),%eax0x080483a3 <bar+15>: mov %eax,-0x4(%ebp)0x080483a6 <bar+18>: mov -0x4(%ebp),%eax0x080483a9 <bar+21>: leave 0x080483aa <bar+22>: ret End of assembler dump.(gdb) si0x0804839d 5 int e=c+d;(gdb) si0x080483a0 5 int e=c+d;(gdb) si0x080483a3 5 int e=c+d;(gdb) si6 return e;(gdb) si7 }(gdb) bt
#0 bar (c=2, d=3) at text_stack.c:7#1 0x080483c3 in foo (a=2, b=3) at text_stack.c:11#2 0x080483df in main (argc=1, argv=-1073744732) at text_stack.c:16(gdb) info rerecord registers (gdb) info regieax 0x5 5ecx 0x4c2f5d43 1278172483edx 0x2 2ebx 0xb7fcaff4 -1208176652esp 0xbffff3c8 0xbffff3c8ebp 0xbffff3d8 0xbffff3d8esi 0x0 0edi 0x0 0eip 0x80483a9 0x80483a9 <bar+21>eflags 0x282 [ SF IF ]cs 0x73 115ss 0x7b 123ds 0x7b 123es 0x7b 123fs 0x0 0gs 0x33 51(gdb) info regieax 0x5 5ecx 0x4c2f5d43 1278172483edx 0x2 2ebx 0xb7fcaff4 -1208176652esp 0xbffff3c8 0xbffff3c8ebp 0xbffff3d8 0xbffff3d8esi 0x0 0edi 0x0 0eip 0x80483a9 0x80483a9 <bar+21>eflags 0x282 [ SF IF ]cs 0x73 115ss 0x7b 123ds 0x7b 123es 0x7b 123fs 0x0 0gs 0x33 51(gdb) x/20 $esp0xbffff3c8: -1073744904 134513689 -1208175868 50xbffff3d8: -1073744920 134513603 2 30xbffff3e8: -1073744904 134513631 2 30xbffff3f8: -1073744776 -1209406298 1 -10737447320xbffff408: -1073744724 -1208084392 -1073744800 -1
这里我们又用了几个新的gdb命令,简单解释一下:info registers可以显示所有寄存器的当前值。在gdb中表示寄存器名时前面要加个$,例如p $esp</span></code>可以打印<code><span style="font-family:NSimsun">esp</span></code>寄存器的值,在上例中<code><span style="font-family:NSimsun">esp</span></code>寄存器的值是0xbffff3c8,所以<code><span style="font-family:NSimsun">x/20 $esp命令查看内存中从0xbffff3c8 地址开始的20个32位数。在执行程序时,操作系统为进程分配一块栈空间来保存函数栈帧,esp寄存器总是指向栈顶,在x86平台上这个栈是从高地址向低地址增长的,我们知道每次调用一个函数都要分配一个栈帧来保存参数和局部变量,现在我们详细分析这些数据在栈空间的布局,根据gdb的输出结果图示如下:



图中每个小方格表示4个字节的内存单元,例如b: 3这个小方格占的内存地址是0xbffff3f4~0xbffff3f7,把地址写在每个小方格的下边界线上,是为了强调该地址是内存单元的起始地址。我们从main函数的这里开始看起:

[plain] view plain copy
print?
  1. foo(2,3);  
  2. 80483cb:    c7 44 24 04 03 00 00    movl   0x3,0x4(0x2,(%esp)  
  3. 80483da:    e8 cc ff ff ff          call   80483ab <foo>  
  4. return 0;  
  5. 80483df:    b8 00 00 00 00          mov    $0x0,%eax  
  foo(2,3); 80483cb:   c7 44 24 04 03 00 00    movl   $0x3,0x4(%esp) 80483d2:   00  80483d3:   c7 04 24 02 00 00 00    movl   $0x2,(%esp) 80483da:   e8 cc ff ff ff          call   80483ab <foo>    return 0; 80483df:   b8 00 00 00 00          mov    $0x0,%eax}

要调用函数foo先要把参数准备好,第二个参数保存在esp+4指向的内存位置,第一个参数保存在esp指向的内存位置,可见参数是从右向左依次压栈的。然后执行call指令,这个指令有两个作用:

  1. foo函数调用完之后要返回到call的下一条指令继续执行,所以把call的下一条指令的地址134513631压栈,同时把esp的值减4,esp的值现在是0xbffff3ec。

  2. 修改程序计数器eip,跳转到foo函数的开头执行。

现在看foo函数的汇编代码:

[plain] view plain copy
print?
  1. 080483ab <foo>:  
  2.   
  3. int foo(int a,int b)  
  4. {  
  5.  80483ab:   55                      push   %ebp  
  6.  80483ac:   89 e5                   mov    %esp,%ebp  
  7.  80483ae:   83 ec 08                sub    $0x8,%esp  
  8.       
080483ab <foo>:int foo(int a,int b){ 80483ab:   55                      push   %ebp 80483ac:   89 e5                   mov    %esp,%ebp 80483ae:   83 ec 08                sub    $0x8,%esp    

push %ebp指令把ebp寄存器的值压栈,同时把esp的值减4。esp的值现在是0xbff1c414,下一条指令把这个值传送给ebp寄存器。这两条指令合起来是把原来ebp的值保存在栈上,然后又给ebp赋了新值。在每个函数的栈帧中,ebp指向栈底,而esp指向栈顶,在函数执行过程中esp随着压栈和出栈操作随时变化,而ebp是不动的,函数的参数和局部变量都是通过ebp的值加上一个偏移量来访问,例如foo函数的参数ab分别通过ebp+8ebp+12来访问。所以下面的指令把参数ab再次压栈,为调用bar函数做准备,然后把返回地址压栈,调用bar函数:


[plain] view plain copy
print?
  1. return bar(a,b);  
  2.  80483b1:   8b 45 0c                mov    0xc(%ebp),%eax  
  3.  80483b4:   89 44 24 04             mov    %eax,0x4(%esp)  
  4.  80483b8:   8b 45 08                mov    0x8(%ebp),%eax  
  5.  80483bb:   89 04 24                mov    %eax,(%esp)  
  6.  80483be:   e8 d1 ff ff ff          call   8048394 <bar>  
  7. }  
  8.  80483c3:   c9                      leave    
  9.  80483c4:   c3                      ret    
return bar(a,b); 80483b1:   8b 45 0c                mov    0xc(%ebp),%eax 80483b4:   89 44 24 04             mov    %eax,0x4(%esp) 80483b8:   8b 45 08                mov    0x8(%ebp),%eax 80483bb:   89 04 24                mov    %eax,(%esp) 80483be:   e8 d1 ff ff ff          call   8048394 <bar>} 80483c3:   c9                      leave   80483c4:   c3                      ret  

现在看bar函数的指令:

[plain] view plain copy
print?
  1. int bar(int c,int d)  
  2. {  
  3.  8048394:   55                      push   %ebp  
  4.  8048395:   89 e5                   mov    %esp,%ebp  
  5.  8048397:   83 ec 10                sub    $0x10,%esp  
  6.     int e=c+d;  
  7.  804839a:   8b 45 0c                mov    0xc(%ebp),%eax  
  8.  804839d:   8b 55 08                mov    0x8(%ebp),%edx  
  9.  80483a0:   8d 04 02                lea    (%edx,%eax,1),%eax  
  10.  80483a3:   89 45 fc                mov    %eax,-0x4(%ebp)  
  11.       
int bar(int c,int d){ 8048394:   55                      push   %ebp 8048395:   89 e5                   mov    %esp,%ebp 8048397:   83 ec 10                sub    $0x10,%esp    int e=c+d; 804839a:   8b 45 0c                mov    0xc(%ebp),%eax 804839d:   8b 55 08                mov    0x8(%ebp),%edx 80483a0:   8d 04 02                lea    (%edx,%eax,1),%eax 80483a3:   89 45 fc                mov    %eax,-0x4(%ebp)

这次又把foo函数的ebp压栈保存,然后给ebp赋了新值,指向bar函数栈帧的栈底,通过ebp+8ebp+12分别可以访问参数cdbar函数还有一个局部变量e,可以通过ebp-4来访问。所以后面几条指令的意思是把参数cd取出来存在寄存器中做加法,计算结果保存在eax寄存器中,再把eax寄存器存回局部变量e的内存单元。

gdb中可以用bt命令和frame命令查看每层栈帧上的参数和局部变量,现在可以解释它的工作原理了:如果我当前在bar函数中,我可以通过ebp找到bar函数的参数和局部变量,也可以找到foo函数的ebp保存在栈上的值,有了foo函数的ebp,又可以找到它的参数和局部变量,也可以找到main函数的ebp保存在栈上的值,因此各层函数栈帧通过保存在栈上的ebp的值串起来了。

现在看bar函数的返回指令:

[plain] view plain copy
print?
  1. return e;  
  2.  80483a6:   8b 45 fc                mov    -0x4(%ebp),%eax  
  3. }  
  4.  80483a9:   c9                      leave    
  5.  80483aa:   c3                      ret   
return e; 80483a6:   8b 45 fc                mov    -0x4(%ebp),%eax} 80483a9:   c9                      leave   80483aa:   c3                      ret 

bar函数有一个int型的返回值,这个返回值是通过eax寄存器传递的,所以首先把e的值读到eax寄存器中。然后执行leave指令,这个指令是函数开头的push %ebpmov %esp,%ebp的逆操作:

  1. ebp的值赋给esp,现在esp的值是0xbffff3d8。

  2. 现在esp所指向的栈顶保存着foo函数栈帧的ebp,把这个值恢复给ebp,同时esp增加4,esp的值变成0xbffff3dc。

最后是ret指令,它是call指令的逆操作:

  1. 现在esp所指向的栈顶保存着返回地址,把这个值恢复给eip,同时esp增加4,esp的值变成0xbffff3e0。

  2. 修改了程序计数器eip,因此跳转到返回地址0x80483c2继续执行。

地址0x80483c2处是foo函数的返回指令:

[plain] view plain copy
print?
  1. 80483c3:    c9                      leave    
  2. 80483c4:    c3                      ret    
 80483c3:   c9                      leave   80483c4:   c3                      ret  

重复同样的过程,又返回到了main函数。注意函数调用和返回过程中的这些规则:

  1. 参数压栈传递,并且是从右向左依次压栈。

  2. ebp总是指向当前栈帧的栈底。

  3. 返回值通过eax寄存器传递。

这些规则并不是体系结构所强加的,ebp寄存器并不是必须这么用,函数的参数和返回值也不是必须这么传,只是操作系统和编译器选择了以这样的方式实现C代码中的函数调用,这称为CallingConvention,Calling Convention是操作系统二进制接口规范(ABI,Application BinaryInterface)的一部分。





原创粉丝点击