首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何列出非本机二进制文件的库依赖关系?

如何列出非本机二进制文件的库依赖关系?
EN

Stack Overflow用户
提问于 2012-04-07 13:26:12
回答 5查看 77.4K关注 0票数 61

在为本机平台进行开发时,我可以使用ldd列出我构建的二进制可执行文件在启动时试图加载的所有共享库(.so文件)。但在交叉编译时,我不知道如何获得相同的信息。ldd不是一个普通的binutils实用程序,如stripar,可以与gcc一起构建以进行交叉编译,但它是一个显然只能在本机平台上运行的神秘的shell脚本。

那么,使用跨目标binutils工具,有没有办法获得外部二进制文件的动态链接依赖关系列表?

EN

回答 5

Stack Overflow用户

发布于 2012-04-07 13:49:40

您可以通过执行bash -x ldd /bin/ls来了解ldd正在做什么。ldd脚本并没有那么“神秘”。它基本上是在运行

代码语言:javascript
复制
LD_TRACE_LOADED_OBJECTS=1 /lib64/ld-linux-x86-64.so.2 /bin/ls

因此,它使用系统的动态加载器(因为ldd的结果取决于您的实际环境和系统!)。但是你可以用objdump -x /bin/ls检查可执行文件的动态部分,例如

代码语言:javascript
复制
% objdump -x /bin/ls
  /bin/ls:     file format elf64-x86-64
  /bin/ls
  architecture: i386:x86-64, flags 0x00000112:
  EXEC_P, HAS_SYMS, D_PAGED
  start address 0x00000000004046d4

  Program Header:
      PHDR off    0x0000000000000040 vaddr 0x0000000000400040 paddr 0x0000000000400040 align 2**3
           filesz 0x00000000000001c0 memsz 0x00000000000001c0 flags r-x
    INTERP off    0x0000000000000200 vaddr 0x0000000000400200 paddr 0x0000000000400200 align 2**0
           filesz 0x000000000000001c memsz 0x000000000000001c flags r--
      LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
           filesz 0x0000000000019ef4 memsz 0x0000000000019ef4 flags r-x
      LOAD off    0x000000000001a000 vaddr 0x000000000061a000 paddr 0x000000000061a000 align 2**21
           filesz 0x000000000000077c memsz 0x0000000000001500 flags rw-
   DYNAMIC off    0x000000000001a028 vaddr 0x000000000061a028 paddr 0x000000000061a028 align 2**3
           filesz 0x00000000000001d0 memsz 0x00000000000001d0 flags rw-
      NOTE off    0x000000000000021c vaddr 0x000000000040021c paddr 0x000000000040021c align 2**2
           filesz 0x0000000000000044 memsz 0x0000000000000044 flags r--
  EH_FRAME off    0x0000000000017768 vaddr 0x0000000000417768 paddr 0x0000000000417768 align 2**2
           filesz 0x00000000000006fc memsz 0x00000000000006fc flags r--
     STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**3
           filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-

  Dynamic Section:
    NEEDED               libselinux.so.1
    NEEDED               librt.so.1
    NEEDED               libacl.so.1
    NEEDED               libc.so.6
    INIT                 0x0000000000402148
    FINI                 0x00000000004125f8
    HASH                 0x0000000000400260
    GNU_HASH             0x00000000004005c0
    STRTAB               0x0000000000401100
    SYMTAB               0x0000000000400620
    STRSZ                0x00000000000004d7
    SYMENT               0x0000000000000018
    DEBUG                0x0000000000000000
    PLTGOT               0x000000000061a208
    PLTRELSZ             0x0000000000000990
    PLTREL               0x0000000000000007
    JMPREL               0x00000000004017b8
    RELA                 0x0000000000401740
    RELASZ               0x0000000000000078
    RELAENT              0x0000000000000018
    VERNEED              0x00000000004016c0
    VERNEEDNUM           0x0000000000000003
    VERSYM               0x00000000004015d8

  Version References:
    required from librt.so.1:
      0x09691a75 0x00 05 GLIBC_2.2.5
    required from libacl.so.1:
      0x05822452 0x00 06 ACL_1.2
      0x05822450 0x00 04 ACL_1.0
    required from libc.so.6:
      0x09691a75 0x00 03 GLIBC_2.2.5
      0x0d696913 0x00 02 GLIBC_2.3

同样,实际的依赖关系取决于运行二进制文件的系统(例如,因为我可能在某个地方有一个带有自己的libc.so.6LD_LIBRARY_PATH,这不是一个好主意)。

所以你需要一个objdump的交叉变种

票数 15
EN

Stack Overflow用户

发布于 2018-01-09 13:46:40

这对我很有帮助

代码语言:javascript
复制
objdump -p /path/to/program | grep NEEDED 
票数 3
EN

Stack Overflow用户

发布于 2014-03-23 15:00:57

很抱歉做了一个僵尸线程,但我正在为我的OpenWRT路由器做一些事情,并希望它检查一些依赖项,看看我的jffs2分区上是否有足够的空间只复制e2fsck。简短的答案是:nope.avi

总之,我写了一个小脚本,它使用accepted answer加上一些(可能过于冗长)的grep调用,并稍微挥手和一些独角兽的泪水(不用担心,它们是快乐的泪水!)我设法将以下脚本组合在一起,为您提供了所有依赖项。我确信有很多改进的空间,特别是RE:循环和递归,以及它一直都是空话(即索引数组)的事实,但这至少在名义上对我是有效的:

代码语言:javascript
复制
#!/bin/bash

declare -a search_path
declare -a found_deps

find_dependencies() {
    local file="$1"
    local -a deps
    local -a deps_to_process

    deps=( $( readelf -d "$file" | grep "NEEDED" | \
        grep -o -E "\[[^]]*\]" | grep -o -E "[^][]*" ) )

    local add_this_dep=true

    # always assume we've found $file and add it to the $found_deps list
    # if it's not there
    for found_dep in "${found_deps[@]}"
    do
        if [ "$found_dep" = "$(basename $file)" ]
        then
            add_this_dep=false
            break
        fi
    done

    # if $add_this_dep is true, go ahead and add to the found_deps list
    if $add_this_dep
    then
        found_deps+=("$(basename $file)")
    fi

    # for every dependency found by readelf (no path)
    for dep in "${deps[@]}"
    do
        local process_dep=true

        # if the basename of the file passed into the function is
        # this dep, skip processing altogether
        if [ "$dep" = "$(basename $file)" ]
        then
            break
        else
            # otherwise, if it's one of the 'found deps' don't process it
            for found_dep in "${found_deps[@]}"
            do
                if [ "$dep" = "$found_dep" ]
                then
                    process_dep=false
                    break
                fi
            done

            # it wasn't one of the 'found deps' so add 
            # it to the found_deps list
            if $process_dep
            then
                found_deps+=($dep)
            fi
        fi

        # if we are supposed to process this dep
        if $process_dep
        then
            local file_path=

            # check each search path for a file named $dep underneath it
            for dir in $search_path
            do
                file_path=$( find "$dir" -name "$dep" | head -n 1 )

                # if the $file_path is not empty, then we found
                # a copy of it, so break out of the loop
                if [ -n "$file_path" ]; then break; fi;
            done

            # if the $file_path is not empty, then we found a copy
            # of the file, place it the list of deps to be processed
            if [ -n "$file_path" ]
            then
                deps_to_process+=($file_path)
            fi
        fi
    done

    # now, go through all of our $deps_to_process (with path)
    # and run "find_dependencies" on them
    for dep_to_process in "${deps_to_process[@]}"
    do
        find_dependencies "$dep_to_process"
    done
}

argc=$#

