From nobody@FreeBSD.org  Thu Sep 29 15:43:46 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 0CE7B106567C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 Sep 2011 15:43:46 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id D77598FC21
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 Sep 2011 15:43:45 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p8TFhjOk009712
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 Sep 2011 15:43:45 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p8TFhj06009701;
	Thu, 29 Sep 2011 15:43:45 GMT
	(envelope-from nobody)
Message-Id: <201109291543.p8TFhj06009701@red.freebsd.org>
Date: Thu, 29 Sep 2011 15:43:45 GMT
From: Naoyuki Tai <ntai@smartfruit.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: gcc 4.2.1 ARM produces bad code with -fstack-protector
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         161128
>Category:       arm
>Synopsis:       gcc 4.2.1 ARM produces bad code with -fstack-protector
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-arm
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Sep 29 15:50:12 UTC 2011
>Closed-Date:    Fri Nov 18 15:02:38 UTC 2011
>Last-Modified:  Fri Nov 18 15:10:08 UTC 2011
>Originator:     Naoyuki Tai
>Release:        FreeBSD 8.2 Release
>Organization:
>Environment:
FreeBSD 8.2 Release ARM
>Description:
This is first manifested as arm/154189, but any application or library that uses -fstack-protector causes crash due to the compiler bug.
So far, I discovered this in lang/perl5.xx and net/avahi-app.

Here is one of manifestation from perl5.10.
This is the assembler output of locale.c in perl5.10, with no -fstack-protector.

Perl_new_collate:
@ args = 0, pretend = 0, frame = 104
@ frame_needed = 1, uses_anonymous_args = 0
mov ip, sp
stmfd sp!, {r4, r5, r6, fp, ip, lr, pc}
sub fp, ip, #4
sub sp, sp, #104
ldr r6, .L48
.LPIC2:
add r6, pc, r6
subs r5, r0, #0
bne .L31
ldr r3, .L48+4

And the data segment .L48 is
L48:
.word _GLOBAL_OFFSET_TABLE_-(.LPIC2+8)
.word PL_collation_name(GOT)


Here is the assembler output of locale.c with the -fstack-protector.
Perl_new_collate:
@ args = 0, pretend = 0, frame = 104
@ frame_needed = 1, uses_anonymous_args = 0
mov ip, sp
stmfd sp!, {r4, r5, r6, fp, ip, lr, pc}
sub fp, ip, #4
sub sp, sp, #104
ldr r6, .L49
.LPIC2:
add r6, pc, r6
ldr r3, .L49+4

And the data segment is

.L49:
.word _GLOBAL_OFFSET_TABLE_-(.LPIC2+8)
.word __stack_chk_guard(GOT)
.word PL_collation_name(GOT)

Notice the last "ldr r3, .L49+4".
It is loading r3 register from __stack_chk_guard(GOT) instead of
PL_collation_name(GOT).
So, I think that the arm backend has a bug that it is producing wrong
offsets in the data segment when the stack protector is used.
After noticing this, I took out the "-fstack-protector" from build and
the build succeeds.
It may be the problem that the combination of -fPIC and
-fstack-protector is used.
In any case, it is clear that the GCC's arm backend has a bug.

cc --version
cc (GCC) 4.2.1 20070719 [FreeBSD]
Copyright (C) 2007 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.

>How-To-Repeat:
Method 1:
Build lang/perl.5.12 on arm.

Method 2:
Build net/avahi-app on arm and run. avahi-daemon crashes.

>Fix:
Obviously, the correct fix is to fix the compiler, and I have no idea how.

If you need these ports to run without crash, after "make configure", take out the -fstack-protector from the Makefiles and what not under expanded "work" directory.
Alternatively, modify the C compiler to ignore -fstack-protector directive.


>Release-Note:
>Audit-Trail:

From: Fabien Thomas <fabien.thomas@netasq.com>
To: bug-followup@FreeBSD.org,
 ntai@smartfruit.com
