如何知道应用程序在 运行 时间内链接了哪个?

How to know which so the app has linked in run time?

背景:

我在 linux 上有以下代码结构,并且在文件夹 correct_sowrong_so 中有两个不同版本的 caculate.c。我想知道 so app 在启动时 link 编辑了哪个 so

caculate.c 构建的

libcac.so 将被 main.c 使用。

~/tt$ tree
.
├── correct_so
│   ├── caculate.c
│   ├── caculate.h
│   └── libcac.so
├── main
├── main.c
└── wrong_so
    ├── caculate.c
    ├── caculate.h
    └── libcac.so

correct_so/caculate.c:

#include "caculate.h"

int add(int a, int b)
{
    return (a + b);
}

wrong_so/caculate.c:

#include "caculate.h"

int add(int a, int b)
{
    return (a + b) * 2;
}

caculate.h:correct_so & wrong_so 相同)

#ifndef _CACULATE_H__INCLUDE_
#define _CACULATE_H__INCLUDE_
int add(int a, int b);
#endif

main.c:

#include <stdio.h>
#include <unistd.h>
#include "caculate.h"

int main()
{
    int a = 1;
    int b = 2;
    while (1)
    {
        printf("%d + %d = %d\n", a, b, add(a, b));
        sleep(1);
    }
    return 0;
}

我的问题:

我按照以下步骤操作,详情参考下一篇日志:

综上所述,如果应用程序在 运行 时没有打印日志,我怎么知道它是否具有 运行 正确的 so 呢?同时让我们假设LD_LIBRARY_PATH会在应用程序运行时被其他人更改,甚至可能在系统中没有历史记录。

然后,我可以告诉别人:哦,系统中有2个版本的库,你只是运行一个问题版本的应用程序,所以应用程序肯定有运行时间问题。

我的实验可以证明我的问题:

~/tt$ cd correct_so/
~/tt/correct_so$ ls
caculate.c  caculate.h  libcac.so
~/tt/correct_so$ gcc -shared -fPIC caculate.c -o libcac.so
~/tt/correct_so$ cd ..
~/tt$ cd wrong_so/
~/tt/wrong_so$ gcc -shared -fPIC caculate.c -o libcac.so
~/tt/wrong_so$ cd ..
~/tt$ gcc main.c -o main -I correct_so -L correct_so -lcac
~/tt$ ldd main
        linux-vdso.so.1 =>  (0x00007fffd3dfe000)
        libcac.so => correct_so/libcac.so (0x00007f1a70b7c000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1a7079f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f1a70d80000)
~/tt$ export LD_LIBRARY_PATH=wrong_so && ./main
1 + 2 = 6
1 + 2 = 6
1 + 2 = 6
^Z
[1]+  Stopped                 ./main
~/tt$ ldd main
        linux-vdso.so.1 =>  (0x00007fff1abd9000)
        libcac.so => wrong_so/libcac.so (0x00007fdb5523c000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fdb54e5f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fdb55440000)
~/tt$ export LD_LIBRARY_PATH=correct_so
~/tt$ ldd main
        linux-vdso.so.1 =>  (0x00007fffa11fe000)
        libcac.so => correct_so/libcac.so (0x00007ffeda6b6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f82f80bc000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f82f849b000)
~/tt$ fg
./main
1 + 2 = 6
^C
ps -ef | grep main // find your process ID
lsof -p ${pid}
here is my output
main    6839 scliang  cwd    DIR   8,17     4096 226363625 /home/scliang/so
main    6839 scliang  rtd    DIR    8,2     4096        96 /
main    6839 scliang  txt    REG   8,17     8528 226363626 /home/scliang/so/main
main    6839 scliang  mem    REG    8,2  2173512      2139 /usr/lib64/libc-2.17.so
main    6839 scliang  mem    REG   8,17     7864 226493228 /home/scliang/so/wrong_so/libcac.so
main    6839 scliang  mem    REG    8,2   164240      2132 /usr/lib64/ld-2.17.so
main    6839 scliang    0u   CHR  136,0      0t0         3 /dev/pts/0
main    6839 scliang    1u   CHR  136,0      0t0         3 /dev/pts/0
main    6839 scliang    2u   CHR  136,0      0t0         3 /dev/pts/0