GOP NOPs compiled

Exit from my usual VC ++ domain to the GCC world (via MINGW32). Trying to create Windows PE, which consists mainly of NOP, ala:

for(i = 0; i < 1000; i++) { asm("nop"); } 

But either I use the wrong syntax, or the compiler optimizes them because these NOPs do not survive the compilation process.

I use the -O0 flag, otherwise the defaults. Any ideas on how I can get the compiler to leave NOPs intact?

+3
source share
2 answers

Do you expect him to deploy a loop of 1000 nop s? I did a quick test with gcc and I don't see (one) nop disappears:

  xorl %eax, %eax .p2align 4,,7 .L2: #APP nop #NO_APP addl $1, %eax cmpl $1000, %eax jne .L2 

With gcc -S -O3 -funroll-all-loops I see that it loops 8 times (thus, 8 nop ), but I think that if you need 1000, this will be the easiest to do:

 #define NOP10() asm("nop;nop;nop;nop;nop;nop;nop;nop;nop;nop") 

And then use NOP10(); ... NOP10(); ...

+2
source

This recent question of cyclizing to 1000 without conventions has led to a clever answer using template recursion, which can actually be used to create a 1000 nop function without repeating asm("nop") at all. There are some caveats: if you donโ€™t get a compiler for the built-in function, you end up with a 1000-recursive stack of individual nop functions. In addition, gcc default depth limit of the template of 500, so you must explicitly specify a higher limit (see below, although you can simply avoid exceeding nop<500>() ).

 // compile time recursion template<int N> inline void nop() { nop<N-1>(); asm("nop"); } template<> inline void nop<0>() { } void nops() { nop<1000>(); } 

Compiled with

  g++ -O2 -ftemplate-depth=1000 ctr.c 
+2
source

Source: https://habr.com/ru/post/1369622/


All Articles