简体   繁体   English

在指定目录中查找所有依赖于指定库的可执行文件

[英]Find all executable files that depend on the specified library in the specified directory

My goal is to write a shell script that uses "objdump -p" command to find all executable files that depend on the specified library in the specified directory.我的目标是编写一个 shell 脚本,该脚本使用“objdump -p”命令在指定目录中查找依赖于指定库的所有可执行文件。 (OpenBSD). (OpenBSD)。 I try something like this:我尝试这样的事情:

find $1 -perm -111 -print0 | xargs -r0 objdump -p | grep -l "NEEDED      $2"

But this solution doesn't work because grep cannot figure out the filenames in which it found the given match.但是这个解决方案不起作用,因为 grep 无法找出它在其中找到给定匹配项的文件名。 The difficulty is to determine the names of the executable files in which grep found the specified library.难点在于确定 grep 在其中找到指定库的可执行文件的名称。 Can anyone suggest a solution using the "objdump -p" command?任何人都可以使用“objdump -p”命令提出解决方案吗?

The trick is to execute a shell script rather than a single command to be able to re-use the file name.诀窍是执行 shell 脚本而不是单个命令,以便能够重新使用文件名。

finddepend() {
# Arg 1: The directory where to find
# Arg 2: The library name
  basedir=$1
  libname=$2
  find "$basedir" \
    \( -perm -100 -o -perm -010 -o -perm -001 \) \
    \( -type f -o -type l \) \
    -exec sh -c '
# Arg 0: Is a dummy _ for this inline script
# Arg 1: The executable file path
# Arg 2: The library name
filepath=$1
libname=$2
objdump -p "$filepath" 2>/dev/null |
  if grep -qF "  NEEDED               $libname"; then
    printf %s\\n "${filepath##*/}"
  fi
' _ {} "$libname" \;
}

Example usage:用法示例:

finddepend /bin libselinux.so
mv
systemctl
tar
sed
udevadm
ls
mknod
systemd
mkdir
ss
dir
vdir
cp
systemd-hwdb
netstat

Why do you want to use objdump when you can use ldd (List Dynamic Dependencies)?当您可以使用ldd (列出动态依赖项)时,为什么要使用objdump objdump gives a complete summary, which you need to process in order only to get the information you're looking for, while ldd only gives you that information. objdump提供了一个完整的摘要,您需要对其进行处理才能获得您正在寻找的信息,而ldd只为您提供该信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Linux 上的指定目录中查找使用特定标头的所有文件? - How to find all the files that use a particular header in a specified directory on Linux? 将指定文件夹中的所有文件移到一个目录中 - Move all files in specified folder up one directory 如何为指定目录的所有现有文件和新文件设置权限 - How to set rights to all existing and new files of specified directory 将具有指定数量文件的所有目录移动到另一个目录 - Moving all directories which have a specified number of files, to another directory 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 从可执行文件中剥离所有未指定的调试信息 - Stripping all but specified debug information from executable 对于所有子目录,将目录中指定扩展名的所有文件递归转换为pdf - Convert all files of a specified extension within a directory to pdf, recursively for all sub-directories 从目录中打开 Sublime Text 中的所有文件,但明确指定的文件除外 - Open all files in Sublime Text from a directory except ones explicitly specified 如何在 Linux C 程序中显示用户指定名称为“ls dir_name”的目录中的所有文件 - How to display all files in directory of which name is specified by the user as “ls dir_name” in Linux C program “查找”文件中包含指定范围内的整数(以bash表示) - 'find' files containing an integer in a specified range (in bash)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM