linuxshared-librariesldd

Does ldd also show dependencies of dependencies?


I have a binary for which ldd shows an unexpected dependency und libicuuc (from "icu").

#ldd A
[...]
libxml2.so.2 => /usr/lib64/libxml2.so.2 (0x00007faaf9722000)
libicuuc.so.49 => /usr/lib64/libicuuc.so.49 (0x00007faaf5689000)

Since on this system libxml depends dynamically on libicuuc, it makes sense that ldd enventually ends up finding it, but is it expected that libicuuc also appears in the ldd output for A? Is there some command to retrieve only the libraries that are linked in as dependencies of dependencies?


Solution

  • Is there some command to retrieve only the libraries that are linked in as dependencies of dependencies?

    You can do set difference of ldd and readelf -d outputs.

    readelf -d shows only direct dependencies of an ELF file recorded in its dynamic section:

    $ readelf -d /bin/ssh | gawk '/NEEDED/ {++n; print} END {printf "---- %d lines\n", n}'
     0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]
     0x0000000000000001 (NEEDED)             Shared library: [libgssapi_krb5.so.2]
     0x0000000000000001 (NEEDED)             Shared library: [libcrypto.so.3]
     0x0000000000000001 (NEEDED)             Shared library: [libz.so.1]
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
    ---- 5 lines
    

    Whereas ldd shows all libraries that would be loaded when starting an ELF executable or loading a shared library, using the dependencies from the ELF dynamic section of each ELF file loaded, recursively:

    $ ldd /bin/ssh | gawk '{print} END {printf "---- %d lines\n", NR}'
        linux-vdso.so.1 (0x00007ffe3d552000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007a3ff7606000)
        libgssapi_krb5.so.2 => /lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007a3ff75b2000)
        libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x00007a3ff7000000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007a3ff7596000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007a3ff6c00000)
        libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007a3ff6f66000)
        /lib64/ld-linux-x86-64.so.2 (0x00007a3ff772d000)
        libkrb5.so.3 => /lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007a3ff6e9d000)
        libk5crypto.so.3 => /lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007a3ff7568000)
        libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007a3ff7562000)
        libkrb5support.so.0 => /lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007a3ff7555000)
        libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007a3ff754c000)
        libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007a3ff7539000)
    ---- 14 lines