Messages in this thread | | | Subject | Re: [PATCH] libosd: Remove ignored __weak attribute | From | Bart Van Assche <> | Date | Tue, 02 Oct 2018 10:57:43 -0700 |
| |
On Tue, 2018-10-02 at 10:24 -0700, Nick Desaulniers wrote: > On Mon, Oct 1, 2018 at 6:16 PM Bart Van Assche <bvanassche@acm.org> wrote: > > Additionally, zero initializers should be left out to minimize > > the size of object files. > > Sorry, my understanding was that global variables either occupy the > .bss section or the .data section, depending on whether they were > zero-initialized vs initialized to non-zero, respectively (where > non-initialized are treated as zero initialized). Looks like without > the explicit zero initialization, compilers will put the symbols in a > "common" section, which `man 1 nm` says is also unitialized data. I > didn't think .bss sections occupied space in an object file or binary; > the kernel's loader would set up the mappings at execution? Can you > clarify?
Explicitly initialized global and static variables end up in the .data section and need space in that section. That is not the case if the initializer is left out and these variables end up in the .bss section. From https://en.wikipedia.org/wiki/.bss:
"In computer programming, the name .bss or bss is used by many compilers and linkers for the portion of an object file or executable containing statically-allocated variables that are not explicitly initialized to any value. It is often referred to as the "bss section" or "bss segment".
Typically only the length of the bss section, but no data, is stored in the object file."
This is why checkpatch warns if a global or static variable is initialized explicitly to zero. From scripts/checkpatch.pl:
our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
# check for global initialisers.
if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { if (ERROR("GLOBAL_INITIALISERS", "do not initialise globals to $1\n" . $herecurr) && $fix) { $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; } }
# check for static initialisers.
if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { if (ERROR("INITIALISED_STATIC", "do not initialise statics to $1\n" . $herecurr) && $fix) { $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; } }
Bart.
| |