如何检查二进制文件中的共享库版本

时间:2022-08-01 07:03:29

I have a program that is linked with some shared library on start. There are multiple versions of the library installed on the host. Is there some function or macro that can check the version of library that was linked to my program? I want something like this:

我有一个程序,在启动时与一些共享库链接。主机上安装了多个版本的库。是否有一些函数或宏可以检查链接到我的程序的库的版本?我想要这样的东西:

int main() {
    REQUIRE_LIBRARY_VERSION_GREATER("libgcc", 1, 2, 3); //example
}

Is it possible in unix? I need it at least on linux and freebsd.

在unix中有可能吗?至少在linux和freebsd上我需要它。

EDIT: I would prefer to avoid fork/exec if possible.

编辑:如果可能的话,我宁愿避免使用fork / exec。

2 个解决方案

#1


1  

I use dl_iterate_phdr function to find out which shared objects are loaded. This is example of dl handler that prints shared libs info

我使用dl_iterate_phdr函数来找出加载的共享对象。这是打印共享库信息的dl处理程序的示例

static int header_handler(struct dl_phdr_info* info, size_t size, void* data)
{
    printf("name=%s (%d segments) address=%p\n",
            info->dlpi_name, info->dlpi_phnum, (void*)info->dlpi_addr);
    for (int j = 0; j < info->dlpi_phnum; j++) {
         printf("\t\t header %2d: address=%10p\n", j,
             (void*) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
         printf("\t\t\t type=%u, flags=0x%X\n",
                 info->dlpi_phdr[j].p_type, info->dlpi_phdr[j].p_flags);
    }
    printf("\n");
    return 0;
}

It is taken from here

它取自这里

It prints for my video processing project following :

它为我的视频处理项目打印如下:

name=/usr/local/lib/libopencv_highgui.so.2.4 (7 segments) address=0x7f467935a000
name=/usr/local/lib/libopencv_imgproc.so.2.4 (7 segments) address=0x7f46796e9000
...

Opencv includes the version in the name of library libopencv_imgproc.so*.2.4* . This way i know wich opencv version loaded. I don't know if it is good for your libraries, but it can be a starting point for your.

Opencv包含库libopencv_imgproc.so * .2.4 *库中的版本。这种方式我知道opencv版本加载。我不知道它对你的图书馆有好处,但它可以作为你的出发点。

#2


0  

The ldd command list shared libraries for your executable

ldd命令列出了可执行文件的共享库

ldd <executable>

#1


1  

I use dl_iterate_phdr function to find out which shared objects are loaded. This is example of dl handler that prints shared libs info

我使用dl_iterate_phdr函数来找出加载的共享对象。这是打印共享库信息的dl处理程序的示例

static int header_handler(struct dl_phdr_info* info, size_t size, void* data)
{
    printf("name=%s (%d segments) address=%p\n",
            info->dlpi_name, info->dlpi_phnum, (void*)info->dlpi_addr);
    for (int j = 0; j < info->dlpi_phnum; j++) {
         printf("\t\t header %2d: address=%10p\n", j,
             (void*) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
         printf("\t\t\t type=%u, flags=0x%X\n",
                 info->dlpi_phdr[j].p_type, info->dlpi_phdr[j].p_flags);
    }
    printf("\n");
    return 0;
}

It is taken from here

它取自这里

It prints for my video processing project following :

它为我的视频处理项目打印如下:

name=/usr/local/lib/libopencv_highgui.so.2.4 (7 segments) address=0x7f467935a000
name=/usr/local/lib/libopencv_imgproc.so.2.4 (7 segments) address=0x7f46796e9000
...

Opencv includes the version in the name of library libopencv_imgproc.so*.2.4* . This way i know wich opencv version loaded. I don't know if it is good for your libraries, but it can be a starting point for your.

Opencv包含库libopencv_imgproc.so * .2.4 *库中的版本。这种方式我知道opencv版本加载。我不知道它对你的图书馆有好处,但它可以作为你的出发点。

#2


0  

The ldd command list shared libraries for your executable

ldd命令列出了可执行文件的共享库

ldd <executable>