GCC Feature Add-on

Whenever I compile C or C ++ code with optimization support, d GCC aligns functions to a 16-byte boundary (on IA-32). If a function is shorter than 16 bytes, GCC fills it with bytes that do not seem random:

19: c3 ret 1a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi 

It seems to be always 8d b6 00 00 00 00 ... or 8d 74 26 00 .

Are there any values โ€‹โ€‹in the bytes of the function fill?

+6
c ++ c gcc
source share
3 answers

The strip is created by assembler, not gcc. It just sees the .align directive (or equivalent) and does not know if a space is inside the function (for example, loop alignment) or between functions, so it should insert some kind of NOP . Modern x86 assemblers use the maximum possible NOP code codes with the intention of having as few loops as possible if padding is designed to align the loop.

Personally, I am skeptical of alignment as an optimization method. I have never seen this help, and it can do a lot of damage by increasing the overall size of the code (and using the cache). If you use the -Os optimization level, it is turned off by default, so you have nothing to worry about. Otherwise, you can turn off all alignments with the corresponding -f options.

+7
source share

The lea 0x0(%esi),%esi instruction is lea 0x0(%esi),%esi just loads the value in %esi into %esi - this is no-operation (or NOP ), which means that if it is executed, it will have no effect.

This is just one instruction, 6 byte NOP. 8d 74 26 00 is just 4 byte encoding of the same instruction.

+2
source share

Assembler first sees the .align directive. Since it does not know whether this address is inside the function body or not, it cannot output NULL 0x00 bytes and must generate NOP ( 0x90 ).

But:

 lea esi,[esi+0x0] ; does nothing, psuedocode: ESI = ESI + 0 

performed in fewer measures than

 nop nop nop nop nop nop 

If this code gets into the body of the function (for example, loop alignment), the lea version will be much faster, but still โ€œdoes nothingโ€.

+2
source share

All Articles