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

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.20