gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:24 25 4
gpt4 key购买 nike

背景:

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

libcac.socaculate.c 构建,将被 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_sowrong_so 相同)

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

ma​​in.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;
}

我的问题:

我做了以下步骤,详情引用下一篇日志:

  • 在 2 个不同的文件夹中编译出 2 个不同的 libcac.so:correct_sowrong_so
  • 使用 libcac.so 的链接编译出 main 应用程序>
  • LD_LIBRARY_PATH使用错误的so路径wrong_so,可以说出1 + 2 = 6的结果。现在我可以使用 ldd main,显示 libcac.so => wrong_so/libcac.soldd 通过 预定义的查找内容顺序,例如/lib、/usr/lib、LD_LIBRARY_PATH 等
  • 如果稍后 export LD_LIBRARY_PATH=correct_so 到正确的版本,ldd 将只显示正确版本的应用程序链接,但实际上当应用程序启动时,它发现错误的版本,因为设置了错误的 LD_LIBRARY_PATH。所以 ldd 在这里帮不了我。

总而言之,如果应用程序在运行时没有打印日志,我怎么知道它是否以正确的方式运行?同时让我们假设 LD_LIBRARY_PATH 会在应用程序运行时被其他人更改,甚至可能在系统中没有历史记录。

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

我的实验可以显示我的问题:

~/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

关于c - 如何知道应用程序在运行时链接了哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52980615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com