Penguin
Annotated edit history of LD_DEBUG version 3, including all changes. View license author blame.
Rev Author # Line
1 JohnMcPherson 1 The dynamic library loader used in linux (part of glibc) has some neat tricks. One of these is that you can set an environment variable called
2 LD_DEBUG
3 to show how symbols (variables and functions, for example) are resolved for a dynamic executable. This can sometimes help resolve obscure bugs where your application isn't doing what you expect (assuming it is caused by symbols being resolved differently to what you were expecting).
4
2 JohnMcPherson 5 This is very useful if you get segmentation violations or aborts for a program - this can sometimes be caused by linking against the wrong version of a library. This is also a really good way to understand what happens when you run any program!
1 JohnMcPherson 6 It has some self-documentation - for the impatient, you can do
3 JohnMcPherson 7 <verbatim>
1 JohnMcPherson 8 $ LD_DEBUG=help /path/to/some/dynamic/executable
3 JohnMcPherson 9 </verbatim>
1 JohnMcPherson 10 eg
3 JohnMcPherson 11 <verbatim>
12 $ LD_DEBUG=help ls
13 </verbatim>
1 JohnMcPherson 14 prints out:
3 JohnMcPherson 15 <verbatim>
1 JohnMcPherson 16 Valid options for the LD_DEBUG environment variable are:
17
18 libs display library search paths
19 reloc display relocation processing
20 files display progress for input file
21 symbols display symbol table processing
22 bindings display information about symbol binding
23 versions display version dependencies
24 all all previous options combined
25 statistics display relocation statistics
26 help display this help message and exit
27
28 To direct the debugging output into a file instead of standard output a
29 filename can be specified using the LD_DEBUG_OUTPUT environment variable.
3 JohnMcPherson 30 </verbatim>
1 JohnMcPherson 31 As a quick example of what it does:
3 JohnMcPherson 32 <verbatim>
1 JohnMcPherson 33 $ LD_DEBUG=all ls 2>&1 > /dev/null | less
34
35 13442:
36 13442: file=librt.so.1; needed by ls
37 13442: find library=librt.so.1; searching
38 13442: search cache=/etc/ld.so.cache
39 13442: trying file=/lib/librt.so.1
40 13442:
41 13442: file=librt.so.1; generating link map
42 13442: dynamic: 0x400263ec base: 0x40020000 size: 0x00010d14
43 13442: entry: 0x400219c0 phdr: 0x40020034 phnum: 6
44 13442:
45 13442:
46 13442: file=libc.so.6; needed by ls
47 13442: find library=libc.so.6; searching
48 13442: search cache=/etc/ld.so.cache
49 13442: trying file=/lib/libc.so.6
50 13442:
51 13442: file=libc.so.6; generating link map
52 13442: dynamic: 0x40146ce4 base: 0x40031000 size: 0x0011ab00
53 13442: entry: 0x4004a184 phdr: 0x40031034 phnum: 6
54 13442:
55 ...
56 13442: checking for version `GLIBC_2.2' in file /lib/librt.so.1 required by file ls
57 13442: checking for version `GLIBC_2.1' in file /lib/libc.so.6 required by file ls
58 13442: checking for version `GLIBC_2.2.3' in file /lib/libc.so.6 required by file ls
59 ...
60 13442: relocation processing: /lib/libpthread.so.0 (lazy)
61 13442: symbol=_errno; lookup in file=ls
62 13442: symbol=_errno; lookup in file=/lib/librt.so.1
63 13442: symbol=_errno; lookup in file=/lib/libc.so.6
64 13442: symbol=_errno; lookup in file=/lib/libpthread.so.0
65 13442: symbol=_errno; lookup in file=/lib/ld-linux.so.2
66 13442: binding file /lib/libpthread.so.0 to /lib/libc.so.6: normal symbol `_errno' [GLIBC_2.0]
67 13442: symbol=_h_errno; lookup in file=ls
68 13442: symbol=_h_errno; lookup in file=/lib/librt.so.1
69 13442: symbol=_h_errno; lookup in file=/lib/libc.so.6
70 13442: symbol=_h_errno; lookup in file=/lib/libpthread.so.0
71 13442: symbol=_h_errno; lookup in file=/lib/ld-linux.so.2
72 13442: binding file /lib/libpthread.so.0 to /lib/libc.so.6: normal symbol `_h_errno' [GLIBC_2.0]
73 ...
3 JohnMcPherson 74 </verbatim>
1 JohnMcPherson 75 In other words, every single function and external variable in the standard library that ls(1) uses must be located each time it is run (kind of obvious, really).
3 JohnMcPherson 76 <verbatim>
1 JohnMcPherson 77 $ ldd /bin/ls
78 librt.so.1 => /lib/librt.so.1 (0x40020000)
79 libc.so.6 => /lib/libc.so.6 (0x40031000)
80 libpthread.so.0 => /lib/libpthread.so.0 (0x4014c000)
81 /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
3 JohnMcPherson 82 </verbatim>
1 JohnMcPherson 83
84 see ld.so(8) for environment variables.
85 ----
86 See also AdvancedUserTips, CommonProgrammingBugs