上节,我们学习了函数中栈变量生成和访问的汇编指令分析,对函数体内的局部变量和局部对象的生成与访问有了一定的了解,本节我们学习全局对象,静态对象及字串、字面常量的汇编访问指令分析机制。
一,全局对象,静态对象的访问指令分析:示例代码如下
#include <stdio.h>
#include <stdlib.h>
class a
{
public:
a()
{
m_a = 3;
printf("====a:a()=====m_a=%d\n",m_a);
}
~a()
{}
int getValue(){return m_a;}
private:
int m_a;
};
a aa; //全局对象
static a bb ; //静态全局对象
int main()
{
static a cc ;//静态局部对象
printf("aa.m_a=%d,bb.m_a=%d,cc.m_a=%d\n",aa.getValue(),bb.getValue(),cc.getValue());
return 0;
}
编译,gdb调试如下:
[xx@xxg++]$ g++ aa.cpp -o aa -g
[xx@xx g++]$ gdb aa
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /xxxx/g++/aa...done.
(gdb) set disassembly-flavor intel
(gdb) b main
Breakpoint 1 at 0x400781: file aa.cpp, line 27.
(gdb) r
Starting program: xxxx/g++/aa
====a:a()=====m_a=3
====a:a()=====m_a=3
Breakpoint 1, main () at aa.cpp:27
27 static a cc ;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64 libgcc-4.4.7-17.el6.x86_64 libstdc++-4.4.7-17.el6.x86_64
从上可看出,程序在进入main函数前,全局对象和静态全局对象已经发生构造。代码汇编如下:
(gdb) disassemble /m
Dump of assembler code for function main():
26 {
0x0000000000400774 <+0>: push rbp
0x0000000000400775 <+1>: mov rbp,rsp
0x0000000000400778 <+4>: push r13
0x000000000040077a <+6>: push r12
0x000000000040077c <+8>: push rbx
0x000000000040077d <+9>: sub rsp,0x8
27 static a cc ;
=> 0x0000000000400781 <+13>: mov eax,0x600e58
0x0000000000400786 <+18>: movzx eax,BYTE PTR [rax]
…….省略
28 printf("aa.m_a=%d,bb.m_a=%d,cc.m_a=%d\n",aa.getValue(),bb.getValue(),cc.getValue());
0x00000000004007f4 <+128>: mov edi,0x600e60
0x00000000004007f9 <+133>: call 0x400764 <a::getValue()>
0x00000000004007f