Cc:  
Subject: Re: arm/161128: gcc 4.2.1 ARM produces bad code with -fstack-protector
Date: Tue, 8 Nov 2011 10:44:30 +0100

 This is a known gcc bug and i've found that this fix correct the problem:
 
 http://people.freebsd.org/~fabient/patch-arm_gcc_stackprotector_pic
 
 The related gcc bug:
 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
 
 Fabien
 
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: arm/161128: commit references a PR
Date: Wed,  9 Nov 2011 16:14:41 +0000 (UTC)

 Author: fabient
 Date: Wed Nov  9 15:59:02 2011
 New Revision: 227391
 URL: http://svn.freebsd.org/changeset/base/227391
 
 Log:
   Import gcc fix for -fstack-protector that produces segfaulting
   binaries on arm/armel.
   
   Related gcc bug:
   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
   
   PR: 161128
   MFC after: 1 week
 
 Modified:
   head/contrib/gcc/config/arm/arm.c
 
 Modified: head/contrib/gcc/config/arm/arm.c
 ==============================================================================
 --- head/contrib/gcc/config/arm/arm.c	Wed Nov  9 15:21:48 2011	(r227390)
 +++ head/contrib/gcc/config/arm/arm.c	Wed Nov  9 15:59:02 2011	(r227391)
 @@ -3217,7 +3217,8 @@ legitimize_pic_address (rtx orig, enum m
  	  gcc_assert (!no_new_pseudos);
  	  if (arm_pic_register != INVALID_REGNUM)
  	    {
 -	      cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
 +	      if (!cfun->machine->pic_reg)
 +		cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 @@ -3229,7 +3230,8 @@ legitimize_pic_address (rtx orig, enum m
  	    {
  	      rtx seq;
  
 -	      cfun->machine->pic_reg = gen_reg_rtx (Pmode);
 +	      if (!cfun->machine->pic_reg)
 +		  cfun->machine->pic_reg = gen_reg_rtx (Pmode);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: fabient 
State-Changed-When: Wed Nov 9 16:39:57 UTC 2011 
State-Changed-Why:  
Committed. Waiting MFC / feedback. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=161128 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: arm/161128: commit references a PR
Date: Wed, 16 Nov 2011 14:33:41 +0000 (UTC)

 Author: fabient
 Date: Wed Nov 16 14:33:30 2011
 New Revision: 227551
 URL: http://svn.freebsd.org/changeset/base/227551
 
 Log:
   MFC r227391:
   Import gcc fix for -fstack-protector that produces segfaulting
   binaries on arm/armel.
   
   Related gcc bug:
   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
   
   Author kindly accepted that all of his patches can be use as GPLv2.
   
   PR:	161128
 
 Modified:
   stable/8/contrib/gcc/config/arm/arm.c
 Directory Properties:
   stable/8/contrib/gcc/   (props changed)
 
 Modified: stable/8/contrib/gcc/config/arm/arm.c
 ==============================================================================
 --- stable/8/contrib/gcc/config/arm/arm.c	Wed Nov 16 10:11:55 2011	(r227550)
 +++ stable/8/contrib/gcc/config/arm/arm.c	Wed Nov 16 14:33:30 2011	(r227551)
 @@ -3217,7 +3217,8 @@ legitimize_pic_address (rtx orig, enum m
  	  gcc_assert (!no_new_pseudos);
  	  if (arm_pic_register != INVALID_REGNUM)
  	    {
 -	      cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
 +	      if (!cfun->machine->pic_reg)
 +		cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 @@ -3229,7 +3230,8 @@ legitimize_pic_address (rtx orig, enum m
  	    {
  	      rtx seq;
  
 -	      cfun->machine->pic_reg = gen_reg_rtx (Pmode);
 +	      if (!cfun->machine->pic_reg)
 +		  cfun->machine->pic_reg = gen_reg_rtx (Pmode);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: arm/161128: commit references a PR
Date: Wed, 16 Nov 2011 14:38:02 +0000 (UTC)

 Author: fabient
 Date: Wed Nov 16 14:37:47 2011
 New Revision: 227552
 URL: http://svn.freebsd.org/changeset/base/227552
 
 Log:
   MFC r227391:
   Import gcc fix for -fstack-protector that produces segfaulting
   binaries on arm/armel.
   
   Related gcc bug:
   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
   
   Author kindly accepted that all of his patches can be use as GPLv2.
   
   PR:	161128
 
 Modified:
   stable/7/contrib/gcc/config/arm/arm.c
 Directory Properties:
   stable/7/contrib/gcc/   (props changed)
 
 Modified: stable/7/contrib/gcc/config/arm/arm.c
 ==============================================================================
 --- stable/7/contrib/gcc/config/arm/arm.c	Wed Nov 16 14:33:30 2011	(r227551)
 +++ stable/7/contrib/gcc/config/arm/arm.c	Wed Nov 16 14:37:47 2011	(r227552)
 @@ -3217,7 +3217,8 @@ legitimize_pic_address (rtx orig, enum m
  	  gcc_assert (!no_new_pseudos);
  	  if (arm_pic_register != INVALID_REGNUM)
  	    {
 -	      cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
 +	      if (!cfun->machine->pic_reg)
 +		cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 @@ -3229,7 +3230,8 @@ legitimize_pic_address (rtx orig, enum m
  	    {
  	      rtx seq;
  
 -	      cfun->machine->pic_reg = gen_reg_rtx (Pmode);
 +	      if (!cfun->machine->pic_reg)
 +		  cfun->machine->pic_reg = gen_reg_rtx (Pmode);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: arm/161128: commit references a PR
Date: Fri, 18 Nov 2011 14:42:07 +0000 (UTC)

 Author: fabient
 Date: Fri Nov 18 14:41:48 2011
 New Revision: 227664
 URL: http://svn.freebsd.org/changeset/base/227664
 
 Log:
   MFC r227391:
   Import gcc fix for -fstack-protector that produces segfaulting
   binaries on arm/armel.
   
   Related gcc bug:
   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
   
   Author kindly accepted that all of his patches can be use as GPLv2.
   
   PR: 161128
   Approved by: re (kib)
 
 Modified:
   stable/9/contrib/gcc/config/arm/arm.c
 Directory Properties:
   stable/9/contrib/gcc/   (props changed)
 
 Modified: stable/9/contrib/gcc/config/arm/arm.c
 ==============================================================================
 --- stable/9/contrib/gcc/config/arm/arm.c	Fri Nov 18 11:18:59 2011	(r227663)
 +++ stable/9/contrib/gcc/config/arm/arm.c	Fri Nov 18 14:41:48 2011	(r227664)
 @@ -3217,7 +3217,8 @@ legitimize_pic_address (rtx orig, enum m
  	  gcc_assert (!no_new_pseudos);
  	  if (arm_pic_register != INVALID_REGNUM)
  	    {
 -	      cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
 +	      if (!cfun->machine->pic_reg)
 +		cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 @@ -3229,7 +3230,8 @@ legitimize_pic_address (rtx orig, enum m
  	    {
  	      rtx seq;
  
 -	      cfun->machine->pic_reg = gen_reg_rtx (Pmode);
 +	      if (!cfun->machine->pic_reg)
 +		  cfun->machine->pic_reg = gen_reg_rtx (Pmode);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: fabient 
State-Changed-When: Fri Nov 18 14:58:14 UTC 2011 
State-Changed-Why:  
All patches done (stable/7, stable/8, stable/9, releng/9.0) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=161128 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: arm/161128: commit references a PR
Date: Fri, 18 Nov 2011 14:56:19 +0000 (UTC)

 Author: fabient
 Date: Fri Nov 18 14:56:06 2011
 New Revision: 227665
 URL: http://svn.freebsd.org/changeset/base/227665
 
 Log:
   MFC r227391:
   Import gcc fix for -fstack-protector that produces segfaulting
   binaries on arm/armel.
   
   Related gcc bug:
   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35965
   
   Author kindly accepted that all of his patches can be use as GPLv2.
   
   PR: 161128
   Approved by: re (kib)
 
 Modified:
   releng/9.0/contrib/gcc/config/arm/arm.c
 Directory Properties:
   releng/9.0/contrib/gcc/   (props changed)
 
 Modified: releng/9.0/contrib/gcc/config/arm/arm.c
 ==============================================================================
 --- releng/9.0/contrib/gcc/config/arm/arm.c	Fri Nov 18 14:41:48 2011	(r227664)
 +++ releng/9.0/contrib/gcc/config/arm/arm.c	Fri Nov 18 14:56:06 2011	(r227665)
 @@ -3217,7 +3217,8 @@ legitimize_pic_address (rtx orig, enum m
  	  gcc_assert (!no_new_pseudos);
  	  if (arm_pic_register != INVALID_REGNUM)
  	    {
 -	      cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
 +	      if (!cfun->machine->pic_reg)
 +		cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 @@ -3229,7 +3230,8 @@ legitimize_pic_address (rtx orig, enum m
  	    {
  	      rtx seq;
  
 -	      cfun->machine->pic_reg = gen_reg_rtx (Pmode);
 +	      if (!cfun->machine->pic_reg)
 +		  cfun->machine->pic_reg = gen_reg_rtx (Pmode);
  
  	      /* Play games to avoid marking the function as needing pic
  		 if we are being called as part of the cost-estimation
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
