Inserting nops

schaecsn schaecsn@gmx.net
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
jpeg_mem_term:                                
	nop
	pushl	%ebp
	nop
	movl	%esp, %ebp
	nop
	popl	%ebp
	nop
	ret
	.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.
 
http://sourceware.org/binutils/docs-2.18/as/i386_002dJumps.html

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

and

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?

 Stefan


-- 

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