/[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.263 Revision 1.264
1/* 1/*
2 * Copyright 2003-2012 Gentoo Foundation 2 * Copyright 2003-2012 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.263 2014/03/20 08:08:37 vapier Exp $ 4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/Attic/scanelf.c,v 1.264 2014/03/21 05:27:21 vapier Exp $
5 * 5 *
6 * Copyright 2003-2012 Ned Ludd - <solar@gentoo.org> 6 * Copyright 2003-2012 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2004-2012 Mike Frysinger - <vapier@gentoo.org> 7 * Copyright 2004-2012 Mike Frysinger - <vapier@gentoo.org>
8 */ 8 */
9 9
10static const char rcsid[] = "$Id: scanelf.c,v 1.263 2014/03/20 08:08:37 vapier Exp $"; 10static const char rcsid[] = "$Id: scanelf.c,v 1.264 2014/03/21 05:27:21 vapier Exp $";
11const char 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 == '+')
68static unsigned int match_perms = 0; 68static unsigned int match_perms = 0;
69static void *ldcache = NULL; 69static void *ldcache = NULL;
70static size_t ldcache_size = 0; 70static size_t ldcache_size = 0;
71static unsigned long setpax = 0UL; 71static unsigned long setpax = 0UL;
72 72
73static int has_objdump = 0; 73static const char *objdump;
74 74
75/* find the path to a file by name */ 75/* find the path to a file by name */
76static int bin_in_path(const char *fname) 76static const char *which(const char *fname, const char *envvar)
77{ 77{
78 char fullpath[__PAX_UTILS_PATH_MAX]; 78 size_t path_len, fname_len;
79 const char *env_path;
79 char *path, *p; 80 char *path, *p, *ep;
80 81
82 p = getenv(envvar);
83 if (p)
84 return p;
85
81 path = getenv("PATH"); 86 env_path = getenv("PATH");
82 if (!path) 87 if (!env_path)
83 return 0; 88 return NULL;
84 89
85 while ((p = strrchr(path, ':')) != NULL) { 90 /* Create a copy of the $PATH that we can safely modify.
86 snprintf(fullpath, sizeof(fullpath), "%s/%s", p + 1, fname); 91 * Make it a little bigger so we can append "/fname".
87 *p = 0; 92 * We do this twice -- once for a perm copy, and once for
93 * room at the end of the last element. */
94 path_len = strlen(env_path);
95 fname_len = strlen(fname);
96 path = xmalloc(path_len + (fname_len * 2) + 2 + 2);
97 memcpy(path, env_path, path_len + 1);
98
99 p = path + path_len + 1 + fname_len + 1;
100 *p = '/';
101 memcpy(p + 1, fname, fname_len + 1);
102
103 /* Repoint fname to the copy in the env string as it has
104 * the leading slash which we can include in a single memcpy.
105 * Increase the fname len to include the '/' and '\0'. */
106 fname = p;
107 fname_len += 2;
108
109 p = path;
110 while (p) {
111 ep = strchr(p, ':');
112 /* Append the /foo path to the current element. */
113 if (ep)
114 memcpy(ep, fname, fname_len);
115 else
116 memcpy(path + path_len, fname, fname_len);
117
88 if (access(fullpath, R_OK) != -1) 118 if (access(p, R_OK) != -1)
89 return 1; 119 return p;
120
121 p = ep;
122 if (ep) {
123 /* If not the last element, restore the chunk we clobbered. */
124 size_t offset = ep - path;
125 size_t restore = min(path_len - offset, fname_len);
126 memcpy(ep, env_path + offset, restore);
127 ++p;
90 } 128 }
129 }
91 130
131 free(path);
92 return 0; 132 return NULL;
93} 133}
94 134
95static FILE *fopenat_r(int dir_fd, const char *path) 135static FILE *fopenat_r(int dir_fd, const char *path)
96{ 136{
97 int fd = openat(dir_fd, path, O_RDONLY|O_CLOEXEC); 137 int fd = openat(dir_fd, path, O_RDONLY|O_CLOEXEC);
568 else \ 608 else \
569 printf("%s", func_name); \ 609 printf("%s", func_name); \
570 } else \ 610 } else \
571 printf("(optimized out)"); \ 611 printf("(optimized out)"); \
572 printf(" [0x%lX]\n", (unsigned long)offset_tmp); \ 612 printf(" [0x%lX]\n", (unsigned long)offset_tmp); \
573 if (be_verbose && has_objdump) { \ 613 if (be_verbose && objdump) { \
574 Elf ## B ## _Addr end_addr = offset_tmp + EGET(func->st_size); \ 614 Elf ## B ## _Addr end_addr = offset_tmp + EGET(func->st_size); \
575 char *sysbuf; \ 615 char *sysbuf; \
576 size_t syslen; \ 616 size_t syslen; \
577 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"; \ 617 const char sysfmt[] = "%s -r -R -d -w -l --start-address=0x%lX --stop-address=0x%lX %s | grep --color -i -C 3 '.*[[:space:]]%lX:[[:space:]]*R_.*'\n"; \
578 syslen = sizeof(sysfmt) + strlen(elf->filename) + 3 * sizeof(unsigned long) + 1; \ 618 syslen = sizeof(sysfmt) + strlen(objdump) + strlen(elf->filename) + 3 * sizeof(unsigned long) + 1; \
579 sysbuf = xmalloc(syslen); \ 619 sysbuf = xmalloc(syslen); \
580 if (end_addr < r_offset) \ 620 if (end_addr < r_offset) \
581 /* not uncommon when things are optimized out */ \ 621 /* not uncommon when things are optimized out */ \
582 end_addr = r_offset + 0x100; \ 622 end_addr = r_offset + 0x100; \
583 snprintf(sysbuf, syslen, sysfmt, \ 623 snprintf(sysbuf, syslen, sysfmt, \
624 objdump, \
584 (unsigned long)offset_tmp, \ 625 (unsigned long)offset_tmp, \
585 (unsigned long)end_addr, \ 626 (unsigned long)end_addr, \
586 elf->filename, \ 627 elf->filename, \
587 (unsigned long)r_offset); \ 628 (unsigned long)r_offset); \
588 fflush(stdout); \ 629 fflush(stdout); \
2228 default: 2269 default:
2229 err("Unhandled option '%c'; please report this", i); 2270 err("Unhandled option '%c'; please report this", i);
2230 } 2271 }
2231 } 2272 }
2232 if (show_textrels && be_verbose) 2273 if (show_textrels && be_verbose)
2233 has_objdump = bin_in_path("objdump"); 2274 objdump = which("objdump", "OBJDUMP");
2234 /* precompile all the regexes */ 2275 /* precompile all the regexes */
2235 if (g_match) { 2276 if (g_match) {
2236 regex_t preg; 2277 regex_t preg;
2237 const char *this_sym; 2278 const char *this_sym;
2238 size_t n; 2279 size_t n;

Legend:
Removed from v.1.263  
changed lines
  Added in v.1.264

  ViewVC Help
Powered by ViewVC 1.1.20