bash$ gcc -o p p.c bash$ gdb p GNU gdb 19991004 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb) disas main Dump of assembler code for function main: 0x804842c <main>: push %ebp 0x804842d <main+1>: mov %esp,%ebp 0x804842f <main+3>: cmpl $0x2,0x8(%ebp) 0x8048433 <main+7>: jne 0x8048448 <main+28> 0x8048435 <main+9>: mov 0xc(%ebp),%eax 0x8048438 <main+12>: add $0x4,%eax 0x804843b <main+15>: mov (%eax),%edx 0x804843d <main+17>: push %edx 0x804843e <main+18>: call 0x8048400 <vulFunc> 0x8048443 <main+23>: add $0x4,%esp 0x8048446 <main+26>: jmp 0x804845b <main+47> 0x8048448 <main+28>: mov 0xc(%ebp),%eax 0x804844b <main+31>: mov (%eax),%edx 0x804844d <main+33>: push %edx 0x804844e <main+34>: push $0x80484bb 0x8048453 <main+39>: call 0x8048330 <printf> 0x8048458 <main+44>: add $0x8,%esp 0x804845b <main+47>: leave 0x804845c <main+48>: ret 0x804845d <main+49>: nop 0x804845e <main+50>: nop 0x804845f <main+51>: nop End of assembler dump.
首先把程序调入. bash$ gdb p GNU gdb 19991004 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb)
把断点设到main的第一条可执行汇编指令上 (gdb) b *0x804842c Breakpoint 1 at 0x804842c
运行程序 (gdb) r AAAAAAAA Starting program: /home/vcat/p AAAAAAAA
Breakpoint 1, 0x804842c in main ()
在断点处停下来了. 看一下这时各寄存器的值 (gdb) i reg eax 0x4010b3f8 1074836472 ecx 0x804842c 134513708 edx 0x4010d098 1074843800 ebx 0x4010c1ec 1074840044 esp 0xbffff6bc -1073744196 ebp 0xbffff6d8 -1073744168 esi 0x4000ae60 1073786464 edi 0xbffff704 -1073744124
再单步执行, 返回到调用main函数的函数 (gdb) si 0x400349cb in __libc_start_main (main=0x804842c <main>, argc=2, argv=0xbffff704, init=0x80482c0 <_init>, fini=0x804848c <_fini>, rtld_fini=0x4000ae60 <_dl_fini>, stack_end=0xbffff6fc) at ../sysdeps/generic/libc-start.c:92 92 ../sysdeps/generic/libc-start.c: No such file or Directory.
从上面的分析可推出, 在内存地址0xbffff6dc的内容0x08048371就是__libc_start_main函数 的返回地址了. 我们来看看是什么函数调用了__libc_start_main. (gdb) disas 0x08048371 Dump of assembler code for function _start: