penny
penny

Reputation: 67

How to create alias for particular (new/existing) function in glibc

For some reasons I wish to create alias to function

pthread_mutex_trylock(pthread_mutex_t *mutex);

from the glibc to alias named

lab_pthread_mutex_trylock(pthread_mutex_t *mutex);

I try add

weak_alias (__pthread_mutex_trylock, lab_pthread_mutex_trylock)

in the file pthread_mutex_trylock.c (editing source code of the library) and then

./configure --prefix=/home/user/glibc
make
make install

After that I compile my program like

  gcc \
  -L "/home/user/glibc/lib" \
  -I "/home/user/glibc/include" \
  -Wl,--rpath="/home/user/glibc/lib" \
  -Wl,--dynamic-linker="/home/user/glibc/lib/ld-linux-x86-64.so.2" \
  -std=c11 \
  -o main.out \
  -v \
  main.c \
  -pthread \
;
ldd ./main.out
./main.out

The ldd script show me that some functions (from default libc) are really from my build of glibc, but use lab_pthread_mutex_trylock(pthread_mutex_t *mutex) causes error.

glibc has very complicated structure and I have quite weak knowledge about build management so I feel that lot of things I should to do was missed by me. Please, help me, it's very important for me...

Error by gcc:

gcc -L "/ home / anahel / glibc-test / lib" -I "/ home / anahel / glibc-test / include" -Wl, - rpath = "/ home / anahel / glibc-test / lib" -Wl , - dynamic-linker = "/ home / anahel / glibc-test / lib / ld-linux-x86-64.so.2" -std = c11 -o main.out main.c -pthread

/ usr / bin / ld: /tmp/ccivqLEz.o: in the "main" function:
main.c :(. text + 0x1b): undefined reference to "lab_pthread_mutex_trylock"
collect2: error: ld returned 1 exit status

The steps I did in glibc sources:

1) If file glibc-2.31/nptl/pthread_mutex_trylock.c I added

weak_alias (__pthread_mutex_trylock, lab_pthread_mutex_trylock)

2) In file glibc-2.31/sysdeps/nptl/pthread.h I added

extern int lab_pthread_mutex_trylock (pthread_mutex_t *__mutex)
 __THROWNL __nonnull ((1));

right after

/* Try locking a mutex.  */
extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)
     __THROWNL __nonnull ((1));

Upvotes: 1

Views: 333

Answers (1)

Employed Russian
Employed Russian

Reputation: 213526

This error:

undefined reference to "lab_pthread_mutex_trylock"

means that the lab_pthread_mutex_trylock is not exported from /home/user/glibc/lib/libpthread.so.0. You can confirm this with:

nm -D /home/user/glibc/lib/libpthread.so.0 | grep lab_pthread_mutex_trylock

(expect no output if my guess is correct).

The likely reason it's not exported: GLIBC build process tightly controls which functions are exported, and what version they have, via a linker script (which is generated by combining several Version files).

In particular, you very likely need to add lab_pthread_mutex_trylock to the glibc-2.31/nptl/Versions file.

Upvotes: 1

Related Questions