From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4E2A23858429; Thu, 11 Aug 2022 18:44:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E2A23858429 From: "michael.hale48 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/106587] New: RISCV invalid jump address when compiled with -fcall-saved-reg and TCO Date: Thu, 11 Aug 2022 18:44:18 +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: 11.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: michael.hale48 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: Thu, 11 Aug 2022 18:44:18 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106587 Bug ID: 106587 Summary: RISCV invalid jump address when compiled with -fcall-saved-reg and TCO Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: michael.hale48 at gmail dot com Target Milestone: --- Description of bug: When compiling with -fcall-saved on temporary registers, program will perform a jump to a register after it has been restored from t= he stack. GCC version: gcc version 11.1.0 (GCC) System type: 5.4.0-81-generic #91~18.04.1-Ubuntu SMP x86_64 GCC configuration:=20 ``` /home/mhale/github/riscv-gnu-toolchain/riscv-gcc/configure --target=3Driscv32-unknown-elf --prefix=3D/opt/riscv --disable-shared --disable-threads --enable-languages=3Dc,c++ --with-system-zlib --enable-tls --with-newlib --with-sysroot=3D/opt/riscv/riscv32-unknown-elf --with-native-system-header-dir=3D/include --disable-libmudflap --disable-l= ibssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=3D.././riscv-gcc --disable-multilib --with-abi=3Dilp32d --with-arch=3Drv32imfdc --with-tune=3Drocket 'CFLAGS_FOR_TARGET=3D-Os -mcmodel=3Dmedlow' 'CXXFLAGS_FOR_TARGET=3D-Os=20= =20 -mcmodel=3Dmedlow'=20 ``` GCC command: ``` riscv32-unknown-elf-gcc -nostartfiles -Wall -Wextra -fcall-saved-t{1..6} clobber.c -save-temps -v -o clobber ``` Compiler output: ``` Using built-in specs. COLLECT_GCC=3D/opt/riscv/bin/riscv32-unknown-elf-gcc COLLECT_LTO_WRAPPER=3D/opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/lto= -wrapper Target: riscv32-unknown-elf Configured with: /home/mhale/github/riscv-gnu-toolchain/riscv-gcc/configure --target=3Driscv32-unknown-elf --prefix=3D/opt/riscv --disable-shared --disable-threads --enable-languages=3Dc,c++ --with-system-zlib --enable-tls --with-newlib --with-sysroot=3D/opt/riscv/riscv32-unknown-elf --with-native-system-header-dir=3D/include --disable-libmudflap --disable-l= ibssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=3D.././riscv-gcc --disable-multilib --with-abi=3Dilp32d --with-arch=3Drv32imfdc --with-tune=3Drocket 'CFLAGS_FOR_TARGET=3D-Os -mcmodel=3Dmedlow' 'CXXFLAGS_FOR_TARGET=3D-Os=20= =20 -mcmodel=3Dmedlow' Thread model: single Supported LTO compression algorithms: zlib gcc version 11.1.0 (GCC)=20 COLLECT_GCC_OPTIONS=3D'-nostartfiles' '-Wall' '-Wextra' '-fcall-saved-t1' '-fcall-saved-t2' '-fcall-saved-t3' '-fcall-saved-t4' '-fcall-saved-t5' '-fcall-saved-t6' '-save-temps' '-v' '-o' 'clobber' '-mtune=3Drocket' '-march=3Drv32imfdc' '-mabi=3Dilp32d' '-march=3Drv32imfdc' /opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/cc1 -E -quiet -v clobber= .c -mtune=3Drocket -march=3Drv32imfdc -mabi=3Dilp32d -march=3Drv32imfdc -Wall = -Wextra -fcall-saved-t1 -fcall-saved-t2 -fcall-saved-t3 -fcall-saved-t4 -fcall-save= d-t5 -fcall-saved-t6 -fpch-preprocess -o clobber.i ignoring nonexistent directory "/opt/riscv/riscv32-unknown-elf/usr/local/include" ignoring duplicate directory "/opt/riscv/riscv32-unknown-elf/include" #include "..." search starts here: #include <...> search starts here: /opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/include /opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/include-fixed /opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/../../../../riscv32-unknown-e= lf/include End of search list. COLLECT_GCC_OPTIONS=3D'-nostartfiles' '-Wall' '-Wextra' '-fcall-saved-t1' '-fcall-saved-t2' '-fcall-saved-t3' '-fcall-saved-t4' '-fcall-saved-t5' '-fcall-saved-t6' '-save-temps' '-v' '-o' 'clobber' '-mtune=3Drocket' '-march=3Drv32imfdc' '-mabi=3Dilp32d' '-march=3Drv32imfdc' /opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/cc1 -fpreprocessed clobb= er.i -quiet -dumpbase clobber.c -dumpbase-ext .c -mtune=3Drocket -march=3Drv32im= fdc -mabi=3Dilp32d -march=3Drv32imfdc -Wall -Wextra -version -fcall-saved-t1 -fcall-saved-t2 -fcall-saved-t3 -fcall-saved-t4 -fcall-saved-t5 -fcall-save= d-t6 -o clobber.s GNU C17 (GCC) version 11.1.0 (riscv32-unknown-elf) compiled by GNU C version 7.5.0, GMP version 6.1.2, MPFR version 4.= 0.1, MPC version 1.1.0, isl version none GGC heuristics: --param ggc-min-expand=3D100 --param ggc-min-heapsize=3D131= 072 GNU C17 (GCC) version 11.1.0 (riscv32-unknown-elf) compiled by GNU C version 7.5.0, GMP version 6.1.2, MPFR version 4.= 0.1, MPC version 1.1.0, isl version none GGC heuristics: --param ggc-min-expand=3D100 --param ggc-min-heapsize=3D131= 072 Compiler executable checksum: aecc75e33d037bca1725d62ef4fb2c89 COLLECT_GCC_OPTIONS=3D'-nostartfiles' '-Wall' '-Wextra' '-fcall-saved-t1' '-fcall-saved-t2' '-fcall-saved-t3' '-fcall-saved-t4' '-fcall-saved-t5' '-fcall-saved-t6' '-save-temps' '-v' '-o' 'clobber' '-mtune=3Drocket' '-march=3Drv32imfdc' '-mabi=3Dilp32d' '-march=3Drv32imfdc' /opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/../../../../riscv32-unknown-e= lf/bin/as -v --traditional-format -march=3Drv32imfdc -march=3Drv32imfdc -mabi=3Dilp32= d -o clobber.o clobber.s GNU assembler version 2.37 (riscv32-unknown-elf) using BFD version (GNU Binutils) 2.37 COMPILER_PATH=3D/opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/:/opt/ris= cv/libexec/gcc/riscv32-unknown-elf/11.1.0/:/opt/riscv/libexec/gcc/riscv32-u= nknown-elf/:/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/:/opt/riscv/lib/g= cc/riscv32-unknown-elf/:/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/../..= /../../riscv32-unknown-elf/bin/ LIBRARY_PATH=3D/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/:/opt/riscv/li= b/gcc/riscv32-unknown-elf/11.1.0/../../../../riscv32-unknown-elf/lib/:/opt/= riscv/riscv32-unknown-elf/lib/ COLLECT_GCC_OPTIONS=3D'-nostartfiles' '-Wall' '-Wextra' '-fcall-saved-t1' '-fcall-saved-t2' '-fcall-saved-t3' '-fcall-saved-t4' '-fcall-saved-t5' '-fcall-saved-t6' '-save-temps' '-v' '-o' 'clobber' '-mtune=3Drocket' '-march=3Drv32imfdc' '-mabi=3Dilp32d' '-march=3Drv32imfdc' '-dumpdir' 'clob= ber.' /opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/collect2 -plugin /opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/liblto_plugin.so -plugin-opt=3D/opt/riscv/libexec/gcc/riscv32-unknown-elf/11.1.0/lto-wrapper -plugin-opt=3D-fresolution=3Dclobber.res -plugin-opt=3D-pass-through=3D-lgcc -plugin-opt=3D-pass-through=3D-lc -plugin-opt=3D-pass-through=3D-lgloss -plugin-opt=3D-pass-through=3D-lgcc --sysroot=3D/opt/riscv/riscv32-unknown-= elf -melf32lriscv -o clobber -L/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0 -L/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/../../../../riscv32-unknown= -elf/lib -L/opt/riscv/riscv32-unknown-elf/lib clobber.o -lgcc --start-group -lc -lgl= oss --end-group -lgcc COLLECT_GCC_OPTIONS=3D'-nostartfiles' '-Wall' '-Wextra' '-fcall-saved-t1' '-fcall-saved-t2' '-fcall-saved-t3' '-fcall-saved-t4' '-fcall-saved-t5' '-fcall-saved-t6' '-save-temps' '-v' '-o' 'clobber' '-mtune=3Drocket' '-march=3Drv32imfdc' '-mabi=3Dilp32d' '-march=3Drv32imfdc' '-dumpdir' 'clob= ber.' ``` Preprocessed file (clobber.i): ``` # 0 "clobber.c" # 0 "" # 0 "" # 1 "clobber.c" typedef void (*void_fptr)(void); void_fptr fptr =3D 0; int g0, g1, g2, g3, g4, g5, g6, g7; void a() {} void __attribute__((optimize("O3"))) b(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7) { g0 =3D arg0; g1 =3D arg1; g2 =3D arg2; g3 =3D arg3; g4 =3D arg4; g5 =3D arg5; g6 =3D arg6; g7 =3D arg7; if (fptr) { fptr(); } } int _start() { fptr =3D a; b(0, 1, 2, 3, 4, 5, 6, 7); return 0; } ``` Additional information: The relevant part of generated assembly is pasted b= elow with annotations: ``` b: lui t0,%hi(g0) sw a0,%lo(g0)(t0) lui a0,%hi(g1) sw a1,%lo(g1)(a0) lui a1,%hi(g2) sw a2,%lo(g2)(a1) lui a2,%hi(g3) addi sp,sp,-16 sw a3,%lo(g3)(a2) lui a3,%hi(g4) sw t1,12(sp) sw a4,%lo(g4)(a3) lui t1,%hi(fptr) lui a4,%hi(g5) sw a5,%lo(g5)(a4) lw t1,%lo(fptr)(t1) lui a5,%hi(g6) sw a6,%lo(g6)(a5) lui a5,%hi(g7) sw a7,%lo(g7)(a5) beq t1,zero,.L2 lw t1,12(sp) <-- t1 restored from stack addi sp,sp,16 jr t1 <-- jump to t1 (fptr) .L2: lw t1,12(sp) addi sp,sp,16 jr ra ``` Since no argument/return value/stack pointer/return address registers were specified with -fcall-saved-reg, it would appear that this usage complies w= ith GCC guidance when using this option. However, t1 is used as the target for = the jump even though there are other caller-saved registers available.=