我想知道我的系统上的可执行文件使用了哪些库。更具体地说,我想对使用最多的库以及使用它们的二进制文件进行排名。我该怎么做呢?
当前回答
我发现这篇文章非常有用,因为我需要调查来自第三方提供的库的依赖关系(32位执行路径vs 64位执行路径)。
我在RHEL 6发行版上基于'readelf -d'建议组合了一个Q&D递归bash脚本。
它是非常基本的,每次都会测试每个依赖项,即使之前可能已经测试过了(即非常冗长)。输出也是非常基本的。
#! /bin/bash
recurse ()
# Param 1 is the nuumber of spaces that the output will be prepended with
# Param 2 full path to library
{
#Use 'readelf -d' to find dependencies
dependencies=$(readelf -d ${2} | grep NEEDED | awk '{ print $5 }' | tr -d '[]')
for d in $dependencies; do
echo "${1}${d}"
nm=${d##*/}
#libstdc++ hack for the '+'-s
nm1=${nm//"+"/"\+"}
# /lib /lib64 /usr/lib and /usr/lib are searched
children=$(locate ${d} | grep -E "(^/(lib|lib64|usr/lib|usr/lib64)/${nm1})")
rc=$?
#at least locate... didn't fail
if [ ${rc} == "0" ] ; then
#we have at least one dependency
if [ ${#children[@]} -gt 0 ]; then
#check the dependeny's dependencies
for c in $children; do
recurse " ${1}" ${c}
done
else
echo "${1}no children found"
fi
else
echo "${1}locate failed for ${d}"
fi
done
}
# Q&D -- recurse needs 2 params could/should be supplied from cmdline
recurse "" !!full path to library you want to investigate!!
将输出重定向到一个文件并grep 'found'或'failed'
使用和修改,当然风险由你自己承担,如你所愿。
其他回答
我没有ldd在我的ARM工具链,所以我使用objdump:
$ (CROSS_COMPILE) objdump - p
例如:
objdump -p /usr/bin/python:
Dynamic Section:
NEEDED libpthread.so.0
NEEDED libdl.so.2
NEEDED libutil.so.1
NEEDED libssl.so.1.0.0
NEEDED libcrypto.so.1.0.0
NEEDED libz.so.1
NEEDED libm.so.6
NEEDED libc.so.6
INIT 0x0000000000416a98
FINI 0x000000000053c058
GNU_HASH 0x0000000000400298
STRTAB 0x000000000040c858
SYMTAB 0x0000000000402aa8
STRSZ 0x0000000000006cdb
SYMENT 0x0000000000000018
DEBUG 0x0000000000000000
PLTGOT 0x0000000000832fe8
PLTRELSZ 0x0000000000002688
PLTREL 0x0000000000000007
JMPREL 0x0000000000414410
RELA 0x0000000000414398
RELASZ 0x0000000000000078
RELAENT 0x0000000000000018
VERNEED 0x0000000000414258
VERNEEDNUM 0x0000000000000008
VERSYM 0x0000000000413534
使用ldd,您可以获得工具使用的库。要对一组工具的库使用情况进行排序,您可以使用类似以下命令的东西。
ldd /bin/* /usr/bin/* ... | sed -e '/^[^\t]/ d; s/^\t\(.* => \)\?\([^ ]*\) (.*/\2/g' | sort | uniq -c
(这里sed去掉所有不以制表符开头的行,只过滤掉实际的库。使用排序| uniq -c,您将获得每个库,并显示它出现的次数。)
您可能希望在末尾添加sort -g以按使用顺序获取库。
注意,使用上述命令可能会得到两行非库行。一种静态可执行文件(“不是动态可执行文件”),另一种没有任何库。后者是linux-gate.so的结果。1,它不是文件系统中的库,而是由内核“提供”的库。
要了解二进制文件使用的库,请使用LDD
ldd path/to/the/tool
您必须编写一个小shell脚本来处理系统范围的故障。
在ubuntu上 打印与可执行文件相关的包
ldd executable_name|awk '{print $3}'|xargs dpkg -S |awk -F ":" '{print $1}'
在UNIX系统上,假设二进制(可执行)名称为test。然后我们使用下面的命令列出测试中使用的库
ldd test