Inserting nops

Wed Jan 23 16:50:00 GMT 2008

Hi there,

I have a gcc generated assembler file. I modified it by inserting before each asm instruction a NOP instruction (that's not my real problem, but when I can solve the NOP problem, I can solve my real problem).

Here is an example:

.globl jpeg_mem_term
        .type	jpeg_mem_term, @function
	pushl	%ebp
	movl	%esp, %ebp
	popl	%ebp
	.size	jpeg_mem_term, .-jpeg_mem_term
	.section	.note.GNU-stack,"",@progbits   

As you can see, I inserted before each instruction a NOP. That's what I did not only with this single function, but with all functions.

As a test program I use djpeg (from libjpeg package). I added NOPs to djpeg. Djpeg doesn't crash, but generates wrong pictures. I wonder why.

My first assumption:

In IA32, there are PC relative jumps and absolute jumps. First, I was assuming, that by inserting NOPs, relative targets move too far, so that they become absolute. If I understand Gas documentation correctly, there is just one mnemonic for jump, and gas decides wich one is more appropriate. This would mean that it should be no problem inserting NOPs.

Gcc does not use  the `jcxz', `jecxz', `loop', `loopz', `loope', `loopnz' and `loopne' instructions [which] only come in byte displacements.


Jump instructions are always optimized to use the smallest possible displacements. This is accomplished by using byte (8-bit) displacement jumps whenever the target is sufficiently close

Well, do you have another explaination (and workaruond) why I can't insert that many NOPs?



Scotty: Captain, we din' can reference it!
Kirk:   Analysis, Mr. Spock?
Spock:  Captain, it doesn't appear in the symbol table.
Kirk:   Then it's of external origin?
Spock:  Affirmative.
Kirk:   Mr. Sulu, go to pass two.
Sulu:   Aye aye, sir, going to pass two.

More information about the Binutils mailing list