public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: target/6912: x86_64 switch code generation problem with -fPIC
@ 2002-07-12 11:06 Frank van der Linden
  0 siblings, 0 replies; 4+ messages in thread
From: Frank van der Linden @ 2002-07-12 11:06 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR target/6912; it has been noted by GNATS.

From: Frank van der Linden <fvdl@wasabisystems.com>
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: target/6912: x86_64 switch code generation problem with -fPIC
Date: Fri, 12 Jul 2002 19:57:41 +0200

 As of 20020711, both the trunk and the 3.1 branch seem to generate correct
 code again.
 
 The 3.1 branch uses a correct movslq, and -current, while not doing the
 sign extension, always seems to put the jumptable in the text segment,
 right after the table index + jump code. So the offsets are always
 positive.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: target/6912: x86_64 switch code generation problem with -fPIC
@ 2002-10-01  5:35 hubicka
  0 siblings, 0 replies; 4+ messages in thread
From: hubicka @ 2002-10-01  5:35 UTC (permalink / raw)
  To: fvdl, gcc-bugs, gcc-prs, nobody

Synopsis: x86_64 switch code generation problem with -fPIC

State-Changed-From-To: open->closed
State-Changed-By: hubicka
State-Changed-When: Tue Oct  1 05:35:27 2002
State-Changed-Why:
    Fixed some time agoathlon

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6912


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: target/6912: x86_64 switch code generation problem with -fPIC
@ 2002-06-05  8:56 Frank van der Linden
  0 siblings, 0 replies; 4+ messages in thread
From: Frank van der Linden @ 2002-06-05  8:56 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR target/6912; it has been noted by GNATS.

From: Frank van der Linden <fvdl@wasabisystems.com>
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: target/6912: x86_64 switch code generation problem with -fPIC
Date: Wed, 5 Jun 2002 17:44:50 +0200

 I did a checkout of gcc 3.1 (gcc_3_1_release tag), and found that
 plain 3.1 generates the correct code. However, the head of the
 3.1 branch (gcc-3_1-branch tag) generates incorrect code for
 this case.
 
 That narrows it down to changes put on the 3.1 branch after
 3.1 was released.
 
 - Frank


^ permalink raw reply	[flat|nested] 4+ messages in thread

* target/6912: x86_64 switch code generation problem with -fPIC
@ 2002-06-02 17:46 fvdl
  0 siblings, 0 replies; 4+ messages in thread
From: fvdl @ 2002-06-02 17:46 UTC (permalink / raw)
  To: gcc-gnats


>Number:         6912
>Category:       target
>Synopsis:       x86_64 switch code generation problem with -fPIC
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Sun Jun 02 17:46:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Frank van der Linden
>Release:        3.2 20020602 (experimental)
>Organization:
>Environment:
System: NetBSD k2 1.5ZC NetBSD 1.5ZC (K2) #0: Wed May 1 02:01:29 CEST 2002 fvdl@k2:/work/trees/empee/sys/arch/i386/compile/K2 i386

host: i386-unknown-netbsdelf1.5ZC
build: i386-unknown-netbsdelf1.5ZC
target: x86_64--netbsd
configured with: /work/trees/toolchain/gcc/configure --target=x86_64--netbsd --disable-shared --with-gnu-ld --with-gas --enable-languages=c c++ --prefix=/work/fvdl/toolchain
>Description:
	gcc 3.1 and up generates bad code for switch jump tables when
	using the -fPIC flag. The problem is that the generated jump
	table contains 32-bit signed values for the relative offset to
	jump to. This is fine, for small mode. However, when they are
	loaded into a register for the computation of the jump destination,
	they are not sign-extended.

	This means that for negative offsets, the computed address will
	be wrong and way out of range (i.e. -10 will be added as
	0x00000000fffffff6).

	This bug may have gone unnoticed for a while, because mostly,
	the jump table resides in memory *before* the code it points
	to (.rodata comes before .text, or the jumptable resides in
	.text before the code), so the offsets will be positive.
	But, there are cases in which this is not the case (like in the
	NetBSD dynamic linker). And a compiler should not make assumptions
	about the order of sections anyway.

	small example program:


/*
 * Simple testcode to tickle a codegen bug with -fPIC and switch
 * statements on x86_64.
 *
 * The code inside the cases is irrelevant, it's just there as
 * filler to avoid stuff being optimised away.
 */

extern int a, b, c, d, e;

int
foo(void)
{
	switch (a) {
	case 1:
		a = b * c;
		a--;
		break;
	case 2:
		a = d * e;
		c = d / b;
		break;
	case 3:
		a = c / d;
		b = d * d;
		c--;
		break;
	case 4:
		a = d * d;
		b = c * c;
		break;
	case 5:
		c++;
		d++;
		c = b * b;
		break;
	}
	return 0;
}

======
	gcc 3.1 and up generates (with -O -fPIC)
======

        .file   "c.c"
        .text
        .align 2
.globl foo
        .type   foo,@function
foo:
.LFB1:
        pushq   %rbx
.LCFI0:
        movq    a@GOTPCREL(%rip), %rax
        cmpl    $5, (%rax)
        ja      .L2
        mov     (%rax), %eax
        leaq    .L8(%rip), %rdx
        mov     (%rdx,%rax,4), %eax		<==== no sign-extension
        addq    %rdx, %rax			<====
        jmp     *%rax
        .section        .rodata
        .align 4
        .align 4
.L8:
        .long   .L2-.L8
        .long   .L3-.L8
        .long   .L4-.L8
        .long   .L5-.L8
        .long   .L6-.L8
        .long   .L7-.L8
        .text
.L3:
[... rest of code deleted]
======
	An older gcc (gcc version 3.1 20010430 (experimental))
	generates (with -O -fPIC) correct code:
======
        .file   "c.c"
        .text
        .align 4
.globl foo
        .type   foo,@function
foo:
.LFB1:
        pushq   %rbx
.LCFI0:
        movq    a@GOTPCREL(%RIP), %rax
        movl    (%rax), %eax
        decl    %eax
        cmpl    $4, %eax
        ja      .L2
        mov     %eax, %eax
        leaq    .L8(%rip), %rdx
        movslq  (%rdx,%rax,4),%rax	<=== correct sign extension
        leaq    (%rax,%rdx), %rax
        jmp     *%rax
        .section        .rodata
        .align 16
        .align 4
.L8:
        .long .L3-.+4+(.-.L8)
        .long .L4-.+4+(.-.L8)
        .long .L5-.+4+(.-.L8)
        .long .L6-.+4+(.-.L8)
        .long .L7-.+4+(.-.L8)
        .text
        .align 4
.L3:
[... rest of output deleted]

>How-To-Repeat:
	Compile with a largish switch with -fPIC, and link the
	resulting program in such a way that the jumptable comes
	after the code it points to.
>Fix:
	Unknown to me, should be simple.
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-10-01 12:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-12 11:06 target/6912: x86_64 switch code generation problem with -fPIC Frank van der Linden
  -- strict thread matches above, loose matches on Subject: below --
2002-10-01  5:35 hubicka
2002-06-05  8:56 Frank van der Linden
2002-06-02 17:46 fvdl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).