xref: /netbsd-src/usr.bin/ldd/ldd_elfxx.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ldd_elfxx.c,v 1.1 2009/01/06 03:59:56 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright 1996 John D. Polstra.
34  * Copyright 1996 Matt Thomas <matt@3am-software.com>
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *      This product includes software developed by John Polstra.
48  * 4. The name of the author may not be used to endorse or promote products
49  *    derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 #ifndef lint
65 __RCSID("$NetBSD: ldd_elfxx.c,v 1.1 2009/01/06 03:59:56 mrg Exp $");
66 #endif /* not lint */
67 
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/wait.h>
71 
72 #include <a.out.h>
73 #include <dirent.h>
74 #include <err.h>
75 #include <errno.h>
76 #include <fcntl.h>
77 #include <stdarg.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 #include <ctype.h>
83 
84 #include "debug.h"
85 #include "rtld.h"
86 #include "ldd.h"
87 
88 /*
89  * elfxx_ldd() - bit-size independant ELF ldd implementation.
90  * returns 0 on success and -1 on failure.
91  */
92 int
93 ELFNAME(ldd)(char *path, char *fmt1, char *fmt2)
94 {
95 	struct stat st;
96 	int fd;
97 
98 	fd = open(path, O_RDONLY);
99 	if (fd == -1) {
100 		warn("%s", path);
101 		return -1;
102 	}
103 	if (fstat(fd, &st) < 0) {
104 		warn("%s", path);
105 		close(fd);
106 		return -1;
107 	}
108 
109 	_rtld_pagesz = sysconf(_SC_PAGESIZE);
110 
111 #ifdef RTLD_ARCH_SUBDIR
112 	_rtld_add_paths(path, &_rtld_default_paths,
113 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
114 #endif
115 	_rtld_add_paths(path, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
116 
117 	_rtld_paths = NULL;
118 	_rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
119 	if (_rtld_trust)
120 		_rtld_add_paths(path, &_rtld_paths, getenv("LD_LIBRARY_PATH"));
121 
122 	_rtld_process_hints(path, &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
123 	_rtld_objmain = _rtld_map_object(xstrdup(path), fd, &st);
124 	if (_rtld_objmain == NULL) {
125 		close(fd);
126 		return -1;
127 	}
128 	close(fd);
129 
130 	_rtld_objmain->path = xstrdup(path);
131 	_rtld_digest_dynamic(path, _rtld_objmain);
132 
133 	/* Link the main program into the list of objects. */
134 	*_rtld_objtail = _rtld_objmain;
135 	_rtld_objtail = &_rtld_objmain->next;
136 	++_rtld_objmain->refcount;
137 
138 	(void) _rtld_load_needed_objects(_rtld_objmain, 0);
139 
140 	if (fmt1 == NULL)
141 		printf("%s:\n", _rtld_objmain->path);
142 	main_local = path;
143 	main_progname = _rtld_objmain->path;
144 	print_needed(_rtld_objmain, fmt1, fmt2);
145 
146 	while (_rtld_objlist != NULL) {
147 		Obj_Entry *obj = _rtld_objlist;
148 		_rtld_objlist = obj->next;
149 		while (obj->rpaths != NULL) {
150 			const Search_Path *rpath = obj->rpaths;
151 			obj->rpaths = rpath->sp_next;
152 			xfree(__UNCONST(rpath->sp_path));
153 			xfree(__UNCONST(rpath));
154 		}
155 		while (obj->needed != NULL) {
156 			const Needed_Entry *needed = obj->needed;
157 			obj->needed = needed->next;
158 			xfree(__UNCONST(needed));
159 		}
160 		(void) munmap(obj->mapbase, obj->mapsize);
161 		xfree(obj->path);
162 		xfree(obj);
163 	}
164 
165 	_rtld_objmain = NULL;
166 	_rtld_objtail = &_rtld_objlist;
167 	/* Need to free _rtld_paths? */
168 
169 	return 0;
170 }
171