5 libkey="lib_symbols_$1"
11 libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
23 usage: $0 [-Uv] [-L LD_LIBRARY_PATH] file
24 -L: Specify an alternative LD_LIBRARY_PATH for the library resolution.
25 -U: Skip looking for unresolved symbols.
26 -v: Show which library each symbol is resolved to.
34 while getopts "L:Uv" flag; do
36 L) LIB_PATH="${OPTARG}" ;;
37 U) CHECK_UNRESOLVED=0 ;;
38 v) VERBOSE_RESOLVED=1 ;;
44 if ! [ -f "$1" ]; then
45 echo "No such file or directory: $1" >&2
49 mime=$(file -L --mime-type $1)
52 *application/x-executable) isbin=1 ;;
53 *application/x-sharedlib);;
54 *) echo "Not an elf file" >&2 ; exit 1;;
57 # Gather all symbols from the target
58 unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
59 [ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
60 if [ -n "${LIB_PATH}" ]; then
61 for libc in /lib/libc.so.*; do
62 LDD_ENV="LD_PRELOAD=${libc}"
64 LDD_ENV="${LDD_ENV} LD_LIBRARY_PATH=${LIB_PATH}"
67 ldd_libs=$(env ${LDD_ENV} ldd $(realpath $1) | awk '{print $1 ":" $3}')
69 # Check for useful libs
72 for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
73 echo -n "checking if $lib is needed: "
74 if [ -n "${lib##/*}" ]; then
75 for libpair in ${ldd_libs}; do
77 ${lib}:*) libpath="${libpair#*:}" && break ;;
83 list_libs="$list_libs $lib"
85 lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
86 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
87 # Save the global symbols for this lib
89 setvar "${libkey}" "${lib_symbols}"
91 for fct in ${lib_symbols}; do
92 case " ${unresolved_symbols} ${bss_symbols} " in
93 *\ ${fct}\ *) foundone="${fct}" && break ;;
96 if [ -n "${foundone}" ]; then
97 echo "yes... ${foundone}"
104 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
105 # Add in crt1 symbols
106 list_libs="${list_libs} crt1.o"
107 lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
108 # Save the global symbols for this lib
110 setvar "${libkey}" "${lib_symbols}"
112 # Now search libs for all symbols and report missing ones.
113 for sym in ${unresolved_symbols}; do
115 for lib in ${list_libs}; do
117 eval "lib_symbols=\"\${${libkey}}\""
118 # lib_symbols now contains symbols for the lib.
119 case " ${lib_symbols} " in
121 [ ${VERBOSE_RESOLVED} -eq 1 ] &&
122 echo "Resolved symbol ${sym} from ${lib}"
128 if [ $found -eq 0 ]; then
129 echo "Unresolved symbol $sym"