How to find function from lib .so files?

11,553

Solution 1

I found solution

nm -C -A lib/*.so | grep cvSetZero

It produce this kind of output:

lib/libopencv_calib3d.so:                 U cvSetZero
lib/libopencv_contrib.so:                 U cvSetZero
lib/libopencv_core.so:00000000000b2540 T cvSetZero
lib/libopencv_highgui.so:                 U cvSetZero
lib/libopencv_imgproc.so:                 U cvSetZero
lib/libopencv_legacy.so:                 U cvSetZero
lib/libopencv_ml.so:                 U cvSetZero
lib/libopencv_objdetect.so:                 U cvSetZero
lib/libopencv_video.so:                 U cvSetZero

Solution 2

You could append one last :
| c++filt

in order to demangle the symbols. Also as a generic note, gcc-nm should be used in a system compiled with LTO.

EDIT: Another way with nm is to use -D and --defined-only and after redirecting the possible errors to /dev/null, grep the exact symbol with '\bsymbol_name\b'.

$ nm -Dn -o --defined-only /lib/* /usr/lib64/* 2> /dev/null | grep '\bprintf\b'
/lib/libc-2.26.so:0000000000058ee0 T printf
/lib/libc.so.6:0000000000058ee0 T printf

This way one can search the library that defines the symbol_name and not just uses it. -D allows to search only in dynamic libraries (.so).

But is seems the ultimate way to scan for library that defines(+) or not(-) a symbol is scanelf :

$ scanelf -qRys +printf /lib64/
printf  /lib64/lib/clang/3.7.0/lib/linux/libclang_rt.asan-i386.so
printf  /lib64/lib/clang/3.7.0/lib/linux/libclang_rt.asan-x86_64.so
printf  /lib64/lib/gcc/x86_64-pc-linux-gnu/5.4.0/libasan.so.2.0.0
printf  /lib64/lib/gcc/x86_64-pc-linux-gnu/5.4.0/libtsan.so.0.0.0
printf  /lib64/lib/gcc/x86_64-pc-linux-gnu/5.4.0/32/libasan.so.2.0.0
printf  /lib64/libc-2.26.so
$ scanelf -qRys -printf /lib64/
printf  /lib64/lib/ConsoleKit/ck-collect-session-info
printf  /lib64/libnsl-2.26.so

Run scanelf with -m option on / to search the whole system, without crossing mount points.

Share:
11,553
mrgloom
Author by

mrgloom

Updated on June 11, 2022

Comments

  • mrgloom
    mrgloom almost 2 years

    I can print list of exported function of one *.so file like

    nm -C lib/libopencv_ml.so

    and then find my function like

    nm -C lib/libopencv_ml.so | grep myfunction

    but when I want to find function from all .so files how to determine which .so contain my function?

    This just print all entries of function but I need to know from which .so file it appear.

    nm -C lib/*.so | grep cvSetZero

    Seems -H option also not helped. -H, --with-filename print the file name for each match

    nm -C lib/*.so | grep -Hn cvSetZero

    Generate output like:

    (standard input):98:                 U cvSetZero
    (standard input):796:                 U cvSetZero
    (standard input):2564:00000000000b2540 T cvSetZero
    (standard input):8673:                 U cvSetZero
    (standard input):12233:                 U cvSetZero
    (standard input):15503:                 U cvSetZero
    (standard input):17460:                 U cvSetZero
    (standard input):18727:                 U cvSetZero
    (standard input):20865:                 U cvSetZero