[Solved] Interpose C struct function pointer


If I understand correctly, you want all calls to sub (or rather _Z3subP7AStructii) to actually be calling your _sub function instead? The problem is that when the “client” calls some_astruct->sub(...) it doesn’t actually call _Z3subP7AStructii directly, and therefore your _sub function won’t be called.

In other words, the “client” doesn’t have a call to _Z3subP7AStructii, but instead it have a call to whatever some_astruct->sub is pointing to. The _Z3subP7AStructii function could even not be exported by the libA library (i.e. have internal linkage with e.g. static), and it could still be called through the structure.

A possible solution to this is not to “interpose” the sub function, but instead the AStruct_create function. In your version call the original, then replace the pointer to the sub function in the structure to your own function (possibly saving the original so you can use it).

solved Interpose C struct function pointer