網頁

2011年6月14日 星期二

LD_DEBUG and LD_LIBRARY_PATH

It is useful to find out which library are loaded when running the program.
LD_DEBUG = help ./a.out
Vaild option for LD_DEBUG
libsdisplay library search paths
relocdisplay relocation processing
filesdisplay progress for input file
symbolsdisplay symbol table processing
bindingsdisplay information about symbol binding
versionsdisplay version dependencies
allall previous options combined
statisticsdisplay relocation statistic
unuseddetermined unused DSOs
helpdisplay this help message and exit
LD_DEBUG=libs ./a.out
17315:    find library=libc.so.6 [0]; searching
17315:     search cache=/etc/ld.so.cache
17315:      trying file=/lib/tls/i686/cmov/libc.so.6
17315:
17315:
17315:    calling init: /lib/tls/i686/cmov/libc.so.6
17315:
17315:
17315:    initialize program: ./a.out
17315:
17315:
17315:    transferring control: ./a.out
17315:
17315:
17315:    calling fini: ./a.out [0]
17315:
17315:
17315:    calling fini: /lib/tls/i686/cmov/libc.so.6 [0]
17315:
From above result, it will find the file "ld.so.conf" to know where the standard library is located.
In this case, the library I only use is glibc. So it is a simple code.
But it is handy tool if your program is very complex and you want to know which library is linked.
gcc --share b.c -o libb.so
gcc -o a{,.c} -lb -L.
Then you should export libary PATH "LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH;./
The program will be executed successfully.
You can try LD_DEBUG again and the loader will search additional path "./" you assigned  and load the specific library to memory.

沒有留言:

張貼留言