Computer Systems: A Programmer's Perspective says about two sections in a ELF relocatable object file:

  • .data Initialized global and static C variables. Local C variables are maintained at run time on the stack and do not appear in either the .data or .bss sections.

  • .bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero. This section occupies no actual space in the object file; it is merely a placeholder. Object file formats distinguish between initialized and uninitialized variables for space efficiency: unini- tialized variables do not have to occupy any actual disk space in the object file. At run time, these variables are allocated in memory with an initial value of zero.

Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?

if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?

Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?

The requirements for .data and for .bss are not mutually exclusive. The .data requirement of "Initialized global and static C variables" doesn't say that such variables must be initialized to nonzero. So if a global or static C variable is initialized to zero, should it be in .data or .bss section?

Thanks.

closed as off-topic by Jeff Schaller, JdeBP, G-Man, dr01, RalfFriedl Oct 22 at 8:03

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Why don't you write a small C program, and test what happens? – dirkt Oct 21 at 19:14
  • 1
    It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss. – mosvy Oct 21 at 19:26
  • @mosvy .data is only for Initialized global and static C variables initialized to nonzero? – Tim Oct 21 at 20:03
  • 1
    that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));. – mosvy Oct 21 at 22:09
  • What, is this off topic? – Tim Oct 22 at 15:57

Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?

“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.

If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?

As per your quote:

.bss Uninitialized global and static C variables

Next:

Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?

Uninitialised global variables are zero-valued by default.

So if a global or static C variable is initialized to zero, should it be in .data or .bss section?

As per your quote:

.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.

Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.

In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.

  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero? – Tim Oct 21 at 20:40
  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background. – Stephen Kitt Oct 21 at 20:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.