Subject: pkg/10330: powerpc-netbsd-gcc generates incorrect object code
To: None <gnats-bugs@gnats.netbsd.org>
From: None <ncrnko@jps.net>
List: netbsd-bugs
Date: 06/08/2000 20:25:05
>Number:         10330
>Category:       pkg
>Synopsis:       powerpc-netbsd-gcc generates incorrect object code
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    pkg-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 08 20:25:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Nenad Crnko
>Release:        netbsd-1.4.1
>Organization:
>Environment:
$ uname -a
NetBSD aeolus 1.4.1 NetBSD 1.4.1 (mariposa) #3: Fri Jun  2 03:15:51 PDT 2000
    root@aeolus:/usr/src/sys/arch/i386/compile/mariposa i386

$ /usr/pkg/cross/bin/powerpc-netbsd-gcc -v
Reading specs from
/usr/pkg/cross/lib/gcc-lib/powerpc-netbsd/egcs-2.91.60/specs
gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)


>Description:
When cross-compiling with optimizations turned off, powerpc-netbsd-gcc
generates incorrect code for initialization of global constant pointers
to structures.


>How-To-Repeat:
#! /bin/sh

cat >foo.c <<EOF
/*
 * problem case
 */
typedef struct
IOports {
   volatile struct A{
      unsigned int ddra;
      unsigned int data;
   } portA;

   volatile struct B{
      unsigned int ddrb;
      unsigned int data;
   } portB;
} IOPORTS;


IOPORTS * const port = (IOPORTS *) 0x12345678UL;

void output(void){


   port->portA.data = 0xaaaa5555UL;

}
/* end of problem case */
EOF

cat foo.c
/usr/pkg/cross/bin/powerpc-netbsd-gcc -v -g -c  -nostdlib \
  -nostartfiles -Wa,-a=foo.lst  -o foo.o foo.c
grep -A 10 "23:foo" foo.lst

# end of "source" file

The incorrect code can be seen in lines numbered 51 and 52:

  23:foo.c         ****    port->portA.data = 0xaaaa5555UL;
  49                    .stabn 68,0,23,.LM2-output
  50                    .LM2:
  51 000c 3D600000              addis 11,0,port@ha
  52 0010 812B0000              lwz 9,port@l(11)
  53 0014 3C00AAAA              lis 0,0xaaaa
  54 0018 60005555              ori 0,0,21845
  55 001c 90090004              stw 0,4(9)
  24:foo.c         ****
  25:foo.c         **** }
  56                    .stabn 68,0,25,.LM3-output

The correct code would be :
     addis 9,0,port@ha
     ori   9,0,port@l

>Fix:
There are couple of known workarounds:
1.) turn on any level of optimization as in 
    /usr/pkg/cross/bin/powerpc-netbsd-gcc -v -g -c -O  -nostdlib \
      -nostartfiles -Wa,-a=foo.lst  -o foo.o foo.c

2.) make pointer initialization local by pulling it into the function
    body (not a preferred workaround).
>Release-Note:
>Audit-Trail:
>Unformatted: