5 libkey="lib_symbols_$1"
11 libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
24 while getopts "Uv" flag; do
26 U) CHECK_UNRESOLVED=0 ;;
27 v) VERBOSE_RESOLVED=1 ;;
32 mime=$(file -L --mime-type $1)
35 *application/x-executable) isbin=1 ;;
36 *application/x-sharedlib);;
37 *) echo "Not an elf file" >&2 ; exit 1;;
40 # Gather all symbols from the target
41 unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
42 [ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
43 ldd_libs=$(ldd $(realpath $1) | awk '{print $1 ":" $3}')
45 # Check for useful libs
48 for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
49 echo -n "checking if $lib is needed: "
50 if [ -n "${lib##/*}" ]; then
51 for libpair in ${ldd_libs}; do
53 ${lib}:*) libpath="${libpair#*:}" && break ;;
59 list_libs="$list_libs $lib"
61 lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
62 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
63 # Save the global symbols for this lib
65 setvar "${libkey}" "${lib_symbols}"
67 for fct in ${lib_symbols}; do
68 case " ${unresolved_symbols} ${bss_symbols} " in
69 *\ ${fct}\ *) foundone="${fct}" && break ;;
72 if [ -n "${foundone}" ]; then
73 echo "yes... ${foundone}"
80 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
82 list_libs="${list_libs} crt1.o"
83 lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
84 # Save the global symbols for this lib
86 setvar "${libkey}" "${lib_symbols}"
88 # Now search libs for all symbols and report missing ones.
89 for sym in ${unresolved_symbols}; do
91 for lib in ${list_libs}; do
93 eval "lib_symbols=\"\${${libkey}}\""
94 # lib_symbols now contains symbols for the lib.
95 case " ${lib_symbols} " in
97 [ ${VERBOSE_RESOLVED} -eq 1 ] &&
98 echo "Resolved symbol ${sym} from ${lib}"
104 if [ $found -eq 0 ]; then
105 echo "Unresolved symbol $sym"