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