Search code examples
c++linuxdladdr

On Linux, in a C++ program, how do I find the path to the shared library that was loaded?


I need to load functions from a custom library on an embedded platform that does not have a lot of the usual tools (e.g. no ldd, no gdb, etc). I'm cross compiling to that platform.

Suppose I want to use the function foo from libx.so. Now the platform has libx.so included in the system libs directory, but I want my program to pick it up from another location. I've set the LD_LIBRARY_PATH accordingly, but I strongly suspect my program is still picking up the system one.

I tried the following:

    Dl_info dl_info;
    dladdr((void*)foo, &dl_info);
    fprintf(stdout, "module %s loaded\n", dl_info.dli_fname);

But this returns a.out, which is not helpful.

I also tried:

    Dl_info dl_info;
    link_map* lm;
    int code = dladdr1((void*)foo, &dl_info, (void**)(&lm), RTLD_DL_LINKMAP);
    if (code == 0)
    {
        std::cout << "Failed" << std::endl;
        return 0;
    }
    fprintf(stdout, "module %s loaded\n", lm->l_name);

And I get an empty string. Note, the return code was not 0.

Any other methods I can try?

Thanks.


Solution

  • You can look at /proc/<pid>/maps (or /proc/self/maps to look at the mappings of the current process). This will show all files that are mmaped into the process address space, which will include shared libraries.

    # cat /proc/self/maps
    55af8c635000-55af8c63d000 r-xp 00000000 08:01 4456473                    /bin/cat
    55af8c83c000-55af8c83d000 r--p 00007000 08:01 4456473                    /bin/cat
    55af8c83d000-55af8c83e000 rw-p 00008000 08:01 4456473                    /bin/cat
    55af8cb0f000-55af8cb30000 rw-p 00000000 00:00 0                          [heap]
    7f90267d6000-7f9027219000 r--p 00000000 08:01 6429169                    /usr/lib/locale/locale-archive
    7f9027219000-7f9027400000 r-xp 00000000 08:01 267123                     /lib/x86_64-linux-gnu/libc-2.27.so
    7f9027400000-7f9027600000 ---p 001e7000 08:01 267123                     /lib/x86_64-linux-gnu/libc-2.27.so
    7f9027600000-7f9027604000 r--p 001e7000 08:01 267123                     /lib/x86_64-linux-gnu/libc-2.27.so
    7f9027604000-7f9027606000 rw-p 001eb000 08:01 267123                     /lib/x86_64-linux-gnu/libc-2.27.so
    7f9027606000-7f902760a000 rw-p 00000000 00:00 0 
    7f902760a000-7f9027631000 r-xp 00000000 08:01 267095                     /lib/x86_64-linux-gnu/ld-2.27.so
    7f90277f7000-7f902781b000 rw-p 00000000 00:00 0 
    7f9027831000-7f9027832000 r--p 00027000 08:01 267095                     /lib/x86_64-linux-gnu/ld-2.27.so
    7f9027832000-7f9027833000 rw-p 00028000 08:01 267095                     /lib/x86_64-linux-gnu/ld-2.27.so
    7f9027833000-7f9027834000 rw-p 00000000 00:00 0 
    7ffd03876000-7ffd03897000 rw-p 00000000 00:00 0                          [stack]
    7ffd0399d000-7ffd039a0000 r--p 00000000 00:00 0                          [vvar]
    7ffd039a0000-7ffd039a2000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]