- How to find function from lib .so files?
- 2 Answers 2
- How can I look inside a Linux .so or .a object and see what functions they contain?
- 2 Answers 2
- How to view the list of c library functions?
- 3 Answers 3
- Get a list of the functions in a shared library?
- 2 Answers 2
- Identifying which Linux system library contains a function
- 6 Answers 6
How to find function from lib .so files?
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
2 Answers 2
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
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.
How can I look inside a Linux .so or .a object and see what functions they contain?
The linker can presumably do this, so is there a command-line tool to list functions in object files and tell me the names of functions and their signatures?
2 Answers 2
For a shared library, you have to use:
Without the -D , nm dumps debug symbols; -D refers to the dynamic symbols that are actually used for dynamic linking. From Ubuntu 12 session:
$ nm /lib/i386-linux-gnu/libc.so.6 nm: /lib/i386-linux-gnu/libc.so.6: no symbols $ nm -D /lib/i386-linux-gnu/libc.so.6 | tail 0011fc20 T xdr_wrapstring 001202c0 T xdrmem_create 00115540 T xdrrec_create 001157f0 T xdrrec_endofrecord 00115740 T xdrrec_eof 00115690 T xdrrec_skiprecord 00120980 T xdrstdio_create 00120c70 T xencrypt 0011d330 T xprt_register 0011d450 T xprt_unregister
On this system libc.so is stripped of debug symbols, so nm shows nothing; but of course there are symbols for the dynamic linking mechanism revealed by nm -D .
For a .a archive or .o object file, just nm . The symbols are the symbols; if these files are stripped, these objects cannot be used for linking.
Exported sumbols are indicated by a T . Required symbols that must be loaded from other shared objects have a U . Note that the symbol table does not include just functions, but exported variables as well.
Or if you only want to see exported symbols, add the —defined-only flag. eg: nm -D —defined-only /lib/libtest.so
How to view the list of c library functions?
I’m a newbie in Linux programming. I found that the way to view the list of system calls in Linux via command-line is:
But now I want to view the list of c library functions, how can I do that? Which command will help me list the c library functions? And another question, where are system calls and c library functions manual pages located? Thank you.
3 Answers 3
Which command will help me list the c library functions?
Here is one way to list all functions defined in the standard C library (GNU libc):
nm -D /lib/$(uname -m)-linux-gnu/libc-*.so | grep -vw U | grep -v "_" | cut -d " " -f3
Note that the system calls will also be included as they are actually functions that wrap the real system calls.
Where are system calls and c library functions manual pages located?
System call manual pages are under:
Library functions manual pages are under:
There is an offline documentation browser that I have found helpful, called Zeal, that is similar to Dash.
And open the files in a text editor.
To recursively search inside files for string:
grep -R "some string" /path/to/includes
Other options may include:
There is a nice package to display C function header, named ref . To know more about it see man ref . It has a good database where you can find the list of C library functions.
The functions are listed in the file /usr/share/elvis/stubs/ansistub.c . You can directly look into it to see the functions, or you can filter them as you wish.
You can use the following big line in your terminal to see the full list at once,
cat /usr/share/elvis/stubs/ansistub.c | grep "^int\|^char\|^void\|^double\|^ldiv_t\|^unsigned\|^type\|^struct\|^FILE\|^size_t\|^time_t\|^long\|^clock\|^mbstowcs\|^div_t" | awk 'NR > 12' | less
void abort(void) int abs(int num) double acos(double cosvalue) char *asctime(const struct tm *currtime) double asin(double sinvalue) void assert(int expression) int atexit(void(*func)(void)) long int atol(const char *string) .
Or you can copy the list to a file myCfunction.list using,
cat /usr/share/elvis/stubs/ansistub.c | grep "^int\|^char\|^void\|^double\|^ldiv_t\|^unsigned\|^type\|^struct\|^FILE\|^size_t\|^time_t\|^long\|^clock\|^mbstowcs\|^div_t" | awk 'NR > 12' > myCfunction.list
To use it first install ref using the command below,
sudo apt-get install elvis-tools
Get a list of the functions in a shared library?
How can I get a list of the functions defined in a shared object library, or find out if a particular function is defined in one?
2 Answers 2
There are different executable file formats on a *nix system. a.out was a common format some years ago and today its ELF on nearby all major systems.
ELF consists of a headers describing each of the files data sections.
The part you are looking for is the symbol table, where each symbol (function, variable) is mapped to its address.
Shared libraries keep their global symbols in a section called .dynsym
What you are looking for are symbols of the type function and a global binding in this section.
readelf —syms ./libfoo.so will give you a output of the symbols.
On Solaris and FreeBSD theres also elfdump available.
objdump displays also a lot of information about your object file and you can specify a section by using the -j switch.
Use nm with the -D (dynamic) switch:
$ nm -D /usr/lib/libpng.so 00000000 A PNG12_0 w _Jv_RegisterClasses w __cxa_finalize U __fprintf_chk w __gmon_start__ U __longjmp_chk U __memcpy_chk U __snprintf_chk U __stack_chk_fail U _setjmp U abort U crc32 U deflate U deflateEnd U deflateInit2_ U deflateReset U fflush U fread U free U fwrite U gmtime U inflate U inflateEnd U inflateInit_ U inflateReset U malloc U memcmp U memcpy U memset 00003fd0 T png_access_version_number 00016ef0 T png_build_grayscale_palette 00004810 T png_check_sig 0001d2d0 T png_chunk_error 0001d070 T png_chunk_warning 00013390 T png_convert_from_struct_tm 00014a90 T png_convert_from_time_t 000048d0 T png_convert_to_rfc1123 000051b0 T png_create_info_struct 00013040 T png_create_read_struct 00012c20 T png_create_read_struct_2 00014a40 T png_create_write_struct 00014710 T png_create_write_struct_2 00004230 T png_data_freer 00005140 T png_destroy_info_struct 00010eb0 T png_destroy_read_struct 00013da0 T png_destroy_write_struct 0001d0f0 T png_error 0001ca10 T png_free 00004a50 T png_free_data 0001c9d0 T png_free_default .
Identifying which Linux system library contains a function
Can’t you use the ‘nm’ command for this: nm lib*.so* | grep lstat . Untested hence the comment, not answer.
lstat is not present as a symbol in libc, it seems to be called __lxstat, and that is probably resolved at link time
Unfortunately, all I get from this is a long list of «nm: libXXX.sp.N: no symbols». Odd. Maybe something is damaged in my system.
nos — __lxstat is indeed there, but it does not appear to work — it keeps returning -1. I guess it’s because it has more or different parameters. (update) Indeed — I should have checked the header file first. It’s declared in there.
For dynamic objects, such as a shared library the -D option is needed with nm to display the dynamic symbols rather than the normal symbols.
6 Answers 6
Build a simple testcase in C, compile it and run ‘ldd -r’ on it to check what libs are loaded. If you don’t get lstat() in C then you have a problem on your dev env. Or this env dates back before the age of symlinks 🙂
Good idea. In this case the problem actually was that there is really no lstat in the lib, but only __lxstat. Which could be seen in the headers, though.
This idea is not too good if there is function from unknown and not-linked-in-by-default library. There will be no binary to run ldd on before library name is added to binary linking. PS: there is also LD_DEBUG=all ./binary to see actual linking process; ld.so will show what symbols are asked and where it try to find them.
tomislav@malik:~$ cd /usr/lib tomislav@malik:/usr/lib$ grep "lstat()" * Binary file libperl.so.5.10 matches Binary file libperl.so.5.10.0 matches tomislav@malik:/usr/lib$
libperl matches because it contains the string lstat() , which is a Perl function implemented here. That has little to do with the underlying syscall.
When I cross-compile Windows applications on Linux, if I have an issue with linking I tend to use this script that I named mingw-findin. A similar script could be used for regular Linux compilation, just instead of using the mingw alternative, use regular nm and instead of looking in the cross-compile prefixed directory, look in /usr/lib. To use this script, I run
#!/bin/sh liblist=` ls /usr/x86_64-w64-mingw32/lib ` for i in $liblist do if x86_64-w64-mingw32-nm /usr/x86_64-w64-mingw32/lib/$i | grep -q $1; then echo $i x86_64-w64-mingw32-nm /usr/x86_64-w64-mingw32/lib/$i | grep $1 fi done