https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111976

            Bug ID: 111976
           Summary: Large constant zero-valued objects should go in .bss
                    rather than .rodata
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

Right now constant objects always go in .rodata. This is nice because it will
give a nice loud error if you ever write to it. However, .rodata takes up space
in the binary file and in memory at runtime. If you instead put it in .bss it
takes up no space in the binary file and, at least on linux, it gets backed by
a single zero-filled page of physical memory. Ideally there would be something
like .robss which gave you the best of both worlds, but this is admittedly
niche for all the effort to add a new section like that. I think the best
option is to leave it in .rodata for non-optimized builds to catch bugs, but
when optimizing, especially with -Os, put it in .bss.

Repro https://www.godbolt.org/z/3rWvTrsTv:

constexpr int GB = 1024*1024*1024;

// Goes in .bss - no space in binary or runtime.
char nonconst_zeros[GB] = {};

// Goes in .rodata - expensive in binary size and runtime memory.
extern const char const_zeros[GB]; // force usage
const char const_zeros[GB] = {};


        .globl  const_zeros
        .section        .rodata
        .align 32
        .type   const_zeros, @object
        .size   const_zeros, 1073741824
const_zeros:
        .zero   1073741824


        .globl  nonconst_zeros
        .bss
        .align 32
        .type   nonconst_zeros, @object
        .size   nonconst_zeros, 1073741824
nonconst_zeros:
        .zero   1073741824

Reply via email to