Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC generates useless instructions?

BOOL32 doStuff() {
    return TRUE;
}

gcc 2.95 for vxworks 5.x, compiling the above code with -O0 for 32-bit x86 generated following code:

          doStuff:
0e9de190:   push    %ebp
0e9de191:   mov     %esp,%ebp
 308          return TRUE;
0e9de193:   mov     $0x1,%eax
0e9de198:   jmp     0xe9de1a0 <doStuff+16>
 312      {
0e9de19a:   lea     0x0(%esi),%esi
// The JMP jumps here
0e9de1a0:   mov     %ebp,%esp
0e9de1a2:   pop     %ebp
0e9de1a3:   ret

Everything looks normal until the JMP and LEA instruction. What are they for?


My guess is that it is some kind of alignment, but I am not sure about this.

I would have done something like this:

          doStuff:
0e9de190:   push    %ebp
0e9de191:   mov     %esp,%ebp
 308          return TRUE;
0e9de193:   mov     $0x1,%eax
0e9de1XX:   mov     %ebp,%esp
0e9de1XX:   pop     %ebp
0e9de1XX:   ret
0e9de1XX:   fill with lea 0x0, %esi
like image 571
maxbit89 Avatar asked Aug 31 '16 11:08

maxbit89


1 Answers

lea 0x0(%esi),%esi is a long NOP, and the jmp is jumping over it. You probably have an ancient version of binutils (containing as) to go with your ancient gcc version.

So when gcc put a .p2align to align a label in the middle of the function that isn't otherwise a branch target (for some bizarre reason, but it's -O0 so it's not even supposed to be good code), the assembler made a long NOP and jumped over it.

Normally you'd only jump over a block of NOPs if there were a lot of them, especially if they were all single-byte NOPs. This is really dumb code, so stop using such crusty tools. You could try upgrading your assembler (but still using gcc2.95 if you need to). Or check that it doesn't happen at -O2 or -O3, in which case it doesn't matter.

If you have to keep using gcc2.95 for some reason, then just be aware that it's ancient, and this is part of the tradeoff you're making to keep using whatever it is that's forcing you to use it.

like image 152
Peter Cordes Avatar answered Oct 20 '22 02:10

Peter Cordes