Bug 53084 - [4.7/4.8 Regression] GCC cannot handle array initialization of string constant with point arithmetic properly
Summary: [4.7/4.8 Regression] GCC cannot handle array initialization of string constan...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.1
Assignee: Jakub Jelinek
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-04-23 10:07 UTC by ray linn
Modified: 2012-04-24 09:40 UTC (History)
2 users (show)

See Also:
Host:
Target: x86_64-*-* i686-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-04-23 00:00:00


Attachments
gcc47-pr53084.patch (699 bytes, patch)
2012-04-23 19:50 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description ray linn 2012-04-23 10:07:01 UTC
Consider the following code:

#include <stdio.h>
int main() {
static const char *const aa[] = {"foo"+1};
printf("aa[0] = %s\n",aa[0]);
return 0;
}

 works fine on GCC 4.5.2 and 4.6.1
C:\work>gcc --version
gcc (tdm-1) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


C:\work>gcc a.c -oa.exe

C:\work>a
aa[0] = oo


C:\work>gcc --version
gcc (tdm-1) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


C:\work>gcc a.c -oa.exe

C:\work>a
aa[0] = oo

On GCC 4.7.0, it fails with segmentation fault.

C:\work>gcc --version
gcc (GCC) 4.7.0
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


C:\work>gcc a.c -oa.exe

C:\work>a

(error dialog)


Please go there to find the original bug threads: 
http://bugs.ruby-lang.org/issues/6333
Comment 1 Richard Biener 2012-04-23 10:22:34 UTC
Confirmed.
Comment 2 Kai Tietz 2012-04-23 10:25:05 UTC
I can confirm it with trunk version for -O0, too.

Issue is that there seems to be a wrong-code generation bug for O0 and read-only section data.

...
        movq    aa.2567(%rip), %rax
        movq    %rax, %rdx
        leaq    .LC0(%rip), %rcx
        call    printf
...

...
        .data
        .align 8
aa.2567:
        .section .rdata,"dr"
.LC1:
        .ascii "foo\0"
        .quad   .LC1+1
...

The aa.2567 label is emitted in data, but its definition in .rdata
Comment 3 Jakub Jelinek 2012-04-23 16:47:32 UTC
Can't reproduce on x86_64-linux, please fill in Target.
Comment 4 Kai Tietz 2012-04-23 17:19:27 UTC
(In reply to comment #3)
> Can't reproduce on x86_64-linux, please fill in Target.

Hmm, I can.  For x86_64-unknown-linux-gnu using -O0 as optimization option, I see for this test

aa.2162:
.LC1:
     .string "foo"
     .quad .LC1+1

which is wrong, too.
Comment 5 Jakub Jelinek 2012-04-23 17:45:55 UTC
I don't see that on the trunk:
	.section	.rodata
.LC1:
	.string	"foo"
	.align 8
	.type	aa.1706, @object
	.size	aa.1706, 8
aa.1706:
	.quad	.LC1+1
Ah, but looking at current 4.7, I can reproduce that.  Will look at it.
Comment 6 Jakub Jelinek 2012-04-23 19:50:40 UTC
Created attachment 27226 [details]
gcc47-pr53084.patch

Untested fix.
Comment 7 Kai Tietz 2012-04-23 20:14:05 UTC
This patch fixes issues for noticed for mingw targets on trunk and for 4.7.
Comment 8 Jakub Jelinek 2012-04-24 06:07:34 UTC
Author: jakub
Date: Tue Apr 24 06:07:30 2012
New Revision: 186742

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186742
Log:
	PR middle-end/53084
	* varasm.c (compute_reloc_for_constant): Handle ADDR_EXPR
	of MEM_REF.
	(output_addressed_constants): Likewise.

	* gcc.c-torture/execute/pr53084.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr53084.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/varasm.c
Comment 9 Jakub Jelinek 2012-04-24 06:11:00 UTC
Author: jakub
Date: Tue Apr 24 06:10:53 2012
New Revision: 186745

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186745
Log:
	PR middle-end/53084
	* varasm.c (compute_reloc_for_constant): Handle ADDR_EXPR
	of MEM_REF.
	(output_addressed_constants): Likewise.

	* gcc.c-torture/execute/pr53084.c: New test.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gcc.c-torture/execute/pr53084.c
Modified:
    branches/gcc-4_7-branch/gcc/ChangeLog
    branches/gcc-4_7-branch/gcc/varasm.c
Comment 10 Jakub Jelinek 2012-04-24 06:14:43 UTC
Author: jakub
Date: Tue Apr 24 06:14:37 2012
New Revision: 186746

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186746
Log:
	PR middle-end/53084
	* varasm.c (compute_reloc_for_constant): Handle ADDR_EXPR
	of MEM_REF.
	(output_addressed_constants): Likewise.

	* gcc.c-torture/execute/pr53084.c: New test.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/gcc.c-torture/execute/pr53084.c
Modified:
    branches/gcc-4_6-branch/gcc/ChangeLog
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_6-branch/gcc/varasm.c
Comment 11 Jakub Jelinek 2012-04-24 09:40:17 UTC
Should be fixed now.