- get rid of building an a.out-happy ldd (but keep supporting a.out

binaries for the ELF version.)

- convert a.out, elf32 and elf64 support into convience libraries and
  link them right ones in.  if elf_ldd() fails on 64 bit platforms,
  try elf32_ldd() before aout_ldd().


now ldd on 64 bit platforms works for 32 bit binaries, aka PR#40199,
and it seems that all the issues from README are gone too.
This commit is contained in:
mrg 2009-01-06 03:59:56 +00:00
parent 268cca30b1
commit 648c936726
14 changed files with 472 additions and 354 deletions

View File

@ -1,14 +1,39 @@
# $NetBSD: Makefile,v 1.7 1998/12/15 22:07:11 pk Exp $
#
# ldd(1) is toolchain dependent
#
# $NetBSD: Makefile,v 1.8 2009/01/06 03:59:56 mrg Exp $
.include <bsd.own.mk> # get OBJECT_FMT definition
.include <bsd.own.mk> # for MKDYNAMICROOT definition
.if (${OBJECT_FMT} == "ELF")
SUBDIR+= ldd_elf
.else
SUBDIR+= ldd_aout
PROG= ldd
SRCS= ldd.c
MAN= ldd.1
SUBDIR= aout
LIB_AOUTDIR!= cd ${.CURDIR}/aout && ${PRINTOBJDIR}
EXTRA_LIBS+= ${LIB_AOUTDIR}/libldd_aout.a
.if (${MACHINE_ARCH} != "alpha")
SUBDIR+=elf32
LIB_ELF32DIR!= cd ${.CURDIR}/elf32 && ${PRINTOBJDIR}
EXTRA_LIBS+= ${LIB_ELF32DIR}/libldd_elf32.a
.endif
.if (${MACHINE_ARCH} == "alpha") || (${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "x86_64") || (${MACHINE_ARCH} == "powerpc64")
SUBDIR+=elf64
LIB_ELF64DIR!= cd ${.CURDIR}/elf64 && ${PRINTOBJDIR}
EXTRA_LIBS+= ${LIB_ELF64DIR}/libldd_elf64.a
CPPFLAGS.ldd.c= -DELFSIZE=64
.else
CPPFLAGS.ldd.c= -DELFSIZE=32
.endif
LDADD+= ${EXTRA_LIBS}
DPADD+= ${EXTRA_LIBS}
.include "Makefile.common"
.if (${MKDYNAMICROOT} == "no")
LDSTATIC?= -static
.endif
.include <bsd.prog.mk>
.include <bsd.subdir.mk>

View File

@ -0,0 +1,13 @@
# $NetBSD: Makefile.common,v 1.1 2009/01/06 03:59:56 mrg Exp $
LDELFSO=${NETBSDSRCDIR}/libexec/ld.elf_so
CPPFLAGS+= -I${LDELFSO} -DLIBDIR=\"${LIBDIR}\"
CPPFLAGS+= -D_RTLD_SOURCE
#CPPFLAGS+= -DDEBUG
.PATH: ${LDELFSO}
.if (${MACHINE_ARCH} == "sparc") || (${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "arm") || (${MACHINE_ARCH} == "m68k") || \
(${MACHINE_ARCH} == "powerpc")
CPPFLAGS+= -DVARPSZ
.endif

16
usr.bin/ldd/Makefile.elf Normal file
View File

@ -0,0 +1,16 @@
# $NetBSD: Makefile.elf,v 1.1 2009/01/06 03:59:56 mrg Exp $
# Makefile fragment to build a (32 or 64 bit) libldd_elfxx.a.
# Expects CPPFLAGS to have ELFSIZE set, and LIB to be set.
LIBISPRIVATE= yes
SRCS= ldd_elfxx.c
.PATH: ${.CURDIR}/..
SRCS+= xmalloc.c debug.c expand.c map_object.c load.c search.c \
headers.c paths.c
.include "Makefile.common"
.include <bsd.lib.mk>

10
usr.bin/ldd/aout/Makefile Normal file
View File

@ -0,0 +1,10 @@
# $NetBSD: Makefile,v 1.1 2009/01/06 03:59:56 mrg Exp $
LIBISPRIVATE= yes
LIB= ldd_aout
SRCS= ${LIB}.c
.PATH: ${.CURDIR}/..
.include <bsd.lib.mk>

View File

@ -0,0 +1,17 @@
# $NetBSD: Makefile,v 1.1 2009/01/06 03:59:56 mrg Exp $
.if ${MACHINE} != "alpha"
CPPFLAGS+= -DELFSIZE=32
LIB= ldd_elf32
# XXX
.if ${MACHINE} == "sparc64"
.include "../../../libexec/ld.elf_so/arch/sparc/Makefile.ld32"
.elif ${MACHINE} == "amd64"
.include "../../../libexec/ld.elf_so/arch/i386/Makefile.ld32"
.endif
.include "../Makefile.elf"
.endif

View File

@ -0,0 +1,32 @@
# $NetBSD: Makefile,v 1.1 2009/01/06 03:59:56 mrg Exp $
.if (${MACHINE_ARCH} == "alpha") || (${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "x86_64") || (${MACHINE_ARCH} == "powerpc64")
CPPFLAGS+= -DELFSIZE=64
LIB= ldd_elf64
# XXX we need to make sure that we don't accidentally get the elf32
# XXX versions of these.
RTLD_FUNCS = \
_rtld_expand_path \
_rtld_digest_dynamic \
_rtld_digest_phdr \
_rtld_load_needed_objects \
_rtld_load_object \
_rtld_map_object \
_rtld_obj_free \
_rtld_obj_new \
_rtld_add_paths \
_rtld_process_hints \
_rtld_sysctl \
_rtld_load_library
.for _d in ${RTLD_FUNCS}
CPPFLAGS+= -D${_d}=_elf64_${_d}
.endfor
.include "../Makefile.elf"
.endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: ldd.c,v 1.32 2008/04/28 20:24:14 martin Exp $ */
/* $NetBSD: ldd.c,v 1.3 2009/01/06 03:59:56 mrg Exp $ */
/*-
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@ -62,7 +62,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ldd.c,v 1.32 2008/04/28 20:24:14 martin Exp $");
__RCSID("$NetBSD: ldd.c,v 1.3 2009/01/06 03:59:56 mrg Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -83,6 +83,7 @@ __RCSID("$NetBSD: ldd.c,v 1.32 2008/04/28 20:24:14 martin Exp $");
#include "debug.h"
#include "rtld.h"
#include "ldd.h"
/*
* Data declarations.
@ -99,12 +100,9 @@ Search_Path *_rtld_default_paths;
Search_Path *_rtld_paths;
Library_Xform *_rtld_xforms;
static void fmtprint(const char *, Obj_Entry *, const char *, const char *);
static void print_needed(Obj_Entry *, const char *, const char *);
static int ldd_aout(char *, char *, char *, int);
static void usage(void) __dead;
static char *main_local;
static char *main_progname;
char *main_local;
char *main_progname;
static void
usage(void)
@ -117,7 +115,6 @@ usage(void)
int
main(int argc, char **argv)
{
struct stat st;
char *fmt1 = NULL, *fmt2 = NULL;
int c;
@ -147,74 +144,14 @@ main(int argc, char **argv)
/*NOTREACHED*/
}
_rtld_pagesz = sysconf(_SC_PAGESIZE);
_rtld_add_paths(argv[0], &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
for (; argc != 0; argc--, argv++)
if (elf_ldd(*argv, fmt1, fmt2) == -1 &&
#if defined(_LP64)
elf32_ldd(*argv, fmt1, fmt2) == -1 &&
#endif
aout_ldd(*argv, fmt1, fmt2) == -1)
warnx("%s", error_message);
for (; argc != 0; argc--, argv++) {
int fd = open(*argv, O_RDONLY);
if (fd == -1) {
warn("%s", *argv);
continue;
}
if (fstat(fd, &st) < 0) {
warn("%s", *argv);
close(fd);
continue;
}
_rtld_paths = NULL;
_rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
if (_rtld_trust)
_rtld_add_paths(argv[0], &_rtld_paths, getenv("LD_LIBRARY_PATH"));
_rtld_process_hints(argv[0], &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
_rtld_objmain = _rtld_map_object(xstrdup(*argv), fd, &st);
if (_rtld_objmain == NULL) {
if (ldd_aout(*argv, fmt1, fmt2, fd) < 0)
warnx("%s", error_message);
close(fd);
continue;
}
close(fd);
_rtld_objmain->path = xstrdup(*argv);
_rtld_digest_dynamic(argv[0], _rtld_objmain);
/* Link the main program into the list of objects. */
*_rtld_objtail = _rtld_objmain;
_rtld_objtail = &_rtld_objmain->next;
++_rtld_objmain->refcount;
(void) _rtld_load_needed_objects(_rtld_objmain, 0);
if (fmt1 == NULL)
printf("%s:\n", _rtld_objmain->path);
main_local = *argv;
main_progname = _rtld_objmain->path;
print_needed(_rtld_objmain, fmt1, fmt2);
while (_rtld_objlist != NULL) {
Obj_Entry *obj = _rtld_objlist;
_rtld_objlist = obj->next;
while (obj->rpaths != NULL) {
const Search_Path *rpath = obj->rpaths;
obj->rpaths = rpath->sp_next;
xfree(__UNCONST(rpath->sp_path));
xfree(__UNCONST(rpath));
}
while (obj->needed != NULL) {
const Needed_Entry *needed = obj->needed;
obj->needed = needed->next;
xfree(__UNCONST(needed));
}
(void) munmap(obj->mapbase, obj->mapsize);
xfree(obj->path);
xfree(obj);
}
_rtld_objmain = NULL;
_rtld_objtail = &_rtld_objlist;
}
return 0;
}
@ -242,7 +179,7 @@ dlerror()
return msg;
}
static void
void
fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
const char *fmt2)
{
@ -327,7 +264,7 @@ fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
}
}
static void
void
print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
{
const Needed_Entry *needed;
@ -346,58 +283,3 @@ print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
}
}
}
static int
ldd_aout(char *file, char *fmt1, char *fmt2, int fd)
{
struct exec hdr;
int status, rval;
lseek(fd, 0, SEEK_SET);
if (read(fd, &hdr, sizeof hdr) != sizeof hdr
|| (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
#if 1 /* Compatibility */
|| hdr.a_entry < N_PAGSIZ(hdr)
#endif
) {
/* calling function prints warning */
return -1;
}
setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
if (fmt1)
setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
if (fmt2)
setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", file, 1);
if (fmt1 == NULL && fmt2 == NULL)
printf("%s:\n", file);
fflush(stdout);
rval = 0;
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",
file, WTERMSIG(status));
rval |= 1;
} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
fprintf(stderr, "%s: exit status %d\n",
file, WEXITSTATUS(status));
rval |= 1;
}
break;
case 0:
rval |= execl(file, file, NULL) != 0;
perror(file);
_exit(1);
}
return rval;
}

