Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out which functions of a shared object are used by a program or an other library?

Tags:

How do I find out which functions of a shared object are used by a program or an other library? In this specific case, I would like to see which functions in /lib/libgcc1_s.so.1 are used by an other dynamic library. Since they are dynamically linked, objdump -d doesn't resolve the function call addresses. Is there a way short of running the program in a debugger or relinking statically? Thanks,

Luca

Edit:

nm and readelf won't do, I don't need to see which symbols are present in a shared object, but which are actually used in an other object that links to it.

like image 488
lultimouomo Avatar asked Feb 24 '11 17:02

lultimouomo


People also ask

Which command shows the shared libraries used by a given command?

The ldd command is the most straightforward one to show the shared libraries of a program.

Where do executables look for shared objects at runtime?

The default directories, normally /lib and /usr/lib. 8. For a native linker on an ELF system, if the file /etc/ld.

What is shared object in system programming?

A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.


2 Answers

nm will only work if the library wasn't stripped of its symbols. However, nm -D could show you some info:

nm -D /lib/libgcc_s.so.1 

But there's another tool which can help you: readelf

readelf - Displays information about ELF files.

And if you check the man pages, option -s: Displays the entries in symbol table section of the file, if it has one.

readelf -s /lib/libgcc_s.so.1 

EDIT:

Well, symbols that are not implemented inside the object you are inspecting with nm will appear with a U flag in front of it, but nm won't tell you which library on your system implements that symbol.

So what you are looking for can probably be achieved with a mixture of ldd and nm. ldd tells which libraries your application is linked with, and nm tells which symbols are undefined (U flag) or implemented locally (T flag).

After listing all the undefined symbols (with nm) on the target application, you should iterate through all libraries reported by ldd in search of those symbols (using nm again). If you find the symbol and it's preceded by the T flag, you found it.

By the way, I just wrote this one-liner for bash to illustrate my idea. It analyses an application named win and tries to find the libraries that implement all the symbols reported as undefined.

target="win"; for symbol in $(nm -D $target | grep "U " | cut -b12-); do for library in $(ldd $target | 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; 

Or, if your terminal supports colors:

target="win"; for symbol in $(nm -D $target | grep "U " | cut -b12-); do for library in $(ldd $target | 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 -e "Found symbol: \e[1;36m$symbol\033[0m at \e[1;34m$library\033[0m"; fi ; done; done; done; 

I'm sure someone will find a performance improvement.

Outputs:

Found symbol: XCreateColormap at [/usr/lib/libX11.so.6] Found symbol: XCreateWindow at [/usr/lib/libX11.so.6] Found symbol: XIfEvent at [/usr/lib/libX11.so.6] Found symbol: XMapWindow at [/usr/lib/libX11.so.6] Found symbol: XOpenDisplay at [/usr/lib/libX11.so.6] Found symbol: __libc_start_main at [/lib/tls/i686/cmov/libc.so.6] Found symbol: __stack_chk_fail at [/lib/tls/i686/cmov/libc.so.6] Found symbol: glClear at [/usr/lib/mesa/libGL.so.1] Found symbol: glClearColor at [/usr/lib/mesa/libGL.so.1] Found symbol: glFlush at [/usr/lib/mesa/libGL.so.1] Found symbol: glXChooseFBConfig at [/usr/lib/mesa/libGL.so.1] Found symbol: glXChooseVisual at [/usr/lib/mesa/libGL.so.1] Found symbol: glXCreateContext at [/usr/lib/mesa/libGL.so.1] Found symbol: glXCreateNewContext at [/usr/lib/mesa/libGL.so.1] Found symbol: glXCreateWindow at [/usr/lib/mesa/libGL.so.1] Found symbol: glXGetVisualFromFBConfig at [/usr/lib/mesa/libGL.so.1] Found symbol: glXMakeContextCurrent at [/usr/lib/mesa/libGL.so.1] Found symbol: glXMakeCurrent at [/usr/lib/mesa/libGL.so.1] Found symbol: glXQueryVersion at [/usr/lib/mesa/libGL.so.1] 
like image 102
karlphillip Avatar answered Oct 06 '22 00:10

karlphillip


Have you looked at ltrace? It intercepts calls to shared library functions at runtime and prints information about them as they occur.

Since this is a dynamic solution, it wouldn't print any information for a library call made in part of your program that never gets executed. But it might still be helpful depending on your needs.

like image 24
Jay Conrod Avatar answered Oct 05 '22 23:10

Jay Conrod