Bug 44690 - -fzero-initialized-in-bss does not work
Summary: -fzero-initialized-in-bss does not work
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.0
: P3 minor
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-06-27 12:24 UTC by Jan Kratochvil
Modified: 2021-10-03 03:01 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-unknown-linux-gnu
Build:
Known to work: 8.1.0
Known to fail: 7.1.0, 7.5.0
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Kratochvil 2010-06-27 12:24:50 UTC
const int i;     -fno-zero-initialized-in-bss -> .bss    = FAIL
const int i;        -fzero-initialized-in-bss -> .bss    
const int i = 0; -fno-zero-initialized-in-bss -> .rodata
const int i = 0;    -fzero-initialized-in-bss -> .rodata = FAIL
const int i = 1; -fno-zero-initialized-in-bss -> .rodata
const int i = 1;    -fzero-initialized-in-bss -> .rodata

gcc (GCC) 4.6.0 20100627 (experimental)
(gcc-4.4+ behaves the same)

-fzero-initialized-in-bss is the default now.

(a) Creating .robss would solve it all but OK, there isn't any.

(b) At least -O0 -g should default to -fno-zero-initialized-in-bss as it better
    catches bugs of modified const zeroed variables.
Comment 1 Andrew Pinski 2010-07-21 22:23:51 UTC
const int i;     -fno-zero-initialized-in-bss -> .bss    = FAIL
You need -fno-common also to get it out of the BSS.
Comment 2 Andrew Pinski 2010-07-21 22:56:04 UTC
>const int i = 0;    -fzero-initialized-in-bss -> .rodata = FAIL

This is correct because it should really be in readonly data section as it is constant.
Comment 3 Jan Kratochvil 2010-07-23 11:11:03 UTC
(In reply to comment #1)
> const int i;     -fno-zero-initialized-in-bss -> .bss    = FAIL
> You need -fno-common also to get it out of the BSS.

-fno-common now at least correctly reports:
echo 'const int i;'|gcc -fno-common -c -o k.o -x c -;echo 'const int i;main(){}'|gcc -fno-common -c -o l.o -x c -;gcc -fno-common -o k k.o l.o
l.o:(.bss+0x0): multiple definition of `i'
k.o:(.bss+0x0): first defined here

So it is no longer SHN_COMMON, thanks.


But reopening the Bug as it is still in .bss, not in .rodata.

echo 'const int i;int main(){return *(int *)&i = 0;}'|gcc -fno-common -Wall -o 1 -x c -;./1;echo $?
0
  [25] .bss              NOBITS           0000000000600818  00000814
       0000000000000018  0000000000000000  WA       0     0     8
   Num:    Value          Size Type    Bind   Vis      Ndx Name
    55: 0000000000600828     4 OBJECT  GLOBAL DEFAULT   25 i

It should SEGV but it does not.
-f{,no-}zero-initialized-in-bss has no effect on it.
Comment 4 Andrew Pinski 2021-10-03 03:01:25 UTC
Fixed by r8-2857-g2ec399d8a6c9c26 (there was a few followups patches which fix problems with this patch too).