4 libkey="lib_symbols_$1"
10 libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
22 usage: $0 [-Uv] [-L LD_LIBRARY_PATH] file
23 -L: Specify an alternative LD_LIBRARY_PATH for the library resolution.
24 -U: Skip looking for unresolved symbols.
25 -v: Show which library each symbol is resolved to.
33 while getopts "L:Uv" flag; do
35 L) LIB_PATH="${OPTARG}" ;;
36 U) CHECK_UNRESOLVED=0 ;;
37 v) VERBOSE_RESOLVED=1 ;;
43 if ! [ -f "$1" ]; then
44 echo "No such file or directory: $1" >&2
48 mime=$(file -L --mime-type $1)
51 *application/x-executable|*application/x-pie-executable) isbin=1 ;;
52 *application/x-sharedlib);;
53 *) echo "Not an elf file" >&2 ; exit 1;;
56 # Gather all symbols from the target
57 unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
58 [ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
59 if [ -n "${LIB_PATH}" ]; then
60 for libc in /lib/libc.so.*; do
61 LDD_ENV="LD_PRELOAD=${libc}"
63 LDD_ENV="${LDD_ENV} LD_LIBRARY_PATH=${LIB_PATH}"
66 ldd_libs=$(env ${LDD_ENV} ldd $(realpath $1) | awk '{print $1 ":" $3}')
68 # Check for useful libs
71 for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
72 echo -n "checking if $lib is needed: "
73 if [ -n "${lib##/*}" ]; then
74 for libpair in ${ldd_libs}; do
76 ${lib}:*) libpath="${libpair#*:}" && break ;;
82 list_libs="$list_libs $lib"
84 lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
85 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
86 # Save the global symbols for this lib
88 setvar "${libkey}" "${lib_symbols}"
90 for fct in ${lib_symbols}; do
91 case " ${unresolved_symbols} ${bss_symbols} " in
92 *\ ${fct}\ *) foundone="${fct}" && break ;;
95 if [ -n "${foundone}" ]; then
96 echo "yes... ${foundone}"
103 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
104 # Add in crt1 symbols
105 list_libs="${list_libs} crt1.o"
106 lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
107 # Save the global symbols for this lib
109 setvar "${libkey}" "${lib_symbols}"
111 # Now search libs for all symbols and report missing ones.
112 for sym in ${unresolved_symbols}; do
114 for lib in ${list_libs}; do
116 eval "lib_symbols=\"\${${libkey}}\""
117 # lib_symbols now contains symbols for the lib.
118 case " ${lib_symbols} " in
120 [ ${VERBOSE_RESOLVED} -eq 1 ] &&
121 echo "Resolved symbol ${sym} from ${lib}"
127 if [ $found -eq 0 ]; then
128 echo "Unresolved symbol $sym"