What does “COM” means in the Ndx column of the .symtab section?

我是研究僧i 提交于 2019-12-04 05:59:20

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

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!