1

I wonder if there is a way to load a C function with its data at runtime to the text segment of a running microcontroller system. After the function is placed in the text segment and the data is stored in data segment the function pointer to the new loaded function becomes invoked in the main application. The functionality would be similar to a boot loader except from loading a entire binary before start up. I know that you can use the scatter-loading functions of the linker to place the function pointer at a fixed address or alter the alignment in the sections. Does anyone know if this is possible and if not why?

Many thanks

Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39
  • The software in hard drives do something similar. Similar to a PC, there's a boot rom, that reads in the main code at startup, but there's also a set of "overlays", a group of functions coded to run at a specific address, that are dynamically loaded for non mainline type oarations, such as diagnostic. This concept dates back to the 1960's and small computers, like the IBM 1130, which used overlays for library functions, loading the functions off the disk at part of a dynamic library function call. – rcgldr Mar 19 '15 at 14:57
  • For which target? All the ARM Cortex chips and 8051 series make no programmatic distinction between RAM and FLASH, so you can easily create a function in RAM at run time and call it. AVR and PIC cannot execute out of RAM so you would have to re-write the FLASH in the same way a boot loader does. – Jon Mar 19 '15 at 17:34
  • Thank you for your answers. I plan to implement this functionality on an ARM controller. How do I get the symbol tables of the two executable matched that the addresses of the dynamically loaded function are still correct. The only way I see is to link the dynamically loaded function with the function loading program. But then I have to load only parts of the symbol table and text segment containing only the information of the function. – Jonny Schubert Mar 20 '15 at 08:13
  • My plan became more concrete. I plan to reserve a certain amount of flash in my loading application at a fixed address. Next to this I reserve a fixed block in RAM for non-stack data. Based on this data I write a linker script telling the compiler for the dynamically loaded function to place the code at the flash memory block and the data at the RAM Block. Of cause I need to prove the size condition. I’m still wondering if it’s possible to compile and link only a function without main? – Jonny Schubert Mar 20 '15 at 10:36
  • You can most certainly compile a function by itself. I would recommend you just write the function without side effects - so it accepts a pointer to any data it needs to manipulate. If you do this you'll more than likely be able to reposition it anywhere in memory on an ARM target, though you'll need to ask someone more familiar with ARM assembly language, or confirm it is only using relative jumps yourself. Otherwise positioning it in the correct location in the linker script should work as well. – Jon Mar 20 '15 at 12:23
  • If you really want to match up the symbol tables then you might have better luck rephrasing your question to be about linker scripts on whatever compiler system you are using. – Jon Mar 20 '15 at 12:29
  • Thank you for your answer Jon. At the moment the project is not a concrete task so that's why I can't tell you the exact compiler. My question was more a prove of conecpt. You all helped me a lot already. – Jonny Schubert Mar 20 '15 at 14:42

1 Answers1

1

Technically it is possible. Keep in mind that any solution will be non-standard, not portable, and very tricky.

Many controllers may execute code only from a read-only memory, which makes the whole concept of dynamic loading problematic:

  • you'd need to erase a whole page first, making sure that no other parts of the application accesses this page during load;

  • you'd need to flush the instruction cache (again, many controllers rely on instruction cache being always valid).

In any case you'd need to ensure that the function being replaced has no stack frame associated with it. Very hard to enforce in a multithreaded system.

Any particular architecture may offer more traps.

user58697
  • 7,808
  • 1
  • 14
  • 28