3 # NON-CONFIGURABLE STUFF!
5 version="@@CT_VERSION@@"
8 my_name="$( basename "${0}" )"
11 readelf="${prefix}-readelf"
12 fake_load_addr="$((0xdeadbeef))"
13 fake_load_addr_sys="$((0x8badf00d))"
14 ld_library_path="/lib:/usr/lib"
17 printf "%s: %s\n" "${my_name}" "$*" >&2
22 printf "Try \`%s --help' for more information\n" >&2
26 # Fake a real ldd, just in case some dumb script would check
28 ldd (crosstool-NG) ${version}
29 Copyright (C) 2010 "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
30 This is free software; see the source for copying conditions. There is NO
31 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
32 Licensed under the GPLv2, see the file LICENSES in the top-directory of the
33 sources for this package.
39 Usage: ${my_name} [OPTION]... --root DIR FILE...
40 --help print this help and exit
41 --version print version information and exit
42 --root dir treat dir as being the root of the target
43 -s, --show-system mark libs from the sysroot with a trailing '[*]'
47 ${my_name} tries to mimick the behavior of a real native ldd, but can be
48 used in a cross-development environment. Here is how it differs from a
51 The LD_LIBRARY_PATH variable is not used, as it can not reliably be
52 guessed except at runtime, and we can't run.
54 ${my_name} does not scan /etc/ld.so.cache, but instead uses /etc/ld.so.conf
55 (it understands the include directives therein for libces that have that).
56 [Note: this is missing for now...]
58 ${my_name} will search the directory specified with --root for libraries
59 to resolve the NEEDED tags. If --root is not set, then ${my_name} will
60 use the value in the environment variable \${CT_XLDD_ROOT}. If neither
61 is set, then this is an error.
63 If NEEDED libraries can't be found in the specified root directory, then
64 ${my_name} will also look in the sysroot of the toolchain to see if it
67 For NEEDED libraries that were found, the output will look like:
68 libneeded.so => /path/to/libneeded.so (0xloadaddr)
70 and for those that were not found, the output will look like:
71 libneeded.so not found
73 The paths are relative to the specified root directory, or to the sysroot
74 (eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).
76 The expected load address 'loadaddr' is a faked address to match the output
77 of the real ldd, but has no actual meaning (set to some constants for now,
78 0x8badf00d for libraries from the sysroot, 0xdeadbeef for others).
82 # -d, --data-relocs process data relocations
83 # -r, --function-relocs process data and function relocations
84 # -u, --unused print unused direct dependencies
85 # -v, --verbose print all information
87 # See also this thread:
88 # http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
91 # Parse command line options
92 root="${CT_XLDD_ROOT}"
115 do_opt_error "unrecognized option \`${1}'"
126 if [ -z "${root}" ]; then
127 do_opt_error "no root given"
130 if [ ! -d "${root}" ]; then
131 do_error "\`${root}': no such file or directory"
135 sysroot="$( "${gcc}" -print-sysroot 2>/dev/null )"
136 if [ -z "${sysroot}" ]; then
137 sysroot="$( "${gcc}" -print-file-name=libc.so 2>/dev/null \
138 |sed -r -e 's:/usr/lib/libc.so$::;' \
141 if [ -z "${sysroot}" ]; then
142 do_error "unable to find sysroot for \`${gcc}'"
145 do_report_needed_found() {
152 if [ -z "${system}" ]; then
153 loadaddr="${fake_load_addr}"
155 loadaddr="${fake_load_addr_sys}"
156 if [ -n "${show_system}" ]; then
161 # 8 to fake a 32-bit load address
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 # Build up the full list of search directories
217 declare -a needed_search_path
218 ld_library_path="${ld_library_path}:"
219 while [ -n "${ld_library_path}" ]; do
220 d="${ld_library_path%%:*}"
221 [ -n "${d}" ] && needed_search_path+=( "${d}" )
222 ld_library_path="${ld_library_path#*:}"
225 do_process_file "${1}"