patchelf - naughtont3/techbits GitHub Wiki

patchelf example

Manually modify rpath in ELF binary

Brief

These are my notes on manual steps to patch ELF shared library to remove UCX from the default rpath. This means that afterward you must set the LD_LIBRARY_PATH correctly to resolve the UCX shared library at runtime.

Steps

    1. Download/build and add patchelf to PATH
  wget https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0.tar.bz2
  tar -xf patchelf-0.18.0.tar.bz2
  cd patchelf-0.18.0/
  ./configure --prefix=$PWD/_install \
          && make \
          && make install
    1. Add patchelf to PATH
  summit-login3:$ export PATH=$HOME/projects.summit/bin:$PATH
  summit-login3:$ which patchelf
  ~/projects.summit/bin/patchelf
    1. Show rpath info in the shared library (e.g., libmpi.so)
  summit-login3:$ readelf -d /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib/libmpi.so | egrep -i 'rpath|runpath'

   0x000000000000000f (RPATH)              Library rpath:
   [/sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib:/sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/ucx-1.14.1-20230608/lib]
   summit-login3:$
    1. Now remove all rpath from library (libmpi.so), and then add just what we want back (i.e., remove UCX part).
  summit-login3:$ patchelf --remove-rpath \
         /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib/libmpi.so

  summit-login3:$ patchelf  \
         --set-rpath /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib \
         /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib/libmpi.so
    1. Then look at rpath info again and to confirm
  summit-login3:$ readelf -d /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib/libmpi.so | egrep -i 'rpath|runpath'

   0x000000000000000f (RPATH)              Library rpath:
   [/sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib]
  summit-login3:$
  • Note if no RPATH info, may need to add --force-rpath when doing the --set-rpath (example)
  summit-login3:$ patchelf  \
         --force-rpath \
         --set-rpath /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib \
         /sw/summit/ums/ompix/DEVELOP/gcc/13.1.1/install/openmpi-5.0.0rc12-20230608/lib/libmpi.so

Notes

  • NOTE: Ensure the needed UCX library is resolved at runtime via setting the LD_LIBRARY_PATH.

  • NOTE: Alternatively, you could use patchelf to change the rpath to a different UCX version if want to continue to resolve without reference to LD_LIBRARY_PATH.

References