A strange code snippet: jump to a return instruction

xmeng xmeng@cs.wisc.edu
Fri Jan 10 17:21:00 GMT 2014


Hi,

Here is a strange code snippet in gcc.bin in version 4.7.0:

0000000000402e20 <_ZL28if_exists_else_spec_functioniPPKc>:
   402e20:       31 c0                   xor    %eax,%eax
   402e22:       83 ff 02                cmp    $0x2,%edi
   402e25:       75 11                   jne    402e38
   402e27:       53                      push   %rbx
   402e28:       48 8b 3e                mov    (%rsi),%rdi
   402e2b:       48 89 f3                mov    %rsi,%rbx
   402e2e:       80 3f 2f                cmpb   $0x2f,(%rdi)
   402e31:       74 0d                   je     402e40
   402e33:       48 8b 43 08             mov    0x8(%rbx),%rax
   402e37:       5b                      pop    %rbx
   402e38:       f3 c3                   repz retq
   402e3a:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
   402e40:       be 04 00 00 00          mov    $0x4,%esi
   402e45:       e8 3e fa ff ff          callq  402888 <access@plt>
   402e4a:       85 c0                   test   %eax,%eax
   402e4c:       75 e5                   jne    402e33
   402e4e:       48 8b 03                mov    (%rbx),%rax
   402e51:       5b                      pop    %rbx
   402e52:       eb e4                   jmp    402e38
   402e54:       66 66 66 2e 0f 1f 84    data32 data32 nopw 
%cs:0x0(%rax,%rax,1)
   402e5b:       00 00 00 00 00

The last instruction of this function is a two bytes jump "jmp 402e38". 
It jumps to a two bytes return "repz retq". Why not just emit a two 
bytes return at the end of the function, instead we jump to the return? 
It smells like a code generation bug to me.

I actually find similar "jump to a return" snippets in every version 
from 4.7.0 to 4.8.2, but I don't find any such case for 4.6 or prior.

The gcc.bin is compiled with the following configurations:

Reading specs from 
/afs/cs.wisc.edu/s/gcc-4.7.0/amd64_rhel6/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.0/specs
COLLECT_GCC=/s/gcc-4.7.0/amd64_rhel6/bin/gcc.bin
COLLECT_LTO_WRAPPER=/afs/cs.wisc.edu/s/gcc-4.7.0/amd64_rhel6/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.7.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.7.0/configure 
--prefix=/s/gcc-4.7.0/amd64_rhel6 --enable-shared --enable-threads=posix 
--enable-checking=release --with-system-zlib --enable-__cxa_atexit 
--disable-libunwind-exceptions --enable-libgcj-multifile 
--enable-languages=c,c++,objc,obj-c++,fortran --disable-dssi 
--enable-plugin --with-cpu=generic
Thread model: posix
gcc version 4.7.0 (GCC)

I think you can find such code snippet in your gcc.bin by searching for 
the function "_ZL28if_exists_else_spec_functioniPPKc"

Thanks

--Xiaozhu



More information about the Gcc-bugs mailing list