What all local variables goto Data/BSS segment?

1.8k views Asked by At

The man page of nm here: MAN NM says that

The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external)

And underneath it has "b" and "B" for "uninitialized data section (known as BSS)" and "d" and "D" for "initialized data section"

But I thought local variables always goto Stack/Heap and not to "Data" or "BSS" sections. Then what local variables is nm talking about?

2

There are 2 answers

0
kaylum On BEST ANSWER

"local" in this context means file scope.

That is:

static int local_data = 1; /* initialised local data */
static int local_bss; /* uninitialised local bss */
int global_data = 1; /* initialised global data */
int global_bss; /* uninitialised global bss */

void main (void)
{
   // Some code
}
0
Jonathan Leffler On

Function-scope static variables go in the data or BSS (or text) sections, depending on initialization:

void somefunc(void)
{
    static char array1[256] = "";            // Goes in BSS, probably
    static char array2[256] = "ABCDEF…XYZ";  // Goes in Data
    static const char string[] = "Kleptomanic Hypochondriac";
                                             // Goes in Text, probably
    …
}

Similar rules apply to variables defined at file scope, with or without the static storage class specifier — uninitialized or zero-initialized data goes in the BSS section; initialized data goes in the Data section; and constant data probably goes in the Text section.