ldd 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/bin/bash
  2. # Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. # The GNU C Library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. # The GNU C Library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Lesser General Public License for more details.
  12. # You should have received a copy of the GNU Lesser General Public
  13. # License along with the GNU C Library; if not, see
  14. # <https://www.gnu.org/licenses/>.
  15. # This is the `ldd' command, which lists what shared libraries are
  16. # used by given dynamically-linked executables. It works by invoking the
  17. # run-time dynamic linker as a command and setting the environment
  18. # variable LD_TRACE_LOADED_OBJECTS to a non-empty value.
  19. # We should be able to find the translation right at the beginning.
  20. TEXTDOMAIN=libc
  21. TEXTDOMAINDIR=/usr/share/locale
  22. RTLDLIST="/usr/lib64/ld-linux-x86-64.so.2 /usr/lib/ld-linux.so.2 /usr/libx32/ld-linux-x32.so.2"
  23. warn=
  24. bind_now=
  25. verbose=
  26. unused=
  27. while test $# -gt 0; do
  28. case "$1" in
  29. --vers | --versi | --versio | --version)
  30. echo 'ldd (GNU libc) 2.43'
  31. printf $"Copyright (C) %s Free Software Foundation, Inc.
  32. This is free software; see the source for copying conditions. There is NO
  33. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  34. " "2024"
  35. printf $"Written by %s and %s.
  36. " "Roland McGrath" "Ulrich Drepper"
  37. exit 0
  38. ;;
  39. --h | --he | --hel | --help)
  40. echo $"Usage: ldd [OPTION]... FILE...
  41. --help print this help and exit
  42. --version print version information and exit
  43. -d, --data-relocs process data relocations
  44. -r, --function-relocs process data and function relocations
  45. -u, --unused print unused direct dependencies
  46. -v, --verbose print all information
  47. "
  48. printf $"For bug reporting instructions, please see:\\n%s.\\n" \
  49. "<https://www.gnu.org/software/libc/bugs.html>"
  50. exit 0
  51. ;;
  52. -d | --d | --da | --dat | --data | --data- | --data-r | --data-re | \
  53. --data-rel | --data-relo | --data-reloc | --data-relocs)
  54. warn=yes
  55. shift
  56. ;;
  57. -r | --f | --fu | --fun | --func | --funct | --functi | --functio | \
  58. --function | --function- | --function-r | --function-re | --function-rel | \
  59. --function-relo | --function-reloc | --function-relocs)
  60. warn=yes
  61. bind_now=yes
  62. shift
  63. ;;
  64. -v | --verb | --verbo | --verbos | --verbose)
  65. verbose=yes
  66. shift
  67. ;;
  68. -u | --u | --un | --unu | --unus | --unuse | --unused)
  69. unused=yes
  70. shift
  71. ;;
  72. --v | --ve | --ver)
  73. echo >&2 $"ldd: option \`$1' is ambiguous"
  74. exit 1
  75. ;;
  76. --) # Stop option processing.
  77. shift; break
  78. ;;
  79. -*)
  80. echo >&2 'ldd:' $"unrecognized option" "\`$1'"
  81. echo >&2 $"Try \`ldd --help' for more information."
  82. exit 1
  83. ;;
  84. *)
  85. break
  86. ;;
  87. esac
  88. done
  89. nonelf ()
  90. {
  91. # Maybe extra code for non-ELF binaries.
  92. return 1;
  93. }
  94. add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
  95. add_env="$add_env LD_VERBOSE=$verbose"
  96. if test "$unused" = yes; then
  97. add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
  98. fi
  99. # The following command substitution is needed to make ldd work in SELinux
  100. # environments where the RTLD might not have permission to write to the
  101. # terminal. The extra "x" character prevents the shell from trimming trailing
  102. # newlines from command substitution results. This function is defined as a
  103. # subshell compound list (using "(...)") to prevent parameter assignments from
  104. # affecting the calling shell execution environment.
  105. try_trace() (
  106. output=$(eval $add_env '"$@"' 2>&1; rc=$?; printf 'x'; exit $rc)
  107. rc=$?
  108. printf '%s' "${output%x}"
  109. return $rc
  110. )
  111. case $# in
  112. 0)
  113. echo >&2 'ldd:' $"missing file arguments"
  114. echo >&2 $"Try \`ldd --help' for more information."
  115. exit 1
  116. ;;
  117. 1)
  118. single_file=t
  119. ;;
  120. *)
  121. single_file=f
  122. ;;
  123. esac
  124. result=0
  125. for file do
  126. # We don't list the file name when there is only one.
  127. test $single_file = t || echo "${file}:"
  128. case $file in
  129. */*) :
  130. ;;
  131. *) file=./$file
  132. ;;
  133. esac
  134. if test ! -e "$file"; then
  135. echo "ldd: ${file}:" $"No such file or directory" >&2
  136. result=1
  137. elif test ! -f "$file"; then
  138. echo "ldd: ${file}:" $"not regular file" >&2
  139. result=1
  140. elif test -r "$file"; then
  141. test -x "$file" || echo 'ldd:' $"\
  142. warning: you do not have execution permission for" "\`$file'" >&2
  143. RTLD=
  144. ret=1
  145. for rtld in ${RTLDLIST}; do
  146. if test -x $rtld; then
  147. verify_out=`${rtld} --verify "$file"`
  148. ret=$?
  149. case $ret in
  150. [02]) RTLD=${rtld}; break;;
  151. esac
  152. fi
  153. done
  154. case $ret in
  155. 1)
  156. # This can be a non-ELF binary or no binary at all.
  157. nonelf "$file" || {
  158. echo $" not a dynamic executable" >&2
  159. result=1
  160. }
  161. ;;
  162. 0|2)
  163. try_trace "$RTLD" "$file" || result=1
  164. ;;
  165. *)
  166. echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
  167. exit 1
  168. ;;
  169. esac
  170. else
  171. echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
  172. result=1
  173. fi
  174. done
  175. exit $result
  176. # Local Variables:
  177. # mode:ksh
  178. # End: