1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#!/usr/bin/env bash
OS=`uname -s`
if test "$OS" == "Darwin" && command -v brew &>/dev/null; then
# add keg-only llvm to PATH so llvm-objcopy and llvm-readobj can be found
PATH=$PATH:$(brew --prefix llvm)/bin
fi
if [ -z "$OBJCOPY" ]; then
OBJCOPY=objcopy
if ! $OBJCOPY --version &> /dev/null; then
OBJCOPY=llvm-objcopy
if ! $OBJCOPY --version &> /dev/null; then
echo "Neither objcopy nor llvm-objcopy are installed"
exit 1
fi
fi
fi
if [ -z "$STRIP" ]; then
STRIP=strip
fi
if [ "$OS" != "Darwin" -a -z "$READELF" ]; then
READELF=readelf
if ! $READELF --version &> /dev/null; then
READELF=llvm-readelf
if ! $READELF --version &> /dev/null; then
echo "Neither readelf nor llvm-readelf are installed"
exit 1
fi
fi
fi
if [ "$#" -lt 1 ]; then
echo "Usage: djlink [-d <debug_name>] <solib_name> [<djstubify_opts>]"
exit 0
fi
while getopts "d:v" opt; do
case "$opt" in
d)
D="$OPTARG"
;;
v)
djstubify -v
exit $?
;;
\?)
exit 1
;;
esac
done
shift $(($OPTIND-1))
SO=$1
shift
if [ ! -f "$SO" ]; then
echo "$0: file $SO missing"
exit 1
fi
if [ -n "$D" ]; then
if test "$OS" == "Darwin"; then
debuginfo="`dsymutil -s libtmp.so | grep N_OSO`"
else
debuginfo="`$READELF -S $SO | grep .debug_info`"
fi
if [ -z "$debuginfo" ]; then
echo "$0: -d is specified but $SO is stripped"
exit 1
fi
DBG="/tmp/$D"
TMP=$(mktemp)
if test "$OS" == "Darwin"; then
dsymutil -f $SO -o $DBG
$STRIP -x -o $TMP $SO
TMP2=$(mktemp)
echo $D > $TMP2
$OBJCOPY --add-section __TEXT,.gnu_debuglink=$TMP2 $TMP
OFFS=`LC_ALL=C llvm-readobj -S $TMP | grep -A 4 .gnu_debuglink | \
grep Offset | sed 's/.* //'`
else
$OBJCOPY --only-keep-debug $SO $DBG
$STRIP -o $TMP $SO
$OBJCOPY --add-gnu-debuglink=$DBG $TMP
OFFS=`LC_ALL=C $READELF -S $TMP | grep .gnu_debuglink | \
tr -s '[:space:]' | cut -d " " -f 6 | sed -E -e 's/0*/0x/'`
fi
SO=$TMP
DOPT="-n $OFFS -l $DBG"
fi
if test "$OS" == "Darwin"; then
ID="`LC_ALL=C llvm-readobj -W -S $SO | grep -A 4 .dj64startup`"
else
ID="`LC_ALL=C $READELF -W -S $SO | grep .dj64startup | sed -E -e 's/.+\] +//' | \
tr -s '[:space:]'`"
fi
if [ -n "$ID" ]; then
if test "$OS" == "Darwin"; then
SZ=`echo "$ID" | grep Size | sed -e 's/.* //'`
OFF=`echo "$ID" | grep Offset | sed -e 's/.* //'`
else
SZ=`echo "$ID" | cut -d " " -f 5 | sed -E -e 's/0*/0x/'`
OFF=`echo "$ID" | cut -d " " -f 4 | sed -E -e 's/0*/0x/'`
fi
DOPT="$DOPT -S $SZ -O $OFF -f 0x8000"
fi
CMD="djstubify -g $* -l $SO $DOPT"
#echo "$CMD"
$CMD
[ -z "$TMP" ] || rm "$TMP"
[ -z "$TMP2" ] || rm "$TMP2"
if [ -n "$DBG" -a -f "$DBG" ]; then
rm $DBG
fi
|