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

MitchRatquest commented Feb 20, 2019

This is enough to get orca copied over. Once you rsync that tar, extract it, then you have to run these:

export LD_LIBRARY_PATH=/lib:/usr/local/lib/orca
modprobe snd_seq
orca --portmidi-list-devices
#you should see something like:
#ID: 0 Name: f_midi
orca --portmidi-output-device 0 midi

#in another terminal:
pd
#these settings worked for me:
#OSS-midi -> input devices -> /dev/midi3
#I also have midi USB OTG configured

######NOTE
#I had accidentally typed pd in a terminal on my dekstop, and f_midi is the USB OTG midi, so it was working from 1 computer to another
#To make it work as intended (localhost) you need snd_card_virmidi enabled under audio in the kernel config
#I'm rebuilding a kernel to test this

#https://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick
#If you set LD_PRELOAD to the path of a shared object, that file will be loaded before any other library (including the C runtime, libc.so).
LD_PRELOAD=/path/to/my/malloc.so /bin/ls
#update feb26: if you use LD_PRELOAD, you need to unset it after you run the command or you risk segfaulting
export LD_PRELOAD=

@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