Skip to content

Instantly share code, notes, and snippets.

@satoooon8888
Last active June 28, 2022 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoooon8888/69a093f849830dd190b4e39fa66bb64b to your computer and use it in GitHub Desktop.
Save satoooon8888/69a093f849830dd190b4e39fa66bb64b to your computer and use it in GitHub Desktop.
patch libc with ubuntu glibc debug symbol
#!/bin/bash -ex
LIBC=$(ls * | grep -P '^(libc\.so\.6|libc-.*\.so)$')
LIBC_DBG_DEB="$(~/package/libc-database/identify $LIBC | sed s/libc6_/libc6-dbg_/g).deb"
if [[ ! -e $LIBC_DBG_DEB ]]; then
wget http://archive.ubuntu.com/ubuntu/pool/main/g/glibc/$LIBC_DBG_DEB
fi
DEBUGID=$(file $LIBC | grep -Po '(?<=BuildID\[sha1\]=)[0-9a-f]{40}')
TEMPDIR=./deb
if [[ ! -d $TEMPDIR ]]; then
mkdir $TEMPDIR
fi
DEBUGFILE="$(echo $DEBUGID | sed -e 's/^\(..\)//g').debug"
ar vx $LIBC_DBG_DEB --output $TEMPDIR
case "$(ls $TEMPDIR/data.* | xargs basename)" in
*data\.tar\.gz)
SYMBOLPATH=$(tar -tvf $TEMPDIR/data.tar.gz | grep -Po "[^ ]*$DEBUGFILE.*")
tar -C $TEMPDIR -xvf $TEMPDIR/data.tar.gz $SYMBOLPATH
;;
*data\.tar\.zst)
SYMBOLPATH=$(tar --use-compress-program=unzstd -tvf $TEMPDIR/data.tar.zst | grep -Po "[^ ]*$DEBUGFILE.*")
tar --use-compress-program=unzstd -C $TEMPDIR -xvf $TEMPDIR/data.tar.zst $SYMBOLPATH
;;
esac
cp "$TEMPDIR/$SYMBOLPATH" "./$LIBC.debug"
rm -r ./deb
rm libc6-*.deb
cp $LIBC "original_$LIBC"
objcopy --remove-section=.gnu_debuglink $LIBC
objcopy --add-gnu-debuglink="./$LIBC.debug" $LIBC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment