什么是"__gmon_start__"?象征? [英] What is the "__gmon_start__" symbol?

查看:440
本文介绍了什么是"__gmon_start__"?象征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用gcc hello.c -o hello -O3编译此代码:

#include <stdio.h>

int main(void) {
    printf("Hello world\n");
    return 0;
}

当我列出重定位时,我得到:

and when I list the relocations I get:

test@southpark$ readelf -r hello | grep gmon
080495a4  00000106 R_386_GLOB_DAT    00000000   __gmon_start__
080495b4  00000107 R_386_JUMP_SLOT   00000000   __gmon_start__

当我列出此文件中的符号时,我得到:

When I list the symbols in this file I get:

test@southpark$ readelf -s hello | grep gmon
     1: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    48: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

gmon_start与gprof有关系吗?为什么即使我未使用-pg-g进行编译/链接,该符号也为何对该符号进行了重定位?哪个库可以解析该符号?

Does gmon_start have anything to do with gprof? Why does it have a relocation for that symbol even I didn't compile/link with -pg or -g? What library would resolve this symbol?

推荐答案

做了一些谷歌搜索,并从此处找到:

Did a little googling and found this from here:

函数call_gmon_start初始化gmon分析系统. 当使用-pg标志编译二进制文件时,将启用此系统, 并创建用于gprof(1)的输出.在这种情况下 二进制call_gmon_start直接位于_start的后面 功能. call_gmon_start函数可在 全局偏移表(也称为__gmon_start__),如果不为NULL, 将控制权传递给指定的地址. __gmon_start__元素 指向gmon初始化函数,该函数开始记录 分析信息并使用以下命令注册清除功能 atexit().但是在我们的情况下,gmon没有被使用,因此 __gmon_start__为NULL.

The function call_gmon_start initializes the gmon profiling system. This system is enabled when binaries are compiled with the -pg flag, and creates output for use with gprof(1). In the case of the scenario binary call_gmon_start is situated directly proceeding that _start function. The call_gmon_start function finds the last entry in the Global Offset Table (also known as __gmon_start__) and, if not NULL, will pass control to the specified address. The __gmon_start__ element points to the gmon initialization function, which starts the recording of profiling information and registers a cleanup function with atexit(). In our case however gmon is not in use, and as such __gmon_start__ is NULL.

所以...

  1. 是的,它确实与gprof有关
  2. 我不确定为什么符号会留在那儿.也许只是为gprof编译时的占位符?

更新:

Update:

好的,所以我在有和没有-pg的情况下都编译了您的代码.看来__gmon_start__映射到了已编译程序中的地址.话虽这么说,但我认为没有一个库可以解析该符号,而是程序本身.

Okay, so I compiled your code with and without -pg. It looks like __gmon_start__ gets mapped to an address within the compiled program. So with that being said, I don't think there's a library which resolves that symbol, but the program itself.

-pg:

with -pg:

akyserr@orion:~$ readelf -r hello

Relocation section '.rel.dyn' at offset 0x32c contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049fec  00000806 R_386_GLOB_DAT    08048460   __gmon_start__

Relocation section '.rel.plt' at offset 0x334 contains 6 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0804a000  00000607 R_386_JUMP_SLOT   080483b0   _mcleanup
0804a004  00000107 R_386_JUMP_SLOT   00000000   __monstartup
0804a008  00000207 R_386_JUMP_SLOT   00000000   mcount
0804a00c  00000307 R_386_JUMP_SLOT   00000000   __cxa_atexit
0804a010  00000407 R_386_JUMP_SLOT   00000000   puts
0804a014  00000507 R_386_JUMP_SLOT   00000000   __libc_start_main

__ gmon_start__代码的objdump:

akyserr@orion:~$ objdump -S hello  | grep "460 <__gmon_start__>:" -A 20

08048460 <__gmon_start__>:
 8048460:       83 ec 1c                sub    $0x1c,%esp
 8048463:       a1 20 a0 04 08          mov    0x804a020,%eax
 8048468:       85 c0                   test   %eax,%eax
 804846a:       75 2a                   jne    8048496 <__gmon_start__+0x36>
 804846c:       c7 05 20 a0 04 08 01    movl   $0x1,0x804a020
 8048473:       00 00 00 
 8048476:       c7 44 24 04 36 86 04    movl   $0x8048636,0x4(%esp)
 804847d:       08 
 804847e:       c7 04 24 30 84 04 08    movl   $0x8048430,(%esp)
 8048485:       e8 36 ff ff ff          call   80483c0 <__monstartup@plt>
 804848a:       c7 04 24 b0 83 04 08    movl   $0x80483b0,(%esp)
 8048491:       e8 1a 01 00 00          call   80485b0 <atexit>
 8048496:       83 c4 1c                add    $0x1c,%esp
 8048499:       c3                      ret    
 804849a:       90                      nop
 804849b:       90                      nop
 804849c:       90                      nop
 804849d:       90                      nop

在已编译的hello程序中存在__gmon_start__的情况下,您可以看到已调用__monstartup. ( monstartup手册页)

With the __gmon_start__ present in the compiled hello program, you can see that that __monstartup is called into. (monstartup man page)

没有-pg:

without -pg:

akyserr@orion:~$ readelf -r hello 

Relocation section '.rel.dyn' at offset 0x290 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049ff0  00000206 R_386_GLOB_DAT    00000000   __gmon_start__

Relocation section '.rel.plt' at offset 0x298 contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0804a000  00000107 R_386_JUMP_SLOT   00000000   puts
0804a004  00000207 R_386_JUMP_SLOT   00000000   __gmon_start__
0804a008  00000307 R_386_JUMP_SLOT   00000000   __libc_start_main

您可以在此处看到__gmon_start__的符号值设置为00000000.

You can see here, that the symbol value of __gmon_start__ is set to 00000000.

这篇关于什么是"__gmon_start__"?象征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