“COM”是什么意思?意味着在 .symtab 部分的 Ndx 列中?

发布于 2024-10-01 09:34:20 字数 1104 浏览 1 评论 0原文

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

读取符号表: 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

Ndx 列中的“COM”意味着什么?我知道“counter”和“a”是在第#3节(即.bss)中定义的,“add”是在第#1节(即.text)中定义的,但我期待“b”和“c”也将在 .bss 部分中定义,因此在 Ndx 列中得到“3”。

谢谢

add2.c:

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

compilation:
gcc -c add2.c -o 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

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.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眸 2024-10-08 09:34:20

gcc 将未显式声明 extern 的未初始化全局变量视为“通用”符号(因此称为“COM”)。

创建最终可执行文件时,链接器会将同一公共符号(跨多个目标文件)的多个定义合并在一起,以便它们都引用相同的存储。其中一个目标文件可能会将其初始化为特定值(在这种情况下,它将最终出现在数据部分中);如果没有目标文件初始化它,它将最终出现在 BSS 中;如果多个对象初始化它,您将收到链接器错误。

总之,如果您有两个 int a 定义:

  • 一个对象中的 int a; 和另一个对象中的 int a; 是OK:两者都引用相同的 a
  • 在一个对象中初始化为 0 int a;,在另一个对象中初始化为 int a = 42; 即可:两者都引用相同的 a
  • 在一个对象中初始化为 42 int a = 23;,在另一个对象中初始化为 int a= 42;给出一个链接错误。

请注意,标准 C 在技术上不允许在两个对象中使用同一符号的多个定义;但许多编译器(包括 gcc)都支持它作为扩展。 (它列在 C99 规范中的“通用扩展”下——没有双关语的意思。)

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

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.

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

  • 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.

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.)

执手闯天涯 2024-10-08 09:34:20

来自此 PDF,表 7-11 :

SHN_COMMON
相对于定义的符号
这部分都是常用的符号,比如
作为 FORTRAN COMMON 或未分配的 C
外部变量。这些符号是
有时称为暂定。

另请参阅此页面

From this PDF, table 7-11:

SHN_COMMON
Symbols defined relative to
this section are common symbols, such
as FORTRAN COMMON or unallocated C
external variables. These symbols are
sometimes referred to as tentative.

Also, see this page.

丑丑阿 2024-10-08 09:34:20

它们是由链接器分配的未初始化的全局变量。
有时称为公共变量。

编辑:嗯,保罗·贝克(Paul Baker)比我先一步,链接也不少。使用他的答案:)

They're uninitialized global variables that are allocated by the linker.
Sometimes referred to as communal variables.

Edit: Hrmm, Paul Baker beat me to it, with links no less. use his answer :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文