- Get a list of the functions in a shared library?
- 2 Answers 2
- List available functions in a shared library .so file
- Как вызвать функцию из существующей библиотеки (.so)
- How can I look inside a Linux .so or .a object and see what functions they contain?
- 2 Answers 2
- How to find function from lib .so files?
- 2 Answers 2
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 .
List available functions in a shared library .so file
Recently, we wanted to see if a certain function call was available in a shared library ( .so ).
To do so we used the command nm .
nm lists symbols from object files.
We used the command nm -D /libs/mylib.so.1 .
The parameter -D (or —dynamic ) displays the dynamic symbols rather than the normal symbols. (This is only meaningful for dynamic objects, such as certain types of shared libraries.)
We got a huge list which was similar to this
. 000000000000e6e0 T sudo_SHA512Update 000000000000eb20 T sudo_sig2str 000000000000b970 T sudo_strlcat 000000000000b910 T sudo_strlcpy 0000000000006f60 T sudo_strsplit_v1 00000000000070a0 T sudo_strtobool_v1 0000000000007330 T sudo_strtoid_v1 0000000000007570 T sudo_strtomode_v1 000000000000bb20 T sudo_strtonum 000000000000ac20 T sudo_term_cbreak_v1 000000000000adb0 T sudo_term_copy_v1 000000000021339c B sudo_term_erase 00000000002133a0 B sudo_term_kill 000000000000a920 T sudo_term_noecho_v1 000000000000aa80 T sudo_term_raw_v1 000000000000a860 T sudo_term_restore_v1 00000000000052a0 T sudo_vfatal_nodebug_v1 00000000000052d0 T sudo_vfatalx_nodebug_v1 0000000000005480 T sudo_vwarn_nodebug_v1 00000000000054b0 T sudo_vwarnx_nodebug_v1 00000000000055c0 T sudo_warn_gettext_v1 00000000000052f0 T sudo_warn_nodebug_v1 00000000000055a0 T sudo_warn_set_conversation_v1 .
We filtered out all elements that had the value T or t on the second column as those objects are symbol in the text (code) section and we found the function call we wanted there!
Как вызвать функцию из существующей библиотеки (.so)
Еcли понимать Ваш вопрос буквально, то то, что Вы написали — это стрельба из пушки по воробьям. Зачем такие сложности?!
Вам нужно просто вызвать функцию test() из библиотеки «libtemp.so» ? Ну так и скажите об этом компилятору напрямую:
#include #include void *test() // Если она определена в dlfcn.h, то - не нужно. int main(int argc, const char *argv[])
А при линковке укажите свою библиотеку:
Проблема может возникнуть только с поиском этой so-шки линкером. Если она НЕ лежит в стандартном пути поиска библиотек, то добавьте этот путь явно. Например, если so-шка расположена в той-же директории, что и main,c, то добавтьте ключик -L. ТОЧКА — обозначение текущей директории.
Спасибо вам за ответ, а компилятор все равно ругается но уже другая ошибка ` gcc -L. -o main main.c -ltemp main.c: In function ‘main’: main.c:7:5: warning: implicit declaration of function ‘test’ [-Wimplicit-function-declaration] test(«aaa»,»bbb»); ^~~~~ /usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/Scrt1.o: неопределённая ссылка на символ «__libc_start_main@@GLIBC_2.4» //lib/arm-linux-gnueabihf/libc.so.6: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status `
@Aleksey Первое. Сообщение implicit declaration of function ‘test’ означает, что в тексте программы есть вызов функции test, но нигде (ни в самой программе, ни в h-файлах) нет объявления этой функции. И ликер не знает, как её линковать. Второе. Насколько я понял по сообщениям, у вас кросс-компиляция? Тогда всё несколько сложнее. Рекомендую Вам как-то перефтормулировать вопрос и дать больше информации.
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 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.