哪里是在ELF文件的全局变量 [英] Where is global variable in elf file

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

问题描述

近日,我想了解有关ELF文件的一些知识,但是当我遇到全局变量,全局静态变量和范围静态变量,我也有点困惑,例如:

Recently,I want to learn about some knowledge about elf file,but when I encounter the global variable,global static variable and scope static variable,I have a little 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();
}

谁能告诉每个变量属于哪段?在我看来,B,D,F属于BSS段,A,C,E都属于数据段,但我不知道全局静态变量之间的差异,全球varibale在ELF文件。

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

推荐答案

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

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

您是正确的, B D ˚F的.bss ,而 A C ,和电子。数据。无论符号是静态的或不被记录在符号的单独的标志表是这样的先按g 标志在第二列

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.

借助精灵(5)手册页说,这些正在使用的 STB_LOCAL记录为符号表的 st_info 成员和 STB_GLOBAL 值。 /usr/include/elf.h 表示, STB_GLOBAL 为1,而 STB_LOCAL 0有一个宏 ST_BIND 来检索 st_info 字段的结合位。

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.

有用于 objdump的吨的其他标志 - 见的人页 objdump的与所有的架构工作,但也有一个 elfdump 工具,确实呈现出一个好一点的工作精灵特有的东东。 objdump的和底层 BFD 库可以做展示一些文件格式,具体数据不好工作。

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天全站免登陆