-
-
Save austinmarton/b8f07c3175eaf15f61905b6770c5d1f7 to your computer and use it in GitHub Desktop.
Given a shared library (or executable) print the symbols it uses from libraries it directly depends on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Given a shared library, print the symbols it uses from other libraries it | |
# directly depends on. | |
LIB=$1 | |
# Use readelf rather than ldd here to only get direct dependencies. | |
DEPS=$(readelf -d $LIB | awk '/Shared library:/{ print substr($5, 2, length($5) - 2) }') | |
UNDEF_SYMS_FILE=solib-deps-$(basename $LIB)-undef.txt | |
echo "Symbols that are not defined in $LIB:" | |
cat $UNDEF_SYMS_FILE | |
echo | |
# Get a list of all undefined symbols in our target library. | |
# We strip off symbol version information, | |
# e.g. "AES_cbc_encrypt@@libcrypto.so.10" becomes "AES_cbc_encrypt". | |
nm -D $LIB | sed -ne 's/[ ]\+U \([^ @]\+\)\(@@.*$\|$\)/\1/p' | sort > $UNDEF_SYMS_FILE | |
# For each library listed as a dependency, get the list of symbols it exports, | |
# and print those that are also in the list of undefined symbols we generated | |
# above. | |
echo "Libraries that provide these symbols:" | |
for dep in $DEPS | |
do | |
# Use ldd here to find the full path to the library. | |
# Regex workaround here for libs including '+', e.g. libstdc++. | |
dep_regex=$(echo $dep | sed -e 's/\+/\\+/g') | |
DEP_FILE=$(ldd $LIB | awk "/$dep_regex =>/{ print \$3 }") | |
if [ ! -x "$DEP_FILE" ]; then | |
DEP_FILE=$(ls -a ./$dep 2>/dev/null) | |
fi | |
if [ ! -x "$DEP_FILE" ]; then | |
echo "W: Couldn't find a file for $dep. Skipping." | |
echo | |
continue | |
fi | |
DEP_PROVIDES_FILE=solib-deps-$(basename $dep)-provides.txt | |
readelf -DsW $DEP_FILE | awk '/GLOBAL DEFAULT/{ print $9 }' | sort > $DEP_PROVIDES_FILE | |
echo $DEP_FILE: | |
comm -12 $UNDEF_SYMS_FILE $DEP_PROVIDES_FILE | sed -e 's/^/ /' | |
echo | |
rm -f $DEP_PROVIDES_FILE | |
done | |
rm -f $UNDEF_SYMS_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment