Drop main() prototype. Syncs with NetBSD-8
[minix.git] / usr.bin / ldd / ldd.c
blob7c97b7b02c955ee94f79b65c3b118f58c980c17f
1 /* $NetBSD: ldd.c,v 1.22 2014/03/02 03:55:19 matt Exp $ */
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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.
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.
33 * Copyright 1996 John D. Polstra.
34 * Copyright 1996 Matt Thomas <matt@3am-software.com>
35 * All rights reserved.
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.
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.
63 #include <sys/cdefs.h>
64 #ifndef lint
65 __RCSID("$NetBSD: ldd.c,v 1.22 2014/03/02 03:55:19 matt Exp $");
66 #endif /* not lint */
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/wait.h>
72 #include <dirent.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <fcntl.h>
76 #include <stdarg.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81 #include <ctype.h>
83 #include "debug.h"
84 #include "rtld.h"
85 #include "ldd.h"
88 * Data declarations.
90 static char *error_message; /* Message for dlopen(), or NULL */
91 bool _rtld_trust; /* False for setuid and setgid programs */
93 * This may be ELF64 or ELF32 but since they are used opaquely it doesn't
94 * really matter.
96 Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
97 Obj_Entry **_rtld_objtail = &_rtld_objlist;
98 /* Link field of last object in list */
99 u_int _rtld_objcount; /* Number of shared objects */
100 u_int _rtld_objloads; /* Number of objects loaded */
102 Obj_Entry *_rtld_objmain; /* The main program shared object */
103 size_t _rtld_pagesz;
105 Search_Path *_rtld_default_paths;
106 Search_Path *_rtld_paths;
107 Library_Xform *_rtld_xforms;
109 static void usage(void) __dead;
110 char *main_local;
111 char *main_progname;
113 static void
114 usage(void)
116 fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
117 " ...\n", getprogname());
118 exit(1);
122 main(int argc, char **argv)
124 const char *fmt1 = NULL, *fmt2 = NULL;
125 int c;
127 #ifdef DEBUG
128 debug = 1;
129 #endif
130 while ((c = getopt(argc, argv, "f:o")) != -1) {
131 switch (c) {
132 case 'f':
133 if (fmt1) {
134 if (fmt2)
135 errx(1, "Too many formats");
136 fmt2 = optarg;
137 } else
138 fmt1 = optarg;
139 break;
140 case 'o':
141 if (fmt1 || fmt2)
142 errx(1, "Cannot use -o and -f together");
143 fmt1 = "%a:-l%o.%m => %p\n";
144 break;
145 default:
146 usage();
147 /*NOTREACHED*/
150 argc -= optind;
151 argv += optind;
153 if (argc <= 0) {
154 usage();
155 /*NOTREACHED*/
158 for (; argc != 0; argc--, argv++) {
159 int fd;
161 fd = open(*argv, O_RDONLY);
162 if (fd == -1) {
163 warn("%s", *argv);
164 continue;
166 if (elf_ldd(fd, *argv, fmt1, fmt2) == -1
167 /* Alpha never had 32 bit support. */
168 #if (defined(_LP64) && !defined(ELF64_ONLY)) || defined(MIPS_N32)
169 && elf32_ldd(fd, *argv, fmt1, fmt2) == -1
170 #if defined(__mips__) && 0 /* XXX this is still hosed for some reason */
171 && elf32_ldd_compat(fd, *argv, fmt1, fmt2) == -1
172 #endif
173 #endif
175 warnx("%s", error_message);
176 close(fd);
179 return 0;
183 * Error reporting function. Use it like printf. If formats the message
184 * into a buffer, and sets things up so that the next call to dlerror()
185 * will return the message.
187 void
188 _rtld_error(const char *fmt, ...)
190 static char buf[512];
191 va_list ap;
192 va_start(ap, fmt);
193 xvsnprintf(buf, sizeof buf, fmt, ap);
194 error_message = buf;
195 va_end(ap);
198 char *
199 dlerror()
201 char *msg = error_message;
202 error_message = NULL;
203 return msg;
206 void
207 _rtld_die(void)
209 const char *msg = dlerror();
211 if (msg == NULL)
212 msg = "Fatal error";
213 xerrx(1, "%s", msg);
216 void
217 _rtld_shared_enter(void)
221 void
222 _rtld_shared_exit(void)
226 void
227 _rtld_exclusive_enter(sigset_t *mask)
231 void
232 _rtld_exclusive_exit(sigset_t *mask)