How to get the current library path

I have a plugin ( .so

file) that loads dynamically. In the same folder with the file .so

, I have several images. One of the features the plugin has to implement is to return paths to images.

How can I get the absolute path .so

from myself in C ++ (or C)?

I know the problem can be easily solved from the executable (the images are in the same folder as .so

), but unfortunately I don't have access to its source code.

+3


source to share


1 answer


On Linux /proc/self/maps

, the full path will be displayed for each shared object loaded. From the proc.5 man page :

/proc/[pid]/maps

      

A file containing the current regions of memory and their access permissions. See Mmap (2) for some additional information on memory mappings.

...

File format:

   address           perms offset  dev   inode       pathname
   00400000-00452000 r-xp 00000000 08:02 173521      /usr/bin/dbus-daemon
   00651000-00652000 r--p 00051000 08:02 173521      /usr/bin/dbus-daemon
   00652000-00655000 rw-p 00052000 08:02 173521      /usr/bin/dbus-daemon
   00e03000-00e24000 rw-p 00000000 00:00 0           [heap]
   00e24000-011f7000 rw-p 00000000 00:00 0           [heap]
   ...
   35b1800000-35b1820000 r-xp 00000000 08:02 135522  /usr/lib64/ld-2.15.so
   35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522  /usr/lib64/ld-2.15.so
   35b1a20000-35b1a21000 rw-p 00020000 08:02 135522  /usr/lib64/ld-2.15.so
   35b1a21000-35b1a22000 rw-p 00000000 00:00 0
   35b1c00000-35b1dac000 r-xp 00000000 08:02 135870  /usr/lib64/libc-2.15.so
   35b1dac000-35b1fac000 ---p 001ac000 08:02 135870  /usr/lib64/libc-2.15.so
   35b1fac000-35b1fb0000 r--p 001ac000 08:02 135870  /usr/lib64/libc-2.15.so
   35b1fb0000-35b1fb2000 rw-p 001b0000 08:02 135870  /usr/lib64/libc-2.15.so
   ...
   f2c6ff8c000-7f2c7078c000 rw-p 00000000 00:00 0    [stack:986]
   ...
   7fffb2c0d000-7fffb2c2e000 rw-p 00000000 00:00 0   [stack]
   7fffb2d48000-7fffb2d49000 r-xp 00000000 00:00 0   [vdso]

      



If you know the base name of your shared object, you can parse the contents of / proc / self / maps and find the full path.

To protect the name of the changed shared object, you can also use the address of the symbol in your shared object to select the correct string from /proc/self/maps

.

+3


source







All Articles