This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
high priority: gcc generates weird code
- To: gcc-bugs at gcc dot gnu dot org
- Subject: high priority: gcc generates weird code
- From: QingLong <qinglong at Bolizm dot ihep dot su>
- Date: Fri, 2 Mar 2001 22:20:08 +0300
Hello!
The program attached to this message been compiled by GCC
segfaults at line 65. This excerpt from gdb:
|
| Starting program: ./reverse
| 20 3-byte elements:
| .oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO
|
| Program received signal SIGSEGV, Segmentation fault.
| 0x8048840 in Queue_reverse_byte_order (the_buf=0x8049d00, the_size=3,
| the_nmemb=20, theVerbose=1, theDebug=1) at reverse.c:65
| 65 *j_tail_p = *j_head_p;
| (gdb) where
| #0 0x8048840 in Queue_reverse_byte_order (the_buf=0x8049d00, the_size=3,
| the_nmemb=20, theVerbose=1, theDebug=1) at reverse.c:65
| #1 0x804892c in main (argc=1, argv=0xbfffe184) at reverse.c:94
| #2 0x40031a4e in __libc_start_main (main=0x8048884 <main>, argc=1,
| argv=0xbfffe184, init=0x80485d0 <_init>, fini=0x8049b74 <_fini>,
| rtld_fini=0x4000c0c4 <_dl_fini>, stack_end=0xbfffe17c)
| at ../sysdeps/generic/libc-start.c:78
| (gdb) print j_head_p
| $1 = 0x8049d00 ".oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO"
| (gdb) print j_tail_p
| $2 = 0x8049d02 "O.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO"
| (gdb) print i_head_p
| $3 = 0x8049d00 ".oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO"
| (gdb) print i_tail_p
| $4 = 0x8049d03 ".oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO"
| (gdb) print *j_tail_p
| $5 = 79 'O'
| (gdb) print *j_head_p
| $6 = 46 '.'
| (gdb) kill
| Kill the program being debugged? (y or n) y
| (gdb) quit
|
I've tried to compile it with (and without) -O, -O6,
tried gcc versions (output of `gcc -v'):
|
| Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
| gcc version 2.96 20000731 (Red Hat Linux 7.0)
+------------------------------------------------------------------
| Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.3/specs
| gcc version 2.95.3 20010125 (prerelease)
+------------------------------------------------------------------
| Reading specs from /usr/lib/gcc-lib/alpha-unknown-openbsd2.8/2.95.3/specs
| gcc version 2.95.3 19991030 (prerelease)
+------------------------------------------------------------------
| Reading specs from /usr/lib/gcc-lib/mipsel-unknown-openbsd2.7/2.95.3/specs
| gcc version 2.95.3 19991030 (prerelease)
+------------------------------------------------------------------
| Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
| gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
+------------------------------------------------------------------
| Reading specs from /opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/specs
| gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)
+------------------------------------------------------------------
| Reading specs from /usr/lib/gcc-lib/mipsel-unknown-openbsd2.3/2.8.1/specs
| gcc version 2.8.1
+------------------------------------------------------------------
| Reading specs from /usr/local/lib/gcc-lib/vax-dec-ultrix4.3/2.8.1/specs
| gcc version 2.8.1
|
with absolutely the same result.
I believe that the code is correct as `gcc -Wall' issues no warnings,
as do cc's on ``OSF1 V5.0 1094 alpha'' and ``IRIX 6.2 06101030 IP22''
(failed to find out more version info on those compilers),
while the latter does produce correct binary that behaves quite as expected.
After trying varoius systems here, I've found out that actually
there are GCC installations that are able to handle the code:
|
| Reading specs from /usr/gnu/lib/gcc-lib/mips-sgi-irix5.3/2.7.2.2/specs
| gcc version 2.7.2.2
+------------------------------------------------------------------
| Reading specs from /usr/local/lib/gcc-lib/alpha-dec-osf4.0b/egcs-2.91.60/specs
| gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)
|
I think it's not worth submitting you separate reports for all those
buggy versions, so I just do it for one of them.
Please see mail attachments. If you need more info, please ask me.
Thank you.
QingLong.
#include <stdio.h>
#include <sys/types.h>
static char* sample[] =
{
".oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO",
".oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o",
"abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345",
"abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345",
"abcdef123456abcdef123456abcdef123456abcdef123456abcdef123456",
"abcdef123456abcdef123456abcdef123456abcdef123456abcdef123456",
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUwWvVxXyYzZ01234567",
"012345678901234567890123456789012345678901234567890123456789",
NULL
};
static size_t sample_length = 60;
static size_t sample_itsize[] = { 3, 5, 5, 10, 6, 12, 2, 10, 0 };
/*
* Reverse byte order in elements of given size.
*/
int Queue_reverse_byte_order(void* the_buf, size_t the_size, size_t the_nmemb,
int theVerbose, int theDebug)
{
static char Iam[] = "qlib.c Queue_reverse_byte_order()";
int returnValue = -1;
if ((the_buf == NULL) ||
(the_size <= 0) ||
(the_nmemb < 0))
{fprintf(stderr, "%s: weird args.\n", Iam);
return returnValue;}
if (the_size == 1)
{if (theDebug) fprintf(stderr, "%s: 1-byte elements?\n", Iam);
return the_nmemb;}
if (the_nmemb == 0)
{if (theDebug) fprintf(stderr, "%s: zero count?\n", Iam);
return 0;}
{
char c;
int i, j;
size_t a_half_size = the_size/2;
char* i_head_p;
char* i_tail_p;
char* j_head_p;
char* j_tail_p;
returnValue = 0; /* Since now this is an element counter. */
i_head_p = i_tail_p = (char*)the_buf;
for (i = the_nmemb;;)
{
i_tail_p += the_size;
j_head_p = i_head_p;
j_tail_p = i_tail_p;
j_tail_p--;
for (j = a_half_size;;)
{
c = *j_tail_p;
*j_tail_p = *j_head_p;
*j_head_p = c;
if (--j == 0) break;
j_head_p++;
j_tail_p--;
}
returnValue++;
if (--i == 0) break;
i_head_p = i_tail_p;
}
}
return returnValue;
}
int main(int argc, char** argv)
{
int i;
size_t item_size, item_number;
size_t* item_size_p = sample_itsize;
char* sample_i;
char** sample_i_p = sample;
for (i = 0; (item_size = *item_size_p++) && (sample_i = *sample_i_p++); i++)
{
item_number = sample_length/item_size;
fprintf(stdout,
"%li %li-byte elements:\n", (long)item_number, (long)item_size);
fputs(sample_i, stdout);
fputs("\n", stdout);
Queue_reverse_byte_order((void*)sample_i, item_size, item_number, 1, 1);
fputs(sample_i, stdout);
fputs("\n\n", stdout);
}
return 0;
}
reverse.i gzipped
.file "reverse.c"
.version "01.01"
gcc2_compiled.:
.data
.align 4
.type sample,@object
sample:
.long .LC0
.long .LC1
.long .LC2
.long .LC2
.long .LC3
.long .LC3
.long .LC4
.long .LC5
.long 0
.section .rodata
.align 32
.LC5:
.string "012345678901234567890123456789012345678901234567890123456789"
.align 32
.LC4:
.string "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUwWvVxXyYzZ01234567"
.align 32
.LC3:
.string "abcdef123456abcdef123456abcdef123456abcdef123456abcdef123456"
.align 32
.LC2:
.string "abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345"
.align 32
.LC1:
.string ".oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o.oO0o"
.align 32
.LC0:
.string ".oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO.oO"
.size sample,36
.data
.align 4
.type sample_length,@object
.size sample_length,4
sample_length:
.long 60
.align 4
.type sample_itsize,@object
sample_itsize:
.long 3
.long 5
.long 5
.long 10
.long 6
.long 12
.long 2
.long 10
.long 0
.size sample_itsize,36
.align 32
.type Iam.2,@object
.size Iam.2,34
Iam.2:
.string "qlib.c Queue_reverse_byte_order()"
.section .rodata
.LC6:
.string "%s: weird args.\n"
.LC7:
.string "%s: 1-byte elements?\n"
.LC8:
.string "%s: zero count?\n"
.text
.align 4
.globl Queue_reverse_byte_order
.type Queue_reverse_byte_order,@function
Queue_reverse_byte_order:
pushl %ebp
movl %esp,%ebp
subl $36,%esp
movl $-1,-4(%ebp)
cmpl $0,8(%ebp)
je .L3
cmpl $0,12(%ebp)
jne .L2
jmp .L3
.p2align 4,,7
.L3:
pushl $Iam.2
pushl $.LC6
movl stderr,%eax
pushl %eax
call fprintf
addl $12,%esp
movl -4(%ebp),%edx
movl %edx,%eax
jmp .L1
.p2align 4,,7
.L2:
cmpl $1,12(%ebp)
jne .L4
cmpl $0,24(%ebp)
je .L5
pushl $Iam.2
pushl $.LC7
movl stderr,%eax
pushl %eax
call fprintf
addl $12,%esp
.L5:
movl 16(%ebp),%edx
movl %edx,%eax
jmp .L1
.p2align 4,,7
.L4:
cmpl $0,16(%ebp)
jne .L6
cmpl $0,24(%ebp)
je .L7
pushl $Iam.2
pushl $.LC8
movl stderr,%eax
pushl %eax
call fprintf
addl $12,%esp
.L7:
xorl %eax,%eax
jmp .L1
.p2align 4,,7
.L6:
movl 12(%ebp),%edx
movl %edx,%eax
shrl $1,%eax
movl %eax,-20(%ebp)
movl $0,-4(%ebp)
movl 8(%ebp),%eax
movl %eax,-28(%ebp)
movl %eax,-24(%ebp)
movl 16(%ebp),%eax
movl %eax,-12(%ebp)
.p2align 4,,7
.L8:
movl 12(%ebp),%eax
addl %eax,-28(%ebp)
movl -24(%ebp),%eax
movl %eax,-32(%ebp)
movl -28(%ebp),%eax
movl %eax,-36(%ebp)
decl -36(%ebp)
movl -20(%ebp),%eax
movl %eax,-16(%ebp)
.p2align 4,,7
.L11:
movl -36(%ebp),%eax
movb (%eax),%dl
movb %dl,-5(%ebp)
movl -36(%ebp),%eax
movl -32(%ebp),%edx
movb (%edx),%cl
movb %cl,(%eax)
movl -32(%ebp),%eax
movb -5(%ebp),%dl
movb %dl,(%eax)
decl -16(%ebp)
cmpl $0,-16(%ebp)
jne .L14
jmp .L12
.p2align 4,,7
.L14:
incl -32(%ebp)
decl -36(%ebp)
.L13:
jmp .L11
.p2align 4,,7
.L12:
incl -4(%ebp)
decl -12(%ebp)
cmpl $0,-12(%ebp)
jne .L15
jmp .L9
.p2align 4,,7
.L15:
movl -28(%ebp),%eax
movl %eax,-24(%ebp)
.L10:
jmp .L8
.p2align 4,,7
.L9:
movl -4(%ebp),%edx
movl %edx,%eax
jmp .L1
.p2align 4,,7
.L1:
movl %ebp,%esp
popl %ebp
ret
.Lfe1:
.size Queue_reverse_byte_order,.Lfe1-Queue_reverse_byte_order
.section .rodata
.LC9:
.string "%li %li-byte elements:\n"
.LC10:
.string "\n"
.LC11:
.string "\n\n"
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
movl $sample_itsize,-16(%ebp)
movl $sample,-24(%ebp)
movl $0,-4(%ebp)
.p2align 4,,7
.L17:
movl -16(%ebp),%edx
movl (%edx),%eax
movl %eax,-8(%ebp)
addl $4,-16(%ebp)
testl %eax,%eax
je .L21
movl -24(%ebp),%edx
movl (%edx),%eax
movl %eax,-20(%ebp)
addl $4,-24(%ebp)
testl %eax,%eax
jne .L20
jmp .L21
.p2align 4,,7
.L21:
jmp .L18
.p2align 4,,7
.L20:
movl sample_length,%ecx
movl %ecx,%eax
xorl %edx,%edx
divl -8(%ebp)
movl %eax,-12(%ebp)
movl -8(%ebp),%eax
pushl %eax
movl -12(%ebp),%eax
pushl %eax
pushl $.LC9
movl stdout,%eax
pushl %eax
call fprintf
addl $16,%esp
movl stdout,%eax
pushl %eax
movl -20(%ebp),%eax
pushl %eax
call fputs
addl $8,%esp
movl stdout,%eax
pushl %eax
pushl $.LC10
call fputs
addl $8,%esp
pushl $1
pushl $1
movl -12(%ebp),%eax
pushl %eax
movl -8(%ebp),%eax
pushl %eax
movl -20(%ebp),%eax
pushl %eax
call Queue_reverse_byte_order
addl $20,%esp
movl stdout,%eax
pushl %eax
movl -20(%ebp),%eax
pushl %eax
call fputs
addl $8,%esp
movl stdout,%eax
pushl %eax
pushl $.LC11
call fputs
addl $8,%esp
.L19:
incl -4(%ebp)
jmp .L17
.p2align 4,,7
.L18:
xorl %eax,%eax
jmp .L16
.p2align 4,,7
.L16:
movl %ebp,%esp
popl %ebp
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) egcs-2.91.60 19981201 (egcs-1.1.1 release)"
Reading specs from /opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/specs
gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)
/opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/cpp -lang-c -v -undef -D__GNUC__=2 -D__GNUC_MINOR__=91 -D__ELF__ -Dunix -Di386 -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__i386__ -D__linux__ -D__unix -D__i386 -D__linux -Asystem(posix) -Asystem(unix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di686 -Dpentiumpro -D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__ reverse.c reverse.i
GNU CPP version egcs-2.91.60 19981201 (egcs-1.1.1 release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/opt/egcs/i686-pc-linux-gnu/include
/opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/include
/usr/include
End of search list.
/opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/cc1 reverse.i -quiet -dumpbase reverse.c -version -o reverse.s
GNU C version egcs-2.91.60 19981201 (egcs-1.1.1 release) (i686-pc-linux-gnu) compiled by GNU C version egcs-2.91.60 19981201 (egcs-1.1.1 release).
as -V -Qy -o reverse.o reverse.s
GNU assembler version 2.9.1 (i686-pc-linux-gnu), using BFD version 2.9.1.0.19
/opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o reverse /usr/lib/crt1.o /usr/lib/crti.o /opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/crtbegin.o -L/opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60 -L/opt/egcs/i686-pc-linux-gnu/lib -L/opt/egcs/lib reverse.o -lgcc -lc -lgcc /opt/egcs/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.60/crtend.o /usr/lib/crtn.o