Merge glibc-ports into ports/ directory.
[platform/upstream/glibc.git] / elf / ldd.bash.in
1 #! @BASH@
2 # Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc.
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
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.
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
13 # Lesser General Public License for more details.
14
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <http://www.gnu.org/licenses/>.
18
19
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
25 # We should be able to find the translation right at the beginning.
26 TEXTDOMAIN=libc
27 TEXTDOMAINDIR=@TEXTDOMAINDIR@
28
29 RTLDLIST=@RTLD@
30 warn=
31 bind_now=
32 verbose=
33
34 while test $# -gt 0; do
35   case "$1" in
36   --vers | --versi | --versio | --version)
37     echo 'ldd (GNU libc) @VERSION@'
38     printf $"Copyright (C) %s Free Software Foundation, Inc.
39 This is free software; see the source for copying conditions.  There is NO
40 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
41 " "2012"
42     printf $"Written by %s and %s.
43 " "Roland McGrath" "Ulrich Drepper"
44     exit 0
45     ;;
46   --h | --he | --hel | --help)
47     echo $"Usage: ldd [OPTION]... FILE...
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
52   -u, --unused            print unused direct dependencies
53   -v, --verbose           print all information
54 "
55     echo $"For bug reporting instructions, please see:
56 <http://www.gnu.org/software/libc/bugs.html>.
57 "
58     exit 0
59     ;;
60   -d | --d | --da | --dat | --data | --data- | --data-r | --data-re | \
61   --data-rel | --data-relo | --data-reloc | --data-relocs)
62     warn=yes
63     shift
64     ;;
65   -r | --f | --fu | --fun | --func | --funct | --functi | --functio | \
66   --function | --function- | --function-r | --function-re | --function-rel | \
67   --function-relo | --function-reloc | --function-relocs)
68     warn=yes
69     bind_now=yes
70     shift
71     ;;
72   -v | --verb | --verbo | --verbos | --verbose)
73     verbose=yes
74     shift
75     ;;
76   -u | --u | --un | --unu | --unus | --unuse | --unused)
77     unused=yes
78     shift
79     ;;
80   --v | --ve | --ver)
81     echo >&2 $"ldd: option \`$1' is ambiguous"
82     exit 1
83     ;;
84   --)           # Stop option processing.
85     shift; break
86     ;;
87   -*)
88     echo >&2 'ldd:' $"unrecognized option" "\`$1'"
89     echo >&2 $"Try \`ldd --help' for more information."
90     exit 1
91     ;;
92   *)
93     break
94     ;;
95   esac
96 done
97
98 nonelf ()
99 {
100   # Maybe extra code for non-ELF binaries.
101   return 1;
102 }
103
104 add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
105 add_env="$add_env LD_VERBOSE=$verbose"
106 if test "$unused" = yes; then
107   add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
108 fi
109
110 # The following use of cat is needed to make ldd work in SELinux
111 # environments where the executed program might not have permissions
112 # to write to the console/tty.  But only bash 3.x supports the pipefail
113 # option, and we don't bother to handle the case for older bash versions.
114 if set -o pipefail 2> /dev/null; then
115   try_trace() {
116     eval $add_env '"$@"' | cat
117   }
118 else
119   try_trace() {
120     eval $add_env '"$@"'
121   }
122 fi
123
124 case $# in
125 0)
126   echo >&2 'ldd:' $"missing file arguments"
127   echo >&2 $"Try \`ldd --help' for more information."
128   exit 1
129   ;;
130 1)
131   single_file=t
132   ;;
133 *)
134   single_file=f
135   ;;
136 esac
137
138 result=0
139 for file do
140   # We don't list the file name when there is only one.
141   test $single_file = t || echo "${file}:"
142   case $file in
143   */*) :
144        ;;
145   *) file=./$file
146      ;;
147   esac
148   if test ! -e "$file"; then
149     echo "ldd: ${file}:" $"No such file or directory" >&2
150     result=1
151   elif test ! -f "$file"; then
152     echo "ldd: ${file}:" $"not regular file" >&2
153     result=1
154   elif test -r "$file"; then
155     test -x "$file" || echo 'ldd:' $"\
156 warning: you do not have execution permission for" "\`$file'" >&2
157     RTLD=
158     ret=1
159     for rtld in ${RTLDLIST}; do
160       if test -x $rtld; then
161         verify_out=`${rtld} --verify "$file"`
162         ret=$?
163         case $ret in
164         [02]) RTLD=${rtld}; break;;
165         esac
166       fi
167     done
168     case $ret in
169     0)
170       # If the program exits with exit code 5, it means the process has been
171       # invoked with __libc_enable_secure.  Fall back to running it through
172       # the dynamic linker.
173       try_trace "$file"
174       rc=$?
175       if [ $rc = 5 ]; then
176         try_trace "$RTLD" "$file"
177         rc=$?
178       fi
179       [ $rc = 0 ] || result=1
180       ;;
181     1)
182       # This can be a non-ELF binary or no binary at all.
183       nonelf "$file" || {
184         echo $" not a dynamic executable"
185         result=1
186       }
187       ;;
188     2)
189       try_trace "$RTLD" "$file" || result=1
190       ;;
191     *)
192       echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
193       exit 1
194       ;;
195     esac
196   else
197     echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
198     result=1
199   fi
200 done
201
202 exit $result
203 # Local Variables:
204 #  mode:ksh
205 # End: