From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C47DC394881B; Tue, 20 Apr 2021 19:44:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C47DC394881B From: "vluchits at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/100163] New: -falign-loops sometimes produces invalid code for SH-2 Date: Tue, 20 Apr 2021 19:44:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 9.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vluchits at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: 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 target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Apr 2021 19:44:17 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100163 Bug ID: 100163 Summary: -falign-loops sometimes produces invalid code for SH-2 Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: vluchits at gmail dot com Target Milestone: --- Hello, relocating functions to .data segment causes gcc to produce invalid code for 'nop' instruction on SH2 architecture when -O2 optimization flag is specifi= ed or -falign-loops is enabled explicitly. Here's disassembly of a C function R_SegCommand that demonstrates the code = that works: 0000050c <_R_SegCommands>: 50c: 2f 86 mov.l r8,@-r15 50e: 2f 96 mov.l r9,@-r15 510: 2f a6 mov.l r10,@-r15 512: 2f b6 mov.l r11,@-r15 514: 2f c6 mov.l r12,@-r15 516: 2f d6 mov.l r13,@-r15 518: 2f e6 mov.l r14,@-r15 51a: 91 b1 mov.w 680 <_R_SegCommands+0x174>,r1 ! 258 51c: 4f 22 sts.l pr,@-r15 51e: d2 5a mov.l 688 <_R_SegCommands+0x17c>,r2 ! 20004024 520: 3f 18 sub r1,r15 522: 00 09 nop 524: 60 21 mov.w @r2,r0 526: 64 0d extu.w r0,r4 528: 24 48 tst r4,r4 52a: 8f fb bf.s 524 <_R_SegCommands+0x18> 52c: e7 03 mov #3,r7 ... Please note the 'nop' command at offset 522, which is encoded as 00 09, whi= ch is the correct opcode for nop on SH2. Now if -O2 or -Os -align-loops are specified and R_SegCommands is relocated= to the .data segment and aligned to 16-byte boundary: void R_SegCommands(void) __attribute__((section(".data"), aligned(16))); the following code is produced: 000000d0 <_R_SegCommands>: d0: 2f 86 mov.l r8,@-r15 d2: 2f 96 mov.l r9,@-r15 d4: 2f a6 mov.l r10,@-r15 d6: 2f b6 mov.l r11,@-r15 d8: 2f c6 mov.l r12,@-r15 da: 2f d6 mov.l r13,@-r15 dc: 2f e6 mov.l r14,@-r15 de: 91 b1 mov.w 244 <_R_SegCommands+0x174>,r1 ! 258 e0: 4f 22 sts.l pr,@-r15 e2: d2 5a mov.l 24c <_R_SegCommands+0x17c>,r2 ! 20004024 e4: 3f 18 sub r1,r15 e6: 00 00 .word 0x0000 e8: 60 21 mov.w @r2,r0 ea: 64 0d extu.w r0,r4 ec: 24 48 tst r4,r4 ee: 8f fb bf.s e8 <_R_SegCommands+0x18> f0: e7 03 mov #3,r7 f2: d5 57 mov.l 250 <_R_SegCommands+0x180>,r5 ! 0 <_R_DrawTexture> f4: e3 01 mov #1,r3 ... Note the opcode at offset e6, which is a nop instruction and was 00 09 in t= he previous version and which is 00 00 now. When this opcode is encountered du= ring the program's execution, it an causes immediate crash. Replacing all occurrences of 00 00 in the binary code with 00 09 makes the makes the program run fine again.=