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

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.35

  ViewVC Help
Powered by ViewVC 1.1.20