Background:
I have following code structure on linux, and have two different versions of caculate.c
in the folder correct_so
& wrong_so
. I want to know which so
the app
has linked when it starts.
libcac.so
which was built out with caculate.c
will be used by 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: (same for 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;
}
My question:
I do following steps, detail refers to next log:
- compile out 2 different
libcac.so
in 2 different folders:correct_so
&wrong_so
- compile out
main
app with link tolibcac.so
- use wrong so path
wrong_so
forLD_LIBRARY_PATH
, you can say the result of1 + 2 = 6
. Now I can useldd main
, showlibcac.so => wrong_so/libcac.so
,ldd
look up things througha predefined order
like/lib, /usr/lib, LD_LIBRARY_PATH etc
- if then later
export LD_LIBRARY_PATH=correct_so
to the correct one,ldd
will just show the app link to correct version, but in fact when the app starts, it find a wrong version because wrongLD_LIBRARY_PATH
set. Soldd
not helps me here.
To sum up, how can I know if an app has run with a correct so when it's in runtime, if no log was print? Meanwhile let's suppose LD_LIBRARY_PATH
will be changed by others when the app run and maybe even without history record in system.
Then, I can tell to others: Oh, there are 2 versions of library in the system, you just run the app with an issue version, so the app certainly have runtime issue.
My experiment which could show my issue:
~/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