This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / aliasing


I'm using gcc-3.1.1 from debian unstable on i386.
The bug is reproducable both on an 700MHz mobile pentium3 and
an 800MHz Athlon machine.

The attached sample program produces wrong output when
compiled with -O2. Output produced when compiled with-O is
correct.
I'm attaching the C source as well as compressed preprcessor output.

$ gcc-3.1 -Wall -O2 bug.c -o bug
$ ./bug
8 8
0xbeaddeef 0x34127856
0xa307401c 0x12345678

$ gcc-3.1 -Wall -O bug.c -o bug
$ ./bug
8 8
0xbeaddeef 0x34127856
0xdeadbeef 0x12345678



$ gcc-3.1 -v -Wall -O2 bug.c -o bug
Reading specs from /usr/lib/gcc-lib/i386-linux/3.1.1/specs
Configured with: /mnt/data/gcc-3.1/gcc-3.1-3.1.1ds3/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/-3.1 --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --enable-clocale=gnu --enable-__cxa_atexit --enable-threads=posix --enable-java-gc=boehm --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.1.1
 /usr/lib/gcc-lib/i386-linux/3.1.1/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=1 -D__GNUC_PATCHLEVEL__=1 -D__ELF__ -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__OPTIMIZE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ bug.c -quiet -dumpbase bug.c -O2 -Wall -version -o /home/js/tmp/ccmIVf8b.s
GNU CPP version 3.1.1 (cpplib) (i386 Linux/ELF)
GNU C version 3.1.1 (i386-linux)
	compiled by GNU C version 3.1.1.
ignoring nonexistent directory "/usr/i386-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc-lib/i386-linux/3.1.1/include
 /usr/include
End of search list.
 as -V -Qy -o /home/js/tmp/cckfE2Pn.o /home/js/tmp/ccmIVf8b.s
GNU assembler version 2.12.90.0.14 (i386-linux) using BFD version 2.12.90.0.14 20020627 Debian GNU/Linux
 /usr/lib/gcc-lib/i386-linux/3.1.1/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o bug /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crt1.o /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crti.o /usr/lib/gcc-lib/i386-linux/3.1.1/crtbegin.o -L/usr/lib/gcc-lib/i386-linux/3.1.1 -L/usr/lib/gcc-lib/i386-linux/3.1.1/../../.. /home/js/tmp/cckfE2Pn.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-linux/3.1.1/crtend.o /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crtn.o


Regards,
Johannes
/* gcc-3.1.1 bug: wrong results with -O2 */

#include <stdio.h>
#include <asm/byteorder.h>

struct foo {
	unsigned int a: 8,
                     b:24;
	unsigned int c:16,
                     d:16;
};
struct foo_swabbed {
	unsigned int b:24,
                     a: 8;
	unsigned int d:16,
                     c:16;
};

static void swab_foo(struct foo *f)
{
        struct foo_swabbed fs = *(struct foo_swabbed *) f;
        unsigned int *p = (unsigned int *)&fs;

        p[0] = __swab32(p[0]);
        p[1] = __swab32(p[1]);
        f->a = fs.a;
        f->b = fs.b;
        f->c = fs.c;
        f->d = fs.d;
}

int main(void)
{
	struct foo f = { 0xef, 0xbeadde,  0x7856, 0x3412 };
        unsigned int *p = (unsigned int *)&f;
	printf("%u %u\n", sizeof(struct foo), sizeof(struct foo_swabbed));
	printf("%#010x %#010x\n", p[0], p[1]);
	swab_foo(&f);
	printf("%#010x %#010x\n", p[0], p[1]);
	return 0;
}

Attachment: bug.i.gz
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]