4

I've read which equivalent for shared libraries and Where do executables look for shared objects at runtime, but is there a command to list all the directories in which the shared libraries will be searched?

Something like a command that auto-computes all the list explained in the second question.

mgarciaisaia
  • 252
  • 4
  • 9

4 Answers4

3

short: no

long: The related environment variables are system- and configuration-dependent. For a given system/configuration, you could write a script which does this.

Where do executables look for shared objects at runtime gives some insight, but is incomplete. It mentions OSX and Solaris, but focuses on Linux, pointing to two resources:

You would also find these useful:

  • ldconfig - configure dynamic linker run-time bindings, Linux-specific (neither OSX or Solaris):

    ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories, /lib and /usr/lib (on some 64-bit architectures such as x86-64, lib and /usr/lib are the trusted directories for 32-bit libraries, while /lib64 and /usr/lib64 are used for 64-bit libraries).

  • How do shared libraries work in a mixed 64bit/32bit system?
  • shlib script, a utility script in ncurses listing a variety of environment variables used for specifying where shared libraries are.

In particular, "sudo ldconfig -v"

-v, --verbose
Verbose mode. Print current version number, the name of each directory as it is scanned, and any links that are created. Overrides quiet mode.

which is close to what was asked, but gives lots of extraneous information. (And it is largely Linux-specific, though BSDs use it — but different, see manual page). If you make some assumptions about its output format, you could get directories from this using

sudo ldconfig -v 2>/dev/null | grep ':$' |sed -e 's/://'

which gives (on one system)

/usr/local/lib
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/lib
/usr/lib

To recap: there is no command, but you can make a script, which is system-dependent.

Thomas Dickey
  • 76,765
1

On a GNU system

ldconfig -p | tail -n +2 | grep -o '/.*/' | sort -u

Would give you the directories where dynamic libraries are found in the cache. You can add the ones in $LD_LIBRARY_PATH. Here with zsh syntax:

(
  ldconfig -p | tail -n +2 | grep -o '/.*/'
  [ -n "$LD_LIBRARY_PATH" ] && printf '%s/\n' ${${(s/:/)LD_LIBRARY_PATH}:A}
) | sort -u

Also note that at least on my x86_64 GNU system, and for 64 bit executables, for each of those directories, it also searches in $dir/x86_64, $dir/tls and $dir/tls/x86_64.

Also note that executables can hard code paths to libraries in them. Still with the GNU dynamic linker, you could use LD_DEBUG=libs to get the search paths, for instance with something like (here for 64bit ELF executables on my GNU/Linux system):

$ LD_DEBUG=libs /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 --inhibit-cache /bin/true 2>&1 |
   sed '/.*search path=/!d;s///;s/[[:blank:]]*([^)]*)$//' |
   tr : '\n' |
   sort -u
/home/stephane/lib
/home/stephane/lib/tls
/home/stephane/lib/tls/x86_64
/home/stephane/lib/x86_64
/lib
/lib/tls
/lib/tls/x86_64
/lib/x86_64
/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu/tls
/lib/x86_64-linux-gnu/tls/x86_64
/lib/x86_64-linux-gnu/x86_64
/usr/lib
/usr/lib/tls
/usr/lib/tls/x86_64
/usr/lib/x86_64
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/tls
/usr/lib/x86_64-linux-gnu/tls/x86_64
/usr/lib/x86_64-linux-gnu/x86_64
0

I am not aware of such a command. However, /etc/ld.so.conf and possibly all files under /etc/ld.so.conf.d/ plus whatever you find in $LD_LIBRARY_PATH yields the answer to the question where are shared libraries searched for?.

Caution: executables may define additional or alternative search paths.

You could also use ldd on all files in the search path in order to see where the dynamic linker actually searches. The following approach takes a while and is certainly not award winning:

ldd /bin/* /usr/bin/* /sbin/* /usr/sbin/* 2>/dev/null | grep '=>' | awk '{print $3;}' | xargs dirname | sort -u
countermode
  • 7,533
  • 5
  • 31
  • 58
-1

I'm not aware of a specific command that will list all shared libraries, but you could use a bit of a "fudge" workaround by just running

which <non-existent command/alias>

and look at the output such as:

[$]› which fake-command
/usr/bin/which: no fake-command in (/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

or find any directory ending in *bin

find / -name *bin -type d 

but that will more than likely pull too many results that won't be in a shared library

RobotJohnny
  • 1,039
  • 8
  • 18