2010-07-22 21:26:08 +00:00
|
|
|
#!@@CT_bash@@
|
|
|
|
|
|
|
|
# NON-CONFIGURABLE STUFF!
|
|
|
|
export LC_ALL=C
|
2010-11-23 20:34:56 +00:00
|
|
|
version="@@CT_VERSION@@"
|
2010-11-23 20:35:15 +00:00
|
|
|
bits="@@CT_BITS@@"
|
2010-07-22 21:26:08 +00:00
|
|
|
sed="@@CT_sed@@"
|
|
|
|
grep="@@CT_grep@@"
|
2010-11-23 20:35:15 +00:00
|
|
|
|
2010-07-22 21:26:08 +00:00
|
|
|
my_name="$( basename "${0}" )"
|
|
|
|
prefix="${0%-ldd}"
|
|
|
|
gcc="${prefix}-gcc"
|
|
|
|
readelf="${prefix}-readelf"
|
2010-11-23 20:36:01 +00:00
|
|
|
fake_load_addr_root="$((0xdeadbeef))"
|
2010-11-24 20:04:55 +00:00
|
|
|
fake_load_addr_rpath="$((0xdeadc0de))"
|
2010-11-23 20:36:01 +00:00
|
|
|
fake_load_addr_sysroot="$((0x8badf00d))"
|
2010-07-22 21:26:08 +00:00
|
|
|
ld_library_path="/lib:/usr/lib"
|
|
|
|
|
|
|
|
do_error() {
|
|
|
|
printf "%s: %s\n" "${my_name}" "$*" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
do_opt_error() {
|
|
|
|
do_error "$@"
|
2011-07-12 21:21:27 +00:00
|
|
|
printf "Try \`%s --help' for more information\n" "${my_name}" >&2
|
2010-07-22 21:26:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 20:35:45 +00:00
|
|
|
do_trace() {
|
|
|
|
local depth=0
|
|
|
|
|
|
|
|
[ -z "${CT_XLDD_VERBOSE}" ] && return 0
|
|
|
|
|
|
|
|
for((depth=0; "${#FUNCNAME[$((depth+1))]}" != 0; depth++)); do :; done
|
|
|
|
printf "%*s" $((4*(depth-1))) "" >&2
|
|
|
|
printf -- "$@" >&2
|
|
|
|
}
|
|
|
|
|
2010-07-22 21:26:08 +00:00
|
|
|
show_version() {
|
|
|
|
# Fake a real ldd, just in case some dumb script would check
|
|
|
|
cat <<_EOF_
|
2010-11-23 20:34:56 +00:00
|
|
|
ldd (crosstool-NG) ${version}
|
2012-03-06 20:36:10 +00:00
|
|
|
Copyright (C) 2010 "Yann E. MORIN" <yann.morin.1998@free.fr>
|
2010-07-22 21:26:08 +00:00
|
|
|
This is free software; see the source for copying conditions. There is NO
|
|
|
|
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
Licensed under the GPLv2, see the file LICENSES in the top-directory of the
|
|
|
|
sources for this package.
|
|
|
|
_EOF_
|
|
|
|
}
|
|
|
|
|
|
|
|
show_help() {
|
|
|
|
cat <<_EOF_
|
|
|
|
Usage: ${my_name} [OPTION]... --root DIR FILE...
|
|
|
|
--help print this help and exit
|
|
|
|
--version print version information and exit
|
|
|
|
--root dir treat dir as being the root of the target
|
|
|
|
-s, --show-system mark libs from the sysroot with a trailing '[*]'
|
2010-11-24 20:04:55 +00:00
|
|
|
and libs found via RPATH with a trailing '[+]'
|
2010-07-22 21:26:08 +00:00
|
|
|
|
|
|
|
_EOF_
|
|
|
|
cat <<_EOF_ |fmt
|
|
|
|
${my_name} tries to mimick the behavior of a real native ldd, but can be
|
|
|
|
used in a cross-development environment. Here is how it differs from a
|
|
|
|
real native ldd:
|
|
|
|
|
2011-07-10 21:37:58 +00:00
|
|
|
If the CT_XLDD_VERBOSE variable is set and non-empty, then ${my_name} will
|
2010-11-23 20:35:45 +00:00
|
|
|
print a lot of debug messages, explaining how it builds the library
|
|
|
|
search path, and how each library was found and why.
|
|
|
|
|
2010-07-22 21:26:08 +00:00
|
|
|
The LD_LIBRARY_PATH variable is not used, as it can not reliably be
|
|
|
|
guessed except at runtime, and we can't run.
|
|
|
|
|
|
|
|
${my_name} does not scan /etc/ld.so.cache, but instead uses /etc/ld.so.conf
|
|
|
|
(it understands the include directives therein for libces that have that).
|
|
|
|
|
2010-11-24 20:04:55 +00:00
|
|
|
${my_name} also interprets (tries to!) the RPATH/RUNPATH records found in
|
|
|
|
the dynamic ELF section. Such paths are searched for only relative to
|
|
|
|
the specified root, not from the sysroot (see below). Also, those paths
|
|
|
|
are searched for not only for the file they appear in, but also for its
|
|
|
|
dependencies.
|
|
|
|
|
2010-07-22 21:26:08 +00:00
|
|
|
${my_name} will search the directory specified with --root for libraries
|
|
|
|
to resolve the NEEDED tags. If --root is not set, then ${my_name} will
|
|
|
|
use the value in the environment variable \${CT_XLDD_ROOT}. If neither
|
|
|
|
is set, then this is an error.
|
|
|
|
|
|
|
|
If NEEDED libraries can't be found in the specified root directory, then
|
|
|
|
${my_name} will also look in the sysroot of the toolchain to see if it
|
|
|
|
can find them.
|
|
|
|
|
|
|
|
For NEEDED libraries that were found, the output will look like:
|
|
|
|
libneeded.so => /path/to/libneeded.so (0xloadaddr)
|
|
|
|
|
|
|
|
and for those that were not found, the output will look like:
|
|
|
|
libneeded.so not found
|
|
|
|
|
|
|
|
The paths are relative to the specified root directory, or to the sysroot
|
|
|
|
(eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).
|
|
|
|
|
|
|
|
The expected load address 'loadaddr' is a faked address to match the output
|
|
|
|
of the real ldd, but has no actual meaning (set to some constants for now,
|
2010-11-24 20:04:55 +00:00
|
|
|
0x8badf00d for libraries from the sysroot, 0xdeadc0de for those found via
|
|
|
|
the RPATH/RUNPATH records, and 0xdeadbeef for others).
|
2010-07-22 21:26:08 +00:00
|
|
|
_EOF_
|
|
|
|
|
|
|
|
# Unimplemeted yet:
|
|
|
|
# -d, --data-relocs process data relocations
|
|
|
|
# -r, --function-relocs process data and function relocations
|
|
|
|
# -u, --unused print unused direct dependencies
|
|
|
|
# -v, --verbose print all information
|
|
|
|
|
|
|
|
# See also this thread:
|
|
|
|
# http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
|
|
|
|
}
|
|
|
|
|
|
|
|
# Parse command line options
|
|
|
|
root="${CT_XLDD_ROOT}"
|
|
|
|
show_system=
|
|
|
|
while true; do
|
|
|
|
case "${1}" in
|
|
|
|
--help)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--version)
|
|
|
|
show_version
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--root)
|
|
|
|
root="$2"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--root=*)
|
|
|
|
root="${1#--root=}"
|
|
|
|
;;
|
|
|
|
--show-system|-s)
|
|
|
|
show_system=1
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
do_opt_error "unrecognized option \`${1}'"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# Sanity checks
|
|
|
|
if [ -z "${root}" ]; then
|
|
|
|
do_opt_error "no root given"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ! -d "${root}" ]; then
|
|
|
|
do_error "\`${root}': no such file or directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2010-11-23 20:35:28 +00:00
|
|
|
sysroot="$( "${gcc}" -print-sysroot 2>/dev/null )"
|
|
|
|
if [ -z "${sysroot}" ]; then
|
|
|
|
sysroot="$( "${gcc}" -print-file-name=libc.so 2>/dev/null \
|
|
|
|
|sed -r -e 's:/usr/lib/libc.so$::;' \
|
|
|
|
)"
|
|
|
|
fi
|
|
|
|
if [ -z "${sysroot}" ]; then
|
|
|
|
do_error "unable to find sysroot for \`${gcc}'"
|
|
|
|
fi
|
2010-07-22 21:26:08 +00:00
|
|
|
|
|
|
|
do_report_needed_found() {
|
|
|
|
local needed="${1}"
|
|
|
|
local path="${2}"
|
2010-11-23 20:36:01 +00:00
|
|
|
local origin="${3}"
|
2010-07-22 21:26:08 +00:00
|
|
|
local loadaddr
|
|
|
|
local sys
|
|
|
|
|
2010-11-23 20:36:01 +00:00
|
|
|
case "${origin}" in
|
|
|
|
root)
|
|
|
|
loadaddr="${fake_load_addr_root}"
|
|
|
|
;;
|
2010-11-24 20:04:55 +00:00
|
|
|
rpath)
|
|
|
|
loadaddr="${fake_load_addr_rpath}"
|
|
|
|
if [ -n "${show_system}" ]; then
|
|
|
|
sys=" [+]"
|
|
|
|
fi
|
|
|
|
;;
|
2010-11-23 20:36:01 +00:00
|
|
|
sysroot)
|
|
|
|
loadaddr="${fake_load_addr_sysroot}"
|
|
|
|
if [ -n "${show_system}" ]; then
|
|
|
|
sys=" [*]"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
2010-07-22 21:26:08 +00:00
|
|
|
|
|
|
|
printf "%8s%s => %s (0x%0*x)%s\n" \
|
|
|
|
"" \
|
|
|
|
"${needed}" \
|
|
|
|
"${path}" \
|
2010-11-23 20:35:15 +00:00
|
|
|
"$((bits/4))" \
|
2010-07-22 21:26:08 +00:00
|
|
|
"${loadaddr}" \
|
|
|
|
"${sys}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Search a needed file, scanning ${lib_dir} in the root directory
|
|
|
|
do_find_needed() {
|
|
|
|
local needed="${1}"
|
2010-11-23 20:36:01 +00:00
|
|
|
local -a list
|
2010-11-24 20:04:55 +00:00
|
|
|
local -a dirs
|
2010-07-22 21:26:08 +00:00
|
|
|
local found
|
2010-11-23 20:36:01 +00:00
|
|
|
local where
|
|
|
|
local base
|
|
|
|
local d i
|
2010-07-22 21:26:08 +00:00
|
|
|
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Searching for '%s'\n" "${needed}"
|
2010-11-23 20:35:45 +00:00
|
|
|
|
2010-11-24 20:04:55 +00:00
|
|
|
# rpath shall come first!
|
2010-11-23 20:36:01 +00:00
|
|
|
list=( \
|
2010-11-24 20:04:55 +00:00
|
|
|
"rpath:${root}" \
|
2010-11-23 20:36:01 +00:00
|
|
|
"root:${root}" \
|
|
|
|
"sysroot:${sysroot}" \
|
|
|
|
)
|
|
|
|
|
|
|
|
for i in "${list[@]}"; do
|
|
|
|
where="${i%%:*}"
|
|
|
|
base="${i#*:}"
|
2010-11-24 20:04:55 +00:00
|
|
|
if [ "${where}" = "rpath" ]; then
|
|
|
|
dirs=( "${search_rpath[@]}" )
|
|
|
|
else
|
|
|
|
dirs=( "${needed_search_path[@]}" )
|
|
|
|
fi
|
|
|
|
for d in "${dirs[@]}"; do
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> looking in '%s' (%s)\n" "${d}" "${where}"
|
2010-11-23 20:36:01 +00:00
|
|
|
if [ -f "${base}${d}/${needed}" ]; then
|
|
|
|
found="${d}/${needed}"
|
|
|
|
do_trace "---> found\n"
|
|
|
|
break 2
|
|
|
|
fi
|
|
|
|
done
|
2010-07-22 21:26:08 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if [ -n "${found}" ]; then
|
2010-11-23 20:36:01 +00:00
|
|
|
do_report_needed_found "${needed}" "${found}" "${where}"
|
|
|
|
do_process_file "${base}${found}"
|
2010-07-22 21:26:08 +00:00
|
|
|
else
|
2010-11-23 20:36:01 +00:00
|
|
|
printf "%8s%s not found\n" "" "${needed}"
|
2010-07-22 21:26:08 +00:00
|
|
|
fi
|
2011-07-10 22:13:57 +00:00
|
|
|
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Done searching for '%s'\n" "${needed}"
|
2010-07-22 21:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Scan a file for all NEEDED tags
|
|
|
|
do_process_file() {
|
|
|
|
local file="${1}"
|
2010-11-24 20:04:55 +00:00
|
|
|
local -a save_search_rpath
|
2010-11-24 20:01:46 +00:00
|
|
|
local n m
|
|
|
|
local found
|
2010-07-22 21:26:08 +00:00
|
|
|
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Parsing file '%s'\n" "${file}"
|
2010-11-23 20:35:45 +00:00
|
|
|
|
2010-11-24 20:04:55 +00:00
|
|
|
save_search_rpath=( "${search_rpath[@]}" )
|
|
|
|
for n in $( "${readelf}" -d "${file}" \
|
|
|
|
|"${grep}" -E '\((RPATH|RUNPATH)\)' \
|
|
|
|
|"${sed}" -r -e 's/^.*Library r(|un)path:[[:space:]]+\[(.*)\]$/\2/;'\
|
|
|
|
); do
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> adding rpath '%s'\n" "${n}"
|
2010-11-24 20:04:55 +00:00
|
|
|
search_rpath+=( "${n}" )
|
|
|
|
done
|
|
|
|
do_trace ": search path:\n"
|
|
|
|
for n in "${search_rpath[@]}" "${needed_search_path[@]}"; do
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace ": - '%s'\n" "${n}"
|
2010-11-24 20:04:55 +00:00
|
|
|
done
|
2012-11-07 20:55:42 +00:00
|
|
|
do_trace ": end search path\n"
|
2010-11-24 20:04:55 +00:00
|
|
|
|
2012-08-02 19:38:41 +00:00
|
|
|
for n in $( "${readelf}" -d "${file}" \
|
|
|
|
|"${grep}" -E '\(NEEDED\)' \
|
|
|
|
|"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[([^]]+)\].*/\1/;' \
|
2010-11-24 20:01:46 +00:00
|
|
|
); do
|
|
|
|
found=0
|
|
|
|
for m in "${needed_list[@]}"; do
|
|
|
|
[ "${n}" = "${m}" ] && found=1 && break
|
|
|
|
done
|
|
|
|
if [ ${found} -ne 0 ]; then
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> skipping already known dependency '%s'\n" "${n}"
|
2010-11-24 20:01:46 +00:00
|
|
|
continue
|
|
|
|
fi
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> handling new dependency '%s'\n" "${n}"
|
2010-11-24 20:01:46 +00:00
|
|
|
needed_list+=( "${n}" )
|
|
|
|
do_find_needed "${n}"
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> done handling dependency '%s'\n" "${n}"
|
2010-11-24 20:04:55 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
search_rpath=( "${save_search_rpath[@]}" )
|
2011-07-10 22:13:57 +00:00
|
|
|
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Finished parsing file '%s'\n" "${file}"
|
2010-07-22 21:26:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 20:35:41 +00:00
|
|
|
# Recursively scan a /etc/ld.so.conf file
|
|
|
|
do_scan_etc_ldsoconf() {
|
|
|
|
local ldsoconf="${1}"
|
|
|
|
local g
|
|
|
|
local f
|
|
|
|
|
|
|
|
[ -f "${ldsoconf}" ] || return 0
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Parsing ld.so.conf: '%s'\n" "${ldsoconf}"
|
2010-11-23 20:35:41 +00:00
|
|
|
|
|
|
|
while read line; do
|
|
|
|
case "${line}" in
|
|
|
|
include\ *)
|
|
|
|
g="${root}${line#include }"
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> handling include directive '%s'\n" "${g}"
|
2010-11-23 20:35:41 +00:00
|
|
|
for f in ${g}; do
|
|
|
|
do_scan_etc_ldsoconf "${f}"
|
|
|
|
done
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> finished handling include directive '%s'\n" "${g}"
|
2010-11-23 20:35:41 +00:00
|
|
|
;;
|
|
|
|
\#*|"")
|
|
|
|
;;
|
|
|
|
*)
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> adding search dir '%s'\n" "${line}"
|
2010-11-23 20:35:41 +00:00
|
|
|
needed_search_path+=( "${line}" )
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done <"${ldsoconf}"
|
2011-07-10 22:13:57 +00:00
|
|
|
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Finished parsing ld.so.conf: '%s'\n" "${ldsoconf}"
|
2010-11-23 20:35:41 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 21:26:08 +00:00
|
|
|
# Build up the full list of search directories
|
|
|
|
declare -a needed_search_path
|
2010-11-23 20:35:45 +00:00
|
|
|
do_trace "Adding basic lib dirs\n"
|
2010-07-22 21:26:08 +00:00
|
|
|
ld_library_path="${ld_library_path}:"
|
|
|
|
while [ -n "${ld_library_path}" ]; do
|
|
|
|
d="${ld_library_path%%:*}"
|
2010-11-23 20:35:45 +00:00
|
|
|
if [ -n "${d}" ]; then
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> adding search dir '%s'\n" "${d}"
|
2010-11-23 20:35:45 +00:00
|
|
|
needed_search_path+=( "${d}" )
|
|
|
|
fi
|
2010-07-22 21:26:08 +00:00
|
|
|
ld_library_path="${ld_library_path#*:}"
|
|
|
|
done
|
2011-07-10 22:13:57 +00:00
|
|
|
do_trace "Done adding basic lib dirs\n"
|
2010-11-23 20:35:45 +00:00
|
|
|
do_trace "Scanning '/etc/ld.so.conf'\n"
|
2010-11-23 20:35:41 +00:00
|
|
|
do_scan_etc_ldsoconf "${root}/etc/ld.so.conf"
|
2011-07-10 22:13:57 +00:00
|
|
|
do_trace "Done scanning '/etc/ld.so.conf'\n"
|
2010-11-23 20:35:45 +00:00
|
|
|
do_trace "Search path:\n"
|
|
|
|
for p in "${needed_search_path[@]}"; do
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "-> '%s'\n" "${p}"
|
2010-11-23 20:35:45 +00:00
|
|
|
done
|
2010-07-22 21:26:08 +00:00
|
|
|
|
2010-11-24 20:01:46 +00:00
|
|
|
declare -a needed_list
|
2010-11-24 20:04:55 +00:00
|
|
|
declare -a search_rpath
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Scanning file '%s'\n" "${1}"
|
2010-07-22 21:26:08 +00:00
|
|
|
do_process_file "${1}"
|
2011-07-10 22:15:00 +00:00
|
|
|
do_trace "Done scanning file '%s'\n" "${1}"
|