1/* 2 * Copyright (c) 1993 Paul Kranenburg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#ifndef lint 32static const char rcsid[] = |
33 "$FreeBSD: head/usr.bin/ldd/ldd.c 90172 2002-02-04 10:33:48Z sobomax $"; |
34#endif /* not lint */ 35 36#include <sys/wait.h> 37#include <machine/elf.h> 38#include <a.out.h> |
39#include <dlfcn.h> |
40#include <err.h> 41#include <fcntl.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <unistd.h> 45 46extern void dump_file __P((const char *)); 47extern int error_count; --- 64 unchanged lines hidden (view full) --- 112 int fd; 113 union { 114 struct exec aout; 115 Elf_Ehdr elf; 116 } hdr; 117 int n; 118 int status; 119 int file_ok; |
120 int is_shlib; |
121 122 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 123 warn("%s", *argv); 124 rval |= 1; 125 continue; 126 } 127 if ((n = read(fd, &hdr, sizeof hdr)) == -1) { 128 warn("%s: can't read program header", *argv); 129 (void)close(fd); 130 rval |= 1; 131 continue; 132 } 133 134 file_ok = 1; |
135 is_shlib = 0; |
136 if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) { 137 /* a.out file */ 138 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC 139#if 1 /* Compatibility */ 140 || hdr.aout.a_entry < __LDPGSZ 141#endif 142 ) { 143 warnx("%s: not a dynamic executable", *argv); --- 21 unchanged lines hidden (view full) --- 165 } 166 if (phdr.p_type == PT_DYNAMIC) 167 dynamic = 1; 168 } 169 } 170 if (!dynamic) { 171 warnx("%s: not a dynamic executable", *argv); 172 file_ok = 0; |
173 } else if (hdr.elf.e_type == ET_DYN) { 174 is_shlib = 1; |
175 } 176 } else { 177 warnx("%s: not a dynamic executable", *argv); 178 file_ok = 0; 179 } 180 (void)close(fd); 181 if (!file_ok) { 182 rval |= 1; --- 21 unchanged lines hidden (view full) --- 204 rval |= 1; 205 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 206 fprintf(stderr, "%s: exit status %d\n", 207 *argv, WEXITSTATUS(status)); 208 rval |= 1; 209 } 210 break; 211 case 0: |
212 if (is_shlib == 0) { 213 execl(*argv, *argv, (char *)NULL); 214 warn("%s", *argv); 215 } else { 216 dlopen(*argv, RTLD_TRACE); 217 warnx("%s: %s", *argv, dlerror()); 218 } |
219 _exit(1); 220 } 221 } 222 223 return rval; 224} |