Why are you using C++ to learn about such a low-level functionality that has nothing to do with C++? You will have much better luck working in assembly.
In-memory execution is an incredibly complex beast. Different operating systems, different hardware, different instruction sets, even different logic for the same OS. I wouldn't touch it with a 50-foot pole.
I Asked you, came here, made an account and posted the question.
The question was: how to do this in C++
Your answer was: don't use C++
For god's sake, did I ask you tell me which language I should use ?
I am already stuck in C++ and this should have done with C++ because I'm working with C++.
If you have an answer, I will be thankful .
If you don't have an answer, tell me you don't know.
It is perfectly fine for me to say that this is the wrong language to use for this task. If every language could be used to do everything we would only ever need one language.
Just because I do not know how to give you the answer you want to hear does not mean I cannot help you in any way at all. Don't be disrespectful because I didn't immediately solve your problem the way you wanted it solved.
Instead, you could have said "I am forced to use C++ for this." and that would have been sufficient.
I disagree that C++ is unsuited to do this. The only real difficulty here is decoding the ELF and loading the binary at the correct location. Standalone executables are generally not built with position-independent code, unlike libraries, so it's not easy to load two executables in the same memory space. If you recompile the embedded program as an .so you'll have a much easier time. After you decode the ELF, you'll be able to obtain a pointer to some exported function. Simply casting this pointer to an appropriate function pointer is enough to execute the function.
We do something like this all the time at my workplace to generate code at run time, only we skip generating proper executables, and JIT engines work like this, too.
I'm not clear enough on your goal to recommend any tools. Are you researching how to run any dynamic code (i.e. code loaded after the initial load that's performed by the OS), or are you specifically interested in getting code off ELF files?
Thatnks dude, have any idea of how to use it in my case, my english is terrible I can't read a very long tutorials without a 24h of headache, I'm russian so english isn't my 1st lang.
I hope you tell me how to use it in my case. Thank you very much!