How to know which library a specific function is defined in?

左心房为你撑大大i 提交于 2019-12-18 12:29:25

问题


[root@xxx memcached-1.4.5]# objdump -R memcached-debug |grep freeaddrinfo
0000000000629e10 R_X86_64_JUMP_SLOT  freeaddrinfo

...

(gdb) disas freeaddrinfo
Dump of assembler code for function freeaddrinfo:
0x00000037aa4baf10 <freeaddrinfo+0>:    push   %rbp
0x00000037aa4baf11 <freeaddrinfo+1>:    push   %rbx
0x00000037aa4baf12 <freeaddrinfo+2>:    mov    %rdi,%rbx

So I know freeaddrinfo is a dynamically linked function,but how to know which .so it's defined in?


回答1:


See this answer. The info symbol freeadrinfo is one way to find out.

On Linux and Solaris you can also use ldd and LD_DEBUG=symbols. For example, if you wanted to find out where localtime in /bin/date is coming from:

LD_DEBUG=bindings ldd -r /bin/date 2>&1 |  grep localtime
     26322: binding file /bin/date [0] to /lib/libc.so.6 [0]: normal symbol `localtime' [GLIBC_2.2.5]



回答2:


For certain low version gdb, "info symbol " could not work as you want.

So please use below method:

  1. Run 'p symbol_name' to get symbol address

    (gdb) p test_fun $1 = {<text variable, no debug info>} 0x84bcc4 <test_fun>

  2. Check /proc/PID/maps to find out the module which the symbol address is in.

    # more /proc/23275/maps 007ce000-0085f000 r-xp 00000000 fd:00 3524598 /usr/lib/libtest.so

    0x84bcc4 is in [007ce000, 0085f000]




回答3:


Inside the libraries directory you could execute the following command to search for a specific symbol on all libraries:

for file in $(ls -1 *.so); do echo "-------> $file"; nm $file; done | c++filt | grep SYMBOL*

An enhanced version would be to list all libraries that are linked to the executable (through ldd) and go after each library listed searching if the symbol is defined there. Depending on your *nix you might have to tweak the cut parsing:

APP=firefox; for symbol in $(nm -D $APP | grep "U " | cut -b12-); do for library in $(ldd $APP | cut -d ' ' -f3- | cut -d' ' -f1); do for lib_symbol in $(nm -D $library | grep "T " | cut -b12-); do if [ $symbol == $lib_symbol ]; then echo "Found symbol: $symbol at [$library]"; fi ; done; done; done;


来源:https://stackoverflow.com/questions/5563354/how-to-know-which-library-a-specific-function-is-defined-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!