&是什么QUOT; COM"指中的.symtab部分的NDX列? [英] What does "COM" means in the Ndx column of the .symtab section?

查看:304
本文介绍了&是什么QUOT; COM"指中的.symtab部分的NDX列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

add2.c:

int counter=0;
int a=0;
int b;
int c;
int add(int a, int b) {
    return a+b;
}

编译:
GCC -c add2.c -o add2.o

compilation: gcc -c add2.c -o add2.o

读取符号表:
readelf --symbols add2.o

reading the symbol table: readelf --symbols add2.o

Symbol table '.symtab' contains 12 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS add2.c
     2: 00000000     0 SECTION LOCAL  DEFAULT    1 
     3: 00000000     0 SECTION LOCAL  DEFAULT    2 
     4: 00000000     0 SECTION LOCAL  DEFAULT    3 
     5: 00000000     0 SECTION LOCAL  DEFAULT    5 
     6: 00000000     0 SECTION LOCAL  DEFAULT    4 
     7: 00000000     4 OBJECT  GLOBAL DEFAULT    3 counter
     8: 00000004     4 OBJECT  GLOBAL DEFAULT    3 a
     9: 00000004     4 OBJECT  GLOBAL DEFAULT  COM b
    10: 00000004     4 OBJECT  GLOBAL DEFAULT  COM c
    11: 00000000    14 FUNC    GLOBAL DEFAULT    1 add

什么是COM是指NDX列?据我所知,反和a中的一节#3(即.bss)在定义和添加,在节#定义1(即.text段),但我期待b和C在.bss段中定义过,因此获得了在NDX列3。

What does "COM" means in the Ndx column ? I understand that "counter" and "a" are defined in the section #3 (ie, .bss) and that "add" is defined in the section #1 (ie, .text), but i was expecting "b" and "c" to be defined in the .bss section too, and so get a "3" in the Ndx column.

感谢您

推荐答案

未初始化的全局GCC治疗未明确声明的extern 为普通符号(因此COM )。

gcc treats uninitialised globals which are not explicitly declared extern as "common" symbols (hence "COM").

相同的通用符号(在多个对象的文件)的多个定义是由链接器产生最后的可执行时,以使它们都指的是同一个存储合并在一起。一个目标文件可以将其初始化为特定值(在这种情况下,它会在数据段结束)的;如果没有目标文件初始化它,是将在BSS告终。如果多个对象初始化它,你会得到链接错误。

Multiple definitions of the same common symbol (across multiple object files) are merged together by the linker when creating the final executable, so that they all refer to the same storage. One of the object files may initialise it to a particular value (in which case it will end up in the data section); if no object files initialise it, is will end up in the BSS; if more than one object initialises it, you'll get a linker error.

总之,如果你有,比如说,两个定义 int类型的

In summary, if you have, say, two definitions of int a:


  • int类型的; 中的一个对象和 int类型的; 中的又一个目的是确定:指的是相同的 A ,初始化为0

  • int类型的; 中的一个对象和 int类型的= 42; 中的又一个目的是确定:既指同样 A ,初始化为42

  • int类型的= 23; 中的一个对象和 int类型的= 42; 另一个对象会给出一个链接错误。

  • int a; in one object and int a; in another object is OK: both refer to the same a, initialised to 0
  • int a; in one object and int a = 42; in another object is OK: both refer to the same a, initialised to 42
  • int a = 23; in one object and int a= 42; in another object will give a link error.

请注意,使用跨越两个对象相同符号的多个定义不是由标准C技术上允许的;但它是由许多编译器,包括海湾合作委员会的支持下,作为扩展。 ( - 没有双关语意 - 它的通用扩展中。在C99规范)

Do note that the use of multiple definitions of the same symbol across two objects is not technically allowed by standard C; but it is supported by many compilers, including gcc, as an extension. (It's listed under "Common extensions" - no pun intended - in the C99 spec.)

这篇关于&是什么QUOT; COM"指中的.symtab部分的NDX列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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