Skip to content

Instantly share code, notes, and snippets.

@MitchRatquest
Last active January 6, 2020 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MitchRatquest/5c4b0ea2c2936e9f9c88fba2400c76b4 to your computer and use it in GitHub Desktop.
Save MitchRatquest/5c4b0ea2c2936e9f9c88fba2400c76b4 to your computer and use it in GitHub Desktop.
Change shared library path on a binary
#!/bin/sh
BINARY='htop'
SHLIB_DEST="usr/lib/$BINARY"
if [[ $UID != 0 ]]
then
echo "please run as root"
exit 1
fi
mkdir -p $SHLIB_DEST
cp `which $BINARY` .
cp $(ldd -v $BINARY | grep "=>" | awk -F"=>" '{print $2}' | awk '{print $1}' | sort | uniq) $SHLIB_DEST/
cp $(which $BINARY) $SHLIB_DEST/
cd $SHLIB_DEST
patchelf --set-rpath . $BINARY
#for any subsequent shlib you need to link to here too
if [ -f libncursesw.so.5 ]
then
patchelf --set-rpath . libncursesw.so.5 #handles libdl.so.6 in nano's case
fi
INTERPRETER=$(basename $(ldd -v $BINARY | grep "=>" | grep ld | awk '{print $1'} | uniq | head -n1) )
sudo patchelf --set-interpreter /usr/lib/$BINARY/$INTERPRETER $BINARY
#now we need to create the binary wrapper to load all this correctly
cd ../..
mkdir -p bin
cd bin
cat << EOF >> $BINARY
#!/bin/sh
PREVIOUSPATH=\$( echo \$LD_LIBRARY_PATH)
export LD_LIBRARY_PATH=/usr/lib/$BINARY
/usr/lib/$BINARY/$BINARY "\$@"
export LD_LIBRARY_PATH=\$PREVIOUSPATH
EOF
chmod 777 $BINARY
cd ../..
tar -cvf $BINARY.tar usr/
echo "ta da!"
echo "extract with: tar xvf $BINARY.tar on the top level"
@MitchRatquest
Copy link
Author

#!/bin/bash
mkdir -p nicegoodlibs
for BINARY in $@
do
rsync -avPL --relative $(ldd -v $BINARY | grep "=>" | awk -F"=>" '{print $2}' | awk '{print $1}' | sort | uniq) nicegoodlibs/
rsync -avPl --relative $BINARY nicegoodlibs
done
tar -cvf nicegoodlibs.tar nicegoodlibs
#if you don't want to mangle each binary, but copy over their depends wholesale
#might be good if doing an incremental version bump without a package manager, ie ipk or deb or whatever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment