ldd.c revision 90755
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 90755 2002-02-17 07:04:32Z obrien $"; 3469827Scharnier#endif /* not lint */ 3569827Scharnier 36696Spaul#include <sys/wait.h> 3776224Sobrien#include <machine/elf.h> 38696Spaul#include <a.out.h> 3990172Ssobomax#include <dlfcn.h> 401741Srich#include <err.h> 411741Srich#include <fcntl.h> 421741Srich#include <stdio.h> 431741Srich#include <stdlib.h> 441741Srich#include <unistd.h> 45696Spaul 4629042Sjdpextern void dump_file __P((const char *)); 4718600Speterextern int error_count; 4818600Speter 49696Spaulvoid 50696Spaulusage() 51696Spaul{ 5290755Sobrien fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n"); 531741Srich exit(1); 54696Spaul} 55696Spaul 56696Spaulint 57696Spaulmain(argc, argv) 58696Spaulint argc; 59696Spaulchar *argv[]; 60696Spaul{ 6118598Speter char *fmt1 = NULL, *fmt2 = NULL; 621741Srich int rval; 63696Spaul int c; 6490755Sobrien int aflag, vflag; 65696Spaul 6690755Sobrien aflag = vflag = 0; 6790755Sobrien 6890755Sobrien while ((c = getopt(argc, argv, "avf:")) != -1) { 69696Spaul switch (c) { 7090755Sobrien case 'a': 7190755Sobrien aflag++; 7290755Sobrien break; 7318600Speter case 'v': 7418600Speter vflag++; 7518600Speter break; 7618598Speter case 'f': 7718598Speter if (fmt1) { 7818598Speter if (fmt2) 7969827Scharnier errx(1, "too many formats"); 8018598Speter fmt2 = optarg; 8118598Speter } else 8218598Speter fmt1 = optarg; 8318598Speter break; 84696Spaul default: 85696Spaul usage(); 861741Srich /*NOTREACHED*/ 87696Spaul } 88696Spaul } 89696Spaul argc -= optind; 90696Spaul argv += optind; 91696Spaul 9218600Speter if (vflag && fmt1) 9318600Speter errx(1, "-v may not be used with -f"); 9418600Speter 95696Spaul if (argc <= 0) { 96696Spaul usage(); 971741Srich /*NOTREACHED*/ 98696Spaul } 99696Spaul 10039354Sdfr#ifdef __i386__ 10118600Speter if (vflag) { 10218600Speter for (c = 0; c < argc; c++) 10318600Speter dump_file(argv[c]); 10418600Speter exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 10518600Speter } 10639354Sdfr#endif 10718600Speter 108696Spaul /* ld.so magic */ 10990755Sobrien setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1); 11018598Speter if (fmt1) 11118598Speter setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1); 11218598Speter if (fmt2) 11318598Speter setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1); 114696Spaul 1151741Srich rval = 0; 11638648Sjdp for ( ; argc > 0; argc--, argv++) { 117696Spaul int fd; 11838648Sjdp union { 11938648Sjdp struct exec aout; 12039354Sdfr Elf_Ehdr elf; 12138648Sjdp } hdr; 12238648Sjdp int n; 123696Spaul int status; 12438648Sjdp int file_ok; 12590172Ssobomax int is_shlib; 126696Spaul 127696Spaul if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 1281741Srich warn("%s", *argv); 129696Spaul rval |= 1; 130696Spaul continue; 131696Spaul } 13238648Sjdp if ((n = read(fd, &hdr, sizeof hdr)) == -1) { 13338648Sjdp warn("%s: can't read program header", *argv); 134696Spaul (void)close(fd); 135696Spaul rval |= 1; 136696Spaul continue; 137696Spaul } 13835575Sdfr 13938648Sjdp file_ok = 1; 14090172Ssobomax is_shlib = 0; 14138648Sjdp if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) { 14235575Sdfr /* a.out file */ 14338648Sjdp if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC 14435575Sdfr#if 1 /* Compatibility */ 14538648Sjdp || hdr.aout.a_entry < __LDPGSZ 14635575Sdfr#endif 14735575Sdfr ) { 14835575Sdfr warnx("%s: not a dynamic executable", *argv); 14938648Sjdp file_ok = 0; 15035575Sdfr } 15138648Sjdp } else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) { 15239354Sdfr Elf_Ehdr ehdr; 15339354Sdfr Elf_Phdr phdr; 15435575Sdfr int dynamic = 0, i; 15535575Sdfr 15670049Sache if (lseek(fd, 0, SEEK_SET) == -1 || 15770049Sache read(fd, &ehdr, sizeof ehdr) != sizeof ehdr || 15870049Sache lseek(fd, ehdr.e_phoff, SEEK_SET) == -1 15970049Sache ) { 16035575Sdfr warnx("%s: can't read program header", *argv); 16138648Sjdp file_ok = 0; 16270049Sache } else { 16370049Sache for (i = 0; i < ehdr.e_phnum; i++) { 16470049Sache if (read(fd, &phdr, ehdr.e_phentsize) 16570049Sache != sizeof phdr) { 16670049Sache warnx("%s: can't read program header", 16770049Sache *argv); 16870049Sache file_ok = 0; 16970049Sache break; 17070049Sache } 17170049Sache if (phdr.p_type == PT_DYNAMIC) 17270049Sache dynamic = 1; 17335575Sdfr } 17435575Sdfr } 17535575Sdfr if (!dynamic) { 17635575Sdfr warnx("%s: not a dynamic executable", *argv); 17738648Sjdp file_ok = 0; 17890172Ssobomax } else if (hdr.elf.e_type == ET_DYN) { 17990385Ssobomax if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) { 18090385Ssobomax is_shlib = 1; 18190385Ssobomax } else { 18290385Ssobomax warnx("%s: not a FreeBSD ELF shared " 18390385Ssobomax "object", *argv); 18490385Ssobomax file_ok = 0; 18590385Ssobomax } 18635575Sdfr } 18738648Sjdp } else { 18838648Sjdp warnx("%s: not a dynamic executable", *argv); 18938648Sjdp file_ok = 0; 19035575Sdfr } 191696Spaul (void)close(fd); 19238648Sjdp if (!file_ok) { 19338648Sjdp rval |= 1; 19438648Sjdp continue; 19538648Sjdp } 196696Spaul 19718598Speter setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1); 19890755Sobrien if (aflag) setenv("LD_TRACE_LOADED_OBJECTS_ALL", "1", 1); 19990755Sobrien else if (fmt1 == NULL && fmt2 == NULL) 20018598Speter /* Default formats */ 20118598Speter printf("%s:\n", *argv); 20218598Speter 2031153Sjkh fflush(stdout); 204696Spaul 205696Spaul switch (fork()) { 206696Spaul case -1: 2071741Srich err(1, "fork"); 208696Spaul break; 209696Spaul default: 2101741Srich if (wait(&status) <= 0) { 2111741Srich warn("wait"); 2121741Srich rval |= 1; 2131741Srich } else if (WIFSIGNALED(status)) { 214696Spaul fprintf(stderr, "%s: signal %d\n", 215696Spaul *argv, WTERMSIG(status)); 216696Spaul rval |= 1; 217696Spaul } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 218696Spaul fprintf(stderr, "%s: exit status %d\n", 219696Spaul *argv, WEXITSTATUS(status)); 220696Spaul rval |= 1; 221696Spaul } 222696Spaul break; 223696Spaul case 0: 22490172Ssobomax if (is_shlib == 0) { 22590172Ssobomax execl(*argv, *argv, (char *)NULL); 22690172Ssobomax warn("%s", *argv); 22790172Ssobomax } else { 22890172Ssobomax dlopen(*argv, RTLD_TRACE); 22990172Ssobomax warnx("%s: %s", *argv, dlerror()); 23090172Ssobomax } 231696Spaul _exit(1); 232696Spaul } 233696Spaul } 234696Spaul 235696Spaul return rval; 236696Spaul} 237