ldd.c (180877) | ldd.c (181136) |
---|---|
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 --- 15 unchanged lines hidden (view full) --- 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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#include <sys/cdefs.h> | 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 --- 15 unchanged lines hidden (view full) --- 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: head/usr.bin/ldd/ldd.c 180877 2008-07-28 12:49:16Z edwin $"); | 32__FBSDID("$FreeBSD: head/usr.bin/ldd/ldd.c 181136 2008-08-01 21:52:41Z jhb $"); |
33 34#include <sys/wait.h> 35 36#include <machine/elf.h> 37 38#include <arpa/inet.h> 39 40#include <a.out.h> 41#include <dlfcn.h> 42#include <err.h> 43#include <errno.h> 44#include <fcntl.h> 45#include <stdio.h> 46#include <stdlib.h> | 33 34#include <sys/wait.h> 35 36#include <machine/elf.h> 37 38#include <arpa/inet.h> 39 40#include <a.out.h> 41#include <dlfcn.h> 42#include <err.h> 43#include <errno.h> 44#include <fcntl.h> 45#include <stdio.h> 46#include <stdlib.h> |
47#include <string.h> |
|
47#include <unistd.h> 48 49#include "extern.h" 50 | 48#include <unistd.h> 49 50#include "extern.h" 51 |
52#ifdef COMPAT_32BIT 53#define LD_ "LD_32_" 54#else 55#define LD_ "LD_" 56#endif 57 |
|
51/* 52 * 32-bit ELF data structures can only be used if the system header[s] declare 53 * them. There is no official macro for determining whether they are declared, 54 * so check for the existence of one of the 32-macros defined in elf(5). 55 */ 56#ifdef ELF32_R_TYPE 57#define ELF32_SUPPORTED 58#endif 59 60static int is_executable(const char *fname, int fd, int *is_shlib, 61 int *type); 62static void usage(void); 63 64#define TYPE_UNKNOWN 0 65#define TYPE_AOUT 1 66#define TYPE_ELF 2 /* Architecture default */ 67#if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED) 68#define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */ | 58/* 59 * 32-bit ELF data structures can only be used if the system header[s] declare 60 * them. There is no official macro for determining whether they are declared, 61 * so check for the existence of one of the 32-macros defined in elf(5). 62 */ 63#ifdef ELF32_R_TYPE 64#define ELF32_SUPPORTED 65#endif 66 67static int is_executable(const char *fname, int fd, int *is_shlib, 68 int *type); 69static void usage(void); 70 71#define TYPE_UNKNOWN 0 72#define TYPE_AOUT 1 73#define TYPE_ELF 2 /* Architecture default */ 74#if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED) 75#define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */ |
69#endif | |
70 | 76 |
71#define ENV_OBJECTS 0 72#define ENV_OBJECTS_FMT1 1 73#define ENV_OBJECTS_FMT2 2 74#define ENV_OBJECTS_PROGNAME 3 75#define ENV_OBJECTS_ALL 4 76#define ENV_LAST 5 | 77#define _PATH_LDD32 "/usr/bin/ldd32" |
77 | 78 |
78const char *envdef[ENV_LAST] = { 79 "LD_TRACE_LOADED_OBJECTS", 80 "LD_TRACE_LOADED_OBJECTS_FMT1", 81 "LD_TRACE_LOADED_OBJECTS_FMT2", 82 "LD_TRACE_LOADED_OBJECTS_PROGNAME", 83 "LD_TRACE_LOADED_OBJECTS_ALL", 84}; 85#if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED) 86const char *env32[ENV_LAST] = { 87 "LD_32_TRACE_LOADED_OBJECTS", 88 "LD_32_TRACE_LOADED_OBJECTS_FMT1", 89 "LD_32_TRACE_LOADED_OBJECTS_FMT2", 90 "LD_32_TRACE_LOADED_OBJECTS_PROGNAME", 91 "LD_32_TRACE_LOADED_OBJECTS_ALL", 92}; | 79static int 80execldd32(char *file, char *fmt1, char *fmt2, int aflag, int vflag) 81{ 82 char *argv[8]; 83 int i, rval, status; 84 85 unsetenv(LD_ "TRACE_LOADED_OBJECTS"); 86 87 rval = 0; 88 i = 0; 89 argv[i++] = strdup(_PATH_LDD32); 90 if (aflag) 91 argv[i++] = strdup("-a"); 92 if (vflag) 93 argv[i++] = strdup("-v"); 94 if (fmt1) { 95 argv[i++] = strdup("-f"); 96 argv[i++] = strdup(fmt1); 97 } 98 if (fmt2) { 99 argv[i++] = strdup("-f"); 100 argv[i++] = strdup(fmt2); 101 } 102 103 argv[i++] = strdup(file); 104 argv[i++] = NULL; 105 106 switch (fork()) { 107 case -1: 108 err(1, "fork"); 109 break; 110 case 0: 111 execv(_PATH_LDD32, argv); 112 warn("%s", _PATH_LDD32); 113 _exit(1); 114 break; 115 default: 116 if (wait(&status) <= 0) { 117 rval = 1; 118 } else if (WIFSIGNALED(status)) { 119 rval = 1; 120 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 121 rval = 1; 122 } 123 break; 124 } 125 while (i--) 126 free(argv[i]); 127 setenv(LD_ "TRACE_LOADED_OBJECTS", "yes", 1); 128 return (rval); 129} |
93#endif 94 95int 96main(int argc, char *argv[]) 97{ 98 char *fmt1, *fmt2; 99 int rval, c, aflag, vflag; 100 --- 38 unchanged lines hidden (view full) --- 139 dump_file(argv[c]); 140 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 141 } 142#endif 143 144 rval = 0; 145 for (; argc > 0; argc--, argv++) { 146 int fd, status, is_shlib, rv, type; | 130#endif 131 132int 133main(int argc, char *argv[]) 134{ 135 char *fmt1, *fmt2; 136 int rval, c, aflag, vflag; 137 --- 38 unchanged lines hidden (view full) --- 176 dump_file(argv[c]); 177 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 178 } 179#endif 180 181 rval = 0; 182 for (; argc > 0; argc--, argv++) { 183 int fd, status, is_shlib, rv, type; |
147 const char **env; | |
148 149 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 150 warn("%s", *argv); 151 rval |= 1; 152 continue; 153 } 154 rv = is_executable(*argv, fd, &is_shlib, &type); 155 close(fd); 156 if (rv == 0) { 157 rval |= 1; 158 continue; 159 } 160 161 switch (type) { 162 case TYPE_ELF: 163 case TYPE_AOUT: | 184 185 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 186 warn("%s", *argv); 187 rval |= 1; 188 continue; 189 } 190 rv = is_executable(*argv, fd, &is_shlib, &type); 191 close(fd); 192 if (rv == 0) { 193 rval |= 1; 194 continue; 195 } 196 197 switch (type) { 198 case TYPE_ELF: 199 case TYPE_AOUT: |
164 env = envdef; | |
165 break; 166#if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED) 167 case TYPE_ELF32: | 200 break; 201#if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED) 202 case TYPE_ELF32: |
168 env = env32; 169 break; | 203 rval |= execldd32(*argv, fmt1, fmt2, aflag, vflag); 204 continue; |
170#endif 171 case TYPE_UNKNOWN: 172 default: 173 /* 174 * This shouldn't happen unless is_executable() 175 * is broken. 176 */ 177 errx(EDOOFUS, "unknown executable type"); 178 } 179 180 /* ld.so magic */ | 205#endif 206 case TYPE_UNKNOWN: 207 default: 208 /* 209 * This shouldn't happen unless is_executable() 210 * is broken. 211 */ 212 errx(EDOOFUS, "unknown executable type"); 213 } 214 215 /* ld.so magic */ |
181 setenv(env[ENV_OBJECTS], "yes", 1); | 216 setenv(LD_ "TRACE_LOADED_OBJECTS", "yes", 1); |
182 if (fmt1 != NULL) | 217 if (fmt1 != NULL) |
183 setenv(env[ENV_OBJECTS_FMT1], fmt1, 1); | 218 setenv(LD_ "TRACE_LOADED_OBJECTS_FMT1", fmt1, 1); |
184 if (fmt2 != NULL) | 219 if (fmt2 != NULL) |
185 setenv(env[ENV_OBJECTS_FMT2], fmt2, 1); | 220 setenv(LD_ "TRACE_LOADED_OBJECTS_FMT2", fmt2, 1); |
186 | 221 |
187 setenv(env[ENV_OBJECTS_PROGNAME], *argv, 1); | 222 setenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1); |
188 if (aflag) | 223 if (aflag) |
189 setenv(env[ENV_OBJECTS_ALL], "1", 1); | 224 setenv(LD_ "TRACE_LOADED_OBJECTS_ALL", "1", 1); |
190 else if (fmt1 == NULL && fmt2 == NULL) 191 /* Default formats */ 192 printf("%s:\n", *argv); 193 fflush(stdout); 194 195 switch (fork()) { 196 case -1: 197 err(1, "fork"); --- 159 unchanged lines hidden --- | 225 else if (fmt1 == NULL && fmt2 == NULL) 226 /* Default formats */ 227 printf("%s:\n", *argv); 228 fflush(stdout); 229 230 switch (fork()) { 231 case -1: 232 err(1, "fork"); --- 159 unchanged lines hidden --- |