https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118222
Bug ID: 118222
Summary: When adding fno-inline, the register does not follow
abi
Product: gcc
Version: 11.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ipa
Assignee: unassigned at gcc dot gnu.org
Reporter: zou116 at purdue dot edu
Target Milestone: ---
We noticed that GCC made a mistake about register allocation when compiling
with "O2 -fno-inline". Specifically, by disassembling the binary compiled by
GCC, we have:
0x00000000000009d7 <+39>: mov %edx,%edi
0x00000000000009d9 <+41>: call 0x30 <is_ENOTSUP>
0x00000000000009de <+46>: cmp $0x26,%edx
Where is_ENOTSUP is an easy function:
(gdb) disassemble is_ENOTSUP
Dump of assembler code for function is_ENOTSUP:
0x0000000000000030 <+0>: cmp $0x5f,%edi
0x0000000000000033 <+3>: sete %al
0x0000000000000036 <+6>: ret
End of assembler dump.
Note that according to the abi rule of x86_AMD, edx should not be used, since
the rdx is not preserved across function calls.
I also use clang with the same optimization flag, and I have:
0x0000000000004dee <+30>: mov (%rax),%ebx
0x0000000000004df0 <+32>: mov %ebx,%edi
0x0000000000004df2 <+34>: call 0x4c70 <is_ENOTSUP>
0x0000000000004df7 <+39>: xor %edx,%edx
To reproduce this bug,
1. Download the coreutils (with commit id
e71b24cedc2ff640f94911298e53b24506f13062), and use the default "CC=gcc
./configure" to compile the Makefile.
2. Change "CFLAGS = -O2 -funit-at-a-time" to "CFLAGS = -O2 -funit-at-a-time
-fno-inline".
3. Disassemble "./src/copy.o" and the assembler code can be found in the
function "punch_hole".
The environment:
1.gcc --version:
gcc (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
2.lsb_release -a:
LSB Version:
core-11.1.0ubuntu2-noarch:printing-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
3. clang --version:
Ubuntu clang version 20.0.0
(++20241201083415+9becc4a3c966-1~exp1~20241201203535.1233)
Target: x86_64-pc-linux-gnu
The register usage for the preserved function call can be found in the 21 page
of https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf