PatchELF: Difference between revisions
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
What if an application binary app uses glibc version 2.22 and you need to configure it to use glibc 2.25 '''without recompiling the binary ? PatchELF comes to play with it.''' | What if an application binary app uses glibc version 2.22 and you need to configure it to use glibc 2.25 '''without recompiling the binary ? PatchELF comes to play with it.''' | ||
PatchELF is a simple utility for modifying existing ELF executables and libraries. In particular, it can do the following: . * Change the dynamic loader ("ELF interpreter") of executables * Change the RPATH of executables and libraries * Remove declared dependencies on dynamic libraries (DT_NEEDED entries) | PatchELF is a simple utility for modifying existing ELF executables and libraries. In particular, it can do the following: . * Change the dynamic loader ("ELF interpreter") of executables * Change the [[RPATH]] of executables and libraries * Remove declared dependencies on dynamic libraries (DT_NEEDED entries) | ||
=== Install patchelf is simple on Ubuntu === | === Install patchelf is simple on Ubuntu === | ||
Line 7: | Line 7: | ||
sudo apt install patchelf | sudo apt install patchelf | ||
=== Change the dynamic loader ("ELF interpreter") of executables:<ref>https://github.com/miko-ai/patchelf</ref> === | === Change the dynamic loader ("ELF interpreter") of executables:<ref>https://github.com/miko-ai/patchelf</ref> <ref>https://github.com/NixOS/patchelf</ref> === | ||
<blockquote><code>$ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program</code></blockquote> | <blockquote><code>$ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program</code></blockquote> | ||
Line 14: | Line 14: | ||
=== Shrink the RPATH of executables and libraries: === | === Shrink the RPATH of executables and libraries: === | ||
<blockquote>$ patchelf --shrink-rpath my-program</blockquote>This removes from the RPATH all directories that do not contain a [[library]] referenced by DT_NEEDED fields of the executable or library. For instance, if an executable references one library libfoo.so, has an RPATH /lib:/usr/lib:/foo/lib, and libfoo.so can only be found in /foo/lib, then the new RPATH will be /foo/lib. | <blockquote>$ [[patchelf]] --shrink-rpath my-program</blockquote>This removes from the RPATH all directories that do not contain a [[library]] referenced by DT_NEEDED fields of the executable or library. For instance, if an executable references one library libfoo.so, has an RPATH /lib:/usr/lib:/foo/lib, and libfoo.so can only be found in /foo/lib, then the new RPATH will be /foo/lib. | ||
In addition, the --allowed-rpath-prefixes option can be used for further rpath tuning. For instance, if an executable has an RPATH /tmp/build-foo/.libs:/foo/lib, it is probably desirable to keep the /foo/lib reference instead of the /tmp entry. To accomplish that, use:<blockquote>$ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program</blockquote> | In addition, the --allowed-rpath-prefixes option can be used for further rpath tuning. For instance, if an executable has an RPATH /tmp/build-foo/.libs:/foo/lib, it is probably desirable to keep the /foo/lib reference instead of the /tmp entry. To accomplish that, use:<blockquote>$ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program</blockquote> |
Latest revision as of 09:34, 23 December 2023
What if an application binary app uses glibc version 2.22 and you need to configure it to use glibc 2.25 without recompiling the binary ? PatchELF comes to play with it.
PatchELF is a simple utility for modifying existing ELF executables and libraries. In particular, it can do the following: . * Change the dynamic loader ("ELF interpreter") of executables * Change the RPATH of executables and libraries * Remove declared dependencies on dynamic libraries (DT_NEEDED entries)
Install patchelf is simple on Ubuntu
sudo apt update sudo apt install patchelf
Change the dynamic loader ("ELF interpreter") of executables:[1] [2]
$ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program
Change the RPATH of executables and libraries:
$ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program
Shrink the RPATH of executables and libraries:
$ patchelf --shrink-rpath my-program
This removes from the RPATH all directories that do not contain a library referenced by DT_NEEDED fields of the executable or library. For instance, if an executable references one library libfoo.so, has an RPATH /lib:/usr/lib:/foo/lib, and libfoo.so can only be found in /foo/lib, then the new RPATH will be /foo/lib. In addition, the --allowed-rpath-prefixes option can be used for further rpath tuning. For instance, if an executable has an RPATH /tmp/build-foo/.libs:/foo/lib, it is probably desirable to keep the /foo/lib reference instead of the /tmp entry. To accomplish that, use:
$ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program
Remove declared dependencies on dynamic libraries (DT_NEEDED entries):
$ patchelf --remove-needed libfoo.so.1 my-program
This option can be given multiple times.
Add a declared dependency on a dynamic library (DT_NEEDED):
$ patchelf --add-needed libfoo.so.1 my-program
This option can be give multiple times.
Replace a declared dependency on a dynamic library with another one (DT_NEEDED):
$ patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program
This option can be give multiple times.
Change SONAME of a dynamic library:
$ patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3
Reference
https://manpages.ubuntu.com/manpages/bionic/man1/patchelf.1.html