I am developing an application for an embedded DSP target (that is not supported by any OS) and the development is carried out using C. I have a simple application that has initialization and a infinite loop as follows :
#include <>
main (){
initialize ;
while (1){
poll_timer_for_scheduling (); // use for timing
remote_link_to_receive_data(); // used to receive data
remote_link_to_transmit_data(); // used to transmit data
switch (sel) {
case '1':
function1();
case '2':
dummy_function(); // to be replaced by function2() later
}
}
} // end of main
//-------------------------
function1()
{
// some code
}
//--------------------------
dummy_function()
{
// NO code; just an empty function to be replaced later by function2()
}
Now, I compile and dump the executable in to the DSP target, which runs perfectly. The dummy_function() is a function that I want to replace later remotely by function2() as the system may not be accessible. This is similar to online patching of application but the application has to run in the target to perform self-patching.
So my requirement is update the executable with additional functionality (to cater to unforeseen conditions) later during the operational phase. How do I compile a c function function2() and upload the executable to the dummy_function()?
I am looking for a simplified version of dynamic linking functionality in the application. Are there some simple mechanisms to achieve this?
You could use pointers to functions to later switch the implementation in a safe and atomic way. So initially the pointers could all point at the same single no-op dummy function. During the update, you would have to load code for the new function into memory, then switch the pointer to point at the location of the function in the newly loaded code.
I guess the tricky part might be not the patching of the function, but compiling that function so that it can be loaded later on. On most desktop platforms, dynamically loading code means performing relocations and stuff like that. Not sure how your DSP works in this respect. But simply recompiling the whole project and extracting the changes from that might not be feasible.