vitakot vitakot - 2 months ago 6
Linux Question

Check loaded shared libraries from another shared library on Linux/Android

Is it possible to check which shared libraries are loaded by the calling process from another shared library (.so)? I know there are command line tools for that, but is it possible to perform such a check in C++ code?

I need to somehow get the list of native shared libraries loaded by an Android application, but it doesn't seem to be possible from a Java code.

Answer

You could use /proc/<pid>/maps file or just /proc/self/maps for the calling process: lines that ends with '.so' are for linked shared libraries. It's a hack, but should work. Note that one library could be mapped multiple times so you need to skip the repetitions.

And good news: you could do it from java. The code snippet below prints shared libraries for the current process to the logcat.

try {
    Set<String> libs = new HashSet<String>();
    String mapsFile = "/proc/self/maps";
    BufferedReader reader = new BufferedReader(new FileReader(mapsFile));
    String line;
    while ((line = reader.readLine()) != null) {
        if (line.endsWith(".so")) {
            int n = line.lastIndexOf(" ");
            libs.add(line.substring(n + 1));
        }
    }
    Log.d("Ldd", libs.size() + " libraries:");
    for (String lib : libs) {
        Log.d("Ldd", lib);
    }
} catch (FileNotFoundException e) {
    // Do some error handling...
} catch (IOException e) {
    // Do some error handling...
}

The output on my device is:

D/Ldd     (11286): 55 libraries:
D/Ldd     (11286): /system/lib/libc.so
D/Ldd     (11286): /system/lib/libdbus.so
D/Ldd     (11286): /system/lib/librpc.so
D/Ldd     (11286): /system/lib/libEGL.so
D/Ldd     (11286): /system/lib/libstagefright_color_conversion.so
D/Ldd     (11286): /system/lib/libmedia.so
D/Ldd     (11286): /system/lib/libemoji.so
D/Ldd     (11286): /system/lib/libcrypto.so
D/Ldd     (11286): /system/lib/libstagefright_avc_common.so
D/Ldd     (11286): /system/lib/libnativehelper.so
D/Ldd     (11286): /system/lib/libskiagl.so
D/Ldd     (11286): /system/lib/libopencore_player.so
D/Ldd     (11286): /system/lib/libjpeg.so
D/Ldd     (11286): /system/lib/libsurfaceflinger_client.so
D/Ldd     (11286): /system/lib/libstagefright.so
D/Ldd     (11286): /system/lib/libdrm1.so
D/Ldd     (11286): /system/lib/libdvm.so
D/Ldd     (11286): /system/lib/libwebcore.so
D/Ldd     (11286): /system/lib/libGLESv1_CM.so
D/Ldd     (11286): /system/lib/libhardware.so
D/Ldd     (11286): /system/lib/libexif.so
D/Ldd     (11286): /system/lib/libgps.so
D/Ldd     (11286): /system/lib/liblog.so
D/Ldd     (11286): /system/lib/libexpat.so
D/Ldd     (11286): /system/lib/libopencore_common.so
D/Ldd     (11286): /system/lib/libbluedroid.so
D/Ldd     (11286): /system/lib/libm.so
D/Ldd     (11286): /system/lib/libicui18n.so
D/Ldd     (11286): /system/lib/libomx_amrenc_sharedlibrary.so
D/Ldd     (11286): /system/lib/libwpa_client.so
D/Ldd     (11286): /system/lib/libstdc++.so
D/Ldd     (11286): /system/lib/libandroid_runtime.so
D/Ldd     (11286): /system/lib/libz.so
D/Ldd     (11286): /system/lib/libETC1.so
D/Ldd     (11286): /system/lib/libsonivox.so
D/Ldd     (11286): /system/lib/libstlport.so
D/Ldd     (11286): /system/lib/libutils.so
D/Ldd     (11286): /system/lib/libicudata.so
D/Ldd     (11286): /system/lib/libsqlite.so
D/Ldd     (11286): /system/lib/libhardware_legacy.so
D/Ldd     (11286): /system/lib/libpixelflinger.so
D/Ldd     (11286): /system/lib/libvorbisidec.so
D/Ldd     (11286): /system/lib/libstagefright_amrnb_common.so
D/Ldd     (11286): /system/lib/libcutils.so
D/Ldd     (11286): /system/lib/libui.so
D/Ldd     (11286): /system/lib/libmedia_jni.so
D/Ldd     (11286): /system/lib/libomx_sharedlibrary.so
D/Ldd     (11286): /system/lib/libcamera_client.so
D/Ldd     (11286): /system/lib/libskia.so
D/Ldd     (11286): /system/lib/libopencore_net_support.so
D/Ldd     (11286): /system/lib/libnetutils.so
D/Ldd     (11286): /system/lib/libbinder.so
D/Ldd     (11286): /system/lib/libssl.so
D/Ldd     (11286): /system/lib/libicuuc.so
D/Ldd     (11286): /system/lib/libGLESv2.so

Also if necessary pid could be obtained by android.os.Process.myPid().