/[gentoo-projects]/pax-utils/scanelf.c
Gentoo

Diff of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.211 Revision 1.212
1/* 1/*
2 * Copyright 2003-2007 Gentoo Foundation 2 * Copyright 2003-2007 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2 3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.211 2009/03/15 09:01:48 vapier Exp $ 4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.212 2009/03/15 09:13:20 vapier Exp $
5 * 5 *
6 * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org> 6 * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org> 7 * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org>
8 */ 8 */
9 9
10static const char *rcsid = "$Id: scanelf.c,v 1.211 2009/03/15 09:01:48 vapier Exp $"; 10static const char *rcsid = "$Id: scanelf.c,v 1.212 2009/03/15 09:13:20 vapier Exp $";
11const char * const argv0 = "scanelf"; 11const char * const argv0 = "scanelf";
12 12
13#include "paxinc.h" 13#include "paxinc.h"
14 14
15#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+') 15#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+')
70static char **qa_execstack = NULL; 70static char **qa_execstack = NULL;
71static char **qa_wx_load = NULL; 71static char **qa_wx_load = NULL;
72 72
73static int match_bits = 0; 73static int match_bits = 0;
74static unsigned int match_perms = 0; 74static unsigned int match_perms = 0;
75static caddr_t ldcache = 0; 75static void *ldcache = NULL;
76static size_t ldcache_size = 0; 76static size_t ldcache_size = 0;
77static unsigned long setpax = 0UL; 77static unsigned long setpax = 0UL;
78 78
79static int has_objdump = 0; 79static int has_objdump = 0;
80 80
692 692
693#if defined(__GLIBC__) || defined(__UCLIBC__) 693#if defined(__GLIBC__) || defined(__UCLIBC__)
694 694
695static char *lookup_cache_lib(elfobj *elf, char *fname) 695static char *lookup_cache_lib(elfobj *elf, char *fname)
696{ 696{
697 int fd = 0; 697 int fd;
698 char *strs; 698 char *strs;
699 static char buf[__PAX_UTILS_PATH_MAX] = ""; 699 static char buf[__PAX_UTILS_PATH_MAX] = "";
700 const char *cachefile = "/etc/ld.so.cache"; 700 const char cachefile[] = "/etc/ld.so.cache";
701 struct stat st; 701 struct stat st;
702 702
703 typedef struct { 703 typedef struct {
704 char magic[LDSO_CACHE_MAGIC_LEN]; 704 char magic[LDSO_CACHE_MAGIC_LEN];
705 char version[LDSO_CACHE_VER_LEN]; 705 char version[LDSO_CACHE_VER_LEN];
715 libentry_t *libent; 715 libentry_t *libent;
716 716
717 if (fname == NULL) 717 if (fname == NULL)
718 return NULL; 718 return NULL;
719 719
720 if (ldcache == 0) { 720 if (ldcache == NULL) {
721 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY)) == -1) 721 if (stat(cachefile, &st))
722 return NULL;
723
724 fd = open(cachefile, O_RDONLY);
725 if (fd == -1)
722 return NULL; 726 return NULL;
723 727
724 /* cache these values so we only map/unmap the cache file once */ 728 /* cache these values so we only map/unmap the cache file once */
725 ldcache_size = st.st_size; 729 ldcache_size = st.st_size;
726 ldcache = mmap(0, ldcache_size, PROT_READ, MAP_SHARED, fd, 0); 730 header = ldcache = mmap(0, ldcache_size, PROT_READ, MAP_SHARED, fd, 0);
727
728 close(fd); 731 close(fd);
729 732
730 if (ldcache == MAP_FAILED) { 733 if (ldcache == MAP_FAILED) {
731 ldcache = 0; 734 ldcache = NULL;
732 return NULL; 735 return NULL;
733 } 736 }
734 737
735 if (memcmp(((header_t *) ldcache)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)) 738 if (memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) ||
739 memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
740 {
741 munmap(ldcache, ldcache_size);
742 ldcache = NULL;
736 return NULL; 743 return NULL;
737 if (memcmp (((header_t *) ldcache)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
738 return NULL;
739 } 744 }
745 } else
746 header = ldcache;
740 747
741 header = (header_t *) ldcache;
742 libent = (libentry_t *) (ldcache + sizeof(header_t)); 748 libent = ldcache + sizeof(header_t);
743 strs = (char *) &libent[header->nlibs]; 749 strs = (char *) &libent[header->nlibs];
744 750
745 for (fd = 0; fd < header->nlibs; fd++) { 751 for (fd = 0; fd < header->nlibs; ++fd) {
746 /* this should be more fine grained, but for now we assume that 752 /* This should be more fine grained, but for now we assume that
747 * diff arches will not be cached together. and we ignore the 753 * diff arches will not be cached together, and we ignore the
748 * the different multilib mips cases. */ 754 * the different multilib mips cases.
755 */
749 if (elf->elf_class == ELFCLASS64 && !(libent[fd].flags & FLAG_REQUIRED_MASK)) 756 if (elf->elf_class == ELFCLASS64 && !(libent[fd].flags & FLAG_REQUIRED_MASK))
750 continue; 757 continue;
751 if (elf->elf_class == ELFCLASS32 && (libent[fd].flags & FLAG_REQUIRED_MASK)) 758 if (elf->elf_class == ELFCLASS32 && (libent[fd].flags & FLAG_REQUIRED_MASK))
752 continue; 759 continue;
753 760
754 if (strcmp(fname, strs + libent[fd].sooffset) != 0) 761 if (strcmp(fname, strs + libent[fd].sooffset) != 0)
755 continue; 762 continue;
763
764 /* Return first hit because that is how the ldso rolls */
756 strncpy(buf, strs + libent[fd].liboffset, sizeof(buf)); 765 strncpy(buf, strs + libent[fd].liboffset, sizeof(buf));
766 break;
757 } 767 }
768
758 return buf; 769 return buf;
759} 770}
771
760#elif defined(__NetBSD__) 772#elif defined(__NetBSD__)
761static char *lookup_cache_lib(elfobj *elf, char *fname) 773static char *lookup_cache_lib(elfobj *elf, char *fname)
762{ 774{
763 static char buf[__PAX_UTILS_PATH_MAX] = ""; 775 static char buf[__PAX_UTILS_PATH_MAX] = "";
764 static struct stat st; 776 static struct stat st;

Legend:
Removed from v.1.211  
changed lines
  Added in v.1.212

  ViewVC Help
Powered by ViewVC 1.1.20