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知 using gcc 4.3.2-0ubuntu2 on Intrepid amd64.
Confirmed, not a regression as far as I can tell.
From C99 6.5.2.5/8: "String literals, and compound literals with const-qualified types, need not designate distinct objects"
This is still present in 4.6, and it is more severe than a "missed optimization". It breaks storing data in ROM on microcontrollers, which is very critical when you have very little RAM.
Potential patch: http://gcc.gnu.org/ml/gcc-patches/2012-05/msg00042.html
Author: ian Date: Tue May 1 21:25:15 2012 New Revision: 187027 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=187027 Log: gcc/: PR c/37303 * c-decl.c (build_compound_literal): Make the decl readonly if it an array of a readonly type. * gimplify.c (gimplify_compound_literal_expr): Add fallback parameter. Change all callers. If the decl is not addressable and is not an l-value, make it readonly. gcc/testsuite: PR c/37303 * gcc.dg/pr37303.c: New test. Added: trunk/gcc/testsuite/gcc.dg/pr37303.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-decl.c trunk/gcc/gimplify.c trunk/gcc/testsuite/ChangeLog
Fixed on mainline for the future 4.8.0 release.
This test fails on MIPS because the section is rdata rather than rodata.
Author: sje Date: Wed Sep 26 20:33:28 2012 New Revision: 191772 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=191772 Log: 2012-09-26 Steve Ellcey <sellcey@mips.com> PR c/37303 * gcc.dg/pr37303.c: Check for rdata or rodata. Modified: trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gcc.dg/pr37303.c
This one should be fixed.