Patchelf

From Leo's Notes
Last edited on 18 February 2022, at 05:50.

Patchelf is a utility to modify an existing ELF binary. It can be used to change, add, or remove dynamic libraries.

Example usage

If a library is missing a dependency, it would fail to run due to a missing symbol. What you can do is inject the missing dependency to the library so that when the code is executed, the dynamic loader is able to pull in the necessary library. For example:

# ldd /usr/lib/x86_64-linux-gnu/liblistSerialsj.so.1.4.0 
	linux-vdso.so.1 (0x00007ffcb89cb000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c17a4a000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f5c17e88000)

We can then inject the missing library in with patchelf:

# patchelf --add-needed /usr/lib/x86_64-linux-gnu/libserialport.so.0 /usr/lib/x86_64-linux-gnu/liblistSerialsj.so.1.4.0

# ldd /usr/lib/x86_64-linux-gnu/liblistSerialsj.so.1.4.0 
	linux-vdso.so.1 (0x00007ffc4ec8c000)
	/usr/lib/x86_64-linux-gnu/libserialport.so.0 (0x00007fccbe15c000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fccbdf23000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fccbe374000)