交叉编译器ldd

时间:2011-05-27 09:00:38

标签: linux ubuntu arm cross-compiling ldd

我创建了一个交叉编译的arm可执行文件。我想找到可执行文件的库依赖项。我正在使用ubuntu natty并安装了arm-linux-gnueabi工具链,它不包含ldd。有没有工具可以在linux中查看arm可执行文件库的依赖性。

6 个答案:

答案 0 :(得分:67)

这有点像kluge,但它是我能找到的最好的解决方案,而且它在基本使用方面确实很有效 - 只需将此脚本保存为“arm-none-linux-gnueabi-ldd”与您的其他交叉工具。

#!/bin/sh
arm-none-linux-gnueabi-readelf -a $1 | grep "Shared library:"

答案 1 :(得分:15)

你也可以使用 objdump ,以便只转储和搜索二进制文件的标题部分。这可能会节省你几毫秒......

#!/bin/sh  
arm-none-linux-gnueabi-objdump -x $1 | grep NEEDED

答案 2 :(得分:6)

这是另一个选项,你可以将LD_TRACE_LOADED_OBJECTS环境变量设置为任何值,比如1,然后你只需运行可执行文件,输出应该是它的动态依赖项。

答案 3 :(得分:3)

我发现了这一点,并希望它可以帮助许多仍在寻找真正的ldd解决方案的人。 ldd只是一个支持ld-linux库的脚本。那么为什么不制作自己的ldd脚本呢?首先,您需要在系统中找到ld-linux库,该库应该可用。在我的例子中它是/lib/ld-linux-armhf.so.3所以我把它放到RTLDLIST中,你可以在下面的脚本中看到。

并将此脚本放入您的设备或主板中,您可以使用ldd来查找可执行文件的从属库非常方便。

祝你好运!

#! /bin/bash
# Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with the GNU C Library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.


# This is the `ldd' command, which lists what shared libraries are
# used by given dynamically-linked executables.  It works by invoking the
# run-time dynamic linker as a command and setting the environment
# variable LD_TRACE_LOADED_OBJECTS to a non-empty value.

# We should be able to find the translation right at the beginning.
TEXTDOMAIN=libc
TEXTDOMAINDIR=/usr/share/locale

RTLDLIST="/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2 /lib/ld-linux-armhf.so.3"
warn=
bind_now=
verbose=

while test $# -gt 0; do
  case "$1" in
  --vers | --versi | --versio | --version)
    echo 'ldd (Ubuntu EGLIBC 2.15-0ubuntu10.3) 2.15'
    printf $"Copyright (C) %s Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
" "2012"
    printf $"Written by %s and %s.
" "Roland McGrath" "Ulrich Drepper"
    exit 0
    ;;
  --h | --he | --hel | --help)
    printf $"Usage: ldd [OPTION]... FILE...
      --help              print this help and exit
      --version           print version information and exit
  -d, --data-relocs       process data relocations
  -r, --function-relocs   process data and function relocations
  -u, --unused            print unused direct dependencies
  -v, --verbose           print all information
"
    printf $"For bug reporting instructions, please see:
%s.
" "<http://www.debian.org/Bugs/>"
    exit 0
    ;;
  -d | --d | --da | --dat | --data | --data- | --data-r | --data-re | \
  --data-rel | --data-relo | --data-reloc | --data-relocs)
    warn=yes
    shift
    ;;
  -r | --f | --fu | --fun | --func | --funct | --functi | --functio | \
  --function | --function- | --function-r | --function-re | --function-rel | \
  --function-relo | --function-reloc | --function-relocs)
    warn=yes
    bind_now=yes
    shift
    ;;
  -v | --verb | --verbo | --verbos | --verbose)
    verbose=yes
    shift
    ;;
  -u | --u | --un | --unu | --unus | --unuse | --unused)
    unused=yes
    shift
    ;;
  --v | --ve | --ver)
    echo >&2 $"ldd: option \`$1' is ambiguous"
    exit 1
    ;;
  --)       # Stop option processing.
    shift; break
    ;;
  -*)
    echo >&2 'ldd:' $"unrecognized option" "\`$1'"
    echo >&2 $"Try \`ldd --help' for more information."
    exit 1
    ;;
  *)
    break
    ;;
  esac
done

nonelf ()
{
  # Maybe extra code for non-ELF binaries.
  return 1;
}

add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
add_env="$add_env LD_LIBRARY_VERSION=\$verify_out"
add_env="$add_env LD_VERBOSE=$verbose"
if test "$unused" = yes; then
  add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
fi

# The following use of cat is needed to make ldd work in SELinux
# environments where the executed program might not have permissions
# to write to the console/tty.  But only bash 3.x supports the pipefail
# option, and we don't bother to handle the case for older bash versions.
if x=`set -o` && test "$x" != "${x#*pipefail}" && set -o pipefail ; then
  try_trace() {
    eval $add_env '"$@"' | cat
  }
else
  try_trace() {
    eval $add_env '"$@"'
  }
fi