45
usr.bin/ldd/ldd.h Normal file
View File

@ -0,0 +1,45 @@
/* $NetBSD: ldd.h,v 1.1 2009/01/06 03:59:56 mrg Exp $ */
/*
* Copyright (c) 2008 Matthew R. Green
* 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. 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.
*/
int aout_ldd(char *, char *, char *);
int elf32_ldd(char *, char *, char *);
#ifdef _LP64
int elf64_ldd(char *, char *, char *);
#define elf_ldd elf64_ldd
#else
#define elf_ldd elf32_ldd
#endif
void fmtprint(const char *, Obj_Entry *, const char *, const char *);
void print_needed(Obj_Entry *, const char *, const char *);
extern char *main_local;
extern char *main_progname;

121
usr.bin/ldd/ldd_aout.c Normal file
View File

@ -0,0 +1,121 @@
/* $NetBSD: ldd_aout.c,v 1.1 2009/01/06 03:59:56 mrg Exp $ */
/*-
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ldd_aout.c,v 1.1 2009/01/06 03:59:56 mrg Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/wait.h>
#include <a.out.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* XXX put this here, rather than a separate ldd_aout.h
#include "ldd.h"
*/
int aout_ldd(char *, char *, char *);
int
aout_ldd(char *file, char *fmt1, char *fmt2)
{
struct stat st;
struct exec hdr;
int status, rval;
int fd;
fd = open(file, O_RDONLY);
if (fd == -1) {
warn("%s", file);
return -1;
}
if (fstat(fd, &st) < 0) {
warn("%s", file);
close(fd);
return -1;
}
lseek(fd, 0, SEEK_SET);
if (read(fd, &hdr, sizeof hdr) != sizeof hdr
|| (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
#if 1 /* Compatibility */
|| hdr.a_entry < N_PAGSIZ(hdr)
#endif
) {
/* calling function prints warning */
return -1;
}
setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
if (fmt1)
setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
if (fmt2)
setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", file, 1);
if (fmt1 == NULL && fmt2 == NULL)
printf("%s:\n", file);
fflush(stdout);
rval = 0;
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",
file, WTERMSIG(status));
rval |= 1;
} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
fprintf(stderr, "%s: exit status %d\n",
file, WEXITSTATUS(status));
rval |= 1;
}
break;
case 0:
rval |= execl(file, file, NULL) != 0;
perror(file);
_exit(1);
}
return rval;
}

