每天学点GDB 6

今天探讨的话题是“helloworld最先是从main函数开始执行的么”。

1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 int main(int argc, char** argv) {
5     printf("hello,world\n");
6     return 0;
7 }

一个最简单的程序,简单到任何人都能信手写来。如果静下来仔细想想,这个程序是如何执行的时候,总会有一些小小的疑惑。
那么用之前的GDB知识来看看这个执行过程吧。

gdb)br 5
gdb)r
gdb)bt

显示调用堆栈

gdb)bt
#0  main (argc=1, argv=0x7fffffffe918) at hello.c:5

进一步显示frame的信息

gdb)info frame
Stack level 0, frame at 0x7fffffffe840:
 rip = 0x40050f in main (hello.c:5); saved rip 0x7ffff7a4fa15
 source language c.
 Arglist at 0x7fffffffe830, args: argc=1, argv=0x7fffffffe918
 Locals at 0x7fffffffe830, Previous frame's sp is 0x7fffffffe840
 Saved registers:
  rbp at 0x7fffffffe830, rip at 0x7fffffffe838

注意两个register的值,分别是保存的rbp和rip. saved rip表示main被调用前的代码。可以用x来看看具体的函数名称

(gdb) x 0x7ffff7a4fa15
0x7ffff7a4fa15 <__libc_start_main+245>:    0xb4e8c789

或者利用另一种方法

info symbol 0x7ffff7a4fa15
__libc_start_main + 245 in section .text of /usr/lib/libc.so.6

至此,可断定main函数被__libc_start_main调用。

如果到这还不尽兴的话,可以用如下指令来看看__libc_start_main中有哪些内容,或者直接去下载glibc源码。

gdb)disassemble  __libc_start_main
原文地址:https://www.cnblogs.com/hseagle/p/3020573.html