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