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 )"
137 do_report_needed_found() {
144 if [ -z "${system}" ]; then
145 loadaddr="${fake_load_addr}"
147 loadaddr="${fake_load_addr_sys}"
148 if [ -n "${show_system}" ]; then
153 # 8 to fake a 32-bit load address
154 printf "%8s%s => %s (0x%0*x)%s\n" \
163 # Search a needed file, scanning ${lib_dir} in the root directory
170 for d in "${needed_search_path[@]}"; do
171 if [ -f "${root}${d}/${needed}" ]; then
172 found="${d}/${needed}"
176 if [ -z "${found}" ]; then
177 for d in "${needed_search_path[@]}"; do
178 if [ -f "${sysroot}${d}/${needed}" ]; then
179 found_sysroot="${d}/${needed}"
185 if [ -n "${found}" ]; then
186 do_report_needed_found "${needed}" "${found}"
187 do_process_file "${root}${found}"
188 elif [ -n "${found_sysroot}" ]; then
189 do_report_needed_found "${needed}" "${found_sysroot}" "sys"
190 do_process_file "${sysroot}${found_sysroot}"
192 printf "%8c%s not found\n" "" "${needed}"
196 # Scan a file for all NEEDED tags
200 "${readelf}" -d "${file}" \
201 |"${grep}" -E '\(NEEDED\)' \
202 |"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[(.*)\]$/\1/;' \
203 |while read needed; do
204 do_find_needed "${needed}"
208 # Build up the full list of search directories
209 declare -a needed_search_path
210 ld_library_path="${ld_library_path}:"
211 while [ -n "${ld_library_path}" ]; do
212 d="${ld_library_path%%:*}"
213 [ -n "${d}" ] && needed_search_path+=( "${d}" )
214 ld_library_path="${ld_library_path#*:}"
217 do_process_file "${1}"