View File

@ -1,9 +0,0 @@
# $NetBSD: Makefile,v 1.8 1998/12/17 22:34:09 thorpej Exp $
PROG= ldd
SRCS= ldd.c
MAN= ldd.1
.PATH: ${.CURDIR}/..
.include <bsd.prog.mk>

View File

@ -1,156 +0,0 @@
/* $NetBSD: ldd.c,v 1.20 2008/04/28 20:24:14 martin Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <a.out.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main __P((int, char *[]));
void usage __P((void));
void
usage()
{
fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
" ...\n", getprogname());
exit(1);
}
int
main(argc, argv)
int argc;
char *argv[];
{
char *fmt1 = NULL, *fmt2 = NULL;
int rval;
int c;
while ((c = getopt(argc, argv, "f:")) != -1) {
switch (c) {
case 'f':
if (fmt1) {
if (fmt2)
errx(1, "Too many formats");
fmt2 = optarg;
} else
fmt1 = optarg;
break;
default:
usage();
/*NOTREACHED*/
}
}
argc -= optind;
argv += optind;
if (argc <= 0) {
usage();
/*NOTREACHED*/
}
/* ld.so magic */
setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
if (fmt1)
setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
if (fmt2)
setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
rval = 0;
while (argc--) {
int fd;
struct exec hdr;
int status;
if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
warn("%s", *argv);
rval |= 1;
argv++;
continue;
}
if (read(fd, &hdr, sizeof hdr) != sizeof hdr
|| (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
#if 1 /* Compatibility */
|| hdr.a_entry < N_PAGSIZ(hdr)
#endif
) {
warnx("%s: not a dynamic executable", *argv);
(void)close(fd);
rval |= 1;
argv++;
continue;
}
(void)close(fd);
setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
if (fmt1 == NULL && fmt2 == NULL)
/* Default formats */
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;
}

