4

In my porject, I need to modify some functions of the glibc source code. I only need to modify part of the pthread. For example, I modified multithreaded related functions such as pthread_create.c or pthread_mutex_lock.c in the source code. Then, when my concrete program is running,I want to specify it to use the modified functions when it needs to using these functions ,and it won't affect other functions.Also,I do not want to specify an entire version of glibc when program is running. I need to ask for your help is there any good solution for this problem? Thanks!! Ding

SJ.Ding
  • 41
  • 2
  • 6
    why not simply use another name instead of replacing standard functions? – Aganju Sep 04 '16 at 14:09
  • Do you mean that you want to replace all instances of certain glibc functions and keep the implementations of others? In that case, you may have success using LD_PRELOAD to load symbols from a SO that contains the replaced functions. – Robert Prévost Sep 04 '16 at 14:34
  • Thanks for your help. Actually, I need to acquire a specific parameter inside the pthread function,such as id of thread assigned by the system.So I inserted a routine inside the function and recompiled the modified glibc ,then used an instrumentation tool to instrument my routine for getting the parameter.As you suggested , I want to rewrite the pthread function,but I was stopped by the complex relationship calls inside the glibc source code.So I thried to recompile the whole glibc and preload the libpthread.so.But it can't work if the system glibc version is different with mine.I am confused. – SJ.Ding Sep 05 '16 at 08:20

1 Answers1

1

This is a job for a shared library interposer. Here is an excellent article.

If a function is in a shared library, the runtime linker can be instructed to call another 'interposed' function instead. The interposer can totally replace the functionality or it can augment it. A great example is the malloc family of functions, a memory leak detector and heap reporting tool can be based on a set of interposers between the user program and the system calls.

Interposers only work for shared (.so) libraries. Static (.a) libraries directly link into the executable and the calls cannot be easily intercepted.

All major flavors of Linux support interposes for the LD_PRELOAD functionality.

Here is an example interposer for pthread_create.

Community
  • 1
  • 1
Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23
  • Thanks for your help.Actually,I need to acquire a specific parameter inside the pthread function,such as id of thread assigned by the system.So I inserted a routine inside the function and recompiled the modified glibc,then used an instrumentation tool to instrument my routine for getting the parameter.As you suggested,I want to rewrite the pthread function,but I was stopped by the complex relationship calls inside the glibc source code.So I thried to recompile the whole glibc and only preload the libpthread.so.But it can't work if the system glibc version is different with mine.I am confused. – SJ.Ding Sep 05 '16 at 08:13
  • Leave glibc as is. Take the implementation of the pthread_create and copy it to your interposer. In the interposer version, add your custom code. If you function has a different signature, just make a new function e.g. my_pthread_create and link as normal without the interposer. – Matthew Fisher Sep 05 '16 at 14:05