This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem when dynamically loading code (Cortex-M4)

Hello everyone.

I want to implement dynamic loading of functions in RAM for a Cortex-M4.  It partially works but with a small hack which is not ideal.

First I create the binary data for the function by compiling my desired function:

Compilation Flags: -fPIC -msingle-pic-base -mcpu=cortex-m4 -mthumb

From the compiled file I used the scripts from this link to obtain the assembly code for my binary function.

From this data I create my binary function data for  my main application:

The actual code to execute the binary function from RAM is the following:

After debugging the code I found out that this only works if I delete the instruction blx r1 from the assembly code manually in my binFunction variable (i.e. removing 0x88, 0x47 from the array)

I noticed I get a HardFault due to r1 having a value of zero which when executes blx makes the program execute line 0x00 instead of continuing to execute the rest of the assembly instructions of the binary function:



I am not an expert on assembly nor on dynamic loading but would be grateful for any hints on how I can debug this issue.

Update:
Seems that the problem resides on code generated by the script to load the correct area where the data section is located and load it to register R9. (see line 11 from generated assembly code)

0