View File

@ -1,33 +0,0 @@
# $NetBSD: Makefile,v 1.17 2007/05/19 15:35:04 christos Exp $
.include <bsd.own.mk> # for MKDYNAMICROOT definition
PROG= ldd
SRCS= ldd.c \
xmalloc.c debug.c expand.c map_object.c load.c search.c headers.c paths.c
MAN= ldd.1
.PATH: ${.CURDIR}/..
.if (${MACHINE_ARCH} == "alpha") || (${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "x86_64") || (${MACHINE_ARCH} == "powerpc64")
CPPFLAGS+= -DELFSIZE=64
.else
CPPFLAGS+= -DELFSIZE=32
.endif
.if (${MACHINE_ARCH} == "sparc") || (${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "arm") || (${MACHINE_ARCH} == "m68k") || \
(${MACHINE_ARCH} == "powerpc")
CPPFLAGS+= -DVARPSZ
.endif
LDELFSO=${NETBSDSRCDIR}/libexec/ld.elf_so
CPPFLAGS+= -I${LDELFSO} -DLIBDIR=\"${LIBDIR}\" # -DDEBUG
CPPFLAGS+= -D_RTLD_SOURCE
.PATH: ${LDELFSO}
.if (${MKDYNAMICROOT} == "no")
LDSTATIC?= -static
.endif
.include <bsd.prog.mk>

View File

@ -1,15 +0,0 @@
$NetBSD: README,v 1.4 2006/11/24 22:52:16 wiz Exp $
BUGS/PROBLEMS:
* Reports "ldd: mmap of entire address space failed: Cannot allocate memory",
or erroneously reports that libraries cannot be found,
when run on many files (`ldd /usr/bin/*').
TO DO:
* Support for executable formats other than ELF.
* Support for coexistence of 32-bit and 64-bit ELF.

170
usr.bin/ldd/ldd_elfxx.c Normal file
View File

@ -0,0 +1,170 @@
/* $NetBSD: ldd_elfxx.c,v 1.1 2009/01/06 03:59:56 mrg Exp $ */
/*-
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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 1996 John D. Polstra.
* Copyright 1996 Matt Thomas <matt@3am-software.com>
* 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 John Polstra.
* 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ldd_elfxx.c,v 1.1 2009/01/06 03:59:56 mrg Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <a.out.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "debug.h"
#include "rtld.h"
#include "ldd.h"
/*
* elfxx_ldd() - bit-size independant ELF ldd implementation.
* returns 0 on success and -1 on failure.
*/
int
ELFNAME(ldd)(char *path, char *fmt1, char *fmt2)
{
struct stat st;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1) {
warn("%s", path);
return -1;
}
if (fstat(fd, &st) < 0) {
warn("%s", path);
close(fd);
return -1;
}
_rtld_pagesz = sysconf(_SC_PAGESIZE);
#ifdef RTLD_ARCH_SUBDIR
_rtld_add_paths(path, &_rtld_default_paths,
RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
#endif
_rtld_add_paths(path, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
_rtld_paths = NULL;
_rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
if (_rtld_trust)
_rtld_add_paths(path, &_rtld_paths, getenv("LD_LIBRARY_PATH"));
_rtld_process_hints(path, &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
_rtld_objmain = _rtld_map_object(xstrdup(path), fd, &st);
if (_rtld_objmain == NULL) {
close(fd);
return -1;
}
close(fd);
_rtld_objmain->path = xstrdup(path);
_rtld_digest_dynamic(path, _rtld_objmain);
/* Link the main program into the list of objects. */
*_rtld_objtail = _rtld_objmain;
_rtld_objtail = &_rtld_objmain->next;
++_rtld_objmain->refcount;
(void) _rtld_load_needed_objects(_rtld_objmain, 0);
if (fmt1 == NULL)
printf("%s:\n", _rtld_objmain->path);
main_local = path;
main_progname = _rtld_objmain->path;
print_needed(_rtld_objmain, fmt1, fmt2);
while (_rtld_objlist != NULL) {
Obj_Entry *obj = _rtld_objlist;
_rtld_objlist = obj->next;
while (obj->rpaths != NULL) {
const Search_Path *rpath = obj->rpaths;
obj->rpaths = rpath->sp_next;
xfree(__UNCONST(rpath->sp_path));
xfree(__UNCONST(rpath));
}
while (obj->needed != NULL) {
const Needed_Entry *needed = obj->needed;
obj->needed = needed->next;
xfree(__UNCONST(needed));
}
(void) munmap(obj->mapbase, obj->mapsize);
xfree(obj->path);
xfree(obj);
}
_rtld_objmain = NULL;
_rtld_objtail = &_rtld_objlist;
/* Need to free _rtld_paths? */
return 0;
}