This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Direct assignment of code to function pointer: not working in gcc 4.x


Hi,
Thank you for your response, Ian. The system is GNU/Linux, so I did as you said, and used "-z execstack" option for the linker, and now the code works properly. It seems then that GCC 4.x is more restrictive about executing things from the stack than GCC 3.x.
Kind regards,


Aníbal.

Ian Lance Taylor wrote:
Anibal Caceres Hernando <anibal.caceres@ericsson.com> writes:

int main() {
   char *code = new char[5];
   int codeIndex = 0;
   printf("Start!!\n");

   code[codeIndex++]=0x55; //push %ebp
   code[codeIndex++]=0x89; //mov    %esp,%ebp
   code[codeIndex++]=0xe5;    //"

   code[codeIndex++]=0xc9; //LEAVE
   code[codeIndex++]=0xc3; //RET

   FunctionType invoke=(FunctionType)&code[0];
   invoke();

   printf("Finish!!\n");
   return 0;
}

If this is compiled with gcc 3.3.3 it executes without any problem:
prints "Start!!", then executes invoke(), which does nothing, as you
can see (the assembler is just a call to an empty function), and
finally it prints "Finish!!".
But the problem happens when I compile this using gcc 4.1.2 (and
the same happens with 4.2.2): prints "Start!!", and it gives a
Segmentation Violation when it starts executing invoke (I've seen
using the gdb that the SIGSEGV happens just in the first instruction,
in the "push %ebp").

This program requires an executable stack. You neglected to mention which system you are running on. If it is GNU/Linux, see the execstack(8) man page.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]