Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to change the SONAME of a binary directly?

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.

like image 966
user2721786 Avatar asked Aug 27 '13 13:08

user2721786


People also ask

What is Soname used for?

In Unix and Unix-like operating systems, a soname is a field of data in a shared object file. The soname is a string, which is used as a "logical name" describing the functionality of the object. Typically, that name is equal to the filename of the library, or to a prefix thereof, e.g. libc.

How Ld so works?

When a program linked with shared libraries runs, program execution does not immediately start with that program's first statement. Instead, the operating system loads and executes the dynamic linker (usually called ld.so), which then scans the list of library names embedded in the executable.

How does shared library work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.

What is a shared library C++?

A shared library is an object module that can be loaded at run time at an arbitrary memory address, and it can be linked to by a program in memory. Shared libraries often are called as shared objects. On most UNIX systems they are denoted with a . so suffix and Microsoft refer to them as DLLs (dynamic link libraries).


2 Answers

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
like image 200
Timmmm Avatar answered Oct 17 '22 00:10

Timmmm


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).

like image 26
Breno Leitão Avatar answered Oct 17 '22 01:10

Breno Leitão