gcc - Neither ld wrap nor LD_PRELOAD working to intercept system call -
i trying use -wl,-wrap=sendto -wl,-wrap,sendto in final g++ link command links app replace standard sendto function own.
i compile following source code gcc -c -o wrap.o wrap.c , include wrap.o in final g++ command links app (the rest of app c++ hence use of g++)
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> ssize_t __real_sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t); ssize_t __wrap_sendto ( int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen ) { printf("my wrap sendto ...\n"); return __real_sendto(sockfd, buf, len, flags, dest_addr, addrlen); }
when use sendto in own source code, wrapper in fact used ok 3rd party shared objects linked against in final g++ command use sendto still use system sendto i.e. not wrapper. how can sendto wrapper used throughout ?
i have tried ld_preload approach sendto , dlsym(rtld_next) inside did not work either.
how can figure out why 3rd party library keeps on using libc sendto directly ?
when use ldd find shared object dependencies of compiled app , objdump -t on each 1 of them grepping sendto, und (undefined) 3rd party shared objects. shared objects define is:
/lib64/libpthread.so.0 000000000000ed80 w df .text 0000000000000064 glibc_2.2.5 sendto /lib64/libc.so.6 00000000000e98d0 w df .text 0000000000000064 glibc_2.2.5 sendto
i see in glibc sendto.c on git following:
weak_alias (__libc_sendto, sendto) weak_alias (__libc_sendto, __sendto)
can of in getting solution ?
the --wrap sendto
option not define sendto
symbol in binary. instead, replaces references symbols __wrap_sendto
, leaves sendto
undefined.
in other words, executable not provide sendto
, run-time symbol resolution picks 1 glibc.
to fix need define sendto
in executable. try dlsym
once again, time without ld_preload
/shim library:
ssize_t sendto ( int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen ) { ssize_t (*libc_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t) = dlsym(rtld_next, "sendto"); printf("my wrap sendto ...\n"); return libc_sendto(sockfd, buf, len, flags, dest_addr, addrlen); }
if third-party libraries keep finding wrong sendto
after this, see 1 (not particularly likely) possibility. shared libraries linked -bsymbolic
/-bsymbolic-functions
, provide own sendto
.
also, since you've tagged question g++
, make sure symbol names don't mangled - use extern "c"
.
Comments
Post a Comment