Executing Data Blocks

Programming, for all ages and all languages.
Post Reply
Guest

Executing Data Blocks

Post 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.
mystran

Re:Executing Data Blocks

Post 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.
Post Reply