Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change 'rpath' in an already compiled binary?

Tags:

linux

linker

elf

People also ask

How do I change my Runpath?

We can change RPATH or RUNPATH of a binary file by using the chrpath tool. Note that you cannot convert a RUNPATH back to RPATH. Note that the string of the new set of paths should be smaller or equal to the length of what was stored earlier in the binary.

How do I specify an Rpath?

To create an RPATH entry, you simply tell it to the linker (not the compiler!), so for instance you could add to your ./configure call the string LDFLAGS=-Wl,-rpath,/my/software/base/my/lib/. libs to build and run against a specific version of your library.

What is Rpath origin?

What is RPATH and $ORIGIN. RPATH stands for run-time search path. According to Wikipedia, “rpath designates the run-time search path hard-coded in an executable file or library.

What is Rpath in GCC?

In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries. Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library).


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

In case there is no rpath in a binary (here called rdsamp), 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.


There is a tool called chrpath which can do this - it's probably available in your distribution's packages.


Just like @user7610 said, the right way to go is the patchelf tool.

But, I feel that I can give a more comprehensive answer, covering all the commands one needs to do exactly that.

For a comprehensive article on the subject, click here

First of all, many developers talk about RPATH, but they actually mean RUNPATH. These are two different optional dynamic sections, and the loader handles them very differently. You can read more about the difference between them in the link I mentioned before.

For now, just remember:

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

See the current R[UN]PATH

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

Clear the R[UN]PATH

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 colon separated directories list, e.g: /my/libs:/my/other/libs
  • If you specify --force-rpath, sets RPATH, otherwise sets RUNPATH