Bug 37303 - const compound initializers in structs are written to .data instead of .rodata
Summary: const compound initializers in structs are written to .data instead of .rodata
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-08-31 20:45 UTC by Anders Kaseorg
Modified: 2015-03-16 15:42 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0
Known to fail: 3.3.3, 4.0.1, 4.1.1, 4.4.0
Last reconfirmed: 2008-09-02 20:57:27


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anders Kaseorg 2008-08-31 20:45:54 UTC
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.
Comment 1 Andrew Pinski 2008-09-02 20:57:27 UTC
Confirmed, not a regression as far as I can tell.
Comment 2 Andrew Pinski 2008-10-01 17:25:40 UTC
From C99 6.5.2.5/8:

"String literals, and compound literals with const-qualified types, need not designate distinct objects"
Comment 3 Dag Ågren 2012-02-28 00:48:24 UTC
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.
Comment 4 Ian Lance Taylor 2012-05-01 20:06:35 UTC
Potential patch: http://gcc.gnu.org/ml/gcc-patches/2012-05/msg00042.html
Comment 5 ian@gcc.gnu.org 2012-05-01 21:25:20 UTC
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
Comment 6 Ian Lance Taylor 2012-05-01 21:26:28 UTC
Fixed on mainline for the future 4.8.0 release.
Comment 7 Andrew Pinski 2012-05-06 01:10:39 UTC
This test fails on MIPS because the section is rdata rather than rodata.
Comment 8 Steve Ellcey 2012-09-26 20:33:32 UTC
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
Comment 9 Marek Polacek 2015-03-16 15:42:46 UTC
This one should be fixed.