Linux find function in library

How to find function in shared library from application crash log

Solution 2: The library loaded at the highest address is at . . should print function name (and file/line info if you have libc debuginfo package installed). Is there some way to select certain functions within the library as export function and make the rest functions in the library remain invisible to external call?

How to find function in shared library from application crash log

Here is part of output from kernel log after application crash:

kernel: [ 252.772000] testsnd/972: potentially unexpected fatal signal 11. . kernel: [ 252.788000] PC is at 0x296313a4 . kernel: [ 252.820000] mmap = 0x00400000->testsnd 0x29558000->ld-2.10.1.so 0x29580000->libpthread-2.10.1.so 0x295a8000->libdl-2.10.1.so 0x295bc000->librt-2.10.1.so 0x295d4000->libc-2.10.1.so 

application has crashed in libc. How to obtain function name where crash had appeared using this information ( 0x295d4000->libc-2.10.1.so and PC is at 0x296313a4 )?

You can find the offsets of each function in the .so using objdump. I typically just use the -S switch, as it will show source when you have it, and will do a basic disassembly as well.

objdump -SR /lib/i386-linux-gnu/libc-2.13.so 

If you know the base addr the library is loaded at, you can then compute the actual locations.

The library loaded at the highest address is libc-2.10.1.so at 0x295d4000 .

perl -e 'printf "0x%x\n", 0x296313a4 - 0x295d4000' 0x5d3a4 addr2line -fe /lib/libc.so.6 0x5d3a4 

. should print function name (and file/line info if you have libc debuginfo package installed). You may have to substitue /lib/libc.so.6 with actual location of 64-bit libc.so.6 on your system.

Thanks for your help guys. I am sure answers can help other programmers. But in my case problem seems to be more platform specific (sh4 ST-Linux). Therefore question shall be asked at ST website or bugzilla.

How do I view the list of functions a Linux shared library, Among other already mentioned tools you can use also readelf ().It is similar to objdump but goes more into detail. See this for the difference explanation. $ readelf -sW /lib/liblzma.so.5 |head -n10 Symbol table ‘.dynsym’ contains 128 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FUNC GLOBAL DEFAULT UND pthread_mutex_unlock@GLIBC_2.0 (4 Code sample$ nm -D /usr/lib/libopenal.so.100012ea0 T alcSetThreadContextFeedback

About shared library in Linux, is there a way to select export functions in a library?

I read the tutorial here: http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-two-dynamic-libraries/

It looks to me there is no functionality like dllexport of DLL in Windows platform.

Is there some way to select certain functions within the library as export function and make the rest functions in the library remain invisible to external call?

Читайте также:  Linux login password file

You may want to use the visibility function attribute of GCC.

See GCC visibility wikipage and read Drepper’s paper How To Write Shared Libraries

There are multiple ways to do this.

Either use the visibility function attribute as mentioned in Basiles answer or use a linker version script to do the job.

In a linker script you list all the functions that you want to export. Here is an example:

During the link step of your shared library you just pass the following extra parameters to gcc:

-Wl,--version-script=MyLinkerScript.exp 

Afterwards all symbols in the shared library will be private except for those listed in the global section of your version script.

Exporting / Importing C++ Functions from Linux, In Visual C++ for Linux Development (VCLinux) you will specify the name of the library, functions , in the project settings under Linker / Input / Library Dependencies and the path to the folder where the library can be found in under Linker / General / Additional Library Directories.

How can I call linux shared library functions in Go?

I have a .so file whose functions I would like to call in my Go code.

How do I go about doing that ? I have read the cgo and syscall package. They are close to what I want but I don’t see any place where I can call the functions in the .so file.

I want to achieve exactly what the ctypes package does in Python.

If you want to use a shared library that is known statically at compile time, you can simply use cgo. Read the documentation on how to do that exactly, but usually you specify some linker flags and a couple of commented out lines. Here is an example on how to call function bar() from libfoo.so .

package example // #cgo LDFLAGS: -lfoo // // #include import "C" func main()

You can also use cgo to access shared objects that are being loaded dynamically at runtime. You can use dlopen() , dlsym() , and dlclose() to open a shared library, retrieve the address of one of the functions inside and finally close the library. Notice that you can’t do these things in Go, you have to write some wrapper code in C that implements the neccessary logic for you.

About shared library in Linux, is there a way to select, Either use the visibility function attribute as mentioned in Basiles answer or use a linker version script to do the job. In a linker script you list all the functions that you want to export. Here is an example: File: MyLinkerScript.exp < global: myExportedFunction1; myExportedFunction2; myExportedFunction3; …

Listing library functions in GCC

Is is possible to list all the library functions available in any library/headers in GCC command line? Anything similar to program like JAVAP which is available for Java? Thanks.

You can use objdump to list symbols in a shared libraries (or executables):

$objdump -T /usr/lib/libclang.so 0000000000124150 g DF .text 00000000000000c1 Base clang_reparseTranslationUnit 000000000010fe40 g DF .text 0000000000000021 Base clang_getNullRange 0000000000135760 g DF .text 000000000000009f Base clang_getPointeeType 0000000000124290 g DF .text 0000000000000289 Base clang_parseTranslationUnit 000000000012b790 g DF .text 0000000000000935 Base clang_findReferencesInFile 0000000000110b80 g DF .text 000000000000001c Base clang_getRangeEnd 0000000000127d20 g DF .text 0000000000000022 Base clang_disposeCodeCompleteResults 0000000000135e10 g DF .text 0000000000000037 Base clang_isPODType 

000000000010f870 g DF .text 0000000000000025 Base clang_getTranslationUnitCursor 0000000000129b50 g DF .text 00000000000002c1 Base clang_getDiagnosticOption

Читайте также:  Linux man page how to

As you can see it lists the different symbols and their relative address.

View the List of Functions Exported by a Linux Shared, Now, let’s learn how to view the exported symbols of a library with the help of the library created in the above example. 3.1. Using readelf. We can use the readelf command with the -s flag to view exported symbols: $ readelf -s lib.so Symbol table ‘.dynsym’ contains 8 entries: Num: Value Size Type Bind Vis Ndx …

Источник

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.

Читайте также:  Virtual consoles in linux

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 

Источник

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

Источник

Оцените статью
Adblock
detector