Search code examples
buildshared-librariesglibc

add a function to glibc and how to change the bind value to GLOBAL?


For some reasons, I need to add a function to glibc. I tried to add a sample function to pthread.so:

  1. add a file glibc/nptl/my_add.c, which return a sum of two numbers.
  2. modify the glibc/nptl/Makefile, Add the my_add to "libpthread-routines"
  3. make glibc and I get the pthread.so, but the func's bind value is LOCAL. How to change the bind value form LOCAL to GLOBAL?
$ readelf build/nptl/pthread.so.0 -s | grep "my_add"
   326: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS my_add.c
   458: 0000000000013550     4 FUNC    LOCAL  DEFAULT   14 __my_add
   510: 0000000000013550     4 FUNC    LOCAL  DEFAULT   14 my_add@@GLIBC_2.2.5

I think that maybe I am ought to modify the gcc flags in Makefile, but I don't know how to do it. The glibc is complicated for me, but I hava to slove it.

please help me, thanks!

my_add.c

#include "pthreadP.h"
#include <shlib-compat.h>


int __my_add (int a, int b)
{
    return a+b;
}
versioned_symbol (libpthread, __my_add, my_add, GLIBC_2_1);

Solution

  • but the func's bind value is LOCAL.

    This is because GLIBC build process tightly controls the set of external symbols (so no undesirable or internal-only symbols are exported).

    You must add my_add function to nptl/Versions file.