I\'m trying to put an elf file into memory and then execute it, these are the steps:
1- file to put into memory
int main()
{
printf(\"Hello world! \\n
I'm trying to put an elf file into memory and then execute it,
For a fully-statically-linked executable, your steps would work (except you need to jump to _start
== entry point 0x8120
, not main
).
Then I have copied all the elf bytes into the allocations
Another possible problem is not paying attention to the .p_offset
. Your memcpy
ies should look something like this:
unsigned char buf1[0x16828]; // read 0x16828 bytes from start of file
memcpy(0x8000, buf1, 0x16828);
unsigned char buf2[0x250]; // read 0x250 bytes from offset 0x016840 into the file
memcpy(0x0001f840, buf2, 0x250);
Your problem is that you need to use the correct entry point and you need initialize the program's stack (and maybe registers) the same way the OS would. You need to use the correct entry point so that the C runtime library is initialized, otherwise your call to printf
(or puts
as the case may be) will almost certainly crash. You need to setup the stack correctly because that's where the C runtime library's initialization code will look for the program's arguments and environment (and maybe other things).
You didn't say what operating system you're using, but if your using Linux you might want to look as an answer to different question given by CesarB describing the initial state of the stack on ARM Linux.