Tool for modifying dynamic section of an ELF binary

replace path to existing library with a custom path

If this is your own library, then you probably linking it like that:

$ cc -o prog1 -l/full/path/to/libABC.so prog1.o

instead of the proper:

$ cc -o prog1 -L/full/path/to/ -lABC prog1.o

The first approach tells Linux linker that application needs precisely that library, only that library and no override should be possible. Second approach tells that application needs the library which would be installed somewhere on the system, either in the default library path or one pointed by the $LD_LIBRARY_PATH (would be looked up during run-time). -L is used only during link-time.

Otherwise, instead of patching the ELF, first check if you can substitute the library using a symlink. This is the usual trick: it is hard to modify executable afterward, but it is very easy to change where to the symlink points.


patchelf is what you want

$ patchelf --replace-needed LIB_ORIGIN  LIB_NEW  ELF_FILE

To see the effect

$ readelf -d ELF_FILE

Install the tools is easy:

$ sudo apt-get install patchelf readelf

Tags:

Linux

C

Elf