“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
读取符号表: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
gcc 将未显式声明 extern 的未初始化全局变量视为“通用”符号(因此称为“COM”)。
创建最终可执行文件时,链接器会将同一公共符号(跨多个目标文件)的多个定义合并在一起,以便它们都引用相同的存储。其中一个目标文件可能会将其初始化为特定值(在这种情况下,它将最终出现在数据部分中);如果没有目标文件初始化它,它将最终出现在 BSS 中;如果多个对象初始化它,您将收到链接器错误。
总之,如果您有两个
int a
定义:int a;
和另一个对象中的int a;
是OK:两者都引用相同的a
,int a;
,在另一个对象中初始化为int a = 42;
即可:两者都引用相同的a
,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 andint a;
in another object is OK: both refer to the samea
, initialised to 0int a;
in one object andint a = 42;
in another object is OK: both refer to the samea
, initialised to 42int a = 23;
in one object andint 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.)
来自此 PDF,表 7-11 :
另请参阅此页面。
From this PDF, table 7-11:
Also, see this page.
它们是由链接器分配的未初始化的全局变量。
有时称为公共变量。
编辑:嗯,保罗·贝克(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 :)