3 # NON-CONFIGURABLE STUFF!
5 version="@@CT_VERSION@@"
10 my_name="$( basename "${0}" )"
13 readelf="${prefix}-readelf"
14 fake_load_addr="$((0xdeadbeef))"
15 fake_load_addr_sys="$((0x8badf00d))"
16 ld_library_path="/lib:/usr/lib"
19 printf "%s: %s\n" "${my_name}" "$*" >&2
24 printf "Try \`%s --help' for more information\n" >&2
28 # Fake a real ldd, just in case some dumb script would check
30 ldd (crosstool-NG) ${version}
31 Copyright (C) 2010 "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
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 Licensed under the GPLv2, see the file LICENSES in the top-directory of the
35 sources for this package.
41 Usage: ${my_name} [OPTION]... --root DIR FILE...
42 --help print this help and exit
43 --version print version information and exit
44 --root dir treat dir as being the root of the target
45 -s, --show-system mark libs from the sysroot with a trailing '[*]'
49 ${my_name} tries to mimick the behavior of a real native ldd, but can be
50 used in a cross-development environment. Here is how it differs from a
53 The LD_LIBRARY_PATH variable is not used, as it can not reliably be
54 guessed except at runtime, and we can't run.
56 ${my_name} does not scan /etc/ld.so.cache, but instead uses /etc/ld.so.conf
57 (it understands the include directives therein for libces that have that).
59 ${my_name} will search the directory specified with --root for libraries
60 to resolve the NEEDED tags. If --root is not set, then ${my_name} will
61 use the value in the environment variable \${CT_XLDD_ROOT}. If neither
62 is set, then this is an error.
64 If NEEDED libraries can't be found in the specified root directory, then
65 ${my_name} will also look in the sysroot of the toolchain to see if it
68 For NEEDED libraries that were found, the output will look like:
69 libneeded.so => /path/to/libneeded.so (0xloadaddr)
71 and for those that were not found, the output will look like:
72 libneeded.so not found
74 The paths are relative to the specified root directory, or to the sysroot
75 (eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).
77 The expected load address 'loadaddr' is a faked address to match the output
78 of the real ldd, but has no actual meaning (set to some constants for now,
79 0x8badf00d for libraries from the sysroot, 0xdeadbeef for others).
83 # -d, --data-relocs process data relocations
84 # -r, --function-relocs process data and function relocations
85 # -u, --unused print unused direct dependencies
86 # -v, --verbose print all information
88 # See also this thread:
89 # http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
92 # Parse command line options
93 root="${CT_XLDD_ROOT}"
116 do_opt_error "unrecognized option \`${1}'"
127 if [ -z "${root}" ]; then
128 do_opt_error "no root given"
131 if [ ! -d "${root}" ]; then
132 do_error "\`${root}': no such file or directory"
136 sysroot="$( "${gcc}" -print-sysroot 2>/dev/null )"
137 if [ -z "${sysroot}" ]; then
138 sysroot="$( "${gcc}" -print-file-name=libc.so 2>/dev/null \
139 |sed -r -e 's:/usr/lib/libc.so$::;' \
142 if [ -z "${sysroot}" ]; then
143 do_error "unable to find sysroot for \`${gcc}'"
146 do_report_needed_found() {
153 if [ -z "${system}" ]; then
154 loadaddr="${fake_load_addr}"
156 loadaddr="${fake_load_addr_sys}"
157 if [ -n "${show_system}" ]; then
162 printf "%8s%s => %s (0x%0*x)%s\n" \
171 # Search a needed file, scanning ${lib_dir} in the root directory
178 for d in "${needed_search_path[@]}"; do
179 if [ -f "${root}${d}/${needed}" ]; then
180 found="${d}/${needed}"
184 if [ -z "${found}" ]; then
185 for d in "${needed_search_path[@]}"; do
186 if [ -f "${sysroot}${d}/${needed}" ]; then
187 found_sysroot="${d}/${needed}"
193 if [ -n "${found}" ]; then
194 do_report_needed_found "${needed}" "${found}"
195 do_process_file "${root}${found}"
196 elif [ -n "${found_sysroot}" ]; then
197 do_report_needed_found "${needed}" "${found_sysroot}" "sys"
198 do_process_file "${sysroot}${found_sysroot}"
200 printf "%8c%s not found\n" "" "${needed}"
204 # Scan a file for all NEEDED tags
208 "${readelf}" -d "${file}" \
209 |"${grep}" -E '\(NEEDED\)' \
210 |"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[(.*)\]$/\1/;' \
211 |while read needed; do
212 do_find_needed "${needed}"
216 # Recursively scan a /etc/ld.so.conf file
217 do_scan_etc_ldsoconf() {
218 local ldsoconf="${1}"
222 [ -f "${ldsoconf}" ] || return 0
227 g="${root}${line#include }"
229 do_scan_etc_ldsoconf "${f}"
235 needed_search_path+=( "${line}" )
241 # Build up the full list of search directories
242 declare -a needed_search_path
243 ld_library_path="${ld_library_path}:"
244 while [ -n "${ld_library_path}" ]; do
245 d="${ld_library_path%%:*}"
246 [ -n "${d}" ] && needed_search_path+=( "${d}" )
247 ld_library_path="${ld_library_path#*:}"
249 do_scan_etc_ldsoconf "${root}/etc/ld.so.conf"
251 do_process_file "${1}"