From e556691e4f2411bddc228266e9fcb8dbbf122ac6 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 6 Nov 2003 22:38:49 +0000 Subject: Begin converting the client utils --- utils/.cvsignore | 4 + utils/Makefile | 95 ++++++ utils/bswap.h | 54 ++++ utils/ldconfig.c | 900 ++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/ldd.c | 664 ++++++++++++++++++++++++++++++++++++++ utils/readelf.c | 348 ++++++++++++++++++++ utils/readsoname.c | 63 ++++ utils/readsoname.h | 4 + utils/readsoname2.c | 115 +++++++ 9 files changed, 2247 insertions(+) create mode 100644 utils/.cvsignore create mode 100644 utils/Makefile create mode 100644 utils/bswap.h create mode 100644 utils/ldconfig.c create mode 100644 utils/ldd.c create mode 100644 utils/readelf.c create mode 100644 utils/readsoname.c create mode 100644 utils/readsoname.h create mode 100644 utils/readsoname2.c (limited to 'utils') diff --git a/utils/.cvsignore b/utils/.cvsignore new file mode 100644 index 000000000..ffa8a1a88 --- /dev/null +++ b/utils/.cvsignore @@ -0,0 +1,4 @@ +ldd +readelf +ldconfig +elf.h diff --git a/utils/Makefile b/utils/Makefile new file mode 100644 index 000000000..3d0a5c3a9 --- /dev/null +++ b/utils/Makefile @@ -0,0 +1,95 @@ +# Makefile for uClibc +# +# Copyright (C) 2000-2003 Erik Andersen +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Library General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more +# details. +# +# You should have received a copy of the GNU Library General Public License +# along with this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Pull in the user's uClibc configuration, but do not +# include Rules.mak..... +TOPDIR=../ +include $(TOPDIR).config + +# A nifty macro to make testing gcc features easier +check_gcc=$(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; \ + then echo "$(1)"; else echo "$(2)"; fi) + +# use '-Os' optimization if available, else use -O2, allow Config to override +OPTIMIZATION+=$(call check_gcc,-Os,-O2) +XWARNINGS=$(subst ",, $(strip $(WARNINGS))) -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing + +MAJOR_VERSION:=0 +UCLIBC_LDSO:=ld-uClibc.so.$(MAJOR_VERSION) + +ifndef CROSS +CROSS= +endif +CC= $(CROSS)gcc +AR= $(CROSS)ar +LD= $(CROSS)ld +NM= $(CROSS)nm +STRIP= $(CROSS)strip +LN=ln + +ifeq ($(DODEBUG),y) + CFLAGS = $(XWARNINGS) -O0 -g3 + LDFLAGS = +else + CFLAGS=$(XWARNINGS) $(OPTIMIZATION) + LDFLAGS = -s +endif + +# Make certain these contain a final "/", but no "//"s. +RUNTIME_PREFIX:=$(strip $(subst //,/, $(subst ,/, $(subst ",, $(strip $(RUNTIME_PREFIX)))))) +DEVEL_PREFIX:=$(strip $(subst //,/, $(subst ,/, $(subst ",, $(strip $(DEVEL_PREFIX)))))) +export RUNTIME_PREFIX DEVEL_PREFIX + + +TARGETS = ldd ldconfig readelf + +ifeq ($(strip $(LDSO_LDD_SUPPORT)),y) +XXFLAGS = -D__LDSO_LDD_SUPPORT +endif + +ifeq ($(strip $(HAVE_SHARED)),y) +all: $(TARGETS) +else +all: +endif + +headers: + $(LN) -fs $(TOPDIR)include/elf.h + +readelf: readelf.c + $(CC) $(CFLAGS) -Wl,-s $^ -o $@ $(LDADD_LIBFLOAT) + $(STRIP) -x -R .note -R .comment $@ + +ldconfig: ldconfig.c readsoname.c + $(CC) $(CFLAGS) $(XXFLAGS) -Wl,-s \ + -DUCLIBC_RUNTIME_PREFIX=$(R_PREFIX) \ + -DUCLIBC_LDSO=$(UCLIBC_LDSO) -I. -I../ldso/include \ + $^ -o $@ $(LDADD_LIBFLOAT) + $(STRIP) -x -R .note -R .comment $@ + +ldd: ldd.c + $(CC) $(CFLAGS) $(XXFLAGS) -Wl,-s \ + -DUCLIBC_RUNTIME_PREFIX=$(R_PREFIX) \ + -DUCLIBC_LDSO=$(UCLIBC_LDSO) \ + $^ -o $@ $(LDADD_LIBFLOAT) + $(STRIP) -x -R .note -R .comment $@ + +clean: + $(RM) $(TARGETS) *.o *~ core *.target elf.h + +readelf.c readsoname.c ldconfig.c ldd.c: headers diff --git a/utils/bswap.h b/utils/bswap.h new file mode 100644 index 000000000..1742d2507 --- /dev/null +++ b/utils/bswap.h @@ -0,0 +1,54 @@ +#ifndef _BSWAP_H +#define _BSWAP_H 1 + +#if !defined(__BYTE_ORDER) && defined(BYTE_ORDER) +# define __BYTE_ORDER = BYTE_ORDER +#endif + +#ifndef __BYTE_ORDER +#ifdef __linux__ +#include +#else +#define __LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ +#define __BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ +#define __PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */ + +#if defined(sun386) || defined(i386) +#define __BYTE_ORDER __LITTLE_ENDIAN +#endif + +#if defined(sparc) +#define __BYTE_ORDER __BIG_ENDIAN +#endif + +#endif /* __linux__ */ +#endif /* __BYTE_ORDER */ + + +#ifndef __BYTE_ORDER +# error "Undefined __BYTE_ORDER" +#endif + +#ifdef __linux__ +#include +#else +#include +static __inline__ uint32_t bswap_32(uint32_t x) + { + uint32_t res; + + swab((void*)&x, (void*)&res, sizeof(uint32_t)); + + return res; + } + +static __inline__ uint16_t bswap_16(uint16_t x) + { + uint16_t res; + + swab((void*)&x, (void*)&res, sizeof(uint16_t)); + return res; + } +#endif + +#endif diff --git a/utils/ldconfig.c b/utils/ldconfig.c new file mode 100644 index 000000000..806604edd --- /dev/null +++ b/utils/ldconfig.c @@ -0,0 +1,900 @@ +/* + * ldconfig - update shared library symlinks + * + * usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ... + * ldconfig -l [-Dv] lib ... + * ldconfig -p + * -D: debug mode, don't update links + * -v: verbose mode, print things as we go + * -q: quiet mode, don't print warnings + * -n: don't process standard directories + * -N: don't update the library cache + * -X: don't update the library links + * -l: library mode, manually link libraries + * -p: print the current library cache + * -f conf: use conf instead of /etc/ld.so.conf + * -C cache: use cache instead of /etc/ld.so.cache + * -r root: first, do a chroot to the indicated directory + * dir ...: directories to process + * lib ...: libraries to link + * + * Copyright 1994-2000 David Engel and Mitch D'Souza + * + * This program may be used for any purpose as long as this + * copyright notice is kept. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "readsoname.h" + +struct exec +{ + unsigned long a_info; /* Use macros N_MAGIC, etc for access */ + unsigned a_text; /* length of text, in bytes */ + unsigned a_data; /* length of data, in bytes */ + unsigned a_bss; /* length of uninitialized data area for file, in bytes */ + unsigned a_syms; /* length of symbol table data in file, in bytes */ + unsigned a_entry; /* start address */ + unsigned a_trsize; /* length of relocation info for text, in bytes */ + unsigned a_drsize; /* length of relocation info for data, in bytes */ +}; + +#if !defined (N_MAGIC) +#define N_MAGIC(exec) ((exec).a_info & 0xffff) +#endif +/* Code indicating object file or impure executable. */ +#define OMAGIC 0407 +/* Code indicating pure executable. */ +#define NMAGIC 0410 +/* Code indicating demand-paged executable. */ +#define ZMAGIC 0413 +/* This indicates a demand-paged executable with the header in the text. + The first page is unmapped to help trap NULL pointer references */ +#define QMAGIC 0314 +/* Code indicating core file. */ +#define CMAGIC 0421 + +char *___strtok = NULL; + +/* For SunOS */ +#ifndef PATH_MAX +#include +#define PATH_MAX _POSIX_PATH_MAX +#endif + +/* For SunOS */ +#ifndef N_MAGIC +#define N_MAGIC(exec) ((exec).a_magic & 0xffff) +#endif + +#define EXIT_OK 0 +#define EXIT_FATAL 128 + +char *prog = NULL; +int debug = 0; /* debug mode */ +int verbose = 0; /* verbose mode */ +int libmode = 0; /* library mode */ +int nocache = 0; /* don't build cache */ +int nolinks = 0; /* don't update links */ + +char *conffile = LDSO_CONF; /* default conf file */ +char *cachefile = LDSO_CACHE; /* default cache file */ +void cache_print(void); +void cache_dolib(const char *dir, const char *so, int libtype); +void cache_write(void); + +/* These two are used internally -- you shouldn't need to use them */ +static void verror_msg(const char *s, va_list p) +{ + fflush(stdout); + fprintf(stderr, "%s: ", prog); + vfprintf(stderr, s, p); +} + +extern void warnx(const char *s, ...) +{ + va_list p; + + va_start(p, s); + verror_msg(s, p); + va_end(p); + fprintf(stderr, "\n"); +} + +extern void err(int errnum, const char *s, ...) +{ + va_list p; + + va_start(p, s); + verror_msg(s, p); + va_end(p); + fprintf(stderr, "\n"); + exit(errnum); +} + +static void vperror_msg(const char *s, va_list p) +{ + int err = errno; + + if (s == 0) + s = ""; + verror_msg(s, p); + if (*s) + s = ": "; + fprintf(stderr, "%s%s\n", s, strerror(err)); +} + +extern void warn(const char *s, ...) +{ + va_list p; + + va_start(p, s); + vperror_msg(s, p); + va_end(p); +} + +void *xmalloc(size_t size) +{ + void *ptr; + if ((ptr = malloc(size)) == NULL) + err(EXIT_FATAL,"out of memory"); + return ptr; +} + +char *xstrdup(const char *str) +{ + char *ptr; + if ((ptr = strdup(str)) == NULL) + err(EXIT_FATAL,"out of memory"); + return ptr; +} + +/* If shared library, return a malloced copy of the soname and set the + type, else return NULL. + + expected_type should be either LIB_ANY or one of the following:- + LIB_DLL + LIB_ELF + LIB_ELF_LIBC5 + LIB_ELF_LIBC6 + + If the lib is ELF and we can not deduce the type the type will + be set based on expected_type. + + If the expected, actual/deduced types missmatch we display a warning + and use the actual/deduced type. +*/ +char *is_shlib(const char *dir, const char *name, int *type, + int *islink, int expected_type) +{ + char *good = NULL; + char *cp, *cp2; + FILE *file; + struct exec exec; + ElfW(Ehdr) *elf_hdr; + struct stat statbuf; + char buff[4096]; + + /* see if name is of the form libZ.so* */ + if ((strncmp(name, "lib", 3) == 0 || strncmp(name, "ld-", 3) == 0) && \ + name[strlen(name)-1] != '~' && (cp = strstr(name, ".so"))) + { + /* find the start of the Vminor part, if any */ + if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.'))) + cp = cp2; + else + cp = cp + strlen(cp); + + /* construct the full path name */ + sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ? + "/" : "", name); + + /* first, make sure it's a regular file */ + if (lstat(buff, &statbuf)) + warn("skipping %s", buff); + else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) + warnx("%s is not a regular file or symlink, skipping", buff); + else + { + /* is it a regular file or a symlink */ + *islink = S_ISLNK(statbuf.st_mode); + + /* then try opening it */ + if (!(file = fopen(buff, "rb"))) + warn("skipping %s", buff); + else + { + /* now make sure it's a shared library */ + if (fread(&exec, sizeof exec, 1, file) < 1) + warnx("can't read header from %s, skipping", buff); + else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC) + { + elf_hdr = (ElfW(Ehdr) *) &exec; + if (elf_hdr->e_ident[0] != 0x7f || + strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0) + { + /* silently ignore linker scripts */ + if (strncmp((char *)&exec, "/* GNU ld", 9) != 0) + warnx("%s is not a shared library, skipping", buff); + } + else + { + /* always call readsoname to update type */ + if(expected_type == LIB_DLL) { + warnx("%s is not an a.out library, its ELF!\n", buff); + expected_type=LIB_ANY; + } + *type = LIB_ELF; + good = readsoname(buff, file, expected_type, type, + elf_hdr->e_ident[EI_CLASS]); + if (good == NULL || *islink) + { + if (good != NULL) + free(good); + good = xstrdup(name); + } + else + { + /* if the soname does not match the filename, + issue a warning, but only in debug mode. */ + int len = strlen(good); + if (debug && (strncmp(good, name, len) != 0 || + (name[len] != '\0' && name[len] != '.'))) + warnx("%s has inconsistent soname (%s)", buff, good); + } + } + } + else + { + if (*islink) + good = xstrdup(name); + else + { + good = xmalloc(cp - name + 1); + strncpy(good, name, cp - name); + good[cp - name] = '\0'; + } + if(expected_type != LIB_ANY && expected_type != LIB_DLL) + { + warnx("%s is not an ELF library, its an a.out DLL!", buff); + expected_type=LIB_ANY; + } + + *type = LIB_DLL; + } + fclose(file); + } + } + } + + return good; +} + +/* update the symlink to new library */ +void link_shlib(const char *dir, const char *file, const char *so) +{ + int change = 1; + char libname[4096]; + char linkname[4096]; + struct stat libstat; + struct stat linkstat; + + /* construct the full path names */ + sprintf(libname, "%s/%s", dir, file); + sprintf(linkname, "%s/%s", dir, so); + + /* see if a link already exists */ + if (!stat(linkname, &linkstat)) + { + /* now see if it's the one we want */ + if (stat(libname, &libstat)) + warn("can't stat %s", libname); + else if (libstat.st_dev == linkstat.st_dev && + libstat.st_ino == linkstat.st_ino) + change = 0; + } + + /* then update the link, if required */ + if (change > 0 && !nolinks) + { + if (!lstat(linkname, &linkstat)) + { + if (!S_ISLNK(linkstat.st_mode)) + { + warnx("%s is not a symlink", linkname); + change = -1; + } + else if (remove(linkname)) + { + warn("can't unlink %s", linkname); + change = -1; + } + } + if (change > 0) + { + if (symlink(file, linkname)) + { + warn("can't link %s to %s", linkname, file); + change = -1; + } + } + } + + /* some people like to know what we're doing */ + if (verbose > 0) + printf("\t%s => %s%s\n", so, file, + change < 0 ? " (SKIPPED)" : + (change > 0 ? " (changed)" : "")); + + return; +} + +/* figure out which library is greater */ +int libcmp(char *p1, char *p2) +{ + while (*p1) + { + if (isdigit(*p1) && isdigit(*p2)) + { + /* must compare this numerically */ + int v1, v2; + v1 = strtoul(p1, &p1, 10); + v2 = strtoul(p2, &p2, 10); + if (v1 != v2) + return v1 - v2; + } + else if (isdigit(*p1) && !isdigit(*p2)) + return 1; + else if (!isdigit(*p1) && isdigit(*p2)) + return -1; + else if (*p1 != *p2) + return *p1 - *p2; + else + p1++, p2++; + } + + return *p1 - *p2; +} + +struct lib +{ + char *so; /* soname of a library */ + char *name; /* name of a library */ + int libtype; /* type of a library */ + int islink; /* is it a symlink */ + struct lib *next; /* next library in list */ +}; + +/* update all shared library links in a directory */ +void scan_dir(const char *rawname) +{ + DIR *dir; + const char *name; + struct dirent *ent; + char *so, *path, *path_n; + struct lib *lp, *libs = NULL; + int i, libtype, islink, expected_type = LIB_ANY; + + /* We need a writable copy of this string */ + path = strdup(rawname); + if (!path) { + err(EXIT_FATAL, "Out of memory!\n"); + } + /* Eliminate all double //s */ + path_n=path; + while((path_n=strstr(path_n, "//"))) { + i = strlen(path_n); + memmove(path_n, path_n+1, i-1); + *(path_n + i - 1)='\0'; + } + name = path; + +#if 0 + char *t; + /* Check for an embedded expected type */ + t=strrchr(name, '='); + if( t ) + { + *t++ = '\0'; /* Skip = char */ + if(strcasecmp(t, "libc4") == 0) + { + expected_type = LIB_DLL; + } + else + { + if(strcasecmp(t, "libc5") == 0) + { + expected_type = LIB_ELF_LIBC5; + } + else + { + if(strcasecmp(t, "libc6") == 0) + { + expected_type = LIB_ELF_LIBC6; + } + else + { + if(strcasecmp(t, "libc0") == 0) + { + expected_type = LIB_ELF_LIBC0; + } + else + { + warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name); + expected_type = LIB_ANY; + } + } + } + } + } +#endif + + /* let 'em know what's going on */ + if (verbose > 0) + printf("%s:\n", name); + + /* if we can't open it, we can't do anything */ + if ((dir = opendir(name)) == NULL) + { + warn("skipping %s", name); + free(path); + return; + } + + /* yes, we have to look at every single file */ + while ((ent = readdir(dir)) != NULL) + { + /* if it's not a shared library, don't bother */ + if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL) + continue; + + /* have we already seen one with the same so name? */ + for (lp = libs; lp; lp = lp->next) + { + if (strcmp(so, lp->so) == 0) + { + /* we have, which one do we want to use? */ + if ((!islink && lp->islink) || + (islink == lp->islink && + libcmp(ent->d_name, lp->name) > 0)) + { + /* let's use the new one */ + free(lp->name); + lp->name = xstrdup(ent->d_name); + lp->libtype = libtype; + lp->islink = islink; + } + break; + } + } + + /* congratulations, you're the first one we've seen */ + if (!lp) + { + lp = xmalloc(sizeof *lp); + lp->so = xstrdup(so); + lp->name = xstrdup(ent->d_name); + lp->libtype = libtype; + lp->islink = islink; + lp->next = libs; + libs = lp; + } + + free(so); + } + + /* don't need this any more */ + closedir(dir); + + /* now we have all the latest libs, update the links */ + for (lp = libs; lp; lp = lp->next) + { + if (!lp->islink) + link_shlib(name, lp->name, lp->so); +#ifdef USE_CACHE + if (!nocache) + cache_dolib(name, lp->so, lp->libtype); +#endif + } + + /* always try to clean up after ourselves */ + while (libs) + { + lp = libs->next; + free(libs->so); + free(libs->name); + free(libs); + libs = lp; + } + + free(path); + return; +} + +/* return the list of system-specific directories */ +char *get_extpath(void) +{ + char *res = NULL, *cp; + FILE *file; + struct stat stat; + + if ((file = fopen(conffile, "r")) != NULL) + { + fstat(fileno(file), &stat); + res = xmalloc(stat.st_size + 1); + fread(res, 1, stat.st_size, file); + fclose(file); + res[stat.st_size] = '\0'; + + /* convert comments fo spaces */ + for (cp = res; *cp; /*nada*/) { + if (*cp == '#') { + do + *cp++ = ' '; + while (*cp && *cp != '\n'); + } else { + cp++; + } + } + } + + return res; +} + +#ifdef USE_CACHE +typedef struct liblist +{ + int flags; + int sooffset; + int liboffset; + char *soname; + char *libname; + struct liblist *next; +} liblist_t; + +static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 }; +static liblist_t *lib_head = NULL; + +static int liblistcomp(liblist_t *x, liblist_t *y) +{ + int res; + + if ((res = libcmp(x->soname, y->soname)) == 0) + { + res = libcmp(strrchr(x->libname, '/') + 1, + strrchr(y->libname, '/') + 1); + } + + return res; +} + +void cache_dolib(const char *dir, const char *so, int libtype) +{ + char fullpath[PATH_MAX]; + liblist_t *new_lib, *cur_lib; + + magic.nlibs++; + sprintf(fullpath, "%s/%s", dir, so); + new_lib = xmalloc(sizeof (liblist_t)); + new_lib->flags = libtype; + new_lib->soname = xstrdup(so); + new_lib->libname = xstrdup(fullpath); + + if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0) + { + new_lib->next = lib_head; + lib_head = new_lib; + } + else + { + for (cur_lib = lib_head; cur_lib->next != NULL && + liblistcomp(new_lib, cur_lib->next) <= 0; + cur_lib = cur_lib->next) + /* nothing */; + new_lib->next = cur_lib->next; + cur_lib->next = new_lib; + } +} + +void cache_write(void) +{ + int cachefd; + int stroffset = 0; + char tempfile[4096]; + liblist_t *cur_lib; + + if (!magic.nlibs) + return; + + sprintf(tempfile, "%s~", cachefile); + + if (unlink(tempfile) && errno != ENOENT) + err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno)); + + if ((cachefd = creat(tempfile, 0644)) < 0) + err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno)); + + if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t)) + err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno)); + + for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) + { + cur_lib->sooffset = stroffset; + stroffset += strlen(cur_lib->soname) + 1; + cur_lib->liboffset = stroffset; + stroffset += strlen(cur_lib->libname) + 1; + if (write(cachefd, cur_lib, sizeof (libentry_t)) != + sizeof (libentry_t)) + err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno)); + } + + for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) + { + if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1) + != strlen(cur_lib->soname) + 1) + err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno)); + if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1) + != strlen(cur_lib->libname) + 1) + err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno)); + } + + if (close(cachefd)) + err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno)); + + if (chmod(tempfile, 0644)) + err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno)); + + if (rename(tempfile, cachefile)) + err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno)); +} + +void cache_print(void) +{ + caddr_t c; + struct stat st; + int fd = 0; + char *strs; + header_t *header; + libentry_t *libent; + + if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0) + err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno)); + if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1) + err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno)); + close(fd); + + if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)) + err(EXIT_FATAL,"%s cache corrupt", cachefile); + + if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)) + err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER); + + header = (header_t *)c; + libent = (libentry_t *)(c + sizeof (header_t)); + strs = (char *)&libent[header->nlibs]; + + printf("%d libs found in cache `%s' (version %s)\n", + header->nlibs, cachefile, LDSO_CACHE_VER); + + for (fd = 0; fd < header->nlibs; fd++) + { + printf("\t%s ", strs + libent[fd].sooffset); + switch (libent[fd].flags & ~LIB_ELF64) + { + case LIB_DLL: + printf("(libc4)"); + break; + case LIB_ELF: + printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : ""); + break; + case LIB_ELF_LIBC5: + case LIB_ELF_LIBC6: + printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3, + libent[fd].flags & LIB_ELF64 ? "/64" : ""); + break; + default: + printf("(unknown)"); + break; + } + printf(" => %s\n", strs + libent[fd].liboffset); + } + + munmap (c,st.st_size); +} +#else +void cache_print(void) +{ + warnx("Cache support disabled\n"); +} +#endif + +void usage(void) +{ + fprintf(stderr, + "ldconfig - updates symlinks for shared libraries\n\n" + "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n" + " ldconfig -l [-Dv] lib ...\n" + " ldconfig -p\n\nOptions:\n" + "\t-D:\t\tdebug mode, don't update links\n" + "\t-v:\t\tverbose mode, print things as we go\n" + "\t-q:\t\tquiet mode, don't print warnings\n" + "\t-n:\t\tdon't process standard directories\n" + "\t-N:\t\tdon't update the library cache\n" + "\t-X:\t\tdon't update the library links\n" + "\t-l:\t\tlibrary mode, manually link libraries\n" + "\t-p:\t\tprint the current library cache\n" + "\t-f conf :\tuse conf instead of %s\n" + "\t-C cache:\tuse cache instead of %s\n" + "\t-r root :\tfirst, do a chroot to the indicated directory\n" + "\tdir ... :\tdirectories to process\n" + "\tlib ... :\tlibraries to link\n\n", + LDSO_CONF, LDSO_CACHE + ); + exit(EXIT_FATAL); +} + +#define DIR_SEP ":, \t\n" +int main(int argc, char **argv) +{ + int i, c; + int nodefault = 0; + int printcache = 0; + char *cp, *dir, *so; + char *extpath; + int libtype, islink; + char *chroot_dir = NULL; + + prog = argv[0]; + opterr = 0; + + while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF) + switch (c) + { + case 'D': + debug = 1; /* debug mode */ + nocache = 1; + nolinks = 1; + verbose = 1; + break; + case 'v': + verbose = 1; /* verbose mode */ + break; + case 'q': + if (verbose <= 0) + verbose = -1; /* quiet mode */ + break; + case 'n': + nodefault = 1; /* no default dirs */ + nocache = 1; + break; + case 'N': + nocache = 1; /* don't build cache */ + break; + case 'X': + nolinks = 1; /* don't update links */ + break; + case 'l': + libmode = 1; /* library mode */ + break; + case 'p': + printcache = 1; /* print cache */ + break; + case 'f': + conffile = optarg; /* alternate conf file */ + break; + case 'C': + cachefile = optarg; /* alternate cache file */ + break; + case 'r': + chroot_dir = optarg; + break; + default: + usage(); + break; + + /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED + IN FUTURE VERSIONS. */ + } + + if (chroot_dir && *chroot_dir) { + if (chroot(chroot_dir) < 0) + err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno)); + if (chdir("/") < 0) + err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno)); + } + + /* allow me to introduce myself, hi, my name is ... */ + if (verbose > 0) + printf("%s: uClibc version\n", argv[0]); + + if (printcache) + { + /* print the cache -- don't you trust me? */ + cache_print(); + exit(EXIT_OK); + } + else if (libmode) + { + /* so you want to do things manually, eh? */ + + /* ok, if you're so smart, which libraries do we link? */ + for (i = optind; i < argc; i++) + { + /* split into directory and file parts */ + if (!(cp = strrchr(argv[i], '/'))) + { + dir = "."; /* no dir, only a filename */ + cp = argv[i]; + } + else + { + if (cp == argv[i]) + dir = "/"; /* file in root directory */ + else + dir = argv[i]; + *cp++ = '\0'; /* neither of the above */ + } + + /* we'd better do a little bit of checking */ + if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL) + err(EXIT_FATAL,"%s%s%s is not a shared library", dir, + (*dir && strcmp(dir, "/")) ? "/" : "", cp); + + /* so far, so good, maybe he knows what he's doing */ + link_shlib(dir, cp, so); + } + } + else + { + /* the lazy bum want's us to do all the work for him */ + + /* don't cache dirs on the command line */ + int nocache_save = nocache; + nocache = 1; + + /* OK, which directories should we do? */ + for (i = optind; i < argc; i++) + scan_dir(argv[i]); + + /* restore the desired caching state */ + nocache = nocache_save; + + /* look ma, no defaults */ + if (!nodefault) + { + /* I guess the defaults aren't good enough */ + if ((extpath = get_extpath())) + { + for (cp = strtok(extpath, DIR_SEP); cp; + cp = strtok(NULL, DIR_SEP)) + scan_dir(cp); + free(extpath); + } + + scan_dir(UCLIBC_RUNTIME_PREFIX "/usr/X11R6/lib"); + scan_dir(UCLIBC_RUNTIME_PREFIX "/usr/lib"); + scan_dir(UCLIBC_RUNTIME_PREFIX "/lib"); + } + +#ifdef USE_CACHE + if (!nocache) + cache_write(); +#endif + } + + exit(EXIT_OK); +} + diff --git a/utils/ldd.c b/utils/ldd.c new file mode 100644 index 000000000..3ea41b424 --- /dev/null +++ b/utils/ldd.c @@ -0,0 +1,664 @@ +/* vi: set sw=4 ts=4: */ +/* + * A small little ldd implementation for uClibc + * + * Copyright (C) 2000 by Lineo, inc and Erik Andersen + * Copyright (C) 2000-2002 Erik Andersen + * + * Several functions in this file (specifically, elf_find_section_type(), + * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from + * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser + * , which is copyright 2000 BitWagon Software LLC + * (GPL2). + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bswap.h" +#if defined (sun) +#include "link.h" +#else +#include "elf.h" +#endif + +#ifdef DMALLOC +#include +#endif + +#if defined(__arm__) +#define MATCH_MACHINE(x) (x == EM_ARM) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__s390__) +#define MATCH_MACHINE(x) (x == EM_S390) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__i386__) +#ifndef EM_486 +#define MATCH_MACHINE(x) (x == EM_386) +#else +#define MATCH_MACHINE(x) (x == EM_386 || x == EM_486) +#endif +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__mc68000__) +#define MATCH_MACHINE(x) (x == EM_68K) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__mips__) +#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__powerpc__) +#define MATCH_MACHINE(x) (x == EM_PPC) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined(__sh__) +#define MATCH_MACHINE(x) (x == EM_SH) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined (__v850e__) +#define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850) +#define ELFCLASSM ELFCLASS32 +#endif + +#if defined (__sparc__) +#define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS) +#define ELFCLASSM ELFCLASS32 +#endif + + +#ifndef MATCH_MACHINE +#warning "You really should add a MATCH_MACHINE() macro for your architecture" +#endif + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define ELFDATAM ELFDATA2LSB +#elif __BYTE_ORDER == __BIG_ENDIAN +#define ELFDATAM ELFDATA2MSB +#endif + +struct library { + char *name; + int resolved; + char *path; + struct library *next; +}; +struct library *lib_list = NULL; +char not_found[] = "not found"; +char *interp = NULL; +char *interp_dir = NULL; +int byteswap; +static int interpreter_already_found=0; + +inline uint32_t byteswap32_to_host(uint32_t value) +{ + if (byteswap==1) { + return(bswap_32(value)); + } else { + return(value); + } +} + + + +Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr) +{ + int j; + Elf32_Shdr *shdr; + shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr); + for (j = ehdr->e_shnum; --j>=0; ++shdr) { + if (key==(int)byteswap32_to_host(shdr->sh_type)) { + return shdr; + } + } + return NULL; +} + +Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr) +{ + int j; + Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr); + for (j = ehdr->e_phnum; --j>=0; ++phdr) { + if (type==(int)byteswap32_to_host(phdr->p_type)) { + return phdr; + } + } + return NULL; +} + +/* Returns value if return_val==1, ptr otherwise */ +void * elf_find_dynamic(int const key, Elf32_Dyn *dynp, + Elf32_Ehdr *ehdr, int return_val) +{ + Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr); + unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset); + for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) { + if (key == (int)byteswap32_to_host(dynp->d_tag)) { + if (return_val == 1) + return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val); + else + return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr ); + } + } + return NULL; +} + +int check_elf_header(Elf32_Ehdr *const ehdr) +{ + if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 || + ehdr->e_ident[EI_CLASS] != ELFCLASS32 || + ehdr->e_ident[EI_VERSION] != EV_CURRENT) + { + return 1; + } + + /* Check if the target endianness matches the host's endianness */ + byteswap = 0; +#if __BYTE_ORDER == __LITTLE_ENDIAN + if (ehdr->e_ident[5] == ELFDATA2MSB) { + /* Ick -- we will have to byte-swap everything */ + byteswap = 1; + } +#elif __BYTE_ORDER == __BIG_ENDIAN + if (ehdr->e_ident[5] == ELFDATA2LSB) { + /* Ick -- we will have to byte-swap everything */ + byteswap = 1; + } +#else +#error Unknown host byte order! +#endif + + /* Be vary lazy, and only byteswap the stuff we use */ + if (byteswap==1) { + ehdr->e_type=bswap_16(ehdr->e_type); + ehdr->e_phoff=bswap_32(ehdr->e_phoff); + ehdr->e_shoff=bswap_32(ehdr->e_shoff); + ehdr->e_phnum=bswap_16(ehdr->e_phnum); + ehdr->e_shnum=bswap_16(ehdr->e_shnum); + } + + return 0; +} + +/* This function's behavior must exactly match that + * in uClibc/ldso/ldso/readelflib1.c */ +static void search_for_named_library(char *name, char *result, const char *path_list) +{ + int i, count = 1; + char *path, *path_n; + struct stat filestat; + + /* We need a writable copy of this string */ + path = strdup(path_list); + if (!path) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + /* Eliminate all double //s */ + path_n=path; + while((path_n=strstr(path_n, "//"))) { + i = strlen(path_n); + memmove(path_n, path_n+1, i-1); + *(path_n + i - 1)='\0'; + } + + /* Replace colons with zeros in path_list and count them */ + for(i=strlen(path); i > 0; i--) { + if (path[i]==':') { + path[i]=0; + count++; + } + } + path_n = path; + for (i = 0; i < count; i++) { + strcpy(result, path_n); + strcat(result, "/"); + strcat(result, name); + if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) { + free(path); + return; + } + path_n += (strlen(path_n) + 1); + } + free(path); + *result = '\0'; +} + +void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_suid, struct library *lib) +{ + char *buf; + char *path; + struct stat filestat; + + /* If this is a fully resolved name, our job is easy */ + if (stat (lib->name, &filestat) == 0) { + lib->path = lib->name; + return; + } + + /* We need some elbow room here. Make some room...*/ + buf = malloc(1024); + if (!buf) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + + /* This function must match the behavior of _dl_load_shared_library + * in readelflib1.c or things won't work out as expected... */ + + /* The ABI specifies that RPATH is searched first, so do that now. */ + path = (char *)elf_find_dynamic(DT_RPATH, dynamic, ehdr, 0); + if (path) { + search_for_named_library(lib->name, buf, path); + if (*buf != '\0') { + lib->path = buf; + return; + } + } + + /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed. + * Since this app doesn't actually run an executable I will skip + * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */ + if (is_suid==1) + path = NULL; + else + path = getenv("LD_LIBRARY_PATH"); + if (path) { + search_for_named_library(lib->name, buf, path); + if (*buf != '\0') { + lib->path = buf; + return; + } + } + +#ifdef USE_CACHE + /* FIXME -- add code to check the Cache here */ +#endif + + + /* Next look for libraries wherever the shared library + * loader was installed -- this is usually where we + * should find things... */ + if (interp_dir) { + search_for_named_library(lib->name, buf, interp_dir); + if (*buf != '\0') { + lib->path = buf; + return; + } + } + + /* Lastly, search the standard list of paths for the library. + This list must exactly match the list in uClibc/ldso/ldso/readelflib1.c */ + path = UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:" + UCLIBC_RUNTIME_PREFIX "usr/lib:" + UCLIBC_RUNTIME_PREFIX "lib:" + "/usr/lib:" + "/lib"; + search_for_named_library(lib->name, buf, path); + if (*buf != '\0') { + lib->path = buf; + } else { + free(buf); + lib->path = not_found; + } +} + +static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_setuid, char *s) +{ + char *tmp, *tmp1, *tmp2; + struct library *cur, *newlib=lib_list; + + if (!s || !strlen(s)) + return 1; + + tmp = s; + while (*tmp) { + if (*tmp == '/') + s = tmp + 1; + tmp++; + } + + /* We add libc.so.0 elsewhere */ + if (interpreter_already_found && (tmp=strrchr(interp, '/')) != NULL) + { + int len = strlen(interp_dir); + if (strcmp(s, interp+1+len)==0) + return 1; + } + + for (cur = lib_list; cur; cur=cur->next) { + /* Check if this library is already in the list */ + tmp1 = tmp2 = cur->name; + while (*tmp1) { + if (*tmp1 == '/') + tmp2 = tmp1 + 1; + tmp1++; + } + if(strcmp(tmp2, s)==0) { + //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); + return 0; + } + } + + /* Ok, this lib needs to be added to the list */ + newlib = malloc(sizeof(struct library)); + if (!newlib) + return 1; + newlib->name = malloc(strlen(s)+1); + strcpy(newlib->name, s); + newlib->resolved = 0; + newlib->path = NULL; + newlib->next = NULL; + + /* Now try and locate where this library might be living... */ + locate_library_file(ehdr, dynamic, is_setuid, newlib); + + //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); + if (!lib_list) { + lib_list = newlib; + } else { + for (cur = lib_list; cur->next; cur=cur->next); /* nothing */ + cur->next = newlib; + } + return 0; +} + + +static void find_needed_libraries(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid) +{ + Elf32_Dyn *dyns; + + for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) { + if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) { + add_library(ehdr, dynamic, is_setuid, + (char*)strtab + byteswap32_to_host(dyns->d_un.d_val)); + } + } +} + +static struct library * find_elf_interpreter(Elf32_Ehdr* ehdr) +{ + Elf32_Phdr *phdr; + + if (interpreter_already_found==1) + return NULL; + phdr = elf_find_phdr_type(PT_INTERP, ehdr); + if (phdr) { + struct library *cur, *newlib=NULL; + char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset); + + char *tmp, *tmp1; + interp = strdup(s); + interp_dir = strdup(s); + tmp = strrchr(interp_dir, '/'); + if (*tmp) + *tmp = '\0'; + else { + free(interp_dir); + interp_dir = interp; + } + tmp1 = tmp = s; + while (*tmp) { + if (*tmp == '/') + tmp1 = tmp + 1; + tmp++; + } + for (cur = lib_list; cur; cur=cur->next) { + /* Check if this library is already in the list */ + if(strcmp(cur->name, tmp1)==0) { + //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); + newlib = cur; + free(newlib->name); + free(newlib->path); + return NULL; + } + } + if (newlib == NULL) + newlib = malloc(sizeof(struct library)); + if (!newlib) + return NULL; + newlib->name = malloc(strlen(s)+1); + strcpy(newlib->name, s); + newlib->path = newlib->name; + newlib->resolved = 1; + newlib->next = NULL; + +#if 0 + //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); + if (!lib_list) { + lib_list = newlib; + } else { + for (cur = lib_list; cur->next; cur=cur->next); /* nothing */ + cur->next = newlib; + } +#endif + interpreter_already_found=1; + return newlib; + } + return NULL; +} + +/* map the .so, and locate interesting pieces */ +int find_dependancies(char* filename) +{ + int is_suid = 0; + FILE *thefile; + struct stat statbuf; + char *dynstr=NULL; + Elf32_Ehdr *ehdr = NULL; + Elf32_Shdr *dynsec = NULL; + Elf32_Dyn *dynamic = NULL; + struct library *interp; + + if (filename == not_found) + return 0; + + if (!filename) { + fprintf(stderr, "No filename specified.\n"); + return -1; + } + if (!(thefile = fopen(filename, "r"))) { + perror(filename); + return -1; + } + if (fstat(fileno(thefile), &statbuf) < 0) { + perror(filename); + return -1; + } + + if ((size_t)statbuf.st_size < sizeof(Elf32_Ehdr)) + goto foo; + + if (!S_ISREG(statbuf.st_mode)) + goto foo; + + /* mmap the file to make reading stuff from it effortless */ + ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size, + PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0); + +foo: + /* Check if this looks like a legit ELF file */ + if (check_elf_header(ehdr)) { + fprintf(stderr, "%s: not an ELF file.\n", filename); + return -1; + } + /* Check if this is the right kind of ELF file */ + if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) { + fprintf(stderr, "%s: not a dynamic executable\n", filename); + return -1; + } + if (ehdr->e_type == ET_EXEC) { + if (statbuf.st_mode & S_ISUID) + is_suid = 1; + if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) + is_suid = 1; + /* FIXME */ + if (is_suid) + fprintf(stderr, "%s: is setuid\n", filename); + } + + interpreter_already_found=0; + interp = find_elf_interpreter(ehdr); + +#ifdef __LDSO_LDD_SUPPORT + if (interp && ehdr->e_type == ET_EXEC && ehdr->e_ident[EI_CLASS] == ELFCLASSM && + ehdr->e_ident[EI_DATA] == ELFDATAM + && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine)) + { + struct stat statbuf; + if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) { + pid_t pid; + int status; + static const char * const environment[] = { + "PATH=/usr/bin:/bin:/usr/sbin:/sbin", + "SHELL=/bin/sh", + "LD_TRACE_LOADED_OBJECTS=1", + NULL + }; + + if ((pid = fork()) == 0) { + /* Cool, it looks like we should be able to actually + * run this puppy. Do so now... */ + execle(filename, filename, NULL, environment); + _exit(0xdead); + } + + /* Wait till it returns */ + waitpid(pid, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status)==0) { + return 1; + } + + /* If the exec failed, we fall through to trying to find + * all the needed libraries ourselves by rummaging about + * in the ELF headers... */ + } + } +#endif + + dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr); + if (dynsec) { + dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr); + dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); + find_needed_libraries(ehdr, dynamic, dynstr, is_suid); + } + + return 0; +} + + + +int main( int argc, char** argv) +{ + int multi=0; + int got_em_all=1; + char *filename = NULL; + struct library *cur; + + if (argc < 2) { + fprintf(stderr, "ldd: missing file arguments\n"); + fprintf(stderr, "Try `ldd --help' for more information.\n"); + exit(EXIT_FAILURE); + } + if (argc > 2) { + multi++; + } + + while (--argc > 0) { + ++argv; + + if(strcmp(*argv, "--")==0) { + /* Ignore "--" */ + continue; + } + + if(strcmp(*argv, "--help")==0) { + fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"); + fprintf(stderr, "\t--help\t\tprint this help and exit\n"); + exit(EXIT_FAILURE); + } + + filename=*argv; + if (!filename) { + fprintf(stderr, "No filename specified.\n"); + exit(EXIT_FAILURE); + } + + if (multi) { + printf("%s:\n", *argv); + } + + if (find_dependancies(filename)!=0) + continue; + + while(got_em_all) { + got_em_all=0; + /* Keep walking the list till everybody is resolved */ + for (cur = lib_list; cur; cur=cur->next) { + if (cur->resolved == 0 && cur->path) { + got_em_all=1; + //printf("checking sub-depends for '%s\n", cur->path); + find_dependancies(cur->path); + cur->resolved = 1; + } + } + } + + + /* Print the list */ + got_em_all=0; + for (cur = lib_list; cur; cur=cur->next) { + got_em_all=1; + printf("\t%s => %s (0x00000000)\n", cur->name, cur->path); + } + if (interp_dir && got_em_all==1) + printf("\t%s => %s (0x00000000)\n", interp, interp); + if (got_em_all==0) + printf("\tnot a dynamic executable\n"); + + for (cur = lib_list; cur; cur=cur->next) { + free(cur->name); + cur->name=NULL; + free(cur->path); + cur->path=NULL; + } + lib_list=NULL; + } + + return 0; +} + diff --git a/utils/readelf.c b/utils/readelf.c new file mode 100644 index 000000000..1d53c4c3d --- /dev/null +++ b/utils/readelf.c @@ -0,0 +1,348 @@ +/* vi: set sw=4 ts=4: */ +/* + * A small little readelf implementation for uClibc + * + * Copyright (C) 2000 by Lineo, inc and Erik Andersen + * Copyright (C) 2000-2002 Erik Andersen + * + * Several functions in this file (specifically, elf_find_section_type(), + * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from + * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser + * , which is copyright 2000 BitWagon Software LLC + * (GPL2). + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bswap.h" +#include "elf.h" + + +int byteswap; +inline uint32_t byteswap32_to_host(uint32_t value) +{ + if (byteswap==1) { + return(bswap_32(value)); + } else { + return(value); + } +} + +Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr) +{ + int j; + Elf32_Shdr *shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr); + for (j = ehdr->e_shnum; --j>=0; ++shdr) { + if (key==(int)byteswap32_to_host(shdr->sh_type)) { + return shdr; + } + } + return NULL; +} + +Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr) +{ + int j; + Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr); + for (j = ehdr->e_phnum; --j>=0; ++phdr) { + if (type==(int)byteswap32_to_host(phdr->p_type)) { + return phdr; + } + } + return NULL; +} + +/* Returns value if return_val==1, ptr otherwise */ +void * elf_find_dynamic(int const key, Elf32_Dyn *dynp, + Elf32_Ehdr *ehdr, int return_val) +{ + Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr); + unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset); + for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) { + if (key == (int)byteswap32_to_host(dynp->d_tag)) { + if (return_val == 1) + return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val); + else + return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr ); + } + } + return NULL; +} + +int check_elf_header(Elf32_Ehdr *const ehdr) +{ + if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 || + ehdr->e_ident[EI_CLASS] != ELFCLASS32 || + ehdr->e_ident[EI_VERSION] != EV_CURRENT) + { + return 1; + } + + /* Check if the target endianness matches the host's endianness */ + byteswap = 0; +#if __BYTE_ORDER == __LITTLE_ENDIAN + if (ehdr->e_ident[5] == ELFDATA2MSB) { + /* Ick -- we will have to byte-swap everything */ + byteswap = 1; + } +#elif __BYTE_ORDER == __BIG_ENDIAN + if (ehdr->e_ident[5] == ELFDATA2LSB) { + byteswap = 1; + } +#else +#error Unknown host byte order! +#endif + /* Be vary lazy, and only byteswap the stuff we use */ + if (byteswap==1) { + ehdr->e_type=bswap_16(ehdr->e_type); + ehdr->e_machine=bswap_16(ehdr->e_machine); + ehdr->e_phoff=bswap_32(ehdr->e_phoff); + ehdr->e_shoff=bswap_32(ehdr->e_shoff); + ehdr->e_phnum=bswap_16(ehdr->e_phnum); + ehdr->e_shnum=bswap_16(ehdr->e_shnum); + } + return 0; +} + + +#define ELFOSABI_NONE 0 /* UNIX System V ABI */ +#define ELFOSABI_HPUX 1 /* HP-UX operating system */ +#define ELFOSABI_NETBSD 2 /* NetBSD */ +#define ELFOSABI_LINUX 3 /* GNU/Linux */ +#define ELFOSABI_HURD 4 /* GNU/Hurd */ +#define ELFOSABI_SOLARIS 6 /* Solaris */ +#define ELFOSABI_AIX 7 /* AIX */ +#define ELFOSABI_IRIX 8 /* IRIX */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD */ +#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ +#define ELFOSABI_ARM 97 /* ARM */ +static void describe_elf_hdr(Elf32_Ehdr* ehdr) +{ + char *tmp, *tmp1; + + switch (ehdr->e_type) { + case ET_NONE: tmp = "None"; tmp1 = "NONE"; break; + case ET_REL: tmp = "Relocatable File"; tmp1 = "REL"; break; + case ET_EXEC: tmp = "Executable file"; tmp1 = "EXEC"; break; + case ET_DYN: tmp = "Shared object file"; tmp1 = "DYN"; break; + case ET_CORE: tmp = "Core file"; tmp1 = "CORE"; break; + default: + tmp = tmp1 = "Unknown"; + } + printf( "Type:\t\t%s (%s)\n", tmp1, tmp); + + switch (ehdr->e_machine) { + case EM_NONE: tmp="No machine"; break; + case EM_M32: tmp="AT&T WE 32100"; break; + case EM_SPARC: tmp="SUN SPARC"; break; + case EM_386: tmp="Intel 80386"; break; + case EM_68K: tmp="Motorola m68k family"; break; + case EM_88K: tmp="Motorola m88k family"; break; + case EM_860: tmp="Intel 80860"; break; + case EM_MIPS: tmp="MIPS R3000 big-endian"; break; + case EM_S370: tmp="IBM System/370"; break; + case EM_MIPS_RS3_LE: tmp="MIPS R3000 little-endian"; break; + case EM_PARISC: tmp="HPPA"; break; + case EM_VPP500: tmp="Fujitsu VPP500"; break; + case EM_SPARC32PLUS: tmp="Sun's v8plus"; break; + case EM_960: tmp="Intel 80960"; break; + case EM_PPC: tmp="PowerPC"; break; + case EM_PPC64: tmp="PowerPC 64-bit"; break; + case EM_S390: tmp="IBM S390"; break; + case EM_V800: tmp="NEC V800 series"; break; + case EM_FR20: tmp="Fujitsu FR20"; break; + case EM_RH32: tmp="TRW RH-32"; break; + case EM_RCE: tmp="Motorola RCE"; break; + case EM_ARM: tmp="ARM"; break; + case EM_FAKE_ALPHA: tmp="Digital Alpha"; break; + case EM_SH: tmp="Hitachi SH"; break; + case EM_SPARCV9: tmp="SPARC v9 64-bit"; break; + case EM_TRICORE: tmp="Siemens Tricore"; break; + case EM_ARC: tmp="Argonaut RISC Core"; break; + case EM_H8_300: tmp="Hitachi H8/300"; break; + case EM_H8_300H: tmp="Hitachi H8/300H"; break; + case EM_H8S: tmp="Hitachi H8S"; break; + case EM_H8_500: tmp="Hitachi H8/500"; break; + case EM_IA_64: tmp="Intel Merced"; break; + case EM_MIPS_X: tmp="Stanford MIPS-X"; break; + case EM_COLDFIRE: tmp="Motorola Coldfire"; break; + case EM_68HC12: tmp="Motorola M68HC12"; break; + case EM_MMA: tmp="Fujitsu MMA Multimedia Accelerator"; break; + case EM_PCP: tmp="Siemens PCP"; break; + case EM_NCPU: tmp="Sony nCPU embeeded RISC"; break; + case EM_NDR1: tmp="Denso NDR1 microprocessor"; break; + case EM_STARCORE: tmp="Motorola Start*Core processor"; break; + case EM_ME16: tmp="Toyota ME16 processor"; break; + case EM_ST100: tmp="STMicroelectronic ST100 processor"; break; + case EM_TINYJ: tmp="Advanced Logic Corp. Tinyj emb.fam"; break; + case EM_X86_64: tmp="AMD x86-64 architecture"; break; + case EM_PDSP: tmp="Sony DSP Processor"; break; + case EM_FX66: tmp="Siemens FX66 microcontroller"; break; + case EM_ST9PLUS: tmp="STMicroelectronics ST9+ 8/16 mc"; break; + case EM_ST7: tmp="STmicroelectronics ST7 8 bit mc"; break; + case EM_68HC16: tmp="Motorola MC68HC16 microcontroller"; break; + case EM_68HC11: tmp="Motorola MC68HC11 microcontroller"; break; + case EM_68HC08: tmp="Motorola MC68HC08 microcontroller"; break; + case EM_68HC05: tmp="Motorola MC68HC05 microcontroller"; break; + case EM_SVX: tmp="Silicon Graphics SVx"; break; + case EM_AT19: tmp="STMicroelectronics ST19 8 bit mc"; break; + case EM_VAX: tmp="Digital VAX"; break; + case EM_CRIS: tmp="Axis Communications 32-bit embedded processor"; break; + case EM_JAVELIN: tmp="Infineon Technologies 32-bit embedded processor"; break; + case EM_FIREPATH: tmp="Element 14 64-bit DSP Processor"; break; + case EM_ZSP: tmp="LSI Logic 16-bit DSP Processor"; break; + case EM_MMIX: tmp="Donald Knuth's educational 64-bit processor"; break; + case EM_HUANY: tmp="Harvard University machine-independent object files"; break; + case EM_PRISM: tmp="SiTera Prism"; break; + case EM_AVR: tmp="Atmel AVR 8-bit microcontroller"; break; + case EM_FR30: tmp="Fujitsu FR30"; break; + case EM_D10V: tmp="Mitsubishi D10V"; break; + case EM_D30V: tmp="Mitsubishi D30V"; break; + case EM_V850: tmp="NEC v850"; break; + case EM_M32R: tmp="Mitsubishi M32R"; break; + case EM_MN10300: tmp="Matsushita MN10300"; break; + case EM_MN10200: tmp="Matsushita MN10200"; break; + case EM_PJ: tmp="picoJava"; break; + default: tmp="unknown"; + } + printf( "Machine:\t%s\n", tmp); + + switch (ehdr->e_ident[EI_CLASS]) { + case ELFCLASSNONE: tmp = "Invalid class"; break; + case ELFCLASS32: tmp = "ELF32"; break; + case ELFCLASS64: tmp = "ELF64"; break; + default: tmp = "Unknown"; + } + printf( "Class:\t\t%s\n", tmp); + + switch (ehdr->e_ident[EI_DATA]) { + case ELFDATANONE: tmp = "Invalid data encoding"; break; + case ELFDATA2LSB: tmp = "2's complement, little endian"; break; + case ELFDATA2MSB: tmp = "2's complement, big endian"; break; + default: tmp = "Unknown"; + } + printf( "Data:\t\t%s\n", tmp); + + printf( "Version:\t%d %s\n", ehdr->e_ident[EI_VERSION], + (ehdr->e_ident[EI_VERSION]==EV_CURRENT)? + "(current)" : "(unknown: %lx)"); + + switch (ehdr->e_ident[EI_OSABI]) { + case ELFOSABI_SYSV: tmp ="UNIX - System V"; break; + case ELFOSABI_HPUX: tmp ="UNIX - HP-UX"; break; + case ELFOSABI_NETBSD: tmp ="UNIX - NetBSD"; break; + case ELFOSABI_LINUX: tmp ="UNIX - Linux"; break; + case ELFOSABI_HURD: tmp ="GNU/Hurd"; break; + case ELFOSABI_SOLARIS: tmp ="UNIX - Solaris"; break; + case ELFOSABI_AIX: tmp ="UNIX - AIX"; break; + case ELFOSABI_IRIX: tmp ="UNIX - IRIX"; break; + case ELFOSABI_FREEBSD: tmp ="UNIX - FreeBSD"; break; + case ELFOSABI_TRU64: tmp ="UNIX - TRU64"; break; + case ELFOSABI_MODESTO: tmp ="Novell - Modesto"; break; + case ELFOSABI_OPENBSD: tmp ="UNIX - OpenBSD"; break; + case ELFOSABI_STANDALONE: tmp ="Standalone App"; break; + case ELFOSABI_ARM: tmp ="ARM"; break; + default: tmp = "Unknown"; + } + printf( "OS/ABI:\t\t%s\n", tmp); + + printf( "ABI Version:\t%d\n", ehdr->e_ident[EI_ABIVERSION]); +} + +static void list_needed_libraries(Elf32_Dyn* dynamic, char *strtab) +{ + Elf32_Dyn *dyns; + + printf("Dependancies:\n"); + for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) { + if (dyns->d_tag == DT_NEEDED) { + printf("\t%s\n", (char*)strtab + byteswap32_to_host(dyns->d_un.d_val)); + } + } +} + +static void describe_elf_interpreter(Elf32_Ehdr* ehdr) +{ + Elf32_Phdr *phdr; + phdr = elf_find_phdr_type(PT_INTERP, ehdr); + if (phdr) { + printf("Interpreter:\t%s\n", (char*)ehdr + byteswap32_to_host(phdr->p_offset)); + } +} + +int main( int argc, char** argv) +{ + /* map the .so, and locate interesting pieces */ + char *dynstr; + char *thefilename = argv[1]; + FILE *thefile; + struct stat statbuf; + Elf32_Ehdr *ehdr = 0; + Elf32_Shdr *dynsec; + Elf32_Dyn *dynamic; + + if (argc < 2 || !thefilename) { + fprintf(stderr, "No filename specified.\n"); + exit(EXIT_FAILURE); + } + if (!(thefile = fopen(thefilename, "r"))) { + perror(thefilename); + exit(EXIT_FAILURE); + } + if (fstat(fileno(thefile), &statbuf) < 0) { + perror(thefilename); + exit(EXIT_FAILURE); + } + + if ((size_t)statbuf.st_size < sizeof(Elf32_Ehdr)) + goto foo; + + /* mmap the file to make reading stuff from it effortless */ + ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size, + PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0); + +foo: + /* Check if this looks legit */ + if (check_elf_header(ehdr)) { + fprintf(stderr, "This does not appear to be an ELF file.\n"); + exit(EXIT_FAILURE); + } + describe_elf_hdr(ehdr); + describe_elf_interpreter(ehdr); + + dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr); + if (dynsec) { + dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr); + dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); + list_needed_libraries(dynamic, dynstr); + } + + return 0; +} + diff --git a/utils/readsoname.c b/utils/readsoname.c new file mode 100644 index 000000000..12c2428f2 --- /dev/null +++ b/utils/readsoname.c @@ -0,0 +1,63 @@ +/* adapted from Eric Youngdale's readelf program */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "readsoname.h" + +void warn(char *fmt, ...); +char *xstrdup(char *); + +struct needed_tab +{ + char *soname; + int type; +}; + +struct needed_tab needed_tab[] = { + { "libc.so.0", LIB_ELF_LIBC0 }, + { "libm.so.0", LIB_ELF_LIBC0 }, + { "libdl.so.0", LIB_ELF_LIBC0 }, + { "libc.so.5", LIB_ELF_LIBC5 }, + { "libm.so.5", LIB_ELF_LIBC5 }, + { "libdl.so.1", LIB_ELF_LIBC5 }, + { "libc.so.6", LIB_ELF_LIBC6 }, + { "libm.so.6", LIB_ELF_LIBC6 }, + { "libdl.so.2", LIB_ELF_LIBC6 }, + { NULL, LIB_ELF } +}; + +char *readsoname(char *name, FILE *infile, int expected_type, + int *type, int elfclass) +{ + char *res; + + if (elfclass == ELFCLASS32) + res = readsoname32(name, infile, expected_type, type); + else + { + res = readsoname64(name, infile, expected_type, type); +#if 0 + *type |= LIB_ELF64; +#endif + } + + return res; +} + +#undef __ELF_NATIVE_CLASS +#undef readsonameXX +#define readsonameXX readsoname32 +#define __ELF_NATIVE_CLASS 32 +#include "readsoname2.c" + +#undef __ELF_NATIVE_CLASS +#undef readsonameXX +#define readsonameXX readsoname64 +#define __ELF_NATIVE_CLASS 64 +#include "readsoname2.c" diff --git a/utils/readsoname.h b/utils/readsoname.h new file mode 100644 index 000000000..78d2216e0 --- /dev/null +++ b/utils/readsoname.h @@ -0,0 +1,4 @@ +char *readsoname(char *name, FILE *file, int expected_type, + int *type, int elfclass); +char *readsoname32(char *name, FILE *file, int expected_type, int *type); +char *readsoname64(char *name, FILE *file, int expected_type, int *type); diff --git a/utils/readsoname2.c b/utils/readsoname2.c new file mode 100644 index 000000000..1bf47b7c6 --- /dev/null +++ b/utils/readsoname2.c @@ -0,0 +1,115 @@ +char *readsonameXX(char *name, FILE *infile, int expected_type, int *type) +{ + ElfW(Ehdr) *epnt; + ElfW(Phdr) *ppnt; + int i, j; + char *header; + ElfW(Word) dynamic_addr = 0; + ElfW(Word) dynamic_size = 0; + unsigned long page_size = getpagesize(); + ElfW(Word) strtab_val = 0; + ElfW(Word) needed_val; + ElfW(Sword) loadaddr = -1; + ElfW(Dyn) *dpnt; + struct stat st; + char *needed; + char *soname = NULL; + int multi_libcs = 0; + + if(expected_type == LIB_DLL) + { + warn("%s does not match type specified for directory!", name); + expected_type = LIB_ANY; + } + + *type = LIB_ELF; + + if (fstat(fileno(infile), &st)) + return NULL; + header = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fileno(infile), 0); + if (header == (caddr_t)-1) + return NULL; + + epnt = (ElfW(Ehdr) *)header; + if ((char *)(epnt+1) > (char *)(header + st.st_size)) + goto skip; + + ppnt = (ElfW(Phdr) *)&header[epnt->e_phoff]; + if ((char *)ppnt < (char *)header || + (char *)(ppnt+epnt->e_phnum) > (char *)(header + st.st_size)) + goto skip; + + for(i = 0; i < epnt->e_phnum; i++) + { + if (loadaddr == -1 && ppnt->p_type == PT_LOAD) + loadaddr = (ppnt->p_vaddr & ~(page_size-1)) - + (ppnt->p_offset & ~(page_size-1)); + if(ppnt->p_type == 2) + { + dynamic_addr = ppnt->p_offset; + dynamic_size = ppnt->p_filesz; + }; + ppnt++; + }; + + dpnt = (ElfW(Dyn) *) &header[dynamic_addr]; + dynamic_size = dynamic_size / sizeof(ElfW(Dyn)); + if ((char *)dpnt < (char *)header || + (char *)(dpnt+dynamic_size) > (char *)(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; + dpnt++; + }; + + if (!strtab_val) + goto skip; + + dpnt = (ElfW(Dyn) *) &header[dynamic_addr]; + while (dpnt->d_tag != DT_NULL) + { + if (dpnt->d_tag == DT_SONAME || dpnt->d_tag == DT_NEEDED) + { + needed_val = dpnt->d_un.d_val; + if (needed_val + strtab_val - loadaddr >= 0 || + needed_val + strtab_val - loadaddr < st.st_size) + { + needed = (char *) (header - loadaddr + strtab_val + needed_val); + + if (dpnt->d_tag == DT_SONAME) + soname = xstrdup(needed); + + for (j = 0; needed_tab[j].soname != NULL; j++) + { + if (strcmp(needed, needed_tab[j].soname) == 0) + { + if (*type != LIB_ELF && *type != needed_tab[j].type) + multi_libcs = 1; + *type = needed_tab[j].type; + } + } + } + } + dpnt++; + }; + + if (multi_libcs) + warn("%s appears to be for multiple libc's", name); + + /* If we could not deduce the libc type, and we know what to expect, set the type */ + if(*type == LIB_ELF && expected_type != LIB_ANY) *type = expected_type; + + if(expected_type != LIB_ANY && expected_type != LIB_ELF && + expected_type != *type) + { + warn("%s does not match type specified for directory!", name); + } + + skip: + munmap(header, st.st_size); + + return soname; +} -- cgit v1.2.3