linux命令查看文件链接的动态库 各种符号

最直接的就用 ldd + 可执行文件:

$ ldd /usr/bin/ls
        linux-vdso.so.1 (0x00007ffea7136000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007fe10fa71000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe10f87f000)
        libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007fe10f7ee000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe10f7e8000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe10fadd000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe10f7c5000)

可以看到可执行文件需要哪些动态库,以及在当前环境下实际连接到的路径。

 

除了ldd,还有一些其它工具:

readelf 都可以看
file 查看文件类型
strings 查看目标文件里所有可打印的字符串,或者非文本文件的 loadable, initialized data sections
nm 查看符号 (strip 去除符号)
patchelf 修改动态库rpath

 

另:动态库加载规则

ld.so: dynamic linker/loader

When resolving shared object dependencies, the dynamic linker first inspects each dependency string to see if it contains a slash (this can occur if a shared object pathname containing slashes was specified at link time). If a slash is found, then the dependency
string is interpreted as a (relative or absolute) pathname, and the shared object is loaded using that pathname.

If a shared object dependency does not contain a slash, then it is searched for in the following order:

o Using the directories specified in the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist. Use of DT_RPATH is deprecated.

o Using the environment variable LD_LIBRARY_PATH, unless the executable is being run in secure-execution mode (see below), in which case this variable is ignored.

o Using the directories specified in the DT_RUNPATH dynamic section attribute of the binary if present. Such directories are searched only to find those objects required by DT_NEEDED (direct dependencies) entries and do not apply to those objects' children, which
must themselves have their own DT_RUNPATH entries. This is unlike DT_RPATH, which is applied to searches for all children in the dependency tree.

o From the cache file /etc/ld.so.cache, which contains a compiled list of candidate shared objects previously found in the augmented library path. If, however, the binary was linked with the -z nodeflib linker option, shared objects in the default paths are skipped.
Shared objects installed in hardware capability directories (see below) are preferred to other shared objects.

o In the default path /lib, and then /usr/lib. (On some 64-bit architectures, the default paths for 64-bit shared objects are /lib64, and then /usr/lib64.) If the binary was linked with the -z nodeflib linker option, this step is skipped.

 

posted @ 2022-04-19 10:55  HarryPotterIsDead!  阅读(1293)  评论(0编辑  收藏  举报