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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (hide annotations) (download) (as text)
Tue Apr 5 03:41:27 2005 UTC (9 years, 7 months ago) by solar
Branch: MAIN
Changes since 1.27: +23 -13 lines
File MIME type: text/x-csrc
- refine symbol formatting

1 solar 1.1 /*
2     * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 vapier 1.8 * Copyright 1999-2005 Gentoo Foundation
4 solar 1.1 * Distributed under the terms of the GNU General Public License v2
5 solar 1.28 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.27 2005/04/05 01:44:08 vapier Exp $
6 solar 1.1 *
7     ********************************************************************
8     * This program is free software; you can redistribute it and/or
9     * modify it under the terms of the GNU General Public License as
10     * published by the Free Software Foundation; either version 2 of the
11     * License, or (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful, but
14     * WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     * General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21     * MA 02111-1307, USA.
22     */
23    
24     #include <stdio.h>
25     #include <stdlib.h>
26     #include <sys/types.h>
27 solar 1.28 #define __USE_GNU
28 solar 1.1 #include <string.h>
29 vapier 1.10 #include <errno.h>
30 solar 1.1 #include <unistd.h>
31     #include <sys/stat.h>
32     #include <dirent.h>
33     #include <getopt.h>
34 solar 1.20 #include <assert.h>
35 solar 1.21
36 solar 1.1 #include "paxelf.h"
37    
38 solar 1.28 static const char *rcsid = "$Id: scanelf.c,v 1.27 2005/04/05 01:44:08 vapier Exp $";
39 vapier 1.10
40    
41     /* helper functions for showing errors */
42 vapier 1.18 #define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
43 vapier 1.10 #define warn(fmt, args...) \
44     fprintf(stderr, "%s: " fmt "\n", argv0, ## args)
45     #define warnf(fmt, args...) warn("%s(): " fmt, __FUNCTION__, ## args)
46     #define err(fmt, args...) \
47     do { \
48     warn(fmt, ## args); \
49     exit(EXIT_FAILURE); \
50     } while (0)
51    
52    
53    
54     /* prototypes */
55     static void scanelf_file(const char *filename);
56     static void scanelf_dir(const char *path);
57     static void scanelf_ldpath();
58     static void scanelf_envpath();
59     static void usage(int status);
60     static void parseargs(int argc, char *argv[]);
61    
62     /* variables to control behavior */
63     static char scan_ldpath = 0;
64     static char scan_envpath = 0;
65     static char dir_recurse = 0;
66 vapier 1.14 static char dir_crossmount = 1;
67 vapier 1.10 static char show_pax = 0;
68     static char show_stack = 0;
69     static char show_textrel = 0;
70     static char show_rpath = 0;
71 solar 1.16 static char show_banner = 1;
72 vapier 1.10 static char be_quiet = 0;
73 vapier 1.14 static char be_verbose = 0;
74 vapier 1.27 static char *find_sym = NULL;
75 vapier 1.10
76 solar 1.1
77    
78 vapier 1.10 /* scan an elf file and show all the fun stuff */
79     static void scanelf_file(const char *filename)
80 solar 1.6 {
81 vapier 1.10 int i;
82 vapier 1.27 char found_pax, found_stack, found_relro, found_textrel, found_rpath, found_sym;
83 vapier 1.26 elfobj *elf;
84 vapier 1.10
85 vapier 1.27 found_pax = found_stack = found_relro = found_textrel = found_rpath = found_sym = 0;
86 solar 1.12
87 vapier 1.10 /* verify this is real ELF */
88 vapier 1.14 if ((elf = readelf(filename)) == NULL) {
89 vapier 1.26 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
90 vapier 1.10 return;
91 vapier 1.14 }
92    
93 vapier 1.26 if (be_verbose > 1)
94     printf("%s: {%s,%s} scanning file\n", filename,
95     get_elfeitype(elf, EI_CLASS, elf->elf_class),
96     get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
97     else if (be_verbose)
98     printf("%s: scanning file\n", filename);
99 vapier 1.10
100     /* show the header */
101 solar 1.16 if (!be_quiet && show_banner) {
102 vapier 1.23 printf(" TYPE ");
103     if (show_pax) printf(" PAX ");
104     if (show_stack) printf(" STK/REL ");
105     if (show_textrel) printf("TEXTREL ");
106     if (show_rpath) printf("RPATH ");
107     printf(" FILE\n");
108 solar 1.16 show_banner = 0;
109 vapier 1.10 }
110    
111     /* dump all the good stuff */
112     if (!be_quiet)
113 vapier 1.26 printf("%-7s ", get_elfetype(elf));
114 vapier 1.10
115 vapier 1.14 if (show_pax) {
116     char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
117     if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
118     found_pax = 1;
119     printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf)));
120     }
121     }
122 vapier 1.10
123     /* stack fun */
124     if (show_stack) {
125 vapier 1.26 #define SHOW_STACK(B) \
126     if (elf->elf_class == ELFCLASS ## B) { \
127     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
128     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
129     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
130     if (EGET(phdr[i].p_type) != PT_GNU_STACK && \
131     EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \
132     if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
133     continue; \
134     if (EGET(phdr[i].p_type) == PT_GNU_STACK) \
135     found_stack = 1; \
136     if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \
137     found_relro = 1; \
138     printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \
139     } \
140 vapier 1.10 }
141 vapier 1.26 SHOW_STACK(32)
142     SHOW_STACK(64)
143 vapier 1.23 if (!be_quiet && !found_stack) printf("--- ");
144     if (!be_quiet && !found_relro) printf("--- ");
145 vapier 1.10 }
146    
147     /* textrel fun */
148     if (show_textrel) {
149 vapier 1.26 #define SHOW_TEXTREL(B) \
150     if (elf->elf_class == ELFCLASS ## B) { \
151     Elf ## B ## _Dyn *dyn; \
152     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
153     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
154     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
155     if (phdr[i].p_type != PT_DYNAMIC) continue; \
156     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
157     while (EGET(dyn->d_tag) != DT_NULL) { \
158     if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
159     found_textrel = 1; \
160     /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
161     printf("TEXTREL "); \
162     } \
163     ++dyn; \
164     } \
165     } }
166     SHOW_TEXTREL(32)
167     SHOW_TEXTREL(64)
168 vapier 1.23 if (!be_quiet && !found_textrel) printf("------- ");
169 vapier 1.10 }
170    
171     /* rpath fun */
172     /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
173     if (show_rpath) {
174 vapier 1.23 char *rpath, *runpath;
175 vapier 1.26 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
176 vapier 1.23 rpath = runpath = NULL;
177 vapier 1.10
178 vapier 1.26 if (strtbl_void) {
179     #define SHOW_RPATH(B) \
180     if (elf->elf_class == ELFCLASS ## B) { \
181     Elf ## B ## _Dyn *dyn; \
182     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
183     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
184     Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
185     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
186     if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
187     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
188     while (EGET(dyn->d_tag) != DT_NULL) { \
189     if (EGET(dyn->d_tag) == DT_RPATH) { \
190     rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
191     found_rpath = 1; \
192     } else if (EGET(dyn->d_tag) == DT_RUNPATH) { \
193     runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
194     found_rpath = 1; \
195     } \
196     ++dyn; \
197     } \
198     } }
199     SHOW_RPATH(32)
200     SHOW_RPATH(64)
201 vapier 1.10 }
202 vapier 1.23 if (rpath && runpath) {
203     if (!strcmp(rpath, runpath))
204     printf("%-5s ", runpath);
205     else {
206     fprintf(stderr, "%s's RPATH [%s] != RUNPATH [%s]\n", filename, rpath, runpath);
207     printf("{%s,%s} ", rpath, runpath);
208     }
209     } else if (rpath || runpath)
210     printf("%-5s ", (runpath ? runpath : rpath));
211     else if (!be_quiet && !found_rpath)
212     printf(" - ");
213 vapier 1.10 }
214    
215 vapier 1.27 if (find_sym) {
216     void *symtab_void, *strtab_void;
217 solar 1.28 char found_sym = 0;
218     char *versioned_symname = malloc(strlen(find_sym)+1);
219    
220     sprintf(versioned_symname, "%s@", find_sym);
221 vapier 1.27 symtab_void = elf_findsecbyname(elf, ".symtab");
222     strtab_void = elf_findsecbyname(elf, ".strtab");
223    
224     if (symtab_void && strtab_void) {
225     #define FIND_SYM(B) \
226     if (elf->elf_class == ELFCLASS ## B) { \
227     Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
228     Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
229     Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
230     int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
231     char *symname; \
232     for (i = 0; i < cnt; ++i) { \
233     if (sym->st_name) { \
234     symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
235 solar 1.28 if (*find_sym == '*') { \
236     printf("%s(%s) %5lX %15s %s\n", ((found_sym == 0) ? "\n\t" : "\t"), \
237     (char *) basename(filename), \
238     (long)sym->st_size, (char *) get_elfstttype(sym->st_info & 0xF), \
239     symname); \
240     found_sym = 1; \
241     } \
242     if ((strcmp(find_sym, symname) == 0) || \
243     (strncmp(symname, versioned_symname, strlen(versioned_symname)) == 0)) \
244     found_sym++; \
245 vapier 1.27 } \
246     ++sym; \
247     } }
248     FIND_SYM(32)
249     FIND_SYM(64)
250     }
251 solar 1.28 free(versioned_symname);
252     if (*find_sym != '*')
253     printf(" %s ", (found_sym == 0) ? "-" : find_sym);
254 vapier 1.27 }
255 vapier 1.10
256 solar 1.28 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
257     printf("%s\n", filename);
258    
259 vapier 1.10 unreadelf(elf);
260 solar 1.6 }
261    
262 solar 1.1 /* scan a directory for ET_EXEC files and print when we find one */
263 vapier 1.10 static void scanelf_dir(const char *path)
264 solar 1.1 {
265 vapier 1.10 register DIR *dir;
266     register struct dirent *dentry;
267 vapier 1.14 struct stat st_top, st;
268 solar 1.21 char buf[_POSIX_PATH_MAX];
269 solar 1.20 size_t len = 0;
270 vapier 1.10
271     /* make sure path exists */
272 vapier 1.14 if (lstat(path, &st_top) == -1)
273 vapier 1.10 return;
274 solar 1.11
275 vapier 1.10 /* ok, if it isn't a directory, assume we can open it */
276 vapier 1.14 if (!S_ISDIR(st_top.st_mode)) {
277 vapier 1.10 scanelf_file(path);
278     return;
279     }
280    
281     /* now scan the dir looking for fun stuff */
282     if ((dir = opendir(path)) == NULL) {
283     warnf("could not opendir %s: %s", path, strerror(errno));
284     return;
285     }
286 vapier 1.15 if (be_verbose) printf("%s: scanning dir\n", path);
287 solar 1.11
288 vapier 1.10 while ((dentry = readdir(dir))) {
289     if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
290     continue;
291     len = (strlen(path) + 2 + strlen(dentry->d_name));
292 solar 1.20 assert(len < sizeof(buf));
293     strncpy(buf, path, len);
294     strncat(buf, "/", len);
295     strncat(buf, dentry->d_name, len);
296     buf[sizeof(buf)] = 0;
297     if (lstat(buf, &st) != -1) {
298 vapier 1.10 if (S_ISREG(st.st_mode))
299 solar 1.20 scanelf_file(buf);
300 vapier 1.10 else if (dir_recurse && S_ISDIR(st.st_mode)) {
301 vapier 1.14 if (dir_crossmount || (st_top.st_dev == st.st_dev))
302 solar 1.20 scanelf_dir(buf);
303 vapier 1.10 }
304     }
305     }
306     closedir(dir);
307 solar 1.1 }
308    
309 vapier 1.10 /* scan /etc/ld.so.conf for paths */
310     static void scanelf_ldpath()
311     {
312 vapier 1.17 char scan_l, scan_ul, scan_ull;
313 vapier 1.10 char *path, *p;
314     FILE *fp;
315    
316     if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
317     err("Unable to open ld.so.conf: %s", strerror(errno));
318    
319 vapier 1.17 scan_l = scan_ul = scan_ull = 0;
320    
321 vapier 1.10 path = malloc(_POSIX_PATH_MAX);
322     while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
323     if (*path == '/') {
324     if ((p = strrchr(path, '\r')) != NULL)
325     *p = 0;
326     if ((p = strrchr(path, '\n')) != NULL)
327     *p = 0;
328 vapier 1.17 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
329     if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
330     if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
331 vapier 1.10 scanelf_dir(path);
332     }
333     free(path);
334    
335 vapier 1.17 if (!scan_l) scanelf_dir("/lib");
336     if (!scan_ul) scanelf_dir("/usr/lib");
337     if (!scan_ull) scanelf_dir("/usr/local/lib");
338    
339 vapier 1.10 fclose(fp);
340     }
341 solar 1.1
342 vapier 1.10 /* scan env PATH for paths */
343     static void scanelf_envpath()
344 solar 1.1 {
345 vapier 1.10 char *path, *p;
346    
347     path = getenv("PATH");
348     if (!path)
349     err("PATH is not set in your env !");
350    
351     if ((path = strdup(path)) == NULL)
352     err("stdup failed: %s", strerror(errno));
353    
354     while ((p = strrchr(path, ':')) != NULL) {
355     scanelf_dir(p + 1);
356     *p = 0;
357     }
358 vapier 1.17
359 vapier 1.10 free(path);
360 solar 1.1 }
361    
362    
363 vapier 1.10
364     /* usage / invocation handling functions */
365 vapier 1.27 #define PARSE_FLAGS "plRmxetrs:aqvo:BhV"
366     #define a_argument required_argument
367 vapier 1.10 static struct option const long_opts[] = {
368     {"path", no_argument, NULL, 'p'},
369     {"ldpath", no_argument, NULL, 'l'},
370     {"recursive", no_argument, NULL, 'R'},
371 vapier 1.14 {"mount", no_argument, NULL, 'm'},
372 vapier 1.10 {"pax", no_argument, NULL, 'x'},
373 solar 1.16 {"header", no_argument, NULL, 'e'},
374 vapier 1.10 {"textrel", no_argument, NULL, 't'},
375     {"rpath", no_argument, NULL, 'r'},
376 vapier 1.27 {"symbol", a_argument, NULL, 's'},
377 vapier 1.10 {"all", no_argument, NULL, 'a'},
378     {"quiet", no_argument, NULL, 'q'},
379 vapier 1.14 {"verbose", no_argument, NULL, 'v'},
380 vapier 1.27 {"file", a_argument, NULL, 'o'},
381 solar 1.16 {"nobanner", no_argument, NULL, 'B'},
382 vapier 1.10 {"help", no_argument, NULL, 'h'},
383     {"version", no_argument, NULL, 'V'},
384     {NULL, no_argument, NULL, 0x0}
385     };
386     static char *opts_help[] = {
387     "Scan all directories in PATH environment",
388     "Scan all directories in /etc/ld.so.conf",
389 vapier 1.14 "Scan directories recursively",
390     "Don't recursively cross mount points\n",
391 vapier 1.10 "Print PaX markings",
392     "Print GNU_STACK markings",
393     "Print TEXTREL information",
394     "Print RPATH information",
395 vapier 1.27 "Find a specified symbol",
396 solar 1.16 "Print all scanned info (-x -e -t -r)\n",
397 vapier 1.14 "Only output 'bad' things",
398     "Be verbose (can be specified more than once)",
399 vapier 1.24 "Write output stream to a filename",
400 vapier 1.14 "Don't display the header",
401 vapier 1.10 "Print this help and exit",
402     "Print version and exit",
403     NULL
404     };
405    
406     /* display usage and exit */
407     static void usage(int status)
408 solar 1.1 {
409 vapier 1.10 int i;
410 vapier 1.27 printf(" Scan ELF binaries for stuff\n"
411 vapier 1.10 "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
412 vapier 1.23 printf("Options:\n");
413 vapier 1.10 for (i = 0; long_opts[i].name; ++i)
414 vapier 1.27 if (long_opts[i].has_arg == no_argument)
415     printf(" -%c, --%-13s %s\n", long_opts[i].val,
416     long_opts[i].name, opts_help[i]);
417     else
418     printf(" -%c, --%-6s <arg> %s\n", long_opts[i].val,
419     long_opts[i].name, opts_help[i]);
420 vapier 1.10 exit(status);
421 solar 1.1 }
422    
423     /* parse command line arguments and preform needed actions */
424 vapier 1.10 static void parseargs(int argc, char *argv[])
425     {
426     int flag;
427    
428     opterr = 0;
429     while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
430     switch (flag) {
431    
432     case 'V': /* version info */
433 solar 1.19 printf("%s compiled %s\n%s\n"
434     "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
435     __FILE__, __DATE__, rcsid, argv0);
436 vapier 1.10 exit(EXIT_SUCCESS);
437     break;
438     case 'h': usage(EXIT_SUCCESS); break;
439    
440 vapier 1.24 case 'o': {
441 solar 1.21 FILE *fp = NULL;
442     fp = freopen(optarg, "w", stdout);
443 vapier 1.24 if (fp == NULL)
444     err("Could not open output stream '%s': %s", optarg, strerror(errno));
445     stdout = fp;
446 solar 1.21 break;
447     }
448 vapier 1.24
449 vapier 1.27 case 's': find_sym = strdup(optarg); break;
450    
451 solar 1.16 case 'B': show_banner = 0; break;
452 vapier 1.10 case 'l': scan_ldpath = 1; break;
453     case 'p': scan_envpath = 1; break;
454     case 'R': dir_recurse = 1; break;
455 vapier 1.14 case 'm': dir_crossmount = 0; break;
456 vapier 1.10 case 'x': show_pax = 1; break;
457 solar 1.16 case 'e': show_stack = 1; break;
458 vapier 1.10 case 't': show_textrel = 1; break;
459     case 'r': show_rpath = 1; break;
460     case 'q': be_quiet = 1; break;
461 vapier 1.14 case 'v': be_verbose = (be_verbose % 20) + 1; break;
462 vapier 1.10 case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break;
463    
464     case ':':
465     warn("Option missing parameter");
466     usage(EXIT_FAILURE);
467     break;
468     case '?':
469     warn("Unknown option");
470     usage(EXIT_FAILURE);
471     break;
472     default:
473     err("Unhandled option '%c'", flag);
474     break;
475     }
476     }
477    
478 vapier 1.14 if (be_quiet && be_verbose)
479     err("You can be quiet or you can be verbose, not both, stupid");
480    
481 vapier 1.10 if (scan_ldpath) scanelf_ldpath();
482     if (scan_envpath) scanelf_envpath();
483 vapier 1.25 if (optind == argc && !scan_ldpath && !scan_envpath)
484     err("Nothing to scan !?");
485 vapier 1.10 while (optind < argc)
486     scanelf_dir(argv[optind++]);
487 vapier 1.27
488     if (find_sym) free(find_sym);
489 vapier 1.10 }
490    
491    
492    
493     int main(int argc, char *argv[])
494 solar 1.1 {
495 vapier 1.10 if (argc < 2)
496     usage(EXIT_FAILURE);
497     parseargs(argc, argv);
498 solar 1.21 fclose(stdout);
499 vapier 1.10 return EXIT_SUCCESS;
500 solar 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20