2

Given the following library :

libfoo.{so,dll,dylib}:

const char* get_lib_path() {
    return ...;
}

built as a shared library (for instance with GCC) :

gcc -shared -fPIC foo.cpp -o libfoo.so

is there a way to get get_lib_path() to return, at runtime, the path where the library resides on the file system, whether on Windows, macOS, Linux ?

The final name of the library is not known : I can't assume a search of the string "libfoo.so" from the list of the loaded libraries.

This must not require the native handle to the dynamically loaded object: I want to know the path from inside my library, and I do not have control over the application that loads it (so I can't get the dlopen handle for instance).

Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
  • 2
    Possible duplicate of [Find location of loaded shared library, from in that shared library?](http://stackoverflow.com/questions/43409167/find-location-of-loaded-shared-library-from-in-that-shared-library) – yugr May 14 '17 at 14:31
  • mhh.. I don't know under which name the library will be so I can't use the solution in there. – Jean-Michaël Celerier May 14 '17 at 14:32
  • You can, just use any address inside the library to identify the module. This would work both for `dl_iterate_phdr` and `/proc/self/maps`. – yugr May 14 '17 at 14:33
  • how would this work with win32? – Jean-Michaël Celerier May 14 '17 at 14:34
  • Windows has similar APIs, check e.g. [here](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682621(v=vs.85).aspx). – yugr May 14 '17 at 14:36
  • A mechanism can be : https://stackoverflow.com/questions/1681060/library-path-when-dynamically-loaded – giorgiomugnaini Sep 03 '18 at 10:08
  • I have added an answer at https://stackoverflow.com/a/57201397/1005215 which uses `dladdr` – Nehal J Wani Jul 25 '19 at 11:55
  • 1
    I wrote a library that uses the /proc/self/maps file to get the file path for any memory address. The 'whereami" function returns the file path to the executable/library containing the caller https://github.com/blackle/whereami – Blackle Mori Nov 05 '21 at 05:03
  • 2
    @BlackleMori I think that the name conflicts a bit with https://github.com/gpakosz/whereami no ? – Jean-Michaël Celerier Nov 05 '21 at 10:20
  • 1
    @Jean-MichaëlCelerier wow! I had no idea this existed! what a wild coincidence that we named it the same thing. It looks like this supports many more platforms than mine. Probably best to use this library than the one I made. – Blackle Mori Nov 05 '21 at 23:42

2 Answers2

2

On Windows, you can use GetModuleFilename or even easier GetMappedFileName. The latter takes any address inside the library.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
-4

I would say no. Technically it does not matter where the library code was loaded from. Once in memory, all that is known are things like the symbols that are being referred to. The original library file is no longer relevant once the code is in memory.

Cheatah
  • 1,825
  • 2
  • 13
  • 21
  • > The original library file is no longer relevant once the code is in memory. In my case it is relevant: I want to ship a library and some data files with it, in the same folder. I want to find the path to the library so that I can find the path to my data files. – Jean-Michaël Celerier May 14 '17 at 14:35
  • 1
    That is not how a shared library should work. You either ship the data in your library, or you put functionality in a library that allows someone to pass a search path. – Cheatah May 14 '17 at 21:38
  • On Windows, the original library file is still relevant, because the code in memory is not a copy, it is a memory-mapped file. – Ben Voigt May 04 '19 at 14:41