一个解析动态库 中unresolved symbol,是不是在别的库中存在的例子

Makefile是这样子的,这就知道了会链接哪些库。 也就是别的库中是不是包含unresolved symbol。

 

LDFLAGS   += -ldl
LDFLAGS   += -lpthread
LDFLAGS   += -lslap

#LDFLAGS   += -lcrypt

EXTRALIBS    += ../library.a
EXTRALIBS    += -lgcc_s
EXTRALIBS    += -lcrypto
EXTRALIBS    += -lssl
EXTRALIBS    += -lz
EXTRALIBS    += -L..

 

下面是这个高级的bash, 其中用到了一些bash的字符处理的用法,挺高级的

 

 

===============================================================

#!/bin/bash

TEST_LIB=libtest.so
MAKEFILE="./Makefile"

nm $TEST_LIB |egrep '^[[:blank:]]+U ' |grep -v 'GLIBC' |grep -v '@@GCC_' >u_list

#get shared lib list from Makefile

#parse the "-lxxx"
SHARED_LIB_NAMES=
grep '^LDFLAGS' $MAKEFILE  >shared_lib_list
for i in `strings shared_lib_list`; do
    if [ ${i:0:2} = "-l"  ]; then
        libname=${i#"-l"}
        if  [ $libname != "dl" ]  && [ $libname != "pthread" ];then
            SHARED_LIB_NAMES=$SHARED_LIB_NAMES" lib"$libname".so"
        fi
    fi
done


#parse the "-Lxxx"
SHARED_LIB_DIRS="."
grep 'EXTRALIBS' $MAKEFILE  >shared_lib_list
for i in `strings shared_lib_list`; do
    if [ ${i:0:2} = "-L"  ]; then
        libdir=${i#"-L"}
        if  [ $libdir != ".." ] ;then
            SHARED_LIB_DIRS=$SHARED_LIB_DIRS" "$libdir
        fi
    fi
done

rm shared_lib_list

#get a lib whole name
SHARED_LIB_LIST=
for i in $SHARED_LIB_NAMES;do
    found=0
    for j in $SHARED_LIB_DIRS; do

        if [ -n "$(find $j -maxdepth 1  -name $i)" ];then
            found=1
            SHARED_LIB_LIST=$SHARED_LIB_LIST" "$j"/"$i
            break
        fi
    done
   
    if [ $found -eq 0 ];then
        echo "lib $i can't be found"
    fi
done





echo "the depending shared libraries are:"
echo $SHARED_LIB_LIST
echo "Now, resolving......"
echo


MISSING_SYMBOLS=

for i in `strings u_list` ;do
    if [ $i != "U"  ]; then
        found=0
        for j in $SHARED_LIB_LIST; do

    if [ ! -e $j ] ;then
        echo "Warning, can't find $j"
    fi
   
        #find the defined symbol
            if [ -n "$(nm $j |grep "[^U] $i/>")" ]; then
                found=1
                break
            fi
        done

        if [ $found -eq 0 ];then
            MISSING_SYMBOLS=$MISSING_SYMBOLS" "$i
        fi
    fi
done

rm u_list


if [ -z "$MISSING_SYMBOLS"  ];then
    echo "$TEST_LIB is safe for loading"
else
    echo "$TEST_LIB will fail for loading, unresolved symbols are:"
    for i in $MISSING_SYMBOLS;do
        echo $i
    done

fi

你可能感兴趣的:(list,gcc,bash,makefile)