if [ $argc -eq 0 ]
then
    printf '%s: prints dependencies of a (potentially) non-native elf executable, recursively\n'
    printf '\n'
    printf 'usage:\n'
    printf '\t%s <non-native elf executable> [ --supress-header ] [ <path> ... ]\n' "$(basename $0)"
    printf '\twhere\n'
    printf '\t\t<non-native elf executable> is the name of a file to find the dependencies of.\n'
    printf '\t\t[ <path> ... ] is an optional list of directories under which to search for libraries.\n'
    printf '\t\t[ --supress-header ] is an optional flag that prints nothing other than the list of files to stdout.\n'
    printf '\t\t\t(without the parameter a banner is sent to stderr)'
    printf '\n'
else
    file="$1"
    shift 1

    show_header=true

    if [ "$1" = "--supress-header" ]; then show_header=false; shift 1; fi;

    if $show_header
    then
        printf ' -------------------------------------------------------------------------------------------------------------\n' 1>&2
        printf '  ldd-nonnative: find all dependencies of a (potentially) non-native binary %s\n' "$file" 1>&2
        printf ' -------------------------------------------------------------------------------------------------------------\n' 1>&2
    fi

    search_path="$@"
    find_dependencies $file

    printf '\t%s\n' "${found_deps[@]}"
fi

# ❤ copyheart, shelleybutterfly, 2014
# love is never subject to the low; please copy and share with love :)

# contact information:
# shelleybutterfly@mojoprocode.com

# I hereby dedicate this software to the public domain in all jurisdictions
# where possible. In other jurisdictions, I license it to you under your
# choice of permissive license, as defined by the Open Source Institute (OSI),
# found at URL http://opensource.org. Should such a license be unavailable in
# your jurisdiection, you may use any copyleft open source license, again as
# defined by OSI; and if that too is unavailable, then you are licensed this
# software under the least restrictive possible terms allowed in your
# jurisdiction.

# I request but do not require that you give credit to me, shelleybutterfly,
# which I will accept in cases of licensing other than the public domain as
# valuable consideration for your use of my software. (I love to know that 
# my name is plastered all over some obscure piece of code that's at least
# appreciated by those who do see it, and I consider that more than enough
# consideration. :D) This software is provided completely as-is, and I take
# absolutely no responsibility for any damages that it may cause. It has
# not been fully tested and should be considered pre-alpha release quality,
# (that is to say, it is likely unstable and unsafe to use without your own
# validation to ensure that it meets some or any your needs without: among
# other things: melting your computer, calling your grandma at midnight,
# telling your girlfriend she's fat, and throwing your computer in the
# dishwasher to make sure it's clean, and you take full responsibility for
# doing your own testing to ensure that it suits your needs, and operates 
# properly int the conditions under which you intend to use it.

# Should you not be willing to take this risk, it is highly recommended
# that you do not use this software at all, ever, and that you instead find
# a real commercial piece of software, or other warranted piece of software,
# as I can not and do not and shall not provide any warranty of fitness for
# any purpose whatsoever, even to scrub your toilet, and it's provided with
# the understanding that it will be used primarily as an educational tool
# rather than any sort of production code. I disclaim any responsibility for
# anything that may happen to you due to your use of software, even if it
# causes huge zits, a rash under your boobs that wont go away, or a burning
# sensation when you pee. Sorry, **especially** for a burning sensation when
# you pee.

# Your use of this software implies acceptance of these terms as well as 
# any painful urination or other issues it may cause in your life.

# [deep breath] 

# my love to you all; I hope this software was useful to you in some way; if
# you end up using it despite the dire warnings against doing so, feel free
# to drop me a note at shelleybutterfly@mojoprocode.com, as I imagine it
# will be rare enough to make my day, every time. ♥

所以,就是这样。希望这能帮助到别人。我花了很长时间才能在shell脚本上做得足够好,能够完成这样的事情,别管它花了我很长时间,所以请原谅可能动摇了你们中的一些脚本大师的东西。:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10052041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档