0

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 to libcac.so
  • use wrong so path wrong_so for LD_LIBRARY_PATH, you can say the result of 1 + 2 = 6. Now I can use ldd main, show libcac.so => wrong_so/libcac.so, ldd look up things through a 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 wrong LD_LIBRARY_PATH set. So ldd 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
atline
  • 28,355
  • 16
  • 77
  • 113
  • Hi, by any chance, did you propose `python-3.6` and `python-3.7` as synonyms for `python-3.x`? There is now [a meta discussion related to it](https://meta.stackoverflow.com/questions/379015/duplicates-spawn-when-trying-to-watch-python-3-tags), in particular Martijn Pieters♦ comment. – Cœur Jan 17 '19 at 15:47
  • 1
    @Cœur, it's ok for me to remove the synonyms, thanks. – atline Jan 21 '19 at 04:40

1 Answers1

1
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
shaocong
  • 48
  • 7
  • This may not happen if ldd is showing correct dependency then runtime linker will load from same path instead it will pick library from earlier defined path. – anshkun Oct 25 '18 at 07:23