1

I want to load some dependent libraries from my program with dlopen function. Is it possible to know actual location of these libraries?

eg, ldd shows all dependent libraries with paths in system. How does it work? Is it possible to get paths to correspondent libraries I need to load with dlopen via some call from my C++ code?

dojo_user
  • 85
  • 10

1 Answers1

1

From man dlopen one can read:

The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):

  • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.

  • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)

  • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.

  • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.

  • The directories /lib and /usr/lib are searched (in that order).

So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.

Community
  • 1
  • 1
YSC
  • 38,212
  • 9
  • 96
  • 149
  • Suppose the program `node` has its own `DT_RUNPATH`, then when it uses `dlopen` to open a shared object which has its own `DT_RUNPATH`, does the dynamic loader search both `DT_RUNPATH`? Or does it only consider the `DT_RUNPATH` inside the shared object? What happens if the shared object itself doesn't have a `DT_RUNPATH`? – CMCDragonkai May 14 '22 at 07:28