elf文件中的全局变量在哪里 [英] Where are global variables located in the elf file

查看:526
本文介绍了elf文件中的全局变量在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解elf文件,但是当我想到全局变量,全局静态变量和作用域静态变量时,
会让我有些困惑。例如:

  int a = 2; 
int b;

静态整数c = 4;
static int d;

void fun(){
static int e = 6;
static int f;
}


int main(void){
fun();
}

谁能分辨出每个变量属于哪个细分市场?我认为
b d f 属于 .bss 段和 a c e 属于数据段,但我不知道全局静态变量和elf文件中的全局变量之间的区别。

解决方案

您可以使用 objdump -t 查看符号表:

  $ objdump -t foo | grep -P'\b(a | b | c | d | e | f)\b'
0000000000601034 l O .data 0000000000000004 c
0000000000601040 l O .bss 0000000000000004 d
0000000000601044 l O .bss 0000000000000004 f.1710
0000000000601038 l O .data 0000000000000004 e.1709
0000000000601048 g O .bss 0000000000000004 b
0000000000601030 g O .data 0000000000000004 a

您说得对, b d f .bss a c e .data 。符号是否静态是记录在符号表的单独标志中,即 l g 标志在第二列中。



elf(5)手册页表示这些记录是使用 st_info <的 STB_LOCAL STB_GLOBAL 值记录的/ code>符号表的成员。 /usr/include/elf.h 表示 STB_GLOBAL 为1,而 STB_LOCAL 为0。有一个宏 ST_BIND 来检索 st_info 字段的绑定位。 / p>




objdump 还有很多其他标志,请参见< a href = http://linux.die.net/man/1/objdump>手册页。 objdump 适用于所有体系结构,但是还有一个 elfdump 工具可以更好地显示特定于elf的工具东东。 objdump 和底层的 BFD 库在显示某些特定于文件格式的数据方面做得不好。


I want to learn about elf files, but when I think of global variables, global static variables and scope static variables, I have some confusion. For example:

int a = 2;
int b;

static int c = 4;
static int d;

void fun(){
  static int e = 6;
  static int f;
}


int main(void){
   fun();
}

Who can tell which segment each variable belongs to? in my opinion, b, d and f belong to the .bss segment and a,c and e belong to the data segment, but I don't know the difference between global static variables and global variables in elf file.

解决方案

You can use objdump -t to view the symbol table:

$ objdump -t foo | grep -P '      \b(a|b|c|d|e|f)\b'
0000000000601034 l     O .data  0000000000000004              c
0000000000601040 l     O .bss   0000000000000004              d
0000000000601044 l     O .bss   0000000000000004              f.1710
0000000000601038 l     O .data  0000000000000004              e.1709
0000000000601048 g     O .bss   0000000000000004              b
0000000000601030 g     O .data  0000000000000004              a

You are right that b, d, and f are .bss while a, c, and e are .data. Whether the symbol is static or not is recorded in a separate flag of the symbol table—that’s the l or g flag in the second column.

The elf(5) man page says that these are recorded using the STB_LOCAL and STB_GLOBAL values for the st_info member of the symbol table. /usr/include/elf.h says that STB_GLOBAL is 1, while STB_LOCAL is 0. There is a macro ST_BIND to retrieve the binding bits of the st_info field.


There are tons of other flags for objdump—see the man page. objdump works with all architectures, but there is also an elfdump tool that does a bit better job of showing elf-specific stuff. objdump and the underlying BFD library can do a bad job of showing some file-format-specific data.

这篇关于elf文件中的全局变量在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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