From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10089 invoked by alias); 11 Jun 2013 09:58:33 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 10063 invoked by uid 48); 11 Jun 2013 09:58:31 -0000 From: "mikpe at it dot uu.se" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/57583] New: large switches with jump tables are horribly broken on m68k Date: Tue, 11 Jun 2013 09:58:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 4.8.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: major X-Bugzilla-Who: mikpe at it dot uu.se X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2013-06/txt/msg00538.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57583 Bug ID: 57583 Summary: large switches with jump tables are horribly broken on m68k Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: major Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: mikpe at it dot uu.se Created attachment 30288 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30288&action=edit test case and generator program Switch jump tables on m68k-linux use 16-bit PC-relative offsets. No verification is made that the offsets actually fit in 16 bits, instead they are silently truncated. As a result, a large switch may branch to a wrong address; in my case it branches into the jump table itself causing a SIGILL. There are two bugs here: 1. GCC seems to hard-code the use of 16-bit offsets in its jump tables on m68k-linux. It should have an option to use 32-bit offsets instead. 2. GAS (not part of GCC I know) truncates ".word" operands to 16 bits without warning or error when significant bits are lost. I'll file that separately at the sourceware/binutils bugzilla. The test case contains a for (;;) loop with a switch () with 64K cases 0 .. 64K-1, each case containing a function call and a break. That switch becomes the following assembly code on m68k-linux with gcc-4.8.1: .L259: move.l (%a2),-(%sp) move.l %a2,-(%sp) jsr fetch addq.l #8,%sp and.l #65535,%d0 move.w .L262(%pc,%d0.l*2),%d0 jmp %pc@(2,%d0:w) .balignw 2,0x284c .L262: .word .L260-.L262 (64K - 1 more of these with varying labels in the first operand) When run on the target the code SIGILLs: 0x80000402 in fetch () 1: x/i $pc => 0x80000402 : rts (gdb) 0x80001c0c in loop () 1: x/i $pc => 0x80001c0c : addql #8,%sp (gdb) 0x80001c0e in loop () 1: x/i $pc => 0x80001c0e : andil #65535,%d0 (gdb) 0x80001c14 in loop () 1: x/i $pc => 0x80001c14 : movew %pc@(0x80001c1c ,%d0:l:2),%d0 (gdb) 0x80001c18 in loop () 1: x/i $pc => 0x80001c18 : jmp %pc@(0x80001c1c ,%d0:w) (gdb) print $d0 $3 = 4 *** THIS "4" SHOWS THAT THE JUMP TABLE ENTRY HAS BEEN TRUNCATED (gdb) stepi 0x80001c20 in loop () 1: x/i $pc => 0x80001c20 : .short 0xfff2 (gdb) stepi Program received signal SIGILL, Illegal instruction. I'm attaching the test case (bug.c) and the program used to generate it (genbug.c). Classifying as a target bug since the code works on x86_64 and powerpc64.