public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vluchits at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-100163-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100163

            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 specified
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, which
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 the
previous version and which is 00 00 now. When this opcode is encountered during
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.

             reply	other threads:[~2021-04-20 19:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-20 19:44 vluchits at gmail dot com [this message]
2021-04-20 20:57 ` [Bug target/100163] " pinskia at gcc dot gnu.org
2021-04-20 22:25 ` vluchits at gmail dot com
2021-04-20 23:50 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-100163-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).