Platform - Linux
I have a typical requirement, where I need to give higher priority to my function then system provided function.
Lets say I have a third party library or executable. It calls many system functions such strcpy or strlen etc. I can not modify the executable/library as I do not have access to code.
Now I have implemented my own strlen or strcpy functions. When the third party library or executable is executing it should call my functions instead of system library functions.
Is it possible. If yes, can somebody guide me how to do this.
LD_PRELOAD trial result
As per suggestion here, I tried with LD_PRELOAD. I tried to override malloc function.
#include <string.h>
void *malloc(int size)
{
void *ptr = NULL;
printf("Inside my malloc function\n");
ptr = malloc(size);
return ptr;
}
I put the above code in a c file and built mem.so file and executed below command.
export LD_PRELOAD=./mem.so
So now I have overwritten malloc function.
If execute ls or clear command I get below result, which is logical.
Inside my malloc function
Inside my malloc function
Inside my malloc function
Inside my malloc function
Inside my malloc function
It looks my overloaded malloc function is invoked recursively. Only one time call I need. How can I achieve that.