I'm working on a project where I need to override a function in a way that ensures my custom implementation is called instead of the original one. The original function is defined as static
in the source file, which means it has internal linkage and is only visible within that file.
To achieve this, I attempted to use the linker flag -Wl,--wrap=function_name
to wrap the function and redirect calls to __wrap_function_name
, which I have defined in code.
Despite setting everything up as described, it seems that my __wrap_function_name()
function is not being called when function_name()
is invoked. Instead, the original function_name()
is still executed.
Could the static
keyword in the original function be interfering with the wrapping process, despite the linker flag?
Are there any additional steps or considerations I should take to ensure that the __wrap_function_name()
function is called instead of the original?
Since the original function is declared static
, it has internal linkage. References to it are resolved during compilation (including assembly of the compiler-generated code), not by the linker. The linker never sees the symbol and so cannot wrap it.