<p>I'd like to know which libraries are used by executables on my system. More specifically, I'd like to rank which libraries are used the most, along with the binaries that use them. How can I do this?</p>

[{'answer_id': 50166, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 6, 'selected': False, 'text': "<p>to learn what libraries a binary uses, use ldd</p>\n\n<pre><code>ldd path/to/the/tool\n</code></pre>\n\n<p>You'd have to write a little shell script to get to your system-wide breakdown.</p>\n"}, {'answer_id': 50218, 'author': 'John Vasileff', 'author_id': 5076, 'author_profile': 'https://Stackoverflow.com/users/5076', 'pm_score': 9, 'selected': True, 'text': '<ol>\n<li>Use <code>ldd</code> to list shared libraries for each executable.</li>\n<li>Cleanup the output</li>\n<li>Sort, compute counts, sort by count</li>\n</ol>\n\n<p>To find the answer for all executables in the "/bin" directory:</p>\n\n<pre><code>find /bin -type f -perm /a+x -exec ldd {} \\; \\\n| grep so \\\n| sed -e \'/^[^\\t]/ d\' \\\n| sed -e \'s/\\t//\' \\\n| sed -e \'s/.*=..//\' \\\n| sed -e \'s/ (0.*)//\' \\\n| sort \\\n| uniq -c \\\n| sort -n\n</code></pre>\n\n<p>Change "/bin" above to "/" to search all directories.</p>\n\n<p>Output (for just the /bin directory) will look something like this:</p>\n\n<pre><code>  1 /lib64/libexpat.so.0\n  1 /lib64/libgcc_s.so.1\n  1 /lib64/libnsl.so.1\n  1 /lib64/libpcre.so.0\n  1 /lib64/libproc-3.2.7.so\n  1 /usr/lib64/libbeecrypt.so.6\n  1 /usr/lib64/libbz2.so.1\n  1 /usr/lib64/libelf.so.1\n  1 /usr/lib64/libpopt.so.0\n  1 /usr/lib64/librpm-4.4.so\n  1 /usr/lib64/librpmdb-4.4.so\n  1 /usr/lib64/librpmio-4.4.so\n  1 /usr/lib64/libsqlite3.so.0\n  1 /usr/lib64/libstdc++.so.6\n  1 /usr/lib64/libz.so.1\n  2 /lib64/libasound.so.2\n  2 /lib64/libblkid.so.1\n  2 /lib64/libdevmapper.so.1.02\n  2 /lib64/libpam_misc.so.0\n  2 /lib64/libpam.so.0\n  2 /lib64/libuuid.so.1\n  3 /lib64/libaudit.so.0\n  3 /lib64/libcrypt.so.1\n  3 /lib64/libdbus-1.so.3\n  4 /lib64/libresolv.so.2\n  4 /lib64/libtermcap.so.2\n  5 /lib64/libacl.so.1\n  5 /lib64/libattr.so.1\n  5 /lib64/libcap.so.1\n  6 /lib64/librt.so.1\n  7 /lib64/libm.so.6\n  9 /lib64/libpthread.so.0\n 13 /lib64/libselinux.so.1\n 13 /lib64/libsepol.so.1\n 22 /lib64/libdl.so.2\n 83 /lib64/ld-linux-x86-64.so.2\n 83 /lib64/libc.so.6\n</code></pre>\n\n<p>Edit - Removed "grep -P"</p>\n'}, {'answer_id': 50245, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 2, 'selected': False, 'text': '<p>With <code>ldd</code> you can get the libraries that tools use. To rank the usage of libraries for a set of tool you can use something like the following command.</p>\n\n<pre><code>ldd /bin/* /usr/bin/* ... | sed -e \'/^[^\\t]/ d; s/^\\t\\(.* =&gt; \\)\\?\\([^ ]*\\) (.*/\\2/g\' | sort | uniq -c\n</code></pre>\n\n<p>(Here <code>sed</code> strips all lines that do not start with a tab and the filters out only the actual libraries. With <code>sort | uniq -c</code> you get each library with a count indicating the number of times it occurred.)</p>\n\n<p>You might want to add <code>sort -g</code> at the end to get the libraries in order of usage.</p>\n\n<p>Note that you probably get lines two non-library lines with the above command. One of static executables ("not a dynamic executable") and one without any library. The latter is the result of <code>linux-gate.so.1</code> which is not a library in your file system but one "supplied" by the kernel.</p>\n'}, {'answer_id': 5727326, 'author': 'Raghwendra', 'author_id': 716714, 'author_profile': 'https://Stackoverflow.com/users/716714', 'pm_score': 3, 'selected': False, 'text': '<p>On UNIX system, suppose binary (executable) name is test. Then we use the following command to list the libraries used in the test is</p>\n\n<pre><code>ldd test\n</code></pre>\n'}, {'answer_id': 8176075, 'author': 'Fabiano Tarlao', 'author_id': 1052899, 'author_profile': 'https://Stackoverflow.com/users/1052899', 'pm_score': 6, 'selected': False, 'text': '<p>On Linux I use:</p>\n\n<pre><code>lsof -P -T -p Application_PID\n</code></pre>\n\n<p>This works better than <code>ldd</code> when the executable uses a <a href="https://stackoverflow.com/questions/10253170/checking-shared-libraries-for-non-default-loaders">non default loader</a></p>\n'}, {'answer_id': 15520982, 'author': 'smichak', 'author_id': 329855, 'author_profile': 'https://Stackoverflow.com/users/329855', 'pm_score': 6, 'selected': False, 'text': "<p>I didn't have ldd on my ARM toolchain so I used objdump:</p>\n\n<p>$(CROSS_COMPILE)objdump -p </p>\n\n<p>For instance:</p>\n\n<pre><code>objdump -p /usr/bin/python:\n\nDynamic Section:\n  NEEDED               libpthread.so.0\n  NEEDED               libdl.so.2\n  NEEDED               libutil.so.1\n  NEEDED               libssl.so.1.0.0\n  NEEDED               libcrypto.so.1.0.0\n  NEEDED               libz.so.1\n  NEEDED               libm.so.6\n  NEEDED               libc.so.6\n  INIT                 0x0000000000416a98\n  FINI                 0x000000000053c058\n  GNU_HASH             0x0000000000400298\n  STRTAB               0x000000000040c858\n  SYMTAB               0x0000000000402aa8\n  STRSZ                0x0000000000006cdb\n  SYMENT               0x0000000000000018\n  DEBUG                0x0000000000000000\n  PLTGOT               0x0000000000832fe8\n  PLTRELSZ             0x0000000000002688\n  PLTREL               0x0000000000000007\n  JMPREL               0x0000000000414410\n  RELA                 0x0000000000414398\n  RELASZ               0x0000000000000078\n  RELAENT              0x0000000000000018\n  VERNEED              0x0000000000414258\n  VERNEEDNUM           0x0000000000000008\n  VERSYM               0x0000000000413534\n</code></pre>\n"}, {'answer_id': 28161306, 'author': 'bluebadge', 'author_id': 3889589, 'author_profile': 'https://Stackoverflow.com/users/3889589', 'pm_score': 3, 'selected': False, 'text': '<p>On OS X by default there is no <code>ldd</code>, <code>objdump</code> or <code>lsof</code>. As an alternative, try <code>otool -L</code>:</p>\n\n<pre><code>$ otool -L `which openssl`\n/usr/bin/openssl:\n    /usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)\n    /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)\n    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)\n</code></pre>\n\n<p>In this example, using <code>which openssl</code> fills in the fully qualified path for the given executable and current user environment.</p>\n'}, {'answer_id': 29838991, 'author': 'kayle', 'author_id': 2536603, 'author_profile': 'https://Stackoverflow.com/users/2536603', 'pm_score': 4, 'selected': False, 'text': '<p>Check shared library dependencies of a program executable</p>\n\n<p>To find out what libraries a particular executable depends on, you can use ldd command. This command invokes dynamic linker to find out library dependencies of an executable.</p>\n\n<p><strong>> $ ldd /path/to/program</strong></p>\n\n<p>Note that it is NOT recommended to run ldd with any untrusted third-party executable because some versions of ldd may directly invoke the executable to identify its library dependencies, which can be security risk.</p>\n\n<p>Instead, a safer way to show library dependencies of an unknown application binary is to use the following command.</p>\n\n<blockquote>\n  <p><strong>$ objdump -p /path/to/program | grep NEEDED</strong></p>\n</blockquote>\n\n<p><a href="http://ask.xmodulo.com/check-library-dependency-program-process-linux.html" rel="noreferrer">for more info</a></p>\n'}, {'answer_id': 43026880, 'author': 'Ciro Santilli OurBigBook.com', 'author_id': 895245, 'author_profile': 'https://Stackoverflow.com/users/895245', 'pm_score': 4, 'selected': False, 'text': '<p><strong><code>readelf -d</code> recursion</strong></p>\n\n<p><code>redelf -d</code> produces similar output to <code>objdump -p</code> which was mentioned at: <a href="https://stackoverflow.com/a/15520982/895245">https://stackoverflow.com/a/15520982/895245</a></p>\n\n<p>But beware that dynamic libraries can depend on other dynamic libraries, to you have to recurse. </p>\n\n<p>Example:</p>\n\n<pre><code>readelf -d /bin/ls | grep \'NEEDED\'\n</code></pre>\n\n<p>Sample ouptut:</p>\n\n<pre><code> 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]\n 0x0000000000000001 (NEEDED)             Shared library: [libacl.so.1]\n 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]\n</code></pre>\n\n<p>Then:</p>\n\n<pre><code>$ locate libselinux.so.1\n/lib/i386-linux-gnu/libselinux.so.1\n/lib/x86_64-linux-gnu/libselinux.so.1\n/mnt/debootstrap/lib/x86_64-linux-gnu/libselinux.so.1\n</code></pre>\n\n<p>Choose one, and repeat:</p>\n\n<pre><code>readelf -d /lib/x86_64-linux-gnu/libselinux.so.1 | grep \'NEEDED\'\n</code></pre>\n\n<p>Sample output:</p>\n\n<pre><code>0x0000000000000001 (NEEDED)             Shared library: [libpcre.so.3]\n0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]\n0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]\n0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]\n</code></pre>\n\n<p>And so on.</p>\n\n<p><strong><code>/proc/&lt;pid&gt;/maps</code> for running processes</strong></p>\n\n<p>This is useful to find all the libraries currently being used by running executables. E.g.:</p>\n\n<pre><code>sudo awk \'/\\.so/{print $6}\' /proc/1/maps | sort -u\n</code></pre>\n\n<p>shows all currently loaded dynamic dependencies of <code>init</code> (PID <code>1</code>):</p>\n\n<pre><code>/lib/x86_64-linux-gnu/ld-2.23.so\n/lib/x86_64-linux-gnu/libapparmor.so.1.4.0\n/lib/x86_64-linux-gnu/libaudit.so.1.0.0\n/lib/x86_64-linux-gnu/libblkid.so.1.1.0\n/lib/x86_64-linux-gnu/libc-2.23.so\n/lib/x86_64-linux-gnu/libcap.so.2.24\n/lib/x86_64-linux-gnu/libdl-2.23.so\n/lib/x86_64-linux-gnu/libkmod.so.2.3.0\n/lib/x86_64-linux-gnu/libmount.so.1.1.0\n/lib/x86_64-linux-gnu/libpam.so.0.83.1\n/lib/x86_64-linux-gnu/libpcre.so.3.13.2\n/lib/x86_64-linux-gnu/libpthread-2.23.so\n/lib/x86_64-linux-gnu/librt-2.23.so\n/lib/x86_64-linux-gnu/libseccomp.so.2.2.3\n/lib/x86_64-linux-gnu/libselinux.so.1\n/lib/x86_64-linux-gnu/libuuid.so.1.3.0\n</code></pre>\n\n<p>This method also shows libraries opened with <code>dlopen</code>, tested with <a href="https://github.com/cirosantilli/cpp-cheat/blob/71ab01c5de024d9d97f0e2f0698cbee6c6a9c87a/shared-library/basic/dlopen.c#L16" rel="noreferrer">this minimal setup</a> hacked up with a <code>sleep(1000)</code> on Ubuntu 18.04.</p>\n\n<p>See also: <a href="https://superuser.com/questions/310199/see-currently-loaded-shared-objects-in-linux/1243089">https://superuser.com/questions/310199/see-currently-loaded-shared-objects-in-linux/1243089</a></p>\n'}, {'answer_id': 43207568, 'author': 'Shimon Doodkin', 'author_id': 466363, 'author_profile': 'https://Stackoverflow.com/users/466363', 'pm_score': 2, 'selected': False, 'text': '<p>on ubuntu\nprint packages related to an executable</p>\n\n<pre><code>ldd executable_name|awk \'{print $3}\'|xargs dpkg -S |awk -F  ":"  \'{print $1}\'\n</code></pre>\n'}, {'answer_id': 46832161, 'author': 'Anders Domeij', 'author_id': 8801700, 'author_profile': 'https://Stackoverflow.com/users/8801700', 'pm_score': 0, 'selected': False, 'text': '<p>I found this post very helpful as I needed to investigate dependencies from a 3rd party supplied library (32 vs 64 bit execution path(s)).</p>\n\n<p>I put together a Q&amp;D recursing bash script based on the \'readelf -d\' suggestion on a RHEL 6 distro.</p>\n\n<p>It is very basic and will test every dependency every time even if it might have been tested before (i.e very verbose). Output is very basic too.</p>\n\n<pre><code>#! /bin/bash\n\nrecurse ()\n# Param 1 is the nuumber of spaces that the output will be prepended with\n# Param 2 full path to library\n{\n#Use \'readelf -d\' to find dependencies\ndependencies=$(readelf -d ${2} | grep NEEDED | awk \'{ print $5 }\' | tr -d \'[]\')\nfor d in $dependencies; do\n   echo "${1}${d}"\n   nm=${d##*/}\n   #libstdc++ hack for the \'+\'-s\n   nm1=${nm//"+"/"\\+"}\n   # /lib /lib64 /usr/lib and /usr/lib are searched\n   children=$(locate ${d} | grep -E "(^/(lib|lib64|usr/lib|usr/lib64)/${nm1})")\n   rc=$?\n   #at least locate... didn\'t fail\n   if [ ${rc} == "0" ] ; then\n      #we have at least one dependency\n      if [ ${#children[@]} -gt 0 ]; then\n         #check the dependeny\'s dependencies\n         for c in $children; do\n          recurse "  ${1}" ${c}\n         done\n      else\n         echo "${1}no children found"\n      fi\n   else\n      echo "${1}locate failed for ${d}"\n   fi\ndone\n}\n# Q&amp;D -- recurse needs 2 params could/should be supplied from cmdline\nrecurse "" !!full path to library you want to investigate!!\n</code></pre>\n\n<p>redirect the output to a file and grep for \'found\' or \'failed\'</p>\n\n<p>Use and modify, at your own risk of course, as you wish.</p>\n'}, {'answer_id': 53554914, 'author': 'SoSen', 'author_id': 7098915, 'author_profile': 'https://Stackoverflow.com/users/7098915', 'pm_score': 2, 'selected': False, 'text': '<p>One more option can be just read the file located at</p>\n\n<pre><code>/proc/&lt;pid&gt;/maps\n</code></pre>\n\n<p>For example is the process id is 2601 then the command is</p>\n\n<pre><code>cat /proc/2601/maps\n</code></pre>\n\n<p>And the output is like</p>\n\n<pre><code>7fb37a8f2000-7fb37a8f4000 r-xp 00000000 08:06 4065647                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/network_networkmanager.so\n7fb37a8f4000-7fb37aaf3000 ---p 00002000 08:06 4065647                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/network_networkmanager.so\n7fb37aaf3000-7fb37aaf4000 r--p 00001000 08:06 4065647                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/network_networkmanager.so\n7fb37aaf4000-7fb37aaf5000 rw-p 00002000 08:06 4065647                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/network_networkmanager.so\n7fb37aaf5000-7fb37aafe000 r-xp 00000000 08:06 4065646                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/config_gnome3.so\n7fb37aafe000-7fb37acfd000 ---p 00009000 08:06 4065646                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/config_gnome3.so\n7fb37acfd000-7fb37acfe000 r--p 00008000 08:06 4065646                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/config_gnome3.so\n7fb37acfe000-7fb37acff000 rw-p 00009000 08:06 4065646                    /usr/lib/x86_64-linux-gnu/libproxy/0.4.15/modules/config_gnome3.so\n7fb37acff000-7fb37ad1d000 r-xp 00000000 08:06 3416761                    /usr/lib/x86_64-linux-gnu/libproxy.so.1.0.0\n7fb37ad1d000-7fb37af1d000 ---p 0001e000 08:06 3416761                    /usr/lib/x86_64-linux-gnu/libproxy.so.1.0.0\n7fb37af1d000-7fb37af1e000 r--p 0001e000 08:06 3416761                    /usr/lib/x86_64-linux-gnu/libproxy.so.1.0.0\n7fb37af1e000-7fb37af1f000 rw-p 0001f000 08:06 3416761                    /usr/lib/x86_64-linux-gnu/libproxy.so.1.0.0\n7fb37af1f000-7fb37af21000 r-xp 00000000 08:06 4065186                    /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so\n7fb37af21000-7fb37b121000 ---p 00002000 08:06 4065186                    /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so\n7fb37b121000-7fb37b122000 r--p 00002000 08:06 4065186                    /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so\n7fb37b122000-7fb37b123000 rw-p 00003000 08:06 4065186                    /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so\n</code></pre>\n'}, {'answer_id': 63699024, 'author': 'nomad', 'author_id': 12207627, 'author_profile': 'https://Stackoverflow.com/users/12207627', 'pm_score': 2, 'selected': False, 'text': "<p>If you don't care about the path to the executable file -</p>\n<pre><code>ldd `which &lt;executable&gt;` # back quotes, not single quotes\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/50159', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3756/']