.bss vs COMMON: what goes where?

gone picture gone · May 30, 2013 · Viewed 11.4k times · Source

From my book:

.bss:

Uninitialized global C variables

COMMON:

Uninitalized data objects that are not yet allocated

I have to say, I don't quite see a clear distinction. I don't even quite understand what an uninitizalied, non-allocated data object is...seems like nothing. I've used GNU's readelf tool to try to take a look at some simple C code, but can't find a single COMMON symbol. I've read things like FORTRAN's COMMON type is an example of a COMMON symbol - but I don't know FORTRAN

Can someone possibly distinguish the two for me? If at all possible, hopefully with a C example? Greatly appreciated.

edit: from this post, the variable c here:

int c;
int main() {} ...

should be COMMON. But using objdump -t shows for me that c is in .bss...

confused

Answer

ouah picture ouah · May 30, 2013
// file a.c
// file-scope

int a = 0;  // goes into BSS

after compilation of a.c into object file a.o, a symbol goes into BSS section.

// file b.c
// file-scope

int b;  // goes into COMMON section

after compilation of b.c into object file b.o, b symbol goes into COMMON section.

After linking of a.o and b.o, both a and b symbols goes into BSS section. Common symbols only exist in object files, not in executable files. The idea of COMMON symbols in Unix is to allow multiple external definitions of a same variable (in different compilation units) under a single common symbol under certain conditions.