summaryrefslogtreecommitdiff
path: root/libexec/ld.so/ldd
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2000-09-17 17:50:58 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2000-09-17 17:50:58 +0000
commitab6dfee5bd566ceab3f3454f91a6a00c9ea5104b (patch)
tree96d99ce730d3ac579f9fecdd8e69048e54a4422a /libexec/ld.so/ldd
parent58cc392834945db9576ff91a4e74733f16ca1c2a (diff)
activate ldd, and indent some ld.so messages to make it easier to see what is going on
Diffstat (limited to 'libexec/ld.so/ldd')
-rw-r--r--libexec/ld.so/ldd/Makefile9
-rw-r--r--libexec/ld.so/ldd/ldd.172
-rw-r--r--libexec/ld.so/ldd/ldd.c259
3 files changed, 340 insertions, 0 deletions
diff --git a/libexec/ld.so/ldd/Makefile b/libexec/ld.so/ldd/Makefile
new file mode 100644
index 00000000000..b0ed6f7af69
--- /dev/null
+++ b/libexec/ld.so/ldd/Makefile
@@ -0,0 +1,9 @@
+# $OpenBSD: Makefile,v 1.1 2000/09/17 17:50:57 deraadt Exp $
+
+PROG= ldd
+SRCS= ldd.c
+MAN= ldd.1
+
+BINDIR= /usr/bin
+
+.include <bsd.prog.mk>
diff --git a/libexec/ld.so/ldd/ldd.1 b/libexec/ld.so/ldd/ldd.1
new file mode 100644
index 00000000000..d6621f3e613
--- /dev/null
+++ b/libexec/ld.so/ldd/ldd.1
@@ -0,0 +1,72 @@
+.\" $OpenBSD: ldd.1,v 1.1 2000/09/17 17:50:57 deraadt Exp $
+.\"
+.\" Copyright (c) 1996 Per Fogelstrom
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed under OpenBSD by
+.\" Per Fogelstrom.
+.\" 4. The name of the author may not be used to endorse or promote products
+.\" derived from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"
+.Dd October 4, 1996
+.Dt LDD 1
+.Os
+.Sh NAME
+.Nm ldd
+.Nd list dynamic object dependencies
+.Sh SYNOPSIS
+.Nm ldd
+.Op Fl x
+.Ar program
+.Sh DESCRIPTION
+.Nm ldd
+displays the shared objects needed to run
+.Ar program.
+.Nm ldd
+uses the
+.Nm DT_NEEDED
+tags to determine what dynamic objects are required. To list the objects
+.Nm ldd
+sets the environment variable
+.Nm LD_TRACE_LOADED_OBJECTS
+and then execs
+.Ar program Ns .
+.Pp
+If
+.Nm ldd
+is invoked with the
+.Nm -x
+flag, the tags from
+.Ar program
+are listed without using current ldconfig configuration.
+.Sh SEE ALSO
+.Xr ldconfig 8 ,
+.Xr ld.so 8
+.Sh DIAGNOSTICS
+Exit status 0 if no error. Exit status 1 if arg error. Exit status 2 if
+.Ar program
+can't be read. If
+.Nm ldd
+fails to open the program file a message is printed.
diff --git a/libexec/ld.so/ldd/ldd.c b/libexec/ld.so/ldd/ldd.c
new file mode 100644
index 00000000000..9c0720c0a47
--- /dev/null
+++ b/libexec/ld.so/ldd/ldd.c
@@ -0,0 +1,259 @@
+/* $OpenBSD: ldd.c,v 1.1 2000/09/17 17:50:57 deraadt Exp $ */
+
+/*
+ * Copyright (c) 1993 Paul Kranenburg
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Paul Kranenburg.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+/*
+ * Copyright (c) 1996 Per Fogelstrom
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed under OpenBSD by
+ * Per Fogelstrom.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+
+/* readsoname() adapted from Eric Youngdale's readelf program */
+
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <err.h>
+#include <fcntl.h>
+#include <elf_abi.h>
+
+int readsoneeded(FILE *f, int flag);
+
+void
+usage()
+{
+ extern char *__progname;
+
+ fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
+ exit(1);
+}
+
+int
+main(argc, argv)
+ int argc;
+ char **argv;
+{
+ FILE *fp;
+ int lflag = 0;
+ int rval;
+ int c;
+
+ while ((c = getopt(argc, argv, "l")) != EOF) {
+ switch (c) {
+ case 'l':
+ lflag = 1;
+ break;
+ default:
+ usage();
+ /*NOTREACHED*/
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc <= 0) {
+ usage();
+ /*NOTREACHED*/
+ }
+
+ if (setenv("LD_TRACE_LOADED_OBJECTS", "1", 1) == -1)
+ errx(1, "cannot setenv LD_TRACE_LOADED_OBJECTS");
+
+ rval = 0;
+ while (argc--) {
+ int fd;
+ int status;
+
+ if (lflag) {
+ if ((fp = fopen(*argv, "r")) == NULL) {
+ warn("%s", *argv);
+ rval |= 1;
+ argv++;
+ continue;
+ }
+ readsoneeded(fp, 0);
+ fclose(fp);
+ continue;
+ }
+ printf("%s:\n", *argv);
+ fflush(stdout);
+
+ switch (fork()) {
+ case -1:
+ err(1, "fork");
+ break;
+ default:
+ if (wait(&status) <= 0) {
+ warn("wait");
+ rval |= 1;
+ } else if (WIFSIGNALED(status)) {
+ fprintf(stderr, "%s: signal %d\n",
+ *argv, WTERMSIG(status));
+ rval |= 1;
+ } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
+ fprintf(stderr, "%s: exit status %d\n",
+ *argv, WEXITSTATUS(status));
+ rval |= 1;
+ }
+ break;
+ case 0:
+ rval |= execl(*argv, *argv, NULL) != 0;
+ perror(*argv);
+ _exit(1);
+ }
+ argv++;
+ }
+
+ return (rval ? 1 : 0);
+}
+
+int
+readsoneeded(FILE *infile, int dyncheck)
+{
+ Elf32_Ehdr *epnt;
+ Elf32_Phdr *ppnt;
+ int i;
+ int isdynamic = 0;
+ char *header;
+ unsigned int dynamic_addr = 0;
+ unsigned int dynamic_size = 0;
+ int strtab_val = 0;
+ int soname_val = 0;
+ int loadaddr = -1;
+ int loadbase = 0;
+ Elf32_Dyn *dpnt;
+ struct stat st;
+ char *res = NULL;
+
+ if (fstat(fileno(infile), &st))
+ return -1L;
+ header = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fileno(infile), 0);
+ if (header == (caddr_t)-1)
+ return -1;
+
+ epnt = (Elf32_Ehdr *)header;
+ if ((int)(epnt+1) > (int)(header + st.st_size))
+ goto skip;
+
+ ppnt = (Elf32_Phdr *)&header[epnt->e_phoff];
+ if ((int)ppnt < (int)header ||
+ (int)(ppnt+epnt->e_phnum) > (int)(header + st.st_size))
+ goto skip;
+
+ for (i = 0; i < epnt->e_phnum; i++) {
+ if (loadaddr == -1 && ppnt->p_vaddr != 0)
+ loadaddr = (ppnt->p_vaddr & 0xfffff000) -
+ (ppnt->p_offset & 0xfffff000);
+ if (ppnt->p_type == 2) {
+ dynamic_addr = ppnt->p_offset;
+ dynamic_size = ppnt->p_filesz;
+ }
+ ppnt++;
+ }
+
+ dpnt = (Elf32_Dyn *) &header[dynamic_addr];
+ dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
+ if ((int)dpnt < (int)header ||
+ (int)(dpnt+dynamic_size) > (int)(header + st.st_size))
+ goto skip;
+
+ while (dpnt->d_tag != DT_NULL) {
+ if (dpnt->d_tag == DT_STRTAB)
+ strtab_val = dpnt->d_un.d_val;
+#define DT_MIPS_BASE_ADDRESS 0x70000006 /* XXX */
+ if (dpnt->d_tag == DT_MIPS_BASE_ADDRESS)
+ loadbase = dpnt->d_un.d_val;
+ dpnt++;
+ }
+
+ if (!strtab_val)
+ goto skip;
+
+ dpnt = (Elf32_Dyn *) &header[dynamic_addr];
+ while (dpnt->d_tag != DT_NULL) {
+ if (dpnt->d_tag == DT_NEEDED) {
+ isdynamic = 1;
+ if (dyncheck)
+ break;
+ soname_val = dpnt->d_un.d_val;
+ if (soname_val != 0 &&
+ soname_val + strtab_val - loadbase >= 0 &&
+ soname_val + strtab_val - loadbase < st.st_size)
+ printf("%s\n",
+ header - loadbase + soname_val + strtab_val);
+ }
+ dpnt++;
+ }
+
+skip:
+ munmap(header, st.st_size);
+
+ return isdynamic;
+}
+