Re: Passing execution to a memory address



On Mar 1, 2:55 pm, Nelu <spamah...@xxxxxxxxx> wrote:
polas wrote:
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"

it's:
#include <stdio.h>

also, you are using malloc a little later so you need stdlib.h
#include <stdlib.h>

void (*p) ();
int main()

preferable to use int main(void)

{
FILE *reader;
reader=fopen("sample", "r"); /* Open in TEXT mode */

you should make sure that fopen succeeded.

void *x=malloc(8000);

you should make sure that malloc succeeded.

int j=fread(x, 1, 8000,reader);

fread returns size_t. It shouldn't be a problem in your case but
it's a good thing to remember if you want values higher than 8000.



printf ("Read %d\n",j);
fclose(reader);
funcp=(fctype)x;

what is fctype?

printf("%d\n",x);

x is a pointer to void. You are not allowed to print it with %d.
If you want to print it's address use: printf("%p\n",x);

p=x;

That's not good. You can't assign a void pointer to a function
pointer. They're not the same thing.

printf("%d\n",p);

You can't print it like that. Not even sure you can print that.



p();

p is not pointing to a function so what is it supposed to do?

}

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.)

Suppose we don't see the other mistakes. If sample is executable
and you open it as a text file then you're not going to read the
file properly.



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.

Try to use the system function. Anything else is both highly
system dependent and OT here.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Thanks for the replies - the question was to "see if it could be done"
in the way I was approaching it, has been answered and I appreciate
it. funcp=(fctype)x; should not have been in there (I edited the code
as I posted it and removed a previous attempt, but missed that line
unfortunately.)

Out of interest, if there was no OS (assuming we had some way of
allocating memory etc..) how would it be done then? - would C suffice
or would the programmer need to put in some assembly code
additionally?

Nick

.



Relevant Pages

  • Re: pointer address is physical or virtual.
    ... begins its execution and exits its execution.) ... void afun ... Pointer: 0x804844e ... physicall one. ...
    (comp.os.linux.development.system)
  • Re: pointer address is physical or virtual.
    ... begins its execution and exits its execution.) ... void afun ... Pointer: 0x804844e ... physicall one. ...
    (comp.os.linux.development.system)
  • Re: __builtins__ magic behavior
    ... dictionary to create a weak form of restricted execution" not only ... globals() or with a copy of globals. ... The exec statement, when given a string source, compiles it and eventually calls PyEval_EvalCodeEx, which creates a new frame using PyFrame_New and finally executes it. ... A frame object contains a pointer to the previous frame, the code to be executed, a pointer to the current globals *and* a separate pointer to the current builtins. ...
    (comp.lang.python)
  • Re: Passing execution to a memory address
    ... execution to this and execute the file. ... You can't assign a void pointer to a function ... Try to use the system function. ...
    (comp.lang.c)
  • Re: Return value from system() on Linux
    ... >> I am trying to launch a web browser from within my code for Linux. ... If string is a null pointer, the system function determines ...
    (comp.lang.c)