2014-11-06 23:45:03 +01:00
|
|
|
#!/bin/sh
|
|
|
|
# $FreeBSD$
|
|
|
|
|
2015-04-21 05:29:03 +02:00
|
|
|
libkey() {
|
|
|
|
libkey="lib_symbols_$1"
|
2015-04-21 07:10:18 +02:00
|
|
|
patterns=[.+,/-]
|
2015-04-21 05:29:03 +02:00
|
|
|
replacement=_
|
|
|
|
while :; do
|
|
|
|
case " ${libkey} " in
|
|
|
|
*${patterns}*)
|
|
|
|
libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-04-20 22:51:19 +02:00
|
|
|
ret=0
|
|
|
|
CHECK_UNRESOLVED=1
|
2015-04-21 05:06:58 +02:00
|
|
|
while getopts "U" flag; do
|
2015-04-20 22:51:19 +02:00
|
|
|
case "${flag}" in
|
|
|
|
U) CHECK_UNRESOLVED=0 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
2015-04-10 00:16:35 +02:00
|
|
|
mime=$(file -L --mime-type $1)
|
2015-04-21 05:29:03 +02:00
|
|
|
isbin=0
|
2014-11-06 23:45:03 +01:00
|
|
|
case $mime in
|
2015-04-21 05:29:03 +02:00
|
|
|
*application/x-executable) isbin=1 ;;
|
2014-11-06 23:45:03 +01:00
|
|
|
*application/x-sharedlib);;
|
|
|
|
*) echo "Not an elf file" >&2 ; exit 1;;
|
|
|
|
esac
|
|
|
|
|
2015-04-20 22:51:19 +02:00
|
|
|
# Gather all symbols from the target
|
2015-04-21 07:41:56 +02:00
|
|
|
unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
|
|
|
|
[ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
|
2015-04-21 04:02:25 +02:00
|
|
|
ldd_libs=$(ldd $(realpath $1) | awk '{print $1 ":" $3}')
|
2015-04-20 22:51:19 +02:00
|
|
|
|
2014-11-06 23:45:03 +01:00
|
|
|
# Check for useful libs
|
2015-04-20 22:51:19 +02:00
|
|
|
list_libs=
|
|
|
|
resolved_symbols=
|
2015-04-10 00:16:35 +02:00
|
|
|
for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
|
2015-04-20 22:45:10 +02:00
|
|
|
echo -n "checking if $lib is needed: "
|
2015-04-21 07:10:18 +02:00
|
|
|
if [ -n "${lib##/*}" ]; then
|
|
|
|
for libpair in ${ldd_libs}; do
|
|
|
|
case "${libpair}" in
|
|
|
|
${lib}:*) libpath="${libpair#*:}" && break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
else
|
|
|
|
libpath="${lib}"
|
|
|
|
fi
|
2015-04-20 22:51:19 +02:00
|
|
|
list_libs="$list_libs $lib"
|
|
|
|
foundone=
|
2015-04-21 05:29:03 +02:00
|
|
|
lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
|
2015-04-20 22:51:19 +02:00
|
|
|
if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
|
|
|
|
# Save the global symbols for this lib
|
|
|
|
libkey "${lib}"
|
2015-04-21 05:29:03 +02:00
|
|
|
setvar "${libkey}" "${lib_symbols}"
|
2015-04-20 22:51:19 +02:00
|
|
|
fi
|
|
|
|
for fct in ${lib_symbols}; do
|
2015-04-21 07:41:56 +02:00
|
|
|
case " ${unresolved_symbols} ${bss_symbols} " in
|
2015-04-20 22:51:19 +02:00
|
|
|
*\ ${fct}\ *) foundone="${fct}" && break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
if [ -n "${foundone}" ]; then
|
|
|
|
echo "yes... ${foundone}"
|
2015-04-20 22:45:10 +02:00
|
|
|
else
|
|
|
|
echo "no"
|
2015-04-20 22:51:19 +02:00
|
|
|
ret=1
|
2015-04-20 22:45:10 +02:00
|
|
|
fi
|
2014-11-06 23:45:03 +01:00
|
|
|
done
|
|
|
|
|
2015-04-20 22:51:19 +02:00
|
|
|
if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
|
2015-04-21 05:29:03 +02:00
|
|
|
# Add in crt1 symbols
|
|
|
|
list_libs="${list_libs} crt1.o"
|
|
|
|
lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
|
|
|
|
# Save the global symbols for this lib
|
|
|
|
libkey "crt1.o"
|
|
|
|
setvar "${libkey}" "${lib_symbols}"
|
|
|
|
|
2015-05-01 06:35:42 +02:00
|
|
|
# Now search libs for all symbols and report missing ones.
|
2015-04-20 22:51:19 +02:00
|
|
|
for sym in ${unresolved_symbols}; do
|
|
|
|
found=0
|
|
|
|
for lib in ${list_libs}; do
|
|
|
|
libkey "${lib}"
|
|
|
|
eval "lib_symbols=\"\${${libkey}}\""
|
|
|
|
# lib_symbols now contains symbols for the lib.
|
|
|
|
case " ${lib_symbols} " in
|
|
|
|
*\ ${sym}\ *) found=1 && break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
if [ $found -eq 0 ]; then
|
|
|
|
echo "Unresolved symbol $sym"
|
|
|
|
ret=1
|
|
|
|
fi
|
2015-04-20 22:45:10 +02:00
|
|
|
done
|
2015-04-20 22:51:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit ${ret}
|