/[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.206 Revision 1.220
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/Attic/scanelf.c,v 1.206 2008/12/30 13:34:46 vapier Exp $ 4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/Attic/scanelf.c,v 1.220 2010/12/08 00:54:40 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.206 2008/12/30 13:34:46 vapier Exp $"; 10static const char *rcsid = "$Id: scanelf.c,v 1.220 2010/12/08 00:54:40 vapier Exp $";
11const char * const argv0 = "scanelf"; 11const char 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 == '+')
16 16
25static void scanelf_envpath(void); 25static void scanelf_envpath(void);
26static void usage(int status); 26static void usage(int status);
27static char **get_split_env(const char *envvar); 27static char **get_split_env(const char *envvar);
28static void parseenv(void); 28static void parseenv(void);
29static int parseargs(int argc, char *argv[]); 29static int parseargs(int argc, char *argv[]);
30static int rematch(const char *regex, const char *match, int cflags);
31 30
32/* variables to control behavior */ 31/* variables to control behavior */
33static char match_etypes[126] = ""; 32static char match_etypes[126] = "";
34static char *ldpaths[256]; 33static char *ldpaths[256];
35static char scan_ldpath = 0; 34static char scan_ldpath = 0;
70static char **qa_execstack = NULL; 69static char **qa_execstack = NULL;
71static char **qa_wx_load = NULL; 70static char **qa_wx_load = NULL;
72 71
73static int match_bits = 0; 72static int match_bits = 0;
74static unsigned int match_perms = 0; 73static unsigned int match_perms = 0;
75static caddr_t ldcache = 0; 74static void *ldcache = NULL;
76static size_t ldcache_size = 0; 75static size_t ldcache_size = 0;
77static unsigned long setpax = 0UL; 76static unsigned long setpax = 0UL;
78 77
79static int has_objdump = 0; 78static int has_objdump = 0;
80 79
81/* find the path to a file by name */ 80/* find the path to a file by name */
82static char *which(const char *fname) 81static char *which(const char *fname)
83{ 82{
84 static char fullpath[BUFSIZ]; 83 static char fullpath[__PAX_UTILS_PATH_MAX];
85 char *path, *p; 84 char *path, *p;
86 85
87 path = getenv("PATH"); 86 path = getenv("PATH");
88 if (!path) 87 if (!path)
89 return NULL; 88 return NULL;
128 127
129/* sub-funcs for scanelf_file() */ 128/* sub-funcs for scanelf_file() */
130static void scanelf_file_get_symtabs(elfobj *elf, void **sym, void **tab) 129static void scanelf_file_get_symtabs(elfobj *elf, void **sym, void **tab)
131{ 130{
132 /* find the best SHT_DYNSYM and SHT_STRTAB sections */ 131 /* find the best SHT_DYNSYM and SHT_STRTAB sections */
132
133 /* debug sections */
134 void *symtab = elf_findsecbyname(elf, ".symtab");
135 void *strtab = elf_findsecbyname(elf, ".strtab");
136 /* runtime sections */
137 void *dynsym = elf_findsecbyname(elf, ".dynsym");
138 void *dynstr = elf_findsecbyname(elf, ".dynstr");
139
133#define GET_SYMTABS(B) \ 140#define GET_SYMTABS(B) \
134 if (elf->elf_class == ELFCLASS ## B) { \ 141 if (elf->elf_class == ELFCLASS ## B) { \
135 Elf ## B ## _Shdr *symtab, *strtab, *dynsym, *dynstr; \
136 /* debug sections */ \
137 symtab = SHDR ## B (elf_findsecbyname(elf, ".symtab")); \
138 strtab = SHDR ## B (elf_findsecbyname(elf, ".strtab")); \
139 /* runtime sections */ \
140 dynsym = SHDR ## B (elf_findsecbyname(elf, ".dynsym")); \
141 dynstr = SHDR ## B (elf_findsecbyname(elf, ".dynstr")); \
142 if (symtab && dynsym) { \ 142 if (symtab && dynsym) { \
143 Elf ## B ## _Shdr *esymtab = symtab; \
144 Elf ## B ## _Shdr *edynsym = dynsym; \
143 *sym = (void*)((EGET(symtab->sh_size) > EGET(dynsym->sh_size)) ? symtab : dynsym); \ 145 *sym = (EGET(esymtab->sh_size) > EGET(edynsym->sh_size)) ? symtab : dynsym; \
144 } else { \ 146 } else \
145 *sym = (void*)(symtab ? symtab : dynsym); \ 147 *sym = symtab ? symtab : dynsym; \
146 } \
147 if (strtab && dynstr) { \ 148 if (strtab && dynstr) { \
149 Elf ## B ## _Shdr *estrtab = strtab; \
150 Elf ## B ## _Shdr *edynstr = dynstr; \
148 *tab = (void*)((EGET(strtab->sh_size) > EGET(dynstr->sh_size)) ? strtab : dynstr); \ 151 *tab = (EGET(estrtab->sh_size) > EGET(edynstr->sh_size)) ? strtab : dynstr; \
149 } else { \ 152 } else \
150 *tab = (void*)(strtab ? strtab : dynstr); \ 153 *tab = strtab ? strtab : dynstr; \
151 } \
152 } 154 }
153 GET_SYMTABS(32) 155 GET_SYMTABS(32)
154 GET_SYMTABS(64) 156 GET_SYMTABS(64)
155} 157}
156 158
280 } else if (elf->shdr != NULL) { \ 282 } else if (elf->shdr != NULL) { \
281 /* no program headers which means this is prob an object file */ \ 283 /* no program headers which means this is prob an object file */ \
282 Elf ## B ## _Shdr *shdr = SHDR ## B (elf->shdr); \ 284 Elf ## B ## _Shdr *shdr = SHDR ## B (elf->shdr); \
283 Elf ## B ## _Shdr *strtbl = shdr + EGET(ehdr->e_shstrndx); \ 285 Elf ## B ## _Shdr *strtbl = shdr + EGET(ehdr->e_shstrndx); \
284 char *str; \ 286 char *str; \
285 if ((void*)strtbl > (void*)elf->data_end) \ 287 if ((void*)strtbl > elf->data_end) \
286 goto skip_this_shdr##B; \ 288 goto skip_this_shdr##B; \
287 check_flags = SHF_WRITE|SHF_EXECINSTR; \ 289 check_flags = SHF_WRITE|SHF_EXECINSTR; \
288 for (i = 0; i < EGET(ehdr->e_shnum); ++i) { \ 290 for (i = 0; i < EGET(ehdr->e_shnum); ++i) { \
289 if (EGET(shdr[i].sh_type) != SHT_PROGBITS) continue; \ 291 if (EGET(shdr[i].sh_type) != SHT_PROGBITS) continue; \
290 offset = EGET(strtbl->sh_offset) + EGET(shdr[i].sh_name); \ 292 offset = EGET(strtbl->sh_offset) + EGET(shdr[i].sh_name); \
345 Elf ## B ## _Off offset; \ 347 Elf ## B ## _Off offset; \
346 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ 348 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
347 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \ 349 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
348 offset = EGET(phdr[i].p_offset); \ 350 offset = EGET(phdr[i].p_offset); \
349 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \ 351 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
350 dyn = DYN ## B (elf->data + offset); \ 352 dyn = DYN ## B (elf->vdata + offset); \
351 while (EGET(dyn->d_tag) != DT_NULL) { \ 353 while (EGET(dyn->d_tag) != DT_NULL) { \
352 if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \ 354 if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
353 *found_textrel = 1; \ 355 *found_textrel = 1; \
354 /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \ 356 /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
355 return (be_wewy_wewy_quiet ? NULL : ret); \ 357 return (be_wewy_wewy_quiet ? NULL : ret); \
400 Elf ## B ## _Rela *rela; \ 402 Elf ## B ## _Rela *rela; \
401 /* search the section headers for relocations */ \ 403 /* search the section headers for relocations */ \
402 for (s = 0; s < EGET(ehdr->e_shnum); ++s) { \ 404 for (s = 0; s < EGET(ehdr->e_shnum); ++s) { \
403 uint32_t sh_type = EGET(shdr[s].sh_type); \ 405 uint32_t sh_type = EGET(shdr[s].sh_type); \
404 if (sh_type == SHT_REL) { \ 406 if (sh_type == SHT_REL) { \
405 rel = REL ## B (elf->data + EGET(shdr[s].sh_offset)); \ 407 rel = REL ## B (elf->vdata + EGET(shdr[s].sh_offset)); \
406 rela = NULL; \ 408 rela = NULL; \
407 rmax = EGET(shdr[s].sh_size) / sizeof(*rel); \ 409 rmax = EGET(shdr[s].sh_size) / sizeof(*rel); \
408 } else if (sh_type == SHT_RELA) { \ 410 } else if (sh_type == SHT_RELA) { \
409 rel = NULL; \ 411 rel = NULL; \
410 rela = RELA ## B (elf->data + EGET(shdr[s].sh_offset)); \ 412 rela = RELA ## B (elf->vdata + EGET(shdr[s].sh_offset)); \
411 rmax = EGET(shdr[s].sh_size) / sizeof(*rela); \ 413 rmax = EGET(shdr[s].sh_size) / sizeof(*rela); \
412 } else \ 414 } else \
413 continue; \ 415 continue; \
414 /* now see if any of the relocs are in the .text */ \ 416 /* now see if any of the relocs are in the .text */ \
415 for (r = 0; r < rmax; ++r) { \ 417 for (r = 0; r < rmax; ++r) { \
430 if (r_offset < vaddr || r_offset >= vaddr + memsz) { \ 432 if (r_offset < vaddr || r_offset >= vaddr + memsz) { \
431 if (be_verbose <= 2) continue; \ 433 if (be_verbose <= 2) continue; \
432 } else \ 434 } else \
433 *found_textrels = 1; \ 435 *found_textrels = 1; \
434 /* locate this relocation symbol name */ \ 436 /* locate this relocation symbol name */ \
435 sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \ 437 sym = SYM ## B (elf->vdata + EGET(symtab->sh_offset)); \
436 if ((void*)sym > (void*)elf->data_end) { \ 438 if ((void*)sym > elf->data_end) { \
437 warn("%s: corrupt ELF symbol", elf->filename); \ 439 warn("%s: corrupt ELF symbol", elf->filename); \
438 continue; \ 440 continue; \
439 } \ 441 } \
440 sym_max = ELF ## B ## _R_SYM(r_info); \ 442 sym_max = ELF ## B ## _R_SYM(r_info); \
441 if (sym_max * EGET(symtab->sh_entsize) < symtab->sh_size) \ 443 if (sym_max * EGET(symtab->sh_entsize) < symtab->sh_size) \
444 sym = NULL; \ 446 sym = NULL; \
445 sym_max = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \ 447 sym_max = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
446 /* show the raw details about this reloc */ \ 448 /* show the raw details about this reloc */ \
447 printf(" %s: ", elf->base_filename); \ 449 printf(" %s: ", elf->base_filename); \
448 if (sym && sym->st_name) \ 450 if (sym && sym->st_name) \
449 printf("%s", (char*)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name))); \ 451 printf("%s", elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
450 else \ 452 else \
451 printf("(memory/data?)"); \ 453 printf("(memory/data?)"); \
452 printf(" [0x%lX]", (unsigned long)r_offset); \ 454 printf(" [0x%lX]", (unsigned long)r_offset); \
453 /* now try to find the closest symbol that this rel is probably in */ \ 455 /* now try to find the closest symbol that this rel is probably in */ \
454 sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \ 456 sym = SYM ## B (elf->vdata + EGET(symtab->sh_offset)); \
455 func = NULL; \ 457 func = NULL; \
456 offset_tmp = 0; \ 458 offset_tmp = 0; \
457 while (sym_max--) { \ 459 while (sym_max--) { \
458 if (EGET(sym->st_value) < r_offset && EGET(sym->st_value) > offset_tmp) { \ 460 if (EGET(sym->st_value) < r_offset && EGET(sym->st_value) > offset_tmp) { \
459 func = sym; \ 461 func = sym; \
473 printf(" [0x%lX]\n", (unsigned long)offset_tmp); \ 475 printf(" [0x%lX]\n", (unsigned long)offset_tmp); \
474 if (be_verbose && has_objdump) { \ 476 if (be_verbose && has_objdump) { \
475 Elf ## B ## _Addr end_addr = offset_tmp + EGET(func->st_size); \ 477 Elf ## B ## _Addr end_addr = offset_tmp + EGET(func->st_size); \
476 char *sysbuf; \ 478 char *sysbuf; \
477 size_t syslen; \ 479 size_t syslen; \
480 int sysret; \
478 const char sysfmt[] = "objdump -r -R -d -w -l --start-address=0x%lX --stop-address=0x%lX %s | grep --color -i -C 3 '.*[[:space:]]%lX:[[:space:]]*R_.*'\n"; \ 481 const char sysfmt[] = "objdump -r -R -d -w -l --start-address=0x%lX --stop-address=0x%lX %s | grep --color -i -C 3 '.*[[:space:]]%lX:[[:space:]]*R_.*'\n"; \
479 syslen = sizeof(sysfmt) + strlen(elf->filename) + 3 * sizeof(unsigned long) + 1; \ 482 syslen = sizeof(sysfmt) + strlen(elf->filename) + 3 * sizeof(unsigned long) + 1; \
480 sysbuf = xmalloc(syslen); \ 483 sysbuf = xmalloc(syslen); \
481 if (end_addr < r_offset) \ 484 if (end_addr < r_offset) \
482 /* not uncommon when things are optimized out */ \ 485 /* not uncommon when things are optimized out */ \
485 (unsigned long)offset_tmp, \ 488 (unsigned long)offset_tmp, \
486 (unsigned long)end_addr, \ 489 (unsigned long)end_addr, \
487 elf->filename, \ 490 elf->filename, \
488 (unsigned long)r_offset); \ 491 (unsigned long)r_offset); \
489 fflush(stdout); \ 492 fflush(stdout); \
490 system(sysbuf); \ 493 sysret = system(sysbuf); \
491 fflush(stdout); \ 494 fflush(stdout); \
492 free(sysbuf); \ 495 free(sysbuf); \
493 } \ 496 } \
494 } \ 497 } \
495 } } 498 } }
551 /* Just scan dynamic headers */ \ 554 /* Just scan dynamic headers */ \
552 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \ 555 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
553 offset = EGET(phdr[i].p_offset); \ 556 offset = EGET(phdr[i].p_offset); \
554 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \ 557 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
555 /* Just scan dynamic RPATH/RUNPATH headers */ \ 558 /* Just scan dynamic RPATH/RUNPATH headers */ \
556 dyn = DYN ## B (elf->data + offset); \ 559 dyn = DYN ## B (elf->vdata + offset); \
557 while ((word=EGET(dyn->d_tag)) != DT_NULL) { \ 560 while ((word=EGET(dyn->d_tag)) != DT_NULL) { \
558 if (word == DT_RPATH) { \ 561 if (word == DT_RPATH) { \
559 r = &rpath; \ 562 r = &rpath; \
560 } else if (word == DT_RUNPATH) { \ 563 } else if (word == DT_RUNPATH) { \
561 r = &runpath; \ 564 r = &runpath; \
565 } \ 568 } \
566 /* Verify the memory is somewhat sane */ \ 569 /* Verify the memory is somewhat sane */ \
567 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \ 570 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
568 if (offset < (Elf ## B ## _Off)elf->len) { \ 571 if (offset < (Elf ## B ## _Off)elf->len) { \
569 if (*r) warn("ELF has multiple %s's !?", get_elfdtype(word)); \ 572 if (*r) warn("ELF has multiple %s's !?", get_elfdtype(word)); \
570 *r = (char*)(elf->data + offset); \ 573 *r = elf->data + offset; \
571 /* cache the length in case we need to nuke this section later on */ \ 574 /* cache the length in case we need to nuke this section later on */ \
572 if (fix_elf) \ 575 if (fix_elf) \
573 offset = strlen(*r); \ 576 offset = strlen(*r); \
574 /* If quiet, don't output paths in ld.so.conf */ \ 577 /* If quiet, don't output paths in ld.so.conf */ \
575 if (be_quiet) { \ 578 if (be_quiet) { \
691 694
692#if defined(__GLIBC__) || defined(__UCLIBC__) 695#if defined(__GLIBC__) || defined(__UCLIBC__)
693 696
694static char *lookup_cache_lib(elfobj *elf, char *fname) 697static char *lookup_cache_lib(elfobj *elf, char *fname)
695{ 698{
696 int fd = 0; 699 int fd;
697 char *strs; 700 char *strs;
698 static char buf[__PAX_UTILS_PATH_MAX] = ""; 701 static char buf[__PAX_UTILS_PATH_MAX] = "";
699 const char *cachefile = "/etc/ld.so.cache"; 702 const char cachefile[] = "/etc/ld.so.cache";
700 struct stat st; 703 struct stat st;
701 704
702 typedef struct { 705 typedef struct {
703 char magic[LDSO_CACHE_MAGIC_LEN]; 706 char magic[LDSO_CACHE_MAGIC_LEN];
704 char version[LDSO_CACHE_VER_LEN]; 707 char version[LDSO_CACHE_VER_LEN];
714 libentry_t *libent; 717 libentry_t *libent;
715 718
716 if (fname == NULL) 719 if (fname == NULL)
717 return NULL; 720 return NULL;
718 721
719 if (ldcache == 0) { 722 if (ldcache == NULL) {
720 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY)) == -1) 723 if (stat(cachefile, &st))
724 return NULL;
725
726 fd = open(cachefile, O_RDONLY);
727 if (fd == -1)
721 return NULL; 728 return NULL;
722 729
723 /* cache these values so we only map/unmap the cache file once */ 730 /* cache these values so we only map/unmap the cache file once */
724 ldcache_size = st.st_size; 731 ldcache_size = st.st_size;
725 ldcache = mmap(0, ldcache_size, PROT_READ, MAP_SHARED, fd, 0); 732 header = ldcache = mmap(0, ldcache_size, PROT_READ, MAP_SHARED, fd, 0);
726
727 close(fd); 733 close(fd);
728 734
729 if (ldcache == MAP_FAILED) { 735 if (ldcache == MAP_FAILED) {
730 ldcache = 0; 736 ldcache = NULL;
731 return NULL; 737 return NULL;
732 } 738 }
733 739
734 if (memcmp(((header_t *) ldcache)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)) 740 if (memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) ||
741 memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
742 {
743 munmap(ldcache, ldcache_size);
744 ldcache = NULL;
735 return NULL; 745 return NULL;
736 if (memcmp (((header_t *) ldcache)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
737 return NULL;
738 } 746 }
747 } else
748 header = ldcache;
739 749
740 header = (header_t *) ldcache;
741 libent = (libentry_t *) (ldcache + sizeof(header_t)); 750 libent = ldcache + sizeof(header_t);
742 strs = (char *) &libent[header->nlibs]; 751 strs = (char *) &libent[header->nlibs];
743 752
744 for (fd = 0; fd < header->nlibs; fd++) { 753 for (fd = 0; fd < header->nlibs; ++fd) {
745 /* this should be more fine grained, but for now we assume that 754 /* This should be more fine grained, but for now we assume that
746 * diff arches will not be cached together. and we ignore the 755 * diff arches will not be cached together, and we ignore the
747 * the different multilib mips cases. */ 756 * the different multilib mips cases.
757 */
748 if (elf->elf_class == ELFCLASS64 && !(libent[fd].flags & FLAG_REQUIRED_MASK)) 758 if (elf->elf_class == ELFCLASS64 && !(libent[fd].flags & FLAG_REQUIRED_MASK))
749 continue; 759 continue;
750 if (elf->elf_class == ELFCLASS32 && (libent[fd].flags & FLAG_REQUIRED_MASK)) 760 if (elf->elf_class == ELFCLASS32 && (libent[fd].flags & FLAG_REQUIRED_MASK))
751 continue; 761 continue;
752 762
753 if (strcmp(fname, strs + libent[fd].sooffset) != 0) 763 if (strcmp(fname, strs + libent[fd].sooffset) != 0)
754 continue; 764 continue;
765
766 /* Return first hit because that is how the ldso rolls */
755 strncpy(buf, strs + libent[fd].liboffset, sizeof(buf)); 767 strncpy(buf, strs + libent[fd].liboffset, sizeof(buf));
768 break;
756 } 769 }
770
757 return buf; 771 return buf;
758} 772}
773
759#elif defined(__NetBSD__) 774#elif defined(__NetBSD__)
760static char *lookup_cache_lib(elfobj *elf, char *fname) 775static char *lookup_cache_lib(elfobj *elf, char *fname)
761{ 776{
762 static char buf[__PAX_UTILS_PATH_MAX] = ""; 777 static char buf[__PAX_UTILS_PATH_MAX] = "";
763 static struct stat st; 778 static struct stat st;
812 Elf ## B ## _Off offset; \ 827 Elf ## B ## _Off offset; \
813 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ 828 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
814 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \ 829 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
815 offset = EGET(phdr[i].p_offset); \ 830 offset = EGET(phdr[i].p_offset); \
816 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \ 831 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
817 dyn = DYN ## B (elf->data + offset); \ 832 dyn = DYN ## B (elf->vdata + offset); \
818 while (EGET(dyn->d_tag) != DT_NULL) { \ 833 while (EGET(dyn->d_tag) != DT_NULL) { \
819 if (EGET(dyn->d_tag) == DT_NEEDED) { \ 834 if (EGET(dyn->d_tag) == DT_NEEDED) { \
820 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \ 835 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
821 if (offset >= (Elf ## B ## _Off)elf->len) { \ 836 if (offset >= (Elf ## B ## _Off)elf->len) { \
822 ++dyn; \ 837 ++dyn; \
823 continue; \ 838 continue; \
824 } \ 839 } \
825 needed = (char*)(elf->data + offset); \ 840 needed = elf->data + offset; \
826 if (op == 0) { \ 841 if (op == 0) { \
827 if (!be_wewy_wewy_quiet) { \ 842 if (!be_wewy_wewy_quiet) { \
828 if (*found_needed) xchrcat(ret, ',', ret_len); \ 843 if (*found_needed) xchrcat(ret, ',', ret_len); \
829 if (use_ldcache) \ 844 if (use_ldcache) \
830 if ((p = lookup_cache_lib(elf, needed)) != NULL) \ 845 if ((p = lookup_cache_lib(elf, needed)) != NULL) \
888 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ 903 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
889 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \ 904 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
890 dynamic = 1; \ 905 dynamic = 1; \
891 offset = EGET(phdr[i].p_offset); \ 906 offset = EGET(phdr[i].p_offset); \
892 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \ 907 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
893 dyn = DYN ## B (elf->data + offset); \ 908 dyn = DYN ## B (elf->vdata + offset); \
894 while (EGET(dyn->d_tag) != DT_NULL) { \ 909 while (EGET(dyn->d_tag) != DT_NULL) { \
895 if (EGET(dyn->d_tag) == DT_BIND_NOW || \ 910 if (EGET(dyn->d_tag) == DT_BIND_NOW || \
896 (EGET(dyn->d_tag) == DT_FLAGS && EGET(dyn->d_un.d_val) & DF_BIND_NOW)) \ 911 (EGET(dyn->d_tag) == DT_FLAGS && EGET(dyn->d_un.d_val) & DF_BIND_NOW)) \
897 { \ 912 { \
898 if (be_quiet) return NULL; \ 913 if (be_quiet) return NULL; \
911 /* don't output anything if quiet mode and the ELF is static or not setuid */ 926 /* don't output anything if quiet mode and the ELF is static or not setuid */
912 if (be_quiet && (!dynamic || (!fstat(elf->fd, &s) && !(s.st_mode & (S_ISUID|S_ISGID))))) { 927 if (be_quiet && (!dynamic || (!fstat(elf->fd, &s) && !(s.st_mode & (S_ISUID|S_ISGID))))) {
913 return NULL; 928 return NULL;
914 } else { 929 } else {
915 *found_bind = 1; 930 *found_bind = 1;
916 return (char *) (dynamic ? "LAZY" : "STATIC"); 931 return (char *)(dynamic ? "LAZY" : "STATIC");
917 } 932 }
918} 933}
919static char *scanelf_file_soname(elfobj *elf, char *found_soname) 934static char *scanelf_file_soname(elfobj *elf, char *found_soname)
920{ 935{
921 unsigned long i; 936 unsigned long i;
939 return NULL; \ 954 return NULL; \
940 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \ 955 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
941 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \ 956 if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
942 offset = EGET(phdr[i].p_offset); \ 957 offset = EGET(phdr[i].p_offset); \
943 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \ 958 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
944 dyn = DYN ## B (elf->data + offset); \ 959 dyn = DYN ## B (elf->vdata + offset); \
945 while (EGET(dyn->d_tag) != DT_NULL) { \ 960 while (EGET(dyn->d_tag) != DT_NULL) { \
946 if (EGET(dyn->d_tag) == DT_SONAME) { \ 961 if (EGET(dyn->d_tag) == DT_SONAME) { \
947 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \ 962 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
948 if (offset >= (Elf ## B ## _Off)elf->len) { \ 963 if (offset >= (Elf ## B ## _Off)elf->len) { \
949 ++dyn; \ 964 ++dyn; \
950 continue; \ 965 continue; \
951 } \ 966 } \
952 soname = (char*)(elf->data + offset); \ 967 soname = elf->data + offset; \
953 *found_soname = 1; \ 968 *found_soname = 1; \
954 return (be_wewy_wewy_quiet ? NULL : soname); \ 969 return (be_wewy_wewy_quiet ? NULL : soname); \
955 } \ 970 } \
956 ++dyn; \ 971 ++dyn; \
957 } \ 972 } \
961 } 976 }
962 977
963 return NULL; 978 return NULL;
964} 979}
965 980
966static int scanelf_match_symname(const char *symname, const char *tomatch) { 981/*
967 /* We do things differently when checking with regexp */ 982 * We support the symbol form:
968 if (g_match) { 983 * [%[modifiers]%][[+-]<symbol name>][,[.....]]
969 return rematch(symname, tomatch, REG_EXTENDED) == 0; 984 * If the symbol name is empty, then all symbols are matched.
985 * If the symbol name is a glob ("*"), then all symbols are dumped (debug).
986 * Do not rely on this output format at all.
987 * Otherwise the symbol name is used to search (either regex or string compare).
988 * If the first char of the symbol name is a plus ("+"), then only match
989 * defined symbols. If it's a minus ("-"), only match undefined symbols.
990 * Putting modifiers in between the percent signs allows for more in depth
991 * filters. There are groups of modifiers. If you don't specify a member
992 * of a group, then all types in that group are matched. The current
993 * groups and their types are:
994 * STT group: STT_NOTYPE:n STT_OBJECT:o STT_FUNC:f SST_FILE:F
995 * STB group: STB_LOCAL:l STB_GLOBAL:g STB_WEAK:w
996 * SHN group: SHN_UNDEF:u SHN_ABS:a SHN_COMMON:c {defined}:d
997 * The "defined" value in the SHN group does not correspond to a SHN_xxx define.
998 * You can search for multiple symbols at once by seperating with a comma (",").
999 *
1000 * Some examples:
1001 * ELFs with a weak function "foo":
1002 * scanelf -s %wf%foo <ELFs>
1003 * ELFs that define the symbol "main":
1004 * scanelf -s +main <ELFs>
1005 * scanelf -s %d%main <ELFs>
1006 * ELFs that refer to the undefined symbol "brk":
1007 * scanelf -s -brk <ELFs>
1008 * scanelf -s %u%brk <ELFs>
1009 * All global defined objects in an ELF:
1010 * scanelf -s %ogd% <ELF>
1011 */
1012static void
1013scanelf_match_symname(elfobj *elf, char *found_sym, char **ret, size_t *ret_len, const char *symname,
1014 unsigned int stt, unsigned int stb, unsigned int shn, unsigned long size)
1015{
1016 char *this_sym, *next_sym, saved = saved;
1017
1018 /* allow the user to specify a comma delimited list of symbols to search for */
1019 next_sym = NULL;
1020 do {
1021 bool inc_notype, inc_object, inc_func, inc_file,
1022 inc_local, inc_global, inc_weak,
1023 inc_def, inc_undef, inc_abs, inc_common;
1024
1025 if (next_sym) {
1026 next_sym[-1] = saved;
1027 this_sym = next_sym;
1028 } else
1029 this_sym = find_sym;
1030 if ((next_sym = strchr(this_sym, ','))) {
1031 /* make parsing easier by killing the comma temporarily */
1032 saved = *next_sym;
1033 *next_sym = '\0';
1034 next_sym += 1;
1035 }
1036
1037 /* symbol selection! */
1038 inc_notype = inc_object = inc_func = inc_file = \
1039 inc_local = inc_global = inc_weak = \
1040 inc_def = inc_undef = inc_abs = inc_common = \
1041 (*this_sym != '%');
1042
1043 /* parse the contents of %...% */
1044 if (!inc_notype) {
1045 while (*(this_sym++)) {
1046 if (*this_sym == '%') {
1047 ++this_sym;
1048 break;
1049 }
1050 switch (*this_sym) {
1051 case 'n': inc_notype = true; break;
1052 case 'o': inc_object = true; break;
1053 case 'f': inc_func = true; break;
1054 case 'F': inc_file = true; break;
1055 case 'l': inc_local = true; break;
1056 case 'g': inc_global = true; break;
1057 case 'w': inc_weak = true; break;
1058 case 'd': inc_def = true; break;
1059 case 'u': inc_undef = true; break;
1060 case 'a': inc_abs = true; break;
1061 case 'c': inc_common = true; break;
1062 default: err("invalid symbol selector '%c'", *this_sym);
1063 }
1064 }
1065
1066 /* If no types are matched, not match all */
1067 if (!inc_notype && !inc_object && !inc_func && !inc_file)
1068 inc_notype = inc_object = inc_func = inc_file = true;
1069 if (!inc_local && !inc_global && !inc_weak)
1070 inc_local = inc_global = inc_weak = true;
1071 if (!inc_def && !inc_undef && !inc_abs && !inc_common)
1072 inc_def = inc_undef = inc_abs = inc_common = true;
1073
1074 /* backwards compat for defined/undefined short hand */
1075 } else if (*this_sym == '+') {
1076 inc_undef = false;
1077 ++this_sym;
1078 } else if (*this_sym == '-') {
1079 inc_def = inc_abs = inc_common = false;
1080 ++this_sym;
1081 }
1082
1083 /* filter symbols */
1084 if ((!inc_notype && stt == STT_NOTYPE) || \
1085 (!inc_object && stt == STT_OBJECT) || \
1086 (!inc_func && stt == STT_FUNC ) || \
1087 (!inc_file && stt == STT_FILE ) || \
1088 (!inc_local && stb == STB_LOCAL ) || \
1089 (!inc_global && stb == STB_GLOBAL) || \
1090 (!inc_weak && stb == STB_WEAK ) || \
1091 (!inc_def && shn && shn < SHN_LORESERVE) || \
1092 (!inc_undef && shn == SHN_UNDEF ) || \
1093 (!inc_abs && shn == SHN_ABS ) || \
1094 (!inc_common && shn == SHN_COMMON))
1095 continue;
1096
1097 if (*this_sym == '*') {
1098 /* a "*" symbol gets you debug output */
1099 printf("%s(%s) %5lX %15s %15s %15s %s\n",
1100 ((*found_sym == 0) ? "\n\t" : "\t"),
1101 elf->base_filename,
1102 size,
1103 get_elfstttype(stt),
1104 get_elfstbtype(stb),
1105 get_elfshntype(shn),
1106 symname);
1107 goto matched;
1108
970 } else { 1109 } else {
1110 if (g_match) {
1111 /* regex match the symbol */
1112 if (rematch(this_sym, symname, REG_EXTENDED) != 0)
1113 continue;
1114
1115 } else if (*this_sym) {
1116 /* give empty symbols a "pass", else do a normal compare */
971 const size_t symname_len = strlen(symname); 1117 const size_t len = strlen(this_sym);
972 return (strncmp(symname, tomatch, symname_len) == 0 && 1118 if (!(strncmp(this_sym, symname, len) == 0 &&
973 /* Accept unversioned symbol names */ 1119 /* Accept unversioned symbol names */
974 (tomatch[symname_len] == '\0' || tomatch[symname_len] == '@')); 1120 (symname[len] == '\0' || symname[len] == '@')))
1121 continue;
1122 }
1123
1124 if (be_semi_verbose) {
1125 char buf[1024];
1126 snprintf(buf, sizeof(buf), "%lX %s %s",
1127 size,
1128 get_elfstttype(stt),
1129 this_sym);
1130 *ret = xstrdup(buf);
1131 } else {
1132 if (*ret) xchrcat(ret, ',', ret_len);
1133 xstrcat(ret, symname, ret_len);
1134 }
1135
1136 goto matched;
975 } 1137 }
1138 } while (next_sym);
1139
1140 return;
1141
1142 matched:
1143 *found_sym = 1;
1144 if (next_sym)
1145 next_sym[-1] = saved;
976} 1146}
977 1147
978static char *scanelf_file_sym(elfobj *elf, char *found_sym) 1148static char *scanelf_file_sym(elfobj *elf, char *found_sym)
979{ 1149{
980 unsigned long i; 1150 unsigned long i;
981 char *ret; 1151 char *ret;
982 void *symtab_void, *strtab_void; 1152 void *symtab_void, *strtab_void;
983 1153
984 if (!find_sym) return NULL; 1154 if (!find_sym) return NULL;
985 ret = find_sym; 1155 ret = NULL;
986 1156
987 scanelf_file_get_symtabs(elf, &symtab_void, &strtab_void); 1157 scanelf_file_get_symtabs(elf, &symtab_void, &strtab_void);
988 1158
989 if (symtab_void && strtab_void) { 1159 if (symtab_void && strtab_void) {
990#define FIND_SYM(B) \ 1160#define FIND_SYM(B) \
991 if (elf->elf_class == ELFCLASS ## B) { \ 1161 if (elf->elf_class == ELFCLASS ## B) { \
992 Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \ 1162 Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
993 Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \ 1163 Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
994 Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \ 1164 Elf ## B ## _Sym *sym = SYM ## B (elf->vdata + EGET(symtab->sh_offset)); \
995 unsigned long cnt = EGET(symtab->sh_entsize); \ 1165 unsigned long cnt = EGET(symtab->sh_entsize); \
996 char *symname; \ 1166 char *symname; \
1167 size_t ret_len = 0; \
997 if (cnt) \ 1168 if (cnt) \
998 cnt = EGET(symtab->sh_size) / cnt; \ 1169 cnt = EGET(symtab->sh_size) / cnt; \
999 for (i = 0; i < cnt; ++i) { \ 1170 for (i = 0; i < cnt; ++i) { \
1000 if ((void*)sym > (void*)elf->data_end) { \ 1171 if ((void*)sym > elf->data_end) { \
1001 warnf("%s: corrupt ELF symbols - aborting", elf->filename); \ 1172 warnf("%s: corrupt ELF symbols - aborting", elf->filename); \
1002 goto break_out; \ 1173 goto break_out; \
1003 } \ 1174 } \
1004 if (sym->st_name) { \ 1175 if (sym->st_name) { \
1005 /* make sure the symbol name is in acceptable memory range */ \ 1176 /* make sure the symbol name is in acceptable memory range */ \
1006 symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \ 1177 symname = elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name); \
1007 if ((void*)symname > (void*)elf->data_end) { \ 1178 if ((void*)symname > elf->data_end) { \
1008 warnf("%s: corrupt ELF symbols", elf->filename); \ 1179 warnf("%s: corrupt ELF symbols", elf->filename); \
1009 ++sym; \ 1180 ++sym; \
1010 continue; \ 1181 continue; \
1011 } \ 1182 } \
1012 /* debug display ... show all symbols and some extra info */ \ 1183 scanelf_match_symname(elf, found_sym, \
1013 if (0 && g_match ? rematch(ret, symname, REG_EXTENDED) == 0 : *ret == '*') { \ 1184 &ret, &ret_len, symname, \
1014 printf("%s(%s) %5lX %15s %s %s\n", \ 1185 ELF##B##_ST_TYPE(EGET(sym->st_info)), \
1015 ((*found_sym == 0) ? "\n\t" : "\t"), \ 1186 ELF##B##_ST_BIND(EGET(sym->st_info)), \
1016 elf->base_filename, \ 1187 EGET(sym->st_shndx), \
1017 (unsigned long)sym->st_size, \ 1188 /* st_size can be 64bit, but no one is really that big, so screw em */ \
1018 get_elfstttype(sym->st_info), \ 1189 EGET(sym->st_size)); \
1019 sym->st_shndx == SHN_UNDEF ? "U" : "D", symname); \
1020 *found_sym = 1; \
1021 } else { \
1022 /* allow the user to specify a comma delimited list of symbols to search for */ \
1023 char *this_sym, *next_sym; \
1024 next_sym = ret; \
1025 while (next_sym) { \
1026 this_sym = next_sym; \
1027 if ((next_sym = strchr(this_sym, ','))) \
1028 next_sym += 1; /* Skip the comma */ \
1029 /* do we want a defined symbol ? */ \
1030 if (*this_sym == '+') { \
1031 if (sym->st_shndx == SHN_UNDEF) \
1032 continue; \
1033 ++this_sym; \
1034 /* do we want an undefined symbol ? */ \
1035 } else if (*this_sym == '-') { \
1036 if (sym->st_shndx != SHN_UNDEF) \
1037 continue; \
1038 ++this_sym; \
1039 } \
1040 if (next_sym) /* Copy it so that we don't have to worry about the final , */ \
1041 this_sym = strndup(this_sym, next_sym-this_sym); \
1042 /* ok, lets compare the name now */ \
1043 if (scanelf_match_symname(this_sym, symname)) { \
1044 if (be_semi_verbose) { \
1045 char buf[126]; \
1046 snprintf(buf, sizeof(buf), "%lX %s %s", \
1047 (unsigned long)sym->st_size, get_elfstttype(sym->st_info), this_sym); \
1048 ret = buf; \
1049 } else \
1050 ret = symname; \
1051 (*found_sym)++; \
1052 goto break_out; \
1053 } \
1054 if (next_sym) free(this_sym); \
1055 } \
1056 } \
1057 } \ 1190 } \
1058 ++sym; \ 1191 ++sym; \
1059 } } 1192 } }
1060 FIND_SYM(32) 1193 FIND_SYM(32)
1061 FIND_SYM(64) 1194 FIND_SYM(64)
1100 else 1233 else
1101 return (char *)" - "; 1234 return (char *)" - ";
1102} 1235}
1103 1236
1104/* scan an elf file and show all the fun stuff */ 1237/* scan an elf file and show all the fun stuff */
1105#define prints(str) write(fileno(stdout), str, strlen(str)) 1238#define prints(str) ({ ssize_t ret = write(fileno(stdout), str, strlen(str)); ret; })
1106static int scanelf_elfobj(elfobj *elf) 1239static int scanelf_elfobj(elfobj *elf)
1107{ 1240{
1108 unsigned long i; 1241 unsigned long i;
1109 char found_pax, found_phdr, found_relro, found_load, found_textrel, 1242 char found_pax, found_phdr, found_relro, found_load, found_textrel,
1110 found_rpath, found_needed, found_interp, found_bind, found_soname, 1243 found_rpath, found_needed, found_interp, found_bind, found_soname,
1235 case 'I': out = get_elfosabi(elf); break; 1368 case 'I': out = get_elfosabi(elf); break;
1236 case 'Y': out = get_elf_eabi(elf); break; 1369 case 'Y': out = get_elf_eabi(elf); break;
1237 case 'Z': snprintf(ubuf, sizeof(ubuf), "%lu", (unsigned long)elf->len); out = ubuf; break;; 1370 case 'Z': snprintf(ubuf, sizeof(ubuf), "%lu", (unsigned long)elf->len); out = ubuf; break;;
1238 default: warnf("'%c' has no scan code?", out_format[i]); 1371 default: warnf("'%c' has no scan code?", out_format[i]);
1239 } 1372 }
1240 if (out) { 1373 if (out)
1241 /* hack for comma delimited output like `scanelf -s sym1,sym2,sym3` */
1242 if (out_format[i] == 's' && (tmp=strchr(out,',')) != NULL)
1243 xstrncat(&out_buffer, out, &out_len, (tmp-out));
1244 else
1245 xstrcat(&out_buffer, out, &out_len); 1374 xstrcat(&out_buffer, out, &out_len);
1246 }
1247 } 1375 }
1248 1376
1249#define FOUND_SOMETHING() \ 1377#define FOUND_SOMETHING() \
1250 (found_pax || found_phdr || found_relro || found_load || found_textrel || \ 1378 (found_pax || found_phdr || found_relro || found_load || found_textrel || \
1251 found_rpath || found_needed || found_interp || found_bind || \ 1379 found_rpath || found_needed || found_interp || found_bind || \
1319 ar = ar_open_fd(filename, fd); 1447 ar = ar_open_fd(filename, fd);
1320 if (ar == NULL) 1448 if (ar == NULL)
1321 return 1; 1449 return 1;
1322 1450
1323 ar_buffer = mmap(0, len, PROT_READ | (fix_elf ? PROT_WRITE : 0), (fix_elf ? MAP_SHARED : MAP_PRIVATE), fd, 0); 1451 ar_buffer = mmap(0, len, PROT_READ | (fix_elf ? PROT_WRITE : 0), (fix_elf ? MAP_SHARED : MAP_PRIVATE), fd, 0);
1324 while ((m=ar_next(ar)) != NULL) { 1452 while ((m = ar_next(ar)) != NULL) {
1453 off_t cur_pos = lseek(fd, 0, SEEK_CUR);
1454 if (cur_pos == -1)
1455 errp("lseek() failed");
1325 elf = readelf_buffer(m->name, ar_buffer+lseek(fd,0,SEEK_CUR), m->size); 1456 elf = readelf_buffer(m->name, ar_buffer + cur_pos, m->size);
1326 if (elf) { 1457 if (elf) {
1327 scanelf_elfobj(elf); 1458 scanelf_elfobj(elf);
1328 unreadelf(elf); 1459 unreadelf(elf);
1329 } 1460 }
1330 } 1461 }
1458 while ((fgets(path, __PAX_UTILS_PATH_MAX, fp)) != NULL) { 1589 while ((fgets(path, __PAX_UTILS_PATH_MAX, fp)) != NULL) {
1459 if ((p = strrchr(path, '\r')) != NULL) 1590 if ((p = strrchr(path, '\r')) != NULL)
1460 *p = 0; 1591 *p = 0;
1461 if ((p = strchr(path, '\n')) != NULL) 1592 if ((p = strchr(path, '\n')) != NULL)
1462 *p = 0; 1593 *p = 0;
1463#ifdef __linux__ 1594
1464 /* recursive includes of the same file will make this segfault. */ 1595 /* recursive includes of the same file will make this segfault. */
1465 if ((memcmp(path, "include", 7) == 0) && isblank(path[7])) { 1596 if ((memcmp(path, "include", 7) == 0) && isblank(path[7])) {
1466 glob64_t gl; 1597 glob_t gl;
1467 size_t x; 1598 size_t x;
1468 char gpath[__PAX_UTILS_PATH_MAX]; 1599 char gpath[__PAX_UTILS_PATH_MAX];
1469 1600
1470 memset(gpath, 0, sizeof(gpath)); 1601 memset(gpath, 0, sizeof(gpath));
1471 1602
1472 if (path[8] != '/') 1603 if (path[8] != '/')
1473 snprintf(gpath, sizeof(gpath), "/etc/%s", &path[8]); 1604 snprintf(gpath, sizeof(gpath), "/etc/%s", &path[8]);
1474 else 1605 else
1475 strncpy(gpath, &path[8], sizeof(gpath)); 1606 strncpy(gpath, &path[8], sizeof(gpath));
1476 1607
1477 if ((glob64(gpath, 0, NULL, &gl)) == 0) { 1608 if (glob(gpath, 0, NULL, &gl) == 0) {
1478 for (x = 0; x < gl.gl_pathc; ++x) { 1609 for (x = 0; x < gl.gl_pathc; ++x) {
1479 /* try to avoid direct loops */ 1610 /* try to avoid direct loops */
1480 if (strcmp(gl.gl_pathv[x], fname) == 0) 1611 if (strcmp(gl.gl_pathv[x], fname) == 0)
1481 continue; 1612 continue;
1482 i = load_ld_cache_config(i, gl.gl_pathv[x]); 1613 i = load_ld_cache_config(i, gl.gl_pathv[x]);
1483 if (i + 1 >= ARRAY_SIZE(ldpaths)) { 1614 if (i + 1 >= ARRAY_SIZE(ldpaths)) {
1484 globfree64(&gl); 1615 globfree(&gl);
1485 return i; 1616 return i;
1486 } 1617 }
1487 } 1618 }
1488 globfree64 (&gl); 1619 globfree(&gl);
1489 continue; 1620 continue;
1490 } 1621 }
1491 } 1622 }
1492#endif 1623
1493 if (*path != '/') 1624 if (*path != '/')
1494 continue; 1625 continue;
1495 1626
1496 ldpaths[i++] = xstrdup(path); 1627 ldpaths[i++] = xstrdup(path);
1497 1628
1502 1633
1503 fclose(fp); 1634 fclose(fp);
1504 return i; 1635 return i;
1505} 1636}
1506 1637
1507#elif defined(__FreeBSD__) || (__DragonFly__) 1638#elif defined(__FreeBSD__) || defined(__DragonFly__)
1508 1639
1509static int load_ld_cache_config(int i, const char *fname) 1640static int load_ld_cache_config(int i, const char *fname)
1510{ 1641{
1511 FILE *fp = NULL; 1642 FILE *fp = NULL;
1512 char *b = NULL, *p; 1643 char *b = NULL, *p;
1568 err("Unable to load any paths from ld.so.conf"); 1699 err("Unable to load any paths from ld.so.conf");
1569 1700
1570 scan_l = scan_ul = scan_ull = 0; 1701 scan_l = scan_ul = scan_ull = 0;
1571 1702
1572 while (ldpaths[i]) { 1703 while (ldpaths[i]) {
1573 if (!scan_l && !strcmp(ldpaths[i], "/lib")) scan_l = 1; 1704 if (!scan_l && !strcmp(ldpaths[i], "/lib")) scan_l = 1;
1574 if (!scan_ul && !strcmp(ldpaths[i], "/usr/lib")) scan_ul = 1; 1705 if (!scan_ul && !strcmp(ldpaths[i], "/usr/lib")) scan_ul = 1;
1575 if (!scan_ull && !strcmp(ldpaths[i], "/usr/local/lib")) scan_ull = 1; 1706 if (!scan_ull && !strcmp(ldpaths[i], "/usr/local/lib")) scan_ull = 1;
1576 scanelf_dir(ldpaths[i]); 1707 scanelf_dir(ldpaths[i]);
1577 ++i; 1708 ++i;
1578 } 1709 }
1579 1710
1673 "Print Endianness", 1804 "Print Endianness",
1674 "Print OSABI", 1805 "Print OSABI",
1675 "Print EABI (EM_ARM Only)", 1806 "Print EABI (EM_ARM Only)",
1676 "Print only ELF files matching octal permissions", 1807 "Print only ELF files matching octal permissions",
1677 "Print ELF file size", 1808 "Print ELF file size",
1678 "Print all scanned info (-x -e -t -r -b)\n", 1809 "Print all useful/simple info\n",
1679 "Only output 'bad' things", 1810 "Only output 'bad' things",
1680 "Be verbose (can be specified more than once)", 1811 "Be verbose (can be specified more than once)",
1681 "Use specified format for output", 1812 "Use specified format for output",
1682 "Read input stream from a filename", 1813 "Read input stream from a filename",
1683 "Write output stream to a filename", 1814 "Write output stream to a filename",

Legend:
Removed from v.1.206  
changed lines
  Added in v.1.220

  ViewVC Help
Powered by ViewVC 1.1.20