How to execute machine language from memory?
I wrote a program to compile a simple text program to a compiled executable... Is it possible that I can load an executable to memory an some how point a pc counter to the memory space at will?
Here is what I made that I would like to store the programs to memory for execution on demand... Kind of wanting to make a little web language like php but compile it... Just for learning.
http://spiceycurry.blogspot.com/2010/05/simple-compila开发者_Go百科ble-programming-language.html
Build it into a shared library which are designed for dynamic loading. I sure there are a lot of headers that you need and a bunch of C style calling conventions you need to obey.
I recommend you write the extension in C, try binding that, and when that works, grab all the linkage assembler from the C compiler code generation phase.
Is it a executable file in memory(like ELF or something like that)? or just executable code in memory?
If it is executable code in memory you could jmp
there if the containing memory is executable and the execution will continue from there.
If it is an executable file you need to actually read and interpret the format fields to load, link etc.. because an executable file contains static memory that needs to be initialized, dynamic libs that need to be linked, and an entry point that needs to be read from the file header.
In theory you can just load the code into memory at a certain address and issue a JMP to its entry point. If you are working in python, you will probably need to do that in an extension module written in C.
Beware that this will be rather useless unless you settle on ways to get data into/out of your little code, and specify what functions (and how) of the parent program the code is allowed to access. This means defining an ABI and calling conventions.
Also, I can't even begin to express how insecure this system will be unless you take rigorous precautions. The risks are increased manifold if you expose it to the public web.
精彩评论