Page 1 of 1

Executing Data Blocks

Posted: Sun Jun 06, 2004 6:55 pm
by Guest
Hey,

Just wondering if anyone can help me out. I am experimenting with some ideas and was wondering how I could go about executing a block of data as if it were code.

For example, say I compile a function, I then read that compiled code into a block of memory, then I need to execute that code, how can I do this?

thanks.

Re:Executing Data Blocks

Posted: Sun Jun 06, 2004 8:32 pm
by mystran
Basicly, if you are playing with something like C, you can simply load the data, cast the pointer to the data into a function pointer, and call that function.

The problem is, unless the code is specifically designed for loading at any address, you need to relocate it. Even position independent code might have issues though, if it needs to call other code, like library functions. Futher, compiling into truly position independent code on x86 is not nice.

If the code is pure computation without any need to talk to other world or you pass it a pointer to a struct with all the stuff it needs, and you either relocate the code, or write it so that it contains no absolute reference whatsoever, then yes, it's possible.

Edit: forgot to say that the correct way to do this is usually to create a shared library (.so/.dll) and load that with normal library routines.

Also, many dynamic languages have built-in support for loading code dynamically.