I am trying to write a program to read a binary file from memory execute it and exit but the OS doesn't seem to let me execute it from memory, the entire point of this exercise is to load a binary file with no header into memory.
This is my code for the binary file:
push eax
mov eax,3
mov edi,eax
sub eax,edi
pop eax
leave
ret
And my loader is as follows:
int main(int argc, char **argv){
void (*ptr)(void);
FILE *fo = fopen(argv[1],"r");
int l = fseek(fo,0,SEEK_END);
fread((void*)ptr,l*sizeof(char),1,fo);
ptr();
return 0;
}
I know I am probably going about this the wrong way.
fread()writes to a bogus location. That's undefined behavior.a binary file with no header- the text section of an EXE, a DLL, something else? Assuming you were using an assembler/linker that could generate a binary file containing just the assembled instructions you show here, you should indeed be able to allocate an appropriate memory buffer, read the bytes into it, and execute them by jumping to the start of the buffer, but not exactly like you show it here. Obviously the assembly code as it stands doesn't serve any useful purpose either, but I guess it's just a proof of concept.