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

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

  ViewVC Help
Powered by ViewVC 1.1.20