20

My program depends on libcurl.so.3, but in RHEL6 there is no symbolic link libcurl.so.3 ⇾ libcurl.so.4 (my program can run smoothly when I create this link). However, there is symbolic link libcurl.so ⇾ libcurl.so.4.

I would like to modify the SONAME embedded in libcurl.so.3.0.0.0 file from libcurl.so.3 to libcurl.so so that I could run my program on RHEL 6 without creating a symbolic link.

My solution could not be optimal but I think learning how to modify the binary directly is valuable.

$readelf -d libcurl.so.3.0.0 

Dynamic segment at offset 0x303cc contains 25 entries:

  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libssl.so.2]
 0x0000000e (SONAME)                     Library soname: [libcurl.so.3]

I would like to change libcurl.so.3 above to libcurl.so.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
user2721786
  • 234
  • 1
  • 2
  • 7
  • What you're talking about is *renaming* a file or a symlink, it's not *modifying* a binary. – devnull Aug 27 '13 at 13:54
  • 1
    You could create a copy of "libcurl.so.4", rename it to "libcurl.so.3" and use a hexadecimal editor to rename the SONAME entry from "libcurl.so.4" to "libcurl.so.3". Often this will not work because version information is also present in dynamic libraries. – Martin Rosenau Aug 27 '13 at 20:49
  • I think the easiest way to have that is to rebuild the libcurl and hack a bit the build rules to have the SONAME you want. For post build changes, I would not advice binary edition. There is a project about patching the ELF format http://nixos.org/patchelf.html, it does not allow to change the SONAME, but it can provide some idea on how to implement such a thing. – joetde Mar 28 '14 at 12:12

2 Answers2

27

Yes, you can use patchelf like this (from its Readme):

patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • After using patchelf -set-soname strip on the so now fails: strip: src/.libs/st1B6Gj2: Not enough room for program headers, try linking with -N strip:src/.libs/st1B6Gj2[.note.gnu.build-id]: Bad value – Tom Jul 05 '19 at 16:57
1

You should avoid removing the version of the SO object, as for example when your application depends on a specific libc (libc.so.6).

The proper way to do it, if you want to use another lib is using the LD_PRELOAD variable before calling your application

If you set LD_PRELOAD to the path of the new file, that file will be loaded before any other library (including even C runtime, libc.so).

Breno Leitão
  • 3,487
  • 2
  • 19
  • 23
  • It is not about source (library), it is about target (executable) where required SONAME is specified. If you don't have required library, how does `LD_PRELOAD` help? – uvsmtid Feb 08 '22 at 06:44