GCC generates identical code for the following two structures:

  const struct { const int *p; } foo = { (const int []) {1, 2, 3} };

  const struct { int *p; } foo = { (int []) {1, 2, 3} };

In both cases, the struct is written to the .rodata section, but the array {1,
2, 3} is written to the .data section:

        .section        .rodata
        .align 8
        .type   foo, @object
        .size   foo, 8
foo:
        .quad   __compound_literal.0
        .data
        .align 4
        .type   __compound_literal.0, @object
        .size   __compound_literal.0, 12
__compound_literal.0:
        .long   1
        .long   2
        .long   3

For the first struct, where the array is const, it should also be written to
the .rodata section.

I’m using gcc 4.3.2-0ubuntu2 on Intrepid amd64.


-- 
           Summary: const compound initializers in structs are written to
                    .data instead of .rodata
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: anders at kaseorg dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37303

Reply via email to