I have a precompiled library. It contains some function, let's consider functions A() and B(). B() calls A() inside this library. I would like to link my own code with this library, replacing all calls of B() with my own C() function that will itself call B().
So, instead of A -> B I need to get A -> C -> B, where C is my own function.
In other words, I want to do (semantically): _original_B = B B = C C() { _original_B() }
It is also ok to just replace B by C without calling original B.
Is it possible using segments manipulations or something else?
I am affraid first variant is not possible, since there can't be two functions (or any other symbol, as a matter of fact) with the same name. The other one (using new function B, but in another module) won't work too. As both functions (A and B) are defined in the same module,both are actually resolved so linker won't search external libraries for possible definitions elsewhere and you'll probably get double-defined symbol error at best.
I fact I found that it is possible. Here is the solution.
I forced Keil to place function B that I wanted to substitute at specific location. Then I forced new function C to be placed at the same location. It generated a warning about overlay, but finally C was placed instead of B, B was deleted and all references from A to B pointed to C.