Can I change the "rpath" in an already compiled binary?

I have an old executable that is scheduled for a bunch of waste, but it does not exist yet. It relies on some libraries that have been removed from my environment, but I have some stub files somewhere where it works fine. Id like to point this executable to these stub files. Yes, I can set LD_LIBRARY_PATH, but this executable is called from many scripts, and many users, and I would like to fix it in one place.

I have no source for this, and it would be difficult to get it. I thought: can I edit this file using the ELF editor and add a simple PATH to rpath so that it gets into new libraries? Is this possible, or as soon as you create the ELF binary, you fix things in places and cannot be moved?

+74
linux linker elf
Dec 07
source share
4 answers

There is a chrpath tool that can do this - it is probably available in your distribution packages.

+66
Dec 07 '12 at 19:05
source share

There is a more universal tool than chrpath called patchelf . It was originally created for use in creating packages for Nix and NixOS (the GNU / Linux packaging system and distribution).

If rpath is missing from the binary (called rdsamp here), chrpath fails:

 chrpath -r '$ORIGIN/../lib64' rdsamp rdsamp: no rpath or runpath tag found. 

On the other hand,

 patchelf --set-rpath '$ORIGIN/../lib64' rdsamp 

succeeds just fine.

+133
Dec 02 '13 at 16:52
source share

As @ user7610 said, the right way is the patchelf tool.

But I feel that I can give a more complete answer, covering all the commands necessary to accomplish just that.

If you want to read a detailed explanation on this, you can find more information here .

First of all, many developers talk about RPATH , but in fact they mean RUNPATH . These are two different optional dynamic partitions, and the loader handles them in completely different ways. You can read more about the difference between the two in the link I mentioned earlier.

In the meantime, just remember:

  • If RUNPATH set, RPATH ignored.
  • RPATH deprecated and should be avoided.
  • RUNPATH is preferred because it can be overridden by LD_LIBRARY_PATH

See current R [UN] PATH

 readelf -d <path-to-elf> | egrep "RPATH|RUNPATH" 

Clear path R [UN]

 patchelf --remove-rpath <path-to-elf> 

Notes:

  • Removes both RPATH and RUNPATH

Add values ​​to R [UN] PATH

 patchelf [--force-rpath] --set-rpath "<desired-rpath>" <path-to-elf> 

Notes:

  • <desired-path> is a comma-separated list of directories, for example: /my/libs: /my/other/libs
  • If you specify --force-rpath , sets RPATH , otherwise sets RUNPATH
+4
Feb 16 '19 at 13:18
source share

This worked for me, replacing XORIGIN with $ ORIGIN.

chrpath -r '\$\ORIGIN/../lib64' httpd

0
Jun 28 '16 at 10:49
source share



All Articles