0

I have a main binary and an app binary. Main binary is compiled with FreeRTOS and has access to HAL layer and thus uart. App binary is loaded at runtime. Now from App binary I need to call a uart_print function of main binary to log the message from uart. Apart from this also I need to call other function of main binary from app binary. I searched on web and found How to write dynamic loader for bare-metal arm-application which suggest implementing jump tables. I have the following implementation: jumptbl.h

typedef struct _MyAPI
{
  void    (*jumptbl_msg)(const char *msg);
} MyAPI;

In main binary I have instantiate the structure:

void PrintMsg(const char* msg)
{
        HAL_UART_Transmit(&huart3, (uint8_t*)'\n', 1,10);
}

__attribute__ ((section (".jumptbl"))) MyAPI main_API =
{
  &PrintMsg,
};

In linker script I create a section to be placed at address :0x20001F00

 .jumptbl_block 0x2001F000:
  {
     KEEP(*(.jumptbl))
  } > RAM 

And then from app binary I call the PrintMsg function.

MyAPI *pAPI = (MyAPI*)(0x2001F000);
pAPI->jumptbl_msg("hello world");

But my program hardfaults when the jump function is called.

Also, I tried another approach. I got the address of PrintMsg using arm-none-eabi-nm and directly calling it, but again the program hard faulted.

typedef void (*t_funcPtr)(const char *);
t_funcPtr MyFunc = (t_funcPtr)0x08001af4;
MyFunc("hello world");

Please can you suggest how can I call function of one binary in section sec_x loaded at address x from another binary.

  • 1
    arm mode or thumb mode? did you try t_funcPtr = MyFunc .... (0x08001af4|1)...based on that address am guessing this is an STM32? so thumb mode. – old_timer Jun 25 '21 at 12:46
  • 1
    did you verify by disassembly or other dumping of the binary or memory what the jump table looks like and likewise is it properly generating thumb function addreses (or arm function addresses?) – old_timer Jun 25 '21 at 12:49
  • 1
    you are on the right track here just make sure you are generating and using the table properly and examine the disassembly of the code doing the call as well as the assignment to the function pointer. If you have to patch it manually (this does happen, the c compiler does not always know what to do here and can get it wrong) then use OR not ADD. – old_timer Jun 25 '21 at 12:58
  • @old_timer, Thanks.. t_funcPtr = MyFunc .... (0x08001af4|1) worked. Sorry, yes, there was a gap in my understanding of thumb and ARM 32 addressing. I was not setting the lsbit. – Alan Hopes Jun 28 '21 at 12:29
  • I verified through disassebmly and the address was proper, but I was not setting the lsbit which was the problem. Now, code is jumping properly to ``` t_funcPtr MyFunc = (t_funcPtr)0x08001af4; MyFunc("hello world");``` , but if I pass arguments to the functions (message to be sent to uart), its getting corrupted. Again, I think there is some gap in my undertanding here, may be I need to setup stack and registers properly to get it to work. – Alan Hopes Jun 28 '21 at 12:33

0 Answers0