Last time I tried this, I ran into a road block: determining the number of bytes in the function. The task would be to use the address of the function, copy the bytes into memory (provided the code is compiled as Position Independent Code, PIC).
A more platform independent method is to review your compiler documentation to see if there is a #pragma
, compiler option, or keyword that allows you to specify the function's address or segment to load at during load time.
Also, search the Embedded Systems groups, as this is a popular technique: Load code that programs a Flash Memory into RAM, execute the function in RAM, then reset the system.
Hope that helps.
Edit:
A suggestion: create a data or code segment using either an assembly language file or instructions to the linker (in the build script). Put your function into a separate code file. Tell the compiler and linker to compile this function into the new code segment. There may be compiler specific statements to get the starting address and size of a segment. Also, the OS may be able to load a segment at a given address for you.
Also look into DLLs or Shared Libraries which can be loaded during run-time, with the help of the OS.