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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (hide annotations) (download) (as text)
Tue Apr 19 22:19:26 2005 UTC (9 years, 3 months ago) by vapier
Branch: MAIN
Changes since 1.37: +31 -6 lines
File MIME type: text/x-csrc
add support for displaying an ELFs interp section

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 vapier 1.38 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.37 2005/04/16 06:34:39 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 vapier 1.38 static const char *rcsid = "$Id: scanelf.c,v 1.37 2005/04/16 06:34:39 vapier Exp $";
39 vapier 1.36 #define argv0 "scanelf"
40 vapier 1.10
41    
42    
43     /* prototypes */
44     static void scanelf_file(const char *filename);
45     static void scanelf_dir(const char *path);
46     static void scanelf_ldpath();
47     static void scanelf_envpath();
48     static void usage(int status);
49     static void parseargs(int argc, char *argv[]);
50    
51     /* variables to control behavior */
52     static char scan_ldpath = 0;
53     static char scan_envpath = 0;
54 vapier 1.37 static char scan_symlink = 1;
55 vapier 1.10 static char dir_recurse = 0;
56 vapier 1.14 static char dir_crossmount = 1;
57 vapier 1.10 static char show_pax = 0;
58     static char show_stack = 0;
59     static char show_textrel = 0;
60     static char show_rpath = 0;
61 vapier 1.32 static char show_needed = 0;
62 vapier 1.38 static char show_interp = 0;
63 solar 1.16 static char show_banner = 1;
64 vapier 1.10 static char be_quiet = 0;
65 vapier 1.14 static char be_verbose = 0;
66 vapier 1.27 static char *find_sym = NULL;
67 vapier 1.10
68 solar 1.1
69    
70 vapier 1.10 /* scan an elf file and show all the fun stuff */
71     static void scanelf_file(const char *filename)
72 solar 1.6 {
73 vapier 1.10 int i;
74 vapier 1.32 char found_pax, found_stack, found_relro, found_textrel,
75 vapier 1.38 found_rpath, found_needed, found_interp, found_sym;
76 vapier 1.26 elfobj *elf;
77 solar 1.33 struct stat st;
78 vapier 1.10
79 vapier 1.37 /* make sure 'filename' exists */
80 solar 1.33 if (lstat(filename, &st) == -1)
81     return;
82 vapier 1.37 /* always handle regular files and handle symlinked files if no -y */
83     if (!(S_ISREG(st.st_mode) || (S_ISLNK(st.st_mode) && scan_symlink)))
84 solar 1.33 return;
85 vapier 1.37
86 vapier 1.32 found_pax = found_stack = found_relro = found_textrel = \
87 vapier 1.38 found_rpath = found_needed = found_interp = found_sym = 0;
88 solar 1.12
89 vapier 1.10 /* verify this is real ELF */
90 vapier 1.14 if ((elf = readelf(filename)) == NULL) {
91 vapier 1.26 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
92 vapier 1.10 return;
93 vapier 1.14 }
94    
95 vapier 1.26 if (be_verbose > 1)
96     printf("%s: {%s,%s} scanning file\n", filename,
97     get_elfeitype(elf, EI_CLASS, elf->elf_class),
98     get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
99     else if (be_verbose)
100     printf("%s: scanning file\n", filename);
101 vapier 1.10
102     /* show the header */
103 solar 1.16 if (!be_quiet && show_banner) {
104 vapier 1.32 printf(" TYPE ");
105     if (show_pax) printf(" PAX ");
106     if (show_stack) printf("STK/REL ");
107 vapier 1.23 if (show_textrel) printf("TEXTREL ");
108     if (show_rpath) printf("RPATH ");
109 vapier 1.32 if (show_needed) printf("NEEDED ");
110 vapier 1.38 if (show_interp) printf("INTERP ");
111 vapier 1.23 printf(" FILE\n");
112 solar 1.16 show_banner = 0;
113 vapier 1.10 }
114    
115     /* dump all the good stuff */
116     if (!be_quiet)
117 vapier 1.26 printf("%-7s ", get_elfetype(elf));
118 vapier 1.10
119 vapier 1.14 if (show_pax) {
120     char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
121     if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
122     found_pax = 1;
123     printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf)));
124     }
125     }
126 vapier 1.10
127     /* stack fun */
128     if (show_stack) {
129 vapier 1.26 #define SHOW_STACK(B) \
130     if (elf->elf_class == ELFCLASS ## B) { \
131     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
132     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
133     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
134     if (EGET(phdr[i].p_type) != PT_GNU_STACK && \
135     EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \
136     if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
137     continue; \
138     if (EGET(phdr[i].p_type) == PT_GNU_STACK) \
139     found_stack = 1; \
140     if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \
141     found_relro = 1; \
142     printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \
143     } \
144 vapier 1.10 }
145 vapier 1.26 SHOW_STACK(32)
146     SHOW_STACK(64)
147 vapier 1.23 if (!be_quiet && !found_stack) printf("--- ");
148     if (!be_quiet && !found_relro) printf("--- ");
149 vapier 1.10 }
150    
151     /* textrel fun */
152     if (show_textrel) {
153 vapier 1.26 #define SHOW_TEXTREL(B) \
154     if (elf->elf_class == ELFCLASS ## B) { \
155     Elf ## B ## _Dyn *dyn; \
156     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
157     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
158     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
159     if (phdr[i].p_type != PT_DYNAMIC) continue; \
160     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
161     while (EGET(dyn->d_tag) != DT_NULL) { \
162     if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
163     found_textrel = 1; \
164     /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
165 solar 1.33 fputs("TEXTREL ", stdout); \
166 vapier 1.26 } \
167     ++dyn; \
168     } \
169     } }
170     SHOW_TEXTREL(32)
171     SHOW_TEXTREL(64)
172 solar 1.33 if (!be_quiet && !found_textrel) fputs("------- ", stdout);
173 vapier 1.10 }
174    
175     /* rpath fun */
176     /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
177     if (show_rpath) {
178 vapier 1.23 char *rpath, *runpath;
179 vapier 1.26 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
180 vapier 1.23 rpath = runpath = NULL;
181 vapier 1.10
182 vapier 1.26 if (strtbl_void) {
183     #define SHOW_RPATH(B) \
184     if (elf->elf_class == ELFCLASS ## B) { \
185     Elf ## B ## _Dyn *dyn; \
186     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
187     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
188     Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
189     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
190     if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
191     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
192     while (EGET(dyn->d_tag) != DT_NULL) { \
193     if (EGET(dyn->d_tag) == DT_RPATH) { \
194     rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
195     found_rpath = 1; \
196     } else if (EGET(dyn->d_tag) == DT_RUNPATH) { \
197     runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
198     found_rpath = 1; \
199     } \
200     ++dyn; \
201     } \
202     } }
203     SHOW_RPATH(32)
204     SHOW_RPATH(64)
205 vapier 1.10 }
206 vapier 1.23 if (rpath && runpath) {
207     if (!strcmp(rpath, runpath))
208     printf("%-5s ", runpath);
209     else {
210     fprintf(stderr, "%s's RPATH [%s] != RUNPATH [%s]\n", filename, rpath, runpath);
211     printf("{%s,%s} ", rpath, runpath);
212     }
213     } else if (rpath || runpath)
214     printf("%-5s ", (runpath ? runpath : rpath));
215     else if (!be_quiet && !found_rpath)
216     printf(" - ");
217 vapier 1.10 }
218    
219 vapier 1.32 /* print out all the NEEDED entries */
220     if (show_needed) {
221     char *needed;
222     void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
223    
224     if (strtbl_void) {
225     #define SHOW_NEEDED(B) \
226     if (elf->elf_class == ELFCLASS ## B) { \
227     Elf ## B ## _Dyn *dyn; \
228     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
229     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
230     Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
231     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
232     if (be_verbose && EGET(phdr[i].p_type) == PT_INTERP) { \
233     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
234     printf("%s\n", elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr)); \
235     exit(0); \
236     } \
237     if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
238     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
239     while (EGET(dyn->d_tag) != DT_NULL) { \
240     if (EGET(dyn->d_tag) == DT_NEEDED) { \
241     needed = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
242     if (found_needed) printf(","); \
243     printf("%s", needed); \
244     found_needed = 1; \
245     } \
246     ++dyn; \
247     } \
248     } }
249     SHOW_NEEDED(32)
250     SHOW_NEEDED(64)
251     }
252     if (!be_quiet && !found_needed)
253     printf(" - ");
254     else if (found_needed)
255     printf(" ");
256     }
257    
258 vapier 1.38 /* print out all the INTERP info (i.e. glibc is /lib/ld-linux.so.2) */
259     if (show_interp) {
260     void *strtbl_void = elf_findsecbyname(elf, ".interp");
261    
262     if (strtbl_void) {
263     #define SHOW_INTERP(B) \
264     if (elf->elf_class == ELFCLASS ## B) { \
265     Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
266     printf("%s ", elf->data + EGET(strtbl->sh_offset)); \
267     found_interp = 1; \
268     }
269     SHOW_INTERP(32)
270     SHOW_INTERP(64)
271     }
272     if (!be_quiet && !found_interp)
273     printf(" - ");
274     else if (found_interp)
275     printf(" ");
276     }
277    
278 vapier 1.32 /* search the symbol table for a specified symbol */
279 vapier 1.27 if (find_sym) {
280     void *symtab_void, *strtab_void;
281 vapier 1.32 char *versioned_symname;
282     size_t len;
283 solar 1.28
284 vapier 1.32 len = strlen(find_sym) + 1;
285     versioned_symname = (char *)malloc(sizeof(char) * (len+1));
286     if (!versioned_symname) {
287     warnf("Could not malloc() mem for sym scan");
288     return;
289     }
290 solar 1.28 sprintf(versioned_symname, "%s@", find_sym);
291 vapier 1.32
292 vapier 1.27 symtab_void = elf_findsecbyname(elf, ".symtab");
293     strtab_void = elf_findsecbyname(elf, ".strtab");
294    
295     if (symtab_void && strtab_void) {
296     #define FIND_SYM(B) \
297     if (elf->elf_class == ELFCLASS ## B) { \
298     Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
299     Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
300     Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
301     int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
302     char *symname; \
303     for (i = 0; i < cnt; ++i) { \
304     if (sym->st_name) { \
305     symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
306 solar 1.28 if (*find_sym == '*') { \
307 vapier 1.32 printf("%s(%s) %5lX %15s %s\n", \
308     ((found_sym == 0) ? "\n\t" : "\t"), \
309     (char *)basename(filename), \
310     (long)sym->st_size, \
311     (char *)get_elfstttype(sym->st_info), \
312     symname); \
313     found_sym = 1; \
314     } else if ((strcmp(find_sym, symname) == 0) || \
315     (strncmp(symname, versioned_symname, len) == 0)) \
316 solar 1.28 found_sym++; \
317 vapier 1.27 } \
318     ++sym; \
319     } }
320     FIND_SYM(32)
321     FIND_SYM(64)
322     }
323 solar 1.28 free(versioned_symname);
324 vapier 1.29 if (*find_sym != '*') {
325     if (found_sym)
326     printf(" %s ", find_sym);
327     else if (!be_quiet)
328 solar 1.33 fputs(" - ", stdout);
329 vapier 1.29 }
330 vapier 1.27 }
331 vapier 1.10
332 vapier 1.32 if (!be_quiet || found_pax || found_stack || found_textrel || \
333     found_rpath || found_needed || found_sym)
334 solar 1.33 puts(filename);
335 solar 1.28
336 vapier 1.10 unreadelf(elf);
337 solar 1.6 }
338    
339 solar 1.1 /* scan a directory for ET_EXEC files and print when we find one */
340 vapier 1.10 static void scanelf_dir(const char *path)
341 solar 1.1 {
342 vapier 1.10 register DIR *dir;
343     register struct dirent *dentry;
344 vapier 1.14 struct stat st_top, st;
345 solar 1.21 char buf[_POSIX_PATH_MAX];
346 vapier 1.32 size_t pathlen = 0, len = 0;
347 vapier 1.10
348     /* make sure path exists */
349 vapier 1.14 if (lstat(path, &st_top) == -1)
350 vapier 1.10 return;
351 solar 1.11
352 vapier 1.10 /* ok, if it isn't a directory, assume we can open it */
353 vapier 1.14 if (!S_ISDIR(st_top.st_mode)) {
354 vapier 1.10 scanelf_file(path);
355     return;
356     }
357    
358     /* now scan the dir looking for fun stuff */
359     if ((dir = opendir(path)) == NULL) {
360     warnf("could not opendir %s: %s", path, strerror(errno));
361     return;
362     }
363 vapier 1.15 if (be_verbose) printf("%s: scanning dir\n", path);
364 solar 1.11
365 vapier 1.32 pathlen = strlen(path);
366 vapier 1.10 while ((dentry = readdir(dir))) {
367     if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
368     continue;
369 vapier 1.32 len = (pathlen + 1 + strlen(dentry->d_name) + 1);
370     if (len >= sizeof(buf)) {
371     warnf("Skipping '%s': len > sizeof(buf); %d > %d\n", path, (int)len, (int)sizeof(buf));
372     continue;
373     }
374 solar 1.31 sprintf(buf, "%s/%s", path, dentry->d_name);
375 solar 1.20 if (lstat(buf, &st) != -1) {
376 vapier 1.10 if (S_ISREG(st.st_mode))
377 solar 1.20 scanelf_file(buf);
378 vapier 1.10 else if (dir_recurse && S_ISDIR(st.st_mode)) {
379 vapier 1.14 if (dir_crossmount || (st_top.st_dev == st.st_dev))
380 solar 1.20 scanelf_dir(buf);
381 vapier 1.10 }
382     }
383     }
384     closedir(dir);
385 solar 1.1 }
386    
387 vapier 1.10 /* scan /etc/ld.so.conf for paths */
388     static void scanelf_ldpath()
389     {
390 vapier 1.17 char scan_l, scan_ul, scan_ull;
391 vapier 1.10 char *path, *p;
392     FILE *fp;
393    
394     if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
395     err("Unable to open ld.so.conf: %s", strerror(errno));
396    
397 vapier 1.17 scan_l = scan_ul = scan_ull = 0;
398    
399 solar 1.30 if ((path = malloc(_POSIX_PATH_MAX)) == NULL) {
400     warn("Can not malloc() memory for ldpath scanning");
401     return;
402     }
403 vapier 1.10 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
404     if (*path == '/') {
405     if ((p = strrchr(path, '\r')) != NULL)
406     *p = 0;
407     if ((p = strrchr(path, '\n')) != NULL)
408     *p = 0;
409 vapier 1.17 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
410     if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
411     if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
412 vapier 1.10 scanelf_dir(path);
413     }
414     free(path);
415 vapier 1.32 fclose(fp);
416 vapier 1.10
417 vapier 1.17 if (!scan_l) scanelf_dir("/lib");
418     if (!scan_ul) scanelf_dir("/usr/lib");
419     if (!scan_ull) scanelf_dir("/usr/local/lib");
420 vapier 1.10 }
421 solar 1.1
422 vapier 1.10 /* scan env PATH for paths */
423     static void scanelf_envpath()
424 solar 1.1 {
425 solar 1.34 char *path, *p;
426 vapier 1.10
427     path = getenv("PATH");
428     if (!path)
429     err("PATH is not set in your env !");
430    
431 solar 1.34 if ((path = strdup(path)) == NULL)
432 vapier 1.32 err("strdup failed: %s", strerror(errno));
433 vapier 1.10
434     while ((p = strrchr(path, ':')) != NULL) {
435     scanelf_dir(p + 1);
436     *p = 0;
437     }
438 vapier 1.17
439 solar 1.34 free(path);
440 solar 1.1 }
441    
442    
443 vapier 1.10
444     /* usage / invocation handling functions */
445 vapier 1.38 #define PARSE_FLAGS "plRmyxetrnis:aqvo:BhV"
446 vapier 1.27 #define a_argument required_argument
447 vapier 1.10 static struct option const long_opts[] = {
448     {"path", no_argument, NULL, 'p'},
449     {"ldpath", no_argument, NULL, 'l'},
450     {"recursive", no_argument, NULL, 'R'},
451 vapier 1.14 {"mount", no_argument, NULL, 'm'},
452 vapier 1.37 {"symlink", no_argument, NULL, 'y'},
453 vapier 1.10 {"pax", no_argument, NULL, 'x'},
454 solar 1.16 {"header", no_argument, NULL, 'e'},
455 vapier 1.10 {"textrel", no_argument, NULL, 't'},
456     {"rpath", no_argument, NULL, 'r'},
457 vapier 1.32 {"needed", no_argument, NULL, 'n'},
458 vapier 1.38 {"interp", no_argument, NULL, 'i'},
459 vapier 1.27 {"symbol", a_argument, NULL, 's'},
460 vapier 1.10 {"all", no_argument, NULL, 'a'},
461     {"quiet", no_argument, NULL, 'q'},
462 vapier 1.14 {"verbose", no_argument, NULL, 'v'},
463 vapier 1.27 {"file", a_argument, NULL, 'o'},
464 solar 1.16 {"nobanner", no_argument, NULL, 'B'},
465 vapier 1.10 {"help", no_argument, NULL, 'h'},
466     {"version", no_argument, NULL, 'V'},
467     {NULL, no_argument, NULL, 0x0}
468     };
469     static char *opts_help[] = {
470     "Scan all directories in PATH environment",
471     "Scan all directories in /etc/ld.so.conf",
472 vapier 1.14 "Scan directories recursively",
473 vapier 1.37 "Don't recursively cross mount points",
474     "Don't scan symlinks\n",
475 vapier 1.10 "Print PaX markings",
476     "Print GNU_STACK markings",
477     "Print TEXTREL information",
478     "Print RPATH information",
479 vapier 1.32 "Print NEEDED information",
480 vapier 1.38 "Print INTERP information",
481 vapier 1.27 "Find a specified symbol",
482 solar 1.16 "Print all scanned info (-x -e -t -r)\n",
483 vapier 1.14 "Only output 'bad' things",
484     "Be verbose (can be specified more than once)",
485 vapier 1.24 "Write output stream to a filename",
486 vapier 1.14 "Don't display the header",
487 vapier 1.10 "Print this help and exit",
488     "Print version and exit",
489     NULL
490     };
491    
492     /* display usage and exit */
493     static void usage(int status)
494 solar 1.1 {
495 vapier 1.10 int i;
496 vapier 1.37 printf(" Scan ELF binaries for stuff\n\n"
497 solar 1.35 "Usage: %s [options] <dir1/file1> [dir2 dirN fileN ...]\n\n", argv0);
498     printf("Options: -[%s]\n", PARSE_FLAGS);
499 vapier 1.10 for (i = 0; long_opts[i].name; ++i)
500 vapier 1.27 if (long_opts[i].has_arg == no_argument)
501     printf(" -%c, --%-13s %s\n", long_opts[i].val,
502     long_opts[i].name, opts_help[i]);
503     else
504     printf(" -%c, --%-6s <arg> %s\n", long_opts[i].val,
505     long_opts[i].name, opts_help[i]);
506 vapier 1.10 exit(status);
507 solar 1.1 }
508    
509     /* parse command line arguments and preform needed actions */
510 vapier 1.10 static void parseargs(int argc, char *argv[])
511     {
512     int flag;
513    
514     opterr = 0;
515     while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
516     switch (flag) {
517    
518     case 'V': /* version info */
519 solar 1.19 printf("%s compiled %s\n%s\n"
520     "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
521     __FILE__, __DATE__, rcsid, argv0);
522 vapier 1.10 exit(EXIT_SUCCESS);
523     break;
524     case 'h': usage(EXIT_SUCCESS); break;
525    
526 vapier 1.24 case 'o': {
527 solar 1.21 FILE *fp = NULL;
528     fp = freopen(optarg, "w", stdout);
529 vapier 1.24 if (fp == NULL)
530     err("Could not open output stream '%s': %s", optarg, strerror(errno));
531     stdout = fp;
532 solar 1.21 break;
533     }
534 vapier 1.24
535 vapier 1.27 case 's': find_sym = strdup(optarg); break;
536    
537 vapier 1.37 case 'y': scan_symlink = 0; break;
538 solar 1.16 case 'B': show_banner = 0; break;
539 vapier 1.10 case 'l': scan_ldpath = 1; break;
540     case 'p': scan_envpath = 1; break;
541     case 'R': dir_recurse = 1; break;
542 vapier 1.14 case 'm': dir_crossmount = 0; break;
543 vapier 1.10 case 'x': show_pax = 1; break;
544 solar 1.16 case 'e': show_stack = 1; break;
545 vapier 1.10 case 't': show_textrel = 1; break;
546     case 'r': show_rpath = 1; break;
547 vapier 1.32 case 'n': show_needed = 1; break;
548 vapier 1.38 case 'i': show_interp = 1; break;
549 vapier 1.10 case 'q': be_quiet = 1; break;
550 vapier 1.14 case 'v': be_verbose = (be_verbose % 20) + 1; break;
551 vapier 1.38 case 'a': show_pax = show_stack = show_textrel = show_rpath = show_needed = show_interp = 1; break;
552 vapier 1.10
553     case ':':
554     warn("Option missing parameter");
555     usage(EXIT_FAILURE);
556     break;
557     case '?':
558     warn("Unknown option");
559     usage(EXIT_FAILURE);
560     break;
561     default:
562     err("Unhandled option '%c'", flag);
563     break;
564     }
565     }
566    
567 vapier 1.14 if (be_quiet && be_verbose)
568     err("You can be quiet or you can be verbose, not both, stupid");
569    
570 vapier 1.10 if (scan_ldpath) scanelf_ldpath();
571     if (scan_envpath) scanelf_envpath();
572 vapier 1.25 if (optind == argc && !scan_ldpath && !scan_envpath)
573     err("Nothing to scan !?");
574 vapier 1.10 while (optind < argc)
575     scanelf_dir(argv[optind++]);
576 vapier 1.27
577     if (find_sym) free(find_sym);
578 vapier 1.10 }
579    
580    
581    
582     int main(int argc, char *argv[])
583 solar 1.1 {
584 vapier 1.10 if (argc < 2)
585     usage(EXIT_FAILURE);
586     parseargs(argc, argv);
587 solar 1.21 fclose(stdout);
588 vapier 1.10 return EXIT_SUCCESS;
589 solar 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20