Recent

Author Topic: Get the shared library's fully qualified path in Linux  (Read 2306 times)

c600g

  • New Member
  • *
  • Posts: 45
Get the shared library's fully qualified path in Linux
« on: July 17, 2023, 08:02:04 pm »
In a FPC/Lazarus shared library project on Windows, it is possible to get the fully qualified path name of the DLL using SysUtils.GetModuleName( hInstance ).

I know that it is possible to get the library name on Linux (see https://forum.lazarus.freepascal.org/index.php?topic=46695.0). However, this does not return the library's path - only it's filename.

Is there a way to return the shared library's fully qualified path on Linux?

Cheers 

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #1 on: July 17, 2023, 08:55:59 pm »
Interesting question, I suspect that it's complicated by the fact that in Linux (possibly in unix, i.e. also the BSDs etc.) the shared library paths are preconfigured: see ld.so.conf etc. In extremis you could probably inspect /proc/XXX/fdinfo where XXX is the current PID.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 3269
Re: Get the shared library's fully qualified path in Linux
« Reply #2 on: July 18, 2023, 09:01:21 am »
why not just fire off a "which Library.so"?
or is he looking for a "custom" libraries path?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #3 on: July 18, 2023, 09:08:52 am »
PATH isn't the same as the library search path.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 3269
Re: Get the shared library's fully qualified path in Linux
« Reply #4 on: July 18, 2023, 09:48:02 am »
PATH isn't the same as the library search path.

MarkMLl
crap. Forgot that. thx.

FWIW: https://unix.stackexchange.com/questions/22178/which-equivalent-for-shared-libraries
Quote
If you have an executable and you want to see where it's picking up libraries, run

ldd /path/to/executable

This will account for libraries on the default search path as well as libraries in this executable's rpath if any.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #5 on: July 18, 2023, 10:16:19 am »
Quote
This will account for libraries on the default search path as well as libraries in this executable's rpath if any.

But not for .so which have been loaded dynamically under program control :-/

I really don't have time to go digging into this in depth, but my recollection is that at least on Linux this is all handled outside the kernel (perhaps somewhere in libc? I forget) so should at least be inspectable. Now it stands to reason that if a program loads a .so explicitly it should know where it found it (think of all the problems of loading database libraries particularly if the expected symlink hasn't been set up) but that doesn't mean that some other part of the program can examine the library (accessible via pointers etc.) and say whence it came.

In my case I normally wrap a dynamic library (.so or .dll) in an instance of a class, but I don't think I've ever looked at exposing the load path.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AmatCoder

  • Jr. Member
  • **
  • Posts: 71
    • My site
Re: Get the shared library's fully qualified path in Linux
« Reply #6 on: July 18, 2023, 01:59:51 pm »
In a FPC/Lazarus shared library project on Windows, it is possible to get the fully qualified path name of the DLL using SysUtils.GetModuleName( hInstance ).

I know that it is possible to get the library name on Linux (see https://forum.lazarus.freepascal.org/index.php?topic=46695.0). However, this does not return the library's path - only it's filename.

Is there a way to return the shared library's fully qualified path on Linux?

As far as I remember, dladdr function gives you a full path to the library, but it needs to be compiled to be position-independent (at least in C).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #7 on: July 18, 2023, 02:41:25 pm »
As far as I remember, dladdr function gives you a full path to the library, but it needs to be compiled to be position-independent (at least in C).

I think you're right. Can't comment on the PI aspect, which probably depends on lots of things.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

c600g

  • New Member
  • *
  • Posts: 45
Re: Get the shared library's fully qualified path in Linux
« Reply #8 on: July 18, 2023, 04:07:05 pm »
Thanks for the ideas, all. I think the /proc/XXX/fd idea is doable, but at this time it isn't critical that I have this functionality on Linux. I just thought it reasonable that a loaded library would know it's own path (as it knows it's filename).

Zvoni

  • Hero Member
  • *****
  • Posts: 3269
Re: Get the shared library's fully qualified path in Linux
« Reply #9 on: July 18, 2023, 04:21:58 pm »
Thanks for the ideas, all. I think the /proc/XXX/fd idea is doable, but at this time it isn't critical that I have this functionality on Linux. I just thought it reasonable that a loaded library would know it's own path (as it knows it's filename).
Then again: Why would you need to know the Filesystem-location of the library within its own code?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #10 on: July 18, 2023, 05:42:47 pm »
Thanks for the ideas, all. I think the /proc/XXX/fd idea is doable, but at this time it isn't critical that I have this functionality on Linux. I just thought it reasonable that a loaded library would know it's own path (as it knows it's filename).

I think AmatCoder's suggestion is a good one, but don't know whether it's Linux-specific.

Then again: Why would you need to know the Filesystem-location of the library within its own code?

Because of any search path the loader might apply. In e.g. a tech support environment you'd very much want to know what /actual/ file was loaded. Another case is where a dynamically-loaded library loads another library dynamically, with the handles being available but not the names.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 3269
Re: Get the shared library's fully qualified path in Linux
« Reply #11 on: July 18, 2023, 06:55:59 pm »

Then again: Why would you need to know the Filesystem-location of the library within its own code?

Because of any search path the loader might apply. In e.g. a tech support environment you'd very much want to know what /actual/ file was loaded. Another case is where a dynamically-loaded library loads another library dynamically, with the handles being available but not the names.

MarkMLl
Sorry, still not getting it.
A shared library is accessed from a „host“-application.
Irrespective if static or dynamic, the lib has to be „findable“ for the host, otherwise it fails to run the code inside the lib.
If he wants to know the libs location from within its own code, the code of the lib must have been called, meaning its host has found it, meaning its location is known
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Get the shared library's fully qualified path in Linux
« Reply #12 on: July 18, 2023, 07:10:14 pm »
meaning its host has found it, meaning its location is known

Those don't follow. There can be an OS-specific library search path in the way, of any number of symlink redirections like you typically get with a database library.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AmatCoder

  • Jr. Member
  • **
  • Posts: 71
    • My site
Re: Get the shared library's fully qualified path in Linux
« Reply #13 on: July 19, 2023, 02:35:27 pm »
meaning its host has found it, meaning its location is known

Location is known by the OS but not by the application (it only knows the name of the library).

Example:
Application ask to the OS for a library (by name) -> OS returns an opaque pointer (not the full path of the library).

 

TinyPortal © 2005-2018