4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright (c) 1988 AT&T
25 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
29 * Print the list of shared objects required by a dynamic executable or shared
32 * usage is: ldd [-d | -r] [-c] [-D] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
33 * [-U | -u] [-v] [-w] file(s)
35 * ldd opens the file and verifies the information in the elf header.
36 * If the file is a dynamic executable, we set up some environment variables
37 * and exec(2) the file. If the file is a shared object, we preload the
38 * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
39 * provides the diagnostic output, according to the environment variables set.
41 * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE].
42 * The runtime linker will print the pathnames of all dynamic objects it
43 * loads, and then exit. Note that we distiguish between ELF and AOUT objects
44 * when setting this environment variable - AOUT executables cause the mapping
45 * of sbcp, the dependencies of which the user isn't interested in.
47 * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
48 * perform its normal relocations and issue warning messages for unresolved
49 * references. It will then exit.
50 * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
51 * will perform all relocations, otherwise (under -d) the runtime linker
52 * will not perform PLT (function) type relocations.
54 * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
55 * configuration file use.
57 * If -D is specified we skip deferred dependency processing. By default,
58 * ldd loads all deferred dependencies. However, during normal process
59 * execution, deferred dependencies are only loaded when an explicit binding
60 * to an individual deferred reference is made. As no user code is executed
61 * under ldd, explicit references to deferred symbols can't be triggered.
63 * If -e is specified the associated environment variable is set for the
64 * child process that will produce ldd's diagnostics.
66 * If -i is specified, we set LD_INIT=1. The order of inititialization
67 * sections to be executed is printed. We also set LD_WARN=1.
69 * If -f is specified, we will run ldd as root on executables that have
70 * an unsercure runtime linker that does not live under the "/usr/lib"
71 * directory. By default we will not let this happen.
73 * If -l is specified it generates a warning for any auxiliary filter not found.
74 * Prior to 2.8 this forced any filters to load (all) their filtees. This is
75 * now the default, however missing auxiliary filters don't generate any error
76 * diagniostic. See also -L.
78 * If -L is specified we revert to lazy loading, thus any filtee or lazy
79 * dependency loading is deferred until relocations cause loading. Without
80 * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
81 * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
82 * any lazy loaded dependencies.
84 * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
85 * the runtime linker to indicate the search algorithm used.
87 * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
88 * linker to indicate all object dependencies (not just the first object
89 * loaded) together with any versioning requirements.
91 * If -U or -u is specified unused dependencies are detected. -u causes
92 * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
93 * process to be detected. -U causes LD_UNREF=1 to be set, which causes
94 * unreferenced objects, and unreferenced cyclic dependencies to be detected.
95 * These options assert that at least -d is set as relocation references are
96 * what determine an objects use.
98 * If -w is specified, no unresolved weak references are allowed. -w causes
99 * LD_NOUNRESWEAK=1 to be set. By default, an unresolved weak reference is
100 * allowed, and a "0" is written to the relocation offset. The -w option
101 * disables this default. Any weak references that can not be resolved result
102 * in relocation error messages. This option has no use without -r or -d.
104 * If the -p option is specified, no unresolved PARENT or EXTERN references are
105 * allowed. -p causes LD_NOPAREXT=1 to be set. By default, PARENT and EXTERN
106 * references, which have been explicitly assigned via a mapfile when a shared
107 * object was built, imply that a caller will provide the symbols, and hence
108 * these are not reported as relocation errors. Note, the -p option is asserted
109 * by default when either the -r or -d options are used to inspect a dynamic
110 * executable. This option has no use with a shared object without -r or -d.
128 static int elf_check(int, char *, char *, Elf
*, int);
129 static int aout_check(int, char *, char *, int, int);
130 static int run(int, char *, char *, const char *, int);
134 * Define all environment variable strings. The character following the "="
135 * will be written to, to disable or enable the associated feature.
137 static char bind
[] = "LD_BIND_NOW= ",
138 load_elf
[] = "LD_TRACE_LOADED_OBJECTS_E= ",
139 load_aout
[] = "LD_TRACE_LOADED_OBJECTS_A= ",
140 path
[] = "LD_TRACE_SEARCH_PATHS= ",
141 verb
[] = "LD_VERBOSE= ",
142 warn
[] = "LD_WARN= ",
143 conf
[] = "LD_NOCONFIG= ",
144 fltr
[] = "LD_LOADFLTR= ",
145 lazy
[] = "LD_NOLAZYLOAD=1",
146 init
[] = "LD_INIT= ",
147 uref
[] = "LD_UNREF= ",
148 used
[] = "LD_UNUSED= ",
149 weak
[] = "LD_NOUNRESWEAK= ",
150 nope
[] = "LD_NOPAREXT= ",
151 defr
[] = "LD_DEFERRED= ";
154 static const char *prefile_32
, *prefile_64
, *prefile
;
155 static APlist
*eopts
= NULL
;
158 main(int argc
, char **argv
, char **envp
)
160 char *str
, *cname
= argv
[0];
163 int cflag
= 0, dflag
= 0, fflag
= 0, iflag
= 0, Lflag
= 0;
164 int lflag
= 0, rflag
= 0, sflag
= 0, Uflag
= 0, uflag
= 0;
165 int Dflag
= 0, pflag
= 0, vflag
= 0, wflag
= 0;
166 int nfile
, var
, error
= 0;
170 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
171 * the binary. If successful, conv_check_native() won't return.
173 * This is done to ensure that ldd can handle objects >2GB.
174 * ldd uses libelf, which is not large file capable. The
175 * 64-bit ldd can handle any sized object.
177 (void) conv_check_native(argv
, envp
);
182 (void) setlocale(LC_MESSAGES
, MSG_ORIG(MSG_STR_EMPTY
));
183 (void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS
));
186 * verify command line syntax and process arguments
188 opterr
= 0; /* disable getopt error mesg */
190 while ((var
= getopt(argc
, argv
, MSG_ORIG(MSG_STR_GETOPT
))) != EOF
) {
192 case 'c' : /* enable config search */
195 case 'D' : /* skip deferred dependencies */
198 case 'd' : /* perform data relocations */
204 if (aplist_append(&eopts
, optarg
, 10) == NULL
) {
205 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_MALLOC
),
219 case 'i' : /* print the order of .init */
223 pflag
= 1; /* expose unreferenced */
224 break; /* parent or externals */
225 case 'r' : /* perform all relocations */
230 case 's' : /* enable search path output */
233 case 'U' : /* list unreferenced */
234 Uflag
= 1; /* dependencies */
238 case 'u' : /* list unused dependencies */
243 case 'v' : /* enable verbose output */
246 case 'w' : /* expose unresolved weak */
247 wflag
= 1; /* references */
257 (void) fprintf(stderr
, MSG_INTL(MSG_ARG_USAGE
), cname
);
262 * Determine if any of the LD_PRELOAD family is already set in the
263 * environment, if so we'll continue to analyze each object with the
264 * appropriate setting.
266 if (((prefile_32
= getenv(MSG_ORIG(MSG_LD_PRELOAD_32
))) == NULL
) ||
267 (*prefile_32
== '\0')) {
268 prefile_32
= MSG_ORIG(MSG_STR_EMPTY
);
270 if (((prefile_64
= getenv(MSG_ORIG(MSG_LD_PRELOAD_64
))) == NULL
) ||
271 (*prefile_64
== '\0')) {
272 prefile_64
= MSG_ORIG(MSG_STR_EMPTY
);
274 if (((prefile
= getenv(MSG_ORIG(MSG_LD_PRELOAD
))) == NULL
) ||
275 (*prefile
== '\0')) {
276 prefile
= MSG_ORIG(MSG_STR_EMPTY
);
280 * Determine if any environment requests are for the LD_PRELOAD family,
281 * and if so override any environment settings we've established above.
283 for (APLIST_TRAVERSE(eopts
, idx
, str
)) {
284 if ((strncmp(str
, MSG_ORIG(MSG_LD_PRELOAD_32
),
285 MSG_LD_PRELOAD_32_SIZE
)) == 0) {
286 str
+= MSG_LD_PRELOAD_32_SIZE
;
287 if ((*str
++ == '=') && (*str
!= '\0'))
291 if ((strncmp(str
, MSG_ORIG(MSG_LD_PRELOAD_64
),
292 MSG_LD_PRELOAD_64_SIZE
)) == 0) {
293 str
+= MSG_LD_PRELOAD_64_SIZE
;
294 if ((*str
++ == '=') && (*str
!= '\0'))
298 if ((strncmp(str
, MSG_ORIG(MSG_LD_PRELOAD
),
299 MSG_LD_PRELOAD_SIZE
)) == 0) {
300 str
+= MSG_LD_PRELOAD_SIZE
;
301 if ((*str
++ == '=') && (*str
!= '\0'))
308 * Set the appropriate relocation environment variables (Note unsetting
309 * the environment variables is done just in case the user already
310 * has these in their environment ... sort of thing the test folks
313 warn
[sizeof (warn
) - 2] = (dflag
|| rflag
|| Uflag
|| uflag
) ? '1' :
315 bind
[sizeof (bind
) - 2] = (rflag
) ? '1' : '\0';
316 path
[sizeof (path
) - 2] = (sflag
) ? '1' : '\0';
317 verb
[sizeof (verb
) - 2] = (vflag
) ? '1' : '\0';
318 fltr
[sizeof (fltr
) - 2] = (Lflag
) ? '\0' : (lflag
) ? '2' : '1';
319 init
[sizeof (init
) - 2] = (iflag
) ? '1' : '\0';
320 conf
[sizeof (conf
) - 2] = (cflag
) ? '1' : '\0';
321 lazy
[sizeof (lazy
) - 2] = (Lflag
) ? '\0' : '1';
322 uref
[sizeof (uref
) - 2] = (Uflag
) ? '1' : '\0';
323 used
[sizeof (used
) - 2] = (uflag
) ? '1' : '\0';
324 weak
[sizeof (weak
) - 2] = (wflag
) ? '1' : '\0';
325 nope
[sizeof (nope
) - 2] = (pflag
) ? '1' : '\0';
326 defr
[sizeof (defr
) - 2] = (Dflag
) ? '\0' : '1';
329 * coordinate libelf's version information
331 if (elf_version(EV_CURRENT
) == EV_NONE
) {
332 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_LIBELF
), cname
,
338 * Loop through remaining arguments. Note that from here on there
339 * are no exit conditions so that we can process a list of files,
340 * any error condition is retained for a final exit status.
342 nfile
= argc
- optind
;
343 for (; optind
< argc
; optind
++) {
344 char *fname
= argv
[optind
];
347 * Open file (do this before checking access so that we can
348 * provide the user with better diagnostics).
350 if ((var
= open(fname
, O_RDONLY
)) == -1) {
352 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
), cname
,
353 fname
, strerror(err
));
359 * Get the files elf descriptor and process it as an elf or
362 elf
= elf_begin(var
, ELF_C_READ
, (Elf
*)0);
363 switch (elf_kind(elf
)) {
365 (void) fprintf(stderr
, MSG_INTL(MSG_USP_NODYNORSO
),
370 (void) fprintf(stderr
, MSG_INTL(MSG_USP_UNKNOWN
),
375 if (elf_check(nfile
, fname
, cname
, elf
, fflag
) != 0)
380 * This is either an unknown file or an aout format
382 if (aout_check(nfile
, fname
, cname
, var
, fflag
) != 0)
395 elf_check(int nfile
, char *fname
, char *cname
, Elf
*elf
, int fflag
)
397 Conv_inv_buf_t inv_buf
;
400 int dynamic
= 0, interp
= 0, cnt
, class;
403 * verify information in file header
405 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
406 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETEHDR
),
407 cname
, fname
, elf_errmsg(-1));
414 if ((ehdr
.e_machine
!= M_MACH_32
) && (ehdr
.e_machine
!= M_MACH_64
) &&
415 (ehdr
.e_machine
!= M_MACHPLUS
)) {
416 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_MACHTYPE
), cname
, fname
,
417 conv_ehdr_mach(ehdr
.e_machine
, 0, &inv_buf
));
422 * Compatible encoding (byte order)
424 if (ehdr
.e_ident
[EI_DATA
] != M_DATA
) {
425 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_DATA
), cname
, fname
,
426 conv_ehdr_data(ehdr
.e_ident
[EI_DATA
], 0, &inv_buf
));
433 switch (class = ehdr
.e_ident
[EI_CLASS
]) {
436 * If M_MACH is not the same thing as M_MACHPLUS and this
437 * is an M_MACHPLUS object, then the corresponding header
440 if ((ehdr
.e_machine
!= M_MACH
) &&
441 ((ehdr
.e_flags
& M_FLAGSPLUS
) == 0)) {
442 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_MACHFLAGS
),
448 /* Requires 64-bit kernel */
449 if (conv_sys_eclass() == ELFCLASS32
) {
450 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_KCLASS32
),
451 cname
, fname
, conv_ehdr_class(class, 0, &inv_buf
));
456 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_CLASS
), cname
, fname
,
457 conv_ehdr_class(class, 0, &inv_buf
));
464 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
) &&
465 (ehdr
.e_type
!= ET_REL
)) {
466 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_BADMAGIC
),
472 * Check that the file is executable. Dynamic executables must be
473 * executable to be exec'ed. Shared objects need not be executable to
474 * be mapped with a dynamic executable.
476 if (access(fname
, X_OK
) != 0) {
477 if (ehdr
.e_type
== ET_EXEC
) {
478 (void) fprintf(stderr
, MSG_INTL(MSG_USP_NOTEXEC_1
),
485 * Determine whether we have a dynamic section or interpretor.
487 for (cnt
= 0; cnt
< (int)ehdr
.e_phnum
; cnt
++) {
488 if (dynamic
&& interp
)
491 if (gelf_getphdr(elf
, cnt
, &phdr
) == NULL
) {
492 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETPHDR
),
493 cname
, fname
, elf_errmsg(-1));
497 if (phdr
.p_type
== PT_DYNAMIC
) {
502 if (phdr
.p_type
!= PT_INTERP
)
508 * If fflag is not set, and euid == root, and the interpreter
509 * does not live under /lib, /usr/lib or /etc/lib then don't
510 * allow ldd to execute the image. This prevents someone
511 * creating a `trojan horse' by substituting their own
512 * interpreter that could preform privileged operations
513 * when ldd is against it.
515 if ((fflag
== 0) && (geteuid() == 0) &&
516 (strcmp(fname
, conv_lddstub(class)) != 0)) {
520 * Does the interpreter live under a trusted directory.
522 interpreter
= elf_getident(elf
, 0) + phdr
.p_offset
;
524 if ((strncmp(interpreter
, MSG_ORIG(MSG_PTH_USRLIB
),
525 MSG_PTH_USRLIB_SIZE
) != 0) &&
526 (strncmp(interpreter
, MSG_ORIG(MSG_PTH_LIB
),
527 MSG_PTH_LIB_SIZE
) != 0) &&
528 (strncmp(interpreter
, MSG_ORIG(MSG_PTH_ETCLIB
),
529 MSG_PTH_ETCLIB_SIZE
) != 0)) {
530 (void) fprintf(stderr
, MSG_INTL(MSG_USP_ELFINS
),
531 cname
, fname
, interpreter
);
538 * Catch the case of a static executable (ie, an ET_EXEC that has a set
539 * of program headers but no PT_DYNAMIC).
541 if (ehdr
.e_phnum
&& !dynamic
) {
542 (void) fprintf(stderr
, MSG_INTL(MSG_USP_NODYNORSO
), cname
,
548 * If there is a dynamic section, then check for the DF_1_NOHDR
549 * flag, and bail if it is present. Such objects are created using
550 * a mapfile option (?N in the version 1 syntax, or HDR_NOALLOC
551 * otherwise). The ELF header and program headers are
552 * not mapped as part of the first segment, and virtual addresses
553 * are computed without them. If ldd tries to interpret such
554 * a file, it will become confused and generate bad output or
555 * crash. Such objects are always special purpose files (like an OS
556 * kernel) --- files for which the ldd operation doesn't make sense.
558 if (dynamic
&& (_gelf_getdyndtflags_1(elf
) & DF_1_NOHDR
)) {
559 (void) fprintf(stderr
, MSG_INTL(MSG_USP_NOHDR
), cname
,
567 * Run the required program (shared and relocatable objects require the
570 if ((ehdr
.e_type
== ET_EXEC
) && interp
)
571 return (run(nfile
, cname
, fname
, (const char *)fname
, class));
573 return (run(nfile
, cname
, fname
, conv_lddstub(class), class));
577 aout_check(int nfile
, char *fname
, char *cname
, int fd
, int fflag
)
582 if (lseek(fd
, 0, SEEK_SET
) != 0) {
584 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_LSEEK
), cname
, fname
,
588 if (read(fd
, (char *)&aout
, sizeof (aout
)) != sizeof (aout
)) {
590 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_READ
), cname
, fname
,
594 if (aout
.a_machtype
!= M_SPARC
) {
595 (void) fprintf(stderr
, MSG_INTL(MSG_USP_UNKNOWN
), cname
, fname
);
598 if (N_BADMAG(aout
) || !aout
.a_dynamic
) {
599 (void) fprintf(stderr
, MSG_INTL(MSG_USP_NODYNORSO
), cname
,
603 if (!fflag
&& (geteuid() == 0)) {
604 (void) fprintf(stderr
, MSG_INTL(MSG_USP_AOUTINS
), cname
, fname
);
609 * Run the required program.
611 if ((aout
.a_magic
== ZMAGIC
) && (aout
.a_entry
<= sizeof (aout
))) {
613 return (run(nfile
, cname
, fname
, conv_lddstub(ELFCLASS32
),
617 return (run(nfile
, cname
, fname
, (const char *)fname
,
624 * Run the required program, setting the preload and trace environment
625 * variables accordingly.
628 run(int nfile
, char *cname
, char *fname
, const char *ename
, int class)
630 const char *preload
= 0;
633 if ((pid
= fork()) == -1) {
635 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_FORK
), cname
,
640 if (pid
) { /* parent */
641 while (wait(&status
) != pid
)
643 if (WIFSIGNALED(status
) && ((WSIGMASK
& status
) != SIGPIPE
)) {
644 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_EXEC
), cname
,
646 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_EXEC_SIG
),
647 (WSIGMASK
& status
), ((status
& WCOREFLG
) ?
648 MSG_INTL(MSG_SYS_EXEC_CORE
) :
649 MSG_ORIG(MSG_STR_EMPTY
)));
651 } else if (WHIBYTE(status
)) {
652 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_EXEC
), cname
,
654 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_EXEC_STAT
),
664 * When using ldd(1) to analyze a shared object we preload the
665 * shared object with lddstub. Any additional preload
666 * requirements are added after the object being analyzed, this
667 * allows us to skip the first object but produce diagnostics
668 * for each other preloaded object.
670 if (fname
!= ename
) {
672 const char *files
= prefile
;
673 const char *format
= MSG_ORIG(MSG_STR_FMT1
);
675 for (str
= fname
; *str
; str
++)
677 format
= MSG_ORIG(MSG_STR_FMT2
);
681 preload
= MSG_ORIG(MSG_LD_PRELOAD
);
684 * Determine which preload files and preload environment
687 if (class == ELFCLASS64
) {
688 if (prefile_64
!= MSG_ORIG(MSG_STR_EMPTY
)) {
690 preload
= MSG_ORIG(MSG_LD_PRELOAD_64
);
693 if (prefile_32
!= MSG_ORIG(MSG_STR_EMPTY
)) {
695 preload
= MSG_ORIG(MSG_LD_PRELOAD_32
);
699 if ((str
= (char *)malloc(strlen(preload
) +
700 strlen(fname
) + strlen(files
) + 5)) == 0) {
701 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_MALLOC
),
706 (void) sprintf(str
, format
, preload
, fname
, files
);
707 if (putenv(str
) != 0) {
708 (void) fprintf(stderr
, MSG_INTL(MSG_ENV_FAILED
),
714 * The pointer "load" has be assigned to load_elf[] or
715 * load_aout[]. Use the size of load_elf[] as the size
716 * of load_aout[] is the same.
718 load
[sizeof (load_elf
) - 2] = '2';
720 load
[sizeof (load_elf
) - 2] = '1';
724 * Establish new environment variables to affect the child
727 if ((putenv(warn
) != 0) || (putenv(bind
) != 0) ||
728 (putenv(path
) != 0) || (putenv(verb
) != 0) ||
729 (putenv(fltr
) != 0) || (putenv(conf
) != 0) ||
730 (putenv(init
) != 0) || (putenv(lazy
) != 0) ||
731 (putenv(uref
) != 0) || (putenv(used
) != 0) ||
732 (putenv(weak
) != 0) || (putenv(load
) != 0) ||
733 (putenv(nope
) != 0) || (putenv(defr
) != 0)) {
734 (void) fprintf(stderr
, MSG_INTL(MSG_ENV_FAILED
), cname
);
739 * Establish explicit environment requires (but don't override
740 * any preload request established to process a shared object).
743 for (APLIST_TRAVERSE(eopts
, idx
, str
)) {
746 size
= strlen(preload
);
747 if ((strncmp(preload
, str
, size
) == 0) &&
748 (str
[size
] == '=')) {
752 if (putenv(str
) != 0) {
753 (void) fprintf(stderr
, MSG_INTL(MSG_ENV_FAILED
),
760 * Execute the object and let ld.so.1 do the rest.
763 (void) printf(MSG_ORIG(MSG_STR_FMT3
), fname
);
764 (void) fflush(stdout
);
765 if ((execl(ename
, ename
, (char *)0)) == -1) {
766 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_EXEC
), cname
,
779 return (gettext(MSG_ORIG(mid
)));