e6015f55e6eb692a78a5ba213d07d4a39a619090
[platform/upstream/linaro-glibc.git] / elf / ldd.bash.in
1 #! @BASH@
2 # Copyright (C) 1996-2001, 2002, 2003, 2004 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, write to the Free
17 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307 USA.
19
20
21 # This is the `ldd' command, which lists what shared libraries are
22 # used by given dynamically-linked executables.  It works by invoking the
23 # run-time dynamic linker as a command and setting the environment
24 # variable LD_TRACE_LOADED_OBJECTS to a non-empty value.
25
26 # We should be able to find the translation right at the beginning.
27 TEXTDOMAIN=libc
28 TEXTDOMAINDIR=@TEXTDOMAINDIR@
29
30 RTLDLIST=@RTLD@
31 warn=
32 bind_now=
33 verbose=
34
35 while test $# -gt 0; do
36   case "$1" in
37   --vers | --versi | --versio | --version)
38     echo 'ldd (GNU libc) @VERSION@'
39     printf $"Copyright (C) %s Free Software Foundation, Inc.
40 This is free software; see the source for copying conditions.  There is NO
41 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
42 " "2004"
43     printf $"Written by %s and %s.
44 " "Roland McGrath" "Ulrich Drepper"
45     exit 0
46     ;;
47   --h | --he | --hel | --help)
48     echo $"Usage: ldd [OPTION]... FILE...
49       --help              print this help and exit
50       --version           print version information and exit
51   -d, --data-relocs       process data relocations
52   -r, --function-relocs   process data and function relocations
53   -v, --verbose           print all information
54 Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
55     exit 0
56     ;;
57   -d | --d | --da | --dat | --data | --data- | --data-r | --data-re | \
58   --data-rel | --data-relo | --data-reloc | --data-relocs)
59     warn=yes
60     shift
61     ;;
62   -r | --f | --fu | --fun | --func | --funct | --functi | --functio | \
63   --function | --function- | --function-r | --function-re | --function-rel | \
64   --function-relo | --function-reloc | --function-relocs)
65     warn=yes
66     bind_now=yes
67     shift
68     ;;
69   -v | --verb | --verbo | --verbos | --verbose)
70     verbose=yes
71     shift
72     ;;
73   --v | --ve | --ver)
74     echo >&2 $"ldd: option \`$1' is ambiguous"
75     exit 1
76     ;;
77   --)           # Stop option processing.
78     shift; break
79     ;;
80   -*)
81     echo >&2 'ldd:' $"unrecognized option" "\`$1'"
82     echo >&2 $"Try \`ldd --help' for more information."
83     exit 1
84     ;;
85   *)
86     break
87     ;;
88   esac
89 done
90
91 nonelf ()
92 {
93   # Maybe extra code for non-ELF binaries.
94   return 1;
95 }
96
97 add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
98 add_env="$add_env LD_VERBOSE=$verbose"
99 case $# in
100 0)
101   echo >&2 'ldd:' $"missing file arguments"
102   echo >&2 $"Try \`ldd --help' for more information."
103   exit 1
104   ;;
105 1)
106   single_file=t
107   ;;
108 *)
109   single_file=f
110   ;;
111 esac
112
113 result=0
114 for file do
115   # We don't list the file name when there is only one.
116   test $single_file = t || echo "${file}:"
117   case $file in
118   */*) :
119        ;;
120   *) file=./$file
121      ;;
122   esac
123   if test ! -f "$file"; then
124     echo "ldd: ${file}:" $"No such file or directory" >&2
125     result=1
126   elif test -r "$file"; then
127     test -x "$file" || echo 'ldd:' $"\
128 warning: you do not have execution permission for" "\`$file'" >&2
129     RTLD=
130     for rtld in ${RTLDLIST}; do
131       if test -x $rtld; then
132         verify_out=`${rtld} --verify "$file"`
133         ret=$?
134         case $ret in
135         [02]) RTLD=${rtld}; break;;
136         esac
137       fi
138     done
139     if test -z "${RTLD}"; then
140       set ${RTLDLIST}
141       RTLD=$1
142       verify_out=`${RTLD} --verify "$file"`
143       ret=$?
144     fi
145     case $ret in
146     0)
147       eval $add_env '"$file"' || result=1
148       ;;
149     1)
150       # This can be a non-ELF binary or no binary at all.
151       nonelf "$file" || {
152         echo $" not a dynamic executable"
153         result=1
154       }
155       ;;
156     2)
157       eval $add_env \${RTLD} '"$file"' || result=1
158       ;;
159     *)
160       echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
161       exit 1
162       ;;
163     esac
164   else
165     echo 'ldd:' $"error: you do not have read permission for" "\`$file'" >&2
166     result=1
167   fi
168 done
169
170 exit $result
171 # Local Variables:
172 #  mode:ksh
173 # End: