New module 'ldd'.
[gnulib.git] / m4 / ldd.m4
1 # ldd.m4 serial 1
2 dnl Copyright (C) 2006 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 # Sets LDDPROG to a command and LDDPOSTPROC to a filter command, such that
8 #   $LDDPROG program $LDDPOSTPROC
9 # outputs a whitespace-separated list of the dynamically linked dependencies
10 # of the program, as library names (no full pathnames), or nothing if the
11 # program is statically linked or if the service is not supported on the given
12 # system.
13
14 dnl From Bruno Haible.
15
16 AC_DEFUN([gl_LDD],
17 [
18   AC_REQUIRE([AC_CANONICAL_HOST])
19   AC_REQUIRE([AC_PROG_CC])
20   dnl Default values.
21   LDDPROG=':'
22   LDDPOSTPROC=
23   dnl First try objdump, since it works when cross-compiling.
24   AC_CHECK_TOOL([OBJDUMP], [objdump], [:])
25   if test "$OBJDUMP" != ":"; then
26     LDDPROG="LC_ALL=C $OBJDUMP -p"
27     dnl The output of "LC_ALL=C objdump -p program" of a program or library
28     dnl looks like this:
29     dnl
30     dnl libnet.so:     file format elf32-i386
31     dnl
32     dnl Program Header:
33     dnl     LOAD off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**12
34     dnl          filesz 0x0001391d memsz 0x0001391d flags r-x
35     dnl     LOAD off    0x00013920 vaddr 0x00014920 paddr 0x00014920 align 2**12
36     dnl          filesz 0x00001874 memsz 0x0001b020 flags rw-
37     dnl  DYNAMIC off    0x00015104 vaddr 0x00016104 paddr 0x00016104 align 2**2
38     dnl          filesz 0x00000090 memsz 0x00000090 flags rw-
39     dnl
40     dnl Dynamic Section:
41     dnl   NEEDED      libroot.so
42     dnl   SONAME      libnet.so
43     dnl   SYMBOLIC    0x0
44     dnl   INIT        0x2aec
45     dnl   FINI        0x12a2c
46     dnl   HASH        0x94
47     dnl   STRTAB      0x1684
48     dnl   SYMTAB      0x774
49     dnl   STRSZ       0xbd5
50     dnl   SYMENT      0x10
51     dnl   PLTGOT      0x15f20
52     dnl   PLTRELSZ    0x320
53     dnl   PLTREL      0x11
54     dnl   JMPREL      0x27cc
55     dnl   REL         0x225c
56     dnl   RELSZ       0x570
57     dnl   RELENT      0x8
58     LDDPOSTPROC="2>/dev/null | sed -n -e 's,^  NEEDED *\\([^ ].*\\)\$,\\1,p'"
59   else
60     if test "$cross_compiling" = no; then
61       dnl Not cross-compiling. Try system dependent vendor tools.
62       case "$host_os" in
63         aix*)
64           LDDPROG="LC_ALL=C dump -H"
65           dnl The output of "LC_ALL=C dump -H program" looks like this:
66           dnl
67           dnl program:
68           dnl
69           dnl                         ***Loader Section***
70           dnl                       Loader Header Information
71           dnl VERSION#         #SYMtableENT     #RELOCent        LENidSTR
72           dnl 0x00000001       0x00000005       0x0000000d       0x0000001e       
73           dnl 
74           dnl #IMPfilID        OFFidSTR         LENstrTBL        OFFstrTBL
75           dnl 0x00000002       0x00000134       0x0000000d       0x00000152       
76           dnl 
77           dnl 
78           dnl                         ***Import File Strings***
79           dnl INDEX  PATH                          BASE                MEMBER              
80           dnl 0      /usr/lib:/lib                                                         
81           dnl 1                                    libc.a              shr.o               
82           dnl
83           LDDPOSTPROC="2>/dev/null | sed -e '/^[^0-9]/d' -e '/^0x/d' | sed -n -e 's,^[0-9]*         *\\([^      ]*\\).*\$,\\1,p' | sed -e 's,^.*/,,'"
84           ;;
85         darwin*)
86           LDDPROG="otool -L"
87           dnl The output of "otool -L program" looks like this:
88           dnl program:
89           dnl         /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 71.1.4)
90           LDDPOSTPROC="2>/dev/null | sed -n -e 's,^     \\([^   ]*\\).*\$,\\1,p' | sed -e 's,^.*/,,'"
91           ;;
92         hpux*)
93           LDDPROG="LC_ALL=C chatr"
94           dnl The output of "LC_ALL=C chatr program" looks like this:
95           dnl program: 
96           dnl          shared executable 
97           dnl          shared library dynamic path search:
98           dnl              SHLIB_PATH     disabled  second 
99           dnl              embedded path  disabled  first  Not Defined
100           dnl          shared library list:
101           dnl              dynamic   /usr/lib/libc.2
102           dnl          shared library binding:
103           dnl              deferred 
104           dnl          global hash table disabled
105           dnl          plabel caching disabled
106           dnl          global hash array size:1103
107           dnl          global hash array nbuckets:3
108           dnl          shared vtable support disabled
109           dnl          static branch prediction disabled
110           dnl          executable from stack: D (default)
111           dnl          kernel assisted branch prediction enabled
112           dnl          lazy swap allocation disabled
113           dnl          text segment locking disabled
114           dnl          data segment locking disabled
115           dnl          third quadrant private data space disabled
116           dnl          fourth quadrant private data space disabled
117           dnl          third quadrant global data space disabled
118           dnl          data page size: D (default)
119           dnl          instruction page size: D (default)
120           dnl          nulptr references disabled
121           dnl          shared library private mapping disabled
122           dnl          shared library text merging disabled
123           dnl
124           dnl or like this:
125           dnl a.out:
126           dnl          32-bit ELF executable
127           dnl          shared library dynamic path search:
128           dnl              LD_LIBRARY_PATH    enabled  first 
129           dnl              SHLIB_PATH         enabled  second
130           dnl              embedded path      enabled  third  /usr/lib/hpux32:/opt/langtools/lib/hpux32
131           dnl          shared library list:
132           dnl              libc.so.1
133           dnl          shared library binding:
134           dnl              deferred
135           dnl          global hash table disabled
136           dnl          global hash table size 1103
137           dnl          shared library mapped private disabled
138           dnl          shared library segment merging disabled
139           dnl          shared vtable support disabled
140           dnl          explicit unloading disabled
141           dnl          segments:
142           dnl              index type     address      flags size
143           dnl                  7 text     04000000     z---c-    D (default)
144           dnl                  8 data     40000000     ---m--    D (default)
145           dnl          executable from stack: D (default)
146           dnl          kernel assisted branch prediction enabled 
147           dnl          lazy swap allocation for dynamic segments disabled
148           dnl          nulptr references disabled
149           dnl          address space model: default
150           dnl          caliper dynamic instrumentation disabled
151           dnl
152           LDDPOSTPROC="2>/dev/null | sed -e '1,/shared library list:/d' -e '/shared library binding:/,\$d' | sed -e 's,^.*[     ]\\([^  ][^     ]*\\)\$,\\1,' | sed -e 's,^.*/,,'"
153           ;;
154         irix*)
155           LDDPROG="elfdump -Dl"
156           dnl The output of "elfdump -Dl program" looks like this:
157           dnl
158           dnl program:
159           dnl
160           dnl                    **** MIPS LIBLIST INFORMATION ****
161           dnl .liblist :
162           dnl [INDEX] Timestamp               Checksum        Flags   Name            Version
163           dnl [1]     Oct  2 05:19:12 1999    0x867bf7a8      -----   libc.so.1       sgi1.0
164           dnl
165           LDDPOSTPROC="2>/dev/null | sed -n -e 's,^[[][0-9]*[]].*       0x[^    ]*      [^      ][^     ]*      \\([^   ][^     ]*\\).*\$,\\1,p' | sed -e 's,^.*/,,'"
166           ;;
167         linux* | gnu* | kfreebsd*-gnu | knetbsd*-gnu) # glibc-based systems
168           LDDPROG="LC_ALL=C ldd"
169           dnl The output of "ldd program" looks like this:
170           dnl         libc.so.6 => /lib/libc.so.6 (0x4002d000)
171           dnl         /lib/ld-linux.so.2 (0x40000000)
172           LDDPOSTPROC="2>/dev/null | sed -n -e 's,^     \\([^   ][^     ]*\\).*\$,\\1,p' | sed -e 's,^.*/,,'"
173           ;;
174         osf*)
175           LDDPROG="LC_ALL=C odump -Dl"
176           dnl The output of "odump -Dl program" looks like this:
177           dnl
178           dnl                         ***LIBRARY LIST SECTION***
179           dnl         Name             Time-Stamp        CheckSum   Flags Version
180           dnl program:
181           dnl         libc.so      Dec 30 00:09:30 1997 0x5e955f9b     0 osf.1
182           dnl
183           LDDPOSTPROC="2>/dev/null | sed -n -e 's,^     \\([^   ][^     ]*\\).*,\\1,p' | sed -e '/^Name\$/d' | sed -e 's,^.*/,,'"
184           ;;
185         solaris*)
186           LDDPROG="LC_ALL=C ldd"
187           dnl The output of "ldd program" looks like this:
188           dnl         libc.so.1 =>     /usr/lib/libc.so.1
189           dnl         libdl.so.1 =>    /usr/lib/libdl.so.1
190           dnl         /usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1
191           dnl The first sed collects the indented lines.
192           dnl The second sed extracts the left-hand part.
193           dnl The third sed removes directory specifications.
194           LDDPOSTPROC="2>/dev/null | sed -n -e 's,^     \\([^ ].*\\)\$,\\1,p' | sed -e 's, =>.*\$,,' | sed -e 's,^.*/,,'"
195           ;;
196       esac
197     fi
198   fi
199   AC_SUBST([LDDPROG])
200   AC_SUBST([LDDPOSTPROC])
201 ])