P
polas
Hi everyone - I have a question. I am just playing around with C (I
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file. However, I can not get it
working and my efforts have resulted in a Seg Fault.
Below is the code I have got
#include "stdio.h"
void (*p) ();
int main()
{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */
void *x=malloc(8000);
int j=fread(x, 1, 8000,reader);
printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;
printf("%d\n",x);
p=x;
printf("%d\n",p);
p();
}
Where sample is a tested small executable file (compiled from c, just
to display a message.) It seems that the file is being read ok (as it
reports reading the correct number of bytes.)
I would appreciate any help on this - it seems to me that I need some
sort of equivalent jump instruction as in assembly, (instead of
function pointers) but I can not find one.
Thanks,
Nick
realise there are better ways to do what I want, but I would like to
do it this way to increase my understanding of C) and would like to
read an executable file in to a portion of memory and then pass
execution to this and execute the file. However, I can not get it
working and my efforts have resulted in a Seg Fault.
Below is the code I have got
#include "stdio.h"
void (*p) ();
int main()
{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */
void *x=malloc(8000);
int j=fread(x, 1, 8000,reader);
printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;
printf("%d\n",x);
p=x;
printf("%d\n",p);
p();
}
Where sample is a tested small executable file (compiled from c, just
to display a message.) It seems that the file is being read ok (as it
reports reading the correct number of bytes.)
I would appreciate any help on this - it seems to me that I need some
sort of equivalent jump instruction as in assembly, (instead of
function pointers) but I can not find one.
Thanks,
Nick