2
votes

I have an IAR STM32 project where I am need to wrap a library function with some custom logic. I do not have the ability to recompile the library itself, so what I would like to do is create a function libfunction_shim that calls into the original libfunction. Using the --redirect linker option (--redirect libfunction=libfunction_shim), I can redirect calls to the original function to the shim, including calls inside the library itself. However, I need to call the original function from the shim.

If I add another redirect (--redirect libfunction_original=libfunction), it ends up redirecting libfunction_original to libfunction_shim, rather than the original libfunction. I've tried reordering the redirects, but it does the same thing regardless of order.

The linker log demonstrates this:

Symbol                     Redirected to          Reason        
-------------              ------                 ------
...
libfunction                libfunction_shim       command line  
libfunction_original       libfunction_shim       command line

What I would like this:

Symbol                     Redirected to          Reason        
-------------              ------                 ------
...
libfunction                libfunction_shim       command line  
libfunction_original       libfunction            command line

Is it possible to do this using the linker?

1
Why do you have to do this at linker level? In the other words, why not just call libfunction_shim at user code directly, or rename function using the preprocessor? - user694733
I need to redirect function calls internal to the library, as well. - GregoryComer

1 Answers

3
votes

This is not possible using linker redirects, you have to write your shim function in a specific way. A complete description is available in the manual (pg 225 in the 8.40.1 version) but to summarize: First you name the shim $sub$$libfunction, where libfunction is the name of the function to shadow. From inside the shim you can use $super$$libfunction to call the original version of libfunction. A minimal example is shown below.

extern void $Super$$foo(void);

void $Sub$$foo(void)
{
  $Super$$foo(); /* calls the original foo() function */
}