I understand somewhat how "int a = b+abs(c)" can be translated to simple assembly instructions and then translate that to some binary blob. But how can this be run and be interacted with dynamically in memory?
-- edit --
I know C doesn't have an eval feature. But what it's compiled to does. I mean this is what makes Java like JITs, and for that matter, code injection malware possible no? For instance the abs() function is just a pointer, which could be called following the cdecl protocol. New functions should be able to be exposed through passing cdecl function pointers. What I don't understand is how this new code can be injected at runtime.
I'm asking this more of as a longtime academic curiosity, then to most efficiently solve an actual problem.
-- example --
Say I have a piece of embedded python code which is called from a native program a lot, and which also calls a native binding notify():
def add(a, b):
notify()
return a+b
For this to be a point in doing, the function should probably contain quite a bit more code (and way more usefull), but bear with me. A profiler (or hints from the c-bindings) has also identified that all calls are with with integers both with all parameters and return value. This matches:
int add(int a, int b) {
notify();
return a + b;
}
Which could be compiled into an x86 cdecl something similar to this:
:_add
push ebp ;setting up scope
mov ebp, esp
call _notify ;or more likely by a pointer reference
mov eax, [ebp + 8]
mov edx, [ebp + 12]
add eax, edx
pop ebp
ret
Then finally assembled into a binary string. Of course one would have to implement a basic compiler for each platform to even get this far. But that problem aside, say I now have a char pointer to this valid binary x86 code. Is it somehow possible to extract a cdecl function pointer useable for the native program from this in any way?
Sorry for the unclear intent about my question