3
votes

I want to implement some custom library functions in Linux. For example, I want to implement my own pthread_mutex_lock, pthread_mutex_unlock, malloc and free functions. I've read LD_PRELOAD can be used to use your own custom functions, although I haven't got into the details.

But I have one question, I also want to use the original functions within my new implementations. What would be the trick to do that, as both would have the same names?

1

1 Answers

3
votes

You could use the dlopen function to open the library you replace (or use RTLD_NEXT if it is already loaded, see comments), and then use dlsym function to find the address of the function in that library that you want to call.