ohair@425: #!/bin/bash ohair@425: # ohair@425: # Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. ohair@425: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@425: # ohair@425: # This code is free software; you can redistribute it and/or modify it ohair@425: # under the terms of the GNU General Public License version 2 only, as ohair@425: # published by the Free Software Foundation. ohair@425: # ohair@425: # This code is distributed in the hope that it will be useful, but WITHOUT ohair@425: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@425: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@425: # version 2 for more details (a copy is included in the LICENSE file that ohair@425: # accompanied this code). ohair@425: # ohair@425: # You should have received a copy of the GNU General Public License version ohair@425: # 2 along with this work; if not, write to the Free Software Foundation, ohair@425: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@425: # ohair@425: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@425: # or visit www.oracle.com if you need additional information or have any ohair@425: # questions. ohair@425: # ohair@425: ohair@425: # Simple tool to diff two shared libraries. ohair@425: # Criterias: two shared libraries are considered equal if: ohair@425: # the file sizes are the same AND the symbols outputs from the nm command are equal ohair@425: ohair@425: if [ $# -lt 2 ] ohair@425: then ohair@425: echo "Diff two shared libs. Return codes:" ohair@425: echo "0 - no diff" ohair@425: echo "1 - Identical symbols AND size, BUT not bytewise identical" ohair@425: echo "2 - Identical symbols BUT NEW size" ohair@425: echo "3 - Differences, content BUT SAME size" ohair@425: echo "4 - Differences, content AND size" ohair@425: echo "10 - Could not perform diff" ohair@425: echo "Use 'quiet' to disable any output." ohair@425: echo "Syntax: $0 file1 file2 [quiet]" ohair@425: exit 10 ohair@425: fi ohair@425: ohair@425: if [ ! -f $1 ] ohair@425: then ohair@425: echo $1 does not exist ohair@425: exit 10 ohair@425: fi ohair@425: ohair@425: if [ ! -f $2 ] ohair@425: then ohair@425: echo $2 does not exist ohair@425: exit 10 ohair@425: fi ohair@425: ohair@425: if [ "`uname`" == "SunOS" ]; then erikj@458: if [ -f "`which gnm`" ]; then erikj@458: NM=gnm erikj@458: # Jonas 2012-05-29: solaris native nm produces radically different output than gnm erikj@458: # so if using that...we need different filter than "cut -f 2-" erikj@458: # erikj@458: elif [ -f "`which nm`" ]; then erikj@458: NM=nm erikj@458: else erikj@458: echo "No nm command found" erikj@458: exit 10 erikj@458: fi erikj@445: LDD=ldd ohair@425: elif [ $OSTYPE == "cygwin" ]; then ohair@425: NM="$VS100COMNTOOLS/../../VC/bin/amd64/dumpbin.exe" ohair@425: NM_ARGS=/exports erikj@445: LDD= erikj@445: elif [ "`uname`" == "Darwin" ]; then erikj@445: NM=nm erikj@445: LDD="otool -L" ohair@425: else ohair@425: NM=nm erikj@445: LDD=ldd ohair@425: fi ohair@425: ohair@425: # Should the differences be viewed? ohair@425: VIEW= ohair@425: # You can do export DIFF=meld to view ohair@425: # any differences using meld instead. ohair@425: if [ -n "$DIFF" ]; then ohair@425: DIFF="$DIFF" ohair@425: else ohair@425: DIFF=diff ohair@425: fi ohair@425: OLD=$(cd $(dirname $1) && pwd)/$(basename $1) ohair@425: NEW=$(cd $(dirname $2) && pwd)/$(basename $2) ohair@425: erikj@458: OLD_SIZE=$(ls -l "$OLD" | awk '{ print $5 }') erikj@458: NEW_SIZE=$(ls -l "$NEW" | awk '{ print $5 }') ohair@425: ohair@425: if [ $# -gt 3 ] ohair@425: then ohair@425: ROOT1=$(cd $3 && pwd) ohair@425: ROOT2=$(cd $4 && pwd) ohair@425: OLD_NAME=$(echo $OLD | sed "s|$ROOT1/||") ohair@425: NEW_NAME=$(echo $NEW | sed "s|$ROOT2/||") ohair@425: if [ "x$5" == "xview" ]; then VIEW=view; fi ohair@425: else ohair@425: ROOT1=$(dirname $OLD)/ ohair@425: ROOT2=$(dirname $NEW)/ ohair@425: OLD_NAME=$OLD ohair@425: NEW_NAME=$NEW ohair@425: if [ "x$3" == "xview" ]; then VIEW=view; fi ohair@425: fi ohair@425: ohair@425: OLD_SUFFIX="${OLD##*.}" ohair@425: NEW_SUFFIX="${NEW##*.}" ohair@425: if [ "$OLD_SUFFIX" != "$NEW_SUFFIX" ]; then ohair@425: echo The files do not have the same suffix type! ohair@425: exit 10 ohair@425: fi ohair@425: ohair@425: if [ "$OLD_SUFFIX" != "so" ] && [ "$OLD_SUFFIX" != "dylib" ] && [ "$OLD_SUFFIX" != "dll" ]; then ohair@425: echo The files have to be .so, .dylib or .dll! They are $OLD_SUFFIX ohair@425: exit 10 ohair@425: fi ohair@425: ohair@425: TYPE="$OLD_SUFFIX" ohair@425: ohair@425: if cmp $OLD $NEW > /dev/null ohair@425: then ohair@425: # The files were bytewise identical. ohair@425: echo Identical: $OLD_NAME ohair@425: exit 0 ohair@425: fi ohair@425: erikj@458: OLD_SYMBOLS=$COMPARE_ROOT/nm.$OLD_NAME.old erikj@458: NEW_SYMBOLS=$COMPARE_ROOT/nm.$NEW_NAME.new ohair@425: ohair@425: mkdir -p $(dirname $OLD_SYMBOLS) ohair@425: mkdir -p $(dirname $NEW_SYMBOLS) ohair@425: ohair@425: if [ $OSTYPE == "cygwin" ]; then ohair@425: "$NM" $NM_ARGS $OLD | grep " = " > $OLD_SYMBOLS ohair@425: "$NM" $NM_ARGS $NEW | grep " = " > $NEW_SYMBOLS ohair@425: "$NM" $NM_ARGS $OLD > $OLD_SYMBOLS.full ohair@425: "$NM" $NM_ARGS $NEW > $NEW_SYMBOLS.full ohair@425: else ohair@425: # Strip the addresses, just compare the ordering of the symbols. ohair@425: $NM $OLD | cut -f 2- -d ' ' > $OLD_SYMBOLS ohair@425: $NM $NEW | cut -f 2- -d ' ' > $NEW_SYMBOLS ohair@425: # But store the full information for easy diff access. ohair@425: $NM $OLD > $OLD_SYMBOLS.full ohair@425: $NM $NEW > $NEW_SYMBOLS.full ohair@425: fi ohair@425: ohair@425: DIFFS=$(LANG=C diff $OLD_SYMBOLS $NEW_SYMBOLS) ohair@425: ohair@425: RESULT=0 ohair@425: erikj@445: if [ "${LDD}" ] erikj@445: then erikj@445: NAME=`basename $OLD` erikj@445: TMP=$COMPARE_ROOT/ldd/ldd.${NAME} erikj@445: rm -rf "${TMP}" erikj@445: mkdir -p "${TMP}" erikj@445: erikj@445: (cd "${TMP}" && cp $OLD . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.old | uniq > dep.uniq.old) erikj@445: (cd "${TMP}" && cp $NEW . && ${LDD} ${NAME} | awk '{ print $1;}' | sort | tee dep.new | uniq > dep.uniq.new) erikj@445: (cd "${TMP}" && rm -f ${NAME}) erikj@445: erikj@445: DIFFS_DEP=$(LANG=C diff "${TMP}/dep.old" "${TMP}/dep.new") erikj@445: DIFFS_UNIQ_DEP=$(LANG=C diff "${TMP}/dep.uniq.old" "${TMP}/dep.uniq.new") erikj@445: erikj@445: DEP_MSG= erikj@445: if [ -z "${DIFFS_UNIQ_DEP}" -a -z "${DIFFS_DEP}" ]; then erikj@445: DEP_MSG="Identical dependencies" erikj@445: elif [ -z "${DIFFS_UNIQ_DEP}" ]; then erikj@445: DEP_MSG="Redundant duplicate dependencies added" erikj@445: RES=1 erikj@445: else erikj@445: DEP_MSG="DIFFERENT dependencies" erikj@445: RES=1 erikj@445: fi erikj@445: fi erikj@445: ohair@425: if [ -n "$DIFFS" ]; then ohair@425: if [ $OLD_SIZE -ne $NEW_SIZE ] ohair@425: then erikj@445: echo Differences, content AND size : $DEP_MSG : $OLD_NAME ohair@425: RESULT=4 ohair@425: else erikj@445: echo Differences, content BUT SAME size: $DEP_MSG : $OLD_NAME ohair@425: RESULT=3 ohair@425: fi ohair@425: if [ "x$VIEW" == "xview" ]; then ohair@425: LANG=C $DIFF $OLD_SYMBOLS $NEW_SYMBOLS ohair@425: fi ohair@425: else ohair@425: if [ $OLD_SIZE -ne $NEW_SIZE ] ohair@425: then erikj@445: echo Identical symbols BUT NEW size : $DEP_MSG : $OLD_NAME ohair@425: RESULT=2 ohair@425: else erikj@445: echo Identical symbols AND size, BUT not bytewise identical: $DEP_MSG : $OLD_NAME ohair@425: RESULT=1 ohair@425: fi ohair@425: fi ohair@425: ohair@425: exit $RESULT ohair@425: ohair@425: ohair@425: