Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New implementation of a library function and calling the old implementation within that

Tags:

c++

c

linux

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?

like image 734
MetallicPriest Avatar asked Oct 17 '25 19:10

MetallicPriest


1 Answers

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.

like image 165
Joan Rieu Avatar answered Oct 20 '25 09:10

Joan Rieu