androidlinuxlinkershared-librariesdynamic-linking

How can I change the name of prebuilt shared library so that the executables can link with the new name?


I have prebuilt shared library (libssl.so) and I need to rename to a different name before linking to my executable(wpa_supplicant). Reason: I don't want other executables from loading this library (because it has some custom code)

I can't modify the build rules for libssl.so in its Android.mk because neither I own that code, nor I can build it.

I tried writing an Android.bp file for including this prebuilt library with a different name(mylibssl.so) and used this new name as LOCAL_SHARED_LIBRARIES for my executable(wpa_supplicant)

But when I checked the dependencies of my executable(using readelf -d wpa_supplicant), it still points to libssl.so

 0x0000000000000001 (NEEDED)             Shared library: [libssl.so]

When we open the corresponding so.toc file from the build intermediates of the shared library, we see that the SONAME is still the original shared library's name and not the renamed shared library filename.

 0x000000000000000e (SONAME)             Library soname: [libssl.so]

Solution

  • First you need to understand how the SONAME propagates.

    A shared library can have an SONAME (baked into it by the linker when the shared library itself is linked), which may be different from the name of the shared library itself.

    When an executable is linked against e.g. libfoo.so which has SONAME libbar.so, the linker adds a NEEDED entry with the value of libbar.so (regardless of what the library itself is named).


    Since you can't rebuild libssl.so to update its SONAME, you must change the SONAME by some other method.

    Fortunately there is a tool which makes this trivial:

    mv libssl.so mylibssl.so
    patchelf --set-soname mylibssl.so mylibssl.so
    

    should do exactly what you want.