From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9277F3857424; Fri, 16 Jul 2021 09:48:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9277F3857424 From: "rin at NetBSD dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/101469] New: wrong code with "-O2 -fPIE" for SH Date: Fri, 16 Jul 2021 09:48:24 +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: 10.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: rin at NetBSD dot org 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 cc target_milestone cf_gcctarget 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: Fri, 16 Jul 2021 09:48:24 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101469 Bug ID: 101469 Summary: wrong code with "-O2 -fPIE" for SH Product: gcc Version: 10.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: rin at NetBSD dot org CC: rin at NetBSD dot org Target Milestone: --- Target: shle--netbsdelf This Bug is for GCC 10.3 for shle: ---- $ shle--netbsdelf-gcc -v Using built-in specs. COLLECT_GCC=3D/build/gcc10/tools/bin/shle--netbsdelf-gcc COLLECT_LTO_WRAPPER=3D/build/gcc10/tools/libexec/gcc/shle--netbsdelf/10.3.0= /lto-wrapper Target: shle--netbsdelf Configured with: /usr/src/tools/gcc/../../external/gpl3/gcc/dist/configure --target=3Dshle--netbsdelf --enable-long-long --enable-threads --with-bugurl=3Dhttp://www.NetBSD.org/support/send-pr.html --with-pkgversion=3D'NetBSD nb1 20210411' --with-system-zlib --without-isl --enable-__cxa_atexit --enable-libstdcxx-time=3Drt --enable-libstdcxx-threa= ds --with-diagnostics-color=3Dauto-if-env --with-default-libstdcxx-abi=3Dnew --with-sysroot=3D/build/gcc10/dest/landisk --with-mpc=3D/build/gcc10/tools --with-mpfr=3D/build/gcc10/tools --with-gmp=3D/build/gcc10/tools --disable-= nls --disable-multilib --program-transform-name=3D's,^,shle--netbsdelf-,' --enable-languages=3D'c c++ objc' --prefix=3D/build/gcc10/tools Thread model: posix Supported LTO compression algorithms: zlib gcc version 10.3.0 (NetBSD nb1 20210411) ---- GCC miscompile this code with "-O2 -fPIE": ---- typedef struct { int pad[16]; int i; int *p; } struct_t; struct_t *sp; void *ptr(void); void func(void) { sp =3D ptr(); sp->p =3D &sp->i; } ---- The following is objdump with comments: ---- 00000000 : 0: mov.l r12,@-r15 2: mova 24 ,r0 4: mov.l 24 ,r12 6: sts.l pr,@-r15 8: add r0,r12 ! r12 =3D .got a: mov.l 28 ,r1 c: bsrf r1 ! r0 =3D ptr() e: nop 10: mov.l 2c ,r1 12: mov r0,r2 ! r2 =3D r0 14: mov r12,r0 16: mov.l r2,@(r0,r1) ! @(.got, 2c) =3D sp =3D r2 18: add #64,r2 ! r2 =3D &sp->i 1a: mov.l r2,@(4,r12) ! XXX 1c: lds.l @r15+,pr 1e: rts 20: mov.l @r15+,r12 22: nop 24: .word 0x0000 26: .word 0x0000 28: sett 2a: .word 0x0000 2c: .word 0x0000 ---- The problem is marked by XXX in comment; if this line were ---- 1a: mov.l r2,@(4,r2) ---- it would make sense, i.e., ---- @(4, &sp->i) =3D sp->p =3D r2 =3D &sp->i ---- However, unfortunately, GCC somehow mistakes r12 (=3D .got) with r2. As a result, sp->p is not correctly set, and .got gets corrupted. Note that generated code is almost same for "-Os -fPIE". And the problem occurs also for GCC 9.3.=