case $# in
0)
  echo >&2 'ldd:' $"missing file arguments"
  echo >&2 $"Try \`ldd --help' for more information."
  exit 1
  ;;
1)
  single_file=t
  ;;
*)
  single_file=f
  ;;
esac

result=0
for file do
  # We don't list the file name when there is only one.
  test $single_file = t || echo "${file}:"
  case $file in
  */*) :
       ;;
  *) file=./$file
     ;;
  esac
  if test ! -e "$file"; then
    echo "ldd: ${file}:" $"No such file or directory" >&2
    result=1
  elif test ! -f "$file"; then
    echo "ldd: ${file}:" $"not regular file" >&2
    result=1
  elif test -r "$file"; then
    RTLD=
    ret=1
    for rtld in ${RTLDLIST}; do
      if test -x $rtld; then
    verify_out=`${rtld} --verify "$file"`
    ret=$?
    case $ret in
    [02]) RTLD=${rtld}; break;;
    esac
      fi
    done
    case $ret in
    0|2)
      try_trace "$RTLD" "$file" || result=1
      ;;
    1|126)
      # This can be a non-ELF binary or no binary at all.
      nonelf "$file" || {
    echo $" not a dynamic executable"
    result=1
      }
      ;;
    *)
      echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
      exit 1
      ;;
    esac
  else
    echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
    result=1
  fi
done

exit $result
# Local Variables:
#  mode:ksh
# End:

答案 4 :(得分:2)

如前所述,按设计ldd can only been executed on target。但是,可以使用ldd模仿readelf行为。在xldd项目中开发了一个名为crosstool-ng的脚本。此脚本的独立版本可在此处获得:

https://gist.github.com/jerome-pouiller/c403786c1394f53f44a3b61214489e6f

答案 5 :(得分:0)

在处理项目时遇到[Sourceware.Bugzilla]: Bug 16628 - Segfault after a binary without pthread dlopen()s a library linked with pthread时,我找到了一个解决方案(涉及在 ARM 上移植一些 C ++ 代码)。

我正在使用 Ubtu 16.04 ,并且对于 ARM 交叉编译工具,我安装了 g ++-4.9-arm-linux-gnueabihf (和依赖项,并且迫使我卸载 g ++-multilib (和依赖项)。为了测试(运行)可执行文件,我安装了QEMU qemu-user-static )。

根据[man7]: LDD(1)(这只是一个shell( bash )脚本):

  

在通常情况下, ldd 将标准动态链接器(请参阅ld.so(8))与 LD_TRACE_LOADED_OBJECTS 环境变量设置为1。

我将以上述错误报告中的数据为例。

issue16417.cpp

#include <string>
#include <iostream>


extern char **environ;


int main() {
    for (char **it = environ; *it != nullptr; ++it) {
        std::string str(*it);
        std::cout << "export " << str.substr(0, str.find('=')) << std::endl;
    }
}

build_issue16417.sh (已删除的编译器不支持的标志):

#!/bin/bash

_BUILD_TOOL_BASE_NAME=arm-linux-gnueabihf-g++-4.9
_FILE_BASE_NAME=issue16417

${_BUILD_TOOL_BASE_NAME} -pthread -std=gnu++0x -pedantic -ggdb -O2 -pipe -mtls-dialect=gnu2 -ftree-loop-distribution -ftree-vectorize -fmerge-all-constants -fira-loop-pressure -pedantic -c -o ${_FILE_BASE_NAME}.o ${_FILE_BASE_NAME}.cpp

${_BUILD_TOOL_BASE_NAME} -pthread -std=gnu++0x -pedantic -ggdb -O2 -pipe -mtls-dialect=gnu2 -ftree-loop-distribution -ftree-vectorize -fmerge-all-constants -fira-loop-pressure -pedantic -Wl,-O1 -Wl,--sort-common -Wl,--enable-new-dtags -Wl,--gc-sections -Wl,--hash-style=gnu -o ${_FILE_BASE_NAME}.${_BUILD_TOOL_BASE_NAME} ${_FILE_BASE_NAME}.o  -Wl,--as-needed -pthread -Wl,-fuse-ld=gold

输出

[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q006150000]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]>
[prompt]> uname -a
Linux cfati-ubtu16x64-0 4.15.0-39-generic #42~16.04.1-Ubuntu SMP Wed Oct 24 17:09:54 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[prompt]> which arm-linux-gnueabihf-g++-4.9
/usr/bin/arm-linux-gnueabihf-g++-4.9
[prompt]> ldd /usr/bin/arm-linux-gnueabihf-g++-4.9
        linux-vdso.so.1 =>  (0x00007ffdf55c2000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f59b6c62000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f59b702c000)
[prompt]> dpkg -S /usr/bin/arm-linux-gnueabihf-g++-4.9
g++-4.9-arm-linux-gnueabihf: /usr/bin/arm-linux-gnueabihf-g++-4.9
[prompt]>
[prompt]> ls
build_issue16417_g++4.9.sh  issue16417.cpp
[prompt]> ./build_issue16417_g++4.9.sh
[prompt]> ls
build_issue16417_g++4.9.sh  issue16417.arm-linux-gnueabihf-g++-4.9  issue16417.cpp  issue16417.o
[prompt]> file ./issue16417.arm-linux-gnueabihf-g++-4.9
./issue16417.arm-linux-gnueabihf-g++-4.9: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=17e0f3d6ceaa13df4ac031e6581074757abee36c, not stripped
[prompt]> echo ${_QEMU_CMD}
/usr/bin/qemu-arm-static -L /usr/arm-linux-gnueabihf
[prompt]> ${_QEMU_CMD} ./issue16417.arm-linux-gnueabihf-g++-4.9
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
[prompt]>
[prompt]> ldd ./issue16417.arm-linux-gnueabihf-g++-4.9
        not a dynamic executable

按预期,(常规) ldd ARM 二进制文件无效。

ldd 脚本中,开始处有一行(没有其他人引用 .so s):

RTLDLIST="/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2 /libx32/ld-linux-x32.so.2"

如图所示,它没有正确的 ld.so (到目前为止,只有 i686 x86_64 ),因此它无法处理 ARM 二进制文件。要使一切正常运行(请注意,对于每个选项,都需要 sudo ):

  1. 在行尾添加 ARM 一个
  2. 创建 ldd 的副本,然后修改该版本(用正确的版本替换现有的 .so

我选择了2 nd 变体。但是 ARM ld.so 在哪里?您可能已经猜到了,它可以从 $ {_ QEMU_CMD} env var中获得(毕竟, qemu 也需要它)。在我的机器上,它是: /usr/arm-linux-gnueabihf/lib/ld-2.23.so (需要 libc6-armhf-cross ,但现在应该安装)。因此,我将 ldd 复制到了 arm-linux-gnueabihf-ldd ,并将上面的行替换为:

_LIB_PATH=/usr/arm-linux-gnueabihf/lib

RTLDLIST="${_LIB_PATH}/ld-2.23.so"

# @TODO - cfati: Not a fan of the line below (I think it's considered bad practice), but without it, it can't find the libraries locations.
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${_LIB_PATH}

还发布 diff 格式(请检查[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)修补 utrunner 部分)以了解如何应用该格式)

[prompt]> diff --binary -uN /usr/bin/ldd /usr/bin/arm-linux-gnueabihf-ldd
--- /usr/bin/ldd        2018-01-15 04:49:13.000000000 +0200
+++ /usr/bin/arm-linux-gnueabihf-ldd    2018-12-04 19:34:19.499001000 +0200
@@ -26,7 +26,13 @@
 TEXTDOMAIN=libc
 TEXTDOMAINDIR=/usr/share/locale

-RTLDLIST="/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2 /libx32/ld-linux-x32.so.2"
+_LIB_PATH=/usr/arm-linux-gnueabihf/lib
+
+RTLDLIST="${_LIB_PATH}/ld-2.23.so"
+
+# @TODO - cfati: Not a fan of the line below (I think it's considered bad practice), but without it, it can't find the libraries locations.
+export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${_LIB_PATH}
+
 warn=
 bind_now=
 verbose=

尝试在 ARM 可执行文件和voilà上运行“ ldd

[prompt]> arm-linux-gnueabihf-ldd ./issue16417.arm-linux-gnueabihf-g++-4.9
        libstdc++.so.6 => /usr/arm-linux-gnueabihf/lib/libstdc++.so.6 (0xf66c6000)
        libc.so.6 => /usr/arm-linux-gnueabihf/lib/libc.so.6 (0xf65da000)
        /lib/ld-linux-armhf.so.3 => /usr/arm-linux-gnueabihf/lib/ld-2.23.so (0xf6fd7000)
        libm.so.6 => /usr/arm-linux-gnueabihf/lib/libm.so.6 (0xf6562000)
        libgcc_s.so.1 => /usr/arm-linux-gnueabihf/lib/libgcc_s.so.1 (0xf6538000)

注意:接下来是不是问题/答案的一部分
无论如何,在 build_issue16417.sh 中将-根据需要替换为- no-根据需要 ,产生:

[prompt]> vi build_issue16417_g++4.9.sh
[prompt]> ./build_issue16417_g++4.9.sh
[prompt]> ${_QEMU_CMD} ./issue16417.arm-linux-gnueabihf-g++-4.9
export LC_NAME
export LC_TIME
export LESSCLOSE
export XDG_RUNTIME_DIR
export LESSOPEN
export SSH_CONNECTION
export XDG_DATA_DIRS
export LOGNAME
export HOME
export SHLVL
export PAPERSIZE
export LC_MEASUREMENT
export LANG
export PWD
export LC_IDENTIFICATION
export QT_QPA_PLATFORMTHEME
export PATH
export MAIL
export LC_TELEPHONE
export LS_COLORS
export USER
export SSH_TTY
export OLDPWD
export LC_NUMERIC
export SSH_CLIENT
export SHELL
export TERM
export ANDROID_HOME
export LC_MONETARY
export XDG_SESSION_ID
export LC_ADDRESS
export LC_PAPER

:)