In the way to understanding binaries (Virtual memory layout, execution...etc), I've written a C code that declares a global string which contains bytes of an executable code, then i overwrote the return address from the main() function to that executable code using a simple trick by declaring a pointer (PTR) in main() which is a local area of memory reserved on the stack 2 WORDS far away from the return address from the main(), so all i do is assigning the address of the return address to that pointer (PTR=(int*)&PTR+2) and then overwrite the content of that address with the address of the executable code (the static string).
Now the dilemma is that, whenever i compile and i execute, i receive a segmentation fault.
The executable code does no memory input/output (its just a bunch of NOPs).
Using GDB i made sure that the process works perfectly: the return address is changed to be the string's address but the return never happens.
All i know is that the executable code is mapped to pages in virtual memory thar are marked RW (.data & .bss segments) so maybe there's no way to do such a code execution unless code is injected in an executable area of memory (Pages that are marked RE). That's my theory about the subject, i invite you to give more details.
char code[]="\x90\x90\x90\x90\x90\x90\x90\x90"; //a static string contains executable code
int main()
{
int *return_address; //Pointer to the return address - uninitialized
return_address = (int *)&return_address + 2; //Initializing the return address - according to stack layout
(*return_address) = (int)code; //Overwriting the return address with the code's address
}