Home > database >  How to find which shared library exported which imported symbol in my binary?
How to find which shared library exported which imported symbol in my binary?

Time:12-28

I am trying to find the shared library which imported an external symbol. Currently I can get all imported symbols by using nm or many alternatives such as using radare2. I can also get the libraries which the binary is dependent on by using ldd. However, I got stuck at this point since I cannot find an efficient way to get which external symbol in my binary is dependent on which shared library. So, for example how can I find the shared library which exports the function named foo or printf or anything in an efficient way? I provide an example:

        Output of nm -D myfile    
             w __cxa_finalize
             U foo
             w __gmon_start__
             w _ITM_deregisterTMCloneTable
             w _ITM_registerTMCloneTable
             U __libc_start_main
             U printf
             U puts

         Output of ldd
                linux-vdso.so.1 (0x00007ffd30904000)
libfoo.so => /home/user/Desktop/dynamic_link_example/libfoo.so (0x00007f1b08aaf000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1b088a1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1b08abb000)

CodePudding user response:

So, for example how can I find the shared library which exports the function named foo or printf or anything in an efficient way?

You can run your program with env LD_DEBUG=bindings ./a.out. This will produce a lot of output, which you can grep for foo and printf.

Note that the answer to "which external symbol in my binary is dependent on which shared library" is "whichever library defines this symbol first".

So if today your binary depends on lifoo.so for foo and on libc.so.6 for printf, nothing stops you from running with a different libfoo.so tomorrow, and that different version of libfoo.so may define different symbols. If the new version of libfoo.so defines printf, that would cause the answer to your question for symbol printf to change from libc.so.6 to libfoo.so.

  • Related