How can I find out from which shared library/shared object the strcp function is obtained?
通常,您不能:该库可以在运行时更改.例如,如果我编译以下源代码:
int strcpy(char *a,const char *src) { abort(); }
$gcc -fPIC -shared -o foo.so foo.c
然后像这样运行你的程序:
LD_PRELOAD=./foo.so /bin/less
然后获取strcpy的库是foo.so.使用LD_PRELOAD这种方式称为库插入,在各种circumstances中都很有用.
除了LD_PRELOAD之外,还有其他方法可以将不同的库注入到进程中.
如果您没有使用任何此类机制,并且正在使用GLIBC,那么您可以要求动态加载程序为您回答该问题.这是一种方式:
LD_DEBUG=bindings ldd -r /bin/less < /dev/null |& egrep '\Wstrcpy\W'
26623: binding file /bin/bash [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `strcpy' [GLIBC_2.2.5]
26633: binding file /lib/x86_64-linux-gnu/libtinfo.so.5 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `strcpy' [GLIBC_2.2.5]
26633: binding file /bin/less [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `strcpy' [GLIBC_2.2.5]
上面你可以看到ldd调用bash而不是单独的进程,并且它们都绑定到libc.so.6这个特定的符号.
How can I programmatically find the corresponding shared object (.so file) to an (imported) function?
如果您使用的是GLIBC,请使用dladdr“询问”动态链接器.返回的“info”参数将告诉您文件名.