diff options
author | Mike Frysinger <vapier@gentoo.org> | 2007-04-17 22:31:01 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2007-04-17 22:31:01 +0000 |
commit | a77c1608a45b4c8870d9e98b2ee44ed7228646ca (patch) | |
tree | 510136ddfcf02ec110fa0e334527b0924959d737 /utils | |
parent | 338053615c6e1504a290355c66e105826ae9e51f (diff) |
cleanup style; no functional changes
Diffstat (limited to 'utils')
-rw-r--r-- | utils/chroot_realpath.c | 15 | ||||
-rw-r--r-- | utils/ldconfig.c | 1479 | ||||
-rw-r--r-- | utils/ldd.c | 210 | ||||
-rw-r--r-- | utils/readsoname2.c | 268 |
4 files changed, 952 insertions, 1020 deletions
diff --git a/utils/chroot_realpath.c b/utils/chroot_realpath.c index dd33c3fc0..0785c6c5b 100644 --- a/utils/chroot_realpath.c +++ b/utils/chroot_realpath.c @@ -29,14 +29,14 @@ #include <stdio.h> #include <string.h> #include <strings.h> -#include <limits.h> /* for PATH_MAX */ -#include <sys/param.h> /* for MAXPATHLEN */ +#include <limits.h> /* for PATH_MAX */ +#include <sys/param.h> /* for MAXPATHLEN */ #include <errno.h> #ifndef __set_errno #define __set_errno(val) ((errno) = (val)) #endif -#include <sys/stat.h> /* for S_IFLNK */ +#include <sys/stat.h> /* for S_IFLNK */ #ifndef PATH_MAX #define PATH_MAX _POSIX_PATH_MAX @@ -44,7 +44,8 @@ #define MAX_READLINKS 32 -char *chroot_realpath(const char *chroot, const char *path, char resolved_path[]) +char *chroot_realpath(const char *chroot, const char *path, + char resolved_path[]) { char copy_path[PATH_MAX]; char link_path[PATH_MAX]; @@ -103,7 +104,7 @@ char *chroot_realpath(const char *chroot, const char *path, char resolved_path[] if (new_path == got_path_root + 1) continue; /* Handle ".." by backing up. */ - while ((--new_path)[-1] != '/'); + while ((--new_path)[-1] != '/') ; continue; } } @@ -144,7 +145,7 @@ char *chroot_realpath(const char *chroot, const char *path, char resolved_path[] new_path = got_path_root; else /* Otherwise back up over this component. */ - while (*(--new_path) != '/'); + while (*(--new_path) != '/') ; /* Safe sex check. */ if (strlen(path) + n >= PATH_MAX - 2) { __set_errno(ENAMETOOLONG); @@ -155,7 +156,7 @@ char *chroot_realpath(const char *chroot, const char *path, char resolved_path[] strcpy(copy_path, link_path); path = copy_path; } -#endif /* S_IFLNK */ +#endif /* S_IFLNK */ *new_path++ = '/'; } /* Delete trailing slash but don't whomp a lone slash. */ diff --git a/utils/ldconfig.c b/utils/ldconfig.c index 671561238..a5d3ce67c 100644 --- a/utils/ldconfig.c +++ b/utils/ldconfig.c @@ -44,16 +44,15 @@ #define BUFFER_SIZE 4096 -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 */ +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) @@ -66,7 +65,7 @@ struct exec #define NMAGIC 0410 /* Code indicating demand-paged executable. */ #define ZMAGIC 0413 -/* This indicates a demand-paged executable with the header in the text. +/* 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. */ @@ -104,27 +103,26 @@ char *cachefile = LDSO_CACHE; /* default cache file */ char *chroot_dir = NULL; int byteswap = 0; -struct needed_tab -{ - char *soname; - int type; +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 } + {"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} }; -extern char *chroot_realpath(const char *chroot, const char *path, char resolved_path[]); - +extern char *chroot_realpath(const char *chroot, const char *path, + char resolved_path[]); /* These two are used internally -- you shouldn't need to use them */ static void verror_msg(const char *s, va_list p) @@ -178,21 +176,20 @@ static void warn(const char *s, ...) static void *xmalloc(size_t size) { - void *ptr; - if ((ptr = malloc(size)) == NULL) - err(EXIT_FATAL,"out of memory"); - return ptr; + void *ptr; + if ((ptr = malloc(size)) == NULL) + err(EXIT_FATAL, "out of memory"); + return ptr; } static char *xstrdup(const char *str) { - char *ptr; - if ((ptr = strdup(str)) == NULL) - err(EXIT_FATAL,"out of memory"); - return ptr; + char *ptr; + if ((ptr = strdup(str)) == NULL) + err(EXIT_FATAL, "out of memory"); + return ptr; } - #undef __ELF_NATIVE_CLASS #undef readsonameXX #define readsonameXX readsoname32 @@ -204,477 +201,426 @@ static char *xstrdup(const char *str) #define readsonameXX readsoname64 #define __ELF_NATIVE_CLASS 64 #include "readsoname2.c" - -char *readsoname(char *name, FILE *infile, int expected_type, +char *readsoname(char *name, FILE *infile, int expected_type, int *type, int elfclass) { - char *res; + char *res; - if (elfclass == ELFCLASS32) - res = readsoname32(name, infile, expected_type, type); - else - { - res = readsoname64(name, infile, expected_type, type); + if (elfclass == ELFCLASS32) + res = readsoname32(name, infile, expected_type, type); + else { + res = readsoname64(name, infile, expected_type, type); #if 0 - *type |= LIB_ELF64; + /* relies on multilib support which we dont have ... */ + *type |= LIB_ELF64; #endif - } + } - return res; + return res; } - - /* 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. -*/ + * 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) + 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[BUFFER_SIZE]; - char real[BUFFER_SIZE]; - static int byteswapflag = -1; /* start with byte-order unknown */ - - /* see if name is of the form *.so* */ - if (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); - - /* get real path in case of chroot */ - if (!chroot_realpath(chroot_dir, buff, real)) - warn("can't resolve %s in chroot %s", buff, chroot_dir); - - /* first, make sure it's a regular file */ - if (lstat(real, &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(real, "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 && - N_MAGIC_SWAP(exec) != ZMAGIC && N_MAGIC_SWAP(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, it's ELF!", buff); - expected_type=LIB_ANY; - } - *type = LIB_ELF; - good = readsoname(buff, file, expected_type, type, - elf_hdr->e_ident[EI_CLASS]); - if (byteswapflag == -1) - /* byte-order detected */ - byteswapflag = byteswap; - 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); - } - } - } + char *good = NULL; + char *cp, *cp2; + FILE *file; + struct exec exec; + ElfW(Ehdr) *elf_hdr; + struct stat statbuf; + char buff[BUFFER_SIZE]; + char real[BUFFER_SIZE]; + static int byteswapflag = -1; /* start with byte-order unknown */ + + /* see if name is of the form *.so* */ + if (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 - { - /* Determine byte-order */ - byteswap = (N_MAGIC(exec) == ZMAGIC || N_MAGIC(exec) == QMAGIC) ? 0 : 1; - if (byteswapflag == -1) - /* byte-order detected */ - byteswapflag = byteswap; - - 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); - - if (byteswapflag >= 0 && byteswap != byteswapflag) - { - byteswapflag = -2; - warnx("mixed byte-order detected, using host byte-order..."); + cp = cp + strlen(cp); + + /* construct the full path name */ + sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ? "/" : "", name); + + /* get real path in case of chroot */ + if (!chroot_realpath(chroot_dir, buff, real)) + warn("can't resolve %s in chroot %s", buff, chroot_dir); + + /* first, make sure it's a regular file */ + if (lstat(real, &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(real, "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 + && N_MAGIC_SWAP(exec) != ZMAGIC + && N_MAGIC_SWAP(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, it's ELF!", buff); + expected_type = LIB_ANY; + } + *type = LIB_ELF; + good = readsoname(buff, file, expected_type, type, + elf_hdr->e_ident[EI_CLASS]); + if (byteswapflag == -1) + /* byte-order detected */ + byteswapflag = byteswap; + 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 { + /* Determine byte-order */ + byteswap = (N_MAGIC(exec) == ZMAGIC || N_MAGIC(exec) == QMAGIC) ? 0 : 1; + if (byteswapflag == -1) + /* byte-order detected */ + byteswapflag = byteswap; + + 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); + + if (byteswapflag >= 0 && byteswap != byteswapflag) { + byteswapflag = -2; + warnx("mixed byte-order detected, using host byte-order..."); + } + if (byteswapflag == -2) + byteswap = 0; + } } - if (byteswapflag == -2) - byteswap = 0; - } } - } - return good; + 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[BUFFER_SIZE]; - char linkname[BUFFER_SIZE]; - char reallibname[BUFFER_SIZE]; - char reallinkname[BUFFER_SIZE]; - struct stat libstat; - struct stat linkstat; - - /* construct the full path names */ - sprintf(libname, "%s/%s", dir, file); - sprintf(linkname, "%s/%s", dir, so); - if (!chroot_realpath(chroot_dir, libname, reallibname)) - warn("can't resolve %s in chroot %s", libname, chroot_dir); - if (!chroot_realpath(chroot_dir, linkname, reallinkname)) - warn("can't resolve %s in chroot %s", linkname, chroot_dir); - - /* see if a link already exists */ - if (!stat(reallinkname, &linkstat)) - { - /* now see if it's the one we want */ - if (stat(reallibname, &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(reallinkname, &linkstat)) - { - if (!S_ISLNK(linkstat.st_mode)) - { - warnx("%s is not a symlink", linkname); - change = -1; - } - else if (remove(reallinkname)) - { - warn("can't unlink %s", linkname); - change = -1; - } + int change = 1; + char libname[BUFFER_SIZE]; + char linkname[BUFFER_SIZE]; + char reallibname[BUFFER_SIZE]; + char reallinkname[BUFFER_SIZE]; + struct stat libstat; + struct stat linkstat; + + /* construct the full path names */ + sprintf(libname, "%s/%s", dir, file); + sprintf(linkname, "%s/%s", dir, so); + if (!chroot_realpath(chroot_dir, libname, reallibname)) + warn("can't resolve %s in chroot %s", libname, chroot_dir); + if (!chroot_realpath(chroot_dir, linkname, reallinkname)) + warn("can't resolve %s in chroot %s", linkname, chroot_dir); + + /* see if a link already exists */ + if (!stat(reallinkname, &linkstat)) { + /* now see if it's the one we want */ + if (stat(reallibname, &libstat)) + warn("can't stat %s", libname); + else if (libstat.st_dev == linkstat.st_dev && + libstat.st_ino == linkstat.st_ino) + change = 0; } - if (change > 0) - { - if (symlink(file, reallinkname)) - { - warn("can't link %s to %s", linkname, file); - change = -1; - } + + /* then update the link, if required */ + if (change > 0 && !nolinks) { + if (!lstat(reallinkname, &linkstat)) { + if (!S_ISLNK(linkstat.st_mode)) { + warnx("%s is not a symlink", linkname); + change = -1; + } else if (remove(reallinkname)) { + warn("can't unlink %s", linkname); + change = -1; + } + } + if (change > 0) { + if (symlink(file, reallinkname)) { + 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)" : "")); + /* 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; + 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; + 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++; } - 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; + + 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 */ +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; - char realname[BUFFER_SIZE]; - - /* 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; + 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; + char realname[BUFFER_SIZE]; + + /* 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", t, name); - expected_type = LIB_ANY; - } + 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", t, name); + expected_type = LIB_ANY; + } + } + } } - } } - } #endif - /* let 'em know what's going on */ - if (verbose > 0) - printf("%s:\n", name); + /* let 'em know what's going on */ + if (verbose > 0) + printf("%s:\n", name); - /* get real path in case of chroot */ - if (!chroot_realpath(chroot_dir, name, realname)) - warn("can't resolve %s in chroot %s", name, chroot_dir); + /* get real path in case of chroot */ + if (!chroot_realpath(chroot_dir, name, realname)) + warn("can't resolve %s in chroot %s", name, chroot_dir); + + /* if we can't open it, we can't do anything */ + if ((dir = opendir(realname)) == NULL) { + warn("skipping %s", name); + free(path); + return; + } - /* if we can't open it, we can't do anything */ - if ((dir = opendir(realname)) == 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; - } + /* 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); + if (!nocache) + cache_dolib(name, lp->so, lp->libtype); } - /* 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; + /* always try to clean up after ourselves */ + while (libs) { + lp = libs->next; + free(libs->so); + free(libs->name); + free(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); - if (!nocache) - cache_dolib(name, lp->so, lp->libtype); - } - - /* 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; + free(path); + return; } #ifndef __LDSO_CACHE_SUPPORT__ void cache_print(void) { - printf("Library cache disabled\n"); + printf("Library cache disabled\n"); } void cache_dolib(const char *dir, const char *so, int libtype) { - return; + return; } void cache_write(void) { - return; + return; } #else /* return the list of system-specific directories */ char *get_extpath(void) { - char *res = NULL, *cp; - FILE *file; - struct stat stat; - char realconffile[BUFFER_SIZE]; - - if (!chroot_realpath(chroot_dir, conffile, realconffile)) - return NULL; - - if ((file = fopen(realconffile, "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; + char *res = NULL, *cp; + FILE *file; + struct stat stat; + char realconffile[BUFFER_SIZE]; + + if (!chroot_realpath(chroot_dir, conffile, realconffile)) + return NULL; + + if ((file = fopen(realconffile, "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; } -typedef struct liblist -{ - int flags; - int sooffset; - int liboffset; - char *soname; - char *libname; - struct liblist *next; +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 }; @@ -682,393 +628,382 @@ static liblist_t *lib_head = NULL; static int liblistcomp(liblist_t *x, liblist_t *y) { - int res; + int res; - if ((res = libcmp(x->soname, y->soname)) == 0) - { - res = libcmp(strrchr(x->libname, '/') + 1, - strrchr(y->libname, '/') + 1); - } + if ((res = libcmp(x->soname, y->soname)) == 0) { + res = libcmp(strrchr(x->libname, '/') + 1, + strrchr(y->libname, '/') + 1); + } - return res; + 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; - } + 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 realcachefile[BUFFER_SIZE]; - char tempfile[BUFFER_SIZE]; - header_t swap_magic; - header_t *magic_ptr; - libentry_t swap_lib; - libentry_t *lib_ptr; - liblist_t *cur_lib; - - if (!magic.nlibs) - return; + int cachefd; + int stroffset = 0; + char realcachefile[BUFFER_SIZE]; + char tempfile[BUFFER_SIZE]; + header_t swap_magic; + header_t *magic_ptr; + libentry_t swap_lib; + libentry_t *lib_ptr; + liblist_t *cur_lib; + + if (!magic.nlibs) + return; + + if (!chroot_realpath(chroot_dir, cachefile, realcachefile)) + err(EXIT_FATAL, "can't resolve %s in chroot %s (%s)", + cachefile, chroot_dir, strerror(errno)); + + sprintf(tempfile, "%s~", realcachefile); + + if (unlink(tempfile) && errno != ENOENT) + err(EXIT_FATAL, "can't unlink %s~ (%s)", cachefile, + strerror(errno)); + + if ((cachefd = creat(tempfile, 0644)) < 0) + err(EXIT_FATAL, "can't create %s~ (%s)", cachefile, + strerror(errno)); - if (!chroot_realpath(chroot_dir, cachefile, realcachefile)) - err(EXIT_FATAL,"can't resolve %s in chroot %s (%s)", - cachefile, chroot_dir, strerror(errno)); - - sprintf(tempfile, "%s~", realcachefile); - - if (unlink(tempfile) && errno != ENOENT) - err(EXIT_FATAL,"can't unlink %s~ (%s)", cachefile, strerror(errno)); - - if ((cachefd = creat(tempfile, 0644)) < 0) - err(EXIT_FATAL,"can't create %s~ (%s)", cachefile, strerror(errno)); - - if (byteswap) { - swap_magic = magic; - swap_magic.nlibs = bswap_32(swap_magic.nlibs); - magic_ptr = &swap_magic; - } else { - magic_ptr = &magic; - } - if (write(cachefd, magic_ptr, sizeof (header_t)) != sizeof (header_t)) - err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, 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 (byteswap) { - swap_lib.flags = bswap_32(cur_lib->flags); - swap_lib.sooffset = bswap_32(cur_lib->sooffset); - swap_lib.liboffset = bswap_32(cur_lib->liboffset); - lib_ptr = &swap_lib; + swap_magic = magic; + swap_magic.nlibs = bswap_32(swap_magic.nlibs); + magic_ptr = &swap_magic; } else { - lib_ptr = (libentry_t *)cur_lib; + magic_ptr = &magic; } - if (write(cachefd, lib_ptr, sizeof (libentry_t)) != - sizeof (libentry_t)) - err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno)); - } - - for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) - { - if ((size_t)write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1) - != strlen(cur_lib->soname) + 1) - err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno)); - if ((size_t)write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1) - != strlen(cur_lib->libname) + 1) - err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno)); - } - - if (close(cachefd)) - err(EXIT_FATAL,"can't close %s~ (%s)", cachefile, strerror(errno)); - - if (chmod(tempfile, 0644)) - err(EXIT_FATAL,"can't chmod %s~ (%s)", cachefile, strerror(errno)); - - if (rename(tempfile, realcachefile)) - err(EXIT_FATAL,"can't rename %s~ (%s)", cachefile, strerror(errno)); + if (write(cachefd, magic_ptr, sizeof(header_t)) != sizeof(header_t)) + err(EXIT_FATAL, "can't write %s~ (%s)", cachefile, + 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 (byteswap) { + swap_lib.flags = bswap_32(cur_lib->flags); + swap_lib.sooffset = bswap_32(cur_lib->sooffset); + swap_lib.liboffset = bswap_32(cur_lib->liboffset); + lib_ptr = &swap_lib; + } else { + lib_ptr = (libentry_t *) cur_lib; + } + if (write(cachefd, lib_ptr, sizeof(libentry_t)) != + sizeof(libentry_t)) + err(EXIT_FATAL, "can't write %s~ (%s)", cachefile, + strerror(errno)); + } + + for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) { + if ((size_t)write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1) + != strlen(cur_lib->soname) + 1) + err(EXIT_FATAL, "can't write %s~ (%s)", cachefile, + strerror(errno)); + if ((size_t)write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1) + != strlen(cur_lib->libname) + 1) + err(EXIT_FATAL, "can't write %s~ (%s)", cachefile, + strerror(errno)); + } + + if (close(cachefd)) + err(EXIT_FATAL, "can't close %s~ (%s)", cachefile, + strerror(errno)); + + if (chmod(tempfile, 0644)) + err(EXIT_FATAL, "can't chmod %s~ (%s)", cachefile, + strerror(errno)); + + if (rename(tempfile, realcachefile)) + err(EXIT_FATAL, "can't rename %s~ (%s)", cachefile, + strerror(errno)); } void cache_print(void) { - caddr_t c; - struct stat st; - int fd = 0; - char *strs; - header_t *header; - libentry_t *libent; - char realcachefile[BUFFER_SIZE]; - - if (!chroot_realpath(chroot_dir, cachefile, realcachefile)) - err(EXIT_FATAL,"can't resolve %s in chroot %s (%s)", - cachefile, chroot_dir, strerror(errno)); - - if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY))<0) - err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno)); - if ((c = mmap(0,st.st_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, 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_LIBC0: - printf("(libc0%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; + caddr_t c; + struct stat st; + int fd = 0; + char *strs; + header_t *header; + libentry_t *libent; + char realcachefile[BUFFER_SIZE]; + + if (!chroot_realpath(chroot_dir, cachefile, realcachefile)) + err(EXIT_FATAL, "can't resolve %s in chroot %s (%s)", + cachefile, chroot_dir, strerror(errno)); + + if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY)) < 0) + err(EXIT_FATAL, "can't read %s (%s)", cachefile, strerror(errno)); + + c = mmap(0, st.st_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0); + if (c == MAP_FAILED) + 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_LIBC0: + printf("(libc0%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); } - printf(" => %s\n", strs + libent[fd].liboffset); - } - munmap (c,st.st_size); + munmap(c, st.st_size); } #endif void usage(void) { - fprintf(stderr, + fprintf(stderr, #ifdef __LDSO_CACHE_SUPPORT__ - "ldconfig - updates symlinks and cache 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" + "ldconfig - updates symlinks and cache 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" #else - "ldconfig - updates symlinks for shared libraries\n\n" - "Usage: ldconfig [-DvqnX] [-r root] dir ...\n" - " ldconfig -l [-Dv] lib ...\n\nOptions:\n" + "ldconfig - updates symlinks for shared libraries\n\n" + "Usage: ldconfig [-DvqnX] [-r root] dir ...\n" + " ldconfig -l [-Dv] lib ...\n\nOptions:\n" #endif - "\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-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" #ifdef __LDSO_CACHE_SUPPORT__ - "\t-f conf :\tuse conf instead of %s\n" - "\t-C cache:\tuse cache instead of %s\n" + "\t-f conf :\tuse conf instead of %s\n" + "\t-C cache:\tuse cache instead of %s\n" #endif - "\t-r root :\tfirst, do a chroot to the indicated directory\n" - "\tdir ... :\tdirectories to process\n" + "\t-r root :\tfirst, do a chroot to the indicated directory\n" + "\tdir ... :\tdirectories to process\n" #ifdef __LDSO_CACHE_SUPPORT__ - "\tlib ... :\tlibraries to link\n\n", - LDSO_CONF, LDSO_CACHE + "\tlib ... :\tlibraries to link\n\n", LDSO_CONF, LDSO_CACHE #else - "\tlib ... :\tlibraries to link\n\n" + "\tlib ... :\tlibraries to link\n\n" #endif - ); - exit(EXIT_FATAL); + ); + exit(EXIT_FATAL); } #define DIR_SEP ":, \t\n" int main(int argc, char **argv) { - int i, c; - int nodefault = 0; - char *cp, *dir, *so; - int libtype, islink; - int printcache = 0; + int i, c; + int nodefault = 0; + char *cp, *dir, *so; + int libtype, islink; + int printcache = 0; #ifdef __LDSO_CACHE_SUPPORT__ - char *extpath; + char *extpath; #endif - 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': + 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': #ifdef __LDSO_CACHE_SUPPORT__ - conffile = optarg; /* alternate conf file */ + conffile = optarg; /* alternate conf file */ #endif - break; - case 'C': + break; + case 'C': #ifdef __LDSO_CACHE_SUPPORT__ - cachefile = optarg; /* alternate cache file */ + cachefile = optarg; /* alternate cache file */ #endif - break; - case 'r': - chroot_dir = optarg; - break; - default: - usage(); - break; - - /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED - IN FUTURE VERSIONS. */ - } + 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) { - if (chdir(chroot_dir) < 0) - err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno)); - } - else - { - if (chdir("/") < 0) - err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno)); - chroot_dir = NULL; + if (chroot_dir && *chroot_dir) { + if (chroot(chroot_dir) < 0) { + if (chdir(chroot_dir) < 0) + err(EXIT_FATAL, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno)); + } else { + if (chdir("/") < 0) + err(EXIT_FATAL, "couldn't chdir to / (%s)", strerror(errno)); + chroot_dir = NULL; + } } - } - /* allow me to introduce myself, hi, my name is ... */ - if (verbose > 0) - printf("%s: uClibc version\n", argv[0]); + /* 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 */ + } - 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); - /* 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 */ - /* 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) - { - scan_dir(UCLIBC_RUNTIME_PREFIX "lib"); - scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib"); + /* 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) { + scan_dir(UCLIBC_RUNTIME_PREFIX "lib"); + scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib"); #ifndef __LDSO_CACHE_SUPPORT__ - scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"); + scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"); #else - /* I guess the defaults aren't good enough */ - if ((extpath = get_extpath())) - { - for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) { - /* strip trailing slashes */ - int len = strlen(cp); - if (len) - while (cp[--len] == '/' && len) - cp[len] = 0; - /* we do the redundancy check only if cache usage is enabled */ - if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 || - strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) { - if (verbose >= 0) - warnx("You should remove `%s' from `%s'", cp, LDSO_CONF); - continue; + /* I guess the defaults aren't good enough */ + if ((extpath = get_extpath())) { + for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) { + /* strip trailing slashes */ + int len = strlen(cp); + if (len) + while (cp[--len] == '/' && len) + cp[len] = 0; + /* we do the redundancy check only if cache usage is enabled */ + if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 + || strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) { + if (verbose >= 0) + warnx("You should remove `%s' from `%s'", cp, LDSO_CONF); + continue; + } + scan_dir(cp); + } + free(extpath); } - scan_dir(cp); - } - free(extpath); - } #endif - } + } - if (!nocache) - cache_write(); - } + if (!nocache) + cache_write(); + } - exit(EXIT_OK); + exit(EXIT_OK); } - diff --git a/utils/ldd.c b/utils/ldd.c index 65aeb31fb..75ad628ea 100644 --- a/utils/ldd.c +++ b/utils/ldd.c @@ -151,49 +151,50 @@ char not_found[] = "not found"; char *interp_name = NULL; char *interp_dir = NULL; int byteswap; -static int interpreter_already_found=0; +static int interpreter_already_found = 0; inline uint32_t byteswap32_to_host(uint32_t value) { - if (byteswap==1) { - return(bswap_32(value)); + if (byteswap == 1) { + return (bswap_32(value)); } else { - return(value); + return (value); } } inline uint64_t byteswap64_to_host(uint64_t value) { - if (byteswap==1) { - return(bswap_64(value)); + if (byteswap == 1) { + return (bswap_64(value)); } else { - return(value); + return (value); } } + #if ELFCLASSM == ELFCLASS32 # define byteswap_to_host(x) byteswap32_to_host(x) #else # define byteswap_to_host(x) byteswap64_to_host(x) #endif -ElfW(Shdr) * elf_find_section_type( uint32_t key, ElfW(Ehdr) *ehdr) +ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr) { int j; ElfW(Shdr) *shdr; - shdr = (ElfW(Shdr) *)(ehdr->e_shoff + (char *)ehdr); - for (j = ehdr->e_shnum; --j>=0; ++shdr) { - if (key==byteswap32_to_host(shdr->sh_type)) { + shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr); + for (j = ehdr->e_shnum; --j >= 0; ++shdr) { + if (key == byteswap32_to_host(shdr->sh_type)) { return shdr; } } return NULL; } -ElfW(Phdr) * elf_find_phdr_type( uint32_t type, ElfW(Ehdr) *ehdr) +ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr) { int j; - ElfW(Phdr) *phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (char *)ehdr); - for (j = ehdr->e_phnum; --j>=0; ++phdr) { - if (type==byteswap32_to_host(phdr->p_type)) { + ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr); + for (j = ehdr->e_phnum; --j >= 0; ++phdr) { + if (type == byteswap32_to_host(phdr->p_type)) { return phdr; } } @@ -201,31 +202,31 @@ ElfW(Phdr) * elf_find_phdr_type( uint32_t type, ElfW(Ehdr) *ehdr) } /* Returns value if return_val==1, ptr otherwise */ -void * elf_find_dynamic( int64_t const key, ElfW(Dyn) *dynp, - ElfW(Ehdr) *ehdr, int return_val) +void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp, + ElfW(Ehdr) *ehdr, int return_val) { ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr); unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset); - for (; DT_NULL!=byteswap_to_host(dynp->d_tag); ++dynp) { + for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) { if (key == byteswap_to_host(dynp->d_tag)) { if (return_val == 1) return (void *)byteswap_to_host(dynp->d_un.d_val); else - return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr ); + return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr); } } return NULL; } -static char * elf_find_rpath(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic) +static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic) { - ElfW(Dyn) *dyns; + ElfW(Dyn) *dyns; - for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) { + for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) { if (DT_RPATH == byteswap_to_host(dyns->d_tag)) { char *strtab; strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); - return ((char*)strtab + byteswap_to_host(dyns->d_un.d_val)); + return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val)); } } return NULL; @@ -233,9 +234,9 @@ static char * elf_find_rpath(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic) int check_elf_header(ElfW(Ehdr) *const ehdr) { - if (! ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 || - ehdr->e_ident[EI_CLASS] != ELFCLASSM || - ehdr->e_ident[EI_VERSION] != EV_CURRENT) + if (!ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 || + ehdr->e_ident[EI_CLASS] != ELFCLASSM || + ehdr->e_ident[EI_VERSION] != EV_CURRENT) { return 1; } @@ -257,7 +258,7 @@ int check_elf_header(ElfW(Ehdr) *const ehdr) #endif /* Be vary lazy, and only byteswap the stuff we use */ - if (byteswap==1) { + if (byteswap == 1) { ehdr->e_type = bswap_16(ehdr->e_type); ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff); ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff); @@ -285,15 +286,14 @@ int map_cache(void) else if (cache_addr != NULL) return 0; - if (stat(LDSO_CACHE, &st) - || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) { + if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) { fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE); cache_addr = (caddr_t) - 1; /* so we won't try again */ return -1; } cache_size = st.st_size; - cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0); + cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); if (cache_addr == MAP_FAILED) { fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE); @@ -302,25 +302,21 @@ int map_cache(void) header = (header_t *) cache_addr; - if (cache_size < sizeof(header_t) || - memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) - || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN) - || cache_size < - (sizeof(header_t) + header->nlibs * sizeof(libentry_t)) - || cache_addr[cache_size - 1] != '\0') + if (cache_size < sizeof(header_t) + || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) + || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN) + || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t)) + || cache_addr[cache_size - 1] != '\0') { fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE); goto fail; } - strtabsize = cache_size - sizeof(header_t) - - header->nlibs * sizeof(libentry_t); + strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t); libent = (libentry_t *) & header[1]; for (i = 0; i < header->nlibs; i++) { - if (libent[i].sooffset >= strtabsize || - libent[i].liboffset >= strtabsize) - { + if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) { fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE); goto fail; } @@ -347,13 +343,18 @@ int unmap_cache(void) return 0; } #else -static inline void map_cache(void) { } -static inline void unmap_cache(void) { } +static inline void map_cache(void) +{ +} +static inline void unmap_cache(void) +{ +} #endif /* This function's behavior must exactly match that * in uClibc/ldso/ldso/dl-elf.c */ -static void search_for_named_library(char *name, char *result, const char *path_list) +static void search_for_named_library(char *name, char *result, + const char *path_list) { int i, count = 1; char *path, *path_n; @@ -366,17 +367,17 @@ static void search_for_named_library(char *name, char *result, const char *path_ exit(EXIT_FAILURE); } /* Eliminate all double //s */ - path_n=path; - while((path_n=strstr(path_n, "//"))) { + 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'; + 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; + for (i = strlen(path); i > 0; i--) { + if (path[i] == ':') { + path[i] = 0; count++; } } @@ -385,7 +386,7 @@ static void search_for_named_library(char *name, char *result, const char *path_ strcpy(result, path_n); strcat(result, "/"); strcat(result, name); - if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) { + if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) { free(path); return; } @@ -395,19 +396,20 @@ static void search_for_named_library(char *name, char *result, const char *path_ *result = '\0'; } -void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, struct library *lib) +void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(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) { + if (stat(lib->name, &filestat) == 0) { lib->path = strdup(lib->name); return; } - /* We need some elbow room here. Make some room...*/ + /* We need some elbow room here. Make some room... */ buf = malloc(1024); if (!buf) { fprintf(stderr, "Out of memory!\n"); @@ -430,7 +432,7 @@ void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, stru /* 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) + if (is_suid == 1) path = NULL; else path = getenv("LD_LIBRARY_PATH"); @@ -441,19 +443,19 @@ void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, stru return; } } - #ifdef __LDSO_CACHE_SUPPORT__ if (cache_addr != NULL && cache_addr != (caddr_t) - 1) { int i; header_t *header = (header_t *) cache_addr; libentry_t *libent = (libentry_t *) & header[1]; - char *strs = (char *) &libent[header->nlibs]; + char *strs = (char *)&libent[header->nlibs]; for (i = 0; i < header->nlibs; i++) { if ((libent[i].flags == LIB_ELF || - libent[i].flags == LIB_ELF_LIBC0 || - libent[i].flags == LIB_ELF_LIBC5) && - strcmp(lib->name, strs + libent[i].sooffset) == 0) { + libent[i].flags == LIB_ELF_LIBC0 || + libent[i].flags == LIB_ELF_LIBC5) && + strcmp(lib->name, strs + libent[i].sooffset) == 0) + { lib->path = strdup(strs + libent[i].liboffset); return; } @@ -461,7 +463,6 @@ void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, stru } #endif - /* Next look for libraries wherever the shared library * loader was installed -- this is usually where we * should find things... */ @@ -475,12 +476,11 @@ void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, stru /* Lastly, search the standard list of paths for the library. This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */ - path = UCLIBC_RUNTIME_PREFIX "lib:" - UCLIBC_RUNTIME_PREFIX "usr/lib" + path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib" #ifndef __LDSO_CACHE_SUPPORT__ - ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib" + ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib" #endif - ; + ; search_for_named_library(lib->name, buf, path); if (*buf != '\0') { lib->path = buf; @@ -490,10 +490,10 @@ void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, stru } } -static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char *s) +static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s) { char *tmp, *tmp1, *tmp2; - struct library *cur, *newlib=lib_list; + struct library *cur, *newlib = lib_list; if (!s || !strlen(s)) return 1; @@ -506,14 +506,13 @@ static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char } /* We add ldso elsewhere */ - if (interpreter_already_found && (tmp=strrchr(interp_name, '/')) != NULL) - { + if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) { int len = strlen(interp_dir); - if (strcmp(s, interp_name+1+len)==0) + if (strcmp(s, interp_name + 1 + len) == 0) return 1; } - for (cur = lib_list; cur; cur=cur->next) { + for (cur = lib_list; cur; cur = cur->next) { /* Check if this library is already in the list */ tmp1 = tmp2 = cur->name; while (*tmp1) { @@ -521,7 +520,7 @@ static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char tmp2 = tmp1 + 1; tmp1++; } - if(strcmp(tmp2, s)==0) { + if (strcmp(tmp2, s) == 0) { //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); return 0; } @@ -531,7 +530,7 @@ static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char newlib = malloc(sizeof(struct library)); if (!newlib) return 1; - newlib->name = malloc(strlen(s)+1); + newlib->name = malloc(strlen(s) + 1); strcpy(newlib->name, s); newlib->resolved = 0; newlib->path = NULL; @@ -544,28 +543,26 @@ static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char if (!lib_list) { lib_list = newlib; } else { - for (cur = lib_list; cur->next; cur=cur->next); /* nothing */ + for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */ cur->next = newlib; } return 0; } -static void find_needed_libraries(ElfW(Ehdr)* ehdr, - ElfW(Dyn)* dynamic, int is_setuid) +static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid) { - ElfW(Dyn) *dyns; + ElfW(Dyn) *dyns; - for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) { + for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) { if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) { char *strtab; strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); - add_library(ehdr, dynamic, is_setuid, - (char*)strtab + byteswap_to_host(dyns->d_un.d_val)); + add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val)); } } } -static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) +static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr) { ElfW(Phdr) *phdr; @@ -573,8 +570,8 @@ static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) return NULL; phdr = elf_find_phdr_type(PT_INTERP, ehdr); if (phdr) { - struct library *cur, *newlib=NULL; - char *s = (char*)ehdr + byteswap_to_host(phdr->p_offset); + struct library *cur, *newlib = NULL; + char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset); char *tmp, *tmp1; interp_name = strdup(s); @@ -592,9 +589,9 @@ static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) tmp1 = tmp + 1; tmp++; } - for (cur = lib_list; cur; cur=cur->next) { + for (cur = lib_list; cur; cur = cur->next) { /* Check if this library is already in the list */ - if(strcmp(cur->name, tmp1)==0) { + if (strcmp(cur->name, tmp1) == 0) { //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); newlib = cur; free(newlib->name); @@ -610,7 +607,7 @@ static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) newlib = malloc(sizeof(struct library)); if (!newlib) return NULL; - newlib->name = malloc(strlen(s)+1); + newlib->name = malloc(strlen(s) + 1); strcpy(newlib->name, s); newlib->path = strdup(newlib->name); newlib->resolved = 1; @@ -621,7 +618,7 @@ static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) if (!lib_list) { lib_list = newlib; } else { - for (cur = lib_list; cur->next; cur=cur->next); /* nothing */ + for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */ cur->next = newlib; } #endif @@ -635,7 +632,7 @@ static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr) /* #warning "There may be two warnings here about vfork() clobbering, ignore them" */ -int find_dependancies(char* filename) +int find_dependancies(char *filename) { int is_suid = 0; FILE *thefile; @@ -662,15 +659,14 @@ int find_dependancies(char* filename) return -1; } - if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr))) + if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr))) goto foo; if (!S_ISREG(statbuf.st_mode)) goto foo; /* mmap the file to make reading stuff from it effortless */ - ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size, - PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0); + ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0); if (ehdr == MAP_FAILED) { fclose(thefile); fprintf(stderr, "Out of memory!\n"); @@ -704,16 +700,18 @@ foo: interp = find_elf_interpreter(ehdr); #ifdef __LDSO_LDD_SUPPORT__ - if (interp && \ - (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && \ - ehdr->e_ident[EI_CLASS] == ELFCLASSM && ehdr->e_ident[EI_DATA] == ELFDATAM && \ - ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine)) + if (interp + && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) + && 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[] = { + static const char *const environment[] = { "PATH=/usr/bin:/bin:/usr/sbin:/sbin", "SHELL=/bin/sh", "LD_TRACE_LOADED_OBJECTS=1", @@ -729,7 +727,7 @@ foo: /* Wait till it returns */ waitpid(pid, &status, 0); - if (WIFEXITED(status) && WEXITSTATUS(status)==0) { + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { return 1; } @@ -742,14 +740,14 @@ foo: dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr); if (dynsec) { - dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr); + dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr); find_needed_libraries(ehdr, dynamic, is_suid); } return 0; } -int main( int argc, char** argv) +int main(int argc, char **argv) { int multi = 0; int got_em_all = 1; @@ -767,7 +765,7 @@ int main( int argc, char** argv) while (--argc > 0) { ++argv; - if(strcmp(*argv, "--")==0) { + if (strcmp(*argv, "--") == 0) { /* Ignore "--" */ continue; } @@ -790,13 +788,13 @@ int main( int argc, char** argv) map_cache(); - if (find_dependancies(filename)!=0) + 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) { + 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); @@ -810,16 +808,16 @@ int main( int argc, char** argv) /* Print the list */ got_em_all = 0; - for (cur = lib_list; cur; cur=cur->next) { + for (cur = lib_list; cur; cur = cur->next) { got_em_all = 1; printf("\t%s => %s (0x00000000)\n", cur->name, cur->path); } - if (interp_name && interpreter_already_found==1) + if (interp_name && interpreter_already_found == 1) printf("\t%s => %s (0x00000000)\n", interp_name, interp_name); else printf("\tnot a dynamic executable\n"); - for (cur = lib_list; cur; cur=cur->next) { + for (cur = lib_list; cur; cur = cur->next) { free(cur->name); cur->name = NULL; if (cur->path && cur->path != not_found) { diff --git a/utils/readsoname2.c b/utils/readsoname2.c index 029a2bbc1..6a9f00775 100644 --- a/utils/readsoname2.c +++ b/utils/readsoname2.c @@ -1,145 +1,143 @@ char *readsonameXX(char *name, FILE *infile, int expected_type, int *type) { - ElfW(Ehdr) *epnt; - ElfW(Phdr) *ppnt; - unsigned int i, j; - char *header; - ElfW(Addr) dynamic_addr = 0; - ElfW(Addr) dynamic_size = 0; - unsigned long page_size = getpagesize(); - ElfW(Addr) strtab_val = 0; - ElfW(Addr) needed_val; - ElfW(Addr) 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|PROT_WRITE, MAP_PRIVATE, 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; + ElfW(Ehdr) *epnt; + ElfW(Phdr) *ppnt; + unsigned int i, j; + char *header; + ElfW(Addr) dynamic_addr = 0; + ElfW(Addr) dynamic_size = 0; + unsigned long page_size = getpagesize(); + ElfW(Addr) strtab_val = 0; + ElfW(Addr) needed_val; + ElfW(Addr) 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 | PROT_WRITE, MAP_PRIVATE, + 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; #if __BYTE_ORDER == __LITTLE_ENDIAN - byteswap = (epnt->e_ident[5] == ELFDATA2MSB) ? 1 : 0; + byteswap = (epnt->e_ident[5] == ELFDATA2MSB) ? 1 : 0; #elif __BYTE_ORDER == __BIG_ENDIAN - byteswap = (epnt->e_ident[5] == ELFDATA2LSB) ? 1 : 0; + byteswap = (epnt->e_ident[5] == ELFDATA2LSB) ? 1 : 0; #else #error Unknown host byte order! #endif - /* Be very lazy, and only byteswap the stuff we use */ - if (byteswap==1) { - epnt->e_phoff=bswap_32(epnt->e_phoff); - epnt->e_phnum=bswap_16(epnt->e_phnum); - } - - 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++) - { - /* Be very lazy, and only byteswap the stuff we use */ - if (byteswap==1) { - ppnt->p_type=bswap_32(ppnt->p_type); - ppnt->p_vaddr=bswap_32(ppnt->p_vaddr); - ppnt->p_offset=bswap_32(ppnt->p_offset); - ppnt->p_filesz=bswap_32(ppnt->p_filesz); - } - - if (loadaddr == (ElfW(Addr))-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; - - if (byteswap==1) { - dpnt->d_tag=bswap_32(dpnt->d_tag); - dpnt->d_un.d_val=bswap_32(dpnt->d_un.d_val); - } - - while (dpnt->d_tag != DT_NULL) - { - if (dpnt->d_tag == DT_STRTAB) - strtab_val = dpnt->d_un.d_val; - dpnt++; - if (byteswap==1) { - dpnt->d_tag=bswap_32(dpnt->d_tag); - dpnt->d_un.d_val=bswap_32(dpnt->d_un.d_val); - } - }; - - 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 || - needed_val + strtab_val < st.st_size - loadaddr) - { - 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; - } + /* Be very lazy, and only byteswap the stuff we use */ + if (byteswap == 1) { + epnt->e_phoff = bswap_32(epnt->e_phoff); + epnt->e_phnum = bswap_16(epnt->e_phnum); + } + + 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++) { + /* Be very lazy, and only byteswap the stuff we use */ + if (byteswap == 1) { + ppnt->p_type = bswap_32(ppnt->p_type); + ppnt->p_vaddr = bswap_32(ppnt->p_vaddr); + ppnt->p_offset = bswap_32(ppnt->p_offset); + ppnt->p_filesz = bswap_32(ppnt->p_filesz); + } + + if (loadaddr == (ElfW(Addr)) - 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; + + if (byteswap == 1) { + dpnt->d_tag = bswap_32(dpnt->d_tag); + dpnt->d_un.d_val = bswap_32(dpnt->d_un.d_val); + } + + while (dpnt->d_tag != DT_NULL) { + if (dpnt->d_tag == DT_STRTAB) + strtab_val = dpnt->d_un.d_val; + dpnt++; + if (byteswap == 1) { + dpnt->d_tag = bswap_32(dpnt->d_tag); + dpnt->d_un.d_val = bswap_32(dpnt->d_un.d_val); + } + }; + + 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 || + needed_val + strtab_val < st.st_size - loadaddr) { + 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); } - } - } - 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; + + skip: + munmap(header, st.st_size); + + return soname; } |