ldd.c revision 69827
1696Spaul/*
2696Spaul * Copyright (c) 1993 Paul Kranenburg
3696Spaul * All rights reserved.
4696Spaul *
5696Spaul * Redistribution and use in source and binary forms, with or without
6696Spaul * modification, are permitted provided that the following conditions
7696Spaul * are met:
8696Spaul * 1. Redistributions of source code must retain the above copyright
9696Spaul *    notice, this list of conditions and the following disclaimer.
10696Spaul * 2. Redistributions in binary form must reproduce the above copyright
11696Spaul *    notice, this list of conditions and the following disclaimer in the
12696Spaul *    documentation and/or other materials provided with the distribution.
13696Spaul * 3. All advertising materials mentioning features or use of this software
14696Spaul *    must display the following acknowledgement:
15696Spaul *      This product includes software developed by Paul Kranenburg.
16696Spaul * 4. The name of the author may not be used to endorse or promote products
171153Sjkh *    derived from this software without specific prior written permission
18696Spaul *
19696Spaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20696Spaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21696Spaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22696Spaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23696Spaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24696Spaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25696Spaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26696Spaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27696Spaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28696Spaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29696Spaul */
30696Spaul
3169827Scharnier#ifndef lint
3269827Scharnierstatic const char rcsid[] =
3369827Scharnier  "$FreeBSD: head/usr.bin/ldd/ldd.c 69827 2000-12-10 20:54:13Z charnier $";
3469827Scharnier#endif /* not lint */
3569827Scharnier
36696Spaul#include <sys/wait.h>
37696Spaul#include <a.out.h>
3835575Sdfr#include <elf.h>
391741Srich#include <err.h>
401741Srich#include <fcntl.h>
411741Srich#include <stdio.h>
421741Srich#include <stdlib.h>
431741Srich#include <unistd.h>
44696Spaul
4529042Sjdpextern void	dump_file __P((const char *));
4618600Speterextern int	error_count;
4718600Speter
48696Spaulvoid
49696Spaulusage()
50696Spaul{
5120050Sbde	fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n");
521741Srich	exit(1);
53696Spaul}
54696Spaul
55696Spaulint
56696Spaulmain(argc, argv)
57696Spaulint	argc;
58696Spaulchar	*argv[];
59696Spaul{
6018598Speter	char		*fmt1 = NULL, *fmt2 = NULL;
611741Srich	int		rval;
62696Spaul	int		c;
6319253Speter	int		vflag = 0;
64696Spaul
6565428Simp	while ((c = getopt(argc, argv, "vf:")) != -1) {
66696Spaul		switch (c) {
6718600Speter		case 'v':
6818600Speter			vflag++;
6918600Speter			break;
7018598Speter		case 'f':
7118598Speter			if (fmt1) {
7218598Speter				if (fmt2)
7369827Scharnier					errx(1, "too many formats");
7418598Speter				fmt2 = optarg;
7518598Speter			} else
7618598Speter				fmt1 = optarg;
7718598Speter			break;
78696Spaul		default:
79696Spaul			usage();
801741Srich			/*NOTREACHED*/
81696Spaul		}
82696Spaul	}
83696Spaul	argc -= optind;
84696Spaul	argv += optind;
85696Spaul
8618600Speter	if (vflag && fmt1)
8718600Speter		errx(1, "-v may not be used with -f");
8818600Speter
89696Spaul	if (argc <= 0) {
90696Spaul		usage();
911741Srich		/*NOTREACHED*/
92696Spaul	}
93696Spaul
9439354Sdfr#ifdef __i386__
9518600Speter	if (vflag) {
9618600Speter		for (c = 0; c < argc; c++)
9718600Speter			dump_file(argv[c]);
9818600Speter		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
9918600Speter	}
10039354Sdfr#endif
10118600Speter
102696Spaul	/* ld.so magic */
10321576Sjdp	setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
10418598Speter	if (fmt1)
10518598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
10618598Speter	if (fmt2)
10718598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
108696Spaul
1091741Srich	rval = 0;
11038648Sjdp	for ( ;  argc > 0;  argc--, argv++) {
111696Spaul		int	fd;
11238648Sjdp		union {
11338648Sjdp			struct exec aout;
11439354Sdfr			Elf_Ehdr elf;
11538648Sjdp		} hdr;
11638648Sjdp		int	n;
117696Spaul		int	status;
11838648Sjdp		int	file_ok;
119696Spaul
120696Spaul		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
1211741Srich			warn("%s", *argv);
122696Spaul			rval |= 1;
123696Spaul			continue;
124696Spaul		}
12538648Sjdp		if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
12638648Sjdp			warn("%s: can't read program header", *argv);
127696Spaul			(void)close(fd);
128696Spaul			rval |= 1;
129696Spaul			continue;
130696Spaul		}
13135575Sdfr
13238648Sjdp		file_ok = 1;
13338648Sjdp		if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
13435575Sdfr			/* a.out file */
13538648Sjdp			if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
13635575Sdfr#if 1 /* Compatibility */
13738648Sjdp			    || hdr.aout.a_entry < __LDPGSZ
13835575Sdfr#endif
13935575Sdfr				) {
14035575Sdfr				warnx("%s: not a dynamic executable", *argv);
14138648Sjdp				file_ok = 0;
14235575Sdfr			}
14338648Sjdp		} else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
14439354Sdfr			Elf_Ehdr ehdr;
14539354Sdfr			Elf_Phdr phdr;
14635575Sdfr			int dynamic = 0, i;
14735575Sdfr
14835575Sdfr			lseek(fd, 0, SEEK_SET);
14935575Sdfr			if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) {
15035575Sdfr				warnx("%s: can't read program header", *argv);
15138648Sjdp				file_ok = 0;
15235575Sdfr			}
15335575Sdfr			lseek(fd, 0, ehdr.e_phoff);
15435575Sdfr			for (i = 0; i < ehdr.e_phnum; i++) {
15535575Sdfr				if (read(fd, &phdr, ehdr.e_phentsize)
15635575Sdfr				   != sizeof phdr) {
15738648Sjdp					warnx("%s: can't read program header",
15838648Sjdp					    *argv);
15938648Sjdp					file_ok = 0;
16035575Sdfr				}
16135575Sdfr				if (phdr.p_type == PT_DYNAMIC)
16235575Sdfr					dynamic = 1;
16335575Sdfr			}
16435575Sdfr			if (!dynamic) {
16535575Sdfr				warnx("%s: not a dynamic executable", *argv);
16638648Sjdp				file_ok = 0;
16735575Sdfr			}
16838648Sjdp		} else {
16938648Sjdp			warnx("%s: not a dynamic executable", *argv);
17038648Sjdp			file_ok = 0;
17135575Sdfr		}
172696Spaul		(void)close(fd);
17338648Sjdp		if (!file_ok) {
17438648Sjdp			rval |= 1;
17538648Sjdp			continue;
17638648Sjdp		}
177696Spaul
17818598Speter		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
17918598Speter		if (fmt1 == NULL && fmt2 == NULL)
18018598Speter			/* Default formats */
18118598Speter			printf("%s:\n", *argv);
18218598Speter
1831153Sjkh		fflush(stdout);
184696Spaul
185696Spaul		switch (fork()) {
186696Spaul		case -1:
1871741Srich			err(1, "fork");
188696Spaul			break;
189696Spaul		default:
1901741Srich			if (wait(&status) <= 0) {
1911741Srich				warn("wait");
1921741Srich				rval |= 1;
1931741Srich			} else if (WIFSIGNALED(status)) {
194696Spaul				fprintf(stderr, "%s: signal %d\n",
195696Spaul						*argv, WTERMSIG(status));
196696Spaul				rval |= 1;
197696Spaul			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
198696Spaul				fprintf(stderr, "%s: exit status %d\n",
199696Spaul						*argv, WEXITSTATUS(status));
200696Spaul				rval |= 1;
201696Spaul			}
202696Spaul			break;
203696Spaul		case 0:
2041741Srich			rval |= execl(*argv, *argv, NULL) != 0;
20569827Scharnier			warn("%s", *argv);
206696Spaul			_exit(1);
207696Spaul		}
208696Spaul	}
209696Spaul
210696Spaul	return rval;
211696Spaul}
212